Search in sources :

Example 11 with SolveResult

use of mb.nabl2.solver.ISolver.SolveResult 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 12 with SolveResult

use of mb.nabl2.solver.ISolver.SolveResult in project nabl by metaborg.

the class BaseSolver method solveGraph.

public GraphSolution solveGraph(BaseSolution initial, Function1<String, String> fresh, ICancel cancel, IProgress progress) throws SolverException, InterruptedException {
    // shared
    final Ref<IUnifier.Immutable> unifier = new Ref<>(initial.unifier());
    final IEsopScopeGraph.Transient<Scope, Label, Occurrence, ITerm> scopeGraph = EsopScopeGraph.Transient.of();
    // solver components
    final SolverCore core = new SolverCore(initial.config(), unifier, fresh, callExternal);
    final AstComponent astSolver = new AstComponent(core, Properties.Transient.of());
    final BaseComponent baseSolver = new BaseComponent(core);
    final EqualityComponent equalitySolver = new EqualityComponent(core, unifier);
    final ScopeGraphComponent scopeGraphSolver = new ScopeGraphComponent(core, scopeGraph);
    try {
        ISolver component = c -> c.matchOrThrow(IConstraint.CheckedCases.<Optional<SolveResult>, InterruptedException>builder().onAst(astSolver::solve).onBase(baseSolver::solve).onEquality(equalitySolver::solve).onScopeGraph(scopeGraphSolver::solve).otherwise(cc -> Optional.empty()));
        final FixedPointSolver solver = new FixedPointSolver(cancel, progress, component, Iterables2.empty());
        final SolveResult solveResult = solver.solve(initial.constraints());
        return ImmutableGraphSolution.of(initial.config(), astSolver.finish(), scopeGraphSolver.finish(), equalitySolver.finish(), solveResult.messages(), solveResult.constraints());
    } catch (RuntimeException ex) {
        throw new SolverException("Internal solver error.", ex);
    }
}
Also used : 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) ScopeGraphComponent(mb.nabl2.solver.components.ScopeGraphComponent) Occurrence(mb.nabl2.scopegraph.terms.Occurrence) IProperties(mb.nabl2.util.collections.IProperties) SolveResult(mb.nabl2.solver.ISolver.SolveResult) SolverCore(mb.nabl2.solver.SolverCore) ICancel(org.metaborg.util.task.ICancel) Value(org.immutables.value.Value) Label(mb.nabl2.scopegraph.terms.Label) Properties(mb.nabl2.util.collections.Properties) IAstConstraint(mb.nabl2.constraints.ast.IAstConstraint) SolverException(mb.nabl2.solver.SolverException) ISolution(mb.nabl2.solver.ISolution) EqualityComponent(mb.nabl2.solver.components.EqualityComponent) EsopScopeGraph(mb.nabl2.scopegraph.esop.reference.EsopScopeGraph) ImmutableSet(com.google.common.collect.ImmutableSet) TermIndex(mb.nabl2.stratego.TermIndex) BaseComponent(mb.nabl2.solver.components.BaseComponent) IScopeGraphConstraint(mb.nabl2.constraints.scopegraph.IScopeGraphConstraint) IEsopScopeGraph(mb.nabl2.scopegraph.esop.IEsopScopeGraph) Set(java.util.Set) Messages(mb.nabl2.solver.messages.Messages) Scope(mb.nabl2.scopegraph.terms.Scope) Sets(com.google.common.collect.Sets) NaBL2DebugConfig(mb.nabl2.config.NaBL2DebugConfig) AstComponent(mb.nabl2.solver.components.AstComponent) Iterables2(org.metaborg.util.iterators.Iterables2) IMessages(mb.nabl2.solver.messages.IMessages) ISolver(mb.nabl2.solver.ISolver) Serial(org.immutables.serial.Serial) SeedResult(mb.nabl2.solver.ISolver.SeedResult) Optional(java.util.Optional) SolverConfig(mb.nabl2.solver.SolverConfig) Collections(java.util.Collections) ScopeGraphComponent(mb.nabl2.solver.components.ScopeGraphComponent) BaseComponent(mb.nabl2.solver.components.BaseComponent) AstComponent(mb.nabl2.solver.components.AstComponent) ISolver(mb.nabl2.solver.ISolver) Optional(java.util.Optional) Label(mb.nabl2.scopegraph.terms.Label) SolverCore(mb.nabl2.solver.SolverCore) IEsopScopeGraph(mb.nabl2.scopegraph.esop.IEsopScopeGraph) 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) SolverException(mb.nabl2.solver.SolverException) Occurrence(mb.nabl2.scopegraph.terms.Occurrence)

