-
Notifications
You must be signed in to change notification settings - Fork 0
/
CNF_helper.txl
497 lines (408 loc) · 9.55 KB
/
CNF_helper.txl
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
define cnf_entry
[cnf_expression]
end define
define cnf_expression
[cnf_expression] [SP] '| [SP] [cnf_expression]
| [cnf_term]
end define
define cnf_term
[cnf_term] [SP] '& [SP] [cnf_term]
| [cnf_inversion]
end define
define cnf_inversion
[SPOFF] '! [cnf_inversion] [SPON]
| [cnf_primary]
end define
define cnf_primary
[cnf_var_or_const]
| ( [cnf_expression] )
end define
define cnf_var_or_const
[id]
| '@
| '0
end define
% =================================================================
rule cnf_buildCNF
replace [cnf_expression]
E [cnf_expression]
construct NewE [cnf_expression]
E [cnf_demorganD]
[cnf_demorganC]
[cnf_cancelDoubleNegation]
[cnf_cancelDoubleNegation2]
[cnf_resolveBrackets]
[cnf_resolveBracketsSimple]
[cnf_resolveBracketsId]
[cnf_resolveBracketsR]
[cnf_resolveBracketsL]
[cnf_distributeOr]
[cnf_distributeOr2]
[cnf_collapseSelfOr]
[cnf_collapseSelfAnd]
[cnf_collapseSelfOr2]
[cnf_collapseSelfOr3]
[cnf_collapseSelfOr4]
[cnf_collapseSelfOr5]
[cnf_collapseSelfId]
[cnf_collapseSelfId2]
[cnf_collapseConstant_True]
[cnf_collapseConstant_True2]
[cnf_collapseConstant_True3]
[cnf_collapseConstant_True_or]
[cnf_collapseConstant_True_or2]
[cnf_collapseConstant_True_and]
[cnf_collapseConstant_True_and2]
[cnf_collapseConstant_False]
[cnf_collapseConstant_False2]
[cnf_collapseConstant_False3]
[cnf_collapseConstant_False_or]
[cnf_collapseConstant_False_or2]
[cnf_collapseConstant_False_and]
[cnf_collapseConstant_False_and2]
[cnf_consumeOr]
[cnf_consumeOr2]
where not
NewE [= E]
by
NewE
end rule
rule cnf_demorganD
replace [cnf_inversion]
'! '( A [cnf_expression] '| B [cnf_expression] ')
by
( '!(A) '& '!(B) )
end rule
rule cnf_demorganC
replace [cnf_inversion]
'!( A [cnf_term] '& B [cnf_term] )
by
( '!(A) '| '!(B) )
end rule
rule cnf_consumeOr
replace [cnf_expression]
A [cnf_term] '& B [cnf_term] '| A
by
A
end rule
rule cnf_consumeOr2
replace [cnf_expression]
A [cnf_term] '| A '& B [cnf_term]
by
A
end rule
rule cnf_cancelDoubleNegation
replace [cnf_inversion]
'!( '! A [cnf_inversion] )
by
A
end rule
rule cnf_cancelDoubleNegation2
replace [cnf_inversion]
'! '! A [cnf_inversion]
by
A
end rule
rule cnf_resolveBrackets
replace [cnf_expression]
( N [cnf_expression] )
by
N
end rule
rule cnf_resolveBracketsSimple
replace [cnf_primary]
( N [cnf_primary] )
by
N
end rule
rule cnf_resolveBracketsId
replace [cnf_expression]
( N [cnf_var_or_const] )
by
N
end rule
rule cnf_resolveBracketsR
replace [cnf_term]
A [cnf_term] '& ( B [cnf_inversion] '& C [cnf_inversion] )
by
A '& B '& C
end rule
rule cnf_resolveBracketsL
replace [cnf_term]
( A [cnf_term] '& B [cnf_inversion] ) '& C [cnf_inversion]
by
A '& B '& C
end rule
rule cnf_distributeOr
replace [cnf_expression]
A [cnf_expression] '| B [cnf_term] '& C [cnf_inversion]
by
(A '| B) '& (A '| C)
end rule
rule cnf_distributeOr2
replace [cnf_expression]
B [cnf_term] '& C [cnf_inversion] '| A [cnf_term]
by
(B '| A) '& (C '| A)
end rule
rule cnf_collapseSelfOr
replace * [cnf_expression]
A [cnf_expression] '| B [cnf_expression]
construct A_str [stringlit]
_ [quote A]
construct B_str [stringlit]
_ [quote B]
where
A_str [= B_str]
by
A
end rule
rule cnf_collapseSelfOr2
replace * [cnf_expression]
A [cnf_var_or_const] '| '! B [cnf_var_or_const] '| C [cnf_expression]
where
A [= B]
by
C
end rule
rule cnf_collapseSelfOr3
replace * [cnf_expression]
'! A [cnf_var_or_const] '| B [cnf_var_or_const] '| C [cnf_expression]
where
A [= B]
by
C
end rule
rule cnf_collapseSelfOr4
replace * [cnf_expression]
A [cnf_expression] '| B [cnf_var_or_const] '| '! C [cnf_var_or_const]
where
B [= C]
by
A
end rule
rule cnf_collapseSelfOr5
replace * [cnf_expression]
A [cnf_expression] '| '! B [cnf_var_or_const] '| C [cnf_var_or_const]
where
B [= C]
by
A
end rule
rule cnf_collapseSelfAnd
replace [cnf_term]
A [cnf_term] '& A
by
A
end rule
rule cnf_collapseSelfId
replace * [cnf_expression]
A [cnf_var_or_const] '| B [cnf_var_or_const] '| C [cnf_expression]
where
A [= B]
by
A '| C
end rule
rule cnf_collapseSelfId2
replace * [cnf_expression]
A [cnf_expression] '| B [cnf_var_or_const] '| C [cnf_var_or_const]
where
B [= C]
by
A '| B
end rule
rule cnf_collapseConstant_True
replace * [cnf_inversion]
'! '@
by
'0
end rule
rule cnf_collapseConstant_True2
replace * [cnf_expression]
'! A [cnf_inversion] '| A
by
'@
end rule
rule cnf_collapseConstant_True3
replace * [cnf_expression]
A [cnf_inversion] '| '! A
by
'@
end rule
rule cnf_collapseConstant_True_or
replace * [cnf_expression]
A [cnf_expression] '| '@
by
'@
end rule
rule cnf_collapseConstant_True_or2
replace * [cnf_expression]
'@ '| A [cnf_expression]
by
'@
end rule
rule cnf_collapseConstant_True_and
replace * [cnf_term]
A [cnf_term] '& '@
by
A
end rule
rule cnf_collapseConstant_True_and2
replace * [cnf_term]
'@ '& A [cnf_term]
by
A
end rule
rule cnf_collapseConstant_False
replace * [cnf_inversion]
'! '0
by
'@
end rule
rule cnf_collapseConstant_False2
replace * [cnf_term]
'! A [cnf_inversion] '& A
by
'0
end rule
rule cnf_collapseConstant_False3
replace * [cnf_term]
A [cnf_inversion] '& '! A
by
'0
end rule
rule cnf_collapseConstant_False_or
replace * [cnf_expression]
A [cnf_expression] '| '0
by
A
end rule
rule cnf_collapseConstant_False_or2
replace * [cnf_expression]
'0 '| A [cnf_expression]
by
A
end rule
rule cnf_collapseConstant_False_and
replace * [cnf_term]
A [cnf_term] '& '0
by
'0
end rule
rule cnf_collapseConstant_False_and2
replace * [cnf_term]
'0 '& A [cnf_term]
by
'0
end rule
% =================================================================
redefine cnf_entry
...
| [cnf_expression_lists]
end redefine
define cnf_expression_lists
[repeat cnf_expression_chain_and]
| [repeat cnf_expression_element_chain]
end define
define cnf_expression_chain_and
[cnf_expression_chain_or] [opt '&]
end define
define cnf_expression_chain_or
[cnf_expression_element]
| ( [repeat cnf_expression_element_chain] )
end define
define cnf_expression_element_chain
[cnf_expression_element] [opt '|]
end define
define cnf_expression_element
[opt '!] [cnf_var_or_const]
end define
% =================================================================
function cnf_convertToChains
replace [cnf_entry]
P [cnf_expression]
construct TypeHolder [cnf_expression_lists]
% void
construct newP [cnf_expression_lists]
TypeHolder [reparse P]
by
newP
end function
function cnf_convertToTree
replace [cnf_entry]
P [cnf_expression_lists]
construct TypeHolder [cnf_expression]
'SOME_USELESS_RANDOM_IDENTIFIER
construct newP [cnf_expression]
TypeHolder [reparse P]
by
newP
end function
% =================================================================
rule cnf_simplify
replace [cnf_expression_lists]
E [cnf_expression_lists]
construct NewE [cnf_expression_lists]
E [cnf_sort]
[cnf_sort2]
[cnf_collapseSelf_or]
[cnf_collapseSelf_and]
%[cnf_simplify_and]
[cnf_fix_tail_of_and_chain]
where not
NewE [= E]
by
NewE
end rule
rule cnf_sort
replace [repeat cnf_expression_element_chain]
Ao [opt '!] A [cnf_var_or_const] '| Bo [opt '!] B [cnf_var_or_const] Co [opt '|] C [repeat cnf_expression_element_chain]
construct A_str [stringlit]
_ [quote A]
construct B_str [stringlit]
_ [quote B]
where
B_str [< A_str]
by
Bo B '| Ao A Co C
end rule
rule cnf_sort2
replace [repeat cnf_expression_chain_and]
A [cnf_expression_chain_or] '& B [cnf_expression_chain_or] Bo [opt '&] C [repeat cnf_expression_chain_and]
construct A_str [stringlit]
_ [quote A]
construct B_str [stringlit]
_ [quote B]
where
B_str [< A_str]
by
B '& A Bo C
end rule
rule cnf_collapseSelf_or
replace [repeat cnf_expression_element_chain]
A [cnf_expression_element] '| A Bo [opt '|] C [repeat cnf_expression_element_chain]
by
A Bo C
end rule
rule cnf_collapseSelf_and
replace [repeat cnf_expression_chain_and]
A [cnf_expression_chain_or] '& A Bo [opt '&] C [repeat cnf_expression_chain_and]
by
A Bo C
end rule
rule cnf_fix_tail_of_and_chain
replace [repeat cnf_expression_chain_and]
B [cnf_expression_chain_or] '&
by
B
end rule
% =================================================================
rule do_cnf
replace $ [cnf_entry]
P [cnf_entry]
by
P [cnf_buildCNF]
[cnf_convertToChains]
[cnf_simplify]
%[cnf_convertToTree]
end rule