-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrule.py
272 lines (220 loc) · 6.6 KB
/
rule.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
# Copyright (C) 2023 IBM Corp.
# SPDX-License-Identifier: Apache-2.0
from abc import abstractclassmethod
from . import error, util
from .expression import Term
from .sequent import Sequent, _Sequent
from .settings import Settings
__all__ = [
'DerivedRule',
'PrimitiveRule',
'Rule',
'RuleError',
]
class RuleError(error.Error):
"""Raised on rule application errors.
Parameters:
rule: :class:`Rule`.
reason: Message.
Returns:
A new :class:`RuleError`.
"""
def __init__(self, rule, reason):
super().__init__(f'{rule.__name__}: {reason}')
self.rule = rule
self.reason = reason
class Rule(Sequent):
"""Abstract base class for rules."""
def __init_subclass__(cls, **kwargs):
if 'test' not in cls.__dict__:
setattr(cls, 'test', classmethod(
lambda x, arg: (
Sequent.test(arg)
and x._test(*arg._unpack_sequent()))))
@classmethod
def test(cls, arg):
"""Tests whether `arg` is an instance of this rule.
Parameters:
arg: Value.
Returns:
``True`` if successful; ``False`` otherwise.
"""
return util.any_map(lambda x: x.test(arg), cls.__subclasses__())
@abstractclassmethod
def _test(cls, hs, c):
raise NotImplementedError
@classmethod
def error(cls, msg):
"""Returns a new :class:`RuleError` with `msg`.
Parameters:
msg: Message.
Returns:
A new :class:`RuleError`.
"""
return RuleError(cls, msg)
@classmethod
def assert_can_apply(cls, arg1, arg2):
"""Checks whether `arg1` can be applied to `arg2`.
Parameters:
arg1: :class:`Term`.
arg2: :class:`Term`.
Raises:
RuleError: `arg1` cannot be applied to `arg2`.
"""
if arg1.type.is_function_type():
dom, _ = arg1.type.unpack_function_type()
if dom == arg2.type:
return True
raise cls.error(f"cannot apply '{arg1}' to '{arg2}'")
@classmethod
def assert_equal(cls, arg1, arg2):
"""Checks whether `arg1` and `arg2` are (alpha) equal.
Parameters:
arg1: :class:`Term`.
arg2: :class:`Term`.
Raises:
RuleError: `arg1` and `arg2` are not (alpha) equal.
"""
if arg1 == arg2:
return True
raise cls.error(f"'{arg1}' and '{arg2}' are not (alpha) equal")
_assertions = {
'variable': (
lambda x: x.is_variable(),
lambda x: x._unpack_variable(),
lambda x: f"'{x}' is not a variable",
),
'constant': (
lambda x: x.is_constant(),
lambda x: x._unpack_constant(),
lambda x: f"'{x}' is not a constant",
),
'application': (
lambda x: x.is_application(),
lambda x: x._unpack_application(),
lambda x: f"'{x}' is not an application",
),
'beta_redex': (
lambda x: x.is_beta_redex(),
lambda x: x._unpack_beta_redex(),
lambda x: f"'{x}' is not a beta-redex",
),
'abstraction': (
lambda x: x.is_abstraction(),
lambda x: x._unpack_abstraction(),
lambda x: f"'{x}' is not an abstraction",
),
'formula': (
lambda x: x.is_formula(),
lambda x: x._unpack_formula(),
lambda x: f"'{x}' is not a formula",
),
'equal': (
lambda x: x.is_equal(),
lambda x: x._unpack_equal(),
lambda x: f"'{x}' is not an equation",
),
'truth': (
lambda x: x.is_truth(),
lambda x: x._unpack_truth(),
lambda x: f"'{x}' is not truth",
),
'implies': (
lambda x: x.is_implies(),
lambda x: x._unpack_implies(),
lambda x: f"'{x}' is not an implication",
),
'iff': (
lambda x: x.is_iff(),
lambda x: x._unpack_iff(),
lambda x: f"'{x}' is not an equivalence",
),
'exists': (
lambda x: x.is_exists(),
lambda x: x._unpack_exists(),
lambda x: f"'{x}' is not an existential",
),
'forall': (
lambda x: x.is_forall(),
lambda x: x._unpack_forall(),
lambda x: f"'{x}' is not a universal",
)
}
for name, (f, g, h) in _assertions.items():
a_suffix = h("")[10:]
suffix = a_suffix[3:] if a_suffix.startswith('an ') else a_suffix[2:]
def mk_assert_(f, h):
def assert_(cls, arg):
status = f(arg)
if not status:
raise cls.error(h(arg))
return status
return assert_
assert_ = mk_assert_(f, h)
assert_.__doc__ = f"""\
Checks whether `arg` is {a_suffix}.
Parameters:
arg: Value.
Raises:
RuleError: `arg` is not {a_suffix}.
"""
setattr(Rule, 'asserted_is_' + name, classmethod(assert_))
def mk_unpack_(assert_, g):
def unpack_(cls, arg):
return assert_(cls, arg) and g(arg)
return unpack_
unpack_ = mk_unpack_(assert_, g)
unpack_.__doc__ = f"""\
Unpacks {suffix} `arg`.
Parameters:
arg: Value.
Returns:
`arg`'s arguments unpacked.
Raises:
RuleError: `arg` is not {a_suffix}.
"""
setattr(
Rule, 'asserted_unpack_' + name,
classmethod(unpack_))
class PrimitiveRule(Rule):
"""Abstract base class for primitive rules."""
def __new__(cls, *args, **kwargs):
annotations = kwargs.pop('annotations', dict())
seq = _Sequent(*cls._new(*args, **kwargs), **annotations)
if cls._thy().settings.record_proofs:
setattr(seq, '_proof', (cls, args))
return seq
@abstractclassmethod
def _new(cls, *args, **kwargs):
raise NotImplementedError
@classmethod
def test(cls, arg):
return super().test(arg)
class RuleAxiom(PrimitiveRule):
r"""Axiom introduction.
(Internal: Not intended for direct use.)
Parameters:
arg1 (:class:`Formula`): :math:`p`.
kwargs: Annotations.
Returns:
:class:`Sequent`:
:math:`⊢ p`.
"""
@classmethod
def _new( # (form,)
cls, arg1, **kwargs):
return set(), arg1
@classmethod
def _test(cls, hs, c):
return not hs
class DerivedRule(Rule):
"""Abstract base class for derived rules."""
def __new__(cls, *args, **kwargs):
annotations = kwargs.pop('annotations', dict())
return cls._new(*args, **kwargs)@annotations
@abstractclassmethod
def _new(cls, *args, **kwargs):
raise NotImplementedError
@classmethod
def test(cls, arg):
return super().test(arg)