Search in sources :

Example 41 with DifferenceArithmeticTheory

use of com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory in project aic-expresso by aic-sri-international.

the class EvaluationTest method testEvaluationOfQuantifiersOverFunctions.

@Test
public void testEvaluationOfQuantifiersOverFunctions() {
    TheoryTestingSupport theoryTestingSupport = TheoryTestingSupport.make(makeRandom(), new CompoundTheory(new EqualityTheory(false, true), new DifferenceArithmeticTheory(false, true), new PropositionalTheory(), new BruteForceFunctionTheory()));
    Map<String, Type> variablesAndTypes = new LinkedHashMap<>(theoryTestingSupport.getVariableNamesAndTypesForTesting());
    Type booleanType = variablesAndTypes.get("P");
    variablesAndTypes.put("S", booleanType);
    variablesAndTypes.put("T", booleanType);
    variablesAndTypes.put("U", booleanType);
    theoryTestingSupport.setVariableNamesAndTypesForTesting(variablesAndTypes);
    Context context = theoryTestingSupport.makeContextWithTestingInformation();
    String expressionString;
    Expression expected;
    expressionString = "sum( {{ (on f in '->'(x(0..2), Boolean))  if f(0) and f(1) then 2 else 3  :  f(2) }} )";
    // 2+3+3+3
    expected = parse("11");
    runTest(expressionString, expected, context);
    expressionString = "sum({{ (on f in '->'(x(1..2), Boolean), g in '->'(x(1..2), Boolean))  if f(1) and g(2) then 2 else 3  :  f(2) }} )";
    expected = parse("22");
    runTest(expressionString, expected, context);
    expressionString = "sum({{ (on f in '->'(x(1..2), Boolean), g in '->'(x(1..2), Boolean))  if f(1) and g(2) then 2 else 3 }} )";
    expected = parse("44");
    runTest(expressionString, expected, context);
    expressionString = "sum({{ (on f in '->'(x(1..2), Boolean), g in '->'(x(1..2), Boolean))  if f(1) then 2 else 3 }} )";
    expected = parse("40");
    runTest(expressionString, expected, context);
    expressionString = "sum({{ (on f in '->'(x(1..2), Boolean))  if f(1) then 2 else 3 }} )";
    expected = parse("10");
    runTest(expressionString, expected, context);
    expressionString = "sum({{ (on f in '->'(x(1..2), 1..2))  if f(1) = 1 then 2 else 3 }} )";
    expected = parse("10");
    runTest(expressionString, expected, context);
    expressionString = "sum({{ (on f in '->'(x(1..2), 1..2))  f(1) }} )";
    expected = parse("6");
    runTest(expressionString, expected, context);
    expressionString = "sum({{ (on f in '->'(1..2))  f() }} )";
    expected = parse("3");
    runTest(expressionString, expected, context);
    expressionString = "sum({{ (on f in '->'(1..2))  f }} )";
    expected = parse("(lambda : 1) + (lambda : 2)");
    runTest(expressionString, expected, context);
    expressionString = "product( {{ (on f in '->'(x(0..2), Boolean))  if f(0) and f(1) then 2 else 3  :  f(2) }} )";
    // 2*3*3*3
    expected = parse("54");
    runTest(expressionString, expected, context);
    expressionString = "max( {{ (on f in '->'(x(0..2), Boolean))  if f(0) and f(1) then 2 else 3  :  f(2) }} )";
    expected = parse("3");
    runTest(expressionString, expected, context);
    expressionString = "| f in '->'(0..2, Boolean) : f(0) |";
    expected = parse("4");
    runTest(expressionString, expected, context);
    expressionString = "| f in '->'(x(0..2), Boolean) : f(0) |";
    expected = parse("4");
    runTest(expressionString, expected, context);
    expressionString = "| f in 0..2 -> Boolean : f(0) |";
    expected = parse("4");
    runTest(expressionString, expected, context);
    expressionString = "| f in '->'(x(0..2, 0..2), Boolean) : f(0, 0) |";
    expected = parse("256");
    runTest(expressionString, expected, context);
    expressionString = "| f in 0..2 x 0..2 -> Boolean : f(0, 0) |";
    expected = parse("256");
    runTest(expressionString, expected, context);
    expressionString = "for all f in '->'(x(0..2), Boolean) : f(0)";
    expected = parse("false");
    runTest(expressionString, expected, context);
    expressionString = "for all f in '->'(x(0..2), Boolean) : (f(0) or not f(0)) and P";
    expected = parse("P");
    runTest(expressionString, expected, context);
    expressionString = "there exists f in '->'(x(0..2), Boolean) : f(0)";
    expected = parse("true");
    runTest(expressionString, expected, context);
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context) EqualityTheory(com.sri.ai.grinder.sgdpllt.theory.equality.EqualityTheory) BruteForceFunctionTheory(com.sri.ai.grinder.sgdpllt.theory.function.BruteForceFunctionTheory) Type(com.sri.ai.expresso.api.Type) Expression(com.sri.ai.expresso.api.Expression) TheoryTestingSupport(com.sri.ai.grinder.sgdpllt.tester.TheoryTestingSupport) DifferenceArithmeticTheory(com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory) PropositionalTheory(com.sri.ai.grinder.sgdpllt.theory.propositional.PropositionalTheory) CompoundTheory(com.sri.ai.grinder.sgdpllt.theory.compound.CompoundTheory) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 42 with DifferenceArithmeticTheory

