|
| 1 | +from itertools import product |
| 2 | +from evaluation_function.propositional_logic import ( |
| 3 | + Atom, |
| 4 | + Negation, |
| 5 | + Conjunction, |
| 6 | + Disjunction, |
| 7 | + Implication, |
| 8 | + Biconditional, |
| 9 | + Assignment, |
| 10 | + FormulaEvaluator, |
| 11 | +) |
| 12 | + |
| 13 | + |
| 14 | +def get_atoms(formula): |
| 15 | + atoms = set() |
| 16 | + |
| 17 | + if isinstance(formula, Atom): |
| 18 | + atoms.add(formula) |
| 19 | + elif hasattr(formula, "operand"): |
| 20 | + atoms.update(get_atoms(formula.operand)) |
| 21 | + elif hasattr(formula, "left") and hasattr(formula, "right"): |
| 22 | + atoms.update(get_atoms(formula.left)) |
| 23 | + atoms.update(get_atoms(formula.right)) |
| 24 | + elif hasattr(formula, "formula"): |
| 25 | + atoms.update(get_atoms(formula.formula)) |
| 26 | + |
| 27 | + return atoms |
| 28 | + |
| 29 | + |
| 30 | +def are_equivalent(formula1, formula2): |
| 31 | + atoms1 = get_atoms(formula1) |
| 32 | + atoms2 = get_atoms(formula2) |
| 33 | + all_atoms = list(atoms1 | atoms2) |
| 34 | + |
| 35 | + for assignment_values in product([False, True], repeat=len(all_atoms)): |
| 36 | + assignment_dict = {atom: val for atom, val in zip(all_atoms, assignment_values)} |
| 37 | + assignment = Assignment(assignment_dict) |
| 38 | + |
| 39 | + evaluator1 = FormulaEvaluator(formula1, assignment) |
| 40 | + evaluator2 = FormulaEvaluator(formula2, assignment) |
| 41 | + |
| 42 | + result1 = evaluator1.evaluate() |
| 43 | + result2 = evaluator2.evaluate() |
| 44 | + |
| 45 | + if result1 != result2: |
| 46 | + return False |
| 47 | + |
| 48 | + return True |
| 49 | + |
| 50 | + |
| 51 | +def main(): |
| 52 | + p = Atom("p") |
| 53 | + q = Atom("q") |
| 54 | + |
| 55 | + print("=== Formula Equivalence Checking ===") |
| 56 | + print() |
| 57 | + |
| 58 | + equivalence_tests = [ |
| 59 | + ( |
| 60 | + "p → q", |
| 61 | + "¬p ∨ q", |
| 62 | + Implication(p, q), |
| 63 | + Disjunction(Negation(p), q) |
| 64 | + ), |
| 65 | + ( |
| 66 | + "p ↔ q", |
| 67 | + "(p → q) ∧ (q → p)", |
| 68 | + Biconditional(p, q), |
| 69 | + Conjunction(Implication(p, q), Implication(q, p)) |
| 70 | + ), |
| 71 | + ( |
| 72 | + "¬(p ∧ q)", |
| 73 | + "¬p ∨ ¬q", |
| 74 | + Negation(Conjunction(p, q)), |
| 75 | + Disjunction(Negation(p), Negation(q)) |
| 76 | + ), |
| 77 | + ( |
| 78 | + "¬(p ∨ q)", |
| 79 | + "¬p ∧ ¬q", |
| 80 | + Negation(Disjunction(p, q)), |
| 81 | + Conjunction(Negation(p), Negation(q)) |
| 82 | + ), |
| 83 | + ( |
| 84 | + "p ∧ q", |
| 85 | + "q ∧ p", |
| 86 | + Conjunction(p, q), |
| 87 | + Conjunction(q, p) |
| 88 | + ), |
| 89 | + ] |
| 90 | + |
| 91 | + for name1, name2, formula1, formula2 in equivalence_tests: |
| 92 | + equivalent = are_equivalent(formula1, formula2) |
| 93 | + status = "✓ EQUIVALENT" if equivalent else "✗ NOT EQUIVALENT" |
| 94 | + print(f"{name1:20} ≡ {name2:25} {status}") |
| 95 | + print(f" Formula 1: {formula1}") |
| 96 | + print(f" Formula 2: {formula2}") |
| 97 | + print() |
| 98 | + |
| 99 | + |
| 100 | +if __name__ == "__main__": |
| 101 | + main() |
0 commit comments