use of mb.nabl2.terms.ITerm 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.ITerm 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;
}
use of mb.nabl2.terms.ITerm in project nabl by metaborg.
the class ScopeGraphContextPrimitive method call.
public Optional<? extends IStrategoTerm> call(IScopeGraphContext<?> context, IStrategoTerm sterm, List<IStrategoTerm> sterms, ITermFactory factory) throws InterpreterException {
StrategoTerms strategoTerms = new StrategoTerms(factory);
ITerm term = ConstraintTerms.specialize(strategoTerms.fromStratego(sterm));
List<ITerm> terms = sterms.stream().map(strategoTerms::fromStratego).map(ConstraintTerms::specialize).collect(Collectors.toList());
Optional<? extends ITerm> result = call(context, term, terms);
return result.map(ConstraintTerms::explicate).map(strategoTerms::toStratego);
}
use of mb.nabl2.terms.ITerm in project nabl by metaborg.
the class TermMultiset method remove.
public int remove(ITerm term, int n, IUnifier unifier) {
final ITerm rep = unifier.findRecursive(term);
final int prev_n = terms.remove(rep, n);
if (prev_n <= n) {
varTerms.values().remove(rep);
}
return Math.min(prev_n, n);
}
use of mb.nabl2.terms.ITerm in project nabl by metaborg.
the class TermMultisetTest method testAdd1Ground.
@Test
public void testAdd1Ground() {
final ITerm t1 = B.newInt(1);
terms.add(t1, unifier);
assertEquals(0, terms.varSet().size());
assertEquals(1, terms.elementSet().size());
assertEquals(1, terms.count(t1, unifier));
}
Aggregations