A categorical programming language
修订版 | b2de512f08a1218800abe9e93879609a5c747c8f (tree) |
---|---|
时间 | 2021-11-04 10:00:26 |
作者 | Corbin <cds@corb...> |
Commiter | Corbin |
Graph complex functions with standard coloration.
@@ -30,7 +30,7 @@ stdenv.mkDerivation { | ||
30 | 30 | cp -r ${pypySrc}/rpython . |
31 | 31 | chmod -R u+w rpython/ |
32 | 32 | # Do the actual translation. |
33 | - ${pypy}/bin/pypy -mrpython -Ojit main.py | |
33 | + ${pypy}/bin/pypy -mrpython -O2 main.py | |
34 | 34 | ''; |
35 | 35 | |
36 | 36 | installPhase = '' |
@@ -13,6 +13,7 @@ import math, sys | ||
13 | 13 | from rpython.rlib.jit import JitDriver, set_param |
14 | 14 | from rpython.rlib.parsing.main import make_parser_from_file |
15 | 15 | from rpython.rlib.parsing.tree import RPythonVisitor |
16 | +from rpython.rlib.rbigint import rbigint | |
16 | 17 | from rpython.rlib.rfile import create_stdio |
17 | 18 | from rpython.rlib.rfloat import string_to_float |
18 | 19 | from rpython.rlib.rstring import StringBuilder, split |
@@ -55,6 +56,14 @@ class B(Element): | ||
55 | 56 | def b(self): |
56 | 57 | return self._b |
57 | 58 | |
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 | + | |
58 | 67 | class F(Element): |
59 | 68 | _immutable_ = True |
60 | 69 |
@@ -177,6 +186,28 @@ class Either(Arrow): | ||
177 | 186 | def run(self, x): |
178 | 187 | return L(T()) if x.b() else R(T()) |
179 | 188 | |
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 | + | |
180 | 211 | class FZero(Arrow): |
181 | 212 | _immutable_ = True |
182 | 213 | def run(self, x): return F(0.0) |
@@ -194,6 +225,14 @@ class FSign(Arrow): | ||
194 | 225 | _immutable_ = True |
195 | 226 | def run(self, x): return B(sign(x.f())) |
196 | 227 | |
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 | + | |
197 | 236 | class FNegate(Arrow): |
198 | 237 | _immutable_ = True |
199 | 238 | def run(self, x): return F(-x.f()) |
@@ -251,10 +290,13 @@ unaryFunctors = { | ||
251 | 290 | "left": Left(), |
252 | 291 | "right": Right(), |
253 | 292 | "either": Either(), |
293 | + "zero": Zero(), | |
294 | + "succ": Succ(), | |
254 | 295 | "f-zero": FZero(), |
255 | 296 | "f-one": FOne(), |
256 | 297 | "f-pi": FPi(), |
257 | 298 | "f-sign": FSign(), |
299 | + "f-floor": FFloor(), | |
258 | 300 | "f-negate": FNegate(), |
259 | 301 | "f-recip": FRecip(), |
260 | 302 | "f-lt": FLT(), |
@@ -281,6 +323,8 @@ def buildCompound(name, args): | ||
281 | 323 | return Curry(args[0]) |
282 | 324 | elif name == "uncurry" and len(args) == 1: |
283 | 325 | return Uncurry(args[0]) |
326 | + elif name == "pr" and len(args) == 2: | |
327 | + return PrimRec(args[0], args[1]) | |
284 | 328 | else: |
285 | 329 | raise BuildProblem("Invalid compound functor: " + name) |
286 | 330 |
@@ -6,7 +6,7 @@ let primitives = | ||
6 | 6 | "id comp ignore fst snd pair left right case curry uncurry \ |
7 | 7 | zero succ pr nil cons fold t f not conj disj either \ |
8 | 8 | 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" | |
10 | 10 | |
11 | 11 | let filter = |
12 | 12 | List.fold_left |
@@ -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) |
@@ -0,0 +1 @@ | ||
1 | +(f/subpair id (comp f-floor (case id (fun/const f-zero)))) |
@@ -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)))) |
@@ -0,0 +1,3 @@ | ||
1 | +(comp | |
2 | + (pair (comp fst h2rgb) (v3/broadcast snd)) | |
3 | + (v3/map2 f-mul)) |
@@ -0,0 +1 @@ | ||
1 | +(pr f-zero (f/addpair id (fun/const f-one))) |
@@ -0,0 +1 @@ | ||
1 | +(v3/triple @0 @0 @0) |
@@ -19,6 +19,7 @@ | ||
19 | 19 | ((== expr 'f-add) (== s (list 'pair 'F 'F)) (== t 'F)) |
20 | 20 | ((== expr 'f-mul) (== s (list 'pair 'F 'F)) (== t 'F)) |
21 | 21 | ((== expr 'f-sqrt) (== s 'F) (== t (list 'sum 'F '1))) |
22 | + ((== expr 'f-floor) (== s 'F) (== t (list 'sum 'F '1))) | |
22 | 23 | ((== expr 'f-atan2) (== s (list 'pair 'F 'F)) (== t 'F)) |
23 | 24 | ; Compound before trivial. |
24 | 25 | ((== expr 'succ) (== s 'N) (== t 'N)) |