-
Notifications
You must be signed in to change notification settings - Fork 0
/
TypeSystem.py
225 lines (163 loc) · 5.55 KB
/
TypeSystem.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
import typing
from dataclasses import dataclass
from typing import Tuple, Optional
from Result import Result
class Type:
is_final: bool = True
def __eq__(self, other) -> bool:
if isinstance(other, Any) or isinstance(self, Any):
return True
elif isinstance(other, AnyOf) and isinstance(self, AnyOf):
return set(self.all).intersection(other.all) != set()
elif isinstance(self, AnyOf):
return other in self
elif isinstance(other, AnyOf):
return self in other
else:
return type(self) == type(other)
def __str__(self) -> str:
return type(self).__name__
def __repr__(self) -> str:
return type(self).__name__
def __or__(self, other):
if isinstance(other, Any) or isinstance(self, Any):
return Any()
elif isinstance(other, AnyOf) and isinstance(self, AnyOf):
return AnyOf(*self.all, *other.all)
elif isinstance(self, AnyOf):
return AnyOf(*self.all, other)
elif isinstance(other, AnyOf):
return AnyOf(self, *other.all)
elif self == other:
return self
else:
return Or(self, other)
def __hash__(self):
return hash(repr(self))
class undef(Type):
is_final = False
class unit(Type):
is_final = True
class Any(Type):
is_final = True
class AnyOf(Type):
all: list[Type]
is_final = False
def __init__(self, *types: Type):
super().__init__()
seen = set()
self.all = [x for x in types if not (x in seen or seen.add(x))]
def __repr__(self):
return ' | '.join(map(str, self.all))
def __str__(self):
return ' | '.join(map(str, self.all))
def __iter__(self):
return iter(self.all)
class Or(AnyOf):
def __init__(self, left: Type, right: Type):
super().__init__(left, right)
# arity has no meaning in equality!
# todo: vector should be a metrix with 1 column/row
class Vector(Type):
def __init__(self, arity: Optional[int] = None):
self.arity = arity
if arity is None:
self.is_final = False
def __str__(self):
if self.arity is not None:
return f"Vector[{self.arity}]"
else:
return f"Vector[?]"
def __repr__(self):
return self.__str__()
# arity has no meaning in equality!
class Matrix(Type):
def __init__(self, rows: Optional[int] = None, cols: Optional[int] = None):
self.rows = rows
self.cols = cols
if not self.rows or not self.cols:
self.is_final = False
def __str__(self):
match self.arity:
case (None, None):
return "Matrix[?, ?]"
case (None, b):
return f"Matrix[?, {b}]"
case (a, None):
return f"Matrix[{a}, ?]"
case (a, b):
return f"Matrix[{a}, {b}]"
def __repr__(self):
return self.__str__()
@property
def arity(self):
return self.rows, self.cols
@dataclass
class VarArg(Type):
type: Type
def __str__(self):
return f"({self.type})*"
def __repr__(self):
return self.__str__()
def __hash__(self):
return hash(repr(self))
class Function(Type):
args: None | Type | Tuple[Type, ...] | VarArg
arity: Optional[int]
result: Type
def __init__(self, args: None | Type | Tuple[Type, ...] | VarArg, result: Type):
if args is None:
self.arity = 0
elif isinstance(args, VarArg):
self.arity = None
elif isinstance(args, Type):
self.arity = 1
elif isinstance(args, Tuple):
self.arity = len(args)
self.args = args
self.result = result
def __str__(self):
return f"({self.args}) -> {self.result}"
def __repr__(self):
return self.__str__()
def __eq__(self, other):
return type(self) == type(other) and self.args == other.args and self.result == other.result
def __hash__(self):
return hash(repr(self))
def takes(self, args: list[Type]) -> bool:
if self.args is None:
return not args
elif isinstance(self.args, VarArg):
return all(a == self.args.type for a in args)
elif isinstance(self.args, Type):
return len(args) == 1 and self.args == args[0]
elif isinstance(self.args, Tuple):
return len(self.args) == len(args) and all(a == b for a, b in zip(self.args, args))
else:
return False
class FunctionTypeFactory(Function):
is_final = False
def __init__(self, args: None | Type | Tuple[Type, ...] | VarArg, result_hint: Type,
result_type_factory: typing.Callable[..., Result[Type]]):
super().__init__(args, result_hint)
self.result_type_factory = result_type_factory
def __call__(self, args: list) -> Result[Type]:
return self.result_type_factory(*args).map(lambda res: Function(self.args, res))
def __str__(self):
return f"TypeFunction: [{hash(self.result_type_factory)}] => {self.args} -> {self.result}" # todo: sth more informative
def __repr__(self):
return self.__str__()
def __eq__(self, other) -> bool:
return type(self) == type(other) and self.result_type_factory == other.result_type_factory
def __hash__(self):
return hash(repr(self))
class Int(Type):
pass
class Float(Type):
pass
class String(Type):
pass
class Bool(Type):
pass
def numerical() -> Type:
return Int() | Float()