-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoa.py
351 lines (247 loc) · 8.43 KB
/
moa.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
from typing import (
TypeVar,
Callable,
Generic,
List,
Any,
Tuple,
Mapping,
Iterable,
NamedTuple,
Type,
MutableMapping,
NoReturn,
)
from typing_extensions import Protocol
from itertools import product
T = TypeVar("T", covariant=True)
class Array(Protocol[T]):
shape: "Array[int]"
def __getitem__(self, ix: Tuple[int, ...]) -> T:
...
class _BaseShape:
"""
Base <1> vector used for the shape of a shape of a shape, so that we don't infinitely recurse.
"""
shape: Array[int]
def __getitem__(self, ix: Tuple[int, ...]) -> int:
return 1
def __str__(self):
return "<1>"
def __repr__(self):
return "_BaseShape()"
base_shape = _BaseShape()
base_shape.shape = base_shape
class PythonArray(Generic[T]):
shape: Array[int]
def __init__(self, x: Any, shape: Tuple[int, ...]) -> None:
self.x = x
if shape == (1,):
self.shape = base_shape
else:
self.shape = PythonArray[int](shape, (len(shape),))
def __getitem__(self, ix: Tuple[int, ...]) -> T:
x = self.x
for i in ix:
x = x[i]
return x
def __str__(self):
return f"({str(self.shape)} - {str(self.x)})"
def __repr__(self):
return f"PythonArray({repr(self.x)}, {repr(self.shape)})"
U = TypeVar("U")
V = TypeVar("V")
def sca(x: U) -> PythonArray[U]:
return PythonArray[U](x, tuple())
def vec(*xs: U) -> PythonArray[U]:
return PythonArray[U](list(xs), (len(xs),))
v = PythonArray[int]([0, 1, 2], (3,))
e_2 = PythonArray[int]([[0, 1], [2, 3], [4, 5]], (3, 2))
e_3 = PythonArray[int](
[[[0, 1], [2, 3]], [[4, 5], [6, 7]], [[8, 9], [10, 11]]], (3, 2, 2)
)
e_4 = PythonArray[int](
[
[[[0, 1], [2, 3]], [[4, 5], [6, 7]]],
[[[8, 9], [10, 11]], [[12, 13], [14, 15]]],
[[[16, 17], [18, 19]], [[20, 21], [22, 23]]],
],
(3, 2, 2, 2),
)
def equiv(a: Array[T], b: Array[T]) -> bool:
if a is base_shape and b is base_shape:
return True
PointWiseRelation(lambda l, r: l == r, a, b)
if not equiv(a.shape, b.shape):
return False
dims = a.shape.shape[0,]
shape = tuple(a.shape[d,] for d in range(dims))
if not shape:
return a[tuple()] == b[tuple()]
for xs in product(*map(range, shape)):
if a[xs] != b[xs]:
return False
return True
class _Empty:
shape: Array[int] = vec(0)
def __getitem__(self, ix: Tuple[int, ...]) -> NoReturn:
raise TypeError()
empty = _Empty()
class Scalar:
shape: Array[int] = empty
class Dimension(Scalar):
def __init__(self, x: Array[T]) -> None:
self.x = x
self.d = x.shape.shape[0,]
def __getitem__(self, ix: Tuple[int, ...]) -> int:
assert not ix
return self.d
def __str__(self):
return f"δ{str(self.x)}"
def __repr__(self):
return f"Dimension({repr(self.x)})"
assert equiv(Dimension(sca("hi")), sca(0))
assert equiv(Dimension(v), sca(1))
assert not equiv(Dimension(v), sca(2))
assert equiv(Dimension(e_2), sca(2))
assert equiv(Dimension(e_3), sca(3))
assert equiv(Dimension(e_4), sca(4))
assert equiv(v.shape, vec(3))
assert equiv(e_2.shape, vec(3, 2))
assert equiv(e_3.shape, vec(3, 2, 2))
assert equiv(e_4.shape, vec(3, 2, 2, 2))
def is_scalar(a: Array[T]) -> bool:
return equiv(Dimension(a), sca(0))
def is_vector(a: Array[T]) -> bool:
return equiv(Dimension(a), sca(1))
class VectorOfScalar(Generic[T]):
shape: Array[int] = base_shape
def __init__(self, x: Array[T]) -> None:
assert is_scalar(x)
self.x = x
def __getitem__(self, ix: Tuple[int, ...]) -> T:
assert ix == (0,)
return self.x[tuple()]
class Psi(Scalar, Generic[T]):
def __init__(self, i: Array[int], e: Array[T]) -> None:
assert is_vector(i)
n = Dimension(e)
assert equiv(i.shape, VectorOfScalar(n))
# 2.00
self.idx: List[int] = []
for j in range(n[tuple()]):
i_ = i[j,]
assert 0 <= i_ and i_ <= e.shape[j,]
self.idx.append(i_)
self.i, self.e = i, e
def __getitem__(self, ix: Tuple[int, ...]) -> T:
assert not ix
return self.e[tuple(self.idx)]
def __str__(self):
return f"{str(self.i)}ψ{str(self.e)}"
assert equiv(Psi(vec(0), v), sca(0))
assert equiv(Psi(vec(1), v), sca(1))
assert equiv(Psi(vec(), sca("hi")), sca("hi"))
class PointWiseRelation(Generic[T, U, V]):
def __init__(self, rel: Callable[[T, U], V], l: Array[T], r: Array[U]) -> None:
assert equiv(l.shape, r.shape)
self.shape = l.shape
self.rel = rel
self.l = l
self.r = r
def __getitem__(self, ix: Tuple[int, ...]) -> V:
return self.rel(self.l[ix], self.r[ix])
def plus(a, b):
return a + b
assert equiv(PointWiseRelation(plus, v, v), PythonArray[int]([0, 2, 4], (3,)))
class ScalarLeftExtensionRelation(Generic[T, U, V]):
def __init__(self, rel: Callable[[T, U], V], l: Array[T], r: Array[U]) -> None:
assert is_scalar(l)
self.shape = r.shape
self.rel = rel
self.l = l
self.r = r
def __getitem__(self, ix: Tuple[int, ...]) -> V:
return self.rel(self.l[tuple()], self.r[ix])
assert equiv(
ScalarLeftExtensionRelation(plus, sca(1), v), PythonArray[int]([1, 2, 3], (3,))
)
assert not equiv(
ScalarLeftExtensionRelation(plus, sca(2), v), PythonArray[int]([1, 2, 3], (3,))
)
print(ScalarLeftExtensionRelation(plus, sca(2), v)[0,])
class ScalarRightExtensionRelation(Generic[T, U, V]):
def __init__(self, rel: Callable[[T, U], V], l: Array[T], r: Array[U]) -> None:
assert is_scalar(r)
self.shape = l.shape
self.rel = rel
self.l = l
self.r = r
def __getitem__(self, ix: Tuple[int, ...]) -> V:
return self.rel(self.l[ix], self.r[tuple()])
class Ravel(Generic[T]):
shape: Array[int]
def __init__(self, e: Array[T]) -> None:
if is_scalar(e):
self.x = x
self.shape = base_shape
return
# if is_vector(e):
# self.shape =
# class Cat(Generic[T]):
# def __init__(self, a: Array[T], b: Array[T]) -> None:
# a_first, *a_rest = a.shape
# b_first, *b_rest = b.shape
# assert a_rest == b_rest
# self.shape = [a_first + b_first] + a_rest
# self._a_first = a_first
# self.a, self.b = a, b
# def indexing(first, *rest):
# if first < self._a_first:
# return a.indexing(first, *rest)
# return b.indexing(first - self._a_first, *rest)
# def __str__(self):
# return f'{self.a} ++ {self.b}'
# a = PythonArray[int]([[1, 2], [2, 4], [3, 4]], [3])
# b = PythonArray[float]([[4, 2], [2, 3]], [2])
# # this will fail type checking :)
# #c = Cat(a, b)
# b = PythonArray[int]([[4, 2], [2, 3]], [2])
# c = Cat(a, b)
# class Take(Array):
# def __init__(self, a: Array[T], b: Array[T]) -> None:
# self.a, self.b = a, b
# a_first, *a_rest = a.shape
# b_first, *b_rest = b.shape
# assert a_rest == b_rest
# self.shape = [a_first + b_first] + a_rest
# self._a_first = a_first
# def indexing(first, *rest):
# if first < self._a_first:
# return a.indexing(first, *rest)
# return b.indexing(first - self._a_first, *rest)
# def __str__(self):
# return f'{self.a} ++ {self.b}'
# # def cat(a: Array[T], b: Array[T]) -> Array[T]:
# # a_first, a_rest = a.shape
# # b_first, b_rest = b.shape
# # assert a_rest == b_rest
# # def indexing(first, *rest):
# # if first < a_first:
# # return a.indexing(first, *rest)
# # return b.indexing(first - a_first, *rest)
# # return Array(indexing, [a_first + b_first] + a_rest)
# # Shape = unary('ρ', 'Shape')
# # Dimension = unary('δ', 'Dimension')
# # Take = binary('△', 'Take')
# # Drop = binary('▽', 'Drop')
# # Cat = new('++', mp.Arity.variadic, 'Cat', infix=True, one_identity=True, associative=True)
# # Psi = binary('ψ', 'Psi')
# # Plus = new('+', mp.Arity.variadic, 'Plus', infix=True, one_identity=True, associative=True, commutative=True)
# # _ = mp.Wildcard.dot()
# # e = mp.Wildcard.dot('e')
# # f = mp.Wildcard.dot('f')
# # a = mp.Wildcard.symbol('a', Array)
# # b = mp.Wildcard.symbol('b', Array)
# # a_is_vec = mp.CustomConstraint(lambda a: a.is_vec)
# # b_is_vec = mp.CustomConstraint(lambda b: b.is_vec)