Search in sources :

Example 71 with IndexExpressionsSet

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

the class DefaultIntensionalBound method applyFunctionToBound.

protected DefaultIntensionalBound applyFunctionToBound(Expression f, Expression variableName, Bound bound, Theory theory, Context context) {
    if (!bound.isIntensionalBound()) {
        return null;
    }
    IntensionalSet intensionalBound = (IntensionalSet) bound;
    IndexExpressionsSet indexExpressions = intensionalBound.getIndexExpressions();
    Expression Head = intensionalBound.getHead();
    Expression condition = intensionalBound.getCondition();
    Expression fOfHead = f.replaceAllOccurrences(variableName, Head, context);
    Expression evaluation = theory.evaluate(fOfHead, context);
    DefaultIntensionalBound result = new DefaultIntensionalBound(indexExpressions, evaluation, condition);
    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)

Example 72 with IndexExpressionsSet

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

the class IntersectionIntensionalSetsSimplifier method standardizeApartIntensionalSets.

public static IntensionalSet standardizeApartIntensionalSets(IntensionalSet intensionalSet, IntensionalSet fromOtherIntensionalSet, Context context) {
    IntensionalSet result = intensionalSet;
    IndexExpressionsSet intensionalSetIndexes = intensionalSet.getIndexExpressions();
    IndexExpressionsSet fromOtherIntensionalSetIn = fromOtherIntensionalSet.getIndexExpressions();
    List<Expression> overlappingIndexNames = new ArrayList<>();
    for (Expression intensionalSetIndex : IndexExpressions.getIndices(intensionalSetIndexes)) {
        if (IndexExpressions.indexExpressionsContainIndex(fromOtherIntensionalSetIn, intensionalSetIndex)) {
            overlappingIndexNames.add(intensionalSetIndex);
        }
    }
    if (overlappingIndexNames.size() > 0) {
        Expression combinedExpression = And.make(intensionalSet, fromOtherIntensionalSet);
        List<Expression> newIndexNames = new ArrayList<>();
        for (Expression overlappingIndex : overlappingIndexNames) {
            Expression newIndexName = Expressions.makeUniqueVariable(overlappingIndex.toString(), combinedExpression, context);
            newIndexNames.add(newIndexName);
        }
        SyntaxTree resultSyntaxTree = result.getSyntaxTree();
        for (int i = 0; i < newIndexNames.size(); i++) {
            Expression replaced = overlappingIndexNames.get(i);
            Expression replacement = newIndexNames.get(i);
            resultSyntaxTree = resultSyntaxTree.replaceSubTreesAllOccurrences(replaced.getSyntaxTree(), replacement.getSyntaxTree());
        }
        result = (IntensionalSet) Expressions.makeFromSyntaxTree(resultSyntaxTree);
    }
    return result;
}
Also used : IntensionalSet(com.sri.ai.expresso.api.IntensionalSet) SyntaxTree(com.sri.ai.expresso.api.SyntaxTree) Expression(com.sri.ai.expresso.api.Expression) ArrayList(java.util.ArrayList) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet)

Example 73 with IndexExpressionsSet

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

the class IntersectionIntensionalSetsSimplifier method simplify.

