Search in sources :

Example 1 with UnificationException

use of mb.nabl2.terms.unification.UnificationException in project nabl by metaborg.

the class UnificationPerformanceTest method testCycle.

private static void testCycle() {
    System.out.println("Testing cycle");
    final IUnifier.Transient unifier = PersistentUnifier.Transient.of();
    ITermVar varA = TermBuild.B.newVar("", A);
    ITermVar varB = TermBuild.B.newVar("", B);
    ITermVar varC = TermBuild.B.newVar("", C);
    ITerm termB = TermBuild.B.newTuple(varB, varB);
    ITerm termC = TermBuild.B.newTuple(varC, varC);
    try {
        unifier.unify(varA, termB);
        unifier.unify(varB, termC);
        unifier.unify(varC, termB);
        System.out.println(unifier);
    } catch (UnificationException e) {
        System.out.println("Could not unify");
    }
    System.out.println("ground = " + unifier.isGround(termB));
    System.out.println("cyclic = " + unifier.isCyclic(termB));
    System.out.println("size = " + unifier.size(termB));
    System.out.println("vars = " + unifier.getVars(termB));
    System.out.println("equal = " + unifier.areEqual(termB, termC));
    System.out.println("unequal = " + unifier.areUnequal(termB, termC));
}
Also used : ITermVar(mb.nabl2.terms.ITermVar) ITerm(mb.nabl2.terms.ITerm)

Example 2 with UnificationException

use of mb.nabl2.terms.unification.UnificationException in project nabl by metaborg.

the class UnificationPerformanceTest method testUnify.

private static IUnifier testUnify(int n) {
    final IUnifier.Transient unifier = PersistentUnifier.Transient.of();
    final ITerm left = TermBuild.B.newTuple(Iterables.concat(createVars(A, n), createTuples(B, n), Iterables2.singleton(createVar(A, n))));
    final ITerm right = TermBuild.B.newTuple(Iterables.concat(createTuples(A, n), createVars(B, n), Iterables2.singleton(createVar(B, n))));
    try {
        unifier.unify(left, right);
    } catch (UnificationException e) {
        System.err.println("Unification failed");
        e.printStackTrace(System.err);
    }
    System.out.println("ground = " + unifier.isGround(left));
    System.out.println("cyclic = " + unifier.isCyclic(left));
    System.out.println("size = " + unifier.size(left));
    System.out.println("vars = " + unifier.getVars(left));
    System.out.println("equal = " + unifier.areEqual(left, right));
    System.out.println("unequal = " + unifier.areUnequal(left, right));
    return unifier;
}
Also used : ITerm(mb.nabl2.terms.ITerm)

Example 3 with UnificationException

use of mb.nabl2.terms.unification.UnificationException 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 4 with UnificationException

use of mb.nabl2.terms.unification.UnificationException in project nabl by metaborg.

the class EqualityComponent method solve.

// ------------------------------------------------------------------------------------------------------//
private Optional<SolveResult> solve(CEqual constraint) {
    final ITerm left = constraint.getLeft();
    final ITerm right = constraint.getRight();
    try {
        final IUnifier.Transient unifier = this.unifier.get().melt();
        final IUnifier.Immutable unifyResult = unifier.unify(left, right);
        final SolveResult solveResult = ImmutableSolveResult.builder().unifierDiff(unifyResult).build();
        this.unifier.set(unifier.freeze());
        return Optional.of(solveResult);
    } catch (UnificationException ex) {
        final MessageContent content = MessageContent.builder().append("Cannot unify ").append(left).append(" with ").append(right).build();
        final IMessageInfo message = (constraint.getMessageInfo().withDefaultContent(content));
        final SolveResult solveResult = SolveResult.messages(message);
        return Optional.of(solveResult);
    }
}
Also used : MessageContent(mb.nabl2.constraints.messages.MessageContent) IUnifier(mb.nabl2.terms.unification.IUnifier) ImmutableSolveResult(mb.nabl2.solver.ImmutableSolveResult) SolveResult(mb.nabl2.solver.ISolver.SolveResult) ITerm(mb.nabl2.terms.ITerm) UnificationException(mb.nabl2.terms.unification.UnificationException) IMessageInfo(mb.nabl2.constraints.messages.IMessageInfo)

Aggregations

ITerm (mb.nabl2.terms.ITerm)4 ITermVar (mb.nabl2.terms.ITermVar)2 IUnifier (mb.nabl2.terms.unification.IUnifier)2 IMessageInfo (mb.nabl2.constraints.messages.IMessageInfo)1 MessageContent (mb.nabl2.constraints.messages.MessageContent)1 SolveResult (mb.nabl2.solver.ISolver.SolveResult)1 ImmutableSolveResult (mb.nabl2.solver.ImmutableSolveResult)1 UnificationException (mb.nabl2.terms.unification.UnificationException)1 Test (org.junit.Test)1