Search in sources :

Example 6 with HOGMConstantDeclaration

use of com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMConstantDeclaration in project aic-praise by aic-sri-international.

the class HOGMModelValidator method validateFunctorsAndArguments.

void validateFunctorsAndArguments(StatementInfo termStatement) {
    // Ensure constant functions have the correct arity and their arguments are of the correct type
    List<Pair<Expression, Map<Expression, HOGMConstantDeclaration>>> constantFunctionsWithScope = getConstantFunctionsWithScope(termStatement.statement);
    constantFunctionsWithScope.forEach(constantFunctionAndScope -> {
        Expression constantFunction = constantFunctionAndScope.first;
        Map<Expression, HOGMConstantDeclaration> scopedConstants = constantFunctionAndScope.second;
        if (!isValidDeclaredConstantFunctorArity(constantFunction, scopedConstants)) {
            newError(Type.TERM_ARITY_OF_FUNCTOR_DOES_NOT_MATCH_DECLARATION, constantFunction, termStatement);
        } else {
            HOGMConstantDeclaration constantDeclaration = scopedConstants.get(constantFunction.getFunctorOrSymbol());
            for (int i = 0; i < constantFunction.numberOfArguments(); i++) {
                Expression arg = constantFunction.get(i);
                if (isUnknownConstant(arg, scopedConstants)) {
                    newError(Type.TERM_CONSTANT_NOT_DEFINED, arg, termStatement);
                } else {
                    if (getSort(constantDeclaration.getParameterSorts().get(i)) != determineSortType(arg, scopedConstants)) {
                        newError(Type.CONSTANT_ARGUMENT_IS_OF_THE_INCORRECT_TYPE, arg, termStatement);
                    }
                }
            }
        }
    });
    // Ensure random functions have the correct arity and their arguments are of the correct type
    List<Pair<Expression, Map<Expression, HOGMConstantDeclaration>>> randomFunctionsWithScope = getRandomFunctionsWithScope(termStatement.statement);
    randomFunctionsWithScope.forEach(randomFunctionAndScope -> {
        Expression randomFunction = randomFunctionAndScope.first;
        Map<Expression, HOGMConstantDeclaration> scopedConstants = randomFunctionAndScope.second;
        if (!isValidDeclaredRandomFunctorArity(randomFunction)) {
            newError(Type.TERM_ARITY_OF_FUNCTOR_DOES_NOT_MATCH_DECLARATION, randomFunction, termStatement);
        } else {
            HOGMRandomVariableDeclaration rvDeclaration = randoms.get(randomFunction.getFunctorOrSymbol());
            for (int i = 0; i < randomFunction.numberOfArguments(); i++) {
                Expression arg = randomFunction.get(i);
                if (isUnknownConstant(arg, scopedConstants)) {
                    newError(Type.TERM_CONSTANT_NOT_DEFINED, arg, termStatement);
                } else {
                    if (getSort(rvDeclaration.getParameterSorts().get(i)) != determineSortType(arg, scopedConstants)) {
                        newError(Type.RANDOM_VARIABLE_ARGUMENT_IS_OF_THE_INCORRECT_TYPE, arg, termStatement);
                    }
                }
            }
        }
    });
    // All of these should belong to the known set of functors
    List<Pair<Expression, Map<Expression, HOGMConstantDeclaration>>> nonConstantAndRandomFunctionsWithScope = getNonConstantRandomFunctionsWithScope(termStatement.statement);
    nonConstantAndRandomFunctionsWithScope.forEach(x -> checkNonConstantAndRandomFunctionWithScope(x, termStatement));
    checkQuantifiedExpressions(termStatement);
}
Also used : HOGMRandomVariableDeclaration(com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMRandomVariableDeclaration) Expression(com.sri.ai.expresso.api.Expression) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression) HOGMConstantDeclaration(com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMConstantDeclaration) Pair(com.sri.ai.util.base.Pair)

Example 7 with HOGMConstantDeclaration

use of com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMConstantDeclaration 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;
}
Also used : Expression(com.sri.ai.expresso.api.Expression) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression) HOGMConstantDeclaration(com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMConstantDeclaration) ArrayList(java.util.ArrayList) List(java.util.List) LinkedHashMap(java.util.LinkedHashMap)

Example 8 with HOGMConstantDeclaration

use of com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMConstantDeclaration in project aic-praise by aic-sri-international.

the class HOGMModelValidator method getQuantifiers.

void getQuantifiers(Expression expr, List<Pair<Expression, Map<Expression, HOGMConstantDeclaration>>> quantifiersWithScope, Map<Expression, HOGMConstantDeclaration> currentScope) {
    if (isQuantifiedExpression(expr)) {
        Map<Expression, HOGMConstantDeclaration> quantifierScope = new LinkedHashMap<>(currentScope);
        quantifierScope.putAll(getQuantifiedExpressionScope(expr));
        quantifiersWithScope.add(new Pair<>(expr, quantifierScope));
        Expression bodyExpression = getQuantifiedExpressionBody(expr);
        if (bodyExpression != null) {
            getQuantifiers(bodyExpression, quantifiersWithScope, quantifierScope);
        }
    }
    if (Expressions.isFunctionApplicationWithArguments(expr)) {
        expr.getArguments().forEach(arg -> getQuantifiers(arg, quantifiersWithScope, currentScope));
    }
}
Also used : Expression(com.sri.ai.expresso.api.Expression) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression) HOGMConstantDeclaration(com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMConstantDeclaration) LinkedHashMap(java.util.LinkedHashMap)

Example 9 with HOGMConstantDeclaration

use of com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMConstantDeclaration 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);
}
Also used : QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression) Expression(com.sri.ai.expresso.api.Expression) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression) HOGMConstantDeclaration(com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMConstantDeclaration)

Aggregations

HOGMConstantDeclaration (com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMConstantDeclaration)9 Expression (com.sri.ai.expresso.api.Expression)8 QuantifiedExpression (com.sri.ai.expresso.api.QuantifiedExpression)7 LinkedHashMap (java.util.LinkedHashMap)4 HOGMRandomVariableDeclaration (com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMRandomVariableDeclaration)2 HOGMSortDeclaration (com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMSortDeclaration)2 ArrayList (java.util.ArrayList)2 IndexExpressionsSet (com.sri.ai.expresso.api.IndexExpressionsSet)1 Pair (com.sri.ai.util.base.Pair)1 List (java.util.List)1 Map (java.util.Map)1