-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_logic.py
204 lines (162 loc) · 6.42 KB
/
parse_logic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
from types import UnionType
from typing import Any, Self
class OpBase:
def __and__(self, other:Self):
assert isinstance(other, OpBase)
return And(self, other)
def __or__(self, other: Self):
assert isinstance(other, OpBase)
return Or(self, other)
def __invert__(self):
return Not(self)
def __rshift__(self, other: Self):
assert isinstance(other, OpBase)
return Implies(self, other)
class Variable(OpBase):
def __init__(self, name:str) -> None:
self._name = name
def __repr__(self) -> str:
return self._name
def str_latex(self):
return self._name
def evaluate(self, variables_values):
return variables_values[self]
# for key, value in variables_values.items():
# if self._name == key._name:
# return value
class Op(OpBase): pass
class Not(Op):
def __init__(self, a) -> None:
self.a = a
def __repr__(self) -> str:
return f"(¬{self.a})"
def evaluate(self, variables_values):
inside = self.a.evaluate(variables_values)
if inside is Logic_True():
return Logic_False()
elif inside is Logic_False():
return Logic_True()
else:
return Logic_Unknown()
def str_latex(self):
return r"(\lnot " + self.a.str_latex() + ")"
class DoubleOp(Op):
def __init__(self, a, b) -> None:
self.a = a
self.b = b
class Or(DoubleOp):
def __init__(self, a, b) -> None:
super().__init__(a, b)
def __repr__(self) -> str:
return f"({self.a} ∨ {self.b})"
def str_latex(self):
return "(" + self.a.str_latex() + r" \vee " + self.b.str_latex() + ")"
def evaluate(self, variables_values):
inside_a = self.a.evaluate(variables_values)
inside_b = self.b.evaluate(variables_values)
match inside_a, inside_b:
case Logic_True(), Logic_True(): return Logic_True()
case Logic_True(), Logic_False(): return Logic_True()
case Logic_True(), Logic_Unknown(): return Logic_True()
case Logic_False(), Logic_True(): return Logic_True()
case Logic_False(), Logic_False(): return Logic_False()
case Logic_False(), Logic_Unknown(): return Logic_Unknown()
case Logic_Unknown(), Logic_True(): return Logic_True()
case Logic_Unknown(), Logic_False(): return Logic_Unknown()
case Logic_Unknown(), Logic_Unknown(): return Logic_Unknown()
class And(DoubleOp):
def __init__(self, a, b) -> None:
super().__init__(a, b)
def __repr__(self) -> str:
return f"({self.a} ∧ {self.b})"
def evaluate(self, variables_values):
inside_a = self.a.evaluate(variables_values)
inside_b = self.b.evaluate(variables_values)
match inside_a, inside_b:
case Logic_True(), Logic_True(): return Logic_True()
case Logic_True(), Logic_False(): return Logic_False()
case Logic_True(), Logic_Unknown(): return Logic_Unknown()
case Logic_False(), Logic_True(): return Logic_False()
case Logic_False(), Logic_False(): return Logic_False()
case Logic_False(), Logic_Unknown(): return Logic_False()
case Logic_Unknown(), Logic_True(): return Logic_Unknown()
case Logic_Unknown(), Logic_False(): return Logic_False()
case Logic_Unknown(), Logic_Unknown(): return Logic_Unknown()
def str_latex(self):
return "(" + self.a.str_latex() + r" \wedge " + self.b.str_latex() + ")"
class Implies(DoubleOp):
def __init__(self, a, b) -> None:
super().__init__(a, b)
def __repr__(self) -> str:
return f"({self.a} ⇒ {self.b})"
def evaluate(self, variables_values):
inside_a = self.a.evaluate(variables_values)
inside_b = self.b.evaluate(variables_values)
match inside_a, inside_b:
case Logic_True(), Logic_True(): return Logic_True()
case Logic_True(), Logic_False(): return Logic_False()
case Logic_True(), Logic_Unknown(): return Logic_Unknown()
case Logic_False(), Logic_True(): return Logic_True()
case Logic_False(), Logic_False(): return Logic_True()
case Logic_False(), Logic_Unknown(): return Logic_True()
case Logic_Unknown(), Logic_True(): return Logic_True()
case Logic_Unknown(), Logic_False(): return Logic_Unknown()
case Logic_Unknown(), Logic_Unknown(): return Logic_True()
def str_latex(self):
return "(" + self.a.str_latex() + r" \Rightarrow " + self.b.str_latex() + ")"
class Logic_Value: pass
class Logic_True(Logic_Value):
_elem = None
def __new__(cls) -> Self:
if cls._elem is not None:
return cls._elem
cls._elem = super().__new__(cls)
return cls._elem
def __repr__(self) -> str:
return "V"
# To sort the table
def __lt__(self, other):
assert isinstance(other, Logic_Value)
match other:
case Logic_True(): return True
case Logic_False(): return True
case Logic_Unknown(): return True
class Logic_False(Logic_Value):
_elem = None
def __new__(cls) -> Self:
if cls._elem is not None:
return cls._elem
cls._elem = super().__new__(cls)
return cls._elem
def __repr__(self) -> str:
return "F"
# To sort the table
def __lt__(self, other):
assert isinstance(other, Logic_Value)
match other:
case Logic_True(): return False
case Logic_False(): return True
case Logic_Unknown(): return True
class Logic_Unknown(Logic_Value):
_elem = None
def __new__(cls) -> Self:
if cls._elem is not None:
return cls._elem
cls._elem = super().__new__(cls)
return cls._elem
def __repr__(self) -> str:
return "I"
# To sort the table
def __lt__(self, other):
assert isinstance(other, Logic_Value)
match other:
case Logic_True(): return False
case Logic_False(): return False
case Logic_Unknown(): return True
L2 = {Logic_True(), Logic_False()}
L3 = {Logic_True(), Logic_False(), Logic_Unknown()}
if __name__ == "__main__":
a = Variable("A")
b = Variable("B")
c = Variable("C")
print(~a & b & (c | a))