-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprover.py
208 lines (191 loc) · 6.9 KB
/
prover.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
from copy import deepcopy
from proposition import PropNode, then_symbol, and_symbol, or_symbol
def dn(prop: PropNode):
return PropNode("→", PropNode("→", prop, PropNode("False")), PropNode("False"))
class Prover:
def __init__(self, goal: PropNode):
self.goal = goal
self.variables = []
self.subgoals = [] # [(goal, variables), ...]
self.undos = []
def __set_undo(self):
self.undos.append(deepcopy((self.goal, self.variables, self.subgoals)))
def undo(self) -> bool:
if not self.undos:
return True
else:
(self.goal, self.variables, self.subgoals) = self.undos.pop()
return False
def assumption(self) -> bool:
if self.goal in self.variables:
self.__set_undo()
self.goal = None
if self.subgoals:
(self.goal, self.variables) = self.subgoals.pop()
return False
else:
return True
def intro(self) -> bool:
if self.goal.symbol == then_symbol:
self.__set_undo()
self.variables.append(self.goal.left)
self.goal = self.goal.right
return False
else:
return True
def apply(self, number: int) -> bool:
if len(self.variables) <= number:
return True
elif (self.variables[number].symbol == then_symbol
and self.variables[number].right == self.goal):
self.__set_undo()
self.goal = self.variables[number].left
return False
else:
return True
def split(self) -> bool:
if self.goal.symbol == and_symbol:
self.__set_undo()
self.subgoals.append((self.goal.right, self.variables))
self.goal = self.goal.left
return False
else:
return True
def left(self):
if self.goal.symbol == or_symbol:
self.__set_undo()
self.goal = self.goal.left
return False
else:
return True
def right(self):
if self.goal.symbol == or_symbol:
self.__set_undo()
self.goal = self.goal.right
return False
else:
return True
def destruct(self, number: int) -> bool:
if len(self.variables) <= number:
return True
elif self.variables[number].symbol == and_symbol:
self.__set_undo()
self.variables.append(self.variables[number].left)
self.variables.append(self.variables[number].right)
del self.variables[number]
return False
elif self.variables[number].symbol == or_symbol:
self.__set_undo()
temp = self.variables[:]
temp[number] = temp[number].right
self.subgoals.append((self.goal, temp))
self.variables[number] = self.variables[number].left
return False
elif self.variables[number].symbol == 'False':
self.__set_undo()
self.goal = None
if self.subgoals:
(self.goal, self.variables) = self.subgoals.pop()
return False
else:
return True
def specialize(self, function, domain) -> bool:
if len(self.variables) <= min(function, domain):
return True
elif (self.variables[function].symbol != "→"
or self.variables[function].left != self.variables[domain]):
return True
else:
self.__set_undo()
self.variables[function] = self.variables[function].right
return False
def add_dn(self):
self.__set_undo()
self.goal = dn(self.goal)
return False
def auto(self) -> [str]:
if not self.assumption():
if self.goal is None:
return ["assumption"]
if ans := self.auto():
return ["assumption"] + ans
self.undo()
if not self.intro():
if ans := self.auto():
return ["intro"] + ans
self.undo()
if not self.split():
if ans := self.auto():
return ["split"] + ans
self.undo()
if not self.left():
if ans := self.auto():
return ["left"] + ans
self.undo()
if not self.right():
if ans := self.auto():
return ["right"] + ans
self.undo()
for i in range(len(self.variables)):
if not self.destruct(i):
if self.goal is None:
return [f"destruct {i}"]
if ans := self.auto():
return [f"destruct {i}"] + ans
self.undo()
if not self.apply(i):
if ans := self.auto():
return [f"apply {i}"] + ans
self.undo()
for j in range(len(self.variables)):
if not self.specialize(i, j):
if ans := self.auto():
return [f"specialize {i} {j}"] + ans
self.undo()
return []
def auto_classical(self, depth_limit: int = 10) -> [str]:
if depth_limit <= 0:
return []
if not self.assumption():
if self.goal is None:
return ["assumption"]
if ans := self.auto_classical(depth_limit-1):
return ["assumption"] + ans
self.undo()
if not self.intro():
if ans := self.auto_classical(depth_limit-1):
return ["intro"] + ans
self.undo()
if not self.split():
if ans := self.auto_classical(depth_limit-1):
return ["split"] + ans
self.undo()
if not self.left():
if ans := self.auto_classical(depth_limit-1):
return ["left"] + ans
self.undo()
if not self.right():
if ans := self.auto_classical(depth_limit-1):
return ["right"] + ans
self.undo()
for i in range(len(self.variables)):
if not self.destruct(i):
if self.goal is None:
return [f"destruct {i}"]
if ans := self.auto_classical(depth_limit-1):
return [f"destruct {i}"] + ans
self.undo()
if not self.apply(i):
if ans := self.auto_classical(depth_limit-1):
return [f"apply {i}"] + ans
self.undo()
for j in range(len(self.variables)):
if not self.specialize(i, j):
if ans := self.auto_classical(depth_limit-1):
return [f"specialize {i} {j}"] + ans
self.undo()
if not self.add_dn():
if ans := self.auto_classical(depth_limit-1):
return ["add_dn"] + ans
self.undo()
return []