Search in sources :

Example 11 with FunctionType

use of com.sri.ai.expresso.type.FunctionType in project aic-expresso by aic-sri-international.

the class GrinderUtilTest method testIsRealIntervalSubtypeOf.

@Test
public void testIsRealIntervalSubtypeOf() {
    RealInterval realInterval = new RealInterval("Real");
    Assert.assertFalse(isTypeSubtypeOf(realInterval, BOOLEAN_TYPE));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, INTEGER_TYPE));
    Assert.assertTrue(isTypeSubtypeOf(realInterval, REAL_TYPE));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new IntegerInterval("Integer")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new IntegerInterval("0..4")));
    Assert.assertTrue(isTypeSubtypeOf(realInterval, new RealInterval("Real")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new RealInterval("[-0.5;4.5]")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new FunctionType(realInterval)));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new FunctionType(realInterval, realInterval)));
    realInterval = new RealInterval("[0;4]");
    Assert.assertFalse(isTypeSubtypeOf(realInterval, INTEGER_TYPE));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new IntegerInterval("0..4")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new IntegerInterval("1..4")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new IntegerInterval("0..3")));
    Assert.assertTrue(isTypeSubtypeOf(realInterval, REAL_TYPE));
    Assert.assertTrue(isTypeSubtypeOf(realInterval, new RealInterval("Real")));
    Assert.assertTrue(isTypeSubtypeOf(realInterval, new RealInterval("[0;4]")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new RealInterval("]0;4]")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new RealInterval("[0;4[")));
    realInterval = new RealInterval("[-infinity;4]");
    Assert.assertFalse(isTypeSubtypeOf(realInterval, INTEGER_TYPE));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new IntegerInterval("0..4")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new IntegerInterval("1..4")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new IntegerInterval("0..3")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new IntegerInterval("-infinity..5")));
    Assert.assertTrue(isTypeSubtypeOf(realInterval, REAL_TYPE));
    Assert.assertTrue(isTypeSubtypeOf(realInterval, new RealInterval("Real")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new RealInterval("[0;4]")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new RealInterval("]0;4]")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new RealInterval("[0;4[")));
    Assert.assertTrue(isTypeSubtypeOf(realInterval, new RealInterval("[-infinity;4]")));
    Assert.assertTrue(isTypeSubtypeOf(realInterval, new RealInterval("]-infinity;4]")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new RealInterval("[-infinity;4[")));
    realInterval = new RealInterval("[0;infinity]");
    Assert.assertFalse(isTypeSubtypeOf(realInterval, INTEGER_TYPE));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new IntegerInterval("0..4")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new IntegerInterval("1..4")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new IntegerInterval("0..3")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new IntegerInterval("-1..infinity")));
    Assert.assertTrue(isTypeSubtypeOf(realInterval, REAL_TYPE));
    Assert.assertTrue(isTypeSubtypeOf(realInterval, new RealInterval("Real")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new RealInterval("[0;4]")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new RealInterval("]0;4]")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new RealInterval("[0;4[")));
    Assert.assertTrue(isTypeSubtypeOf(realInterval, new RealInterval("[0;infinity]")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new RealInterval("]0;infinity]")));
    Assert.assertTrue(isTypeSubtypeOf(realInterval, new RealInterval("[0;infinity[")));
    realInterval = new RealInterval("[-infinity;infinity]");
    Assert.assertFalse(isTypeSubtypeOf(realInterval, INTEGER_TYPE));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new IntegerInterval("0..4")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new IntegerInterval("1..4")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new IntegerInterval("0..3")));
    Assert.assertTrue(isTypeSubtypeOf(realInterval, REAL_TYPE));
    Assert.assertTrue(isTypeSubtypeOf(realInterval, new RealInterval("Real")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new RealInterval("[0;4]")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new RealInterval("]0;4]")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new RealInterval("[0;4[")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new RealInterval("[-infinity;4]")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new RealInterval("]-infinity;4]")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new RealInterval("[-infinity;4[")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new RealInterval("[0;infinity]")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new RealInterval("]0;infinity]")));
    Assert.assertFalse(isTypeSubtypeOf(realInterval, new RealInterval("[0;infinity[")));
    Assert.assertTrue(isTypeSubtypeOf(realInterval, new RealInterval("[-infinity;infinity]")));
    Assert.assertTrue(isTypeSubtypeOf(realInterval, new RealInterval("]-infinity;infinity]")));
    Assert.assertTrue(isTypeSubtypeOf(realInterval, new RealInterval("[-infinity;infinity[")));
    Assert.assertTrue(isTypeSubtypeOf(realInterval, new RealInterval("]-infinity;infinity[")));
}
Also used : IntegerInterval(com.sri.ai.expresso.type.IntegerInterval) FunctionType(com.sri.ai.expresso.type.FunctionType) RealInterval(com.sri.ai.expresso.type.RealInterval) Test(org.junit.Test)

