Search in sources :

Example 31 with ExtensionalIndexExpressionsSet

use of com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet in project aic-expresso by aic-sri-international.

the class IndexExpressions method makeExtensionalIndexExpressionsSetFromSymbolsAndTypesStrings.

public static ExtensionalIndexExpressionsSet makeExtensionalIndexExpressionsSetFromSymbolsAndTypesStrings(String... symbolsAndTypes) {
    Expression[] symbolsAndTypesExpressions = GrinderUtil.makeListOfSymbolsAndTypesExpressionsFromSymbolsAndTypesStrings(symbolsAndTypes);
    List<Expression> indexExpressions = GrinderUtil.makeIndexExpressionsFromSymbolsAndTypes(symbolsAndTypesExpressions);
    ExtensionalIndexExpressionsSet result = new ExtensionalIndexExpressionsSet(indexExpressions);
    return result;
}
Also used : ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) Expression(com.sri.ai.expresso.api.Expression) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression)

Example 32 with ExtensionalIndexExpressionsSet

use of com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet in project aic-expresso by aic-sri-international.

the class Measure method constructComponentIntensionalSet.

private static Expression constructComponentIntensionalSet(Type indexType, IntensionalSet intensionalSet, Expression additiveIdentityElement, Context intensionalSetContext) {
    Expression conditionedBody = IfThenElse.make(intensionalSet.getCondition(), intensionalSet.getHead(), additiveIdentityElement);
    Expression componentIndex = Expressions.makeUniqueVariable("C", conditionedBody, intensionalSetContext);
    Expression indexExpression = IndexExpressions.makeIndexExpression(componentIndex, Expressions.parse(indexType.getName()));
    Expression intensionalCondition = Expressions.TRUE;
    // NOTE: handle the REAL cases where an SingleVariableLinearRealArithmeticConstraint is expected.
    if (indexType instanceof RealExpressoType || indexType instanceof RealInterval) {
        SingleVariableLinearRealArithmeticConstraint svlraConstraint = new SingleVariableLinearRealArithmeticConstraint(componentIndex, true, intensionalSetContext.getTheory());
        intensionalCondition = svlraConstraint;
    }
    Expression result = IntensionalSet.make(Sets.isMultiSet(intensionalSet) ? IntensionalSet.MULTI_SET_LABEL : IntensionalSet.UNI_SET_LABEL, new ExtensionalIndexExpressionsSet(Arrays.asList(indexExpression)), componentIndex, intensionalCondition);
    return result;
}
Also used : ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) Expression(com.sri.ai.expresso.api.Expression) RealExpressoType(com.sri.ai.expresso.type.RealExpressoType) SingleVariableLinearRealArithmeticConstraint(com.sri.ai.grinder.theory.linearrealarithmetic.SingleVariableLinearRealArithmeticConstraint) RealInterval(com.sri.ai.expresso.type.RealInterval)

Example 33 with ExtensionalIndexExpressionsSet

use of com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet 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 34 with ExtensionalIndexExpressionsSet

use of com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet 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 35 with ExtensionalIndexExpressionsSet

use of com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet in project aic-expresso by aic-sri-international.

the class InversionSimplifier method applyInversion.

