Search in sources :

Example 71 with Context

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

the class InversionSimplifier method applyInversion.

private static Expression applyInversion(Expression summationIndexedByFunction, Expression summationIndexFunctionName, FunctionType summationIndexFunctionType, List<Expression> originalQuantifierOrder, List<Expression> inversionQuantifierOrder, Context context) {
    Expression result;
    int indexOfSummationIndexedByFunction = inversionQuantifierOrder.indexOf(summationIndexedByFunction);
    // NOTE: only products will bubble up before the summation.
    List<Expression> productsBefore = inversionQuantifierOrder.subList(0, indexOfSummationIndexedByFunction);
    Expression lastQuantifierHead = getHead(originalQuantifierOrder.get(originalQuantifierOrder.size() - 1));
    Context innerContext = context;
    for (Expression quantifier : originalQuantifierOrder) {
        innerContext = innerContext.extendWith(getIndexExpressions(quantifier));
    }
    Context lastQuantifierHeadContext = innerContext;
    // TODO - remove temporary hack which collapses the function's argument domains
    // due to all the domain arguments being treated as constants. Instead should be using
    // set expression types on singletons.				
    // Determine which domain arguments from the summation function can be collapsed
    List<Expression> productsBeforeIndices = new ArrayList<>();
    for (Expression productBefore : productsBefore) {
        productsBeforeIndices.add(getIndexAndType(productBefore).first);
    }
    Set<Integer> domainArgsToRemove = new HashSet<>();
    Set<Integer> domainArgsHaveVar = new HashSet<>();
    new SubExpressionsDepthFirstIterator(lastQuantifierHead).forEachRemaining(e -> {
        if (e.hasFunctor(summationIndexFunctionName)) {
            for (int i = 0; i < e.numberOfArguments(); i++) {
                if (lastQuantifierHeadContext.getTheory().isVariable(e.get(0), lastQuantifierHeadContext)) {
                    domainArgsHaveVar.add(i);
                }
                if (productsBeforeIndices.contains(e.get(i)) || Util.thereExists(new SubExpressionsDepthFirstIterator(e.get(i)), es -> productsBeforeIndices.contains(es))) {
                    domainArgsToRemove.add(i);
                }
            }
        }
    });
    // Remove arg positions that were not populated by a variable.
    for (int i = 0; i < summationIndexFunctionType.getArity(); i++) {
        if (!domainArgsHaveVar.contains(i)) {
            domainArgsToRemove.add(i);
        }
    }
    List<Expression> argTypes = new ArrayList<>();
    for (int i = 0; i < summationIndexFunctionType.getArity(); i++) {
        if (!domainArgsToRemove.contains(i)) {
            argTypes.add(parse(summationIndexFunctionType.getArgumentTypes().get(i).getName()));
        }
    }
    Expression codomainType = parse(summationIndexFunctionType.getCodomain().getName());
    Expression summationIndexReducedType;
    if (argTypes.size() == 0) {
        summationIndexReducedType = codomainType;
    } else {
        summationIndexReducedType = FunctionType.make(codomainType, argTypes);
    }
    Expression summationIndex = IndexExpressions.makeIndexExpression(summationIndexFunctionName, summationIndexReducedType);
    Expression phi = lastQuantifierHead.replaceAllOccurrences(e -> {
        Expression r = e;
        if (e.hasFunctor(summationIndexFunctionName)) {
            List<Expression> argsToKeep = new ArrayList<>();
            for (int i = 0; i < e.numberOfArguments(); i++) {
                if (!domainArgsToRemove.contains(i)) {
                    argsToKeep.add(e.get(i));
                }
            }
            if (argsToKeep.size() > 0) {
                r = Expressions.apply(summationIndexFunctionName, argsToKeep);
            } else {
                r = summationIndexFunctionName;
            }
        }
        return r;
    }, lastQuantifierHeadContext);
    Expression summationHead = phi;
    for (int i = indexOfSummationIndexedByFunction + 1; i < inversionQuantifierOrder.size(); i++) {
        Expression quantifier = inversionQuantifierOrder.get(i);
        Expression quantifierIntensionalSet = IntensionalSet.intensionalMultiSet(getIndexExpressions(quantifier), summationHead, getCondition(quantifier));
        summationHead = Expressions.apply(quantifier.getFunctor(), quantifierIntensionalSet);
    }
    Expression innerSummation = IntensionalSet.intensionalMultiSet(new ExtensionalIndexExpressionsSet(summationIndex), summationHead, getCondition(summationIndexedByFunction));
    result = Expressions.apply(FunctorConstants.SUM, innerSummation);
    for (int i = productsBefore.size() - 1; i >= 0; i--) {
        Expression product = productsBefore.get(i);
        product = IntensionalSet.intensionalMultiSet(getIndexExpressions(product), result, getCondition(product));
        result = Expressions.apply(FunctorConstants.PRODUCT, product);
    }
    return result;
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context) SubExpressionsDepthFirstIterator(com.sri.ai.expresso.helper.SubExpressionsDepthFirstIterator) ForAll(com.sri.ai.grinder.sgdpllt.library.boole.ForAll) Expressions(com.sri.ai.expresso.helper.Expressions) Expression(com.sri.ai.expresso.api.Expression) Disequality(com.sri.ai.grinder.sgdpllt.library.Disequality) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) GrinderUtil(com.sri.ai.grinder.helper.GrinderUtil) ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) Expressions.parse(com.sri.ai.expresso.helper.Expressions.parse) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet) And(com.sri.ai.grinder.sgdpllt.library.boole.And) Pair(com.sri.ai.util.base.Pair) Equality(com.sri.ai.grinder.sgdpllt.library.Equality) Sets(com.sri.ai.grinder.sgdpllt.library.set.Sets) Type(com.sri.ai.expresso.api.Type) Implication(com.sri.ai.grinder.sgdpllt.library.boole.Implication) Simplifier(com.sri.ai.grinder.sgdpllt.rewriter.api.Simplifier) Set(java.util.Set) IntensionalSet(com.sri.ai.expresso.api.IntensionalSet) Context(com.sri.ai.grinder.sgdpllt.api.Context) FunctionType(com.sri.ai.expresso.type.FunctionType) List(java.util.List) IndexExpressions(com.sri.ai.grinder.sgdpllt.library.indexexpression.IndexExpressions) Util(com.sri.ai.util.Util) FunctorConstants(com.sri.ai.grinder.sgdpllt.library.FunctorConstants) ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) Expression(com.sri.ai.expresso.api.Expression) ArrayList(java.util.ArrayList) SubExpressionsDepthFirstIterator(com.sri.ai.expresso.helper.SubExpressionsDepthFirstIterator) HashSet(java.util.HashSet)