Example 12 with FunctionType

use of com.sri.ai.expresso.type.FunctionType in project aic-expresso by aic-sri-international.

the class SetOfArgumentTuplesForFunctionOccurringInExpression method compute.

public static Expression compute(Expression fName, FunctionType fType, Expression e) {
    List<Expression> unionArgs = new ArrayList<>();
    Predicate<Expression> isF = expr -> expr.equals(fName);
    Predicate<Expression> isFApplication = expr -> expr.hasFunctor(fName) && expr.numberOfArguments() == fType.getArity();
    computeUnionArgs(unionArgs, isF, fType, isFApplication, e);
    Expression result = makeUnion(unionArgs);
    return result;
}
Also used : CountingFormula(com.sri.ai.expresso.api.CountingFormula) LambdaExpression(com.sri.ai.expresso.api.LambdaExpression) SubExpressionsDepthFirstIterator(com.sri.ai.expresso.helper.SubExpressionsDepthFirstIterator) Type(com.sri.ai.expresso.api.Type) ForAll(com.sri.ai.grinder.sgdpllt.library.boole.ForAll) Expressions(com.sri.ai.expresso.helper.Expressions) Expression(com.sri.ai.expresso.api.Expression) ExtensionalSets(com.sri.ai.grinder.sgdpllt.library.set.extensional.ExtensionalSets) IntensionalSet(com.sri.ai.expresso.api.IntensionalSet) Collectors(java.util.stream.Collectors) IfThenElse(com.sri.ai.grinder.sgdpllt.library.controlflow.IfThenElse) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression) ArrayList(java.util.ArrayList) QuantifiedExpressionWithABody(com.sri.ai.expresso.api.QuantifiedExpressionWithABody) FunctionType(com.sri.ai.expresso.type.FunctionType) List(java.util.List) ThereExists(com.sri.ai.grinder.sgdpllt.library.boole.ThereExists) Predicate(com.google.common.base.Predicate) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet) Util(com.sri.ai.util.Util) FunctorConstants(com.sri.ai.grinder.sgdpllt.library.FunctorConstants) Sets(com.sri.ai.grinder.sgdpllt.library.set.Sets) LambdaExpression(com.sri.ai.expresso.api.LambdaExpression) Expression(com.sri.ai.expresso.api.Expression) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression) ArrayList(java.util.ArrayList)

Example 13 with FunctionType

use of com.sri.ai.expresso.type.FunctionType in project aic-expresso by aic-sri-international.

the class TheoryTestingSupport method getTestingVariableType.

/**
 * Get the type associated with the given testing variable.
 *
 * @param variable
 *            the testing variable whose type is to be returned.
 * @return the type of the given testing variable.
 */
default Type getTestingVariableType(String variable) {
    String variableName = getVariableName(variable);
    Type result = getVariableNamesAndTypesForTesting().get(variableName);
    // We need to check if the variable is a function application
    // because if it is we need to use the codomain of its function type
    // as the type of the testing variable.
    Expression variableExpresison = parse(variable);
    Expression functor = variableExpresison.getFunctor();
    if (functor != null) {
        result = ((FunctionType) result).getCodomain();
    }
    return result;
}
Also used : Type(com.sri.ai.expresso.api.Type) FunctionType(com.sri.ai.expresso.type.FunctionType) Expression(com.sri.ai.expresso.api.Expression)

Example 14 with FunctionType

use of com.sri.ai.expresso.type.FunctionType in project aic-expresso by aic-sri-international.

the class CompoundTheoryTestingSupport method setVariableNamesAndTypesForTesting.

/**
 * This is overridden so that given variables and types for testing are distributed to their
 * respective theories according to {@link #isSuitableFor(Type)}.
 */
