Skip to content

Commit 7f77ab6

Browse files
committed
Defined repr for all formula objects
1 parent 1b17f0c commit 7f77ab6

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/logicalpy/base.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ def __eq__(self, other):
2121
return self.name == other.name
2222
return False
2323

24+
def __repr__(self) -> str:
25+
return f"Proposition('{self.name}')"
26+
2427
def as_latex(self) -> str:
2528
return self.name
2629

@@ -77,6 +80,9 @@ def __eq__(self, other) -> bool:
7780
return self.a == other.a
7881
return False
7982

83+
def __repr__(self) -> str:
84+
return f"Not({repr(self.a)})"
85+
8086
def as_latex(self) -> str:
8187
return r"\neg " + self.a.as_latex()
8288

@@ -136,6 +142,9 @@ def __eq__(self, other) -> bool:
136142
return self.a == other.a and self.b == other.b
137143
return False
138144

145+
def __repr__(self) -> str:
146+
return type(self).__name__ + f"({repr(self.a)}, {repr(self.b)})"
147+
139148
def as_latex(self) -> str:
140149
return f"({self.a.as_latex()} {self.LATEX_SYMBOL} {self.b.as_latex()})"
141150

@@ -331,6 +340,9 @@ def __eq__(self, other) -> bool:
331340
return self.formula == other.formula
332341
return False
333342

343+
def __repr__(self) -> str:
344+
return f"Formula({repr(self.formula)})"
345+
334346
def __hash__(self) -> int:
335347
return hash(str(self))
336348

tests/test_base.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ def test_str(self):
2323
Q = Proposition("Q")
2424
self.assertEqual(str(Formula(Implies(And(Or(P, Q), Not(Q)), P))), "((P ∨ Q) ∧ ¬Q) → P")
2525

26+
def test_repr(self):
27+
P = Proposition("P")
28+
self.assertEqual(repr(Formula(Or(And(P, P), P))), "Formula(Or(And(Proposition('P'), Proposition('P')), Proposition('P')))")
29+
2630
def test_propositions(self):
2731
P = Proposition("P")
2832
Q = Proposition("Q")

0 commit comments

Comments
 (0)