Search in sources :

Example 16 with ITermVar

use of mb.nabl2.terms.ITermVar in project nabl by metaborg.

the class BaseMultiFileSolver method solveIntra.

public ISolution solveIntra(GraphSolution initial, Collection<ITermVar> intfVars, @Nullable Collection<Scope> intfScopes, 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);
    intfVars.stream().forEach(activeVars::add);
    // guards -- intfScopes == null indicates we do not know the interface scopes, and resolution should be delayed.
    final Predicate2<Scope, Label> isEdgeClosed = (s, l) -> intfScopes != null && !intfScopes.contains(s);
    // more shared
    final IEsopScopeGraph.Transient<Scope, Label, Occurrence, ITerm> scopeGraph = initial.scopeGraph().melt();
    final IEsopNameResolution<Scope, Label, Occurrence> nameResolution = EsopNameResolution.of(config.getResolutionParams(), scopeGraph, isEdgeClosed);
    // 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 PolymorphismComponent polySolver = new PolymorphismComponent(core, Predicate1.never(), Predicate1.never(), nameResolutionSolver::getProperty);
    final RelationComponent relationSolver = new RelationComponent(core, Predicate1.never(), 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());
    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));
    solver.step().subscribe(r -> {
        if (!r.unifierDiff().isEmpty()) {
            try {
                nameResolutionSolver.update();
            } catch (InterruptedException ex) {
            // ignore here
            }
        }
    });
    try {
        nameResolutionSolver.update();
        final SolveResult solveResult = solver.solve(initial.constraints());
        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, solveResult.messages(), 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) ITermVar(mb.nabl2.terms.ITermVar) Properties(mb.nabl2.util.collections.Properties) SolverException(mb.nabl2.solver.SolverException) ISolution(mb.nabl2.solver.ISolution) ISymbolicConstraints(mb.nabl2.symbolic.ISymbolicConstraints) Nullable(javax.annotation.Nullable) EqualityComponent(mb.nabl2.solver.components.EqualityComponent) ActiveVars(mb.nabl2.solver.properties.ActiveVars) NameResolutionComponent(mb.nabl2.solver.components.NameResolutionComponent) BaseComponent(mb.nabl2.solver.components.BaseComponent) Collection(java.util.Collection) ImmutableFlowSpecSolution(mb.nabl2.controlflow.terms.ImmutableFlowSpecSolution) IEsopScopeGraph(mb.nabl2.scopegraph.esop.IEsopScopeGraph) SymbolicConstraints(mb.nabl2.symbolic.SymbolicConstraints) EsopNameResolution(mb.nabl2.scopegraph.esop.lazy.EsopNameResolution) Predicate2(org.metaborg.util.functions.Predicate2) 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) NameSetsComponent(mb.nabl2.solver.components.NameSetsComponent) ISolver(mb.nabl2.solver.ISolver) Optional(java.util.Optional) SolverConfig(mb.nabl2.solver.SolverConfig) SetComponent(mb.nabl2.solver.components.SetComponent) BaseComponent(mb.nabl2.solver.components.BaseComponent) IUnifier(mb.nabl2.terms.unification.IUnifier) SymbolicComponent(mb.nabl2.solver.components.SymbolicComponent) RelationComponent(mb.nabl2.solver.components.RelationComponent) Label(mb.nabl2.scopegraph.terms.Label) IEsopScopeGraph(mb.nabl2.scopegraph.esop.IEsopScopeGraph) ControlFlowComponent(mb.nabl2.solver.components.ControlFlowComponent) NameResolutionComponent(mb.nabl2.solver.components.NameResolutionComponent) ActiveVars(mb.nabl2.solver.properties.ActiveVars) ISymbolicConstraints(mb.nabl2.symbolic.ISymbolicConstraints) Occurrence(mb.nabl2.scopegraph.terms.Occurrence) SolverConfig(mb.nabl2.solver.SolverConfig) ISolver(mb.nabl2.solver.ISolver) Optional(java.util.Optional) CFGNode(mb.nabl2.controlflow.terms.CFGNode) NameSetsComponent(mb.nabl2.solver.components.NameSetsComponent) SolverCore(mb.nabl2.solver.SolverCore) PolymorphismComponent(mb.nabl2.solver.components.PolymorphismComponent) Ref(org.metaborg.util.Ref) SolveResult(mb.nabl2.solver.ISolver.SolveResult) Scope(mb.nabl2.scopegraph.terms.Scope) ITerm(mb.nabl2.terms.ITerm) EqualityComponent(mb.nabl2.solver.components.EqualityComponent) NameResolutionResult(mb.nabl2.solver.components.NameResolutionComponent.NameResolutionResult) SolverException(mb.nabl2.solver.SolverException)

Example 17 with ITermVar

use of mb.nabl2.terms.ITermVar in project nabl by metaborg.

the class FixedPointSolver method updateVars.

private void updateVars(Set<ITermVar> vars) {
    for (IConstraintSetProperty property : properties) {
        component.update(vars);
        property.update(vars);
    }
}
Also used : IConstraintSetProperty(mb.nabl2.solver.properties.IConstraintSetProperty)

Example 18 with ITermVar

use of mb.nabl2.terms.ITermVar 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 19 with ITermVar

use of mb.nabl2.terms.ITermVar in project nabl by metaborg.

the class ActiveVars method add.

// ---------------------------------------------
@Override
public boolean add(IConstraint constraint) {
    final Multiset<ITermVar> vars = findActiveVars(constraint);
    boolean change = false;
    for (ITermVar var : vars) {
        change |= add(var);
    }
    return change;
}
Also used : ITermVar(mb.nabl2.terms.ITermVar)

Example 20 with ITermVar

use of mb.nabl2.terms.ITermVar in project nabl by metaborg.

the class VarMultiset method remove.

public boolean remove(ITermVar var, int n, IUnifier unifier) {
    final Multiset<ITermVar> reps = unifier.findRecursive(var).getVars();
    boolean change = false;
    for (ITermVar rep : reps) {
        int prev_n = vars.remove(rep, n);
        change |= prev_n > 0 && n > 0;
    }
    return change;
}
Also used : ITermVar(mb.nabl2.terms.ITermVar)

Aggregations

ITermVar (mb.nabl2.terms.ITermVar)25 ITerm (mb.nabl2.terms.ITerm)15 Collection (java.util.Collection)3 Occurrence (mb.nabl2.scopegraph.terms.Occurrence)3 SolveResult (mb.nabl2.solver.ISolver.SolveResult)3 HashMultimap (com.google.common.collect.HashMultimap)2 Multimap (com.google.common.collect.Multimap)2 Map (java.util.Map)2 Optional (java.util.Optional)2 NaBL2DebugConfig (mb.nabl2.config.NaBL2DebugConfig)2 IConstraint (mb.nabl2.constraints.IConstraint)2 ImmutableTypeVar (mb.nabl2.poly.ImmutableTypeVar)2 TypeVar (mb.nabl2.poly.TypeVar)2 Scope (mb.nabl2.scopegraph.terms.Scope)2 ISolution (mb.nabl2.solver.ISolution)2 SolverException (mb.nabl2.solver.SolverException)2 TypeException (mb.nabl2.solver.TypeException)2 IUnifier (mb.nabl2.terms.unification.IUnifier)2 HashMultiset (com.google.common.collect.HashMultiset)1 Lists (com.google.common.collect.Lists)1