-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathformulacnf.py
executable file
·1134 lines (959 loc) · 32.2 KB
/
formulacnf.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
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# ----------------------------------
#
# Module formulacnf.py
"""
Clausification of first-order formulas. Clausification is done in
several steps:
1) Simplification
Exhaustively apply the simplifiction rules described in the header
to FormulaSimplify
2) Construction of the Negation Normal Form
3) Miniscoping
4) Variable renaming
5) Skolemization
6) Shift out universal quantors
7) Distribute disjunctions
8) Extract clauses
This basically follows [NW:SmallCNF-2001], albeit with some minor
changes. The first version does not use formula renaming.
@InCollection{NW:SmallCNF-2001,
author = {A. Nonnengart and C. Weidenbach},
title = {{Computing Small Clause Normal Forms}},
booktitle = {Handbook of Automated Reasoning},
publisher = {Elsevier Science and MIT Press},
year = {2001},
editor = {A. Robinson and A. Voronkov},
volume = {I},
chapter = {5},
pages = {335--367}
}
Copyright 2011-2023 Stephan Schulz, [email protected]
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program ; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston,
MA 02111-1307 USA
The original copyright holder can be contacted as
Stephan Schulz
Auf der Altenburg 7
70376 Stuttgart
Germany
Email: [email protected]
"""
import unittest
from lexer import Token,Lexer
from derivations import Derivable,Derivation,flatDerivation,enableDerivationOutput,toggleDerivationOutput
from terms import *
from substitutions import Substitution, freshVar
from literals import Literal
from clauses import Clause
from formulas import Formula, WFormula, parseWFormula, parseFormula
class SkolemSymbols(object):
"""
Class for providing fresh Skolem symbols.
"""
skolemCount = 0
def __init__(self):
pass
def newSkolemSymbol(self):
"""
Return a new skolem symbol. This is a simple version, not
suitable for a real production system. The symbol is not
guaranteed to be globally fresh. It's the user's
responsibility to ensure that no symbols of the form
"skolemXXXX" are in the input.
"""
SkolemSymbols.skolemCount += 1
return "skolem%04d"%(SkolemSymbols.skolemCount,)
def newSkolemTerm(self, varlist):
"""
Return a new skolem term for the given (list of) variables.
"""
symbol = self.newSkolemSymbol()
res = [symbol]
res.extend(varlist)
return res
def __call__(self, varlist):
"""
Nicer interface to make getting new Skolem terms more
convenient.
"""
return self.newSkolemTerm(varlist)
skolemGenerator = SkolemSymbols()
def formulaOpSimplify(f):
"""
Simplify the formula by eliminating the <=, ~|, ~& and <~>. This
is not strictly necessary, but means fewer cases to consider
later. The following rules are applied exhaustively:
F~|G -> ~(F|G)
F~&G -> ~(F&G)
F<=G -> G=>F
F<~>G -> ~(F<=>G)
Returns f', modified
"""
if f.isLiteral():
return f, False
modified = False
# First simplify the subformulas
if f.hasSubform1():
child1, m = formulaOpSimplify(f.child1)
modified |= m
else:
child1 = f.child1
if f.hasSubform2():
child2, m = formulaOpSimplify(f.child2)
modified |= m
else:
child2 = None
if modified:
f = Formula(f.op, child1, child2)
if f.op == "<~>":
handle = Formula("<=>", f.child1, f.child2)
newform = Formula("~", handle)
return newform, True
elif f.op == "<=":
newform = Formula("=>", f.child2, f.child1)
return newform, True
elif f.op == "~|":
handle = Formula("|", f.child1, f.child2)
newform = Formula("~", handle)
return newform, True
elif f.op == "~&":
handle = Formula("&", f.child1, f.child2)
newform = Formula("~", handle)
return newform, True
return f, modified
def formulaTopSimplify(f):
"""
Try to apply the following simplification rules to f at the top
level. Return (f',m), where f' is the result of simplification,
and m indicates if f'!=f, i.e. if any of the simplification rules
has been applied.
"""
if f.op == "~":
if f.child1.isLiteral():
# Push ~ into literals if possible. This covers the case
# of ~~P -> P if one of the negations is in the literal.
return Formula("", f.child1.child1.negate()), True
elif f.op == "|":
if f.child1.isPropConst(True):
# T | P -> T. Note that child1 is $true or
# equivalent. This applies to several other cases where we
# can reuse a $true or $false child instead of creating a
# new formula.
return f.child1, True
elif f.child2.isPropConst(True):
# P | T -> T
return f.child2, True
elif f.child1.isPropConst(False):
# F | P -> P
return f.child2, True
elif f.child2.isPropConst(False):
# P | F -> P
return f.child1, True
elif f.child1.isEqual(f.child2):
# P | P -> P
return f.child2, True
elif f.op == "&":
if f.child1.isPropConst(True):
# T & P -> P
return f.child2, True
elif f.child2.isPropConst(True):
# P & T -> P
return f.child1, True
elif f.child1.isPropConst(False):
# F & P -> F
return f.child1, True
elif f.child2.isPropConst(False):
# P & F -> F
return f.child2, True
elif f.child1.isEqual(f.child2):
# P & P -> P
return f.child2, True
elif f.op == "<=>":
if f.child1.isPropConst(True):
# T <=> P -> P
return f.child2, True
elif f.child2.isPropConst(True):
# P <=> T -> P
return f.child1, True
elif f.child1.isPropConst(False):
# P <=> F -> ~P
newform = Formula("~", f.child2)
newform, m = formulaSimplify(newform)
return newform, True
elif f.child2.isPropConst(False):
# F <=> P -> ~P
newform = Formula("~", f.child1)
newform, m = formulaSimplify(newform)
return newform, True
elif f.child1.isEqual(f.child2):
# P <=> P -> T
return Formula("", Literal(["$true"])), True
elif f.op == "=>":
if f.child1.isPropConst(True):
# T => P -> P
return f.child2, True
elif f.child1.isPropConst(False):
# F => P -> T
return Formula("", Literal(["$true"])), True
elif f.child2.isPropConst(True):
# P => T -> T
return Formula("", Literal(["$true"])), True
elif f.child2.isPropConst(False):
# P => F -> ~P
newform = Formula("~", f.child1)
newform, m = formulaSimplify(newform)
return newform, True
elif f.child1.isEqual(f.child2):
# P => P -> T
return Formula("", Literal(["$true"])), True
elif f.op in ["!", "?"]:
# ![X] F -> F if X is not free in F
# ?[X] F -> F if X is not free in F
vars = f.child2.collectFreeVars()
if not f.child1 in vars:
return f.child2, True
assert f.op == "" or "Unexpected op"
return f, False
def formulaSimplify(f):
"""
Exhaustively apply simplification to f, creating the simplified
version f'. See formulaTopSimplify()
above for the individual rules.
Returns (f', True) if f'!=f, (f', False) otherwise (i.e. if no
simplification happened because the formula already was completely
simplified.
"""
if f.isLiteral():
return f, False
modified = False
# First simplify the subformulas
if f.hasSubform1():
child1, m = formulaSimplify(f.child1)
modified |= m
else:
child1 = f.child1
if f.hasSubform2():
child2, m = formulaSimplify(f.child2)
modified |= m
else:
child2 = None
if modified:
f = Formula(f.op, child1, child2)
topmod = True
while topmod:
f, topmod = formulaTopSimplify(f)
modified |= topmod
return f, modified
def rootFormulaNNF(f, polarity):
"""
Apply all NNF transformation rules that can be applied at top
level. Return result and modification flag.
"""
normalform = False
modified = False
while not normalform:
normalform = True
m = False
if f.op == "~":
if f.child1.isLiteral():
# Move negations into literals
f = Formula("", f.child1.child1.negate())
m = True
elif f.child1.op == "|":
# De Morgan: ~(P|Q) -> ~P & ~Q
f = Formula("&",
Formula("~", f.child1.child1),
Formula("~", f.child1.child2))
m = True
elif f.child1.op == "&":
# De Morgan: ~(P&Q) -> ~P | ~Q
f = Formula("|",
Formula("~", f.child1.child1),
Formula("~", f.child1.child2))
m = True
elif f.child1.op == "!":
# ~(![X]:P) -> ?[X]:~P
f = Formula("?",
f.child1.child1,
Formula("~", f.child1.child2))
m = True
elif f.child1.op == "?":
# ~(?[X]:P) -> ![X]:~P
f = Formula("!",
f.child1.child1,
Formula("~", f.child1.child2))
m = True
elif f.op == "=>":
# Expand P=>Q into ~P|Q
f = Formula("|",
Formula("~", f.child1),
f.child2)
m = True
elif f.op == "<=>":
if polarity == 1:
# P<=>Q -> (P=>Q)&(Q=>P)
f = Formula("&",
Formula("=>", f.child1, f.child2),
Formula("=>", f.child2, f.child1))
m = True
else:
assert polarity == -1
# P<=>Q -> (P & Q) | (~P & ~Q)
f = Formula("|",
Formula("&", f.child1, f.child2),
Formula("&",
Formula("~", f.child1),
Formula("~", f.child2)))
m = True
normalform = not m
modified |= m
return f, modified
def formulaNNF(f, polarity):
"""
Convert f into a NNF. Equivalences (<=>) are eliminated
polarity-dependend, top to bottom. Returns (f', m), where f' is a
NNF of f, and m indicates if f!=f'
"""
normalform = False
modified = False
while not normalform:
normalform = True
f, m = rootFormulaNNF(f, polarity)
modified |= m
if f.op == "~":
handle, m = formulaNNF(f.child1, -polarity)
if m:
normalform = False
f = Formula("~", handle)
elif f.op in ["!", "?"]:
handle, m = formulaNNF(f.child2, polarity)
if m:
normalform = False
f = Formula(f.op, f.child1, handle)
elif f.op in ["|", "&"]:
handle1, m1 = formulaNNF(f.child1, polarity)
handle2, m2 = formulaNNF(f.child2, polarity)
m = m1 or m2
if m:
normalform = False
f = Formula(f.op, handle1, handle2)
else:
assert f.isLiteral()
modified |= m
return f, modified
def formulaMiniScope(f):
"""
Perform miniscoping, i.e. move quantors in as far as possible, so
that their scope is only the smallest subformula in which the
variable occurs.
"""
res = False
if f.isQuantified():
op = f.child2.op
quant = f.op
var = f.child1
subf = f.child2
if op == "&" or op == "|":
if not var in subf.child1.collectFreeVars():
# q[X]:(P op Q) -> P op (q[X]:Q) if X not free in P
arg2 = Formula(quant, var, subf.child2)
arg1 = subf.child1
f = Formula(op, arg1, arg2)
res = True
elif not var in subf.child2.collectFreeVars():
# q[X]:(P op Q) -> (q[X]:P) op Q if X not free in Q
arg1 = Formula(quant, var, subf.child1)
arg2 = subf.child2
f = Formula(op, arg1, arg2)
res = True
else:
if op == "&" and quant == "!":
# ![X]:(P&Q) -> ![X]:P & ![X]:Q
arg1 = Formula("!", var, subf.child1)
arg2 = Formula("!", var, subf.child2)
f = Formula("&" , arg1, arg2)
res = True
elif op == "|" and quant == "?":
# ?[X]:(P|Q) -> ?[X]:P | ?[X]:Q
arg1 = Formula("?", var, subf.child1)
arg2 = Formula("?", var, subf.child2)
f = Formula("|", arg1, arg2)
res = True
arg1 = f.child1
arg2 = f.child2
modified = False
if f.hasSubform1():
arg1, m = formulaMiniScope(f.child1)
modified |= m
if f.hasSubform2():
arg2, m = formulaMiniScope(f.child2)
modified |= m
if modified:
f = Formula(f.op, arg1, arg2)
f,m = formulaMiniScope(f)
res = True
return f, res
def formulaVarRename(f, subst = None):
"""
Rename variables in f so that all bound variables are unique.
"""
if subst == None:
subst = Substitution()
if f.isQuantified():
# New scope of a variable -> add a new binding to a new
# variable. Store potential old binding to restore when
# leaving the scope later
var = f.child1
newvar = freshVar()
oldbinding = subst.modifyBinding((var, newvar))
if f.isLiteral():
# Create copy with the new variables recorded in subst
child = f.child1.instantiate(subst)
f = Formula("", child)
else:
# This is a composite formula. Rename it...
arg1 = None
arg2 = None
if f.isQuantified():
# Apply new renaming locally to the bound variable and
# recusively to the subformula
arg1 = newvar
arg2 = formulaVarRename(f.child2, subst)
else:
# Apply renaming to all subformulas
if f.hasSubform1():
arg1 = formulaVarRename(f.child1, subst)
if f.hasSubform2():
arg2 = formulaVarRename(f.child2, subst)
f = Formula(f.op, arg1, arg2)
if f.isQuantified():
# We are leaving the scope of the quantifier, so restore
# substitution.
subst.modifyBinding((var, oldbinding))
return f
def formulaRekSkolemize(f, variables, subst):
"""
Perform Skolemization of f, which is assumed to be in the scope of
the list of variables provided.
"""
if f.isLiteral():
child = f.child1.instantiate(subst)
f = Formula("", child)
elif f.op == "?":
var = f.child1
skTerm = skolemGenerator(variables)
oldbinding = subst.modifyBinding((var,skTerm))
f = formulaRekSkolemize(f.child2, variables, subst)
subst.modifyBinding((var, oldbinding))
elif f.op == "!":
var = f.child1
variables.append(var)
handle = formulaRekSkolemize(f.child2, variables, subst)
f = Formula("!", var, handle)
variables.pop()
else:
arg1 = None
arg2 = None
if f.hasSubform1():
arg1 = formulaRekSkolemize(f.child1, variables, subst)
if f.hasSubform2():
arg2 = formulaRekSkolemize(f.child2, variables, subst)
f = Formula(f.op, arg1, arg2)
return f
def formulaSkolemize(f):
"""
Perform an outermost Skolemization of f, removing all existential
quantifiers. Formulas are considered to be universally closed,
i.e. free variables (which should not occur) are treated as
universally quantified.
"""
vars = f.collectFreeVars()
varstack = [v for v in vars]
res = formulaRekSkolemize(f, varstack, Substitution())
return res
def separateQuantors(f, varlist=None):
"""
Remove all quantors from f, returning the quantor-free core of the
formula and a list of quanified variables. This will only be
applied to Skolemized formulas, thus finding an existential
quantor is an error. To be useful, the input formula also has to
be variable-normalized.
"""
if varlist == None:
varlist = list()
if f.isQuantified():
assert f.op == "!"
varlist.append(f.child1)
f, dummy = separateQuantors(f.child2, varlist)
elif f.isLiteral():
pass
else:
arg1 = None
arg2 = None
if f.hasSubform1():
arg1, dummy = separateQuantors(f.child1, varlist)
if f.hasSubform2():
arg2, dummy = separateQuantors(f.child2, varlist)
f = Formula(f.op, arg1, arg2)
return f, varlist
def formulaShiftQuantorsOut(f):
"""
Shift all (universal) quantor to the outermost level.
"""
f, varlist = separateQuantors(f)
while varlist:
f = Formula("!", varlist.pop(), f)
return f
def formulaDistributeDisjunctions(f):
"""
Convert a Skolemized formula in prefix-NNF form into Conjunctive
Normal Form.
"""
arg1 = None
arg2 = None
if f.isQuantified():
arg1 = f.child1
arg2 = formulaDistributeDisjunctions(f.child2)
f = Formula(f.op, arg1, arg2)
elif f.isLiteral():
pass
else:
if f.hasSubform1():
arg1 = formulaDistributeDisjunctions(f.child1)
if f.hasSubform2():
arg2 = formulaDistributeDisjunctions(f.child2)
f = Formula(f.op, arg1, arg2)
if f.op == "|":
if f.child1.op == "&":
# (P&Q)|R -> (P|R) & (Q|R)
arg1 = Formula("|", f.child1.child1, f.child2)
arg2 = Formula("|", f.child1.child2, f.child2)
f = Formula("&", arg1, arg2)
f = formulaDistributeDisjunctions(f)
elif f.child2.op == "&":
# (R|(P&Q) -> (R|P) & (R|Q)
arg1 = Formula("|", f.child1, f.child2.child1)
arg2 = Formula("|", f.child1, f.child2.child2)
f = Formula("&", arg1, arg2)
f = formulaDistributeDisjunctions(f)
return f
def formulaCNFSplit(f):
"""
Given a formula in CNF, convert it to a set of clauses.
"""
ftype = "plain"
if(f.type in ["conjecture", "negated_conjecture"]):
ftype = f.type
matrix = f.formula.getMatrix()
res = []
conjuncts = matrix.conj2List()
for c in conjuncts:
litlist = [l.child1 for l in c.disj2List()]
clause = Clause(litlist, ftype)
res.append(clause)
return res
def wFormulaCNF(wf):
"""
Convert a (wrapped) formula to Conjunctive Normal Form.
"""
ftype = "plain"
if(wf.type in ["conjecture", "negated_conjecture"]):
ftype = wf.type
f, m0 = formulaOpSimplify(wf.formula)
f, m1 = formulaSimplify(f)
if m0 or m1:
tmp = WFormula(f, ftype)
tmp.setDerivation(flatDerivation("fof_simplification", [wf]))
wf = tmp
f,m = formulaNNF(f,1)
if m:
tmp = WFormula(f, ftype)
tmp.setDerivation(flatDerivation("fof_nnf", [wf]))
wf = tmp
f,m = formulaMiniScope(f)
if m:
tmp = WFormula(f, ftype)
tmp.setDerivation(flatDerivation("shift_quantors", [wf]))
wf = tmp
f = formulaVarRename(f)
if not f.isEqual(wf.formula):
tmp = WFormula(f, ftype)
tmp.setDerivation(flatDerivation("variable_rename", [wf]))
wf = tmp
f = formulaSkolemize(f)
if not f.isEqual(wf.formula):
tmp = WFormula(f, ftype)
tmp.setDerivation(flatDerivation("skolemize", [wf], "status(esa)"))
wf = tmp
f = formulaShiftQuantorsOut(f)
if not f.isEqual(wf.formula):
tmp = WFormula(f, ftype)
tmp.setDerivation(Derivation("shift_quantors", [wf]))
wf = tmp
f = formulaDistributeDisjunctions(f)
if not f.isEqual(wf.formula):
tmp = WFormula(f, ftype)
tmp.setDerivation(flatDerivation("distribute", [wf]))
wf = tmp
return wf
def wFormulaClausify(wf):
"""
Convert a formula into Clause Normal Form.
"""
wf = wFormulaCNF(wf)
clauses = formulaCNFSplit(wf)
for c in clauses:
c.setDerivation(flatDerivation("split_conjunct", [wf]))
return clauses
# ------------------------------------------------------------------
# Unit test section
# ------------------------------------------------------------------
class TestCNF(unittest.TestCase):
"""
Test cases for clausification.
"""
def setUp(self):
"""
Setup function for clause/literal unit tests. Initialize
variables needed throughout the tests.
"""
print()
self.formulas = """
![X]:(a(X) ~| ~a=b)
![X]:(a(X)|b(X)|?[X,Y]:(p(X,f(Y))<~>q(g(a),X)))
![X]:(a(X) <= ~a=b)
((((![X]:a(X))|b(X))|(?[X]:(?[Y]:p(X,f(Y)))))~&q(g(a),X))
![X]:(a(X)|$true)
"""
lex = Lexer(self.formulas)
self.f1 = parseFormula(lex)
self.f2 = parseFormula(lex)
self.f3 = parseFormula(lex)
self.f4 = parseFormula(lex)
self.f5 = parseFormula(lex)
self.simple_ops = set(["", "!", "?", "~", "&","|", "=>", "<=>"])
self.nnf_ops = set(["", "!", "?", "&","|"])
self.covformulas ="""
(a|$true)
($true|a)
(a|$false)
($false|a)
(a|a)
(a&$true)
($true&a)
(a&$false)
($false&a)
(a&a)
(a=>$true)
($true=>a)
(a=>$false)
($false=>a)
(a=>a)
(a<=>$true)
($true<=>a)
(a<=>$false)
($false<=>a)
(a<=>a)
![X]:(a<=>a)
?[X]:(a<=>a)
a<=>b
"""
self.testformulas = """
fof(t12_autgroup,conjecture,(
! [A] :
( ( ~ v3_struct_0(A)
& v1_group_1(A)
& v3_group_1(A)
& v4_group_1(A)
& l1_group_1(A) )
=> r1_tarski(k4_autgroup(A),k1_fraenkel(u1_struct_0(A),u1_struct_0(A))) ) )).
fof(abstractness_v1_group_1,axiom,(
! [A] :
( l1_group_1(A)
=> ( v1_group_1(A)
=> A = g1_group_1(u1_struct_0(A),u1_group_1(A)) ) ) )).
fof(antisymmetry_r2_hidden,axiom,(
! [A,B] :
( r2_hidden(A,B)
=> ~ r2_hidden(B,A) ) )).
fof(cc1_fraenkel,axiom,(
! [A] :
( v1_fraenkel(A)
=> ! [B] :
( m1_subset_1(B,A)
=> ( v1_relat_1(B)
& v1_funct_1(B) ) ) ) )).
fof(cc1_funct_1,axiom,(
! [A] :
( v1_xboole_0(A)
=> v1_funct_1(A) ) )).
fof(cc1_funct_2,axiom,(
! [A,B,C] :
( m1_relset_1(C,A,B)
=> ( ( v1_funct_1(C)
& v1_partfun1(C,A,B) )
=> ( v1_funct_1(C)
& v1_funct_2(C,A,B) ) ) ) )).
fof(testscosko, axiom, (![X]:?[Y]:((p(X)&q(X))|q(X,Y))|a)).
"""
def testOpSimplification(self):
"""
Test that operator simplification works.
"""
f,m = formulaOpSimplify(self.f1)
self.assertTrue(m)
self.assertTrue(f.collectOps() <= self.simple_ops)
f,m = formulaOpSimplify(self.f2)
self.assertTrue(m)
self.assertTrue(f.collectOps() <= self.simple_ops)
f,m = formulaOpSimplify(self.f3)
self.assertTrue(m)
self.assertTrue(f.collectOps() <= self.simple_ops)
f,m = formulaOpSimplify(self.f4)
self.assertTrue(m)
self.assertTrue(f.collectOps() <= self.simple_ops)
f,m = formulaOpSimplify(self.f5)
self.assertTrue(not m)
self.assertTrue(f.collectOps() <= self.simple_ops)
def checkSimplificationResult(self, f):
"""
Simplification results in a formula that does not contain
the constant predicates $true or $false. The only exception is
when the whole formula has been has been reduced to a single
literal (in which case it can be either $true, or $false, or
any other literal).
"""
funs = f.collectFuns()
if f.isPropConst(True) or f.isPropConst(False):
self.assertTrue(funs in [set(["$true"]), set(["$false"])])
else:
self.assertTrue(not "$true" in funs )
self.assertTrue(not "$false" in funs )
def testSimplification(self):
"""
Test that simplification works.
"""
f,m = formulaOpSimplify(self.f1)
f,m = formulaSimplify(f)
self.checkSimplificationResult(f)
f,m = formulaOpSimplify(self.f2)
f,m = formulaSimplify(f)
self.checkSimplificationResult(f)
f,m = formulaOpSimplify(self.f3)
f,m = formulaSimplify(f)
self.checkSimplificationResult(f)
f,m = formulaOpSimplify(self.f4)
f,m = formulaSimplify(f)
self.checkSimplificationResult(f)
f,m = formulaOpSimplify(self.f5)
f,m = formulaSimplify(f)
self.checkSimplificationResult(f)
lex = Lexer(self.covformulas)
while not lex.TestTok(Token.EOFToken):
f = parseFormula(lex)
f,m = formulaOpSimplify(f)
f,m = formulaSimplify(f)
self.checkSimplificationResult(f)
def checkNNFResult(self, f):
"""
A simplified formula is either $true/$false, or it only
contains &, |, !, ? as operators (~ is shifted into the
literals).
"""
print("NNF:", f)
if f.isPropConst(True) or f.isPropConst(False):
funs = f.collectFuns()
self.assertTrue(funs in [set(["$true"]), set(["$false"])])
else:
ops = f.collectOps()
self.assertTrue(ops <= self.nnf_ops)
def testNNF(self):
"""
Test NNF transformation
"""
f,m = formulaOpSimplify(self.f1)
f,m = formulaSimplify(f)
f,m = formulaNNF(f, 1)
self.checkNNFResult(f)
f,m = formulaOpSimplify(self.f2)
f,m = formulaSimplify(f)
f,m = formulaNNF(f,1)
self.checkNNFResult(f)
f,m = formulaOpSimplify(self.f3)
f,m = formulaSimplify(f)
f,m = formulaNNF(f,1)
self.checkNNFResult(f)
f,m = formulaOpSimplify(self.f4)
f,m = formulaSimplify(f)
f,m = formulaNNF(f,1)
self.checkNNFResult(f)
f,m = formulaOpSimplify(self.f5)
f,m = formulaSimplify(f)
f,m = formulaNNF(f,1)
self.checkNNFResult(f)
lex = Lexer(self.covformulas)
while not lex.TestTok(Token.EOFToken):
f = parseFormula(lex)
f,m = formulaOpSimplify(f)
f,m = formulaSimplify(f)
f,m = formulaNNF(f,1)
self.checkNNFResult(f)
def testMiniScope(self):
"""
Test Miniscoping.
"""
lex = Lexer("""
![X]:(p(X)|q(a))
?[X]:(p(a)&q(X))
![X]:(p(X)&q(X))
?[X]:(p(X)|q(X))
![X]:(p(X)|q(X))
![X,Y]:?[Z]:(p(Z)|q(X))
""")
res = [True, True, True, True, False, True]
while not lex.TestTok(Token.EOFToken):
expected = res.pop(0)
f = parseFormula(lex)
f1,m = formulaMiniScope(f)
print(f, f1, m, expected)
self.assertEqual(expected, m)
if m:
self.assertTrue(not f1.isQuantified())
def testRenaming(self):
"""
Test variable renaming
"""
lex = Lexer("![X]:(p(X)|![X]:(q(X)&?[X]:r(X)))")
f = parseFormula(lex)
v1 = f.collectVars()
self.assertEqual(v1, set(["X"]))
v2 = f.collectFreeVars()
self.assertEqual(v2, set())
f1 = formulaVarRename(f)
print(f, f1)
v1 = f1.collectVars()
self.assertEqual(len(v1), 3)
v2 = f1.collectFreeVars()
self.assertEqual(v2, set())