-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathknowledge_base_test.py
263 lines (197 loc) · 7.9 KB
/
knowledge_base_test.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
from functools import reduce
from itertools import product
def a(sym):
return Atom(sym)
def dissociate(op, clause):
results = []
def collect(clause):
if isinstance(clause, op):
collect(clause.lchild)
collect(clause.rchild)
else:
results.append(clause)
if isinstance(clause, Atom):
return [clause]
if isinstance(clause, Not):
return [clause]
collect(clause)
return results
class Formula:
def __invert__(self):
return Not(self)
def __and__(self, other):
return And(self, other)
def __or__(self, other):
return Or(self, other)
def __rshift__(self, other):
return Implies(self, other)
def __lshift__(self, other):
return Iff(self, other)
def __eq__(self, other):
return self.__class__ == other.__class__ and self.eq(other)
def v(self, v):
raise NotImplementedError("Plain formula can not be valuated")
def _t(self, left, right):
while True:
found = True
for item in left:
if item in right:
return None
if not isinstance(item, Atom):
left.remove(item)
tup = item._tleft(left, right)
left, right = tup[0]
if len(tup) > 1:
v = self._t(*tup[1])
if v is not None:
return v
found = False
break
for item in right:
if item in left:
return None
if not isinstance(item, Atom):
right.remove(item)
tup = item._tright(left, right)
left, right = tup[0]
if len(tup) > 1:
v = self._t(*tup[1])
if v is not None:
return v
found = False
break
if found:
return set(left)
def t(self):
return self._t([], [self])
def to_cnf(self):
def eliminate_implications(clause):
if isinstance(clause, Atom):
return clause
args = None
if isinstance(clause, Not):
args = list(map(eliminate_implications, [clause.child]))
else:
args = list(map(eliminate_implications, [clause.lchild, clause.rchild]))
a, b = args[0], args[-1]
if isinstance(clause, Implies):
return b | ~a
if isinstance(clause, Iff):
return (a | ~b) & (b | ~a)
if isinstance(clause, And):
return a & b
if isinstance(clause, Or):
return a | b
if isinstance(clause, Not):
return ~a
def move_not_inwards(clause):
if isinstance(clause, Not):
def _not(c):
return move_not_inwards(~c)
if isinstance(clause.child, Not):
return move_not_inwards(clause.child.child) # ~~A => A
if isinstance(clause.child, And):
return _not(clause.child.lchild) | _not(clause.child.rchild)
if isinstance(clause.child, Or):
return _not(clause.child.lchild) & _not(clause.child.rchild)
return clause
if isinstance(clause, Atom):
return clause
if isinstance(clause, And):
return move_not_inwards(clause.lchild) & move_not_inwards(clause.rchild)
if isinstance(clause, Or):
return move_not_inwards(clause.lchild) | move_not_inwards(clause.rchild)
def distribute_and_over_or(clause):
if isinstance(clause, Atom) or isinstance(clause, Not):
return clause
if isinstance(clause, Or):
left_conj = dissociate(And, distribute_and_over_or(clause.lchild))
right_conj = dissociate(And, distribute_and_over_or(clause.rchild))
disjunctions = list(map(lambda el: el[0] | el[1], product(left_conj, right_conj, repeat=1)))
return reduce(lambda a, b: a & b, disjunctions)
if isinstance(clause, And):
return distribute_and_over_or(clause.lchild) & distribute_and_over_or(clause.rchild)
return clause
if isinstance(self, Atom):
return self
s = eliminate_implications(self)
s = move_not_inwards(s)
return distribute_and_over_or(s)
class BinOp(Formula):
def __init__(self, lchild, rchild):
self.lchild = lchild
self.rchild = rchild
def __str__(self):
return "(" + str(self.lchild) + " " + self.op + " " + str(self.rchild) + ")"
def __hash__(self):
return hash(self.lchild) ^ hash(self.rchild) ^ hash(self.op)
def eq(self, other):
return self.lchild == other.lchild and self.rchild == other.rchild
class And(BinOp):
op = "∧"
def v(self, v):
return self.lchild.v(v) and self.rchild.v(v)
def _tleft(self, left, right):
return ((left + [self.lchild, self.rchild], right),)
def _tright(self, left, right):
return (left, right + [self.lchild]), (left, right + [self.rchild])
class Or(BinOp):
op = "∨"
def v(self, v):
return self.lchild.v(v) or self.rchild.v(v)
def _tleft(self, left, right):
return (left + [self.lchild], right), (left + [self.rchild], right)
def _tright(self, left, right):
return ((left, right + [self.lchild, self.rchild]),)
class Implies(BinOp):
op = "→"
def v(self, v):
return not self.lchild.v(v) or self.rchild.v(v)
def _tleft(self, left, right):
return (left + [self.rchild], right), (left, right + [self.lchild])
def _tright(self, left, right):
return ((left + [self.lchild], right + [self.rchild]),)
class Iff(BinOp):
op = "↔"
def v(self, v):
return self.lchild.v(v) is self.rchild.v(v)
def _tleft(self, left, right):
return (left + [self.lchild, self.rchild], right), (left, right + [self.lchild, self.rchild])
def _tright(self, left, right):
return (left + [self.lchild], right + [self.rchild]), (left + [self.rchild], right + [self.lchild])
class Not(Formula):
def __init__(self, child):
self.child = child
def v(self, v):
return not self.child.v(v)
def __str__(self):
return "¬" + str(self.child)
def __hash__(self):
return hash(self.child) ^ hash("¬")
def eq(self, other):
return self.child == other.child
def _tleft(self, left, right):
return ((left, right + [self.child]),)
def _tright(self, left, right):
return ((left + [self.child], right),)
class Atom(Formula):
def __init__(self, name):
self.name = name
def __hash__(self):
return hash(self.name)
def v(self, v):
return self in v
def __str__(self):
return str(self.name)
__repr__ = __str__
def eq(self, other):
return self.name == other.name
# region [ To cnf tests ]
# def test_to_cnf():
# assert (~(a('B') | a('C'))).to_cnf().__str__() == '(¬B ∧ ¬C)'
# assert (a('P') >> (a('Q') & a('S'))).to_cnf().__str__() == '((Q ∨ ¬P) ∧ (S ∨ ¬P))'
# assert ((a('P') >> a('Q')) & a('S')).to_cnf().__str__() == '((Q ∨ ¬P) ∧ S)'
# assert ((a('P') & a('Q')) | (~a('P') & ~a('Q'))).to_cnf().__str__() == '((((P ∨ ¬P) ∧ (P ∨ ¬Q)) ∧ (Q ∨ ¬P)) ∧ (Q ∨ ¬Q))'
# assert (a('A') << a('B')).to_cnf().__str__() == '((A ∨ ¬B) ∧ (B ∨ ¬A))'
# assert (a('A') << ~a('B') >> (a('C') | ~a('D'))).to_cnf().__str__() == '(((((C ∨ ¬D) ∨ (¬A ∨ B)) ∧ ((C ∨ ¬D) ∨ (¬A ∨ A))) ∧ ((C ∨ ¬D) ∨ (¬B ∨ B))) ∧ ((C ∨ ¬D) ∨ (¬B ∨ A)))'
# endregion