Search in sources :

Example 6 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 7 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 8 with QuantifiedExpression

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

the class HOGMModelValidator method getIndexToTypeMap.

private Map<Expression, Expression> getIndexToTypeMap(QuantifiedExpression quantifiedExpression) {
    IndexExpressionsSet indexExpressionSet = quantifiedExpression.getIndexExpressions();
    Map<Expression, Expression> indexToTypeMap = getIndexToTypeMapWithDefaultNull(indexExpressionSet);
    return indexToTypeMap;
}
Also used : Expression(com.sri.ai.expresso.api.Expression) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet)

Example 9 with QuantifiedExpression

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

the class SGDPLLTTester method runModelCountingTestForSingleVariableConstraint.

private static void runModelCountingTestForSingleVariableConstraint(Expression variable, Collection<Expression> literals, Constraint constraint, boolean testAgainstBruteForce, Theory theory, Context context) {
    Expression literalsConjunction = And.make(literals);
    String problemDescription = "model counting of " + literalsConjunction + " for variable " + variable;
    output("Problem: " + problemDescription);
    Simplifier symbolicSolver = (e, p) -> computeModelCountBySolver((SingleVariableConstraint) e, p);
    SingleVariableConstraint singleVariableConstraint = (SingleVariableConstraint) constraint;
    Expression symbolicSolution = symbolicSolver.apply(singleVariableConstraint, context);
    if (Util.thereExists(new SubExpressionsDepthFirstIterator(symbolicSolution), e -> e instanceof QuantifiedExpression || Sets.isIntensionalSet(e))) {
        throw new Error("Symbolic solution is not quantifier-free: " + symbolicSolution);
    }
    output("Symbolic result: " + symbolicSolution);
    if (testAgainstBruteForce) {
        if (singleVariableConstraint.isContradiction()) {
            if (!symbolicSolution.equals(ZERO)) {
                throw new Error("Constraint is contradiction, but symbolic solver does not produce 0, but instead " + symbolicSolution);
            }
        } else {
            Expression testingVariable = singleVariableConstraint.getVariable();
            Set<Expression> allVariables = getVariableReferences(singleVariableConstraint, context);
            Collection<Expression> otherVariables = removeFromSetNonDestructively(allVariables, v -> v.equals(testingVariable));
            BinaryFunction<BruteForceCommonInterpreter, Context, Expression> fromInterpreterAndContextWithAssignmentToOtherVariablesToBruteForceSolution = (interpreter, contextWithAssignmentToOtherVariables) -> bruteForceModelCounterForVariableGivenInterpreterAndAssignmentToOtherVariables(variable, literalsConjunction, interpreter, theory, contextWithAssignmentToOtherVariables);
            testSymbolicVsBruteForceComputationForEachAssignment(theory, problemDescription, otherVariables, symbolicSolution, fromInterpreterAndContextWithAssignmentToOtherVariablesToBruteForceSolution, context);
        }
    } else {
        output("Skipping test againt brute-force.");
    }
}
Also used : BinaryFunction(com.sri.ai.util.base.BinaryFunction) Util.removeFromSetNonDestructively(com.sri.ai.util.Util.removeFromSetNonDestructively) SubExpressionsDepthFirstIterator(com.sri.ai.expresso.helper.SubExpressionsDepthFirstIterator) Expression(com.sri.ai.expresso.api.Expression) Constraint(com.sri.ai.grinder.sgdpllt.api.Constraint) Function(java.util.function.Function) GrinderUtil(com.sri.ai.grinder.helper.GrinderUtil) Util.in(com.sri.ai.util.Util.in) ThereExists(com.sri.ai.grinder.sgdpllt.library.boole.ThereExists) AbstractIterativeMultiIndexQuantifierElimination(com.sri.ai.grinder.sgdpllt.interpreter.AbstractIterativeMultiIndexQuantifierElimination) Expressions.parse(com.sri.ai.expresso.helper.Expressions.parse) Map(java.util.Map) CompleteMultiVariableContext(com.sri.ai.grinder.sgdpllt.core.constraint.CompleteMultiVariableContext) And(com.sri.ai.grinder.sgdpllt.library.boole.And) DefaultMultiVariableConstraint(com.sri.ai.grinder.sgdpllt.core.constraint.DefaultMultiVariableConstraint) ZERO(com.sri.ai.expresso.helper.Expressions.ZERO) LinkedHashSet(java.util.LinkedHashSet) Sets(com.sri.ai.grinder.sgdpllt.library.set.Sets) Type(com.sri.ai.expresso.api.Type) NullaryFunction(com.sri.ai.util.base.NullaryFunction) Util.join(com.sri.ai.util.Util.join) SingleVariableConstraint(com.sri.ai.grinder.sgdpllt.api.SingleVariableConstraint) Simplifier(com.sri.ai.grinder.sgdpllt.rewriter.api.Simplifier) Collection(java.util.Collection) Util.list(com.sri.ai.util.Util.list) Set(java.util.Set) Expressions.getVariableReferences(com.sri.ai.expresso.helper.Expressions.getVariableReferences) Context(com.sri.ai.grinder.sgdpllt.api.Context) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression) Theory(com.sri.ai.grinder.sgdpllt.api.Theory) Beta(com.google.common.annotations.Beta) IndexExpressions(com.sri.ai.grinder.sgdpllt.library.indexexpression.IndexExpressions) Expressions.makeSymbol(com.sri.ai.expresso.helper.Expressions.makeSymbol) Util(com.sri.ai.util.Util) AssociativeCommutativeGroup(com.sri.ai.grinder.sgdpllt.group.AssociativeCommutativeGroup) Util.pickKElementsWithoutReplacement(com.sri.ai.util.Util.pickKElementsWithoutReplacement) TRUE(com.sri.ai.expresso.helper.Expressions.TRUE) BruteForceCommonInterpreter(com.sri.ai.grinder.sgdpllt.interpreter.BruteForceCommonInterpreter) AssignmentsIterator(com.sri.ai.grinder.helper.AssignmentsIterator) CompleteMultiVariableContext(com.sri.ai.grinder.sgdpllt.core.constraint.CompleteMultiVariableContext) Context(com.sri.ai.grinder.sgdpllt.api.Context) BruteForceCommonInterpreter(com.sri.ai.grinder.sgdpllt.interpreter.BruteForceCommonInterpreter) SingleVariableConstraint(com.sri.ai.grinder.sgdpllt.api.SingleVariableConstraint) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression) Expression(com.sri.ai.expresso.api.Expression) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression) Simplifier(com.sri.ai.grinder.sgdpllt.rewriter.api.Simplifier) SubExpressionsDepthFirstIterator(com.sri.ai.expresso.helper.SubExpressionsDepthFirstIterator)

