Search in sources :

Example 16 with Occurrence

use of mb.nabl2.scopegraph.terms.Occurrence in project nabl by metaborg.

the class PolymorphismComponent method solve.

// ------------------------------------------------------------------------------------------------------//
private Optional<SolveResult> solve(CGeneralize gen) {
    final ITerm declTerm = unifier().findRecursive(gen.getDeclaration());
    if (!declTerm.isGround()) {
        return Optional.empty();
    }
    final Occurrence decl = Occurrence.matcher().match(declTerm, unifier()).orElseThrow(() -> new TypeException("Expected an occurrence as first argument to " + gen));
    final ITerm type = gen.getType();
    if (!isGenSafe.test(type)) {
        return Optional.empty();
    }
    // linked map to preserve key order
    final Map<ITermVar, TypeVar> subst = Maps.newLinkedHashMap();
    final ITerm scheme;
    {
        int c = 0;
        for (ITermVar var : type.getVars()) {
            subst.put(var, ImmutableTypeVar.of("T" + (++c)));
        }
        scheme = subst.isEmpty() ? type : ImmutableForall.of(subst.values(), subst(type, subst));
    }
    SolveResult result = SolveResult.constraints(// @formatter:off
    ImmutableCDeclProperty.of(decl, DeclProperties.TYPE_KEY, scheme, 0, gen.getMessageInfo()), ImmutableCEqual.of(gen.getGenVars(), B.newList(subst.keySet()), gen.getMessageInfo()));
    return Optional.of(result);
}
Also used : ImmutableTypeVar(mb.nabl2.poly.ImmutableTypeVar) TypeVar(mb.nabl2.poly.TypeVar) SolveResult(mb.nabl2.solver.ISolver.SolveResult) ITermVar(mb.nabl2.terms.ITermVar) ITerm(mb.nabl2.terms.ITerm) TypeException(mb.nabl2.solver.TypeException) Occurrence(mb.nabl2.scopegraph.terms.Occurrence)

Example 17 with Occurrence

use of mb.nabl2.scopegraph.terms.Occurrence in project nabl by metaborg.

the class ScopeGraphComponent method solve.

private boolean solve(CGRef c) {
    final ITerm scopeTerm = unifier().findRecursive((c.getScope()));
    final ITerm refTerm = unifier().findRecursive((c.getReference()));
    if (!(scopeTerm.isGround() && refTerm.isGround())) {
        return false;
    }
    Occurrence ref = Occurrence.matcher().match(refTerm, unifier()).orElseThrow(() -> new TypeException("Expected an occurrence as first argument to " + c));
    Scope scope = Scope.matcher().match(scopeTerm, unifier()).orElseThrow(() -> new TypeException("Expected a scope as second argument to " + c));
    scopeGraph.addRef(ref, scope);
    return true;
}
Also used : Scope(mb.nabl2.scopegraph.terms.Scope) ITerm(mb.nabl2.terms.ITerm) TypeException(mb.nabl2.solver.TypeException) Occurrence(mb.nabl2.scopegraph.terms.Occurrence)

Example 18 with Occurrence

use of mb.nabl2.scopegraph.terms.Occurrence in project nabl by metaborg.

the class SingleFileSolver method solve.

