-
Notifications
You must be signed in to change notification settings - Fork 1
/
sequent.py
219 lines (196 loc) · 8.75 KB
/
sequent.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
from tape import ClauseTape
connectives = ['/\\', '\\/', '->', '~']
class Sequent:
left_tapes: list[ClauseTape]
right_tapes: list[ClauseTape]
chlidren: list
parent: object
is_calculated: bool
level: int
level_idx: int
size: int
space_before: int
def __init__(self, left_tapes=[], right_tapes=[], level=1, level_idx=1) -> None:
self.left_tapes = left_tapes
self.right_tapes = right_tapes
self.children = []
self.is_calculated = False
self.parent = None
self.level = level
self.level_idx = level_idx
self.space_before = 0
self.size = 0
def __str__(self) -> None:
left = ", ".join([str(t) for t in self.left_tapes])
right = ", ".join([str(t) for t in self.right_tapes])
return f"{left} => {right}"
def __repr__(self) -> str:
return self.__str__()
@classmethod
def init_by_formula(cls, formula) -> None:
tape = ClauseTape.init_by_formula(formula, connectives)
return cls(right_tapes=[tape])
def calculate(self) -> bool:
self.is_calculated = True
left_atomics, left_highway = self.get_atomics_and_highway(self.left_tapes)
right_atomics, right_highway = self.get_atomics_and_highway(self.right_tapes)
common_atomics = left_atomics.intersection(right_atomics)
if common_atomics:
return True
if left_highway < 0 and right_highway < 0:
return False
if left_highway >= 0:
return self.operate_on_highway(True, left_highway)
return self.operate_on_highway(False, right_highway)
def operate_on_highway(self, is_left, highway):
if is_left:
if self.left_tapes[highway].connective == "~":
return self.left_neg(highway)
elif self.left_tapes[highway].connective == "/\\":
return self.left_and(highway)
elif self.left_tapes[highway].connective == "\\/":
return self.left_or(highway)
else:
return self.left_imp(highway)
if self.right_tapes[highway].connective == "~":
return self.right_neg(highway)
elif self.right_tapes[highway].connective == "/\\":
return self.right_and(highway)
elif self.right_tapes[highway].connective == "\\/":
return self.right_or(highway)
else:
return self.right_imp(highway)
def left_neg(self, highway) -> bool:
tape1 = ClauseTape.init_by_formula(self.left_tapes[highway].subs[0], connectives)
ltapes1 = [t for i, t in enumerate(self.left_tapes) if i != highway]
rtapes1 = self.right_tapes.copy()
rtapes1.append(tape1)
child1 = Sequent(left_tapes=ltapes1, right_tapes=rtapes1, level=self.level+1, level_idx=self.level_idx)
self.add_child(child1)
return child1.calculate()
def right_neg(self, highway) -> bool:
tape1 = ClauseTape.init_by_formula(self.right_tapes[highway].subs[0], connectives)
ltapes1 = self.left_tapes.copy()
ltapes1.append(tape1)
rtapes1 = [t for i, t in enumerate(self.right_tapes) if i != highway]
child1 = Sequent(left_tapes=ltapes1, right_tapes=rtapes1, level=self.level+1, level_idx=self.level_idx)
self.add_child(child1)
return child1.calculate()
def left_and(self, highway) -> bool:
tape1 = ClauseTape.init_by_formula(self.left_tapes[highway].subs[0], connectives)
tape2 = ClauseTape.init_by_formula(self.left_tapes[highway].subs[1], connectives)
ltapes1 = [t for i, t in enumerate(self.left_tapes) if i != highway]
ltapes1 += [tape1, tape2]
rtapes1 = self.right_tapes.copy()
child1 = Sequent(left_tapes=ltapes1, right_tapes=rtapes1, level=self.level+1, level_idx=self.level_idx)
self.add_child(child1)
return child1.calculate()
def right_and(self, highway) -> bool:
tape1 = ClauseTape.init_by_formula(self.right_tapes[highway].subs[0], connectives)
tape2 = ClauseTape.init_by_formula(self.right_tapes[highway].subs[1], connectives)
ltapes1 = self.left_tapes.copy()
ltapes2 = self.left_tapes.copy()
rtapes1 = [t for i, t in enumerate(self.right_tapes) if i != highway]
rtapes2 = rtapes1.copy()
rtapes1.append(tape1)
rtapes2.append(tape2)
child1 = Sequent(left_tapes=ltapes1, right_tapes=rtapes1, level=self.level+1, level_idx=self.level_idx)
child2 = Sequent(left_tapes=ltapes2, right_tapes=rtapes2, level=self.level+1, level_idx=self.level_idx+1)
self.add_child(child1)
self.add_child(child2)
return child1.calculate() and child2.calculate()
def left_or(self, highway) -> bool:
tape1 = ClauseTape.init_by_formula(self.left_tapes[highway].subs[0], connectives)
tape2 = ClauseTape.init_by_formula(self.left_tapes[highway].subs[1], connectives)
ltapes1 = [t for i, t in enumerate(self.left_tapes) if i != highway]
ltapes2 = ltapes1.copy()
ltapes1.append(tape1)
ltapes2.append(tape2)
rtapes1 = self.right_tapes.copy()
rtapes2 = self.right_tapes.copy()
child1 = Sequent(left_tapes=ltapes1, right_tapes=rtapes1, level=self.level+1, level_idx=self.level_idx)
child2 = Sequent(left_tapes=ltapes2, right_tapes=rtapes2, level=self.level+1, level_idx=self.level_idx+1)
self.add_child(child1)
self.add_child(child2)
return child1.calculate() and child2.calculate()
def right_or(self, highway) -> bool:
tape1 = ClauseTape.init_by_formula(self.right_tapes[highway].subs[0], connectives)
tape2 = ClauseTape.init_by_formula(self.right_tapes[highway].subs[1], connectives)
ltapes1 = self.left_tapes.copy()
rtapes1 = [t for i, t in enumerate(self.right_tapes) if i != highway]
rtapes1 += [tape1, tape2]
child1 = Sequent(left_tapes=ltapes1, right_tapes=rtapes1, level=self.level+1, level_idx=self.level_idx)
self.add_child(child1)
return child1.calculate()
def left_imp(self, highway) -> bool:
tape1 = ClauseTape.init_by_formula(self.left_tapes[highway].subs[0], connectives)
tape2 = ClauseTape.init_by_formula(self.left_tapes[highway].subs[1], connectives)
ltapes1 = [t for i, t in enumerate(self.left_tapes) if i != highway]
ltapes2 = ltapes1.copy()
ltapes1.append(tape2)
rtapes1 = self.right_tapes.copy()
rtapes2 = self.right_tapes.copy()
rtapes2.append(tape1)
child1 = Sequent(left_tapes=ltapes1, right_tapes=rtapes1, level=self.level+1, level_idx=self.level_idx)
child2 = Sequent(left_tapes=ltapes2, right_tapes=rtapes2, level=self.level+1, level_idx=self.level_idx+1)
self.add_child(child1)
self.add_child(child2)
return child1.calculate() and child2.calculate()
def right_imp(self, highway) -> bool:
tape1 = ClauseTape.init_by_formula(self.right_tapes[highway].subs[0], connectives)
tape2 = ClauseTape.init_by_formula(self.right_tapes[highway].subs[1], connectives)
ltapes1 = self.left_tapes.copy()
ltapes1.append(tape1)
rtapes1 = [t for i, t in enumerate(self.right_tapes) if i != highway]
rtapes1.append(tape2)
child1 = Sequent(left_tapes=ltapes1, right_tapes=rtapes1, level=self.level+1, level_idx=self.level_idx)
self.add_child(child1)
return child1.calculate()
def cal_size(self):
if not self.children:
self.size = 1
return 1
s = 0
for ch in self.children:
s += ch.cal_size()
self.size = s
return s
def cal_children_space_before(self):
s = 0
for ch in self.children:
ch.space_before = self.space_before + s
s += ch.size
ch.cal_children_space_before()
def cal_max_length(self):
ml = len(str(self))
for ch in self.children:
l = ch.cal_max_length()
if l > ml:
ml = l
return ml
def add_child(self, child) -> None:
self.children.append(child)
child.parent = self
def get_level(self) -> int:
lv = 0
p = self.parent
while p:
lv += 1
p = p.parent
return lv
def print_tree(self) -> None:
spaces = " " * self.get_level() * 3
prefix = spaces + "=>" if self.parent else ""
print(prefix + self.left_tapes)
for child in self.children:
child.print_tree()
@staticmethod
def get_atomics_and_highway(tapes):
highway = -1
atomics = set()
for idx, tape in enumerate(tapes):
if tape.is_atomic:
atomics.add(tape.subs[0])
else:
highway = idx
return atomics, highway