Example 13 with SolveResult

use of mb.nabl2.solver.ISolver.SolveResult in project nabl by metaborg.

the class SemiIncrementalMultiFileSolver method solveInter.

public ISolution solveInter(ISolution initial, Iterable<? extends ISolution> unitSolutions, IMessageInfo message, 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());
    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);
    // 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);
    // solver components
    final SolverCore core = new SolverCore(config, unifier, fresh, callExternal);
    final AstComponent astSolver = new AstComponent(core, initial.astProperties().melt());
    final BaseComponent baseSolver = new BaseComponent(core);
    final EqualityComponent equalitySolver = new EqualityComponent(core, unifier);
    final NameResolutionComponent nameResolutionSolver = new NameResolutionComponent(core, scopeGraph, nameResolution, initial.declProperties().melt());
    final NameSetsComponent nameSetSolver = new NameSetsComponent(core, scopeGraph, nameResolution);
    final RelationComponent relationSolver = new RelationComponent(core, isRelationComplete, config.getFunctions(), VariantRelations.melt(initial.relations()));
    final SetComponent setSolver = new SetComponent(core, nameSetSolver.nameSets());
    final SymbolicComponent symSolver = new SymbolicComponent(core, initial.symbolic());
    final ControlFlowComponent cfgSolver = new ControlFlowComponent(core, ImmutableFlowSpecSolution.of());
    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 {
        // seed unit solutions
        final java.util.Set<IConstraint> constraints = Sets.newHashSet(initial.constraints());
        final IMessages.Transient messages = initial.messages().melt();
        for (ISolution unitSolution : unitSolutions) {
            seed(astSolver.seed(unitSolution.astProperties(), message), messages, constraints);
            seed(equalitySolver.seed(unitSolution.unifier(), message), messages, constraints);
            final NameResolutionResult nameResult = ImmutableNameResolutionResult.of(unitSolution.scopeGraph(), unitSolution.declProperties()).withResolutionCache(unitSolution.nameResolutionCache());
            seed(nameResolutionSolver.seed(nameResult, message), messages, constraints);
            seed(relationSolver.seed(unitSolution.relations(), message), messages, constraints);
            seed(symSolver.seed(unitSolution.symbolic(), message), messages, constraints);
            seed(cfgSolver.seed(unitSolution.flowSpecSolution(), message), messages, constraints);
            constraints.addAll(unitSolution.constraints());
            messages.addAll(unitSolution.messages());
        }
        // solve constraints
        nameResolutionSolver.update();
        SolveResult solveResult = solver.solve(constraints);
        messages.addAll(solveResult.messages());
        // build result
        IProperties.Immutable<TermIndex, ITerm, ITerm> astResult = astSolver.finish();
        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, astResult, 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 : ControlFlowComponent(mb.nabl2.solver.components.ControlFlowComponent) IUnifier(mb.nabl2.terms.unification.IUnifier) ITerm(mb.nabl2.terms.ITerm) IProgress(org.metaborg.util.task.IProgress) Ref(org.metaborg.util.Ref) IConstraint(mb.nabl2.constraints.IConstraint) IProperties(mb.nabl2.util.collections.IProperties) SolverCore(mb.nabl2.solver.SolverCore) ICancel(org.metaborg.util.task.ICancel) IFlowSpecSolution(mb.nabl2.controlflow.terms.IFlowSpecSolution) Map(java.util.Map) SymbolicComponent(mb.nabl2.solver.components.SymbolicComponent) SolverException(mb.nabl2.solver.SolverException) ISolution(mb.nabl2.solver.ISolution) EqualityComponent(mb.nabl2.solver.components.EqualityComponent) ImmutableFlowSpecSolution(mb.nabl2.controlflow.terms.ImmutableFlowSpecSolution) IEsopScopeGraph(mb.nabl2.scopegraph.esop.IEsopScopeGraph) Predicate1(org.metaborg.util.functions.Predicate1) IMessageInfo(mb.nabl2.constraints.messages.IMessageInfo) Sets(com.google.common.collect.Sets) ImmutableNameResolutionResult(mb.nabl2.solver.components.ImmutableNameResolutionResult) AstComponent(mb.nabl2.solver.components.AstComponent) Iterables2(org.metaborg.util.iterators.Iterables2) NameSetsComponent(mb.nabl2.solver.components.NameSetsComponent) ISolver(mb.nabl2.solver.ISolver) Optional(java.util.Optional) VariantRelations(mb.nabl2.relations.variants.VariantRelations) Function1(org.metaborg.util.functions.Function1) ImmutableSolution(mb.nabl2.solver.ImmutableSolution) 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) IVariantRelation(mb.nabl2.relations.variants.IVariantRelation) RelationComponent(mb.nabl2.solver.components.RelationComponent) CFGNode(mb.nabl2.controlflow.terms.CFGNode) Label(mb.nabl2.scopegraph.terms.Label) ActiveDeclTypes(mb.nabl2.solver.properties.ActiveDeclTypes) ISymbolicConstraints(mb.nabl2.symbolic.ISymbolicConstraints) ActiveVars(mb.nabl2.solver.properties.ActiveVars) NameResolutionComponent(mb.nabl2.solver.components.NameResolutionComponent) TermIndex(mb.nabl2.stratego.TermIndex) BaseComponent(mb.nabl2.solver.components.BaseComponent) EsopNameResolution(mb.nabl2.scopegraph.esop.lazy.EsopNameResolution) Scope(mb.nabl2.scopegraph.terms.Scope) NaBL2DebugConfig(mb.nabl2.config.NaBL2DebugConfig) PolymorphismComponent(mb.nabl2.solver.components.PolymorphismComponent) IMessages(mb.nabl2.solver.messages.IMessages) HasRelationBuildConstraints(mb.nabl2.solver.properties.HasRelationBuildConstraints) PolySafe(mb.nabl2.solver.properties.PolySafe) SolverConfig(mb.nabl2.solver.SolverConfig) SymbolicComponent(mb.nabl2.solver.components.SymbolicComponent) Label(mb.nabl2.scopegraph.terms.Label) IProperties(mb.nabl2.util.collections.IProperties) 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) AstComponent(mb.nabl2.solver.components.AstComponent) ISolver(mb.nabl2.solver.ISolver) Optional(java.util.Optional) CFGNode(mb.nabl2.controlflow.terms.CFGNode) SolverCore(mb.nabl2.solver.SolverCore) IConstraint(mb.nabl2.constraints.IConstraint) ISolution(mb.nabl2.solver.ISolution) TermIndex(mb.nabl2.stratego.TermIndex) 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) ImmutableNameResolutionResult(mb.nabl2.solver.components.ImmutableNameResolutionResult) NameResolutionResult(mb.nabl2.solver.components.NameResolutionComponent.NameResolutionResult) SolverException(mb.nabl2.solver.SolverException)

