Search in sources :

Example 11 with QuantifiedExpression

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

the class Theory method isVariable.

/**
	 * Indicates whether an expression is considered a variable in this theory,
	 * meaning that it is not a constants of any of the registered types in context
	 * (per {@link Context#getTypes()} and {@link Context#isUniquelyNamedConstant(Expression)}),
	 * it is not interpreted in propositional logic
	 * (per {@link FormulaUtil#isInterpretedInPropositionalLogicIncludingConditionals(Expression)}),
	 * and is not interpreted in this theory besides boolean connectives
	 * (per {@link #isInterpretedInThisTheoryBesidesBooleanConnectives(Expression, Context)}.
	 * @param expression
	 * @param context
	 * @return
	 */
default default boolean isVariable(Expression expression, Context context) {
    Expression typeExpression;
    Type type;
    boolean result = !context.isUniquelyNamedConstant(expression) && !(expression instanceof QuantifiedExpression) && !isInterpretedInPropositionalLogicIncludingConditionals(expression) && !isInterpretedInThisTheoryBesidesBooleanConnectives(expression) && (typeExpression = GrinderUtil.getTypeExpression(expression, context)) != null && (type = context.getType(typeExpression)) != null && isSuitableFor(expression, type) && !thereExists(context.getTypes(), t -> t.contains(expression));
    return result;
}
Also used : Type(com.sri.ai.expresso.api.Type) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression) Expression(com.sri.ai.expresso.api.Expression) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression)

Example 12 with QuantifiedExpression

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

the class TupleQuantifierSimplifier method rewriteThereExists.

private static Expression rewriteThereExists(Expression quantifiedExpression, Map<Expression, Expression> indexToTypeMap, Map<Expression, Expression> indexToTupleOfVars, Context context) {
    if (indexToTypeMap.size() > 1) {
        throw new IllegalStateException("We have an Existential Quantifier with > 1 index : " + quantifiedExpression);
    }
    Pair<IndexExpressionsSet, Expression> updatePair = update(ThereExists.getBody(quantifiedExpression), indexToTypeMap, indexToTupleOfVars, context);
    Expression result = ThereExists.make(updatePair.first, updatePair.second);
    return result;
}
Also used : DefaultLambdaExpression(com.sri.ai.expresso.core.DefaultLambdaExpression) Expression(com.sri.ai.expresso.api.Expression) LambdaExpression(com.sri.ai.expresso.api.LambdaExpression) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression) ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet)

Example 13 with QuantifiedExpression

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

the class TupleQuantifierSimplifier method rewriteForAll.

private static Expression rewriteForAll(Expression quantifiedExpression, Map<Expression, Expression> indexToTypeMap, Map<Expression, Expression> indexToTupleOfVars, Context context) {
    if (indexToTypeMap.size() > 1) {
        throw new IllegalStateException("We have a Universal Quantifier with > 1 index : " + quantifiedExpression);
    }
    Pair<IndexExpressionsSet, Expression> updatePair = update(ForAll.getBody(quantifiedExpression), indexToTypeMap, indexToTupleOfVars, context);
    Expression result = ForAll.make(updatePair.first, updatePair.second);
    return result;
}
Also used : DefaultLambdaExpression(com.sri.ai.expresso.core.DefaultLambdaExpression) Expression(com.sri.ai.expresso.api.Expression) LambdaExpression(com.sri.ai.expresso.api.LambdaExpression) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression) ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet)

Example 14 with QuantifiedExpression

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

the class TupleQuantifierSimplifier method createTuplesOfVarsForTupleTypes.

private static Map<Expression, Expression> createTuplesOfVarsForTupleTypes(QuantifiedExpression quantifiedExpression, List<Map.Entry<Expression, Expression>> indexesOfTupleType) {
    Map<Expression, Expression> result = new HashMap<>();
    Set<Expression> allSubExpressions = Util.addAllToSet(new SubExpressionsDepthFirstIterator(quantifiedExpression));
    for (Map.Entry<Expression, Expression> entry : indexesOfTupleType) {
        List<Expression> tupleVars = new ArrayList<>();
        for (int i = 1; i <= entry.getValue().numberOfArguments(); i++) {
            Expression proposedVar = Expressions.makeSymbol(entry.getKey().toString() + "_" + i);
            Expression actualVar = Expressions.primedUntilUnique(proposedVar, expr -> !allSubExpressions.contains(expr));
            tupleVars.add(actualVar);
        }
        result.put(entry.getKey(), Expressions.makeTuple(tupleVars));
    }
    return result;
}
Also used : DefaultLambdaExpression(com.sri.ai.expresso.core.DefaultLambdaExpression) Expression(com.sri.ai.expresso.api.Expression) LambdaExpression(com.sri.ai.expresso.api.LambdaExpression) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) Map(java.util.Map) SubExpressionsDepthFirstIterator(com.sri.ai.expresso.helper.SubExpressionsDepthFirstIterator)