Example 72 with Context

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

the class InversionSimplifier method getIndexAndFunctionType.

private static Pair<Expression, FunctionType> getIndexAndFunctionType(Expression functionOnIntensionalSet, Context context) {
    IndexExpressionsSet indexExpressionsSet = getIndexExpressions(functionOnIntensionalSet);
    List<Expression> indices = IndexExpressions.getIndices(indexExpressionsSet);
    if (indices.size() != 1) {
        throw new UnsupportedOperationException("Currently only support singular indices");
    }
    Expression index = indices.get(0);
    Context intensionalSetContext = context.extendWith(indexExpressionsSet);
    Type type = GrinderUtil.getType(index, intensionalSetContext);
    FunctionType functionType = null;
    if (type instanceof FunctionType) {
        functionType = (FunctionType) type;
    }
    Pair<Expression, FunctionType> result = new Pair<>(index, functionType);
    return result;
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context) Type(com.sri.ai.expresso.api.Type) FunctionType(com.sri.ai.expresso.type.FunctionType) Expression(com.sri.ai.expresso.api.Expression) FunctionType(com.sri.ai.expresso.type.FunctionType) ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet) Pair(com.sri.ai.util.base.Pair)

Example 73 with Context

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

the class IntensionalUnionIntersectionEqualToEmptySetSimplifier method standardizeApart.

