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[")));
}
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;
}
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;
}
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);
}
}
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;
}
Aggregations