public ISolution solve(GraphSolution initial, Function1<String, String> fresh, ICancel cancel, IProgress progress) throws SolverException, InterruptedException {
    final SolverConfig config = initial.config();
    // shared
    final Ref<IUnifier.Immutable> unifier = new Ref<>(initial.unifier());
    // constraint set properties
    final ActiveVars activeVars = new ActiveVars(unifier);
    final ActiveDeclTypes activeDeclTypes = new ActiveDeclTypes(unifier);
    final HasRelationBuildConstraints hasRelationBuildConstraints = new HasRelationBuildConstraints();
    // guards
    final Predicate1<String> isRelationComplete = r -> !hasRelationBuildConstraints.contains(r);
    // more shared
    final IEsopScopeGraph.Transient<Scope, Label, Occurrence, ITerm> scopeGraph = initial.scopeGraph().melt();
    final IEsopNameResolution<Scope, Label, Occurrence> nameResolution = EsopNameResolution.of(config.getResolutionParams(), scopeGraph, (s, l) -> true);
    // solver components
    final SolverCore core = new SolverCore(config, unifier, fresh, callExternal);
    final BaseComponent baseSolver = new BaseComponent(core);
    final EqualityComponent equalitySolver = new EqualityComponent(core, unifier);
    final NameResolutionComponent nameResolutionSolver = new NameResolutionComponent(core, scopeGraph, nameResolution, Properties.Transient.of());
    final NameSetsComponent nameSetSolver = new NameSetsComponent(core, scopeGraph, nameResolution);
    final RelationComponent relationSolver = new RelationComponent(core, isRelationComplete, config.getFunctions(), VariantRelations.transientOf(config.getRelations()));
    final SetComponent setSolver = new SetComponent(core, nameSetSolver.nameSets());
    final SymbolicComponent symSolver = new SymbolicComponent(core, SymbolicConstraints.of());
    final ControlFlowComponent cfgSolver = new ControlFlowComponent(core, ImmutableFlowSpecSolution.of());
    // polymorphism solver
    final PolySafe polySafe = new PolySafe(activeVars, activeDeclTypes, nameResolutionSolver);
    final PolymorphismComponent polySolver = new PolymorphismComponent(core, polySafe::isGenSafe, polySafe::isInstSafe, nameResolutionSolver::getProperty);
    final ISolver component = c -> c.matchOrThrow(IConstraint.CheckedCases.<Optional<SolveResult>, InterruptedException>builder().onBase(baseSolver::solve).onEquality(equalitySolver::solve).onNameResolution(nameResolutionSolver::solve).onPoly(polySolver::solve).onRelation(relationSolver::solve).onSet(setSolver::solve).onSym(symSolver::solve).onControlflow(cfgSolver::solve).otherwise(ISolver.deny("Not allowed in this phase")));
    final FixedPointSolver solver = new FixedPointSolver(cancel, progress, component, Iterables2.from(activeVars, hasRelationBuildConstraints));
    solver.step().subscribe(r -> {
        if (!r.unifierDiff().isEmpty()) {
            try {
                nameResolutionSolver.update();
            } catch (InterruptedException ex) {
            // ignore here
            }
        }
    });
    try {
        nameResolutionSolver.update();
        SolveResult solveResult = solver.solve(initial.constraints());
        final IMessages.Transient messages = initial.messages().melt();
        messages.addAll(solveResult.messages());
        NameResolutionResult nameResolutionResult = nameResolutionSolver.finish();
        IUnifier.Immutable unifierResult = equalitySolver.finish();
        Map<String, IVariantRelation.Immutable<ITerm>> relationResult = relationSolver.finish();
        ISymbolicConstraints symbolicConstraints = symSolver.finish();
        IFlowSpecSolution<CFGNode> fsSolution = cfgSolver.finish();
        return ImmutableSolution.of(config, initial.astProperties(), nameResolutionResult.scopeGraph(), nameResolutionResult.declProperties(), relationResult, unifierResult, symbolicConstraints, fsSolution, messages.freeze(), solveResult.constraints()).withNameResolutionCache(nameResolutionResult.resolutionCache());
    } catch (RuntimeException ex) {
        throw new SolverException("Internal solver error.", ex);
    }
}
Also used : VariantRelations(mb.nabl2.relations.variants.VariantRelations) ControlFlowComponent(mb.nabl2.solver.components.ControlFlowComponent) IUnifier(mb.nabl2.terms.unification.IUnifier) Function1(org.metaborg.util.functions.Function1) ITerm(mb.nabl2.terms.ITerm) IProgress(org.metaborg.util.task.IProgress) ImmutableSolution(mb.nabl2.solver.ImmutableSolution) Ref(org.metaborg.util.Ref) IConstraint(mb.nabl2.constraints.IConstraint) SetComponent(mb.nabl2.solver.components.SetComponent) Occurrence(mb.nabl2.scopegraph.terms.Occurrence) IEsopNameResolution(mb.nabl2.scopegraph.esop.IEsopNameResolution) NameResolutionResult(mb.nabl2.solver.components.NameResolutionComponent.NameResolutionResult) SolveResult(mb.nabl2.solver.ISolver.SolveResult) SolverCore(mb.nabl2.solver.SolverCore) IVariantRelation(mb.nabl2.relations.variants.IVariantRelation) RelationComponent(mb.nabl2.solver.components.RelationComponent) CFGNode(mb.nabl2.controlflow.terms.CFGNode) ICancel(org.metaborg.util.task.ICancel) Label(mb.nabl2.scopegraph.terms.Label) IFlowSpecSolution(mb.nabl2.controlflow.terms.IFlowSpecSolution) Map(java.util.Map) SymbolicComponent(mb.nabl2.solver.components.SymbolicComponent) Properties(mb.nabl2.util.collections.Properties) ActiveDeclTypes(mb.nabl2.solver.properties.ActiveDeclTypes) SolverException(mb.nabl2.solver.SolverException) ISolution(mb.nabl2.solver.ISolution) ISymbolicConstraints(mb.nabl2.symbolic.ISymbolicConstraints) EqualityComponent(mb.nabl2.solver.components.EqualityComponent) ActiveVars(mb.nabl2.solver.properties.ActiveVars) NameResolutionComponent(mb.nabl2.solver.components.NameResolutionComponent) BaseComponent(mb.nabl2.solver.components.BaseComponent) ImmutableFlowSpecSolution(mb.nabl2.controlflow.terms.ImmutableFlowSpecSolution) IEsopScopeGraph(mb.nabl2.scopegraph.esop.IEsopScopeGraph) SymbolicConstraints(mb.nabl2.symbolic.SymbolicConstraints) EsopNameResolution(mb.nabl2.scopegraph.esop.lazy.EsopNameResolution) Scope(mb.nabl2.scopegraph.terms.Scope) Predicate1(org.metaborg.util.functions.Predicate1) NaBL2DebugConfig(mb.nabl2.config.NaBL2DebugConfig) Iterables2(org.metaborg.util.iterators.Iterables2) PolymorphismComponent(mb.nabl2.solver.components.PolymorphismComponent) IMessages(mb.nabl2.solver.messages.IMessages) NameSetsComponent(mb.nabl2.solver.components.NameSetsComponent) ISolver(mb.nabl2.solver.ISolver) HasRelationBuildConstraints(mb.nabl2.solver.properties.HasRelationBuildConstraints) PolySafe(mb.nabl2.solver.properties.PolySafe) Optional(java.util.Optional) SolverConfig(mb.nabl2.solver.SolverConfig) SymbolicComponent(mb.nabl2.solver.components.SymbolicComponent) Label(mb.nabl2.scopegraph.terms.Label) IEsopScopeGraph(mb.nabl2.scopegraph.esop.IEsopScopeGraph) ControlFlowComponent(mb.nabl2.solver.components.ControlFlowComponent) Occurrence(mb.nabl2.scopegraph.terms.Occurrence) SolverConfig(mb.nabl2.solver.SolverConfig) ActiveDeclTypes(mb.nabl2.solver.properties.ActiveDeclTypes) ISolver(mb.nabl2.solver.ISolver) Optional(java.util.Optional) CFGNode(mb.nabl2.controlflow.terms.CFGNode) SolverCore(mb.nabl2.solver.SolverCore) SolveResult(mb.nabl2.solver.ISolver.SolveResult) SetComponent(mb.nabl2.solver.components.SetComponent) BaseComponent(mb.nabl2.solver.components.BaseComponent) IUnifier(mb.nabl2.terms.unification.IUnifier) RelationComponent(mb.nabl2.solver.components.RelationComponent) NameResolutionComponent(mb.nabl2.solver.components.NameResolutionComponent) ActiveVars(mb.nabl2.solver.properties.ActiveVars) PolySafe(mb.nabl2.solver.properties.PolySafe) ISymbolicConstraints(mb.nabl2.symbolic.ISymbolicConstraints) NameSetsComponent(mb.nabl2.solver.components.NameSetsComponent) IMessages(mb.nabl2.solver.messages.IMessages) PolymorphismComponent(mb.nabl2.solver.components.PolymorphismComponent) Ref(org.metaborg.util.Ref) Scope(mb.nabl2.scopegraph.terms.Scope) HasRelationBuildConstraints(mb.nabl2.solver.properties.HasRelationBuildConstraints) ITerm(mb.nabl2.terms.ITerm) EqualityComponent(mb.nabl2.solver.components.EqualityComponent) NameResolutionResult(mb.nabl2.solver.components.NameResolutionComponent.NameResolutionResult) SolverException(mb.nabl2.solver.SolverException)

