Search in sources :

Example 21 with ITermVar

use of mb.nabl2.terms.ITermVar in project nabl by metaborg.

the class TermMultisetTest method testContainsOriginalAndUnifiedTerm.

@Test
public void testContainsOriginalAndUnifiedTerm() throws UnificationException {
    final ITermVar v1 = B.newVar("", "v1");
    final ITerm t1 = B.newInt(1);
    final ITerm t2 = B.newTuple(v1);
    terms.add(t2, unifier);
    assertEquals(1, terms.varSet().size());
    IUnifier.Immutable result = unifier.unify(v1, t1);
    terms.update(result.varSet(), unifier);
    assertTrue(terms.contains(t2, unifier));
    assertTrue(terms.contains(unifier.findTerm(t2), unifier));
    assertEquals(0, terms.varSet().size());
}
Also used : IUnifier(mb.nabl2.terms.unification.IUnifier) ITermVar(mb.nabl2.terms.ITermVar) ITerm(mb.nabl2.terms.ITerm) Test(org.junit.Test)

Example 22 with ITermVar

use of mb.nabl2.terms.ITermVar in project nabl by metaborg.

the class PersistentUnifier method findVarRecursive.

private ITerm findVarRecursive(final ITermVar var, final Set<ITermVar> stack, final java.util.Map<ITermVar, ITerm> visited) {
    final ITermVar rep = findRep(var);
    final ITerm instance;
    if (!visited.containsKey(rep)) {
        stack.add(rep);
        visited.put(rep, rep);
        final ITerm term = terms().get(rep);
        instance = term != null ? findTermRecursive(term, stack, visited) : rep;
        visited.put(rep, instance);
        stack.remove(rep);
        return instance;
    } else if (stack.contains(rep)) {
        throw new IllegalArgumentException("Recursive terms cannot be instantiated.");
    } else {
        instance = visited.get(rep);
    }
    return instance;
}
Also used : ITermVar(mb.nabl2.terms.ITermVar) ITerm(mb.nabl2.terms.ITerm)

Example 23 with ITermVar

use of mb.nabl2.terms.ITermVar in project nabl by metaborg.

the class PersistentUnifier method isGround.

private boolean isGround(final ITermVar var, final Set<ITermVar> stack, final java.util.Map<ITermVar, Boolean> visited) {
    final boolean ground;
    final ITermVar rep = findRep(var);
    if (!visited.containsKey(rep)) {
        stack.add(rep);
        visited.put(rep, false);
        final ITerm term = terms().get(rep);
        ground = term != null ? isGround(term.getVars().elementSet(), stack, visited) : false;
        visited.put(rep, ground);
        stack.remove(rep);
    } else if (stack.contains(rep)) {
        ground = false;
    } else {
        ground = visited.get(rep);
    }
    return ground;
}
Also used : ITermVar(mb.nabl2.terms.ITermVar) ITerm(mb.nabl2.terms.ITerm)

Example 24 with ITermVar

use of mb.nabl2.terms.ITermVar in project nabl by metaborg.

the class PersistentUnifier method toString.

@Override
public String toString() {
    final StringBuilder sb = new StringBuilder();
    sb.append("{");
    boolean first = true;
    for (ITermVar var : terms().keySet()) {
        sb.append(first ? " " : ", ");
        first = false;
        sb.append(var);
        sb.append(" |-> ");
        sb.append(terms().get(var));
    }
    for (ITermVar var : reps().keySet()) {
        sb.append(first ? " " : ", ");
        first = false;
        sb.append(var);
        sb.append(" |-> ");
        sb.append(findRep(var));
    }
    sb.append(first ? "}" : " }");
    return sb.toString();
}
Also used : ITermVar(mb.nabl2.terms.ITermVar)

Example 25 with ITermVar

use of mb.nabl2.terms.ITermVar in project nabl by metaborg.

the class PersistentUnifier method getVars.

private void getVars(final ITermVar var, final LinkedList<ITermVar> stack, final Set<ITermVar> visited, Set<ITermVar> vars) {
    final ITermVar rep = findRep(var);
    if (!visited.contains(rep)) {
        visited.add(rep);
        stack.push(rep);
        final ITerm term = terms().get(rep);
        if (term != null) {
            getVars(term.getVars().elementSet(), stack, visited, vars);
        } else {
            vars.add(rep);
        }
        stack.pop();
    } else {
        // linear
        final int index = stack.indexOf(rep);
        if (index >= 0) {
            vars.addAll(stack.subList(0, index + 1));
        }
    }
}
Also used : ITermVar(mb.nabl2.terms.ITermVar) ITerm(mb.nabl2.terms.ITerm)

Aggregations

ITermVar (mb.nabl2.terms.ITermVar)25 ITerm (mb.nabl2.terms.ITerm)15 Collection (java.util.Collection)3 Occurrence (mb.nabl2.scopegraph.terms.Occurrence)3 SolveResult (mb.nabl2.solver.ISolver.SolveResult)3 HashMultimap (com.google.common.collect.HashMultimap)2 Multimap (com.google.common.collect.Multimap)2 Map (java.util.Map)2 Optional (java.util.Optional)2 NaBL2DebugConfig (mb.nabl2.config.NaBL2DebugConfig)2 IConstraint (mb.nabl2.constraints.IConstraint)2 ImmutableTypeVar (mb.nabl2.poly.ImmutableTypeVar)2 TypeVar (mb.nabl2.poly.TypeVar)2 Scope (mb.nabl2.scopegraph.terms.Scope)2 ISolution (mb.nabl2.solver.ISolution)2 SolverException (mb.nabl2.solver.SolverException)2 TypeException (mb.nabl2.solver.TypeException)2 IUnifier (mb.nabl2.terms.unification.IUnifier)2 HashMultiset (com.google.common.collect.HashMultiset)1 Lists (com.google.common.collect.Lists)1