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