Example 10 with QuantifiedExpression

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

the class SetOfArgumentTuplesForFunctionOccurringInExpression method computeUnionArgs.

private static void computeUnionArgs(List<Expression> unionArgs, Predicate<Expression> isF, FunctionType fType, Predicate<Expression> isFApplication, Expression e) {
    // if E does not contain &fnof;, oc<sub>&fnof;</sub>[E] is &empty;
    if (!containsF(e, isF)) {
        unionArgs.add(Sets.EMPTY_SET);
    } else // if E is &fnof;(t) for t a tuple, oc<sub>&fnof;</sub>[E] is {t}
    if (isFApplication.apply(e)) {
        Expression tupleT = Expressions.makeTuple(e.getArguments());
        Expression setT = ExtensionalSets.makeUniSet(tupleT);
        unionArgs.add(setT);
    } else // if E is &fnof;, oc<sub>&fnof;</sub>[E] is &Alpha;
    if (isF.apply(e)) {
        List<Expression> domainTypes = new ArrayList<>();
        for (Type domainType : fType.getArgumentTypes()) {
            domainTypes.add(Expressions.parse(domainType.getName()));
        }
        Expression tupleT = Expressions.makeTuple(domainTypes);
        Expression setT = ExtensionalSets.makeUniSet(tupleT);
        unionArgs.add(setT);
    } else // if E is g(E&prime;) for g a function symbol distinct from &fnof; and t a k-tuple of expressions
    if (Expressions.isFunctionApplicationWithArguments(e)) {
        // if g(E&prime;) is if C the E<sub>1</sub> else E<sub>2</sub> and C does not contain &fnof;
        if (IfThenElse.isIfThenElse(e) && !containsF(IfThenElse.condition(e), isF)) {
            // then oc<sub>&fnof;</sub>[E] is<br>
            // if C then oc<sub>&fnof;</sub>[E<sub>1</sub>] else oc<sub>&fnof;</sub>[E<sub>2</sub>]				
            Expression e1 = IfThenElse.thenBranch(e);
            List<Expression> thenUnionArgs = new ArrayList<>();
            computeUnionArgs(thenUnionArgs, isF, fType, isFApplication, e1);
            Expression thenBranch = makeUnion(thenUnionArgs);
            Expression e2 = IfThenElse.elseBranch(e);
            List<Expression> elseUnionArgs = new ArrayList<>();
            computeUnionArgs(elseUnionArgs, isF, fType, isFApplication, e2);
            Expression elseBranch = makeUnion(elseUnionArgs);
            Expression ifThenElse = IfThenElse.make(IfThenElse.condition(e), thenBranch, elseBranch);
            unionArgs.add(ifThenElse);
        } else {
            // oc<sub>&fnof;</sub>[t<sub>1</sub>] &cup; &hellip;	&cup; oc<sub>&fnof;</sub>[t<sub>k</sub>]
            for (Expression t : e.getArguments()) {
                computeUnionArgs(unionArgs, isF, fType, isFApplication, t);
            }
        }
    } else // quantified expression, 
    if (isArbitraryQuantifier(e) && !containsF(getQuantifierCondition(e), isF)) {
        // then oc<sub>&fnof;</sub>[E] is<br> 
        // &bigcup;<sub>x:C</sub>oc<sub>&fnof;</sub>[E&prime;]
        QuantifiedExpression q = (QuantifiedExpression) e;
        IndexExpressionsSet x = q.getIndexExpressions();
        Expression c = getQuantifierCondition(e);
        Expression ePrime = getQuantifiedExpression(q);
        List<Expression> ePrimeUnionArgs = new ArrayList<>();
        computeUnionArgs(ePrimeUnionArgs, isF, fType, isFApplication, ePrime);
        Expression ocfOfEPrime = makeUnion(ePrimeUnionArgs);
        Expression intensionalMultiSet = IntensionalSet.intensionalMultiSet(x, ocfOfEPrime, c);
        Expression intensionalUnion = Expressions.apply(FunctorConstants.INTENSIONAL_UNION, intensionalMultiSet);
        unionArgs.add(intensionalUnion);
    } else // quantified expression,
    if (isArbitraryQuantifier(e)) {
        // then oc<sub>&fnof;</sub>[E] is<br> 
        // &bigcup;<sub>x</sub>(oc<sub>&fnof;</sub>[C] &cup; oc<sub>&fnof;</sub>[E&prime;])
        QuantifiedExpression q = (QuantifiedExpression) e;
        IndexExpressionsSet x = q.getIndexExpressions();
        Expression c = getQuantifierCondition(e);
        Expression ePrime = getQuantifiedExpression(q);
        List<Expression> conditionUnionArgs = new ArrayList<>();
        computeUnionArgs(conditionUnionArgs, isF, fType, isFApplication, c);
        List<Expression> ePrimeUnionArgs = new ArrayList<>();
        computeUnionArgs(ePrimeUnionArgs, isF, fType, isFApplication, ePrime);
        List<Expression> combinedUnionArgs = new ArrayList<>();
        combinedUnionArgs.addAll(conditionUnionArgs);
        combinedUnionArgs.addAll(ePrimeUnionArgs);
        Expression combinedUnion = makeUnion(combinedUnionArgs);
        Expression intensionalMultiSet = IntensionalSet.intensionalMultiSet(x, combinedUnion, Expressions.TRUE);
        Expression intensionalUnion = Expressions.apply(FunctorConstants.INTENSIONAL_UNION, intensionalMultiSet);
        unionArgs.add(intensionalUnion);
    } else {
        throw new UnsupportedOperationException("Do not have logic for handling expression of the form: " + e);
    }
}
Also used : Type(com.sri.ai.expresso.api.Type) FunctionType(com.sri.ai.expresso.type.FunctionType) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression) LambdaExpression(com.sri.ai.expresso.api.LambdaExpression) Expression(com.sri.ai.expresso.api.Expression) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet)

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