"""Expression evaluator module.This module provides utilities for evaluating logical expressions with different truth assignments."""fromtypingimportDictfromagent_logic.core.baseimportLogicalExpressionfromagent_logic.core.functionsimportFunction,Relationfromagent_logic.core.operationsimportBinaryOp,Not,Propositionfromagent_logic.core.quantifiersimportExistentialQuantifier,UniversalQuantifier
[docs]classEvaluator:""" Evaluates logical expressions with different truth assignments. This class provides methods to evaluate expressions and determine logical properties such as consistency, validity, and equivalence. """
[docs]@staticmethoddefevaluate_with_assignment(expression:LogicalExpression,assignment:Dict[str,bool])->bool:""" Evaluates a logical expression under a specific truth assignment. Args: expression: The logical expression to evaluate assignment: Dictionary mapping variable names to boolean values Returns: Boolean result of evaluating the expression """returnexpression.evaluate(assignment)
[docs]@staticmethoddefare_equivalent(expr1:LogicalExpression,expr2:LogicalExpression)->bool:""" Determines if two expressions are logically equivalent. Expressions are equivalent if they have the same truth value for all possible truth assignments to their variables. Args: expr1: First logical expression expr2: Second logical expression Returns: True if the expressions are equivalent, False otherwise """# Get all variables from both expressionsvariables=set(expr1.variables()+expr2.variables())# Generate all possible truth assignmentsfromagent_logic.evaluation.truth_tableimportTruthTableassignments=TruthTable._generate_assignments(list(variables))# Check if the expressions have the same value for all assignmentsforassignmentinassignments:ifexpr1.evaluate(assignment)!=expr2.evaluate(assignment):returnFalsereturnTrue
[docs]@staticmethoddefis_satisfiable(expression:LogicalExpression)->bool:""" Determines if an expression is satisfiable (true for at least one assignment). Args: expression: The logical expression to check Returns: True if the expression is satisfiable, False otherwise """fromagent_logic.evaluation.truth_tableimportTruthTablereturnTruthTable(expression).is_satisfiable()
[docs]@staticmethoddefis_valid(expression:LogicalExpression)->bool:""" Determines if an expression is valid (true for all assignments). Args: expression: The logical expression to check Returns: True if the expression is valid, False otherwise """fromagent_logic.evaluation.truth_tableimportTruthTablereturnTruthTable(expression).is_tautology()
[docs]@staticmethoddefevaluate(expression:LogicalExpression,context:Dict[str,bool])->bool:"""Evaluates a logical expression under a given context of truth values."""ifisinstance(expression,Proposition):returnexpression.evaluate(context)elifisinstance(expression,Not):returnexpression.evaluate(context)elifisinstance(expression,BinaryOp):returnexpression.evaluate(context)elifisinstance(expression,Function):returnexpression.evaluate(context)elifisinstance(expression,Relation):returnexpression.evaluate(context)elifisinstance(expression,UniversalQuantifier):returnexpression.evaluate(context)elifisinstance(expression,ExistentialQuantifier):returnexpression.evaluate(context)raiseValueError(f"Unknown expression type: {expression}")
[docs]@staticmethoddefexpression_depth(expression:LogicalExpression)->int:"""Computes the depth of a logical expression."""returnexpression.depth()