use of com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory in project aic-expresso by aic-sri-international.

the class EvaluationTest method testEvaluationOfQuantifiedExpressions.

@Test
public void testEvaluationOfQuantifiedExpressions() {
    TheoryTestingSupport theoryTestingSupport = TheoryTestingSupport.make(makeRandom(), new CompoundTheory(new EqualityTheory(false, true), new DifferenceArithmeticTheory(false, true), new PropositionalTheory()));
    Map<String, Type> variablesAndTypes = new LinkedHashMap<>(theoryTestingSupport.getVariableNamesAndTypesForTesting());
    Type booleanType = variablesAndTypes.get("P");
    variablesAndTypes.put("S", booleanType);
    variablesAndTypes.put("T", booleanType);
    variablesAndTypes.put("U", booleanType);
    theoryTestingSupport.setVariableNamesAndTypesForTesting(variablesAndTypes);
    Context context = theoryTestingSupport.makeContextWithTestingInformation();
    String expressionString;
    Expression expected;
    expressionString = "for all I in 1..10 : (I != 4 or I = 4) and P";
    expected = parse("P");
    runTest(expressionString, expected, context);
    // the following example tests that quantified expressions that are function arguments get evaluated properly, as there once was a bug preventing it
    expressionString = "not(for all I in 1..10 : (I != 4 or I = 4))";
    expected = parse("false");
    runTest(expressionString, expected, context);
    expressionString = "for all I in 1..10 : for all J in 1..2 : I != 4";
    expected = parse("false");
    runTest(expressionString, expected, context);
    expressionString = "for all I in 1..10 : for all P in Boolean : I != 4 or I = 4 and (P or not P)";
    expected = parse("true");
    runTest(expressionString, expected, context);
    expressionString = "there exists I in 1..10 : I != 4 and P";
    expected = parse("P");
    runTest(expressionString, expected, context);
    expressionString = "there exists I in 1..10 : there exists J in 1..2 : I != 4 and J != 1";
    expected = parse("true");
    runTest(expressionString, expected, context);
    expressionString = "there exists I in 1..10 : there exists P in Boolean : I != 4 and P";
    expected = parse("true");
    runTest(expressionString, expected, context);
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context) EqualityTheory(com.sri.ai.grinder.sgdpllt.theory.equality.EqualityTheory) Type(com.sri.ai.expresso.api.Type) Expression(com.sri.ai.expresso.api.Expression) TheoryTestingSupport(com.sri.ai.grinder.sgdpllt.tester.TheoryTestingSupport) DifferenceArithmeticTheory(com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory) PropositionalTheory(com.sri.ai.grinder.sgdpllt.theory.propositional.PropositionalTheory) CompoundTheory(com.sri.ai.grinder.sgdpllt.theory.compound.CompoundTheory) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 43 with DifferenceArithmeticTheory