private static IntensionalSet standardizeApart(IntensionalSet intensionalSet, List<Expression> forAllsSoFar, Context context) {
    IntensionalSet result = intensionalSet;
    if (forAllsSoFar.size() > 0) {
        // May need to standardize apart
        List<Expression> tupleArgs = new ArrayList<>();
        for (Expression forAll : forAllsSoFar) {
            tupleArgs.add(ForAll.getIndexExpression(forAll));
            tupleArgs.add(ForAll.getBody(forAll));
        }
        Expression tupleOfForAlls = Expressions.makeTuple(tupleArgs);
        List<Expression> originalIndexes = IndexExpressions.getIndices(intensionalSet.getIndexExpressions());
        List<Expression> saIndexes = new ArrayList<>();
        for (Expression index : originalIndexes) {
            Expression saIndex = Expressions.primedUntilUnique(index, tupleOfForAlls, context);
            saIndexes.add(saIndex);
        }
        if (!originalIndexes.equals(saIndexes)) {
            // We need to update intensional set with standardized apart indices
            IndexExpressionsSet saIndexExpressionsSet = intensionalSet.getIndexExpressions();
            Expression saHead = intensionalSet.getHead();
            Expression saCondition = intensionalSet.getCondition();
            Context intensionalSetContext = context.extendWith(saIndexExpressionsSet);
            for (int i = 0; i < originalIndexes.size(); i++) {
                Expression originalIndex = originalIndexes.get(i);
                Expression saIndex = saIndexes.get(i);
                if (!originalIndex.equals(saIndex)) {
                    saIndexExpressionsSet = saIndexExpressionsSet.replaceSymbol(originalIndex, saIndex, context);
                    saHead = saHead.replaceAllOccurrences(originalIndex, saIndex, intensionalSetContext);
                    saCondition = saCondition.replaceAllOccurrences(originalIndex, saIndex, intensionalSetContext);
                }
            }
            result = (IntensionalSet) IntensionalSet.intensionalSetOfSameKindAs(intensionalSet, saIndexExpressionsSet, saHead, saCondition);
        }
    }
    return result;
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context) IntensionalSet(com.sri.ai.expresso.api.IntensionalSet) Expression(com.sri.ai.expresso.api.Expression) ArrayList(java.util.ArrayList) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet)

Example 74 with Context

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

the class InversionSimplifier method isInvertible.

