-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathderivations.py
executable file
·306 lines (265 loc) · 9.11 KB
/
derivations.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
#!/usr/bin/env python3
# ----------------------------------
#
# Module derivations.py
"""
A datatype for representing derivations, i.e. jusifications for
clauses and formulas. Derivations are recursively defined: A
derivation can be the trivial derivation (the clause or formula is
read directly from the input), or it consists of an operator (the
inference rule) and a list of parents.
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
class Derivable(object):
"""
This class represents "derivable" objects. Derivable objects have
a name and a justification. Names can be generated
automatically. They are not strictly required to be different for
different objects, but will usually be (this makes live easier for
users). Derivable objects will typically be logical formulas,
either full FOF formulas, or clauses.
"""
derivedIdCounter = 0
"""
Counter for generating new clause names.
"""
printDerivation = False
"""
Indicate if derivations shouldbe printed as part of Derivable
objects. It's up to the concrete classes to support this.
"""
def __init__(self, name=None, derivation = None):
"""
Initialize the object..
"""
self.setName(name)
self.derivation = derivation
self.refCount = 0
def __repr__(self):
return self.name
def setName(self, name = None):
"""
Set the name. If no name is given, generate a default name.
"""
if name:
self.name = name
else:
self.name = "c%d"%(Derivable.derivedIdCounter,)
Derivable.derivedIdCounter=Derivable.derivedIdCounter+1
def setDerivation(self, derivation):
"""
Set the derivation that created this derivable.
"""
self.derivation = derivation
def setInputDeriv(self, filename, name):
"""
Special case: It's an input object.
"""
self.derivation = Derivation("file('%s', %s)"%(filename,name))
def getParents(self):
"""
Return a list of all ancestors of this node in the derivation
graph.
"""
if self.derivation:
return self.derivation.getParents()
else:
return []
def incRefCount(self):
"""
Increase reference counter (counts virtual edges in the
derivation graph coming from the children).
"""
self.refCount = self.refCount+1
def decRefCount(self):
"""
See above.
"""
self.refCount = self.refCount-1
def strDerivation(self):
"""
If printing of derivations is enabled, return a string
representartion suitable as part of TPTP-3 output. Otherwise
return the empty string.
"""
if not self.derivation:
return ""
if Derivable.printDerivation:
return ","+repr(self.derivation)
return ""
def annotateDerivationGraph(self):
"""
Compute and set the number of virtual edges in all descendents
of self. The root node has one "virtual" edge.
"""
if self.refCount == 0:
parents = self.getParents()
for p in parents:
p.annotateDerivationGraph()
self.incRefCount()
def linearizeDerivation(self, res = None):
"""
Return linearized derivation.
"""
if res == None:
res = list()
self.decRefCount()
if self.refCount==0:
res.append(self)
parents = self.getParents()
for p in parents:
p.linearizeDerivation(res)
return res
def orderedDerivation(self):
self.annotateDerivationGraph()
res = self.linearizeDerivation()
res.reverse()
return res
def enableDerivationOutput():
Derivable.printDerivation = True
def disableDerivationOutput():
Derivable.printDerivation = False
def toggleDerivationOutput():
Derivable.printDerivation = not Derivable.printDerivation
class Derivation(object):
"""
A derivation object. A derivation is either trivial ("input"), a
reference to an existing Derivable object ("reference"), or an
inference with a list of premises.
"""
def __init__(self, operator, parents=None, status="status(thm)"):
"""
Initialize a derivation object with the operator and a list
of parents (which can be Derivations or, in the case of
"reference", Derivables).
"""
self.operator = operator
self.parents = parents
self.status = status
def __repr__(self):
"""
Return a string for the derivation in TPTP-3 format.
"""
if self.operator.startswith("file("):
return self.operator
elif self.operator.startswith("theory("):
return self.operator
elif self.operator == "reference":
assert(len(self.parents)==1)
return self.parents[0].name
else:
return "inference(%s,[%s],%s)"%\
(self.operator, self.status, repr(self.parents))
def getParents(self):
"""
Return a list of all derived objects that are used in this
derivation.
"""
if self.operator.startswith("file("):
return []
elif self.operator.startswith("theory("):
return []
elif self.operator == "reference":
assert(len(self.parents)==1)
return self.parents
else:
res = []
for p in self.parents:
res.extend(p.getParents())
return res
def flatDerivation(operator, parents, status="status(thm)"):
"""
Simple convenience function: Create a derivation which directly
references all parents.
"""
parentlist = [Derivation("reference", [p]) for p in parents]
return Derivation(operator, parentlist, status)
class TestDerivations(unittest.TestCase):
"""
"""
def setUp(self):
print()
def testDerivable(self):
"""
Test basic properties of derivations.
"""
o1 = Derivable()
o2 = Derivable()
o3 = Derivable()
o3.setDerivation(flatDerivation("resolution", [o1, o2]))
self.assertEqual(o1.getParents(),[])
self.assertEqual(o2.getParents(),[])
self.assertEqual(len(o3.getParents()), 2)
print(o3)
print(o3.derivation)
o3.setDerivation(flatDerivation("factor", [o1]))
print(o3.derivation)
self.assertEqual(len(o3.getParents()), 1)
def testProofExtraction(self):
"""
Test basic proof extraction.
"""
o1 = Derivable()
o2 = Derivable()
o3 = Derivable()
o4 = Derivable()
o5 = Derivable()
o6 = Derivable()
o7 = Derivable()
o1.setDerivation(Derivation("theory(equality)"))
print(repr(o1.derivation))
o2.setDerivation(Derivation("file('fake', fake')"))
o3.setDerivation(flatDerivation("factor", [o1]))
o4.setDerivation(flatDerivation("factor", [o3]))
o5.setDerivation(flatDerivation("resolution", [o1,o2]))
o6.setDerivation(Derivation("reference", [o5]))
o7.setDerivation(flatDerivation("resolution", [o5,o1]))
proof = o7.orderedDerivation()
print(proof)
self.assertEqual(len(proof),4)
self.assertTrue(o1 in proof)
self.assertTrue(o2 in proof)
self.assertTrue(o5 in proof)
self.assertTrue(o7 in proof)
def testOutput(self):
"""
Test derivation output functions.
"""
o1 = Derivable()
o2 = Derivable()
o3 = Derivable()
o4 = Derivable()
o1.setDerivation(Derivation("theory(equality)"))
o2.setDerivation(Derivation("input"))
o3.setDerivation(flatDerivation("resolution", [o1, o2]))
enableDerivationOutput()
self.assertTrue(o2.strDerivation()!="")
self.assertTrue(o3.strDerivation()!="")
self.assertTrue(o4.strDerivation()=="")
disableDerivationOutput()
self.assertTrue(o3.strDerivation()=="")
self.assertTrue(o4.strDerivation()=="")
toggleDerivationOutput()
self.assertTrue(o3.strDerivation()!="")
self.assertTrue(o4.strDerivation()=="")
if __name__ == '__main__':
unittest.main()