Search in sources :

Example 11 with IntensionalSet

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

the class IntensionalSetFalseConditionToEmptySetSimplifier method simplify.

public static Expression simplify(Expression expression, Context context) {
    Expression result = expression;
    if (Sets.isIntensionalSet(expression)) {
        IntensionalSet intensionalSet = (IntensionalSet) expression;
        Expression condition = intensionalSet.getCondition();
        if (condition.equals(Expressions.FALSE)) {
            result = Sets.EMPTY_SET;
        }
    }
    return result;
}
Also used : IntensionalSet(com.sri.ai.expresso.api.IntensionalSet) Expression(com.sri.ai.expresso.api.Expression)

Example 12 with IntensionalSet

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

the class Measure method get.

public static Rational get(Expression intensionalSetExpression, Context context) {
    Rational result;
    if (Sets.isIntensionalSet(intensionalSetExpression)) {
        IntensionalSet intensionalSet = (IntensionalSet) intensionalSetExpression;
        IndexExpressionsSet indexExpressionsSet = intensionalSet.getIndexExpressions();
        List<Expression> indices = IndexExpressions.getIndices(indexExpressionsSet);
        if (indices.size() == 1) {
            Expression evaluatedResult;
            Expression intensionalSetIndex = indices.get(0);
            Expression intensionalSetHead = intensionalSet.getHead();
            if (!intensionalSetHead.equals(intensionalSetIndex)) {
                throw new UnsupportedOperationException("Index and Head must be the same to calculate the meaure of an Intensional : " + intensionalSet);
            }
            Expression intensionalSetCondition = intensionalSet.getCondition();
            Context intensionalSetContext = context.extendWith(indexExpressionsSet);
            Type indexType = GrinderUtil.getTypeOfExpression(intensionalSetIndex, intensionalSetContext);
            if (intensionalSetCondition.equals(false)) {
                // short circuit known empty sets up front.
                evaluatedResult = Expressions.ZERO;
            } else if (indexType instanceof RealExpressoType || indexType instanceof RealInterval) {
                // NOTE : For Reals can always assume the condition is of this type.
                SingleVariableLinearRealArithmeticConstraint svConstraint = (SingleVariableLinearRealArithmeticConstraint) intensionalSetCondition;
                MeasureOfSingleVariableLinearRealArithmeticConstraintStepSolver realSolver = new MeasureOfSingleVariableLinearRealArithmeticConstraintStepSolver(svConstraint);
                evaluatedResult = realSolver.solve(intensionalSetContext);
            } else if (indexType instanceof FunctionType) {
                if (!intensionalSetCondition.equals(true)) {
                    throw new UnsupportedOperationException("Measure of intensional set with a function type domain currently do not support conditions: " + intensionalSet);
                }
                // measure(co-domain)^measure(domain)
                FunctionType indexFunctionType = (FunctionType) indexType;
                Expression condomainIntensionalSet = constructComponentIntensionalSet(indexFunctionType.getCodomain(), intensionalSet, ZERO, intensionalSetContext);
                Rational codomainMeasure = get(condomainIntensionalSet, intensionalSetContext);
                Rational domainMeasure = Rational.ONE;
                for (Type argDomainType : indexFunctionType.getArgumentTypes()) {
                    Expression argDomainIntensionalSet = constructComponentIntensionalSet(argDomainType, intensionalSet, ZERO, intensionalSetContext);
                    Rational argMeasure = get(argDomainIntensionalSet, intensionalSetContext);
                    domainMeasure = domainMeasure.multiply(argMeasure);
                }
                evaluatedResult = Expressions.makeSymbol(codomainMeasure.pow(domainMeasure.intValueExact()));
            } else if (indexType instanceof TupleType) {
                if (!intensionalSetCondition.equals(true)) {
                    throw new UnsupportedOperationException("Measure of intensional set with a tuple type domain currently do not support conditions: " + intensionalSet);
                }
                // (element_1, ..., element_n) = measure(element_1) * ... * measure(element_n)
                TupleType indexTupleType = (TupleType) indexType;
                Rational elementMeasuresProduct = Rational.ONE;
                for (Type elementType : indexTupleType.getElementTypes()) {
                    Expression elementDomainIntensionalSet = constructComponentIntensionalSet(elementType, intensionalSet, ZERO, intensionalSetContext);
                    Rational elementMeasure = get(elementDomainIntensionalSet, intensionalSetContext);
                    elementMeasuresProduct = elementMeasuresProduct.multiply(elementMeasure);
                }
                evaluatedResult = Expressions.makeSymbol(elementMeasuresProduct);
            } else {
                Expression countingFormula = new DefaultCountingFormula(indexExpressionsSet, intensionalSet.getCondition());
                evaluatedResult = context.getTheory().evaluate(countingFormula, context);
            }
            if (Expressions.isNumber(evaluatedResult)) {
                result = evaluatedResult.rationalValue();
            } else {
                throw new UnsupportedOperationException("Unable to compute a finite measure for: " + intensionalSet + ", got : " + evaluatedResult);
            }
        } else {
            throw new UnsupportedOperationException("Currently only support the measure of single indexed intensional sets: " + intensionalSet);
        }
    } else {
        throw new IllegalArgumentException("Not an intensional set: " + intensionalSetExpression);
    }
    return result;
}
Also used : Context(com.sri.ai.grinder.api.Context) Rational(com.sri.ai.util.math.Rational) FunctionType(com.sri.ai.expresso.type.FunctionType) DefaultCountingFormula(com.sri.ai.expresso.core.DefaultCountingFormula) RealInterval(com.sri.ai.expresso.type.RealInterval) Type(com.sri.ai.expresso.api.Type) RealExpressoType(com.sri.ai.expresso.type.RealExpressoType) FunctionType(com.sri.ai.expresso.type.FunctionType) TupleType(com.sri.ai.expresso.type.TupleType) IntensionalSet(com.sri.ai.expresso.api.IntensionalSet) Expression(com.sri.ai.expresso.api.Expression) RealExpressoType(com.sri.ai.expresso.type.RealExpressoType) TupleType(com.sri.ai.expresso.type.TupleType) ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet) SingleVariableLinearRealArithmeticConstraint(com.sri.ai.grinder.theory.linearrealarithmetic.SingleVariableLinearRealArithmeticConstraint) MeasureOfSingleVariableLinearRealArithmeticConstraintStepSolver(com.sri.ai.grinder.theory.linearrealarithmetic.MeasureOfSingleVariableLinearRealArithmeticConstraintStepSolver)

