• 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

修订版b2de512f08a1218800abe9e93879609a5c747c8f (tree)
时间2021-11-04 10:00:26
作者Corbin <cds@corb...>
CommiterCorbin

Log Message

Graph complex functions with standard coloration.

更改概述

差异

--- a/cammy-run/default.nix
+++ b/cammy-run/default.nix
@@ -30,7 +30,7 @@ stdenv.mkDerivation {
3030 cp -r ${pypySrc}/rpython .
3131 chmod -R u+w rpython/
3232 # Do the actual translation.
33- ${pypy}/bin/pypy -mrpython -Ojit main.py
33+ ${pypy}/bin/pypy -mrpython -O2 main.py
3434 '';
3535
3636 installPhase = ''
--- a/cammy-run/main.py
+++ b/cammy-run/main.py
@@ -13,6 +13,7 @@ import math, sys
1313 from rpython.rlib.jit import JitDriver, set_param
1414 from rpython.rlib.parsing.main import make_parser_from_file
1515 from rpython.rlib.parsing.tree import RPythonVisitor
16+from rpython.rlib.rbigint import rbigint
1617 from rpython.rlib.rfile import create_stdio
1718 from rpython.rlib.rfloat import string_to_float
1819 from rpython.rlib.rstring import StringBuilder, split
@@ -55,6 +56,14 @@ class B(Element):
5556 def b(self):
5657 return self._b
5758
59+class N(Element):
60+ _immutable_ = True
61+ def __init__(self, bi):
62+ self._bi = bi
63+
64+ def n(self):
65+ return self._bi
66+
5867 class F(Element):
5968 _immutable_ = True
6069
@@ -177,6 +186,28 @@ class Either(Arrow):
177186 def run(self, x):
178187 return L(T()) if x.b() else R(T())
179188
189+class Zero(Arrow):
190+ _immutable_ = True
191+ def run(self, x): return N(rbigint.fromint(0))
192+
193+class Succ(Arrow):
194+ _immutable_ = True
195+ def run(self, x): return N(x.n().int_add(1))
196+
197+class PrimRec(Arrow):
198+ _immutable_ = True
199+ def __init__(self, x, f):
200+ self._x = x
201+ self._f = f
202+
203+ def run(self, x):
204+ n = x.n()
205+ rv = self._x.run(T())
206+ while n.tobool():
207+ n = n.int_sub(1)
208+ rv = self._f.run(rv)
209+ return rv
210+
180211 class FZero(Arrow):
181212 _immutable_ = True
182213 def run(self, x): return F(0.0)
@@ -194,6 +225,14 @@ class FSign(Arrow):
194225 _immutable_ = True
195226 def run(self, x): return B(sign(x.f()))
196227
228+class FFloor(Arrow):
229+ _immutable_ = True
230+ def run(self, x):
231+ try:
232+ return L(F(float(math.floor(x.f()))))
233+ except (ValueError, OverflowError):
234+ return R(T())
235+
197236 class FNegate(Arrow):
198237 _immutable_ = True
199238 def run(self, x): return F(-x.f())
@@ -251,10 +290,13 @@ unaryFunctors = {
251290 "left": Left(),
252291 "right": Right(),
253292 "either": Either(),
293+ "zero": Zero(),
294+ "succ": Succ(),
254295 "f-zero": FZero(),
255296 "f-one": FOne(),
256297 "f-pi": FPi(),
257298 "f-sign": FSign(),
299+ "f-floor": FFloor(),
258300 "f-negate": FNegate(),
259301 "f-recip": FRecip(),
260302 "f-lt": FLT(),
@@ -281,6 +323,8 @@ def buildCompound(name, args):
281323 return Curry(args[0])
282324 elif name == "uncurry" and len(args) == 1:
283325 return Uncurry(args[0])
326+ elif name == "pr" and len(args) == 2:
327+ return PrimRec(args[0], args[1])
284328 else:
285329 raise BuildProblem("Invalid compound functor: " + name)
286330
--- a/frame/frame.ml
+++ b/frame/frame.ml
@@ -6,7 +6,7 @@ let primitives =
66 "id comp ignore fst snd pair left right case curry uncurry \
77 zero succ pr nil cons fold t f not conj disj either \
88 f-zero f-one f-pi \
9- f-sign f-negate f-recip f-lt f-add f-mul f-sqrt f-atan2"
9+ f-sign f-floor f-negate f-recip f-lt f-add f-mul f-sqrt f-atan2"
1010
1111 let filter =
1212 List.fold_left
--- /dev/null
+++ b/hive/complex-graph.cammy
@@ -0,0 +1,9 @@
1+(comp
2+ (pair
3+ (f/addpair
4+ (f/divpair
5+ (comp fun/swap f-atan2)
6+ (fun/const (f/mulpair f-pi f/2)))
7+ (comp f/2 f-recip))
8+ v2/norm)
9+ hv2rgb)
--- /dev/null
+++ b/hive/f/fract.cammy
@@ -0,0 +1 @@
1+(f/subpair id (comp f-floor (case id (fun/const f-zero))))
--- /dev/null
+++ b/hive/h2rgb.cammy
@@ -0,0 +1,14 @@
1+(comp
2+ (comp
3+ (pair
4+ (v3/broadcast id)
5+ (fun/const (v3/triple f-one (f/divpair f/2 f/3) (comp f/3 f-recip))))
6+ (v3/map2 f-add))
7+ (v3/map
8+ (f/subpair
9+ (comp
10+ (f/subpair
11+ (f/mulpair f/fract (fun/const (f/mulpair f/2 f/3)))
12+ (fun/const f/3))
13+ f/abs)
14+ (fun/const f-one))))
--- /dev/null
+++ b/hive/hv2rgb.cammy
@@ -0,0 +1,3 @@
1+(comp
2+ (pair (comp fst h2rgb) (v3/broadcast snd))
3+ (v3/map2 f-mul))
--- /dev/null
+++ b/hive/nat/to-f.cammy
@@ -0,0 +1 @@
1+(pr f-zero (f/addpair id (fun/const f-one)))
--- /dev/null
+++ b/hive/v3/broadcast.cammy
@@ -0,0 +1 @@
1+(v3/triple @0 @0 @0)
--- a/movelist/movelist.scm
+++ b/movelist/movelist.scm
@@ -19,6 +19,7 @@
1919 ((== expr 'f-add) (== s (list 'pair 'F 'F)) (== t 'F))
2020 ((== expr 'f-mul) (== s (list 'pair 'F 'F)) (== t 'F))
2121 ((== expr 'f-sqrt) (== s 'F) (== t (list 'sum 'F '1)))
22+ ((== expr 'f-floor) (== s 'F) (== t (list 'sum 'F '1)))
2223 ((== expr 'f-atan2) (== s (list 'pair 'F 'F)) (== t 'F))
2324 ; Compound before trivial.
2425 ((== expr 'succ) (== s 'N) (== t 'N))