-
Notifications
You must be signed in to change notification settings - Fork 0
/
verifier.py
233 lines (202 loc) · 7.36 KB
/
verifier.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
#!/usr/bin/env python
from collections import deque
'''
Verifies transformational proofs in George syntax.
Code is currently very ugly, it would be appreciated if people can help clean/bugfix.
'''
'''
Copyright (c) 2015 Filip Burlacu
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
'''
# parse a string containing just the Transformational Proof
proof = """
! (r => ! ((p | r) & (r | q))) <-> r
%% blink
1) ! (r => ! ((p | r) & (r | q))) %% waggle
2) ! (r => ! ((r | p) & (r | q))) by comm
3) ! (r => ! (r | (p & q))) by distr
4) !(! r | ! (r | (p & q))) by impl
%% snout
5) !(! r | ! r & ! (p & q)) by dm
6) !!r by simp2
7) r by neg
"""
precedence = { # all ops are right-associative
'=' : 1,
'>' : 2,
'|' : 3,
'&' : 4,
'!' : 5, # highest precedence, simple as that
}
ops = set(precedence.keys())
def tokenize(s):
'''
tokenize an expression
'''
# Woefully inefficient multi-pass design, but it's simple and it works
# We'd use re if we were doing giant lines, obv
s = s.replace('(', ' ( ')
s = s.replace(')', ' ) ')
s = s.replace('|', ' | ')
s = s.replace('&', ' & ')
s = s.replace('!', ' ! ')
s = s.replace('<=>', ' = ') # note the change of symbol
s = s.replace('=>', ' > ') # so all operators
s = s.split() # s is now tokenized!
return s
def lex(proof):
proof = proof.split("\n")
for i in xrange(len(proof)):
proof[i] = proof[i].split("%")[0] # remove comments before removing empty lines
proof = filter(lambda s: s != "", proof) # remove empty lines
proof[0] = proof[0].split('<->')
# remove labels in front of proof lines:
proof[1:] = (s.split(')', 1)[-1] for s in proof[1:])
proof[1] = [proof[1]] # listify
proof[2:]= (s.split('by') for s in proof[2:]) # each line is now a pair [expr, rule]
for a in proof:
a[:] = (s.strip() for s in a)
a[0] = tokenize(a[0])
proof[0][1] = tokenize(proof[0][1]) # The only expr left out by above loop
# And now we rearrange it for easier interpretation:
proof[1].append('premise')
temp = zip(*proof[1:])
out = {'target' : proof[0], 'steps' : temp[0], 'rules' : temp[1]}
return out
def prefix(s):
'''
Does the shunting-yarding of a tokenized expr, returning the prefix-notation form
'''
out = []
stack = []
s = deque(s)
while s:
tok = s.popleft()
if tok.isalpha():
out.append(tok)
continue
if tok is '(':
stack.append(tok)
continue
if tok in ops:
while stack and stack[-1] in ops and precedence[tok] < precedence[stack[-1]]:
out.append(stack.pop())
stack.append(tok)
if tok is ')':
while stack and stack[-1] != '(':
out.append(stack.pop())
if not stack:
print "MISHMASHED PARENTS"
return False
stack.pop() # pop '('
while stack:
tok = stack.pop()
if tok is '(':
print "MISHMASHED PARENTS"
return False
out.append(tok)
out.reverse() # because it makes a world of difference
return out
def diffPN(t1,t2):
'Return the first differing index of two trees in prefix notation'
for i in xrange(len(t1)):
if t1[i] != t2[i]: return i
return -1
def childPN(parent, i):
'returns the child tree starting at the specified index of a prefix tree'
count = 1
for j in xrange(i, len(parent)):
tok = parent[j]
if tok is '!':
count += 0
elif tok in '&|>=':
count += 1
else:
count -= 1
if count == 0:
return [e for e in parent[i:j+1]]
raise Exception("Bad Syntax, Cannot get child of ", parent, "at", i)
'''
Basic method for testing if a rule was applied correctly:
- Diff the two expressions, grab the differing subtrees
- With these two subtrees, pattern-match the first and second layers of children
against the listed rule
'''
def testRule(t1, t2, rule):
i = diffPN(t1, t2)
# [op0, *c_1, *c_2] where op0 is the same but c_1[0] is different
op0 = t1[i - 1]
c11 = childPN(t1, i)
c21 = childPN(t2, i)
# Now to choose the rule:
if rule is "comm":
if i <= 0: return False # the trees cannot be completely different
c12 = childPN(t1, i + len(c11))
c22 = childPN(t2, i + len(c21))
return op0 in '&|=' and c11 == c22 and c12 == c21
if rule is 'contr': # should probably work on factoring this out and rerouting
if c11[0] is 'false':
if c21[0] != '&': return False
left = childPN(c21, 1)
right = childPN(c21, 1 + len(left))
if left[0] is '!' and left[1:] == right: return True
if right[0] is '!' and right[1:] == left: return True
if c21[0] is 'false':
if c11[0] != '&': return False
left = childPN(c11, 1)
right = childPN(c11, 1 + len(left))
if left[0] is '!' and left[1:] == right: return True
if right[0] is '!' and right[1:] == left: return True
return False
if rule is 'lem': # almost exactly the same as contr
if c11[0] is 'true':
if c21[0] != '|': return False
left = childPN(c21, 1)
right = childPN(c21, 1 + len(left))
if left[0] is '!' and left[1:] == right: return True
if right[0] is '!' and right[1:] == left: return True
if c21[0] is 'true':
if c11[0] != '|': return False
left = childPN(c11, 1)
right = childPN(c11, 1 + len(left))
if left[0] is '!' and left[1:] == right: return True
if right[0] is '!' and right[1:] == left: return True
return False
proof = lex(proof)
'''
print proof['target']
for i in proof['steps']:
r = prefix(i)
print " ".join(i) + " ~~ " + " ".join(r)
print proof['rules']'''
#print RPN(proof['steps'][0])
print " ".join(proof['steps'][0])
pn = prefix(proof['steps'][0])
for i in xrange(len(pn)):
print childPN(pn, i)
def test(t1, t2, rule):
test1 = prefix(tokenize(t1))
test2 = prefix(tokenize(t2))
print testRule(test1, test2, rule)
test("a & b", "b & a", 'comm')
test("a | b", "b | a", 'comm')
test("a = b", "b = a", 'comm')
test("a | b", "b & a", 'comm')
test("p & ! p", 'false', 'contr')
test("p & ! c", 'false', 'contr')
test("p | ! p", 'true', 'lem')
test("p | ! c", 'true', 'lem')