Search in sources :

Example 6 with ExpressionLiteralSplitterStepSolver

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

the class TupleTheory method getSingleQuantifierEliminatorStepSolver.

@Override
public ExpressionLiteralSplitterStepSolver getSingleQuantifierEliminatorStepSolver(SingleQuantifierEliminationProblem problem, Context context) {
    // The tuple-specific version will do the following:
    // - create a E expression equivalent to the quantifier elimination of the constraint given here.
    // - you can use AssociativeCommutativeGroup.makeProblemExpression(Expression index, Expression indexType, Expression constraint, Expression body)
    // to create E
    Expression variable = problem.getIndex();
    Expression typeExpression = GrinderUtil.getTypeExpressionOfExpression(variable, context);
    Type type = context.getTypeFromTypeExpression(typeExpression);
    if (!isSuitableFor(type)) {
        throw new Error("Theory " + this + " asked to eliminate quantifier indexed by " + variable + " in " + typeExpression + ", but this theory is not suitable for this type.");
    }
    Expression expression = problem.toExpression();
    // - use TupleQuantifierSimplifier to transform it to another expression E' without quantification on tuples
    Expression expressionWithoutQuantificationOnTuples = tupleQuantifierSimplifier.apply(expression, context);
    // - return context.getTheory().getRewriter().makeStepSolver(E')
    ExpressionLiteralSplitterStepSolver result = context.getTheory().makeEvaluatorStepSolver(expressionWithoutQuantificationOnTuples);
    return result;
}
Also used : Type(com.sri.ai.expresso.api.Type) TupleType(com.sri.ai.expresso.type.TupleType) Expression(com.sri.ai.expresso.api.Expression) ExpressionLiteralSplitterStepSolver(com.sri.ai.grinder.api.ExpressionLiteralSplitterStepSolver)

Example 7 with ExpressionLiteralSplitterStepSolver

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

the class MultiVariableContextWithCheckedProperty method check.

private MultiVariableContextWithCheckedProperty check(Context context) {
    MultiVariableContextWithCheckedProperty result;
    if (checked) {
        result = this;
    } else {
        ExpressionLiteralSplitterStepSolver problem = contextDependentProblemStepSolverMaker.apply(head, context);
        Expression solution = problem.solve(tail);
        if (solution == null) {
            // tail is found to be inconsistent with given context
            result = makeContradiction();
        } else if (solution.equals(FALSE)) {
            // the head constraint does not exhibit the property in all contexts, so the total constraint does not either.
            result = makeContradiction();
        } else {
            this.checked = true;
            result = this;
        }
    }
    return result;
}
Also used : Expression(com.sri.ai.expresso.api.Expression) ExpressionLiteralSplitterStepSolver(com.sri.ai.grinder.api.ExpressionLiteralSplitterStepSolver)

Example 8 with ExpressionLiteralSplitterStepSolver

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

the class AbstractSingleQuantifierEliminationStepSolver method step.

