Repository navigation
Expand file tree
/
Copy pathinference_rules.py
More file actions
189 lines (150 loc) · 6.14 KB
/
Copy pathinference_rules.py
File metadata and controls
189 lines (150 loc) · 6.14 KB
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
from dataclasses import dataclass, field
from enum import Enum
from parser import NK, Formula, make_formula
from sequent import FormulaSlot, Sequent, make_slot
class Rules(Enum):
IDENTITY = "identity"
AND_LEFT = "and_left"
AND_RIGHT = "and_right"
OR_LEFT = "or_left"
OR_RIGHT = "or_right"
IMPLIES_LEFT = "implies_left"
IMPLIES_RIGHT = "implies_right"
NOT_LEFT = "not_left"
NOT_RIGHT = "not_right"
EXISTS_LEFT = "exists_left"
EXISTS_RIGHT = "exists_right"
FORALL_LEFT = "forall_left"
FORALL_RIGHT = "forall_right"
TRUE_LEFT = "true_left"
FALSE_RIGHT = "false_right"
ROOT = "root"
@dataclass
class ProofNode:
"""A node in the proof search tree. Records which rule was applied,
which children were explored, and whether the branch closed."""
sequent: Sequent
rule: Rules | None = None
branches: list["ProofNode"] = field(default_factory=list)
closed: bool = False
def substitute(node: Formula, subst: dict[str, Formula]) -> Formula:
"""Substitution that avoids capturing bound variables.
Dict keys are variable names to replace and values are the formulas
to substitute in.
"""
if node.kind == NK.PREDICATE:
return make_formula(
node.kind,
node.value,
tuple(substitute(c, subst) for c in node.children),
)
if node.kind == NK.VARIABLE:
return subst.get(node.value, node)
if node.kind in (NK.FORALL, NK.EXISTS):
inner = {k: v for k, v in subst.items() if k != node.value}
return make_formula(
node.kind, node.value, (substitute(node.children[0], inner),)
)
if node.kind == NK.NOT:
return make_formula(
node.kind, node.value, (substitute(node.children[0], subst),)
)
if node.children:
return make_formula(
node.kind,
node.value,
tuple(substitute(c, subst) for c in node.children),
)
return node
def is_identity(sequent: Sequent) -> bool:
"""Checks if a branch can be closed by an axiom rule."""
if any(s.formula.kind == NK.TRUE for s in sequent.rhs):
return True
if any(s.formula.kind == NK.FALSE for s in sequent.lhs):
return True
rhs_set = {s.formula for s in sequent.rhs}
return any(s.formula in rhs_set for s in sequent.lhs)
def and_left(slot: FormulaSlot, sequent: Sequent):
f = slot.formula
new_seq = sequent.replace(
lhs_remove=slot,
lhs_add=(make_slot(f.children[0]), make_slot(f.children[1])),
)
return (ProofNode(new_seq, rule=Rules.AND_LEFT),)
def and_right(slot: FormulaSlot, sequent: Sequent):
f = slot.formula
s1 = sequent.replace(rhs_remove=slot, rhs_add=(make_slot(f.children[0]),))
s2 = sequent.replace(rhs_remove=slot, rhs_add=(make_slot(f.children[1]),))
return (
ProofNode(s1, rule=Rules.AND_RIGHT),
ProofNode(s2, rule=Rules.AND_RIGHT),
)
def or_left(slot: FormulaSlot, sequent: Sequent):
f = slot.formula
s1 = sequent.replace(lhs_remove=slot, lhs_add=(make_slot(f.children[0]),))
s2 = sequent.replace(lhs_remove=slot, lhs_add=(make_slot(f.children[1]),))
return (
ProofNode(s1, rule=Rules.OR_LEFT),
ProofNode(s2, rule=Rules.OR_LEFT),
)
def or_right(slot: FormulaSlot, sequent: Sequent):
f = slot.formula
new_seq = sequent.replace(
rhs_remove=slot,
rhs_add=(make_slot(f.children[0]), make_slot(f.children[1])),
)
return (ProofNode(new_seq, rule=Rules.OR_RIGHT),)
def implies_left(slot: FormulaSlot, sequent: Sequent):
f = slot.formula
s1 = sequent.replace(lhs_remove=slot, rhs_add=(make_slot(f.children[0]),))
s2 = sequent.replace(lhs_remove=slot, lhs_add=(make_slot(f.children[1]),))
return (
ProofNode(s1, rule=Rules.IMPLIES_LEFT),
ProofNode(s2, rule=Rules.IMPLIES_LEFT),
)
def implies_right(slot: FormulaSlot, sequent: Sequent):
f = slot.formula
new_seq = sequent.replace(
rhs_remove=slot,
lhs_add=(make_slot(f.children[0]),),
rhs_add=(make_slot(f.children[1]),),
)
return (ProofNode(new_seq, rule=Rules.IMPLIES_RIGHT),)
def not_left(slot: FormulaSlot, sequent: Sequent):
f = slot.formula
new_seq = sequent.replace(lhs_remove=slot, rhs_add=(make_slot(f.children[0]),))
return (ProofNode(new_seq, rule=Rules.NOT_LEFT),)
def not_right(slot: FormulaSlot, sequent: Sequent):
f = slot.formula
new_seq = sequent.replace(rhs_remove=slot, lhs_add=(make_slot(f.children[0]),))
return (ProofNode(new_seq, rule=Rules.NOT_RIGHT),)
def true_left(slot: FormulaSlot, sequent: Sequent):
return (ProofNode(sequent.replace(lhs_remove=slot), rule=Rules.TRUE_LEFT),)
def false_right(slot: FormulaSlot, sequent: Sequent):
return (ProofNode(sequent.replace(rhs_remove=slot), rule=Rules.FALSE_RIGHT),)
def exists_left(slot: FormulaSlot, sequent: Sequent, fresh_var: Formula):
f = slot.formula
substituted = substitute(f.children[0], {f.value: fresh_var})
new_seq = sequent.replace(lhs_remove=slot, lhs_add=(make_slot(substituted),))
return (ProofNode(new_seq, rule=Rules.EXISTS_LEFT),)
def forall_right(slot: FormulaSlot, sequent: Sequent, fresh_var: Formula):
f = slot.formula
substituted = substitute(f.children[0], {f.value: fresh_var})
new_seq = sequent.replace(rhs_remove=slot, rhs_add=(make_slot(substituted),))
return (ProofNode(new_seq, rule=Rules.FORALL_RIGHT),)
def forall_left(slot: FormulaSlot, sequent: Sequent, term: Formula, term_name: str):
f = slot.formula
substituted = substitute(f.children[0], {f.value: term})
updated = make_slot(f, slot.used | {term_name})
new_seq = sequent.replace(
lhs_remove=slot, lhs_add=(updated, make_slot(substituted))
)
return (ProofNode(new_seq, rule=Rules.FORALL_LEFT),)
def exists_right(slot: FormulaSlot, sequent: Sequent, term: Formula, term_name: str):
f = slot.formula
substituted = substitute(f.children[0], {f.value: term})
updated = make_slot(f, slot.used | {term_name})
new_seq = sequent.replace(
rhs_remove=slot, rhs_add=(updated, make_slot(substituted))
)
return (ProofNode(new_seq, rule=Rules.EXISTS_RIGHT),)