Search in sources :

Example 16 with IntensionalSet

use of com.sri.ai.expresso.api.IntensionalSet in project aic-expresso by aic-sri-international.

the class IntensionalUnionIntersectionEqualToEmptySetSimplifier method standardizeApart.

private static IntensionalSet standardizeApart(IntensionalSet intensionalSet, List<Expression> forAllsSoFar, Context context) {
    IntensionalSet result = intensionalSet;
    if (forAllsSoFar.size() > 0) {
        // May need to standardize apart
        List<Expression> tupleArgs = new ArrayList<>();
        for (Expression forAll : forAllsSoFar) {
            tupleArgs.add(ForAll.getIndexExpression(forAll));
            tupleArgs.add(ForAll.getBody(forAll));
        }
        Expression tupleOfForAlls = Expressions.makeTuple(tupleArgs);
        List<Expression> originalIndexes = IndexExpressions.getIndices(intensionalSet.getIndexExpressions());
        List<Expression> saIndexes = new ArrayList<>();
        for (Expression index : originalIndexes) {
            Expression saIndex = Expressions.primedUntilUnique(index, tupleOfForAlls, context);
            saIndexes.add(saIndex);
        }
        if (!originalIndexes.equals(saIndexes)) {
            // We need to update intensional set with standardized apart indices
            IndexExpressionsSet saIndexExpressionsSet = intensionalSet.getIndexExpressions();
            Expression saHead = intensionalSet.getHead();
            Expression saCondition = intensionalSet.getCondition();
            Context intensionalSetContext = context.extendWith(saIndexExpressionsSet);
            for (int i = 0; i < originalIndexes.size(); i++) {
                Expression originalIndex = originalIndexes.get(i);
                Expression saIndex = saIndexes.get(i);
                if (!originalIndex.equals(saIndex)) {
                    saIndexExpressionsSet = saIndexExpressionsSet.replaceSymbol(originalIndex, saIndex, context);
                    saHead = saHead.replaceAllOccurrences(originalIndex, saIndex, intensionalSetContext);
                    saCondition = saCondition.replaceAllOccurrences(originalIndex, saIndex, intensionalSetContext);
                }
            }
            result = (IntensionalSet) IntensionalSet.intensionalSetOfSameKindAs(intensionalSet, saIndexExpressionsSet, saHead, saCondition);
        }
    }
    return result;
}
Also used : Context(com.sri.ai.grinder.api.Context) IntensionalSet(com.sri.ai.expresso.api.IntensionalSet) Expression(com.sri.ai.expresso.api.Expression) ArrayList(java.util.ArrayList) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet)

Example 17 with IntensionalSet

use of com.sri.ai.expresso.api.IntensionalSet in project aic-expresso by aic-sri-international.

the class IntensionalUnionIntersectionEqualToEmptySetSimplifier method intensionalSetToForAllAndPhi.

private static Expression[] intensionalSetToForAllAndPhi(IntensionalSet intensionalSet, List<Expression> forAllsSoFar, Context context) {
    IntensionalSet saIntensionalSet = standardizeApart(intensionalSet, forAllsSoFar, context);
    Expression forAll = ForAll.make(saIntensionalSet.getIndexExpressions(), saIntensionalSet.getCondition());
    Expression phi = saIntensionalSet.getHead();
    Expression[] result = new Expression[] { forAll, phi };
    return result;
}
Also used : IntensionalSet(com.sri.ai.expresso.api.IntensionalSet) Expression(com.sri.ai.expresso.api.Expression)

Example 18 with IntensionalSet

use of com.sri.ai.expresso.api.IntensionalSet in project aic-expresso by aic-sri-international.

the class IntensionalUnionIntersectionEqualToEmptySetSimplifier method simplify.

public static Expression simplify(Expression expression, Context context) {
    Expression result = expression;
    if (Equality.isEquality(expression) && expression.numberOfArguments() == 2) {
        Expression intersection = null;
        Expression emptySet = null;
        for (Expression arg : expression.getArguments()) {
            if (Expressions.hasFunctor(arg, FunctorConstants.INTERSECTION)) {
                intersection = arg;
            } else if (Sets.isEmptySet(arg)) {
                emptySet = arg;
            }
        }
        if (intersection != null && emptySet != null) {
            // (&cup;<sub>i &isin; D:C</sub>&Phi;) &cap; (&cup;<sub>i' &isin; D':C'</sub>&Phi;') = &empty;
            if (intersection.getArguments().stream().allMatch(Sets::isIntensionalUnion)) {
                List<Expression> forAlls = new ArrayList<>();
                List<Expression> phis = new ArrayList<>();
                // &forall; i &isin; D : C &rArr; &forall; i' &isin; D' : C' &rArr;  (&Phi; &cap; &Phi;' = &empty;)
                for (Expression intensionalUnion : intersection.getArguments()) {
                    IntensionalSet intensionalSet = (IntensionalSet) intensionalUnion.get(0);
                    Expression[] forAllAndPhi = intensionalSetToForAllAndPhi(intensionalSet, forAlls, context);
                    Expression forAll = forAllAndPhi[0];
                    Expression phi = forAllAndPhi[1];
                    handleNestedForAlls(forAll, forAlls);
                    phis.add(phi);
                }
                result = Equality.make(Expressions.apply(FunctorConstants.INTERSECTION, phis), emptySet);
                for (int i = forAlls.size() - 1; i >= 0; i--) {
                    Expression forAll = forAlls.get(i);
                    Expression body = Implication.make(ForAll.getBody(forAll), result);
                    result = ForAll.make(ForAll.getIndexExpression(forAll), body);
                }
            }
        }
    }
    return result;
}
Also used : IntensionalSet(com.sri.ai.expresso.api.IntensionalSet) Expression(com.sri.ai.expresso.api.Expression) Sets(com.sri.ai.grinder.library.set.Sets) ArrayList(java.util.ArrayList)