public static Expression simplify(Expression expression, Context context) {
    Expression result = expression;
    if (expression.hasFunctor(FunctorConstants.INTERSECTION)) {
        List<Expression> intensionalMultiSetArgs = new ArrayList<>();
        List<Expression> nonIntensionalMultiSetArgs = new ArrayList<>();
        for (Expression arg : expression.getArguments()) {
            if (Sets.isIntensionalMultiSet(arg)) {
                intensionalMultiSetArgs.add(arg);
            } else {
                nonIntensionalMultiSetArgs.add(arg);
            }
        }
        if (intensionalMultiSetArgs.size() > 1) {
            boolean resultIsEmptySet = false;
            IntensionalSet intersectedMultiSet = (IntensionalSet) intensionalMultiSetArgs.get(0);
            for (int i = 1; i < intensionalMultiSetArgs.size() && !resultIsEmptySet; i++) {
                IntensionalSet otherMultiSet = standardizeApartIntensionalSets((IntensionalSet) intensionalMultiSetArgs.get(i), intersectedMultiSet, context);
                // {{ (on I1) H1 : C1 }} intersection {{ (on I2) H2 : C2 }}
                // ---->
                // {{ (on I1) H1 : C1 and evaluate(there exists I2 : C2 and H2 = H1) }}
                IndexExpressionsSet i1 = intersectedMultiSet.getIndexExpressions();
                IndexExpressionsSet i2 = otherMultiSet.getIndexExpressions();
                Expression h1 = intersectedMultiSet.getHead();
                Expression h2 = otherMultiSet.getHead();
                Expression c1 = intersectedMultiSet.getCondition();
                Expression c2 = otherMultiSet.getCondition();
                Expression thereExists = ThereExists.make(i2, And.make(c2, Equality.make(h2, h1)));
                Context i1ExtendedContext = context.extendWith(i1);
                Expression thereExistsEvaluated = context.getTheory().evaluate(thereExists, i1ExtendedContext);
                if (thereExistsEvaluated.equals(false)) {
                    // They don't intersect, which means you have an empty
                    // set in the intersection, which means the whole thing
                    // results in the empty set.
                    resultIsEmptySet = true;
                } else if (!thereExistsEvaluated.equals(true)) {
                    // If we have a condition, other than false and true
                    // we will want to extend the current result by the condition
                    Expression extendedCondition = And.make(c1, thereExistsEvaluated);
                    intersectedMultiSet = (IntensionalSet) IntensionalSet.intensionalMultiSet(i1, h1, extendedCondition);
                    // Ensure we don't have a false condition.
                    Expression simplifiedIntersectedMultiSet = context.getTheory().evaluate(intersectedMultiSet, context);
                    if (Sets.isEmptySet(simplifiedIntersectedMultiSet)) {
                        resultIsEmptySet = true;
                    }
                }
            }
            if (resultIsEmptySet) {
                result = Sets.EMPTY_SET;
            } else if (nonIntensionalMultiSetArgs.size() > 0) {
                List<Expression> intersectedArgs = new ArrayList<>();
                intersectedArgs.add(intersectedMultiSet);
                intersectedArgs.addAll(nonIntensionalMultiSetArgs);
                result = Sets.makeIntersection(intersectedArgs.toArray(new Expression[intersectedArgs.size()]));
            } else {
                result = intersectedMultiSet;
            }
        }
    }
    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) ArrayList(java.util.ArrayList) List(java.util.List) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet)

Example 74 with IndexExpressionsSet

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

the class SetOfArgumentTuplesForFunctionOccurringInExpression method computeUnionArgs.

