use of com.sri.ai.expresso.api.IntensionalSet in project aic-expresso by aic-sri-international.
the class IntensionalUnionEqualToEmptySetSimplifier method simplify.
public static Expression simplify(Expression expression, Context context) {
Expression result = expression;
if (Equality.isEquality(expression) && expression.numberOfArguments() == 2) {
Expression intensionalUnion = null;
Expression emptySet = null;
for (Expression arg : expression.getArguments()) {
if (Sets.isIntensionalUnion(arg)) {
intensionalUnion = arg;
} else if (Sets.isEmptySet(arg)) {
emptySet = arg;
}
}
// (∪<sub>i ∈ D:C</sub>Φ) = ∅
if (intensionalUnion != null && emptySet != null) {
IntensionalSet intensionalSet = (IntensionalSet) intensionalUnion.get(0);
Expression C = intensionalSet.getCondition();
Expression notC = Not.make(C);
Expression Phi = intensionalSet.getHead();
Expression notCorPhi = Or.make(notC, Phi);
Expression notCorPhiEqualEmptySet = Equality.make(notCorPhi, emptySet);
// ∀ i ∈ D : (¬C ∨ Φ = ∅)
result = ForAll.make(intensionalSet.getIndexExpressions(), notCorPhiEqualEmptySet);
}
}
return result;
}
use of com.sri.ai.expresso.api.IntensionalSet in project aic-expresso by aic-sri-international.
the class Sets method expandApplicationOfAssociativeCommutativeFunctionToIntensionalSetWithMultipleIndexExpressionsFrom.
private static Expression expandApplicationOfAssociativeCommutativeFunctionToIntensionalSetWithMultipleIndexExpressionsFrom(int i, Expression functor, IntensionalSet intensionalSet, ExtensionalIndexExpressionsSet indexExpressions) {
Expression result;
int numberOfIndexExpressions = indexExpressions.getList().size();
if (i == numberOfIndexExpressions - 1) {
Expression iThIndexExpression = indexExpressions.getList().get(i);
ExtensionalIndexExpressionsSet indexExpressionsSetWithIthIndexExpressionOnly = new ExtensionalIndexExpressionsSet(iThIndexExpression);
result = apply(functor, intensionalSet.setIndexExpressions(indexExpressionsSetWithIthIndexExpressionOnly));
} else {
Expression iThIndexExpression = indexExpressions.getList().get(i);
ExtensionalIndexExpressionsSet indexExpressionsSetWithIthIndexExpressionOnly = new ExtensionalIndexExpressionsSet(iThIndexExpression);
Expression innerHead = expandApplicationOfAssociativeCommutativeFunctionToIntensionalSetWithMultipleIndexExpressionsFrom(i + 1, functor, intensionalSet, indexExpressions);
IntensionalSet innerSet = intensionalSet.setHeadAndCondition(innerHead, TRUE);
innerSet = innerSet.setIndexExpressions(indexExpressionsSetWithIthIndexExpressionOnly);
result = apply(functor, innerSet);
}
return result;
}
use of com.sri.ai.expresso.api.IntensionalSet in project aic-expresso by aic-sri-international.
the class Sets method expandApplicationOfAssociativeCommutativeFunction.
/**
* Transforms an application of a associative commutative function to an intensional set with one or more indices
* to an equivalent expression in which intensional sets have a single index each.
* An example is <code>sum( {{ (on X in 1..10, Y in 1..10) 3 | X != Y }} )</code>
* being transformed to <code>sum( {{ (on X in 1..10) sum({{ (on Y in 1..10) 3 | X != Y }}) | true }} )</code>
* @param functionApplicationOnIntensionalSet
* @return
*/
public static Expression expandApplicationOfAssociativeCommutativeFunction(Expression functionApplicationOnIntensionalSet) {
IntensionalSet intensionalSet = (IntensionalSet) functionApplicationOnIntensionalSet.get(0);
ExtensionalIndexExpressionsSet indexExpressions = (ExtensionalIndexExpressionsSet) intensionalSet.getIndexExpressions();
myAssert(() -> indexExpressions.getList().size() != 0, () -> "There must be at least one index expression");
Expression result;
if (indexExpressions.getList().size() == 1) {
result = functionApplicationOnIntensionalSet;
} else {
Expression functor = functionApplicationOnIntensionalSet.getFunctor();
result = expandApplicationOfAssociativeCommutativeFunctionToIntensionalSetWithMultipleIndexExpressionsFrom(0, functor, intensionalSet, indexExpressions);
}
return result;
}
use of com.sri.ai.expresso.api.IntensionalSet in project aic-expresso by aic-sri-international.
the class SampleCommonInterpreterTest method run.
private Expression run(int sampleSizeN, boolean alwaysSample, String expressionString) {
SampleCommonInterpreter interpreter = new SampleCommonInterpreter(sampleSizeN, alwaysSample, random);
Expression expression = parse(expressionString);
if (expression.numberOfArguments() == 1 && Sets.isIntensionalSet(expression.get(0))) {
IntensionalSet intensionalSet = (IntensionalSet) expression.get(0);
IndexExpressionsSet indexExpressions = intensionalSet.getIndexExpressions();
List<Expression> indices = IndexExpressions.getIndices(indexExpressions);
if (indices.size() == 1) {
Expression index = indices.get(0);
Context intensionalSetContext = context.extendWith(indexExpressions);
// Ensure condition of correct type is created
Type indexType = GrinderUtil.getType(index, intensionalSetContext);
SingleVariableConstraint singleVariableConstraint = null;
if (indexType instanceof RealExpressoType || indexType instanceof RealInterval) {
singleVariableConstraint = new SingleVariableLinearRealArithmeticConstraint(index, true, intensionalSetContext.getTheory());
} else if (indexType instanceof IntegerExpressoType || indexType instanceof IntegerInterval) {
singleVariableConstraint = new SingleVariableDifferenceArithmeticConstraint(index, true, intensionalSetContext.getTheory());
}
if (singleVariableConstraint != null) {
singleVariableConstraint = singleVariableConstraint.conjoin(intensionalSet.getCondition(), intensionalSetContext);
intensionalSet = intensionalSet.setCondition(singleVariableConstraint);
expression = expression.set(0, intensionalSet);
}
}
}
Expression result = interpreter.apply(expression, context);
System.out.println("Evaluation with " + sampleSizeN + " samples of " + expressionString + " = " + result.doubleValue() + " (as rational=" + toString(result) + ")");
return result;
}
use of com.sri.ai.expresso.api.IntensionalSet 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;
}
Aggregations