• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Tags
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

A categorical programming language


Commit MetaInfo

Revisióne701bc50c81237a7415a177d9dc173ec0d8afecb (tree)
Tiempo2021-11-13 14:38:30
AutorCorbin <cds@corb...>
CommiterCorbin

Log Message

cammy-run: Add lists.

Cambiar Resumen

Diferencia incremental

--- a/cammy-run/main.py
+++ b/cammy-run/main.py
@@ -42,6 +42,9 @@ class Element(object):
4242 def apply(self, y):
4343 raise TypeFail("not hom")
4444
45+ def fold(self, nil, cons):
46+ raise TypeFail("not list")
47+
4548 class T(Element):
4649 _immutable_ = True
4750
@@ -111,6 +114,16 @@ class H(Element):
111114 def apply(self, y):
112115 return self._f.run(P(self._x, y))
113116
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))
114127
115128 class Arrow(object):
116129 _immutable_ = True
@@ -205,6 +218,21 @@ class PrimRec(Arrow):
205218 rv = self._f.run(rv)
206219 return rv
207220
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+
208236 class FZero(Arrow):
209237 _immutable_ = True
210238 def run(self, x): return F(0.0)
@@ -306,6 +334,8 @@ unaryFunctors = {
306334 "either": Either(),
307335 "zero": Zero(),
308336 "succ": Succ(),
337+ "nil": Nil(),
338+ "cons": Cons(),
309339 "f-zero": FZero(),
310340 "f-one": FOne(),
311341 "f-pi": FPi(),
@@ -341,6 +371,8 @@ def buildCompound(name, args):
341371 return Uncurry(args[0])
342372 elif name == "pr" and len(args) == 2:
343373 return PrimRec(args[0], args[1])
374+ elif name == "fold" and len(args) == 2:
375+ return Fold(args[0], args[1])
344376 else:
345377 raise BuildProblem("Invalid compound functor: " + name)
346378