use of com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory in project aic-expresso by aic-sri-international.

the class EvaluationTest method testEvaluationOfCardinalityExpressions.

@Test
public void testEvaluationOfCardinalityExpressions() {
    TheoryTestingSupport theoryTestingSupport = TheoryTestingSupport.make(makeRandom(), new CompoundTheory(new EqualityTheory(false, true), new DifferenceArithmeticTheory(false, true), new PropositionalTheory()));
    Map<String, Type> variablesAndTypes = new LinkedHashMap<>(theoryTestingSupport.getVariableNamesAndTypesForTesting());
    Type booleanType = variablesAndTypes.get("P");
    variablesAndTypes.put("S", booleanType);
    variablesAndTypes.put("T", booleanType);
    variablesAndTypes.put("U", booleanType);
    theoryTestingSupport.setVariableNamesAndTypesForTesting(variablesAndTypes);
    Context context = theoryTestingSupport.makeContextWithTestingInformation();
    String expressionString;
    Expression expected;
    expressionString = "| {{ (on I in 1..10) 3 : I != 4 and P }} |";
    expected = parse("if P then 9 else 0");
    runTest(expressionString, expected, context);
    expressionString = "| I in 1..10 : I != 4 and P |";
    expected = parse("if P then 9 else 0");
    runTest(expressionString, expected, context);
    expressionString = "| {{ (on ) 3 : I != 4 and P }} |";
    expected = parse("if I != 4 then if P then 1 else 0 else 0");
    runTest(expressionString, expected, context);
    expressionString = "| : I != 4 and P |";
    expected = parse("if I != 4 then if P then 1 else 0 else 0");
    runTest(expressionString, expected, context);
    expressionString = "| {{ (on ) 3 : P and not P }} |";
    expected = parse("0");
    runTest(expressionString, expected, context);
    expressionString = "| : P and not P |";
    expected = parse("0");
    runTest(expressionString, expected, context);
    expressionString = "| {{ (on I in 1..10, J in 1..2) 3 : I != 4 }} |";
    expected = parse("18");
    runTest(expressionString, expected, context);
    expressionString = "| I in 1..10, J in 1..2 : I != 4 |";
    expected = parse("18");
    runTest(expressionString, expected, context);
    expressionString = "| {{ (on I in 1..10, P in Boolean) 3 : I != 4 }} |";
    expected = parse("18");
    runTest(expressionString, expected, context);
    expressionString = "| I in 1..10, P in Boolean: I != 4 |";
    expected = parse("18");
    runTest(expressionString, expected, context);
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context) EqualityTheory(com.sri.ai.grinder.sgdpllt.theory.equality.EqualityTheory) Type(com.sri.ai.expresso.api.Type) Expression(com.sri.ai.expresso.api.Expression) TheoryTestingSupport(com.sri.ai.grinder.sgdpllt.tester.TheoryTestingSupport) DifferenceArithmeticTheory(com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory) PropositionalTheory(com.sri.ai.grinder.sgdpllt.theory.propositional.PropositionalTheory) CompoundTheory(com.sri.ai.grinder.sgdpllt.theory.compound.CompoundTheory) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 44 with DifferenceArithmeticTheory

use of com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory in project aic-expresso by aic-sri-international.

the class RecursiveTest method runTest.

