Search in sources :

Example 16 with RealExpressoType

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;
}
Also used : FunctionType(com.sri.ai.expresso.type.FunctionType) IntegerInterval(com.sri.ai.expresso.type.IntegerInterval) IntegerExpressoType(com.sri.ai.expresso.type.IntegerExpressoType) TupleType(com.sri.ai.expresso.type.TupleType) RealExpressoType(com.sri.ai.expresso.type.RealExpressoType) RealInterval(com.sri.ai.expresso.type.RealInterval)

Aggregations

RealExpressoType (com.sri.ai.expresso.type.RealExpressoType)16 RealInterval (com.sri.ai.expresso.type.RealInterval)16 Expression (com.sri.ai.expresso.api.Expression)12 Type (com.sri.ai.expresso.api.Type)11 FunctionType (com.sri.ai.expresso.type.FunctionType)7 IntegerExpressoType (com.sri.ai.expresso.type.IntegerExpressoType)7 IntegerInterval (com.sri.ai.expresso.type.IntegerInterval)7 IntensionalSet (com.sri.ai.expresso.api.IntensionalSet)6 TupleType (com.sri.ai.expresso.type.TupleType)6 SingleVariableLinearRealArithmeticConstraint (com.sri.ai.grinder.theory.linearrealarithmetic.SingleVariableLinearRealArithmeticConstraint)6 IndexExpressionsSet (com.sri.ai.expresso.api.IndexExpressionsSet)4 ExtensionalIndexExpressionsSet (com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet)4 SingleVariableLinearRealArithmeticConstraint (com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.SingleVariableLinearRealArithmeticConstraint)4 Rational (com.sri.ai.util.math.Rational)4 Context (com.sri.ai.grinder.api.Context)3 Context (com.sri.ai.grinder.sgdpllt.api.Context)3 SingleVariableDifferenceArithmeticConstraint (com.sri.ai.grinder.theory.differencearithmetic.SingleVariableDifferenceArithmeticConstraint)3 DefaultCountingFormula (com.sri.ai.expresso.core.DefaultCountingFormula)2 Categorical (com.sri.ai.expresso.type.Categorical)2 TrueContext (com.sri.ai.grinder.core.TrueContext)2