private static boolean isInvertible(Expression summationIndexedByFunction, Expression summationIndexFunctionName, FunctionType summationIndexFunctionType, List<Expression> originalQuantifierOrder, List<Expression> inversionQuantifierOrder, Context context) {
    boolean result = false;
    int indexOfSummationIndexedByFunction = inversionQuantifierOrder.indexOf(summationIndexedByFunction);
    // NOTE: we will use the full nested context in order to derive primedUntilUnique variables to ensure
    // we have globally unique variables in the expressions (simplifies reading/understanding).
    Expression lastQuantifierHead = getHead(originalQuantifierOrder.get(originalQuantifierOrder.size() - 1));
    Context lastQuantifierHeadContext = context;
    for (Expression quantifier : originalQuantifierOrder) {
        lastQuantifierHeadContext = lastQuantifierHeadContext.extendWith(getIndexExpressions(quantifier));
    }
    // Construct E correctly based on quantifiers after summation.
    Expression E = lastQuantifierHead;
    List<Expression> quantifiersAfter = inversionQuantifierOrder.subList(indexOfSummationIndexedByFunction + 1, inversionQuantifierOrder.size());
    for (int i = quantifiersAfter.size() - 1; i >= 0; i--) {
        E = quantifyE(E, quantifiersAfter.get(i));
    }
    // Now compute ocfE
    Expression ocfE = SetOfArgumentTuplesForFunctionOccurringInExpression.compute(summationIndexFunctionName, summationIndexFunctionType, E);
    // NOTE: only products will bubble up before the summation.
    List<Expression> productsBefore = inversionQuantifierOrder.subList(0, indexOfSummationIndexedByFunction);
    // Create the two sets of replacement quantifiers before summation indexed by function indices
    // to ensure we have disjoint applications.
    List<Expression> productsBeforeIndices = new ArrayList<>();
    for (Expression productBefore : productsBefore) {
        productsBeforeIndices.add(getIndexAndType(productBefore).first);
    }
    List<Expression> allBeforeIndices = new ArrayList<>(productsBeforeIndices);
    List<Expression> productsBeforeIndicesPrime = new ArrayList<>();
    List<Expression> productsBeforeIndices2Prime = new ArrayList<>();
    for (int i = 0; i < productsBeforeIndices.size(); i++) {
        Expression productBeforeIndex = productsBeforeIndices.get(i);
        Expression allIndicesTuple = Expressions.makeTuple(allBeforeIndices);
        Expression productBeforeIndexPrime = Expressions.primedUntilUnique(productBeforeIndex, allIndicesTuple, lastQuantifierHeadContext);
        productsBeforeIndicesPrime.add(productBeforeIndexPrime);
        allBeforeIndices.add(productBeforeIndexPrime);
        allIndicesTuple = Expressions.makeTuple(allBeforeIndices);
        Expression productBeforeIndex2Prime = Expressions.primedUntilUnique(productBeforeIndex, allIndicesTuple, lastQuantifierHeadContext);
        productsBeforeIndices2Prime.add(productBeforeIndex2Prime);
        allBeforeIndices.add(productBeforeIndex2Prime);
    }
    // Create the antecendant part of implication for condition testing inversion
    // i.e.
    //     C_1[x_1/x'_1] and ... C_k[x_k/x'_k]
    // and C_1[x_1/x''_1] and ... C_k[x_k/x''_k]
    // and (x'_1,...,x'_k) != (x''_1,...,x''_k)
    List<Expression> conjunctsPrime = new ArrayList<>();
    List<Expression> conjuncts2Prime = new ArrayList<>();
    for (int i = 0; i < productsBefore.size(); i++) {
        Expression quantifierBefore = productsBefore.get(i);
        Expression condition = getCondition(quantifierBefore);
        // C_n[x_n/x'_n]
        Expression conjunctPrime = replaceAll(condition, productsBeforeIndices, productsBeforeIndicesPrime, lastQuantifierHeadContext);
        conjunctsPrime.add(conjunctPrime);
        // C_n[x_n/x''_n]
        Expression conjunct2Prime = replaceAll(condition, productsBeforeIndices, productsBeforeIndices2Prime, lastQuantifierHeadContext);
        conjuncts2Prime.add(conjunct2Prime);
    }
    // (x'_1,...,x'_k) != (x''_1,...,x''_k)
    Expression primesNotEqual = Disequality.make(Expressions.makeTuple(productsBeforeIndicesPrime), Expressions.makeTuple(productsBeforeIndices2Prime));
    List<Expression> allConjuncts = new ArrayList<>();
    allConjuncts.addAll(conjunctsPrime);
    allConjuncts.addAll(conjuncts2Prime);
    allConjuncts.add(primesNotEqual);
    Expression conjunct = And.make(allConjuncts);
    // Create the consequent part of implication for condition testing inversion
    // i.e.:
    // (oc_f[E][x_1/x'_1,....,x_k/x'_k]
    //       intersection
    //  oc_f[E][x_1/x''_1,....,x_k/x''_k])
    // = {}
    Expression ocfEPrime = replaceAll(ocfE, productsBeforeIndices, productsBeforeIndicesPrime, lastQuantifierHeadContext);
    Expression ocfE2Prime = replaceAll(ocfE, productsBeforeIndices, productsBeforeIndices2Prime, lastQuantifierHeadContext);
    Expression intersection = Sets.makeIntersection(ocfEPrime, ocfE2Prime);
    Expression equality = Equality.make(intersection, Sets.EMPTY_SET);
    Expression implication = Implication.make(conjunct, equality);
    // Collect the index expressions for the universal quantifiers:
    // i.e.
    // for all x'_1 el. T_1 ... for all x'_k el. T_k for all x''_1 el. T_1 ... for all x''_k el. T_k
    List<Expression> productsBeforeIndexExpressionSetsPrime = new ArrayList<>();
    List<Expression> productsBeforeIndexExpressionSets2Prime = new ArrayList<>();
    for (int i = 0; i < productsBefore.size(); i++) {
        Expression productBefore = productsBefore.get(i);
        Expression productBeforeIndexType = getIndexAndType(productBefore).second;
        Expression indexExpressionPrime = IndexExpressions.makeIndexExpression(productsBeforeIndicesPrime.get(i), productBeforeIndexType);
        productsBeforeIndexExpressionSetsPrime.add(indexExpressionPrime);
        Expression indexExpression2Prime = IndexExpressions.makeIndexExpression(productsBeforeIndices2Prime.get(i), productBeforeIndexType);
        productsBeforeIndexExpressionSets2Prime.add(indexExpression2Prime);
    }
    List<Expression> forAllIndexExpressionSets = new ArrayList<>();
    forAllIndexExpressionSets.addAll(productsBeforeIndexExpressionSetsPrime);
    forAllIndexExpressionSets.addAll(productsBeforeIndexExpressionSets2Prime);
    // Construct the nested for all statement.
    Expression forAll = implication;
    for (int i = forAllIndexExpressionSets.size() - 1; i >= 0; i--) {
        Expression forAllIndexExpressionSet = forAllIndexExpressionSets.get(i);
        forAll = ForAll.make(forAllIndexExpressionSet, forAll);
    }
    Expression forAllEvaluated = context.getTheory().evaluate(forAll, context);
    if (Expressions.TRUE.equals(forAllEvaluated)) {
        result = true;
    }
    return result;
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context) Expression(com.sri.ai.expresso.api.Expression) ArrayList(java.util.ArrayList)