@Override
public Step step(Context context) {
    Step result;
    Context contextForBody = getContextForBody(context);
    if (contextForBody.isContradiction()) {
        result = new Solution(getGroup().additiveIdentityElement());
    } else {
        ExpressionLiteralSplitterStepSolver bodyStepSolver = getInitialBodyStepSolver(context.getTheory());
        ExpressionLiteralSplitterStepSolver.Step bodyStep = bodyStepSolver.step(contextForBody);
        // Check (**) in this file to see where this happens
        if (!bodyStep.itDepends()) {
            ExpressionLiteralSplitterStepSolver evaluatorStepSolver = context.getTheory().makeEvaluatorStepSolver(bodyStep.getValue());
            bodyStep = evaluatorStepSolver.step(context);
        }
        if (bodyStep.itDepends()) {
            // "intercept" literals containing the index and split the quantifier based on it
            if (isSubExpressionOf(getIndex(), bodyStep.getSplitterLiteral())) {
                Expression literalOnIndex = bodyStep.getSplitterLiteral();
                result = resultIfLiteralContainsIndex(literalOnIndex, bodyStep, contextForBody, context);
            } else {
                // not on index, just pass the expression on which we depend on, but with appropriate sub-step solvers (this, for now)
                AbstractSingleQuantifierEliminationStepSolver ifTrue = clone();
                AbstractSingleQuantifierEliminationStepSolver ifFalse = clone();
                ifTrue.initialBodyEvaluationStepSolver = bodyStep.getStepSolverForWhenSplitterIsTrue();
                ifFalse.initialBodyEvaluationStepSolver = bodyStep.getStepSolverForWhenSplitterIsFalse();
                ifTrue.initialContextForBody = bodyStep.getContextSplittingWhenSplitterIsLiteral().getContextAndLiteral();
                ifFalse.initialContextForBody = bodyStep.getContextSplittingWhenSplitterIsLiteral().getContextAndLiteralNegation();
                // to compute the result's constraint splitting,
                // we cannot directly re-use bodyStep.getConstraintSplitting() because it was not obtained from
                // the context it is returning to,
                // but from the context conjoined with the index constraint.
                // In order to provide two contexts to work with the sequel step solvers,
                // we calculate the splittings here.
                // TODO: In the future, we expect it possible to efficiently extract the contextForBody component relative
                // to the original context only, excluding the index.
                ContextSplitting split = new ContextSplitting(bodyStep.getSplitterLiteral(), context);
                result = new ItDependsOn(bodyStep.getSplitterLiteral(), split, ifTrue, ifFalse);
            }
        } else {
            // body is already literal free
            Expression literalFreeBody = bodyStep.getValue();
            result = eliminateQuantifierForLiteralFreeBody(literalFreeBody, context);
            boolean solutionToQuantifiedLiteralFreeBodyIsNotConditionalItself = !result.itDepends();
            if (solutionToQuantifiedLiteralFreeBodyIsNotConditionalItself) {
                IntegrationRecording.registerGroupIntegration(problem, literalFreeBody, result, context);
            }
        }
    }
    if (context.getGlobalObject(BRUTE_FORCE_CHECKING_OF_NON_CONDITIONAL_PROBLEMS) != null) {
        if (!result.itDepends()) {
            Expression indexType = context.getTypeExpressionOfRegisteredSymbol(getIndex());
            SingleQuantifierEliminationProblem problem = new DefaultSingleQuantifierEliminationProblem(getGroup(), getIndex(), indexType, getIndexConstraint(), getBody());
            Expression problemExpression = problem.toExpression();
            Set<Expression> freeVariables = Expressions.freeVariables(problemExpression, context);
            AssignmentMapsIterator assignments = new AssignmentMapsIterator(freeVariables, context);
            for (Map<Expression, Expression> assignment : in(assignments)) {
                BruteForceCommonInterpreter bruteForceCommonInterpreter = new BruteForceCommonInterpreter();
                Context extendedContext = Assignment.extendAssignments(assignment, context);
                // Only go on if the assignment satisfies the context:
                if (bruteForceCommonInterpreter.apply(context, extendedContext).equals(Expressions.TRUE)) {
                    Expression bruteForceResult = bruteForceCommonInterpreter.apply(problemExpression, extendedContext);
                    Expression resultGivenAssignment = bruteForceCommonInterpreter.apply(result.getValue(), extendedContext);
                    Expression evaluatedProblem = bruteForceCommonInterpreter.apply(problemExpression, extendedContext);
                    if (!bruteForceResult.equals(resultGivenAssignment)) {
                        String message = "Disagreement on " + problemExpression + "\nunder " + assignment + ".\n" + "Context: " + context + ".\n" + "Evaluated problem: " + evaluatedProblem + ".\n" + "Brute force says " + bruteForceResult + ", symbolic says " + resultGivenAssignment;
                        println(message);
                        throw new Error(message);
                    } else {
                        String message = "Agreement on " + problemExpression + "\nunder " + assignment + ".\n" + "Context: " + context + ".\n" + "Evaluated problem: " + evaluatedProblem + ".\n" + "Brute force says " + bruteForceResult + ", symbolic says " + resultGivenAssignment;
                        println(message);
                    }
                }
            }
        }
    }
    return result;
}
Also used : Context(com.sri.ai.grinder.api.Context) AssignmentMapsIterator(com.sri.ai.grinder.helper.AssignmentMapsIterator) BruteForceCommonInterpreter(com.sri.ai.grinder.interpreter.BruteForceCommonInterpreter) Expression(com.sri.ai.expresso.api.Expression) ExpressionLiteralSplitterStepSolver(com.sri.ai.grinder.api.ExpressionLiteralSplitterStepSolver) SingleQuantifierEliminationProblem(com.sri.ai.grinder.api.SingleQuantifierEliminationProblem) ContextSplitting(com.sri.ai.grinder.core.constraint.ContextSplitting)