private static void computeUnionArgs(List<Expression> unionArgs, Predicate<Expression> isF, FunctionType fType, Predicate<Expression> isFApplication, Expression e) {
    // if E does not contain &fnof;, oc<sub>&fnof;</sub>[E] is &empty;
    if (!containsF(e, isF)) {
        unionArgs.add(Sets.EMPTY_SET);
    } else // if E is &fnof;(t) for t a tuple, oc<sub>&fnof;</sub>[E] is {t}
    if (isFApplication.apply(e)) {
        Expression tupleT = Expressions.makeTuple(e.getArguments());
        Expression setT = ExtensionalSets.makeUniSet(tupleT);
        unionArgs.add(setT);
    } else // if E is &fnof;, oc<sub>&fnof;</sub>[E] is &Alpha;
    if (isF.apply(e)) {
        List<Expression> domainTypes = new ArrayList<>();
        for (Type domainType : fType.getArgumentTypes()) {
            domainTypes.add(Expressions.parse(domainType.getName()));
        }
        Expression tupleT = Expressions.makeTuple(domainTypes);
        Expression setT = ExtensionalSets.makeUniSet(tupleT);
        unionArgs.add(setT);
    } else // if E is g(E&prime;) for g a function symbol distinct from &fnof; and t a k-tuple of expressions
    if (Expressions.isFunctionApplicationWithArguments(e)) {
        // if g(E&prime;) is if C the E<sub>1</sub> else E<sub>2</sub> and C does not contain &fnof;
        if (IfThenElse.isIfThenElse(e) && !containsF(IfThenElse.condition(e), isF)) {
            // then oc<sub>&fnof;</sub>[E] is<br>
            // if C then oc<sub>&fnof;</sub>[E<sub>1</sub>] else oc<sub>&fnof;</sub>[E<sub>2</sub>]
            Expression e1 = IfThenElse.thenBranch(e);
            List<Expression> thenUnionArgs = new ArrayList<>();
            computeUnionArgs(thenUnionArgs, isF, fType, isFApplication, e1);
            Expression thenBranch = makeUnion(thenUnionArgs);
            Expression e2 = IfThenElse.elseBranch(e);
            List<Expression> elseUnionArgs = new ArrayList<>();
            computeUnionArgs(elseUnionArgs, isF, fType, isFApplication, e2);
            Expression elseBranch = makeUnion(elseUnionArgs);
            Expression ifThenElse = IfThenElse.make(IfThenElse.condition(e), thenBranch, elseBranch);
            unionArgs.add(ifThenElse);
        } else {
            // oc<sub>&fnof;</sub>[t<sub>1</sub>] &cup; &hellip;	&cup; oc<sub>&fnof;</sub>[t<sub>k</sub>]
            for (Expression t : e.getArguments()) {
                computeUnionArgs(unionArgs, isF, fType, isFApplication, t);
            }
        }
    } else // quantified expression,
    if (isArbitraryQuantifier(e) && !containsF(getQuantifierCondition(e), isF)) {
        // then oc<sub>&fnof;</sub>[E] is<br>
        // &bigcup;<sub>x:C</sub>oc<sub>&fnof;</sub>[E&prime;]
        QuantifiedExpression q = (QuantifiedExpression) e;
        IndexExpressionsSet x = q.getIndexExpressions();
        Expression c = getQuantifierCondition(e);
        Expression ePrime = getQuantifiedExpression(q);
        List<Expression> ePrimeUnionArgs = new ArrayList<>();
        computeUnionArgs(ePrimeUnionArgs, isF, fType, isFApplication, ePrime);
        Expression ocfOfEPrime = makeUnion(ePrimeUnionArgs);
        Expression intensionalMultiSet = IntensionalSet.intensionalMultiSet(x, ocfOfEPrime, c);
        Expression intensionalUnion = Expressions.apply(FunctorConstants.INTENSIONAL_UNION, intensionalMultiSet);
        unionArgs.add(intensionalUnion);
    } else // quantified expression,
    if (isArbitraryQuantifier(e)) {
        // then oc<sub>&fnof;</sub>[E] is<br>
        // &bigcup;<sub>x</sub>(oc<sub>&fnof;</sub>[C] &cup; oc<sub>&fnof;</sub>[E&prime;])
        QuantifiedExpression q = (QuantifiedExpression) e;
        IndexExpressionsSet x = q.getIndexExpressions();
        Expression c = getQuantifierCondition(e);
        Expression ePrime = getQuantifiedExpression(q);
        List<Expression> conditionUnionArgs = new ArrayList<>();
        computeUnionArgs(conditionUnionArgs, isF, fType, isFApplication, c);
        List<Expression> ePrimeUnionArgs = new ArrayList<>();
        computeUnionArgs(ePrimeUnionArgs, isF, fType, isFApplication, ePrime);
        List<Expression> combinedUnionArgs = new ArrayList<>();
        combinedUnionArgs.addAll(conditionUnionArgs);
        combinedUnionArgs.addAll(ePrimeUnionArgs);
        Expression combinedUnion = makeUnion(combinedUnionArgs);
        Expression intensionalMultiSet = IntensionalSet.intensionalMultiSet(x, combinedUnion, Expressions.TRUE);
        Expression intensionalUnion = Expressions.apply(FunctorConstants.INTENSIONAL_UNION, intensionalMultiSet);
        unionArgs.add(intensionalUnion);
    } else {
        throw new UnsupportedOperationException("Do not have logic for handling expression of the form: " + e);
    }
}
Also used : Type(com.sri.ai.expresso.api.Type) FunctionType(com.sri.ai.expresso.type.FunctionType) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression) 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) ArrayList(java.util.ArrayList) List(java.util.List) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet)

Aggregations

IndexExpressionsSet (com.sri.ai.expresso.api.IndexExpressionsSet)74 Expression (com.sri.ai.expresso.api.Expression)73 ExtensionalIndexExpressionsSet (com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet)48 IntensionalSet (com.sri.ai.expresso.api.IntensionalSet)24 LambdaExpression (com.sri.ai.expresso.api.LambdaExpression)23 ArrayList (java.util.ArrayList)20 DefaultLambdaExpression (com.sri.ai.expresso.core.DefaultLambdaExpression)17 Type (com.sri.ai.expresso.api.Type)15 QuantifiedExpression (com.sri.ai.expresso.api.QuantifiedExpression)12 Context (com.sri.ai.grinder.api.Context)12 IndexExpressions (com.sri.ai.grinder.library.indexexpression.IndexExpressions)9 Context (com.sri.ai.grinder.sgdpllt.api.Context)9 FunctionType (com.sri.ai.expresso.type.FunctionType)8 Util.mapIntoArrayList (com.sri.ai.util.Util.mapIntoArrayList)8 Rational (com.sri.ai.util.math.Rational)7 List (java.util.List)7 SyntaxTree (com.sri.ai.expresso.api.SyntaxTree)6 QuantifiedExpressionWithABody (com.sri.ai.expresso.api.QuantifiedExpressionWithABody)5 DefaultCountingFormula (com.sri.ai.expresso.core.DefaultCountingFormula)5 DefaultIntensionalMultiSet (com.sri.ai.expresso.core.DefaultIntensionalMultiSet)5