Search in sources :

Example 1 with Simplifier

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));
}
Also used : TupleSetSimplifier(com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleSetSimplifier) Expression(com.sri.ai.expresso.api.Expression) Simplifier(com.sri.ai.grinder.sgdpllt.rewriter.api.Simplifier) TupleEqualitySimplifier(com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleEqualitySimplifier) TupleGetSimplifier(com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleGetSimplifier) TupleQuantifierSimplifier(com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleQuantifierSimplifier) TupleDisequalitySimplifier(com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleDisequalitySimplifier) TupleValuedFreeVariablesSimplifier(com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleValuedFreeVariablesSimplifier) TupleSetSimplifier(com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleSetSimplifier) Test(org.junit.Test)

Example 2 with Simplifier

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));
}
Also used : Expression(com.sri.ai.expresso.api.Expression) Simplifier(com.sri.ai.grinder.sgdpllt.rewriter.api.Simplifier) TupleEqualitySimplifier(com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleEqualitySimplifier) TupleGetSimplifier(com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleGetSimplifier) TupleQuantifierSimplifier(com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleQuantifierSimplifier) TupleDisequalitySimplifier(com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleDisequalitySimplifier) TupleValuedFreeVariablesSimplifier(com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleValuedFreeVariablesSimplifier) TupleSetSimplifier(com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleSetSimplifier) TupleGetSimplifier(com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleGetSimplifier) Test(org.junit.Test)

Example 3 with Simplifier

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));
}
Also used : TupleEqualitySimplifier(com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleEqualitySimplifier) Expression(com.sri.ai.expresso.api.Expression) Simplifier(com.sri.ai.grinder.sgdpllt.rewriter.api.Simplifier) TupleEqualitySimplifier(com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleEqualitySimplifier) TupleGetSimplifier(com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleGetSimplifier) TupleQuantifierSimplifier(com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleQuantifierSimplifier) TupleDisequalitySimplifier(com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleDisequalitySimplifier) TupleValuedFreeVariablesSimplifier(com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleValuedFreeVariablesSimplifier) TupleSetSimplifier(com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleSetSimplifier) Test(org.junit.Test)

Example 4 with Simplifier

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.");
    }
}
Also used : BinaryFunction(com.sri.ai.util.base.BinaryFunction) Util.removeFromSetNonDestructively(com.sri.ai.util.Util.removeFromSetNonDestructively) SubExpressionsDepthFirstIterator(com.sri.ai.expresso.helper.SubExpressionsDepthFirstIterator) Expression(com.sri.ai.expresso.api.Expression) Constraint(com.sri.ai.grinder.sgdpllt.api.Constraint) Function(java.util.function.Function) GrinderUtil(com.sri.ai.grinder.helper.GrinderUtil) Util.in(com.sri.ai.util.Util.in) ThereExists(com.sri.ai.grinder.sgdpllt.library.boole.ThereExists) AbstractIterativeMultiIndexQuantifierElimination(com.sri.ai.grinder.sgdpllt.interpreter.AbstractIterativeMultiIndexQuantifierElimination) Expressions.parse(com.sri.ai.expresso.helper.Expressions.parse) Map(java.util.Map) CompleteMultiVariableContext(com.sri.ai.grinder.sgdpllt.core.constraint.CompleteMultiVariableContext) And(com.sri.ai.grinder.sgdpllt.library.boole.And) DefaultMultiVariableConstraint(com.sri.ai.grinder.sgdpllt.core.constraint.DefaultMultiVariableConstraint) ZERO(com.sri.ai.expresso.helper.Expressions.ZERO) LinkedHashSet(java.util.LinkedHashSet) Sets(com.sri.ai.grinder.sgdpllt.library.set.Sets) Type(com.sri.ai.expresso.api.Type) NullaryFunction(com.sri.ai.util.base.NullaryFunction) Util.join(com.sri.ai.util.Util.join) SingleVariableConstraint(com.sri.ai.grinder.sgdpllt.api.SingleVariableConstraint) Simplifier(com.sri.ai.grinder.sgdpllt.rewriter.api.Simplifier) Collection(java.util.Collection) Util.list(com.sri.ai.util.Util.list) Set(java.util.Set) Expressions.getVariableReferences(com.sri.ai.expresso.helper.Expressions.getVariableReferences) Context(com.sri.ai.grinder.sgdpllt.api.Context) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression) Theory(com.sri.ai.grinder.sgdpllt.api.Theory) Beta(com.google.common.annotations.Beta) IndexExpressions(com.sri.ai.grinder.sgdpllt.library.indexexpression.IndexExpressions) Expressions.makeSymbol(com.sri.ai.expresso.helper.Expressions.makeSymbol) Util(com.sri.ai.util.Util) AssociativeCommutativeGroup(com.sri.ai.grinder.sgdpllt.group.AssociativeCommutativeGroup) Util.pickKElementsWithoutReplacement(com.sri.ai.util.Util.pickKElementsWithoutReplacement) TRUE(com.sri.ai.expresso.helper.Expressions.TRUE) BruteForceCommonInterpreter(com.sri.ai.grinder.sgdpllt.interpreter.BruteForceCommonInterpreter) AssignmentsIterator(com.sri.ai.grinder.helper.AssignmentsIterator) CompleteMultiVariableContext(com.sri.ai.grinder.sgdpllt.core.constraint.CompleteMultiVariableContext) Context(com.sri.ai.grinder.sgdpllt.api.Context) BruteForceCommonInterpreter(com.sri.ai.grinder.sgdpllt.interpreter.BruteForceCommonInterpreter) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression) Theory(com.sri.ai.grinder.sgdpllt.api.Theory) Expression(com.sri.ai.expresso.api.Expression) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression) Simplifier(com.sri.ai.grinder.sgdpllt.rewriter.api.Simplifier) SubExpressionsDepthFirstIterator(com.sri.ai.expresso.helper.SubExpressionsDepthFirstIterator)

