use of com.sri.ai.grinder.sgdpllt.rewriter.api.Simplifier in project aic-expresso by aic-sri-international.
the class TupleRewriterTest method testTupleSetSimplification.
@Test
public void testTupleSetSimplification() {
Simplifier tupleSetSimplifier = new TupleSetSimplifier();
Assert.assertEquals(parse("(e,b,c)"), tupleSetSimplifier.apply(parse("set((a,b,c),1,e)"), context));
Assert.assertEquals(parse("(a,e,c)"), tupleSetSimplifier.apply(parse("set((a,b,c),2,e)"), context));
Assert.assertEquals(parse("(a,b,e)"), tupleSetSimplifier.apply(parse("set((a,b,c),3,e)"), context));
Assert.assertEquals(parse("tuple(e)"), tupleSetSimplifier.apply(parse("set(tuple(a),I,e)"), context));
Assert.assertEquals(parse("if I = 1 then (e,b) else (a,e)"), tupleSetSimplifier.apply(parse("set((a,b),I,e)"), context));
Assert.assertEquals(parse("if I = 1 then (e,b,c) else if I = 2 then (a,e,c) else (a,b,e)"), tupleSetSimplifier.apply(parse("set((a,b,c),I,e)"), context));
Expression expr = parse("set(I, (a,b,c), e)");
Assert.assertTrue(expr == tupleSetSimplifier.apply(expr, context));
}
use of com.sri.ai.grinder.sgdpllt.rewriter.api.Simplifier in project aic-expresso by aic-sri-international.
the class TupleRewriterTest method testTupleGetSimplification.
@Test
public void testTupleGetSimplification() {
Simplifier tupleGetSimplifier = new TupleGetSimplifier();
Assert.assertEquals(parse("a"), tupleGetSimplifier.apply(parse("get((a,b,c),1)"), context));
Assert.assertEquals(parse("b"), tupleGetSimplifier.apply(parse("get((a,b,c),2)"), context));
Assert.assertEquals(parse("c"), tupleGetSimplifier.apply(parse("get((a,b,c),3)"), context));
Assert.assertEquals(parse("a"), tupleGetSimplifier.apply(parse("get(tuple(a),I)"), context));
Assert.assertEquals(parse("if I = 1 then a else b"), tupleGetSimplifier.apply(parse("get((a,b),I)"), context));
Assert.assertEquals(parse("if I = 1 then a else if I = 2 then b else c"), tupleGetSimplifier.apply(parse("get((a,b,c),I)"), context));
Expression expr = parse("get(I, (a,b,c))");
Assert.assertTrue(expr == tupleGetSimplifier.apply(expr, context));
expr = parse("get(N, 1)");
Assert.assertTrue(expr == tupleGetSimplifier.apply(expr, context));
}
use of com.sri.ai.grinder.sgdpllt.rewriter.api.Simplifier in project aic-expresso by aic-sri-international.
the class TupleRewriterTest method testTupleEqualitySimplification.
@Test
public void testTupleEqualitySimplification() {
Simplifier tupleEqualitySimplifier = new TupleEqualitySimplifier();
Assert.assertEquals(parse("A1 = B1 and A2 = B2 and A3 = B3"), tupleEqualitySimplifier.apply(parse("(A1, A2, A3) = (B1, B2, B3)"), context));
Expression expr = parse("(A1, A2) = (B1, B2, B3)");
Assert.assertTrue(expr == tupleEqualitySimplifier.apply(expr, context));
expr = parse("A1 = B1");
Assert.assertTrue(expr == tupleEqualitySimplifier.apply(expr, context));
}
use of com.sri.ai.grinder.sgdpllt.rewriter.api.Simplifier in project aic-expresso by aic-sri-international.
the class SGDPLLTTester method runGroupProblemSolvingTestGivenConstraintAndProblem.
/**
* @param problem
* @param indices
* @param constraint
* @param body
* @param testAgainstBruteForce
* @param theoryTestingSupport
* @param context
* @throws Error
*/
public static void runGroupProblemSolvingTestGivenConstraintAndProblem(Expression problem, Collection<Expression> indices, Constraint constraint, Expression body, boolean testAgainstBruteForce, TheoryTestingSupport theoryTestingSupport, Context context) throws Error {
Theory theory = theoryTestingSupport.getTheory();
Collection<Expression> freeVariables = getFreeVariableMinusIndices(indices, constraint, body, context);
String problemDescription = problem.toString();
output(problemDescription);
Simplifier symbolicInterpreter = (e, c) -> theory.evaluate(e, c);
long start = System.currentTimeMillis();
Expression symbolicSolution = symbolicInterpreter.apply(problem, context);
long time = System.currentTimeMillis() - start;
output("Symbolic solution: " + symbolicSolution);
output("Computed in " + time + " ms");
if (Util.thereExists(new SubExpressionsDepthFirstIterator(symbolicSolution), e -> e instanceof QuantifiedExpression || Sets.isIntensionalSet(e))) {
throw new Error("Symbolic solution is not quantifier-free: " + symbolicSolution);
}
if (testAgainstBruteForce) {
BinaryFunction<BruteForceCommonInterpreter, Context, Expression> bruteForceSolutionGivenInterpreterAndContextWithAssignmentToOtherVariables = (i, c) -> i.apply(problem, c);
testSymbolicVsBruteForceComputationForEachAssignment(theory, problemDescription, freeVariables, symbolicSolution, bruteForceSolutionGivenInterpreterAndContextWithAssignmentToOtherVariables, context);
// A more elegant approach would be to create a "for all free variables : symbolic = problem" expression
// and solve it by brute force instead of using testSymbolicVsBruteForceComputation
// which replicates the brute force interpreter to some extent.
// The reason we do not do this is simply due to the fact that the brute force interpreter would return "false"
// in case of failure, without indicating which assignment failed, which is very useful for debugging.
// If interpreters, and in fact the whole framework, provided proofs of its calculations,
// then we could simply use the more elegant approach.
} else {
output("Skipping test againt brute-force.");
}
}
use of com.sri.ai.grinder.sgdpllt.rewriter.api.Simplifier in project aic-expresso by aic-sri-international.
the class AbstractEqualityConstraintTest method runCompleteSatisfiabilityTest.
/**
* @param conjunction
* @param expected
*/
private void runCompleteSatisfiabilityTest(String conjunction, Expression expected, TheoryTestingSupport theoryTestingSupport) {
Context context = theoryTestingSupport.makeContextWithTestingInformation();
Constraint constraint = new CompleteMultiVariableContext(theoryTestingSupport.getTheory(), context);
for (Expression literal : And.getConjuncts(parse(conjunction))) {
constraint = constraint.conjoin(literal, context);
if (constraint.isContradiction()) {
break;
}
}
if (expected == null && !constraint.isContradiction()) {
throw new AssertionError("Expected null but was <" + constraint + ">");
} else if (expected != null && constraint.isContradiction()) {
throw new AssertionError("Expected <" + expected + "> but was null");
} else if (expected != null && !constraint.isContradiction() && !expected.equals(constraint)) {
Simplifier interpreter = (e, c) -> theoryTestingSupport.getTheory().evaluate(e, c);
// Simplifier interpreter = new Evaluator(theoryTestingSupport.getTheory());
Expression equivalenceDefinition = apply(EQUIVALENCE, expected, constraint);
Expression universallyQuantified = universallyQuantifyFreeVariables(equivalenceDefinition, context);
Expression equivalent = interpreter.apply(universallyQuantified, context);
if (equivalent.equals(FALSE)) {
throw new Error("Expected <" + expected + "> but got <" + constraint + ">, which is not equivalent either");
}
}
}
Aggregations