-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathformulas.py
executable file
·697 lines (602 loc) · 20.6 KB
/
formulas.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
#!/usr/bin/env python3
# ----------------------------------
#
# Module formulas.py
"""
A simple implementation of first-order formulas and their associated
meta-information.
See literals.py for the definition of atoms.
A formula is either a first-order-atom, or build from pre-existing
formulas using the various logical connectives and quantifiers:
Assume F,G are arbitrary formulas and X is an arbitrary variable. Then
(~F)
(F&G)
(F|G)
(F->G)
(F<=>G)
(F<~>G)
(F<-G)
(F~&G)
(F~|G)
(![X]:F)
(?[X]:F)
are formulas.
The set of all formulas for a given signature is denoted as
Formulas(P,F,V).
In the external representation, some parentheses can be omitted. Lists
of either conjunctively or disjunctively connected subformula are
assumed to associate left. (F & G & H) is equivalent to ((F&G)&H)
Formulas are represented on two levels: The actual logical formula is
a recursive data structure. This is wrapped in a container that
associates the formula with its meta-information. The implementation
uses literals as the base case, not atoms. That allows us to reuse
some code for parsing and printing infix equality, but also to
represent a formula in Negation Normal Form without any negations in
the frame of the formula.
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 collections import deque
from lexer import Token,Lexer
from derivations import Derivable,Derivation,flatDerivation,toggleDerivationOutput
from signature import Signature
from terms import *
import substitutions
from literals import Literal, parseLiteral, parseLiteralList,\
literalList2String, litInLitList, oppositeInLitList
class Formula(object):
"""
A class representing a naked first-order formula
formula. First-order operators are represented as strings, an
empty operator (i.e. "") indicates an atomic formula.
child1 and child2 are the subformulas (child2 may be empty).
In the case of an atomic formula (with operator ""), child1 is
a Literal object (this unifies parsing and simplifies
clausification), and child2 is empty (i.e. None).
In the case of quantified formulae (operator is "!" or "?"),
child1 is a plain string (i.e. the term representing the variable)
and child2 is the formula quantified over.
In the case of a negated formula, the operator is "~", child1 is
the single subformula, and child2 is empty.
In the last case (a binary composite formula), the operator is one
of "&", "|", "=>", "<=", "<=>", "<~>", "~|", "~&", and child1 and
child2 are the two subformulas.
"""
def __init__(self, op, child1, child2=None):
"""
Initialize the formula.
"""
self.op = op
self.child1 = child1
self.child2 = child2
def __repr__(self):
"""
Return a string representation of the formula.
"""
if not self.op:
res=str(self.child1)
elif self.op=="~":
res="(~"+repr(self.child1)+")"
elif self.op in ["&", "|", "=>", "<=", "<=>", "<~>", "~|", "~&"]:
res = "("+repr(self.child1)+self.op+repr(self.child2)+")"
else:
assert self.op in ["!", "?"]
res = "("+self.op+"["+term2String(self.child1)+\
"]:"+repr(self.child2)+")"
return res
def isLiteral(self):
"""
Return True if self is a literal.
"""
return not self.op
def isPropConst(self, polarity):
"""
Return True if self is a propositional constant of the given
polarity.
"""
if self.isLiteral():
if polarity:
return self.child1.isPropTrue()
else:
return self.child1.isPropFalse()
else:
return False
def isQuantified(self):
"""
Return True if self is quantified at the top level.
"""
return self.op in ["!", "?"]
def isBinary(self):
"""
Return True if self has a binary operator at the top level.
"""
return self.op in ["&", "|", "=>", "<=", "<=>", "<~>", "~|", "~&"]
def isUnary(self):
"""
Return True if self has a unary operator at the top level.
"""
return self.op == "~"
def isLiteralDisjunction(self):
"""
Return True iff the formula is a disjunction of literals.
"""
if self.isLiteral():
return True
if self.op == "|":
return self.child1.isLiteralDisjunction() and \
self.child2.isLiteralDisjunction()
return False
def isClauseConjunction(self):
"""
Return True if the formula is a conjunction of disjunction of
literals.
"""
if self.isLiteral():
return True
if self.op == "|":
return self.isLiteralDisjunction()
if self.op == "&":
return self.child1.isClauseConjunction() and \
self.child2.isClauseConjunction()
return False
def isCNF(self):
"""
Return True if the formula is in conjunctive normal form.
"""
if self.op == "!":
return self.child2.isCNF()
return self.isClauseConjunction()
def getMatrix(self):
"""
Return the formula without any leading quantifiers (if the
formula is in prefix normal form, this is the matrix of the
formuma).
"""
f = self
while f.isQuantified():
f = f.child2
return f
def conj2List(self):
"""
Return a list of the subformula connected by top-level "&".
"""
if self.op == "&":
return self.child1.conj2List()+self.child2.conj2List()
return [self]
def disj2List(self):
"""
Return a list of the subformula connected by top-level "|".
"""
if self.op == "|":
return self.child1.disj2List()+self.child2.disj2List()
return [self]
def hasSubform1(self):
"""
Return True if self has a proper subformula as the first
argument. This is false for quantified formulas and literals.
"""
return self.isUnary() or self.isBinary()
def hasSubform2(self):
"""
Return True if self has a proper subformula as the first
argument. This is the case for quantified formulas and binary
formulas.
"""
return self.isQuantified() or self.isBinary()
def isEqual(self, other):
"""
Return True if self is structurally equal to other.
"""
if self.op!=other.op:
return False
elif self.isLiteral():
return self.child1.isEqual(other.child1)
elif self.isQuantified():
return termEqual(self.child1, other.child1) and \
self.child2.isEqual(other.child2)
elif self.isUnary():
return self.child1.isEqual(other.child1)
else:
return self.child1.isEqual(other.child1) and \
self.child2.isEqual(other.child2)
def collectOps(self):
"""
Return the set of all (first-order) operators and quantors
used in the formula. This is mostly for unit-testing
transformations later on.
"""
res = set([self.op])
if self.isLiteral():
pass
elif self.isUnary():
res|=self.child1.collectOps()
elif self.isBinary():
res|=self.child1.collectOps()
res|=self.child2.collectOps()
else:
assert self.isQuantified()
res |= self.child2.collectOps()
return res
def collectFuns(self):
"""
Return the set of all function and predicate symbols used in
the formula.
"""
res = set()
if self.isLiteral():
self.child1.collectFuns(res)
elif self.isUnary():
res|=self.child1.collectFuns()
elif self.isBinary():
res|=self.child1.collectFuns()
res|=self.child2.collectFuns()
else:
assert self.isQuantified()
res |= self.child2.collectFuns()
return res
def collectSig(self, sig = None):
"""
Return the set of all function and predicate symbols used in
the formula.
"""
if not sig:
sig = Signature()
todo = deque()
todo.append(self)
while todo:
f = todo.popleft()
if f.isLiteral():
f.child1.collectSig(sig)
elif f.isUnary():
todo.append(f.child1)
elif f.isBinary():
todo.append(f.child1)
todo.append(f.child2)
else:
assert f.isQuantified()
todo.append(f.child2)
return sig
def collectVars(self):
"""
Return the set of all variables in self.
"""
if self.isLiteral():
res=self.child1.collectVars()
elif self.isUnary():
res=self.child1.collectVars()
elif self.isBinary():
res=self.child1.collectVars()
res|=self.child2.collectVars()
else:
assert self.isQuantified()
res = termCollectVars(self.child1)
res |= self.child2.collectVars()
return res
def collectFreeVars(self):
"""
Return the set of all free variables in self.
"""
if self.isLiteral():
res=self.child1.collectVars()
elif self.isUnary():
res=self.child1.collectFreeVars()
elif self.isBinary():
res=self.child1.collectFreeVars()
res|=self.child2.collectFreeVars()
else:
# Quantor case. We first collect all free variables in
# the quantified formula, then remove the one bound by the
# quantifier.
assert self.isQuantified()
res = self.child2.collectFreeVars()
res.discard(self.child1)
return res
def parseQuantified(lexer, quantor):
"""
Parse the "remainder" of a formula starting with the given
quantor.
"""
lexer.CheckTok(Token.IdentUpper)
var = parseTerm(lexer)
if lexer.TestTok(Token.Comma):
lexer.AcceptTok(Token.Comma)
rest = parseQuantified(lexer, quantor)
else:
lexer.AcceptTok(Token.CloseSquare)
lexer.AcceptTok(Token.Colon)
rest = parseUnitaryFormula(lexer)
res = Formula(quantor, var, rest)
return res
def parseUnitaryFormula(lexer):
"""
Parse a "unitary" formula (following TPTP-3 syntax
terminology). This can be the first unitary formula of a binary
formula, of course.
"""
if lexer.TestTok([Token.Universal, Token.Existential]):
quantor = lexer.LookLit()
lexer.Next()
lexer.AcceptTok(Token.OpenSquare)
res = parseQuantified(lexer, quantor)
elif lexer.TestTok(Token.OpenPar):
lexer.AcceptTok(Token.OpenPar)
res = parseFormula(lexer)
lexer.AcceptTok(Token.ClosePar)
elif lexer.TestTok(Token.Negation):
lexer.AcceptTok(Token.Negation)
subform = parseUnitaryFormula(lexer)
res = Formula("~", subform, None)
else:
lit = parseLiteral(lexer)
res = Formula("", lit, None)
return res
def parseAssocFormula(lexer, head):
"""
Parse the rest of the associative formula that starts with head
and continues ([&|] form *).
"""
op = lexer.LookLit()
while lexer.TestLit(op):
lexer.AcceptLit(op)
next = parseUnitaryFormula(lexer)
head = Formula(op, head, next)
return head
def parseFormula(lexer):
"""
Parse a (naked) formula.
"""
res = parseUnitaryFormula(lexer)
if lexer.TestTok([Token.Or, Token.And]):
res = parseAssocFormula(lexer, res)
elif lexer.TestTok([Token.Nor, Token.Nand, Token.Equiv,
Token.Xor, Token.Implies, Token.BImplies]):
op = lexer.LookLit()
lexer.Next()
rest = parseUnitaryFormula(lexer)
res = Formula(op, res, rest)
return res
class WFormula(Derivable):
"""
Datatype for the complete first-order formula, including
meta-information like type and name.
"""
def __init__(self, formula, type="plain", name=None):
"""
Constructor, takes formula, type, and optional name.
"""
self.formula = formula
self.type = type
Derivable.__init__(self, name)
def __repr__(self):
"""
Return a string representation of the formula.
"""
res = "fof(%s,%s,%s%s)."%(self.name, self.type,
repr(self.formula),self.strDerivation())
return res
def collectSig(self, sig = None):
"""
Collect formula signature.
"""
return self.formula.collectSig(sig)
def parseWFormula(lexer):
"""
Parse a formula in (slightly simplified) TPTP-3 syntax. It is
written
fof(<name>, <type>, <lformula>).
where <name> is a lower-case ident, type is a lower-case ident
from a specific list, and <lformula> is a Formula.
For us, all clause types are essentially the same, so we only
distinguish "axiom", "conjecture", and "negated_conjecture", and
map everything else to "plain".
"""
lexer.AcceptLit("fof");
lexer.AcceptTok(Token.OpenPar)
name = lexer.LookLit()
lexer.AcceptTok([Token.IdentLower,Token.SQString])
lexer.AcceptTok(Token.Comma)
type = lexer.LookLit()
if not type in ["axiom", "conjecture", "negated_conjecture"]:
type = "plain"
lexer.AcceptTok(Token.IdentLower)
lexer.AcceptTok(Token.Comma)
form = parseFormula(lexer)
lexer.AcceptTok(Token.ClosePar)
lexer.AcceptTok(Token.FullStop)
res = WFormula(form, type, name)
res.setInputDeriv(lexer.getName(), name)
return res
def negateConjecture(wform):
"""
If wform is a conjecture, return its negation. Otherwise
return it unchanged.
"""
if wform.type == "conjecture":
negf = Formula("~", wform.formula)
negw = WFormula(negf, "negated_conjecture")
negw.setDerivation(flatDerivation("assume_negation",\
[wform],\
"status(cth)"))
return negw
else:
return wform
# ------------------------------------------------------------------
# Unit test section
# ------------------------------------------------------------------
class TestFormulas(unittest.TestCase):
"""
Unit test class for clauses. Test clause and literal
functionality.
"""
def setUp(self):
"""
Setup function for clause/literal unit tests. Initialize
variables needed throughout the tests.
"""
print()
self.nformulas = """
![X]:(p(X) | ~a=b)
(![X]:a(X)|b(X)|?[X,Y]:(p(X,f(Y))))<=>q(g(a),X)
((((![X]:a(X))|b(X))|(?[X]:(?[Y]:p(X,f(Y)))))<=>q(g(a),X))
![X]:(p(X) | ~q(X))
"""
self.wformulas = """
fof(small, axiom, ![X]:(p(X) | ~a=b)).
fof(complex, conjecture, (![X]:r(X)|p(X)|?[X,Y]:(p(X,f(Y))))<=>q(g(a),X)).
fof(clean, conjecture,
((((![X]:p(X))|r(X))|(?[X]:(?[Y]:p(X,f(Y)))))<=>q(g(a),X))).
fof(weird, weird, ![X]:(p(X) | ~q(X))).
"""
def testNakedFormula(self):
"""
Test that basic parsing and functionality works.
"""
lex = Lexer(self.nformulas)
f1 = parseFormula(lex)
print("f1:", f1)
f2 = parseFormula(lex)
print("f2:", f2)
f3 = parseFormula(lex)
print("f3:", f3)
f4 = parseFormula(lex)
print("f4:", f4)
self.assertTrue(f2.isEqual(f3))
self.assertTrue(f3.isEqual(f2))
self.assertTrue(not f1.isEqual(f2))
self.assertTrue(not f2.isEqual(f1))
self.assertTrue(not f1.isEqual(f4))
self.assertEqual(f1.collectFreeVars(), set())
self.assertEqual(f2.collectFreeVars(), set(["X"]))
self.assertEqual(f3.collectFreeVars(), set(["X"]))
self.assertEqual(f1.collectVars(), set(["X"]))
self.assertEqual(f2.collectVars(), set(["X","Y"]))
self.assertEqual(f3.collectVars(), set(["X","Y"]))
self.assertTrue(f1.isQuantified())
self.assertTrue(not f2.isQuantified())
self.assertTrue(not f3.isQuantified())
self.assertTrue(not f1.hasSubform1())
self.assertTrue(f2.hasSubform1())
self.assertTrue(f3.hasSubform1())
self.assertTrue(f1.hasSubform2())
self.assertTrue(f2.hasSubform2())
self.assertTrue(f3.hasSubform2())
def testPropositional(self):
"""
Check propositional literal recognition and manipulation.
"""
lex = Lexer("$true $false $true|$false ")
t = parseFormula(lex)
f = parseFormula(lex)
c = parseFormula(lex)
self.assertTrue(t.isPropConst(True))
self.assertTrue(not t.isPropConst(False))
self.assertTrue(f.isPropConst(False))
self.assertTrue(not f.isPropConst(True))
self.assertTrue(not c.isPropConst(True))
self.assertTrue(not c.isPropConst(False))
def testCollectOps(self):
"""
Test if operator and funtion symbol collection works.
"""
lex = Lexer("a&(b|~c) "\
"(a=>b)<=(c<=>?[X]:p(X))"\
"(a~&(b<~>(c=>d)))~|![Y]:e(Y)")
f = parseFormula(lex)
self.assertEqual(f.collectOps(), set(["", "&", "|", "~"]))
self.assertEqual(f.collectFuns(), set(["a", "b", "c"]))
f = parseFormula(lex)
self.assertEqual(f.collectOps(), set(["", "=>", "<=", "?", "<=>"]))
self.assertEqual(f.collectFuns(), set(["a", "b", "c", "p"]))
f = parseFormula(lex)
self.assertEqual(f.collectOps(),
set(["", "~&", "~|", "!", "<~>", "=>"]))
self.assertEqual(f.collectFuns(), set(["a", "b", "c", "d", "e"]))
def testCNFTest(self):
"""
Check the CNF test.
"""
lex = Lexer("""
a=>b
a & (a|(b=>c))
![X]:((a|b)&c)
![X]:![Y]:((a|b)&(?[X]:c(X)))
![X]:(a & (a|b|c) & (a|b|c|d))
""")
expected = [False, False, True, False, True]
while not lex.TestTok(Token.EOFToken):
f = parseFormula(lex)
res = expected.pop(0)
self.assertEqual(f.isCNF(), res)
def testSplitter(self):
"""
Test splitting of conjunctions/disjunktions.
"""
lex = Lexer("""
a=>b
a & (a|(b=>c))
![X]:((a|b)&c)
![X]:![Y]:((a|b)&(?[X]:c(X)))
![X]:(a & (a|b|c) & (a|b|c|d))
a|(b&c)|(b<=>d)|![X]:p(X)
""")
cexpected = [1, 2, 2, 2, 3, 1]
dexpected = [1, 1, 1, 1, 1, 4]
while not lex.TestTok(Token.EOFToken):
f = parseFormula(lex)
f = f.getMatrix()
cs = f.conj2List()
res = cexpected.pop(0)
self.assertEqual(len(cs),res)
ds = f.disj2List()
res = dexpected.pop(0)
self.assertEqual(len(ds),res)
def testWrappedFormula(self):
"""
Test basic parsing and output of wrapped formulae.
"""
lex = Lexer(self.wformulas)
f1 = parseWFormula(lex)
print(f1)
f2 = parseWFormula(lex)
print(f2)
f3 = parseWFormula(lex)
print(f3)
f4 = parseWFormula(lex)
print(f4)
toggleDerivationOutput()
print(f1)
print(f2)
print(f3)
print(f4)
toggleDerivationOutput()
tmp = negateConjecture(f2)
print(tmp)
sig = f1.collectSig()
f2.collectSig(sig)
f3.collectSig(sig)
f4.collectSig(sig)
print(sig)
sig.isPred("q")
sig.isPred("r")
sig.isFun("a")
sig.isConstant("a")
if __name__ == '__main__':
unittest.main()