use of com.sri.ai.grinder.api.Context in project aic-expresso by aic-sri-international.
the class DefaultIntensionalBound method boundProduct.
public static DefaultIntensionalBound boundProduct(Theory theory, Context context, Bound... listOfBounds) {
if (listOfBounds.length == 0) {
DefaultIntensionalBound result = new DefaultIntensionalBound();
return result;
}
Set<Expression> alreadyDefined = Util.set();
alreadyDefined.addAll(context.getSymbols());
Predicate<Expression> isAlreadyDefined = e -> alreadyDefined.contains(e);
ArrayList<Expression> productIndexExpressionList = new ArrayList<>();
Object[] productHeadArray = new Expression[listOfBounds.length];
Object[] productConditionArray = new Expression[listOfBounds.length];
int k = 0;
for (Bound bound : Arrays.asList(listOfBounds)) {
if (!bound.isIntensionalBound()) {
return null;
}
DefaultIntensionalBound intensionalBound = (DefaultIntensionalBound) bound;
ExtensionalIndexExpressionsSet indexExpressions = (ExtensionalIndexExpressionsSet) intensionalBound.getIndexExpressions();
Expression Head = intensionalBound.getHead();
Expression condition = intensionalBound.getCondition();
ArrayList<Expression> newIndexExpressionsList = new ArrayList<>(indexExpressions.getList());
for (int i = 0; i != newIndexExpressionsList.size(); i++) {
Expression indexExpression = newIndexExpressionsList.get(i);
Symbol index = (Symbol) indexExpression.get(0);
Expression type = indexExpression.get(1);
PairOf<Expression> newIndexAndNewExpressionInScope = Expressions.standardizeApart(index, isAlreadyDefined, Head);
Expression newIndex = newIndexAndNewExpressionInScope.first;
Head = newIndexAndNewExpressionInScope.second;
// type should not contain the index
Expression newIndexExpression = apply(IN, newIndex, type);
context = context.extendWithSymbolsAndTypes(newIndex, type);
newIndexExpressionsList.set(i, newIndexExpression);
alreadyDefined.add(newIndex);
for (int j = i + 1; j != newIndexExpressionsList.size(); j++) {
Expression anotherIndexExpression = newIndexExpressionsList.get(j);
Expression anotherIndex = anotherIndexExpression.get(0);
Expression anotherType = anotherIndexExpression.get(1);
Expression newAnotherType = anotherType.replaceSymbol(index, newIndex, context);
// anotherIndex is a symbols and does not contain index
Expression newAnotherIndexExpression = apply(IN, anotherIndex, newAnotherType);
newIndexExpressionsList.set(j, newAnotherIndexExpression);
}
}
productIndexExpressionList.addAll(newIndexExpressionsList);
productHeadArray[k] = Head;
productConditionArray[k] = condition;
k++;
}
Expression productCondition = apply(AND, productConditionArray);
productCondition = theory.evaluate(productCondition, context);
Expression productHead = apply(TIMES, productHeadArray);
productHead = theory.evaluate(productHead, context);
DefaultIntensionalBound result = new DefaultIntensionalBound(productIndexExpressionList, productHead, productCondition);
return result;
}
use of com.sri.ai.grinder.api.Context in project aic-expresso by aic-sri-international.
the class IntensionalSetConditionSimplifier method simplify.
public static Expression simplify(Expression expression, Context context) {
Expression result = expression;
if (Sets.isIntensionalSet(expression)) {
IntensionalSet intensionalSet = (IntensionalSet) expression;
Expression condition = intensionalSet.getCondition();
if (!(condition.equals(Expressions.TRUE) || condition.equals(Expressions.FALSE))) {
IndexExpressionsSet indexExpressionsSet = intensionalSet.getIndexExpressions();
Context extendedContext = context.extendWith(indexExpressionsSet);
Expression evaluatedCondition = context.getTheory().evaluate(condition, extendedContext);
if (!evaluatedCondition.equals(condition)) {
result = IntensionalSet.make(Sets.isIntensionalMultiSet(expression) ? IntensionalSet.MULTI_SET_LABEL : IntensionalSet.UNI_SET_LABEL, indexExpressionsSet, intensionalSet.getHead(), evaluatedCondition);
}
}
}
return result;
}
use of com.sri.ai.grinder.api.Context in project aic-expresso by aic-sri-international.
the class Measure method get.
public static Rational get(Expression intensionalSetExpression, Context context) {
Rational result;
if (Sets.isIntensionalSet(intensionalSetExpression)) {
IntensionalSet intensionalSet = (IntensionalSet) intensionalSetExpression;
IndexExpressionsSet indexExpressionsSet = intensionalSet.getIndexExpressions();
List<Expression> indices = IndexExpressions.getIndices(indexExpressionsSet);
if (indices.size() == 1) {
Expression evaluatedResult;
Expression intensionalSetIndex = indices.get(0);
Expression intensionalSetHead = intensionalSet.getHead();
if (!intensionalSetHead.equals(intensionalSetIndex)) {
throw new UnsupportedOperationException("Index and Head must be the same to calculate the meaure of an Intensional : " + intensionalSet);
}
Expression intensionalSetCondition = intensionalSet.getCondition();
Context intensionalSetContext = context.extendWith(indexExpressionsSet);
Type indexType = GrinderUtil.getTypeOfExpression(intensionalSetIndex, intensionalSetContext);
if (intensionalSetCondition.equals(false)) {
// short circuit known empty sets up front.
evaluatedResult = Expressions.ZERO;
} else if (indexType instanceof RealExpressoType || indexType instanceof RealInterval) {
// NOTE : For Reals can always assume the condition is of this type.
SingleVariableLinearRealArithmeticConstraint svConstraint = (SingleVariableLinearRealArithmeticConstraint) intensionalSetCondition;
MeasureOfSingleVariableLinearRealArithmeticConstraintStepSolver realSolver = new MeasureOfSingleVariableLinearRealArithmeticConstraintStepSolver(svConstraint);
evaluatedResult = realSolver.solve(intensionalSetContext);
} else if (indexType instanceof FunctionType) {
if (!intensionalSetCondition.equals(true)) {
throw new UnsupportedOperationException("Measure of intensional set with a function type domain currently do not support conditions: " + intensionalSet);
}
// measure(co-domain)^measure(domain)
FunctionType indexFunctionType = (FunctionType) indexType;
Expression condomainIntensionalSet = constructComponentIntensionalSet(indexFunctionType.getCodomain(), intensionalSet, ZERO, intensionalSetContext);
Rational codomainMeasure = get(condomainIntensionalSet, intensionalSetContext);
Rational domainMeasure = Rational.ONE;
for (Type argDomainType : indexFunctionType.getArgumentTypes()) {
Expression argDomainIntensionalSet = constructComponentIntensionalSet(argDomainType, intensionalSet, ZERO, intensionalSetContext);
Rational argMeasure = get(argDomainIntensionalSet, intensionalSetContext);
domainMeasure = domainMeasure.multiply(argMeasure);
}
evaluatedResult = Expressions.makeSymbol(codomainMeasure.pow(domainMeasure.intValueExact()));
} else if (indexType instanceof TupleType) {
if (!intensionalSetCondition.equals(true)) {
throw new UnsupportedOperationException("Measure of intensional set with a tuple type domain currently do not support conditions: " + intensionalSet);
}
// (element_1, ..., element_n) = measure(element_1) * ... * measure(element_n)
TupleType indexTupleType = (TupleType) indexType;
Rational elementMeasuresProduct = Rational.ONE;
for (Type elementType : indexTupleType.getElementTypes()) {
Expression elementDomainIntensionalSet = constructComponentIntensionalSet(elementType, intensionalSet, ZERO, intensionalSetContext);
Rational elementMeasure = get(elementDomainIntensionalSet, intensionalSetContext);
elementMeasuresProduct = elementMeasuresProduct.multiply(elementMeasure);
}
evaluatedResult = Expressions.makeSymbol(elementMeasuresProduct);
} else {
Expression countingFormula = new DefaultCountingFormula(indexExpressionsSet, intensionalSet.getCondition());
evaluatedResult = context.getTheory().evaluate(countingFormula, context);
}
if (Expressions.isNumber(evaluatedResult)) {
result = evaluatedResult.rationalValue();
} else {
throw new UnsupportedOperationException("Unable to compute a finite measure for: " + intensionalSet + ", got : " + evaluatedResult);
}
} else {
throw new UnsupportedOperationException("Currently only support the measure of single indexed intensional sets: " + intensionalSet);
}
} else {
throw new IllegalArgumentException("Not an intensional set: " + intensionalSetExpression);
}
return result;
}
use of com.sri.ai.grinder.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;
}
use of com.sri.ai.grinder.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;
}
Aggregations