Search in sources :

Example 1 with ExpressionAndSyntacticContext

use of com.sri.ai.expresso.api.ExpressionAndSyntacticContext 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)

Example 2 with ExpressionAndSyntacticContext

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

the class AbstractQuantifiedExpression method makeAddressForIndexType.

private ExpressionAndSyntacticContext makeAddressForIndexType(int indexExpressionIndex, Expression type) {
    SubExpressionAddress address = new IndexExpressionTypeSubExpressionAddress(indexExpressionIndex);
    ExpressionAndSyntacticContext expressionAndSyntacticContext = new DefaultExpressionAndSyntacticContext(type, address);
    return expressionAndSyntacticContext;
}
Also used : ExpressionAndSyntacticContext(com.sri.ai.expresso.api.ExpressionAndSyntacticContext) SubExpressionAddress(com.sri.ai.expresso.api.SubExpressionAddress)

Example 3 with ExpressionAndSyntacticContext

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

the class AbstractQuantifiedExpression method makeAddressForIndexArgument.

private ExpressionAndSyntacticContext makeAddressForIndexArgument(int indexExpressionIndex, Expression index, int argumentIndex) {
    SubExpressionAddress address = new IndexExpressionArgumentSubExpressionAddress(indexExpressionIndex, argumentIndex);
    ExpressionAndSyntacticContext expressionAndSyntacticContext = new DefaultExpressionAndSyntacticContext(index.get(argumentIndex), address);
    return expressionAndSyntacticContext;
}
Also used : ExpressionAndSyntacticContext(com.sri.ai.expresso.api.ExpressionAndSyntacticContext) SubExpressionAddress(com.sri.ai.expresso.api.SubExpressionAddress)

Example 4 with ExpressionAndSyntacticContext

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

the class Expressions method freeVariables.

private static void freeVariables(Expression expression, Set<Expression> freeVariables, Set<Expression> quantifiedVariables, Predicate<Expression> isVariable) {
    if (expression.getSyntacticFormType().equals(Symbol.SYNTACTIC_FORM_TYPE)) {
        if (isVariable.apply(expression)) {
            if (!quantifiedVariables.contains(expression)) {
                freeVariables.add(expression);
            }
        }
    } else {
        Iterator<ExpressionAndSyntacticContext> subExpressionAndSyntacticContextsIterator = expression.getImmediateSubExpressionsAndContextsIterator();
        Set<Expression> newLocalQuantifiedVariables = null;
        while (subExpressionAndSyntacticContextsIterator.hasNext()) {
            ExpressionAndSyntacticContext subExpressionAndSyntacticContext = subExpressionAndSyntacticContextsIterator.next();
            // initialize newLocalQuantifiedVariables with an empty collection
            if (newLocalQuantifiedVariables == null) {
                // For efficiency, only instantiate once
                newLocalQuantifiedVariables = new LinkedHashSet<Expression>();
            } else {
                newLocalQuantifiedVariables.clear();
            }
            for (Expression localVariable : subExpressionAndSyntacticContext.getIndices()) {
                if (quantifiedVariables.add(localVariable)) {
                    newLocalQuantifiedVariables.add(localVariable);
                }
            }
            freeVariables(subExpressionAndSyntacticContext.getExpression(), freeVariables, quantifiedVariables, isVariable);
            // Backtrack to what quantifiedVariables was at the beginning of this call; perhaps it would be more efficient to keep this on a stack?
            quantifiedVariables.removeAll(newLocalQuantifiedVariables);
        }
    }
    return;
}
Also used : 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)

Example 5 with ExpressionAndSyntacticContext

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

the class Expressions method freeVariables.

//
// PRIVATE METHODS
//
private static void freeVariables(Expression expression, Set<Expression> freeVariables, Set<Expression> quantifiedVariables, Registry registry) {
    if (expression.getSyntacticFormType().equals(Symbol.SYNTACTIC_FORM_TYPE)) {
        if (registry.isVariable(expression)) {
            if (!quantifiedVariables.contains(expression)) {
                freeVariables.add(expression);
            }
        }
    } else {
        Iterator<ExpressionAndSyntacticContext> subExpressionAndSyntacticContextsIterator = expression.getImmediateSubExpressionsAndContextsIterator();
        Set<Expression> newLocalQuantifiedVariables = null;
        while (subExpressionAndSyntacticContextsIterator.hasNext()) {
            ExpressionAndSyntacticContext subExpressionAndSyntacticContext = subExpressionAndSyntacticContextsIterator.next();
            // initialize newLocalQuantifiedVariables with an empty collection
            if (newLocalQuantifiedVariables == null) {
                // For efficiency, only instantiate once
                newLocalQuantifiedVariables = new LinkedHashSet<Expression>();
            } else {
                newLocalQuantifiedVariables.clear();
            }
            for (Expression localVariable : subExpressionAndSyntacticContext.getIndices()) {
                if (quantifiedVariables.add(localVariable)) {
                    newLocalQuantifiedVariables.add(localVariable);
                }
            }
            freeVariables(subExpressionAndSyntacticContext.getExpression(), freeVariables, quantifiedVariables, registry);
            // Backtrack to what quantifiedVariables was at the beginning of this call; perhaps it would be more efficient to keep this on a stack?
            quantifiedVariables.removeAll(newLocalQuantifiedVariables);
        }
    }
    return;
}
Also used : 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)

Aggregations

ExpressionAndSyntacticContext (com.sri.ai.expresso.api.ExpressionAndSyntacticContext)8 Expression (com.sri.ai.expresso.api.Expression)6 LambdaExpression (com.sri.ai.expresso.api.LambdaExpression)4 DefaultLambdaExpression (com.sri.ai.expresso.core.DefaultLambdaExpression)4 SubExpressionAddress (com.sri.ai.expresso.api.SubExpressionAddress)2 IndexExpressionsSet (com.sri.ai.expresso.api.IndexExpressionsSet)1 QuantifiedExpression (com.sri.ai.expresso.api.QuantifiedExpression)1 ExtensionalIndexExpressionsSet (com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet)1 AbstractExpression (com.sri.ai.grinder.core.AbstractExpression)1