Search in sources :

Example 21 with IndexExpressionsSet

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

the class AbstractQuantifiedExpressionWithABody method replaceSymbol.

@Override
public Expression replaceSymbol(Expression symbol, Expression newSymbol, Registry registry) {
    AbstractQuantifiedExpression result = this;
    Function<Expression, Expression> renameSymbol = e -> IndexExpressions.renameSymbol(e, symbol, newSymbol, registry);
    List<Expression> indexExpressionsList = ((ExtensionalIndexExpressionsSet) getIndexExpressions()).getList();
    IndexExpressionsSet newIndexExpressions = new ExtensionalIndexExpressionsSet(replaceElementsNonDestructively(indexExpressionsList, renameSymbol));
    Expression newBody = getBody().replaceSymbol(symbol, newSymbol, registry);
    result = replaceIfNeeded(newIndexExpressions, newBody);
    return result;
}
Also used : Function(com.google.common.base.Function) Iterator(java.util.Iterator) Util.join(com.sri.ai.util.Util.join) SubExpressionAddress(com.sri.ai.expresso.api.SubExpressionAddress) Expressions(com.sri.ai.expresso.helper.Expressions) ExpressionAndSyntacticContext(com.sri.ai.expresso.api.ExpressionAndSyntacticContext) Util.castOrThrowError(com.sri.ai.util.Util.castOrThrowError) Util.mapIntoArrayList(com.sri.ai.util.Util.mapIntoArrayList) Expression(com.sri.ai.expresso.api.Expression) Util.mapIntoList(com.sri.ai.util.Util.mapIntoList) SyntaxTree(com.sri.ai.expresso.api.SyntaxTree) SyntaxTrees.makeCompoundSyntaxTree(com.sri.ai.expresso.helper.SyntaxTrees.makeCompoundSyntaxTree) Beta(com.google.common.annotations.Beta) QuantifiedExpressionWithABody(com.sri.ai.expresso.api.QuantifiedExpressionWithABody) Util.replaceElementsNonDestructively(com.sri.ai.util.Util.replaceElementsNonDestructively) List(java.util.List) IndexExpressions(com.sri.ai.grinder.library.indexexpression.IndexExpressions) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet) Registry(com.sri.ai.grinder.api.Registry) SyntaxTrees(com.sri.ai.expresso.helper.SyntaxTrees) LinkedList(java.util.LinkedList) Expression(com.sri.ai.expresso.api.Expression) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet)

Example 22 with IndexExpressionsSet

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

the class DefaultIntensionalMultiSet method intensionalMultiSet.

/**
 * Creates an intensional multi-set on a single index, obtaining its type from the context
 * (so it must be registered there).
 * @param index
 * @param body
 * @param condition
 * @param context
 * @return an intensional multi-set expression.
 */
public static Expression intensionalMultiSet(Expression index, Expression body, Expression condition, Context context) {
    Expression type = context.getMandatoryTypeExpressionOfRegisteredSymbol(index);
    Expression indexExpression = apply(FunctorConstants.IN, index, type);
    IndexExpressionsSet indexExpressions = new ExtensionalIndexExpressionsSet(indexExpression);
    Expression set = new DefaultIntensionalMultiSet(indexExpressions, body, condition);
    return set;
}
Also used : Expression(com.sri.ai.expresso.api.Expression) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet)

Example 23 with IndexExpressionsSet

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

the class Expressions method makeDefaultIntensionalMultiSetFromLabelAndSubTrees.

private static Expression makeDefaultIntensionalMultiSetFromLabelAndSubTrees(Object label, Object[] subTreeObjects) {
    if (subTreeObjects.length == 1 && subTreeObjects[0] instanceof Collection) {
        subTreeObjects = ((Collection) subTreeObjects[0]).toArray();
    }
    ArrayList<Expression> subTreeExpressions = Util.mapIntoArrayList(subTreeObjects, Expressions::makeFromObject);
    if (subTreeExpressions.size() == 1) {
        subTreeExpressions = new ArrayList<Expression>(Expressions.ensureListFromKleeneList(subTreeExpressions.get(0)));
    }
    Expression scopingExpression = subTreeExpressions.get(0);
    IndexExpressionsSet indexExpressions = new ExtensionalIndexExpressionsSet((scopingExpression == null || scopingExpression.numberOfArguments() == 0) ? Util.list() : new ArrayList<Expression>(Expressions.ensureListFromKleeneList(scopingExpression.get(0))));
    Expression conditioningSyntaxTree = subTreeExpressions.get(2);
    Expression condition = conditioningSyntaxTree == null ? Expressions.TRUE : conditioningSyntaxTree.get(0);
    Expression result = new DefaultIntensionalMultiSet(indexExpressions, subTreeExpressions.get(1), condition);
    return result;
}
Also used : ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) DefaultLambdaExpression(com.sri.ai.expresso.core.DefaultLambdaExpression) Expression(com.sri.ai.expresso.api.Expression) LambdaExpression(com.sri.ai.expresso.api.LambdaExpression) ArrayList(java.util.ArrayList) Collection(java.util.Collection) IndexExpressions(com.sri.ai.grinder.library.indexexpression.IndexExpressions) ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet) DefaultIntensionalMultiSet(com.sri.ai.expresso.core.DefaultIntensionalMultiSet)