Example 15 with QuantifiedExpression

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

the class TupleQuantifierSimplifier method simplify.

public static Expression simplify(Expression expression, Context context) {
    Expression result = expression;
    if (expression instanceof QuantifiedExpression) {
        QuantifiedExpression quantifiedExpression = (QuantifiedExpression) expression;
        Map<Expression, Expression> indexToTypeMap = IndexExpressions.getIndexToTypeMapWithDefaultNull(quantifiedExpression);
        List<Map.Entry<Expression, Expression>> indexesOfTupleType = indexToTypeMap.entrySet().stream().filter(entry -> entry.getValue() != null && TupleType.isTupleType(entry.getValue())).collect(Collectors.toList());
        if (indexesOfTupleType.size() > 0) {
            Map<Expression, Expression> indexToTupleOfVars = createTuplesOfVarsForTupleTypes(quantifiedExpression, indexesOfTupleType);
            result = rewriteQuantifiedExpression(quantifiedExpression, indexToTypeMap, indexToTupleOfVars, context);
        }
    }
    return result;
}
Also used : CountingFormula(com.sri.ai.expresso.api.CountingFormula) SubExpressionsDepthFirstIterator(com.sri.ai.expresso.helper.SubExpressionsDepthFirstIterator) DefaultLambdaExpression(com.sri.ai.expresso.core.DefaultLambdaExpression) Expressions(com.sri.ai.expresso.helper.Expressions) HashMap(java.util.HashMap) Expression(com.sri.ai.expresso.api.Expression) ArrayList(java.util.ArrayList) ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) TupleType(com.sri.ai.expresso.type.TupleType) IndexExpressions(com.sri.ai.grinder.library.indexexpression.IndexExpressions) Map(java.util.Map) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet) DefaultCountingFormula(com.sri.ai.expresso.core.DefaultCountingFormula) ThereExists(com.sri.ai.grinder.library.boole.ThereExists) Context(com.sri.ai.grinder.api.Context) Pair(com.sri.ai.util.base.Pair) LambdaExpression(com.sri.ai.expresso.api.LambdaExpression) Set(java.util.Set) IntensionalSet(com.sri.ai.expresso.api.IntensionalSet) Collectors(java.util.stream.Collectors) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression) 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) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression) DefaultLambdaExpression(com.sri.ai.expresso.core.DefaultLambdaExpression) Expression(com.sri.ai.expresso.api.Expression) LambdaExpression(com.sri.ai.expresso.api.LambdaExpression) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression)

Aggregations

Expression (com.sri.ai.expresso.api.Expression)20 QuantifiedExpression (com.sri.ai.expresso.api.QuantifiedExpression)20 IndexExpressionsSet (com.sri.ai.expresso.api.IndexExpressionsSet)10 LambdaExpression (com.sri.ai.expresso.api.LambdaExpression)10 Type (com.sri.ai.expresso.api.Type)8 DefaultLambdaExpression (com.sri.ai.expresso.core.DefaultLambdaExpression)8 SubExpressionsDepthFirstIterator (com.sri.ai.expresso.helper.SubExpressionsDepthFirstIterator)8 Map (java.util.Map)8 ExtensionalIndexExpressionsSet (com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet)6 Util (com.sri.ai.util.Util)6 Set (java.util.Set)6 ArrayList (java.util.ArrayList)5 Beta (com.google.common.annotations.Beta)4 TRUE (com.sri.ai.expresso.helper.Expressions.TRUE)4 ZERO (com.sri.ai.expresso.helper.Expressions.ZERO)4 Expressions.makeSymbol (com.sri.ai.expresso.helper.Expressions.makeSymbol)4 Expressions.parse (com.sri.ai.expresso.helper.Expressions.parse)4 GrinderUtil (com.sri.ai.grinder.helper.GrinderUtil)4 List (java.util.List)4 Context (com.sri.ai.grinder.api.Context)3