use of com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet in project aic-expresso by aic-sri-international.
the class ForAll method make.
public static Expression make(IndexExpressionsSet indexExpressions, Expression body) {
Expression current = body;
List<Expression> indexExpressionsList = ((ExtensionalIndexExpressionsSet) indexExpressions).getList();
for (int i = indexExpressionsList.size() - 1; i >= 0; i--) {
current = make(indexExpressionsList.get(i), current);
}
return current;
}
use of com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet in project aic-expresso by aic-sri-international.
the class ThereExists method make.
public static Expression make(IndexExpressionsSet indexExpressions, Expression body) {
Expression current = body;
List<Expression> indexExpressionsList = ((ExtensionalIndexExpressionsSet) indexExpressions).getList();
for (int i = indexExpressionsList.size() - 1; i >= 0; i--) {
current = make(indexExpressionsList.get(i), current);
}
return current;
}
use of com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet 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);
}
use of com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet in project aic-expresso by aic-sri-international.
the class SampleMultiIndexQuantifierEliminator method computeMeasureAndDetermineIfShouldSample.
private Pair<Rational, Boolean> computeMeasureAndDetermineIfShouldSample(Expression index, Expression indexCondition, Expression additiveIdentityElement, Context context) {
Pair<Rational, Boolean> result;
Expression indexType = GrinderUtil.getTypeExpression(index, context);
IndexExpressionsSet indexExpressionsSet = new ExtensionalIndexExpressionsSet(IndexExpressions.makeIndexExpression(index, indexType));
Expression intensionalSet = IntensionalSet.intensionalMultiSet(indexExpressionsSet, index, indexCondition);
Rational measureSetOfI = Measure.get(intensionalSet, context);
boolean sample = true;
if (!alwaysSample) {
Type type = GrinderUtil.getType(index, context);
// NOTE: We always sample from continuous domains
if (type != null && type.isDiscrete()) {
if (measureSetOfI.compareTo(sampleSizeN) <= 0) {
// Domain is discrete and sample size is >= the size of the domain
// so we don't want to sample in this instance
sample = false;
}
}
}
result = new Pair<>(measureSetOfI, sample);
return result;
}
use of com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet 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;
}
Aggregations