Example 75 with Context

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

the class RandomConditionalExpressionGenerator method main.

public static void main(String[] args) {
    Random random = new Random();
    TheoryTestingSupport theoryTestingSupport = TheoryTestingSupport.make(new Random(), new DifferenceArithmeticTheory(true, true));
    for (int numberOfVariables = 3; numberOfVariables != 5; numberOfVariables++) {
        Map<String, Type> variableNamesAndTypes = map();
        Type integerInterval = new IntegerInterval(0, 100);
        for (int v = 0; v != numberOfVariables; v++) {
            variableNamesAndTypes.put("v" + v, integerInterval);
        }
        theoryTestingSupport.setVariableNamesAndTypesForTesting(variableNamesAndTypes);
        Context context = theoryTestingSupport.makeContextWithTestingInformation();
        RandomConditionalExpressionGenerator generator = new RandomConditionalExpressionGenerator(theoryTestingSupport, 4, () -> makeSymbol(random.nextDouble()), context);
        System.out.println();
        System.out.println();
        for (Map.Entry<String, Type> variableNameAndType : variableNamesAndTypes.entrySet()) {
            Type type = variableNameAndType.getValue();
            IntegerInterval interval = (IntegerInterval) type;
            System.out.println("random " + variableNameAndType.getKey() + ": " + interval.getNonStrictLowerBound() + ".." + interval.getNonStrictUpperBound() + ";");
        }
        for (int i = 0; i != 5; i++) {
            Expression output = generator.apply();
            System.out.println(output + ";");
        }
    }
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context) DifferenceArithmeticTheory(com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory) IntegerInterval(com.sri.ai.expresso.type.IntegerInterval) Type(com.sri.ai.expresso.api.Type) Random(java.util.Random) Expression(com.sri.ai.expresso.api.Expression) Map(java.util.Map)

Aggregations

Context (com.sri.ai.grinder.sgdpllt.api.Context)105 Expression (com.sri.ai.expresso.api.Expression)80 Test (org.junit.Test)48 TheoryTestingSupport (com.sri.ai.grinder.sgdpllt.tester.TheoryTestingSupport)36 TrueContext (com.sri.ai.grinder.sgdpllt.core.TrueContext)30 Type (com.sri.ai.expresso.api.Type)28 DifferenceArithmeticTheory (com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory)28 PropositionalTheory (com.sri.ai.grinder.sgdpllt.theory.propositional.PropositionalTheory)22 EqualityTheory (com.sri.ai.grinder.sgdpllt.theory.equality.EqualityTheory)20 CompoundTheory (com.sri.ai.grinder.sgdpllt.theory.compound.CompoundTheory)18 Constraint (com.sri.ai.grinder.sgdpllt.api.Constraint)17 Map (java.util.Map)17 Beta (com.google.common.annotations.Beta)16 CompleteMultiVariableContext (com.sri.ai.grinder.sgdpllt.core.constraint.CompleteMultiVariableContext)16 Expressions.parse (com.sri.ai.expresso.helper.Expressions.parse)14 Theory (com.sri.ai.grinder.sgdpllt.api.Theory)14 LinearRealArithmeticTheory (com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.LinearRealArithmeticTheory)14 FunctionType (com.sri.ai.expresso.type.FunctionType)13 Util (com.sri.ai.util.Util)13 SingleVariableConstraint (com.sri.ai.grinder.sgdpllt.api.SingleVariableConstraint)12