Example 9 with ExpressionLiteralSplitterStepSolver

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

the class ContextDependentExpressionProblemSolver method solve.

/**
 * Returns the solution for a problem using a step solver.
 * @param stepSolver
 * @param context
 * @return
 */
public Expression solve(ExpressionLiteralSplitterStepSolver stepSolver, Context context) {
    if (interrupted) {
        throw new Error("Solver interrupted.");
    }
    Expression result;
    ExpressionLiteralSplitterStepSolver.Step step = stepSolver.step(context);
    if (step.itDepends()) {
        Expression splitter = step.getSplitter();
        ContextSplitting split = (ContextSplitting) step.getContextSplittingWhenSplitterIsLiteral();
        myAssert(() -> split.isUndefined(), () -> "Undefined " + ContextSplitting.class + " result value: " + split.getResult());
        Expression subSolution1 = solve(step.getStepSolverForWhenSplitterIsTrue(), split.getConstraintAndLiteral());
        Expression subSolution2 = solve(step.getStepSolverForWhenSplitterIsFalse(), split.getConstraintAndLiteralNegation());
        result = IfThenElse.make(splitter, subSolution1, subSolution2, true);
    } else {
        result = step.getValue();
    }
    return result;
}
Also used : Expression(com.sri.ai.expresso.api.Expression) ExpressionLiteralSplitterStepSolver(com.sri.ai.grinder.api.ExpressionLiteralSplitterStepSolver) ContextSplitting(com.sri.ai.grinder.core.constraint.ContextSplitting)

Example 10 with ExpressionLiteralSplitterStepSolver

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

the class TheorySolvedSingleQuantifierEliminationProblem method solve.

public Expression solve(Context context) {
    ExpressionLiteralSplitterStepSolver quantifierEliminatorStepSolver = makeStepSolver(context);
    Expression result = quantifierEliminatorStepSolver.solve(context);
    return result;
}
Also used : Expression(com.sri.ai.expresso.api.Expression) ExpressionLiteralSplitterStepSolver(com.sri.ai.grinder.api.ExpressionLiteralSplitterStepSolver)

Aggregations

ExpressionLiteralSplitterStepSolver (com.sri.ai.grinder.api.ExpressionLiteralSplitterStepSolver)26 Expression (com.sri.ai.expresso.api.Expression)14 Context (com.sri.ai.grinder.api.Context)5 ContextSplitting (com.sri.ai.grinder.core.constraint.ContextSplitting)5 ExpressionStepSolver (com.sri.ai.grinder.api.ExpressionStepSolver)4 Theory (com.sri.ai.grinder.api.Theory)4 ExpressionStepSolverToLiteralSplitterStepSolverAdapter (com.sri.ai.grinder.core.solver.ExpressionStepSolverToLiteralSplitterStepSolverAdapter)4 QuantifierEliminationOnBodyInWhichIndexOnlyOccursInsideLiteralsStepSolver (com.sri.ai.grinder.core.solver.QuantifierEliminationOnBodyInWhichIndexOnlyOccursInsideLiteralsStepSolver)4 Test (org.junit.Test)4 Step (com.sri.ai.grinder.api.ExpressionLiteralSplitterStepSolver.Step)3 StepSolver (com.sri.ai.grinder.api.StepSolver)3 AbstractTheory (com.sri.ai.grinder.core.constraint.AbstractTheory)3 Rewriter (com.sri.ai.grinder.rewriter.api.Rewriter)3 ConstantExpressionStepSolver (com.sri.ai.grinder.theory.base.ConstantExpressionStepSolver)3 Expressions (com.sri.ai.expresso.helper.Expressions)2 Solution (com.sri.ai.grinder.api.ExpressionLiteralSplitterStepSolver.Solution)2 TrueContext (com.sri.ai.grinder.core.TrueContext)2 Sum (com.sri.ai.grinder.group.Sum)2 TheoryTestingSupport (com.sri.ai.grinder.tester.TheoryTestingSupport)2 ConstantStepSolver (com.sri.ai.grinder.theory.base.ConstantStepSolver)2