Example 13 with IntensionalSet

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

the class Sets method expandApplicationOfAssociativeCommutativeFunctionToIntensionalSetWithMultipleIndexExpressionsFrom.

private static Expression expandApplicationOfAssociativeCommutativeFunctionToIntensionalSetWithMultipleIndexExpressionsFrom(int i, Expression functor, IntensionalSet intensionalSet, ExtensionalIndexExpressionsSet indexExpressions) {
    Expression result;
    int numberOfIndexExpressions = indexExpressions.getList().size();
    if (i == numberOfIndexExpressions - 1) {
        Expression iThIndexExpression = indexExpressions.getList().get(i);
        ExtensionalIndexExpressionsSet indexExpressionsSetWithIthIndexExpressionOnly = new ExtensionalIndexExpressionsSet(iThIndexExpression);
        result = apply(functor, intensionalSet.setIndexExpressions(indexExpressionsSetWithIthIndexExpressionOnly));
    } else {
        Expression iThIndexExpression = indexExpressions.getList().get(i);
        ExtensionalIndexExpressionsSet indexExpressionsSetWithIthIndexExpressionOnly = new ExtensionalIndexExpressionsSet(iThIndexExpression);
        Expression innerHead = expandApplicationOfAssociativeCommutativeFunctionToIntensionalSetWithMultipleIndexExpressionsFrom(i + 1, functor, intensionalSet, indexExpressions);
        IntensionalSet innerSet = intensionalSet.setHeadAndCondition(innerHead, TRUE);
        innerSet = innerSet.setIndexExpressions(indexExpressionsSetWithIthIndexExpressionOnly);
        result = apply(functor, innerSet);
    }
    return result;
}
Also used : ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) IntensionalSet(com.sri.ai.expresso.api.IntensionalSet) Expression(com.sri.ai.expresso.api.Expression)

Example 14 with IntensionalSet

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

the class Sets method expandApplicationOfAssociativeCommutativeFunction.

/**
 * Transforms an application of a associative commutative function to an intensional set with one or more indices
 * to an equivalent expression in which intensional sets have a single index each.
 * An example is <code>sum( {{ (on X in 1..10, Y in 1..10) 3 | X != Y }} )</code>
 * being transformed to <code>sum( {{ (on X in 1..10) sum({{ (on Y in 1..10) 3 | X != Y }}) | true }} )</code>
 * @param functionApplicationOnIntensionalSet
 * @return
 */
public static Expression expandApplicationOfAssociativeCommutativeFunction(Expression functionApplicationOnIntensionalSet) {
    IntensionalSet intensionalSet = (IntensionalSet) functionApplicationOnIntensionalSet.get(0);
    ExtensionalIndexExpressionsSet indexExpressions = (ExtensionalIndexExpressionsSet) intensionalSet.getIndexExpressions();
    myAssert(() -> indexExpressions.getList().size() != 0, () -> "There must be at least one index expression");
    Expression result;
    if (indexExpressions.getList().size() == 1) {
        result = functionApplicationOnIntensionalSet;
    } else {
        Expression functor = functionApplicationOnIntensionalSet.getFunctor();
        result = expandApplicationOfAssociativeCommutativeFunctionToIntensionalSetWithMultipleIndexExpressionsFrom(0, functor, intensionalSet, indexExpressions);
    }
    return result;
}
Also used : ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) IntensionalSet(com.sri.ai.expresso.api.IntensionalSet) Expression(com.sri.ai.expresso.api.Expression)

Example 15 with IntensionalSet

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

the class ElementOfIntensionalUnionSimplifier method simplify.

public static Expression simplify(Expression expression, Context context) {
    Expression result = expression;
    if (Expressions.hasFunctor(expression, FunctorConstants.IN) && expression.numberOfArguments() == 2) {
        Expression t = expression.get(0);
        Expression set = expression.get(1);
        if (Sets.isIntensionalUnion(set)) {
            IntensionalSet intensionalSet = (IntensionalSet) set.get(0);
            Expression tElementOfPhi = Expressions.apply(FunctorConstants.IN, t, intensionalSet.getHead());
            Expression existsBody = And.make(intensionalSet.getCondition(), tElementOfPhi);
            result = ThereExists.make(intensionalSet.getIndexExpressions(), existsBody);
        }
    }
    return result;
}
Also used : IntensionalSet(com.sri.ai.expresso.api.IntensionalSet) Expression(com.sri.ai.expresso.api.Expression)

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