Example 14 with SolveResult

use of mb.nabl2.solver.ISolver.SolveResult in project nabl by metaborg.

the class NameResolutionComponent method solve.

private Optional<SolveResult> solve(CDeclProperty c) {
    final ITerm declTerm = unifier().findRecursive(c.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 " + c));
    final SolveResult result = putProperty(decl, c.getKey(), c.getValue(), c.getMessageInfo()).map(cc -> SolveResult.constraints(cc)).orElseGet(() -> SolveResult.empty());
    return Optional.of(result);
}
Also used : INameResolutionConstraint(mb.nabl2.constraints.nameresolution.INameResolutionConstraint) CDeclProperty(mb.nabl2.constraints.nameresolution.CDeclProperty) TypeException(mb.nabl2.solver.TypeException) ITerm(mb.nabl2.terms.ITerm) ImmutableCEqual(mb.nabl2.constraints.equality.ImmutableCEqual) IConstraint(mb.nabl2.constraints.IConstraint) Multimap(com.google.common.collect.Multimap) Occurrence(mb.nabl2.scopegraph.terms.Occurrence) IProperties(mb.nabl2.util.collections.IProperties) IEsopNameResolution(mb.nabl2.scopegraph.esop.IEsopNameResolution) SolveResult(mb.nabl2.solver.ISolver.SolveResult) SolverCore(mb.nabl2.solver.SolverCore) HashMultimap(com.google.common.collect.HashMultimap) Lists(com.google.common.collect.Lists) VarMultimap(mb.nabl2.terms.collection.VarMultimap) Value(org.immutables.value.Value) Label(mb.nabl2.scopegraph.terms.Label) ASolver(mb.nabl2.solver.ASolver) IEsopScopeGraph(mb.nabl2.scopegraph.esop.IEsopScopeGraph) CResolve(mb.nabl2.constraints.nameresolution.CResolve) Scope(mb.nabl2.scopegraph.terms.Scope) IResolutionPath(mb.nabl2.scopegraph.path.IResolutionPath) Collectors(java.util.stream.Collectors) IMessageInfo(mb.nabl2.constraints.messages.IMessageInfo) Sets(com.google.common.collect.Sets) MessageContent(mb.nabl2.constraints.messages.MessageContent) ImmutableSolveResult(mb.nabl2.solver.ImmutableSolveResult) List(java.util.List) Paths(mb.nabl2.scopegraph.terms.path.Paths) Serial(org.immutables.serial.Serial) SeedResult(mb.nabl2.solver.ISolver.SeedResult) Optional(java.util.Optional) CAssoc(mb.nabl2.constraints.nameresolution.CAssoc) SolveResult(mb.nabl2.solver.ISolver.SolveResult) ImmutableSolveResult(mb.nabl2.solver.ImmutableSolveResult) ITerm(mb.nabl2.terms.ITerm) TypeException(mb.nabl2.solver.TypeException) Occurrence(mb.nabl2.scopegraph.terms.Occurrence)

Example 15 with SolveResult

use of mb.nabl2.solver.ISolver.SolveResult 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)