Example 19 with IntensionalSet

use of com.sri.ai.expresso.api.IntensionalSet in project aic-expresso by aic-sri-international.

the class IntensionalUnionToUnionsOfIntensionalSetsOfBaseTypeSimplifier method simplify.

public static Expression simplify(Expression expression, Context context) {
    Expression result = expression;
    if (Sets.isIntensionalUnion(expression)) {
        IntensionalSet intensionalMultiSet = (IntensionalSet) expression.get(0);
        Expression intensionalHead = intensionalMultiSet.getHead();
        // TODO - base case set is an extensional uniset?
        if (Sets.isExtensionalSet(intensionalHead) && intensionalHead.numberOfArguments() == 1) {
            Expression baseTypeHead = intensionalHead.get(0);
            result = IntensionalSet.intensionalMultiSet(intensionalMultiSet.getIndexExpressions(), baseTypeHead, intensionalMultiSet.getCondition());
        } else // Determine if recursive case
        if (intensionalHead.hasFunctor(FunctorConstants.UNION)) {
            // Union( {{ (on I)   Set1 union ... union Set_m : C }} )
            // --->
            // recurse(Union( {{ (on I) Set1 : C }} ))
            // union ... union
            // recurse(Union( {{ (on I) Set_m : C }} ))
            List<Expression> recursedUnionArgs = new ArrayList<>();
            for (Expression setI : intensionalHead.getArguments()) {
                Expression setIUnionArg = IntensionalSet.intensionalMultiSet(intensionalMultiSet.getIndexExpressions(), setI, intensionalMultiSet.getCondition());
                Expression setIUnion = Expressions.apply(FunctorConstants.INTENSIONAL_UNION, setIUnionArg);
                Expression setIRecurseResult = simplify(setIUnion, context);
                recursedUnionArgs.add(setIRecurseResult);
            }
            result = Sets.makeUnion(recursedUnionArgs.toArray(new Expression[recursedUnionArgs.size()]));
        }
    }
    return result;
}
Also used : IntensionalSet(com.sri.ai.expresso.api.IntensionalSet) Expression(com.sri.ai.expresso.api.Expression) List(java.util.List) ArrayList(java.util.ArrayList)

Example 20 with IntensionalSet

use of com.sri.ai.expresso.api.IntensionalSet in project aic-expresso by aic-sri-international.

the class InversionSimplifier method isFunctionOnIntensionalSetWithSingleIndex.

private static boolean isFunctionOnIntensionalSetWithSingleIndex(Object functor, Expression expression) {
    boolean result = false;
    if (((functor == null && Expressions.isFunctionApplicationWithArguments(expression)) || expression.hasFunctor(functor)) && expression.numberOfArguments() == 1) {
        Expression expressionArg1 = expression.get(0);
        if (Sets.isIntensionalSet(expressionArg1)) {
            IntensionalSet intensionalSet = (IntensionalSet) expressionArg1;
            IndexExpressionsSet indexExpressionsSet = intensionalSet.getIndexExpressions();
            List<Expression> indices = IndexExpressions.getIndices(indexExpressionsSet);
            if (indices.size() == 1) {
                result = true;
            }
        }
    }
    return result;
}
Also used : IntensionalSet(com.sri.ai.expresso.api.IntensionalSet) Expression(com.sri.ai.expresso.api.Expression) ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet)

Aggregations

Expression (com.sri.ai.expresso.api.Expression)46 IntensionalSet (com.sri.ai.expresso.api.IntensionalSet)46 IndexExpressionsSet (com.sri.ai.expresso.api.IndexExpressionsSet)24 ExtensionalIndexExpressionsSet (com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet)15 ArrayList (java.util.ArrayList)12 Type (com.sri.ai.expresso.api.Type)11 Context (com.sri.ai.grinder.api.Context)9 Context (com.sri.ai.grinder.sgdpllt.api.Context)9 RealExpressoType (com.sri.ai.expresso.type.RealExpressoType)8 RealInterval (com.sri.ai.expresso.type.RealInterval)8 Rational (com.sri.ai.util.math.Rational)8 TrueContext (com.sri.ai.grinder.core.TrueContext)5 List (java.util.List)5 CountingFormula (com.sri.ai.expresso.api.CountingFormula)4 FunctionType (com.sri.ai.expresso.type.FunctionType)4 IntegerExpressoType (com.sri.ai.expresso.type.IntegerExpressoType)4 IntegerInterval (com.sri.ai.expresso.type.IntegerInterval)4 TupleType (com.sri.ai.expresso.type.TupleType)4 TrueContext (com.sri.ai.grinder.sgdpllt.core.TrueContext)3 Beta (com.google.common.annotations.Beta)2