use of com.sri.ai.expresso.type.RealExpressoType in project aic-expresso by aic-sri-international.
the class GrinderUtil method isTypeSubtypeOf.
/**
* Test if a type is a subtype of another type.
*
* @param type the type to test if it is a subtype.
* @param ofType type to be tested if a subtype of.
*
* @return true if 'type' is a subtype of 'ofType', false otherwise.
*/
public static boolean isTypeSubtypeOf(Type type, Type ofType) {
boolean result = false;
if (type.equals(ofType)) {
result = true;
} else {
if (type instanceof FunctionType && ofType instanceof FunctionType) {
FunctionType typeFunctionType = (FunctionType) type;
FunctionType ofTypeFunctionType = (FunctionType) ofType;
if (typeFunctionType.getArity() == ofTypeFunctionType.getArity()) {
result = isTypeSubtypeOf(typeFunctionType.getCodomain(), ofTypeFunctionType.getCodomain()) && IntStream.range(0, typeFunctionType.getArity()).allMatch(idx -> isTypeSubtypeOf(ofTypeFunctionType.getArgumentTypes().get(idx), typeFunctionType.getArgumentTypes().get(idx)));
}
} else if (type instanceof TupleType && ofType instanceof TupleType) {
TupleType typeTupleType = (TupleType) type;
TupleType ofTypeTupleType = (TupleType) ofType;
if (typeTupleType.getArity() == ofTypeTupleType.getArity()) {
result = IntStream.range(0, typeTupleType.getArity()).allMatch(idx -> isTypeSubtypeOf(typeTupleType.getElementTypes().get(idx), ofTypeTupleType.getElementTypes().get(idx)));
}
} else if (type instanceof IntegerInterval) {
IntegerInterval typeIntegerInterval = (IntegerInterval) type;
if (ofType instanceof IntegerInterval) {
IntegerInterval ofTypeIntegerInterval = (IntegerInterval) ofType;
result = ofTypeIntegerInterval.isSuperset(typeIntegerInterval.getNonStrictLowerBound(), typeIntegerInterval.getNonStrictUpperBound());
} else if (ofType instanceof RealInterval) {
RealInterval ofTypeRealInterval = (RealInterval) ofType;
result = ofTypeRealInterval.isSuperset(typeIntegerInterval.getNonStrictLowerBound(), typeIntegerInterval.getNonStrictUpperBound());
} else if (ofType instanceof IntegerExpressoType || ofType instanceof RealExpressoType) {
result = true;
}
} else if (type instanceof IntegerExpressoType) {
if (ofType instanceof IntegerInterval) {
IntegerInterval ofTypeIntegerInterval = (IntegerInterval) ofType;
result = ofTypeIntegerInterval.noLowerBound() && ofTypeIntegerInterval.noUpperBound();
} else if (ofType instanceof RealInterval) {
RealInterval ofTypeRealInterval = (RealInterval) ofType;
result = ofTypeRealInterval.noLowerBound() && ofTypeRealInterval.noUpperBound();
} else if (ofType instanceof RealExpressoType) {
result = true;
}
} else if (type instanceof RealInterval) {
RealInterval typeRealInterval = (RealInterval) type;
if (ofType instanceof RealInterval) {
RealInterval ofTypeRealInterval = (RealInterval) ofType;
result = ofTypeRealInterval.isSuperset(typeRealInterval.getLowerBound(), typeRealInterval.getUpperBound());
} else if (ofType instanceof RealExpressoType) {
result = true;
}
} else if (type instanceof RealExpressoType) {
if (ofType instanceof RealInterval) {
RealInterval ofTypeRealInterval = (RealInterval) ofType;
result = ofTypeRealInterval.noLowerBound() && ofTypeRealInterval.noUpperBound();
} else if (ofType instanceof RealExpressoType) {
result = true;
}
}
}
return result;
}
Aggregations