@Override
public void setVariableNamesAndTypesForTesting(Map<String, Type> variableNamesAndTypesForTesting) {
    // First ensure the compound set of variables names and type information is setup correctly
    super.setVariableNamesAndTypesForTesting(variableNamesAndTypesForTesting);
    // Then update the sub-theories so that they share appropriate subsets of this information
    Map<Theory, Map<String, Type>> mapForSubTheory = map();
    for (Theory subTheory : getTheory().getSubTheories()) {
        mapForSubTheory.put(subTheory, map());
    }
    for (Map.Entry<String, Type> variableNameAndType : getVariableNamesAndTypesForTesting().entrySet()) {
        String variableName = variableNameAndType.getKey();
        Type type = variableNameAndType.getValue();
        for (Theory subTheory : getTheory().getSubTheories()) {
            if (subTheory.isSuitableFor(type) || (type instanceof FunctionType && subTheory.isSuitableFor(((FunctionType) type).getCodomain()))) {
                mapForSubTheory.get(subTheory).put(variableName, type);
            }
        }
    }
    for (Map.Entry<Theory, TheoryTestingSupport> entry : getTheoryToTestingSupport().entrySet()) {
        Map<String, Type> forThisSubTheory = mapForSubTheory.get(entry.getKey());
        entry.getValue().setVariableNamesAndTypesForTesting(forThisSubTheory);
    }
}
Also used : Type(com.sri.ai.expresso.api.Type) FunctionType(com.sri.ai.expresso.type.FunctionType) Theory(com.sri.ai.grinder.api.Theory) AbstractTheoryTestingSupport(com.sri.ai.grinder.core.constraint.AbstractTheoryTestingSupport) TheoryTestingSupport(com.sri.ai.grinder.tester.TheoryTestingSupport) FunctionType(com.sri.ai.expresso.type.FunctionType) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 15 with FunctionType

use of com.sri.ai.expresso.type.FunctionType in project aic-expresso by aic-sri-international.

the class BruteForceFunctionTheoryTestingSupport method makeFunctionApplication.

protected Expression makeFunctionApplication(String functorName, FunctionType functionType) {
    // Generate arguments for the application
    List<Expression> args = new ArrayList<>();
    for (Type argType : functionType.getArgumentTypes()) {
        // If constants supported, use at random
        if (argType.isSampleUniquelyNamedConstantSupported() && getRandom().nextBoolean()) {
            args.add(argType.sampleUniquelyNamedConstant(getRandom()));
        } else {
            // Otherwise retrieve a term variable matching that type and use it
            String termVariable = pickTestingVariableAtRandom(getTermVariableNamesAndTypesForTesting(), argType, variableName -> true);
            Type termType = getTermVariableNamesAndTypesForTesting().get(termVariable);
            if (termType instanceof FunctionType) {
                // Allow for nested function applications
                args.add(makeFunctionApplication(termVariable, (FunctionType) termType));
            } else {
                // Otherwise just assign the variable for the term.
                args.add(parse(termVariable));
            }
        }
    }
    Expression result = Expressions.apply(functorName, args.toArray(new Object[args.size()]));
    return result;
}
Also used : Type(com.sri.ai.expresso.api.Type) FunctionType(com.sri.ai.expresso.type.FunctionType) Expression(com.sri.ai.expresso.api.Expression) FunctionType(com.sri.ai.expresso.type.FunctionType) Util.mapIntoArrayList(com.sri.ai.util.Util.mapIntoArrayList) ArrayList(java.util.ArrayList)

Aggregations

FunctionType (com.sri.ai.expresso.type.FunctionType)57 Test (org.junit.Test)28 Expression (com.sri.ai.expresso.api.Expression)23 Type (com.sri.ai.expresso.api.Type)22 IntegerInterval (com.sri.ai.expresso.type.IntegerInterval)17 Context (com.sri.ai.grinder.api.Context)15 Context (com.sri.ai.grinder.sgdpllt.api.Context)14 IndexExpressionsSet (com.sri.ai.expresso.api.IndexExpressionsSet)13 ArrayList (java.util.ArrayList)13 RealInterval (com.sri.ai.expresso.type.RealInterval)12 TheoryTestingSupport (com.sri.ai.grinder.sgdpllt.tester.TheoryTestingSupport)11 TheoryTestingSupport (com.sri.ai.grinder.tester.TheoryTestingSupport)11 TupleType (com.sri.ai.expresso.type.TupleType)10 StepSolver (com.sri.ai.grinder.api.StepSolver)10 StepSolver (com.sri.ai.grinder.sgdpllt.api.StepSolver)10 UnificationStepSolver (com.sri.ai.grinder.sgdpllt.theory.base.UnificationStepSolver)10 UnificationStepSolver (com.sri.ai.grinder.theory.base.UnificationStepSolver)10 Ignore (org.junit.Ignore)10 IntensionalSet (com.sri.ai.expresso.api.IntensionalSet)9 ExtensionalIndexExpressionsSet (com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet)9