Aggregations

ITerm (mb.nabl2.terms.ITerm)16 SolveResult (mb.nabl2.solver.ISolver.SolveResult)14 IMessageInfo (mb.nabl2.constraints.messages.IMessageInfo)11 Occurrence (mb.nabl2.scopegraph.terms.Occurrence)9 Optional (java.util.Optional)8 MessageContent (mb.nabl2.constraints.messages.MessageContent)8 SolverCore (mb.nabl2.solver.SolverCore)8 IConstraint (mb.nabl2.constraints.IConstraint)7 Label (mb.nabl2.scopegraph.terms.Label)7 Scope (mb.nabl2.scopegraph.terms.Scope)7 Map (java.util.Map)5 IVariantRelation (mb.nabl2.relations.variants.IVariantRelation)5 VariantRelations (mb.nabl2.relations.variants.VariantRelations)5 IEsopScopeGraph (mb.nabl2.scopegraph.esop.IEsopScopeGraph)5 IUnifier (mb.nabl2.terms.unification.IUnifier)5 Function1 (org.metaborg.util.functions.Function1)5 Predicate1 (org.metaborg.util.functions.Predicate1)5 Iterables2 (org.metaborg.util.iterators.Iterables2)5 Set (java.util.Set)4 NaBL2DebugConfig (mb.nabl2.config.NaBL2DebugConfig)4