use of com.sri.ai.expresso.api.IntensionalSet 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.expresso.api.IntensionalSet in project aic-expresso by aic-sri-international.
the class IntensionalUnionIntersectionEqualToEmptySetSimplifier method intensionalSetToForAllAndPhi.
private static Expression[] intensionalSetToForAllAndPhi(IntensionalSet intensionalSet, List<Expression> forAllsSoFar, Context context) {
IntensionalSet saIntensionalSet = standardizeApart(intensionalSet, forAllsSoFar, context);
Expression forAll = ForAll.make(saIntensionalSet.getIndexExpressions(), saIntensionalSet.getCondition());
Expression phi = saIntensionalSet.getHead();
Expression[] result = new Expression[] { forAll, phi };
return result;
}
use of com.sri.ai.expresso.api.IntensionalSet in project aic-expresso by aic-sri-international.
the class IntensionalUnionIntersectionEqualToEmptySetSimplifier method simplify.
public static Expression simplify(Expression expression, Context context) {
Expression result = expression;
if (Equality.isEquality(expression) && expression.numberOfArguments() == 2) {
Expression intersection = null;
Expression emptySet = null;
for (Expression arg : expression.getArguments()) {
if (Expressions.hasFunctor(arg, FunctorConstants.INTERSECTION)) {
intersection = arg;
} else if (Sets.isEmptySet(arg)) {
emptySet = arg;
}
}
if (intersection != null && emptySet != null) {
// (∪<sub>i ∈ D:C</sub>Φ) ∩ (∪<sub>i' ∈ D':C'</sub>Φ') = ∅
if (intersection.getArguments().stream().allMatch(Sets::isIntensionalUnion)) {
List<Expression> forAlls = new ArrayList<>();
List<Expression> phis = new ArrayList<>();
// ∀ i ∈ D : C ⇒ ∀ i' ∈ D' : C' ⇒ (Φ ∩ Φ' = ∅)
for (Expression intensionalUnion : intersection.getArguments()) {
IntensionalSet intensionalSet = (IntensionalSet) intensionalUnion.get(0);
Expression[] forAllAndPhi = intensionalSetToForAllAndPhi(intensionalSet, forAlls, context);
Expression forAll = forAllAndPhi[0];
Expression phi = forAllAndPhi[1];
handleNestedForAlls(forAll, forAlls);
phis.add(phi);
}
result = Equality.make(Expressions.apply(FunctorConstants.INTERSECTION, phis), emptySet);
for (int i = forAlls.size() - 1; i >= 0; i--) {
Expression forAll = forAlls.get(i);
Expression body = Implication.make(ForAll.getBody(forAll), result);
result = ForAll.make(ForAll.getIndexExpression(forAll), body);
}
}
}
}
return result;
}
use of com.sri.ai.expresso.api.IntensionalSet in project aic-expresso by aic-sri-international.
the class IntensionalUnionToUnionsOfIntensionalSetsOfBaseTypeSimplifier method simplify.
public static Expression simplify(Expression expression, Context context) {
Expression result = expression;
if (Sets.isIntensionalUnion(expression)) {
IntensionalSet intensionalMultiSet = (IntensionalSet) expression.get(0);
Expression intensionalHead = intensionalMultiSet.getHead();
// TODO - base case set is an extensional uniset?
if (Sets.isExtensionalSet(intensionalHead) && intensionalHead.numberOfArguments() == 1) {
Expression baseTypeHead = intensionalHead.get(0);
result = IntensionalSet.intensionalMultiSet(intensionalMultiSet.getIndexExpressions(), baseTypeHead, intensionalMultiSet.getCondition());
} else // Determine if recursive case
if (intensionalHead.hasFunctor(FunctorConstants.UNION)) {
// Union( {{ (on I) Set1 union ... union Set_m : C }} )
// --->
// recurse(Union( {{ (on I) Set1 : C }} ))
// union ... union
// recurse(Union( {{ (on I) Set_m : C }} ))
List<Expression> recursedUnionArgs = new ArrayList<>();
for (Expression setI : intensionalHead.getArguments()) {
Expression setIUnionArg = IntensionalSet.intensionalMultiSet(intensionalMultiSet.getIndexExpressions(), setI, intensionalMultiSet.getCondition());
Expression setIUnion = Expressions.apply(FunctorConstants.INTENSIONAL_UNION, setIUnionArg);
Expression setIRecurseResult = simplify(setIUnion, context);
recursedUnionArgs.add(setIRecurseResult);
}
result = Sets.makeUnion(recursedUnionArgs.toArray(new Expression[recursedUnionArgs.size()]));
}
}
return result;
}
use of com.sri.ai.expresso.api.IntensionalSet in project aic-expresso by aic-sri-international.
the class InversionSimplifier method isFunctionOnIntensionalSetWithSingleIndex.
private static boolean isFunctionOnIntensionalSetWithSingleIndex(Object functor, Expression expression) {
boolean result = false;
if (((functor == null && Expressions.isFunctionApplicationWithArguments(expression)) || expression.hasFunctor(functor)) && expression.numberOfArguments() == 1) {
Expression expressionArg1 = expression.get(0);
if (Sets.isIntensionalSet(expressionArg1)) {
IntensionalSet intensionalSet = (IntensionalSet) expressionArg1;
IndexExpressionsSet indexExpressionsSet = intensionalSet.getIndexExpressions();
List<Expression> indices = IndexExpressions.getIndices(indexExpressionsSet);
if (indices.size() == 1) {
result = true;
}
}
}
return result;
}
Aggregations