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);
}
}
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);
}
}
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);
}
}
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);
}
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);
}
Aggregations