private void runTest(Rewriter rewriter, Expression initial, Expression expected, Map<Expression, Expression> symbolsAndTypes) {
    CompoundTheory theory = new CompoundTheory(new PropositionalTheory(), new DifferenceArithmeticTheory(false, true));
    Context context = new TrueContext(theory);
    context = context.registerAdditionalSymbolsAndTypes(symbolsAndTypes);
    Rewriter recursive = new Recursive(rewriter);
    Expression solution = recursive.apply(initial, context);
    System.out.println("Solution: " + solution);
    assertEquals(expected, solution);
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context) TrueContext(com.sri.ai.grinder.sgdpllt.core.TrueContext) Expression(com.sri.ai.expresso.api.Expression) DifferenceArithmeticTheory(com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory) Rewriter(com.sri.ai.grinder.sgdpllt.rewriter.api.Rewriter) PropositionalTheory(com.sri.ai.grinder.sgdpllt.theory.propositional.PropositionalTheory) CompoundTheory(com.sri.ai.grinder.sgdpllt.theory.compound.CompoundTheory) Recursive(com.sri.ai.grinder.sgdpllt.rewriter.core.Recursive) TrueContext(com.sri.ai.grinder.sgdpllt.core.TrueContext)

Example 45 with DifferenceArithmeticTheory

use of com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory in project aic-expresso by aic-sri-international.

the class SetDNFRewriterTest method setUp.

@Before
public void setUp() {
    context = new TrueContext(new CompoundTheory(new DifferenceArithmeticTheory(false, false), new TupleTheory()));
    IntegerInterval intType = new IntegerInterval(1, 10);
    context = (Context) GrinderUtil.extendRegistryWith(map("M", intType.toString(), "N", intType.toString()), Arrays.asList(intType), context);
    rewriter = new SetDNFRewriter();
}
Also used : DifferenceArithmeticTheory(com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory) IntegerInterval(com.sri.ai.expresso.type.IntegerInterval) CompoundTheory(com.sri.ai.grinder.sgdpllt.theory.compound.CompoundTheory) TrueContext(com.sri.ai.grinder.sgdpllt.core.TrueContext) TupleTheory(com.sri.ai.grinder.sgdpllt.theory.tuple.TupleTheory) SetDNFRewriter(com.sri.ai.grinder.sgdpllt.library.set.invsupport.SetDNFRewriter) Before(org.junit.Before)

Aggregations

DifferenceArithmeticTheory (com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory)46 CompoundTheory (com.sri.ai.grinder.sgdpllt.theory.compound.CompoundTheory)34 Context (com.sri.ai.grinder.sgdpllt.api.Context)28 Expression (com.sri.ai.expresso.api.Expression)25 Test (org.junit.Test)25 PropositionalTheory (com.sri.ai.grinder.sgdpllt.theory.propositional.PropositionalTheory)24 TheoryTestingSupport (com.sri.ai.grinder.sgdpllt.tester.TheoryTestingSupport)23 TrueContext (com.sri.ai.grinder.sgdpllt.core.TrueContext)22 EqualityTheory (com.sri.ai.grinder.sgdpllt.theory.equality.EqualityTheory)20 TupleTheory (com.sri.ai.grinder.sgdpllt.theory.tuple.TupleTheory)16 IntegerInterval (com.sri.ai.expresso.type.IntegerInterval)15 Before (org.junit.Before)11 Type (com.sri.ai.expresso.api.Type)8 LinearRealArithmeticTheory (com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.LinearRealArithmeticTheory)8 Theory (com.sri.ai.grinder.sgdpllt.api.Theory)7 FunctionType (com.sri.ai.expresso.type.FunctionType)5 LinkedHashMap (java.util.LinkedHashMap)5 StepSolver (com.sri.ai.grinder.sgdpllt.api.StepSolver)4 Rewriter (com.sri.ai.grinder.sgdpllt.rewriter.api.Rewriter)4 Categorical (com.sri.ai.expresso.type.Categorical)3