Search in sources :

Example 11 with Type

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

the class MeasureTest method measure.

private Rational measure(String testIntensionalSetString) {
    Expression testIntensionalSetExpression = parse(testIntensionalSetString);
    Expression properlyConditionedIntensionalSetExpression = testIntensionalSetExpression;
    if (Sets.isIntensionalSet(testIntensionalSetExpression)) {
        IntensionalSet intensionalSet = (IntensionalSet) testIntensionalSetExpression;
        List<Expression> indices = IndexExpressions.getIndices(intensionalSet.getIndexExpressions());
        if (indices.size() == 1) {
            Expression index = indices.get(0);
            Context intensionalSetContext = context.extendWith(intensionalSet.getIndexExpressions());
            Type type = GrinderUtil.getType(index, intensionalSetContext);
            if (type instanceof RealExpressoType || type instanceof RealInterval) {
                SingleVariableLinearRealArithmeticConstraint singleVariableConstraint = new SingleVariableLinearRealArithmeticConstraint(index, true, context.getTheory());
                singleVariableConstraint = (SingleVariableLinearRealArithmeticConstraint) singleVariableConstraint.conjoin(intensionalSet.getCondition(), intensionalSetContext);
                properlyConditionedIntensionalSetExpression = IntensionalSet.make(Sets.isMultiSet(intensionalSet) ? IntensionalSet.MULTI_SET_LABEL : IntensionalSet.UNI_SET_LABEL, intensionalSet.getIndexExpressions(), intensionalSet.getHead(), singleVariableConstraint);
            }
        }
    }
    Rational result = Measure.get(properlyConditionedIntensionalSetExpression, context);
    return result;
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context) TrueContext(com.sri.ai.grinder.sgdpllt.core.TrueContext) Type(com.sri.ai.expresso.api.Type) RealExpressoType(com.sri.ai.expresso.type.RealExpressoType) IntensionalSet(com.sri.ai.expresso.api.IntensionalSet) Rational(com.sri.ai.util.math.Rational) Expression(com.sri.ai.expresso.api.Expression) RealExpressoType(com.sri.ai.expresso.type.RealExpressoType) SingleVariableLinearRealArithmeticConstraint(com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.SingleVariableLinearRealArithmeticConstraint) RealInterval(com.sri.ai.expresso.type.RealInterval)

Example 12 with Type

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

the class InversionPerformanceEvaluationTest method computeSize.

private Rational computeSize(Expression functionOnIntensionalSet, Rational resultSoFar, Context context) {
    IntensionalSet intensionalSet = (IntensionalSet) functionOnIntensionalSet.get(0);
    IndexExpressionsSet indexExpressionsSet = intensionalSet.getIndexExpressions();
    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);
    Rational result = resultSoFar.multiply(type.cardinality().rationalValue());
    Expression head = intensionalSet.getHead();
    if (Expressions.isFunctionApplicationWithArguments(head) && Sets.isIntensionalSet(head.get(0))) {
        result = computeSize(head, result, intensionalSetContext);
    }
    return result;
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context) TrueContext(com.sri.ai.grinder.sgdpllt.core.TrueContext) Type(com.sri.ai.expresso.api.Type) IntensionalSet(com.sri.ai.expresso.api.IntensionalSet) Rational(com.sri.ai.util.math.Rational) Expression(com.sri.ai.expresso.api.Expression) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet)

Example 13 with Type

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

the class LambdaBetaReductionSimplifierTest method testNoReduction.

@Test
public void testNoReduction() {
    Type peopleType = new Categorical("People", 4, arrayList(makeSymbol("ann"), makeSymbol("bob"), makeSymbol("tom")));
    Context context = new TrueContext();
    context = context.add(peopleType);
    Assert.assertEquals(parse("(lambda X in People : if X = ann then 0 else if X = bob then 0 else 0)()"), simplifier.apply(parse("(lambda X in People : if X = ann then 0 else if X = bob then 0 else 0)()"), context));
    Assert.assertEquals(parse("(lambda X in People, Y in People : if X = ann then 0 else if Y = bob then 0 else 0)(ann)"), simplifier.apply(parse("(lambda X in People, Y in People : if X = ann then 0 else if Y = bob then 0 else 0)(ann)"), context));
    Assert.assertEquals(parse("(lambda X in People, Y in People : if X = ann then 0 else if Y = bob then 0 else 0)(ann, bob, tom)"), simplifier.apply(parse("(lambda X in People, Y in People : if X = ann then 0 else if Y = bob then 0 else 0)(ann, bob, tom)"), context));
}
Also used : TrueContext(com.sri.ai.grinder.sgdpllt.core.TrueContext) Context(com.sri.ai.grinder.sgdpllt.api.Context) Type(com.sri.ai.expresso.api.Type) Categorical(com.sri.ai.expresso.type.Categorical) TrueContext(com.sri.ai.grinder.sgdpllt.core.TrueContext) Test(org.junit.Test)

