Repository navigation
Expand file tree
/
Copy pathcnf_converter.py
More file actions
390 lines (316 loc) · 11 KB
/
Copy pathcnf_converter.py
File metadata and controls
390 lines (316 loc) · 11 KB
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
"""
CNF Converter module for propositional logic resolution prover.
Converts propositional logic formulas to Conjunctive Normal Form (CNF).
* The following code is adapted from propositional-logic-theorem-prover-using-resolution-refutation
* Original author: Chakshu Gupta (ChakshuGupta13)
* Source: https://github.com/ChakshuGupta13/propositional-logic-theorem-prover-using-resolution-refutation
"""
from parser import forward_slice, backward_slice
def induce_parenthesis(sentence):
"""
Induces parentheses in a propositional formula according to operator precedence.
Args:
sentence (list): Tokenized propositional formula
Returns:
list: Formula with parentheses inserted based on operator precedence
"""
sentence = around_unary_op(sentence, "!")
sentence = around_binary_op(sentence, "&")
sentence = around_binary_op(sentence, "|")
sentence = around_binary_op(sentence, ">")
sentence = around_binary_op(sentence, "=")
return sentence
def around_unary_op(sentence, op):
"""
Places parentheses around expressions with unary operators.
Args:
sentence (list): Tokenized propositional formula
op (str): Unary operator (e.g., "!")
Returns:
list: Formula with parentheses added around unary operations
"""
processed_sentence = []
i = 0
while i < len(sentence):
if sentence[i] == op:
i += 1
sentence_slice, i = forward_slice(sentence, i)
sentence_slice = around_unary_op(sentence_slice, op)
processed_sentence += ["(", "!"] + sentence_slice.copy() + [")"]
else:
processed_sentence.append(sentence[i])
i += 1
return processed_sentence
def around_binary_op(sentence, op):
"""
Places parentheses around expressions with binary operators.
Args:
sentence (list): Tokenized propositional formula
op (str): Binary operator (e.g., "&", "|", ">", "=")
Returns:
list: Formula with parentheses added around binary operations
"""
processed_sentence = []
i = 0
while i < len(sentence):
if sentence[i] == op:
A, processed_sentence = backward_slice(processed_sentence)
A = around_binary_op(A, op)
i += 1
sentence_slice, i = forward_slice(sentence, i)
sentence_slice = around_binary_op(sentence_slice, op)
processed_sentence += (
["("] + A.copy() + [op] + sentence_slice.copy() + [")"]
)
else:
processed_sentence.append(sentence[i])
i += 1
return processed_sentence
def literal_not_protected(sentence):
"""
Checks if there's a literal in the sentence that is not protected by parentheses.
Args:
sentence (list): Tokenized propositional formula
Returns:
bool: True if there's an unprotected literal, False otherwise
"""
from parser import OPERATORS
if not any(i in OPERATORS for i in sentence):
return False
off_balance = 0
for _, token in enumerate(sentence):
if token == "(":
off_balance += 1
elif token == ")":
off_balance -= 1
elif off_balance == 0:
return True
return False
def eliminate_invalid_parenthesis(sentence):
"""
Eliminates unnecessary parentheses from a propositional formula.
Args:
sentence (list): Tokenized propositional formula
Returns:
list: Formula with unnecessary parentheses removed
"""
processed_sentence = []
brackets = []
content = []
for _, token in enumerate(sentence):
if token == "(":
content.append(processed_sentence.copy())
brackets.append("(")
processed_sentence.clear()
elif token == ")" and content:
if literal_not_protected(processed_sentence):
processed_sentence = ["("] + processed_sentence + [")"]
processed_sentence = content[len(content) - 1].copy() + processed_sentence
brackets.pop()
content.pop()
else:
processed_sentence.append(token)
return processed_sentence
def iff_equivalent(A, B):
"""
Returns ((A>B)&(B>A)) as equivalent of (A=B).
Args:
A (list): Tokenized propositional formula
B (list): Tokenized propositional formula
Returns:
list: Equivalent formula for A=B in terms of implications
"""
return (
["(", "("]
+ A.copy()
+ [">"]
+ B.copy()
+ [")", "&", "("]
+ B.copy()
+ [">"]
+ A.copy()
+ [")", ")"]
)
def implies_equivalent(A, B):
"""
Returns ((!A)|B) as equivalent of (A>B).
Args:
A (list): Tokenized propositional formula
B (list): Tokenized propositional formula
Returns:
list: Equivalent formula for A>B in terms of negation and disjunction
"""
return ["(", "(", "!"] + A.copy() + [")", "|"] + B.copy() + [")"]
def eliminate_op(sentence, op):
"""
Eliminates complex operators (= and >) from a propositional formula.
Args:
sentence (list): Tokenized propositional formula
op (str): Operator to eliminate (e.g., "=", ">")
Returns:
list: Formula with the specified operator eliminated
"""
processed_sentence = []
i = 0
while i < len(sentence):
if sentence[i] == op:
A, processed_sentence = backward_slice(processed_sentence)
i += 1
B, i = forward_slice(sentence, i)
A = eliminate_op(A, op)
B = eliminate_op(B, op)
processed_sentence += (
iff_equivalent(A, B) if op == "=" else implies_equivalent(A, B)
).copy()
else:
processed_sentence.append(sentence[i])
i += 1
return processed_sentence
def move_not_inwards(sentence):
"""
Moves negations inward using De Morgan's laws.
Args:
sentence (list): Tokenized propositional formula
Returns:
list: Formula with negations moved inward
"""
processed_sentence = []
while True:
processed_sentence = []
i = 0
while i < len(sentence):
if sentence[i] == "!":
i += 1
B, i = forward_slice(sentence, i)
if B[0] == "(":
processed_sentence.append("(")
j = 1
while j < len(B):
tmp, j = forward_slice(B, j)
tmp.pop(0) if tmp[0] == "!" else tmp.insert(0, "!")
processed_sentence += tmp.copy()
j += 1
if j < len(B) - 1:
processed_sentence += (
["&"] if B[j] == "|" else ["|"] if (B[j] == "&") else []
)
j += 1
processed_sentence.append(")")
else:
B.pop(0) if B[0] == "!" else processed_sentence.append("!")
processed_sentence += B.copy()
else:
processed_sentence.append(sentence[i])
i += 1
if processed_sentence == sentence:
break
sentence = processed_sentence
return processed_sentence
def distribute_or_over_and(sentence):
"""
Distributes OR over AND to convert to CNF.
Args:
sentence (list): Tokenized propositional formula
Returns:
list: Formula with OR distributed over AND
"""
processed_sentence = []
i = 0
while i < len(sentence):
if sentence[i] == "|":
A, processed_sentence = backward_slice(processed_sentence)
A = distribute_or_over_and(A)
tmp3 = []
if A[0] == "(":
j = 1
while j < (len(A) - 1):
tmp, j = forward_slice(A, j)
tmp3.append(tmp.copy())
j += 2
else:
tmp3.append(A.copy())
i += 1
assert i < len(sentence)
B, i = forward_slice(sentence, i)
B = distribute_or_over_and(B)
tmp2 = []
if B[0] == "(":
j = 1
while j < (len(B) - 1):
tmp, j = forward_slice(B, j)
tmp2.append(tmp.copy())
j += 2
else:
tmp2.append(B.copy())
for k in range(0, len(tmp2)):
for m in range(0, len(tmp3)):
processed_sentence += (
["("]
+ tmp3[m].copy()
+ ["|"]
+ tmp2[k].copy()
+ [")"]
+ (["&"] if m != len(tmp3) - 1 else [])
)
processed_sentence += ["&"] if k != len(tmp2) - 1 else []
else:
processed_sentence.append(sentence[i])
i += 1
return processed_sentence
def _process_operand(operand):
"""
Processes an operand by removing unnecessary parentheses.
Args:
operand (list): Tokenized propositional formula
Returns:
list: Processed operand
"""
operand = eliminate_invalid_parenthesis(operand)
if operand[0] == "(":
return (
["("]
+ [j for j in operand[1 : len(operand) - 1] if j not in ["(", ")"]]
+ [")"]
)
else:
return [j for j in operand[0 : len(operand)] if j not in ["(", ")"]]
def split_around_and(sentence):
"""
Splits a sentence around AND operators.
Args:
sentence (list): Tokenized propositional formula
Returns:
list: Formula split around AND operators
"""
processed_sentence = []
operand = []
for _, token in enumerate(sentence):
if token == "&":
processed_sentence += _process_operand(operand).copy()
processed_sentence.append("&")
operand.clear()
else:
operand.append(token)
return processed_sentence + _process_operand(operand).copy()
def to_cnf(sentence):
"""
Converts a propositional formula to Conjunctive Normal Form (CNF).
Args:
sentence (list): Tokenized propositional formula
Returns:
list: Formula in CNF
"""
sentence = induce_parenthesis(sentence)
sentence = eliminate_invalid_parenthesis(sentence)
sentence = eliminate_op(sentence, "=")
sentence = eliminate_invalid_parenthesis(sentence)
sentence = eliminate_op(sentence, ">")
sentence = eliminate_invalid_parenthesis(sentence)
sentence = move_not_inwards(sentence)
sentence = eliminate_invalid_parenthesis(sentence)
prev = []
while prev != sentence:
prev = sentence
sentence = distribute_or_over_and(sentence)
sentence = eliminate_invalid_parenthesis(sentence)
return split_around_and(sentence)