Repository navigation
Expand file tree
/
Copy pathextension.py
More file actions
202 lines (154 loc) · 5.55 KB
/
Copy pathextension.py
File metadata and controls
202 lines (154 loc) · 5.55 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
190
191
192
193
194
195
196
197
198
199
200
201
202
# Copyright (C) 2023 IBM Corp.
# SPDX-License-Identifier: Apache-2.0
from . import error
from .object import Object
__all__ = [
'Assertion',
'Assumption',
'Extension',
'ExtensionError',
'NewAxiom',
'NewConstant',
'NewDefinition',
'NewPythonTypeAlias',
'NewTheorem',
'NewTypeConstructor',
'NewTypeSpec',
'Notation',
]
class ExtensionError(error.Error):
def __init__(self, ext, reason):
super().__init__(f'{ext.__class__.__name__}: {reason}')
self.extension = ext
self.reason = reason
class Extension(Object):
@property
def id(self):
return self.get_id()
def get_id(self):
return None
def error(self, msg):
raise ExtensionError(self, msg)
class Assumption(Extension):
pass
class NewTypeConstructor(Assumption):
def __init__(self, arg1, **kwargs):
super().__init__( # (tcons,)
arg1, **kwargs)
def _preprocess_arg(self, arg, i):
arg = super()._preprocess_arg(arg, i)
if i == 1: # tcons
return self.TypeConstructor.check(
arg, self.__class__.__name__, None, i)
else:
error.should_not_get_here()
def get_id(self):
return self[0].id
class NewConstant(Assumption):
def __init__( # (const,)
self, arg1, **kwargs):
super().__init__(arg1, **kwargs)
def _preprocess_arg(self, arg, i):
arg = super()._preprocess_arg(arg, i)
if i == 1: # const
return self.Constant.check(
arg, self.__class__.__name__, None, i)
else:
error.should_not_get_here()
def get_id(self):
return self[0].id
class NewAxiom(Assumption):
def __init__( # (const, form)
self, arg1, arg2, **kwargs):
super().__init__(arg1, arg2, **kwargs)
def _preprocess_arg(self, arg, i):
arg = super()._preprocess_arg(arg, i)
if i == 1: # const
arg = self._preprocess_arg_constant(self, arg, i)
return arg.check_formula(self.__class__.__name__, None, i)
elif i == 2: # form
return self._preprocess_arg_formula(self, arg, i)
else:
error.should_not_get_here()
def get_id(self):
return self[0].id
class Assertion(Extension):
pass
class NewDefinition(Assertion):
def __init__( # (equal,)
self, arg1, **kwargs):
super().__init__(arg1, **kwargs)
def _preprocess_arg(self, arg, i):
arg = super()._preprocess_arg(arg, i)
if i == 1: # equal
arg = self._preprocess_arg_equal(self, arg, i)
l, r = arg.unpack_equal()
if not l.is_variable():
error.arg_error(
arg, 'not a definitional equation',
self.__class__.__name__, None, i)
elif r.has_free_variables():
error.arg_error(
arg, 'definiens is not closed',
self.__class__.__name__, None, i)
elif not (r.type_variables <= l.type_variables):
msg = error._build_plural_message(
'extra type variable%s in definiens',
*(r.type_variables - l.type_variables))
error.arg_error(
arg, msg, self.__class__.__name__, None, i)
return arg
else:
error.should_not_get_here()
def get_id(self):
l, _ = self[0]._unpack_equal()
return l.id
class NewTheorem(Assertion):
def __init__( # (const, seq)
self, arg1, arg2, **kwargs):
super().__init__(arg1, arg2, **kwargs)
def _preprocess_arg(self, arg, i):
arg = super()._preprocess_arg(arg, i)
if i == 1: # const
arg = self._preprocess_arg_constant(self, arg, i)
return arg.check_formula(self.__class__.__name__, None, i)
elif i == 2: # seq
return self._preprocess_arg_sequent(self, arg, i)
else:
error.should_not_get_here()
def get_id(self):
return self[0].id
class Notation(Extension):
pass
class NewPythonTypeAlias(Notation):
def __init__( # (py_type, type, spec)
self, arg1, arg2, arg3=None, **kwargs):
super().__init__(arg1, arg2, arg3, **kwargs)
def _preprocess_arg(self, arg, i):
if i != 3: # skip possible None
arg = super()._preprocess_arg(arg, i)
if i == 1: # py_type or str
arg = __builtins__.get(arg, arg)
return error.check_arg_class(
arg, type, self.__class__.__name__, None, i)
elif i == 2: # type
return self.Type.check(
arg, self.__class__.__name__, None, i)
elif i == 3:
if isinstance(arg, type):
arg = arg.__name__
return error.check_optional_arg(
arg, lambda x: isinstance(x, str), None,
None, self.__class__.__name__, None, i)
else:
error.should_not_get_here()
class NewTypeSpec(Notation):
def __init__( # (type, spec)
self, arg1, **kwargs):
super().__init__(arg1, **kwargs)
def _preprocess_arg(self, arg, i):
arg = super()._preprocess_arg(arg, i)
if isinstance(arg, type):
arg = arg.__name__
return error.check_arg_class(
arg, str, self.__class__.__name__, None, i)