private static Expression applyInversion(Expression summationIndexedByFunction, Expression summationIndexFunctionName, FunctionType summationIndexFunctionType, List<Expression> originalQuantifierOrder, List<Expression> inversionQuantifierOrder, Context context) {
    Expression result;
    int indexOfSummationIndexedByFunction = inversionQuantifierOrder.indexOf(summationIndexedByFunction);
    // NOTE: only products will bubble up before the summation.
    List<Expression> productsBefore = inversionQuantifierOrder.subList(0, indexOfSummationIndexedByFunction);
    Expression lastQuantifierHead = getHead(originalQuantifierOrder.get(originalQuantifierOrder.size() - 1));
    Context innerContext = context;
    for (Expression quantifier : originalQuantifierOrder) {
        innerContext = innerContext.extendWith(getIndexExpressions(quantifier));
    }
    Context lastQuantifierHeadContext = innerContext;
    // TODO - remove temporary hack which collapses the function's argument domains
    // due to all the domain arguments being treated as constants. Instead should be using
    // set expression types on singletons.
    // Determine which domain arguments from the summation function can be collapsed
    List<Expression> productsBeforeIndices = new ArrayList<>();
    for (Expression productBefore : productsBefore) {
        productsBeforeIndices.add(getIndexAndType(productBefore).first);
    }
    Set<Integer> domainArgsToRemove = new HashSet<>();
    Set<Integer> domainArgsHaveVar = new HashSet<>();
    new SubExpressionsDepthFirstIterator(lastQuantifierHead).forEachRemaining(e -> {
        if (e.hasFunctor(summationIndexFunctionName)) {
            for (int i = 0; i < e.numberOfArguments(); i++) {
                if (lastQuantifierHeadContext.getTheory().isVariable(e.get(0), lastQuantifierHeadContext)) {
                    domainArgsHaveVar.add(i);
                }
                if (productsBeforeIndices.contains(e.get(i)) || Util.thereExists(new SubExpressionsDepthFirstIterator(e.get(i)), es -> productsBeforeIndices.contains(es))) {
                    domainArgsToRemove.add(i);
                }
            }
        }
    });
    // Remove arg positions that were not populated by a variable.
    for (int i = 0; i < summationIndexFunctionType.getArity(); i++) {
        if (!domainArgsHaveVar.contains(i)) {
            domainArgsToRemove.add(i);
        }
    }
    List<Expression> argTypes = new ArrayList<>();
    for (int i = 0; i < summationIndexFunctionType.getArity(); i++) {
        if (!domainArgsToRemove.contains(i)) {
            argTypes.add(parse(summationIndexFunctionType.getArgumentTypes().get(i).getName()));
        }
    }
    Expression codomainType = parse(summationIndexFunctionType.getCodomain().getName());
    Expression summationIndexReducedType;
    if (argTypes.size() == 0) {
        summationIndexReducedType = codomainType;
    } else {
        summationIndexReducedType = FunctionType.make(codomainType, argTypes);
    }
    Expression summationIndex = IndexExpressions.makeIndexExpression(summationIndexFunctionName, summationIndexReducedType);
    Expression phi = lastQuantifierHead.replaceAllOccurrences(e -> {
        Expression r = e;
        if (e.hasFunctor(summationIndexFunctionName)) {
            List<Expression> argsToKeep = new ArrayList<>();
            for (int i = 0; i < e.numberOfArguments(); i++) {
                if (!domainArgsToRemove.contains(i)) {
                    argsToKeep.add(e.get(i));
                }
            }
            if (argsToKeep.size() > 0) {
                r = Expressions.apply(summationIndexFunctionName, argsToKeep);
            } else {
                r = summationIndexFunctionName;
            }
        }
        return r;
    }, lastQuantifierHeadContext);
    Expression summationHead = phi;
    for (int i = indexOfSummationIndexedByFunction + 1; i < inversionQuantifierOrder.size(); i++) {
        Expression quantifier = inversionQuantifierOrder.get(i);
        Expression quantifierIntensionalSet = IntensionalSet.intensionalMultiSet(getIndexExpressions(quantifier), summationHead, getCondition(quantifier));
        summationHead = Expressions.apply(quantifier.getFunctor(), quantifierIntensionalSet);
    }
    Expression innerSummation = IntensionalSet.intensionalMultiSet(new ExtensionalIndexExpressionsSet(summationIndex), summationHead, getCondition(summationIndexedByFunction));
    result = Expressions.apply(FunctorConstants.SUM, innerSummation);
    for (int i = productsBefore.size() - 1; i >= 0; i--) {
        Expression product = productsBefore.get(i);
        product = IntensionalSet.intensionalMultiSet(getIndexExpressions(product), result, getCondition(product));
        result = Expressions.apply(FunctorConstants.PRODUCT, product);
    }
    return result;
}
Also used : Context(com.sri.ai.grinder.api.Context) SubExpressionsDepthFirstIterator(com.sri.ai.expresso.helper.SubExpressionsDepthFirstIterator) Expressions(com.sri.ai.expresso.helper.Expressions) Expression(com.sri.ai.expresso.api.Expression) Sets(com.sri.ai.grinder.library.set.Sets) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) GrinderUtil(com.sri.ai.grinder.helper.GrinderUtil) And(com.sri.ai.grinder.library.boole.And) ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) Equality(com.sri.ai.grinder.library.Equality) IndexExpressions(com.sri.ai.grinder.library.indexexpression.IndexExpressions) Expressions.parse(com.sri.ai.expresso.helper.Expressions.parse) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet) Context(com.sri.ai.grinder.api.Context) Pair(com.sri.ai.util.base.Pair) Implication(com.sri.ai.grinder.library.boole.Implication) Type(com.sri.ai.expresso.api.Type) Disequality(com.sri.ai.grinder.library.Disequality) Set(java.util.Set) IntensionalSet(com.sri.ai.expresso.api.IntensionalSet) FunctionType(com.sri.ai.expresso.type.FunctionType) List(java.util.List) ForAll(com.sri.ai.grinder.library.boole.ForAll) Simplifier(com.sri.ai.grinder.rewriter.api.Simplifier) Util(com.sri.ai.util.Util) FunctorConstants(com.sri.ai.grinder.library.FunctorConstants) ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) Expression(com.sri.ai.expresso.api.Expression) ArrayList(java.util.ArrayList) SubExpressionsDepthFirstIterator(com.sri.ai.expresso.helper.SubExpressionsDepthFirstIterator) HashSet(java.util.HashSet)

Aggregations

Expression (com.sri.ai.expresso.api.Expression)53 ExtensionalIndexExpressionsSet (com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet)53 IndexExpressionsSet (com.sri.ai.expresso.api.IndexExpressionsSet)24 ArrayList (java.util.ArrayList)14 LambdaExpression (com.sri.ai.expresso.api.LambdaExpression)11 DefaultLambdaExpression (com.sri.ai.expresso.core.DefaultLambdaExpression)10 IntensionalSet (com.sri.ai.expresso.api.IntensionalSet)9 IndexExpressions (com.sri.ai.grinder.library.indexexpression.IndexExpressions)8 Type (com.sri.ai.expresso.api.Type)7 Context (com.sri.ai.grinder.api.Context)7 Expressions (com.sri.ai.expresso.helper.Expressions)5 Theory (com.sri.ai.grinder.api.Theory)5 Util (com.sri.ai.util.Util)5 QuantifiedExpression (com.sri.ai.expresso.api.QuantifiedExpression)4 Symbol (com.sri.ai.expresso.api.Symbol)4 Pair (com.sri.ai.util.base.Pair)4 Collection (java.util.Collection)4 List (java.util.List)4 Map (java.util.Map)4 Set (java.util.Set)4