• R/O
  • HTTP
  • SSH
  • HTTPS

提交

标签
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-cqtcocoa誰得pythonphprubygameguibathyscaphec翻訳計画中(planning stage)omegatframeworktwittertestdomvb.netdirectxbtronarduinopreviewerゲームエンジン

A categorical programming language


Commit MetaInfo

修订版5e592f74e009e74fbc58b83f558cd7654711b2a9 (tree)
时间2021-11-14 02:23:00
作者Corbin <cds@corb...>
CommiterCorbin

Log Message

demo/mandelbrot: Use bounded unfolds instead of application.

更改概述

差异

--- a/cammy-run/main.py
+++ b/cammy-run/main.py
@@ -30,6 +30,9 @@ class Element(object):
3030 def f(self):
3131 raise TypeFail("not f")
3232
33+ def l(self):
34+ raise TypeFail("not list")
35+
3336 def first(self):
3437 raise TypeFail("not a pair")
3538
@@ -42,9 +45,6 @@ class Element(object):
4245 def apply(self, y):
4346 raise TypeFail("not hom")
4447
45- def fold(self, nil, cons):
46- raise TypeFail("not list")
47-
4848 class T(Element):
4949 _immutable_ = True
5050
@@ -114,16 +114,12 @@ class H(Element):
114114 def apply(self, y):
115115 return self._f.run(P(self._x, y))
116116
117-class Z(Element):
117+class Xs(Element):
118118 _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
127123
128124 class Arrow(object):
129125 _immutable_ = True
@@ -220,18 +216,22 @@ class PrimRec(Arrow):
220216
221217 class Nil(Arrow):
222218 _immutable_ = True
223- def run(self, x): return Z()
219+ def run(self, x): return Xs([])
224220
225221 class Cons(Arrow):
226222 _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())
228224
229225 class Fold(Arrow):
230226 _immutable_ = True
231227 def __init__(self, n, c):
232228 self._n = n
233229 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
235235
236236 class FZero(Arrow):
237237 _immutable_ = True
--- a/hive/demo/mandelbrot.cammy
+++ b/hive/demo/mandelbrot.cammy
@@ -1,9 +1,12 @@
11 (comp
22 (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)
73 (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)))