A categorical programming language
修订版 | 5e592f74e009e74fbc58b83f558cd7654711b2a9 (tree) |
---|---|
时间 | 2021-11-14 02:23:00 |
作者 | Corbin <cds@corb...> |
Commiter | Corbin |
demo/mandelbrot: Use bounded unfolds instead of application.
@@ -30,6 +30,9 @@ class Element(object): | ||
30 | 30 | def f(self): |
31 | 31 | raise TypeFail("not f") |
32 | 32 | |
33 | + def l(self): | |
34 | + raise TypeFail("not list") | |
35 | + | |
33 | 36 | def first(self): |
34 | 37 | raise TypeFail("not a pair") |
35 | 38 |
@@ -42,9 +45,6 @@ class Element(object): | ||
42 | 45 | def apply(self, y): |
43 | 46 | raise TypeFail("not hom") |
44 | 47 | |
45 | - def fold(self, nil, cons): | |
46 | - raise TypeFail("not list") | |
47 | - | |
48 | 48 | class T(Element): |
49 | 49 | _immutable_ = True |
50 | 50 |
@@ -114,16 +114,12 @@ class H(Element): | ||
114 | 114 | def apply(self, y): |
115 | 115 | return self._f.run(P(self._x, y)) |
116 | 116 | |
117 | -class Z(Element): | |
117 | +class Xs(Element): | |
118 | 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)) | |
119 | + _immutable_fields_ = "xs[*]", | |
120 | + def __init__(self, xs): | |
121 | + self.xs = xs | |
122 | + def l(self): return self.xs | |
127 | 123 | |
128 | 124 | class Arrow(object): |
129 | 125 | _immutable_ = True |
@@ -220,18 +216,22 @@ class PrimRec(Arrow): | ||
220 | 216 | |
221 | 217 | class Nil(Arrow): |
222 | 218 | _immutable_ = True |
223 | - def run(self, x): return Z() | |
219 | + def run(self, x): return Xs([]) | |
224 | 220 | |
225 | 221 | class Cons(Arrow): |
226 | 222 | _immutable_ = True |
227 | - def run(self, x): return C(x.first(), x.second()) | |
223 | + def run(self, x): return Xs([x.first()] + x.second().l()) | |
228 | 224 | |
229 | 225 | class Fold(Arrow): |
230 | 226 | _immutable_ = True |
231 | 227 | def __init__(self, n, c): |
232 | 228 | self._n = n |
233 | 229 | self._c = c |
234 | - def run(self, x): return x.fold(self._n, self._c) | |
230 | + def run(self, x): | |
231 | + rv = self._n.run(T()) | |
232 | + for e in x.l(): | |
233 | + rv = self._c.run(P(e, rv)) | |
234 | + return rv | |
235 | 235 | |
236 | 236 | class FZero(Arrow): |
237 | 237 | _immutable_ = True |
@@ -1,9 +1,12 @@ | ||
1 | 1 | (comp |
2 | 2 | (comp |
3 | - (pair v2/mandelbrot (comp (fun/const f-zero) fun/dup)) | |
4 | - fun/app-80) | |
5 | - (comp | |
6 | - (comp (pair v2/norm (fun/const f/2)) f-lt) | |
7 | 3 | (comp |
8 | - (comp either (case f-zero f-one)) | |
9 | - (v3/triple id id id))))) | |
4 | + (fun/apppair | |
5 | + (fun/const (comp nat/256 nonempty/unfold)) | |
6 | + (pair (comp (fun/const f-zero) fun/dup) v2/mandelbrot)) | |
7 | + snd) | |
8 | + (comp | |
9 | + (list/filter (comp (pair v2/norm (fun/const f/2)) f-lt)) | |
10 | + (comp list/len | |
11 | + (f/divpair nat/to-f (fun/const (comp nat/256 nat/to-f)))))) | |
12 | + (v3/broadcast (f/subpair (fun/const f-one) id))) |