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