A categorical programming language
Revisión | e701bc50c81237a7415a177d9dc173ec0d8afecb (tree) |
---|---|
Tiempo | 2021-11-13 14:38:30 |
Autor | Corbin <cds@corb...> |
Commiter | Corbin |
cammy-run: Add lists.
@@ -42,6 +42,9 @@ class Element(object): | ||
42 | 42 | def apply(self, y): |
43 | 43 | raise TypeFail("not hom") |
44 | 44 | |
45 | + def fold(self, nil, cons): | |
46 | + raise TypeFail("not list") | |
47 | + | |
45 | 48 | class T(Element): |
46 | 49 | _immutable_ = True |
47 | 50 |
@@ -111,6 +114,16 @@ class H(Element): | ||
111 | 114 | def apply(self, y): |
112 | 115 | return self._f.run(P(self._x, y)) |
113 | 116 | |
117 | +class Z(Element): | |
118 | + _immutable_ = True | |
119 | + def fold(self, nil, cons): return nil.run(T()) | |
120 | + | |
121 | +class C(Element): | |
122 | + _immutable_ = True | |
123 | + def __init__(self, x, xs): | |
124 | + self._x = x | |
125 | + self._xs = xs | |
126 | + def fold(self, nil, cons): return cons.run(P(self._x, self._xs)) | |
114 | 127 | |
115 | 128 | class Arrow(object): |
116 | 129 | _immutable_ = True |
@@ -205,6 +218,21 @@ class PrimRec(Arrow): | ||
205 | 218 | rv = self._f.run(rv) |
206 | 219 | return rv |
207 | 220 | |
221 | +class Nil(Arrow): | |
222 | + _immutable_ = True | |
223 | + def run(self, x): return Z() | |
224 | + | |
225 | +class Cons(Arrow): | |
226 | + _immutable_ = True | |
227 | + def run(self, x): return C(x.first(), x.second()) | |
228 | + | |
229 | +class Fold(Arrow): | |
230 | + _immutable_ = True | |
231 | + def __init__(self, n, c): | |
232 | + self._n = n | |
233 | + self._c = c | |
234 | + def run(self, x): return x.fold(self._n, self._c) | |
235 | + | |
208 | 236 | class FZero(Arrow): |
209 | 237 | _immutable_ = True |
210 | 238 | def run(self, x): return F(0.0) |
@@ -306,6 +334,8 @@ unaryFunctors = { | ||
306 | 334 | "either": Either(), |
307 | 335 | "zero": Zero(), |
308 | 336 | "succ": Succ(), |
337 | + "nil": Nil(), | |
338 | + "cons": Cons(), | |
309 | 339 | "f-zero": FZero(), |
310 | 340 | "f-one": FOne(), |
311 | 341 | "f-pi": FPi(), |
@@ -341,6 +371,8 @@ def buildCompound(name, args): | ||
341 | 371 | return Uncurry(args[0]) |
342 | 372 | elif name == "pr" and len(args) == 2: |
343 | 373 | return PrimRec(args[0], args[1]) |
374 | + elif name == "fold" and len(args) == 2: | |
375 | + return Fold(args[0], args[1]) | |
344 | 376 | else: |
345 | 377 | raise BuildProblem("Invalid compound functor: " + name) |
346 | 378 |