|
| 1 | +from mathesis.forms import ( |
| 2 | + Formula, |
| 3 | + Atom, |
| 4 | + Top, |
| 5 | + Bottom, |
| 6 | + Negation, |
| 7 | + Disjunction, |
| 8 | + Conjunction, |
| 9 | + Conditional, |
| 10 | + Universal, |
| 11 | + Particular |
| 12 | +) |
| 13 | + |
| 14 | + |
| 15 | +# ⊤ ∨ (¬Q ∨ R) |
| 16 | +propositional_formula_1 = Disjunction( |
| 17 | + Top(), |
| 18 | + Disjunction( |
| 19 | + Negation(Atom("Q")), |
| 20 | + Atom("R") |
| 21 | + ) |
| 22 | +) |
| 23 | + |
| 24 | +# (P ∧ ¬P) → ⊥ |
| 25 | +propositional_formula_2 = Conditional( |
| 26 | + Conjunction( |
| 27 | + Atom("P"), |
| 28 | + Negation(Atom("P")) |
| 29 | + ), |
| 30 | + Bottom() |
| 31 | +) |
| 32 | + |
| 33 | +# ∀x(P(x) → (Q(x) ∨ R(x))) |
| 34 | +quantified_formula_1 = Universal( |
| 35 | + "x", |
| 36 | + Conditional( |
| 37 | + Atom("P", terms=["x"]), |
| 38 | + Disjunction( |
| 39 | + Atom("Q", terms=["x"]), |
| 40 | + Atom("R", terms=["x"]) |
| 41 | + ) |
| 42 | + ) |
| 43 | +) |
| 44 | + |
| 45 | +# ∃y(¬Q(y) ∧ ¬R(a, b, y)) |
| 46 | +quantified_formula_2 = Particular( |
| 47 | + "y", |
| 48 | + Conjunction( |
| 49 | + Negation(Atom("Q", terms=["y"])), |
| 50 | + Negation(Atom("R", terms=["a", "b", "y"])) |
| 51 | + ) |
| 52 | +) |
| 53 | + |
| 54 | + |
| 55 | +def test_repr(): |
| 56 | + assert repr(propositional_formula_1) == "Disj[Top, Disj[Neg[Atom[Q]], Atom[R]]]" |
| 57 | + assert repr(propositional_formula_2) == "Cond[Conj[Atom[P], Neg[Atom[P]]], Bottom]" |
| 58 | + assert repr(quantified_formula_1) == "Forall<x>[Cond[Atom[P(x)], Disj[Atom[Q(x)], Atom[R(x)]]]]" |
| 59 | + assert repr(quantified_formula_2) == "Exists<y>[Conj[Neg[Atom[Q(y)]], Neg[Atom[R(a, b, y)]]]]" |
| 60 | + |
| 61 | + |
| 62 | +def test_str(): |
| 63 | + assert str(propositional_formula_1) == "⊤ ∨ (¬Q ∨ R)" |
| 64 | + assert str(propositional_formula_2) == "(P ∧ ¬P) → ⊥" |
| 65 | + assert str(quantified_formula_1) == "∀x(P(x) → (Q(x) ∨ R(x)))" |
| 66 | + assert str(quantified_formula_2) == "∃y(¬Q(y) ∧ ¬R(a, b, y))" |
| 67 | + |
| 68 | + |
| 69 | +def test_latex(): |
| 70 | + assert propositional_formula_1.latex() == r"\top \lor (\neg Q \lor R)" |
| 71 | + assert propositional_formula_2.latex() == r"(P \land \neg P) \to \bot" |
| 72 | + assert quantified_formula_1.latex() == r"\forall x (P(x) \to (Q(x) \lor R(x)))" |
| 73 | + assert quantified_formula_2.latex() == r"\exists y (\neg Q(y) \land \neg R(a, b, y))" |
| 74 | + |
| 75 | + |
| 76 | +def test_atoms(): |
| 77 | + assert propositional_formula_1.atoms == { |
| 78 | + "Q": [Atom("Q")], |
| 79 | + "R": [Atom("R")] |
| 80 | + } |
| 81 | + assert propositional_formula_2.atoms == { |
| 82 | + "P": [Atom("P"), Atom("P")] |
| 83 | + } |
| 84 | + assert quantified_formula_1.atoms == { |
| 85 | + "P(x)": [Atom("P", terms=["x"])], |
| 86 | + "Q(x)": [Atom("Q", terms=["x"])], |
| 87 | + "R(x)": [Atom("R", terms=["x"])] |
| 88 | + } |
| 89 | + assert quantified_formula_2.atoms == { |
| 90 | + "Q(y)": [Atom("Q", terms=["y"])], |
| 91 | + "R(a, b, y)": [Atom("R", terms=["a", "b", "y"])] |
| 92 | + } |
| 93 | + |
| 94 | + |
| 95 | +def test_atom_symbols(): |
| 96 | + assert propositional_formula_1.atom_symbols == ["Q", "R"] |
| 97 | + assert propositional_formula_2.atom_symbols == ["P"] |
| 98 | + assert quantified_formula_1.atom_symbols == ["P(x)", "Q(x)", "R(x)"] |
| 99 | + assert quantified_formula_2.atom_symbols == ["Q(y)", "R(a, b, y)"] |
| 100 | + |
| 101 | + |
| 102 | +def test_equality(): |
| 103 | + assert propositional_formula_1 == propositional_formula_1 |
| 104 | + assert propositional_formula_2 != propositional_formula_1 |
| 105 | + assert propositional_formula_2 != quantified_formula_1 |
| 106 | + assert quantified_formula_1 == quantified_formula_1 |
| 107 | + assert quantified_formula_2 == quantified_formula_2 |
| 108 | + assert quantified_formula_1 != quantified_formula_2 |
| 109 | + |
| 110 | + |
| 111 | +def test_free_terms(): |
| 112 | + assert propositional_formula_1.free_terms == [] |
| 113 | + assert propositional_formula_2.free_terms == [] |
| 114 | + assert quantified_formula_1.free_terms == [] |
| 115 | + assert quantified_formula_2.free_terms == ["a", "b"] |
| 116 | + |
| 117 | + |
| 118 | +def test_replace_term(): |
| 119 | + assert propositional_formula_1.replace_term("Q", "R") == propositional_formula_1 |
| 120 | + assert quantified_formula_1.replace_term("x", "y") == Conditional( |
| 121 | + Atom("P", terms=["y"]), |
| 122 | + Disjunction( |
| 123 | + Atom("Q", terms=["y"]), |
| 124 | + Atom("R", terms=["y"]) |
| 125 | + ) |
| 126 | + ) |
| 127 | + assert quantified_formula_2.replace_term("a", "b") == Conjunction( |
| 128 | + Negation(Atom("Q", terms=["y"])), |
| 129 | + Negation(Atom("R", terms=["b", "b", "y"])) |
| 130 | + ) |
0 commit comments