Example 19 with Occurrence

use of mb.nabl2.scopegraph.terms.Occurrence in project nabl by metaborg.

the class SG_get_ref_resolution method call.

@Override
public Optional<? extends ITerm> call(IScopeGraphUnit unit, ITerm term, List<ITerm> terms) throws InterpreterException {
    return unit.solution().flatMap(s -> {
        return Occurrence.matcher().match(term, s.unifier()).<ITerm>flatMap(ref -> {
            return s.nameResolution().resolve(ref).map(paths -> {
                List<ITerm> pathTerms = Lists.newArrayListWithExpectedSize(paths.size());
                for (IResolutionPath<Scope, Label, Occurrence> path : paths) {
                    pathTerms.add(B.newTuple(path.getDeclaration(), Paths.toTerm(path)));
                }
                ITerm result = B.newList(pathTerms);
                return result;
            });
        });
    });
}
Also used : Scope(mb.nabl2.scopegraph.terms.Scope) Label(mb.nabl2.scopegraph.terms.Label) ITerm(mb.nabl2.terms.ITerm) Occurrence(mb.nabl2.scopegraph.terms.Occurrence)

Aggregations

ITerm (mb.nabl2.terms.ITerm)19 Occurrence (mb.nabl2.scopegraph.terms.Occurrence)17 Scope (mb.nabl2.scopegraph.terms.Scope)15 Label (mb.nabl2.scopegraph.terms.Label)11 SolveResult (mb.nabl2.solver.ISolver.SolveResult)9 TypeException (mb.nabl2.solver.TypeException)8 Map (java.util.Map)6 IConstraint (mb.nabl2.constraints.IConstraint)6 IEsopNameResolution (mb.nabl2.scopegraph.esop.IEsopNameResolution)6 ISolution (mb.nabl2.solver.ISolution)6 IUnifier (mb.nabl2.terms.unification.IUnifier)6 Optional (java.util.Optional)5 IEsopScopeGraph (mb.nabl2.scopegraph.esop.IEsopScopeGraph)5 SolverCore (mb.nabl2.solver.SolverCore)5 NaBL2DebugConfig (mb.nabl2.config.NaBL2DebugConfig)4 IResolutionPath (mb.nabl2.scopegraph.path.IResolutionPath)4 ISolver (mb.nabl2.solver.ISolver)4 ImmutableSolution (mb.nabl2.solver.ImmutableSolution)4 SolverConfig (mb.nabl2.solver.SolverConfig)4 SolverException (mb.nabl2.solver.SolverException)4