use of com.sri.ai.grinder.sgdpllt.api.Context 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));
}
use of com.sri.ai.grinder.sgdpllt.api.Context in project aic-expresso by aic-sri-international.
the class AbstractSingleVariableNumericConstraintFeasibilityRegionStepSolver method makeUpperBoundsAndStrictness.
/**
* A method setting {@link #upperBoundsIncludingImplicitOnes} and {@link #fromUpperBoundsIncludingImplicitOnesToStrictness}
* from constraint and variable's type.
* @param context
*/
protected void makeUpperBoundsAndStrictness(Context context) {
AbstractSingleVariableConstraint abstractSingleVariableConstraint = (AbstractSingleVariableConstraint) constraint;
FunctionIterator<Expression, Pair<Expression, Boolean>> upperBoundsFromPositiveNormalizedAtomsIterator = functionIterator(predicateIterator(abstractSingleVariableConstraint.getPositiveNormalizedAtoms(), e -> e.hasFunctor(LESS_THAN)), // strict
e -> processExplicitUpperBoundAndStrictnessPair(e.get(1), true, context));
FunctionIterator<Expression, Pair<Expression, Boolean>> upperBoundsFromNegativeNormalizedAtomsIterator = functionIterator(predicateIterator(abstractSingleVariableConstraint.getNegativeNormalizedAtoms(), // not (X > Y) <=> X <= Y, so Y is a non-strict upper bound
e -> e.hasFunctor(GREATER_THAN)), // non-strict
e -> processExplicitUpperBoundAndStrictnessPair(e.get(1), false, context));
Pair<Expression, Boolean> typeUpperBound = getTypeUpperBoundAndStrictness(context);
Iterator<Pair<Expression, Boolean>> upperBoundsAndStrictnessIterator = new NestedIterator<>(upperBoundsFromPositiveNormalizedAtomsIterator, upperBoundsFromNegativeNormalizedAtomsIterator, typeUpperBound);
upperBoundsIncludingImplicitOnes = arrayList();
fromUpperBoundsIncludingImplicitOnesToStrictness = map();
for (Pair<Expression, Boolean> boundAndStrictness : in(upperBoundsAndStrictnessIterator)) {
Expression bound = boundAndStrictness.first;
upperBoundsIncludingImplicitOnes.add(bound);
Boolean strictness = boundAndStrictness.second;
Boolean previousStrictness = fromUpperBoundsIncludingImplicitOnesToStrictness.get(bound);
if (previousStrictness == null || (!previousStrictness && strictness)) {
// if no strictness information so far, store current one; otherwise, only need to change it if previous occurrences were non-strict and this one is strict
fromUpperBoundsIncludingImplicitOnesToStrictness.put(bound, strictness);
}
}
}
use of com.sri.ai.grinder.sgdpllt.api.Context 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;
}
use of com.sri.ai.grinder.sgdpllt.api.Context 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;
}
use of com.sri.ai.grinder.sgdpllt.api.Context in project aic-expresso by aic-sri-international.
the class ContextTest method testExtendWith.
@Test
public void testExtendWith() {
Context context;
ExtensionalIndexExpressionsSet indexExpressions;
ExtensionalIndexExpressionsSet expectedNewIndexExpressionsSet;
Triple<Context, ExtensionalIndexExpressionsSet, Expression> triple;
Map<Expression, Expression> symbolsAndTypes;
Expression expressionInScope;
Expression expectedNewExpressionInScope;
context = new TrueContext();
indexExpressions = new ExtensionalIndexExpressionsSet("X", "Integer", "Y", "X..10");
expectedNewIndexExpressionsSet = new ExtensionalIndexExpressionsSet("X'", "Integer", "Y'", "X'..10");
expressionInScope = parse("X = 1 and Y = 2");
expectedNewExpressionInScope = parse("X' = 1 and Y' = 2");
symbolsAndTypes = map(parse("X"), parse("Integer"), parse("Y"), parse("X..10"), parse("X'"), parse("Integer"), parse("Y'"), parse("X'..10"));
context = context.extendWith(indexExpressions);
triple = context.extendWith(indexExpressions, expressionInScope);
println(triple);
assertEquals(symbolsAndTypes, triple.first.getSymbolsAndTypes());
assertEquals(expectedNewIndexExpressionsSet, triple.second);
assertEquals(expectedNewExpressionInScope, triple.third);
}
Aggregations