Example 24 with IndexExpressionsSet

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

the class Expressions method makeDefaultLambdaExpressionFromLabelAndSubTrees.

private static Expression makeDefaultLambdaExpressionFromLabelAndSubTrees(Object label, Object[] subTreeObjects) {
    ArrayList<Expression> subTreeExpressions = Util.mapIntoArrayList(subTreeObjects, Expressions::makeFromObject);
    Expression indexExpressionsKleeneList = subTreeExpressions.get(0);
    IndexExpressionsSet indexExpressions = new ExtensionalIndexExpressionsSet(ensureListFromKleeneList(indexExpressionsKleeneList));
    Expression body = subTreeExpressions.get(1);
    Expression result = new DefaultLambdaExpression(indexExpressions, body);
    return result;
}
Also used : ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) DefaultLambdaExpression(com.sri.ai.expresso.core.DefaultLambdaExpression) Expression(com.sri.ai.expresso.api.Expression) LambdaExpression(com.sri.ai.expresso.api.LambdaExpression) DefaultLambdaExpression(com.sri.ai.expresso.core.DefaultLambdaExpression) IndexExpressions(com.sri.ai.grinder.library.indexexpression.IndexExpressions) ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet)

Example 25 with IndexExpressionsSet

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

the class Expressions method freeSymbols.

private static void freeSymbols(Expression expression, Set<Expression> freeSymbols, Stack<Expression> quantifiedSymbols, Registry registry) {
    if (expression.getSyntacticFormType().equals(Symbol.SYNTACTIC_FORM_TYPE)) {
        if (!quantifiedSymbols.contains(expression)) {
            freeSymbols.add(expression);
        }
    } else {
        Iterator<ExpressionAndSyntacticContext> subExpressionAndSyntacticContextsIterator = expression.getImmediateSubExpressionsAndContextsIterator();
        while (subExpressionAndSyntacticContextsIterator.hasNext()) {
            ExpressionAndSyntacticContext subExpressionAndSyntacticContext = subExpressionAndSyntacticContextsIterator.next();
            IndexExpressionsSet indexExpressions = subExpressionAndSyntacticContext.getIndexExpressions();
            List<Expression> indexExpressionsList = ((ExtensionalIndexExpressionsSet) indexExpressions).getList();
            List<Expression> newQuantifiedSymbols = Util.mapIntoList(indexExpressionsList, IndexExpressions.GET_INDEX);
            int numberOfPushed = Util.pushAll(quantifiedSymbols, newQuantifiedSymbols);
            freeSymbols(subExpressionAndSyntacticContext.getExpression(), freeSymbols, quantifiedSymbols, registry);
            Util.popAll(quantifiedSymbols, numberOfPushed);
        }
    }
    return;
}
Also used : ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) DefaultLambdaExpression(com.sri.ai.expresso.core.DefaultLambdaExpression) Expression(com.sri.ai.expresso.api.Expression) LambdaExpression(com.sri.ai.expresso.api.LambdaExpression) ExpressionAndSyntacticContext(com.sri.ai.expresso.api.ExpressionAndSyntacticContext) ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet)

Aggregations

IndexExpressionsSet (com.sri.ai.expresso.api.IndexExpressionsSet)79 Expression (com.sri.ai.expresso.api.Expression)78 ExtensionalIndexExpressionsSet (com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet)49 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)14 Context (com.sri.ai.grinder.api.Context)13 IndexExpressions (com.sri.ai.grinder.library.indexexpression.IndexExpressions)9 Context (com.sri.ai.grinder.sgdpllt.api.Context)9 List (java.util.List)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 SyntaxTree (com.sri.ai.expresso.api.SyntaxTree)6 DefaultIntensionalMultiSet (com.sri.ai.expresso.core.DefaultIntensionalMultiSet)6 QuantifiedExpressionWithABody (com.sri.ai.expresso.api.QuantifiedExpressionWithABody)5 DefaultCountingFormula (com.sri.ai.expresso.core.DefaultCountingFormula)5