Example 14 with Type

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

the class TupleTheory method getSingleVariableConstraintQuantifierEliminatorStepSolver.

@Override
public ExpressionLiteralSplitterStepSolver getSingleVariableConstraintQuantifierEliminatorStepSolver(AssociativeCommutativeGroup group, SingleVariableConstraint constraint, Expression body, 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 = constraint.getVariable();
    Expression typeExpression = GrinderUtil.getTypeExpression(variable, context);
    Type type = context.getType(typeExpression);
    if (!isSuitableFor(variable, 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 exprE = group.makeProblemExpression(variable, typeExpression, constraint, body);
    // - use TupleQuantifierSimplifier to transform it to another expression E' without quantification on tuples
    Expression exprEPrime = tupleQuantifierSimplifier.apply(exprE, context);
    // - return context.getTheory().getRewriter().makeStepSolver(E')
    ExpressionLiteralSplitterStepSolver result = context.getTheory().getRewriter().makeStepSolver(exprEPrime);
    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.sgdpllt.api.ExpressionLiteralSplitterStepSolver)

Example 15 with Type

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

the class TupleValuedFreeVariablesSimplifier method extendContextWithComponentVariables.

private static Context extendContextWithComponentVariables(Context context, Map<Expression, TupleType> freeVariablesOfTupleType, Map<Expression, List<Pair<Expression, Integer>>> freeVariableComponentsMap) {
    Map<String, String> mapFromSymbolNameToTypeName = new LinkedHashMap<>();
    Set<Type> componentTypes = new LinkedHashSet<>();
    for (Map.Entry<Expression, TupleType> freeVariableOfTupleType : freeVariablesOfTupleType.entrySet()) {
        Expression freeVariable = freeVariableOfTupleType.getKey();
        TupleType freeVariableTupleType = freeVariableOfTupleType.getValue();
        componentTypes.addAll(freeVariableTupleType.getElementTypes());
        List<Pair<Expression, Integer>> components = freeVariableComponentsMap.get(freeVariable);
        for (Pair<Expression, Integer> freeVariableComponent : components) {
            Expression freeVariableComponentVar = freeVariableComponent.first;
            Type freeVariableComponentType = freeVariableTupleType.getElementTypes().get(freeVariableComponent.second - 1);
            mapFromSymbolNameToTypeName.put(freeVariableComponentVar.toString(), freeVariableComponentType.getName());
        }
    }
    Context result = (Context) GrinderUtil.extendRegistryWith(mapFromSymbolNameToTypeName, componentTypes, context);
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Context(com.sri.ai.grinder.sgdpllt.api.Context) LinkedHashMap(java.util.LinkedHashMap) Type(com.sri.ai.expresso.api.Type) TupleType(com.sri.ai.expresso.type.TupleType) Expression(com.sri.ai.expresso.api.Expression) TupleType(com.sri.ai.expresso.type.TupleType) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Pair(com.sri.ai.util.base.Pair)

Aggregations

Type (com.sri.ai.expresso.api.Type)123 Expression (com.sri.ai.expresso.api.Expression)93 FunctionType (com.sri.ai.expresso.type.FunctionType)28 Test (org.junit.Test)25 Context (com.sri.ai.grinder.api.Context)24 LinkedHashMap (java.util.LinkedHashMap)22 Categorical (com.sri.ai.expresso.type.Categorical)21 Context (com.sri.ai.grinder.sgdpllt.api.Context)20 RealExpressoType (com.sri.ai.expresso.type.RealExpressoType)19 TupleType (com.sri.ai.expresso.type.TupleType)19 ArrayList (java.util.ArrayList)19 Map (java.util.Map)19 IndexExpressionsSet (com.sri.ai.expresso.api.IndexExpressionsSet)18 IntegerInterval (com.sri.ai.expresso.type.IntegerInterval)18 IntegerExpressoType (com.sri.ai.expresso.type.IntegerExpressoType)15 RealInterval (com.sri.ai.expresso.type.RealInterval)14 Beta (com.google.common.annotations.Beta)13 IntensionalSet (com.sri.ai.expresso.api.IntensionalSet)12 ExtensionalIndexExpressionsSet (com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet)12 List (java.util.List)12