use of com.sri.ai.expresso.api.QuantifiedExpression in project aic-expresso by aic-sri-international.
the class Theory method isVariable.
/**
* Indicates whether an expression is considered a variable in this theory,
* meaning that it is not a constants of any of the registered types in context
* (per {@link Context#getTypes()} and {@link Context#isUniquelyNamedConstant(Expression)}),
* it is not interpreted in propositional logic
* (per {@link FormulaUtil#isInterpretedInPropositionalLogicIncludingConditionals(Expression)}),
* and is not interpreted in this theory besides boolean connectives
* (per {@link #isInterpretedInThisTheoryBesidesBooleanConnectives(Expression, Context)}.
* @param expression
* @param context
* @return
*/
default boolean isVariable(Expression expression, Context context) {
Expression typeExpression;
Type type;
boolean result = !context.isUniquelyNamedConstant(expression) && expression.getSyntacticFormType().equals("Symbol") && !(expression instanceof QuantifiedExpression) && !isInterpretedInPropositionalLogicIncludingConditionals(expression) && !isInterpretedInThisTheoryBesidesBooleanConnectives(expression) && (typeExpression = GrinderUtil.getTypeExpressionOfExpression(expression, context)) != null && (type = context.getTypeFromTypeExpression(typeExpression)) != null && isSuitableFor(type) && !thereExists(context.getTypes(), t -> t.contains(expression));
return result;
}
use of com.sri.ai.expresso.api.QuantifiedExpression in project aic-expresso by aic-sri-international.
the class SetOfArgumentTuplesForFunctionOccurringInExpression method computeUnionArgs.
private static void computeUnionArgs(List<Expression> unionArgs, Predicate<Expression> isF, FunctionType fType, Predicate<Expression> isFApplication, Expression e) {
// if E does not contain ƒ, oc<sub>ƒ</sub>[E] is ∅
if (!containsF(e, isF)) {
unionArgs.add(Sets.EMPTY_SET);
} else // if E is ƒ(t) for t a tuple, oc<sub>ƒ</sub>[E] is {t}
if (isFApplication.apply(e)) {
Expression tupleT = Expressions.makeTuple(e.getArguments());
Expression setT = ExtensionalSets.makeUniSet(tupleT);
unionArgs.add(setT);
} else // if E is ƒ, oc<sub>ƒ</sub>[E] is Α
if (isF.apply(e)) {
List<Expression> domainTypes = new ArrayList<>();
for (Type domainType : fType.getArgumentTypes()) {
domainTypes.add(Expressions.parse(domainType.getName()));
}
Expression tupleT = Expressions.makeTuple(domainTypes);
Expression setT = ExtensionalSets.makeUniSet(tupleT);
unionArgs.add(setT);
} else // if E is g(E′) for g a function symbol distinct from ƒ and t a k-tuple of expressions
if (Expressions.isFunctionApplicationWithArguments(e)) {
// if g(E′) is if C the E<sub>1</sub> else E<sub>2</sub> and C does not contain ƒ
if (IfThenElse.isIfThenElse(e) && !containsF(IfThenElse.condition(e), isF)) {
// then oc<sub>ƒ</sub>[E] is<br>
// if C then oc<sub>ƒ</sub>[E<sub>1</sub>] else oc<sub>ƒ</sub>[E<sub>2</sub>]
Expression e1 = IfThenElse.thenBranch(e);
List<Expression> thenUnionArgs = new ArrayList<>();
computeUnionArgs(thenUnionArgs, isF, fType, isFApplication, e1);
Expression thenBranch = makeUnion(thenUnionArgs);
Expression e2 = IfThenElse.elseBranch(e);
List<Expression> elseUnionArgs = new ArrayList<>();
computeUnionArgs(elseUnionArgs, isF, fType, isFApplication, e2);
Expression elseBranch = makeUnion(elseUnionArgs);
Expression ifThenElse = IfThenElse.make(IfThenElse.condition(e), thenBranch, elseBranch);
unionArgs.add(ifThenElse);
} else {
// oc<sub>ƒ</sub>[t<sub>1</sub>] ∪ … ∪ oc<sub>ƒ</sub>[t<sub>k</sub>]
for (Expression t : e.getArguments()) {
computeUnionArgs(unionArgs, isF, fType, isFApplication, t);
}
}
} else // quantified expression,
if (isArbitraryQuantifier(e) && !containsF(getQuantifierCondition(e), isF)) {
// then oc<sub>ƒ</sub>[E] is<br>
// ⋃<sub>x:C</sub>oc<sub>ƒ</sub>[E′]
QuantifiedExpression q = (QuantifiedExpression) e;
IndexExpressionsSet x = q.getIndexExpressions();
Expression c = getQuantifierCondition(e);
Expression ePrime = getQuantifiedExpression(q);
List<Expression> ePrimeUnionArgs = new ArrayList<>();
computeUnionArgs(ePrimeUnionArgs, isF, fType, isFApplication, ePrime);
Expression ocfOfEPrime = makeUnion(ePrimeUnionArgs);
Expression intensionalMultiSet = IntensionalSet.intensionalMultiSet(x, ocfOfEPrime, c);
Expression intensionalUnion = Expressions.apply(FunctorConstants.INTENSIONAL_UNION, intensionalMultiSet);
unionArgs.add(intensionalUnion);
} else // quantified expression,
if (isArbitraryQuantifier(e)) {
// then oc<sub>ƒ</sub>[E] is<br>
// ⋃<sub>x</sub>(oc<sub>ƒ</sub>[C] ∪ oc<sub>ƒ</sub>[E′])
QuantifiedExpression q = (QuantifiedExpression) e;
IndexExpressionsSet x = q.getIndexExpressions();
Expression c = getQuantifierCondition(e);
Expression ePrime = getQuantifiedExpression(q);
List<Expression> conditionUnionArgs = new ArrayList<>();
computeUnionArgs(conditionUnionArgs, isF, fType, isFApplication, c);
List<Expression> ePrimeUnionArgs = new ArrayList<>();
computeUnionArgs(ePrimeUnionArgs, isF, fType, isFApplication, ePrime);
List<Expression> combinedUnionArgs = new ArrayList<>();
combinedUnionArgs.addAll(conditionUnionArgs);
combinedUnionArgs.addAll(ePrimeUnionArgs);
Expression combinedUnion = makeUnion(combinedUnionArgs);
Expression intensionalMultiSet = IntensionalSet.intensionalMultiSet(x, combinedUnion, Expressions.TRUE);
Expression intensionalUnion = Expressions.apply(FunctorConstants.INTENSIONAL_UNION, intensionalMultiSet);
unionArgs.add(intensionalUnion);
} else {
throw new UnsupportedOperationException("Do not have logic for handling expression of the form: " + e);
}
}
use of com.sri.ai.expresso.api.QuantifiedExpression in project aic-praise by aic-sri-international.
the class HOGMModelValidator method getQuantifiedExpressionScope.
public Map<Expression, HOGMConstantDeclaration> getQuantifiedExpressionScope(Expression quantifiedExpression) {
Map<Expression, HOGMConstantDeclaration> result = new LinkedHashMap<>();
IndexExpressionsSet indexExpressionSet = ((QuantifiedExpression) quantifiedExpression).getIndexExpressions();
IndexExpressions.getIndexToTypeMapWithDefaultNull(indexExpressionSet).forEach((name, type) -> {
HOGMSortDeclaration localSort = getSort(type);
if (localSort != null) {
result.put(name, new HOGMConstantDeclaration(name, Expressions.ZERO, localSort.getName()));
}
});
return result;
}
use of com.sri.ai.expresso.api.QuantifiedExpression in project aic-praise by aic-sri-international.
the class HOGMModelValidator method updateQuantifiers.
Expression updateQuantifiers(Expression expr, Map<Expression, HOGMConstantDeclaration> currentScope) {
Expression result = expr;
if (isQuantifiedExpression(expr)) {
Expression bodyExpression = getQuantifiedExpressionBody(expr);
if (bodyExpression != null) {
Map<Expression, HOGMConstantDeclaration> quantifierScope = new LinkedHashMap<>(currentScope);
quantifierScope.putAll(getQuantifiedExpressionScope(expr));
Expression updatedBodyExpression = updateQuantifiers(bodyExpression, quantifierScope);
HOGMModelValidator.TermCategoryType updatedBodyTermCategoryType = determineTermCategoryType(updatedBodyExpression);
if (updatedBodyTermCategoryType == TermCategoryType.NUMERIC) {
Expression intensionalMultiSet = IntensionalSet.intensionalMultiSet(((QuantifiedExpression) expr).getIndexExpressions(), updatedBodyExpression, Expressions.TRUE);
result = Expressions.apply(FunctorConstants.PRODUCT, intensionalMultiSet);
} else if (bodyExpression != updatedBodyExpression) {
if (ForAll.isForAll(expr)) {
result = ForAll.make(ForAll.getIndexExpression(expr), updatedBodyExpression);
} else {
// Otherwise is existential
result = ThereExists.make(ThereExists.getIndexExpression(expr), updatedBodyExpression);
}
}
}
} else if (Expressions.isFunctionApplicationWithArguments(expr)) {
List<Expression> updatedArgs = expr.getArguments().stream().map(arg -> updateQuantifiers(arg, currentScope)).collect(Collectors.toList());
if (!sameInstancesInSameIterableOrder(expr.getArguments(), updatedArgs)) {
result = Expressions.apply(expr.getFunctor(), updatedArgs);
}
HOGMModelValidator.TermCategoryType resultTermCategoryType = determineTermCategoryType(result);
if (resultTermCategoryType == TermCategoryType.INVALID) {
if (IfThenElse.isIfThenElse(result)) {
Expression asRule = attemptMakeRule(result);
if (asRule != null) {
result = asRule;
}
}
}
}
return result;
}
use of com.sri.ai.expresso.api.QuantifiedExpression in project aic-praise by aic-sri-international.
the class HOGMModelValidator method checkQuantifiedExpressionWithScope.
private void checkQuantifiedExpressionWithScope(StatementInfo termStatement, Pair<Expression, Map<Expression, HOGMConstantDeclaration>> quantifiedExpressionWithScope) {
QuantifiedExpression quantifiedExpression = (QuantifiedExpression) quantifiedExpressionWithScope.first;
Map<Expression, HOGMConstantDeclaration> scopedConstants = quantifiedExpressionWithScope.second;
checkIfIndexSortsAreDefined(termStatement, quantifiedExpression);
checkIfBodyIsBoolean(termStatement, quantifiedExpression, scopedConstants);
}
Aggregations