Search in sources :

Example 56 with ITerm

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);
}
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 57 with ITerm

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;
}
Also used : Scope(mb.nabl2.scopegraph.terms.Scope) ITerm(mb.nabl2.terms.ITerm) TypeException(mb.nabl2.solver.TypeException) Occurrence(mb.nabl2.scopegraph.terms.Occurrence)

Example 58 with ITerm

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);
}
Also used : StrategoTerms(mb.nabl2.stratego.StrategoTerms) ITerm(mb.nabl2.terms.ITerm)

Example 59 with ITerm

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);
}
Also used : ITerm(mb.nabl2.terms.ITerm)

Example 60 with ITerm

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));
}
Also used : ITerm(mb.nabl2.terms.ITerm) Test(org.junit.Test)

Aggregations

ITerm (mb.nabl2.terms.ITerm)79 Test (org.junit.Test)24 Occurrence (mb.nabl2.scopegraph.terms.Occurrence)19 Scope (mb.nabl2.scopegraph.terms.Scope)18 SolveResult (mb.nabl2.solver.ISolver.SolveResult)16 Optional (java.util.Optional)15 IMessageInfo (mb.nabl2.constraints.messages.IMessageInfo)15 ITermVar (mb.nabl2.terms.ITermVar)15 Label (mb.nabl2.scopegraph.terms.Label)14 Map (java.util.Map)12 Set (java.util.Set)10 MessageContent (mb.nabl2.constraints.messages.MessageContent)10 SolverCore (mb.nabl2.solver.SolverCore)9 IUnifier (mb.nabl2.terms.unification.IUnifier)9 Function1 (org.metaborg.util.functions.Function1)9 Lists (com.google.common.collect.Lists)8 ISolution (mb.nabl2.solver.ISolution)8 HashMultimap (com.google.common.collect.HashMultimap)7 Multimap (com.google.common.collect.Multimap)7 IConstraint (mb.nabl2.constraints.IConstraint)7