use of com.sri.ai.grinder.library.FunctorConstants.AND 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;
}
Aggregations