use of com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet in project aic-expresso by aic-sri-international.
the class IndexExpressions method makeExtensionalIndexExpressionsSetFromSymbolsAndTypesStrings.
public static ExtensionalIndexExpressionsSet makeExtensionalIndexExpressionsSetFromSymbolsAndTypesStrings(String... symbolsAndTypes) {
Expression[] symbolsAndTypesExpressions = GrinderUtil.makeListOfSymbolsAndTypesExpressionsFromSymbolsAndTypesStrings(symbolsAndTypes);
List<Expression> indexExpressions = GrinderUtil.makeIndexExpressionsFromSymbolsAndTypes(symbolsAndTypesExpressions);
ExtensionalIndexExpressionsSet result = new ExtensionalIndexExpressionsSet(indexExpressions);
return result;
}
use of com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet in project aic-expresso by aic-sri-international.
the class Measure method constructComponentIntensionalSet.
private static Expression constructComponentIntensionalSet(Type indexType, IntensionalSet intensionalSet, Expression additiveIdentityElement, Context intensionalSetContext) {
Expression conditionedBody = IfThenElse.make(intensionalSet.getCondition(), intensionalSet.getHead(), additiveIdentityElement);
Expression componentIndex = Expressions.makeUniqueVariable("C", conditionedBody, intensionalSetContext);
Expression indexExpression = IndexExpressions.makeIndexExpression(componentIndex, Expressions.parse(indexType.getName()));
Expression intensionalCondition = Expressions.TRUE;
// NOTE: handle the REAL cases where an SingleVariableLinearRealArithmeticConstraint is expected.
if (indexType instanceof RealExpressoType || indexType instanceof RealInterval) {
SingleVariableLinearRealArithmeticConstraint svlraConstraint = new SingleVariableLinearRealArithmeticConstraint(componentIndex, true, intensionalSetContext.getTheory());
intensionalCondition = svlraConstraint;
}
Expression result = IntensionalSet.make(Sets.isMultiSet(intensionalSet) ? IntensionalSet.MULTI_SET_LABEL : IntensionalSet.UNI_SET_LABEL, new ExtensionalIndexExpressionsSet(Arrays.asList(indexExpression)), componentIndex, intensionalCondition);
return result;
}
use of com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet 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.core.ExtensionalIndexExpressionsSet 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.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