Example 5 with Simplifier

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");
        }
    }
}
Also used : CompleteMultiVariableContext(com.sri.ai.grinder.sgdpllt.core.constraint.CompleteMultiVariableContext) Context(com.sri.ai.grinder.sgdpllt.api.Context) CompleteMultiVariableContext(com.sri.ai.grinder.sgdpllt.core.constraint.CompleteMultiVariableContext) FALSE(com.sri.ai.expresso.helper.Expressions.FALSE) Categorical(com.sri.ai.expresso.type.Categorical) EQUIVALENCE(com.sri.ai.grinder.sgdpllt.library.FunctorConstants.EQUIVALENCE) HashMap(java.util.HashMap) Expression(com.sri.ai.expresso.api.Expression) GrinderUtil.universallyQuantifyFreeVariables(com.sri.ai.grinder.helper.GrinderUtil.universallyQuantifyFreeVariables) Constraint(com.sri.ai.grinder.sgdpllt.api.Constraint) Util.map(com.sri.ai.util.Util.map) SGDPLLTTester(com.sri.ai.grinder.sgdpllt.tester.SGDPLLTTester) Expressions.apply(com.sri.ai.expresso.helper.Expressions.apply) Expressions.parse(com.sri.ai.expresso.helper.Expressions.parse) Map(java.util.Map) CompleteMultiVariableContext(com.sri.ai.grinder.sgdpllt.core.constraint.CompleteMultiVariableContext) And(com.sri.ai.grinder.sgdpllt.library.boole.And) TheoryTestingSupport(com.sri.ai.grinder.sgdpllt.tester.TheoryTestingSupport) Util.arrayList(com.sri.ai.util.Util.arrayList) Type(com.sri.ai.expresso.api.Type) Simplifier(com.sri.ai.grinder.sgdpllt.rewriter.api.Simplifier) Test(org.junit.Test) EqualityTheory(com.sri.ai.grinder.sgdpllt.theory.equality.EqualityTheory) Context(com.sri.ai.grinder.sgdpllt.api.Context) Sum(com.sri.ai.grinder.sgdpllt.group.Sum) AbstractTheoryIncludingEqualityTest(com.sri.ai.test.grinder.sgdpllt.theory.base.AbstractTheoryIncludingEqualityTest) Beta(com.google.common.annotations.Beta) Max(com.sri.ai.grinder.sgdpllt.group.Max) SingleVariableEqualityConstraint(com.sri.ai.grinder.sgdpllt.theory.equality.SingleVariableEqualityConstraint) Assert(org.junit.Assert) Constraint(com.sri.ai.grinder.sgdpllt.api.Constraint) SingleVariableEqualityConstraint(com.sri.ai.grinder.sgdpllt.theory.equality.SingleVariableEqualityConstraint) Expression(com.sri.ai.expresso.api.Expression) Simplifier(com.sri.ai.grinder.sgdpllt.rewriter.api.Simplifier)

Aggregations

Simplifier (com.sri.ai.grinder.sgdpllt.rewriter.api.Simplifier)9 Expression (com.sri.ai.expresso.api.Expression)8 Test (org.junit.Test)7 TupleDisequalitySimplifier (com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleDisequalitySimplifier)5 TupleEqualitySimplifier (com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleEqualitySimplifier)5 TupleGetSimplifier (com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleGetSimplifier)5 TupleQuantifierSimplifier (com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleQuantifierSimplifier)5 TupleSetSimplifier (com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleSetSimplifier)5 TupleValuedFreeVariablesSimplifier (com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleValuedFreeVariablesSimplifier)5 Beta (com.google.common.annotations.Beta)4 Expressions.parse (com.sri.ai.expresso.helper.Expressions.parse)4 Constraint (com.sri.ai.grinder.sgdpllt.api.Constraint)4 Context (com.sri.ai.grinder.sgdpllt.api.Context)4 Type (com.sri.ai.expresso.api.Type)3 CompleteMultiVariableContext (com.sri.ai.grinder.sgdpllt.core.constraint.CompleteMultiVariableContext)3 And (com.sri.ai.grinder.sgdpllt.library.boole.And)3 BinaryFunction (com.sri.ai.util.base.BinaryFunction)3 QuantifiedExpression (com.sri.ai.expresso.api.QuantifiedExpression)2 TRUE (com.sri.ai.expresso.helper.Expressions.TRUE)2 ZERO (com.sri.ai.expresso.helper.Expressions.ZERO)2