• 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

修订版9cc1c3a0fccc5664e0d4e3413783588c23aed667 (tree)
时间2022-04-01 13:37:56
作者Corbin <cds@corb...>
CommiterCorbin

Log Message

Get faster fractal membership working, somewhat.

I'm not quite confident in the correctness of my step "monad", which
deliberately breaks the monad laws to facilitate counting evaluation
steps. I'm also not sure why it's so slow.

更改概述

差异

--- a/cammy-rpy/draw.py
+++ b/cammy-rpy/draw.py
@@ -1,6 +1,7 @@
11 import math, sys
22
3-from rpython.rlib.jit import JitDriver, set_param, unroll_safe
3+from rpython.rlib.jit import JitDriver, unroll_safe #, set_param
4+from rpython.rlib.rfile import create_stdio
45 from rpython.rlib.rfloat import string_to_float
56 from rpython.rlib.rstring import StringBuilder, split
67
@@ -77,6 +78,7 @@ def multisample(program, radius, x, y):
7778 return finishChannel(r / l), finishChannel(g / l), finishChannel(b / l)
7879
7980 def drawPixels(size, program, window):
81+ _, stdout, _ = create_stdio()
8082 sb = StringBuilder()
8183 i = 0
8284 while i < size:
@@ -84,6 +86,9 @@ def drawPixels(size, program, window):
8486 r, g, b = multisample(program, window.pixelRadius, c1, c2)
8587 sb.append(chr(r) + chr(g) + chr(b))
8688 i += 1
89+ if not (i % 1000):
90+ stdout.write(".")
91+ stdout.flush()
8792 return sb.build()
8893
8994 def drawPNG(program, filename, corners, width, height):
@@ -95,7 +100,7 @@ def drawPNG(program, filename, corners, width, height):
95100
96101
97102 def main(argv):
98- set_param(None, "trace_limit", 50001)
103+ # set_param(None, "trace_limit", 50001)
99104
100105 prog = argv[1]
101106 window = [string_to_float(s) for s in split(argv[2])]
--- a/cammy-rpy/repl.py
+++ b/cammy-rpy/repl.py
@@ -69,19 +69,16 @@ def repl(hive, stdin, stdout):
6969 command(hive, line[1], line[3:])
7070 continue
7171 sexp, trail = parse(line)
72- print "Got:", line
73- print "S-expression:", sexp.asStr()
74- print "Trail:", trail
7572 try:
7673 sexp = sexp.canonicalize(hive)
77- print "Canonicalized:", sexp.asStr()
74+ # print "Canonicalized:", sexp.asStr()
7875 except MissingAtom as ma:
7976 print "Couldn't canonicalize S-expression; missing atom:", ma.atom
8077 sexp = jellify(sexp)
81- print "Jellified:", sexp.asStr()
78+ # print "Jellified:", sexp.asStr()
8279 try:
8380 arrow = sexp.buildArrow()
84- print "Arrow:", arrow
81+ # print "Arrow:", arrow
8582 cs = ConstraintStore()
8683 domain, codomain = arrow.types(cs)
8784 extractor = TypeExtractor(cs, PlaintextTypeFormatter())
--- /dev/null
+++ b/hive/bits/about.md
@@ -0,0 +1,2 @@
1+The type $[2]$ can be interpreted as a type of natural numbers equipped with a
2+relatively fast primitive recursion.
--- /dev/null
+++ b/hive/bits/succ.cammy
@@ -0,0 +1,9 @@
1+(comp
2+ (fold (pair t nil)
3+ (comp
4+ (comp pair/assl
5+ (pair/mapfst (comp bool/half-adder pair/swap)))
6+ (comp pair/assr (pair/mapsnd cons))))
7+ (uncurry (bool/if (list/conspair (fun/const t) id) id)))
8+
9+The successor of a natural number is also a natural number.
--- /dev/null
+++ b/hive/bits/zero.cammy
@@ -0,0 +1 @@
1+nil
--- /dev/null
+++ b/hive/bool/half-adder.cammy
@@ -0,0 +1,3 @@
1+(pair bool/xor conj)
2+
3+Add two bits, returning a pair of the result and carry bit.
--- /dev/null
+++ b/hive/bool/parity.cammy
@@ -0,0 +1,3 @@
1+(fold f bool/xor)
2+
3+The parity of a list of bits.
--- /dev/null
+++ b/hive/bool/popcount.cammy
@@ -0,0 +1,3 @@
1+(fold zero (uncurry (bool/if succ id)))
2+
3+The number of set bits in a list of bits.
--- /dev/null
+++ b/hive/bool/xor.cammy
@@ -0,0 +1,3 @@
1+(comp (pair disj (comp conj not)) conj)
2+
3+The exclusive-or operation, also known as the parity operation.
--- /dev/null
+++ b/hive/demo/burning-ship-fast.cammy
@@ -0,0 +1,5 @@
1+(comp (fractal-membership-fast v2/burning-ship nat/256) (v3/broadcast (f/subpair (fun/const f-one) id)))
2+
3+Draw membership for the [Burning Ship
4+fractal](https://en.wikipedia.org/wiki/Burning_Ship_fractal), a relative of
5+the Mandelbrot set.
--- a/hive/demo/burning-ship.cammy
+++ b/hive/demo/burning-ship.cammy
@@ -1,6 +1,4 @@
1-(comp
2- (fractal-membership v2/burning-ship nat/256)
3- (v3/broadcast (f/subpair (fun/const f-one) id)))
1+(comp (fractal-membership v2/burning-ship nat/256) (v3/broadcast (f/subpair (fun/const f-one) id)))
42
53 Draw membership for the [Burning Ship
64 fractal](https://en.wikipedia.org/wiki/Burning_Ship_fractal), a relative of
--- a/hive/demo/mandelbrot.cammy
+++ b/hive/demo/mandelbrot.cammy
@@ -1,6 +1,4 @@
1-(comp
2- (fractal-membership v2/mandelbrot nat/256)
3- (v3/broadcast (f/subpair (fun/const f-one) id)))
1+(comp (fractal-membership v2/mandelbrot nat/256) (v3/broadcast (f/subpair (fun/const f-one) id)))
42
53 Draw membership in the [Mandelbrot
64 set](https://en.wikipedia.org/wiki/Mandelbrot_set).
--- /dev/null
+++ b/hive/fps/about.md
@@ -0,0 +1,3 @@
1+The function type $\mathbb{N}^\mathbb{N}$ can be interpreted as a [formal
2+power series](https://en.wikipedia.org/wiki/Formal_power_series) on the
3+natural numbers with coefficients in the natural numbers.
--- /dev/null
+++ b/hive/fps/add.cammy
@@ -0,0 +1,3 @@
1+(curry (comp (comp (pair/mapsnd pair/dup) (v2/map2 fun/app)) nat/add))
2+
3+The sum of two formal power series.
--- /dev/null
+++ b/hive/fps/diff.cammy
@@ -0,0 +1,3 @@
1+(curry (comp (comp (pair/mapsnd succ) (pair fun/app snd)) nat/mul))
2+
3+The formal derivative of a formal power series.
--- /dev/null
+++ b/hive/fps/extract.cammy
@@ -0,0 +1,3 @@
1+(comp pair/swap fun/app)
2+
3+Extract a coefficient from a formal power series.
--- /dev/null
+++ b/hive/fps/zero.cammy
@@ -0,0 +1,3 @@
1+(curry (comp fst zero))
2+
3+The zero formal power series.
--- /dev/null
+++ b/hive/fractal-maybe.cammy
@@ -0,0 +1,3 @@
1+(comp (comp @0 (pair v2/complex/mag-2 (pair left (comp ignore right)))) bool/pick)
2+
3+Take a step in an IFS, failing if the step has magnitude 2 or greater.
--- /dev/null
+++ b/hive/fractal-membership-fast.cammy
@@ -0,0 +1,5 @@
1+(comp (iter-fractal-fast @0 @1) (case (fun/const f-one) (fun/const f-zero)))
2+
3+Iterate a fractal for a maximum number of steps, and return a value in $[0,1]$
4+indicating how many steps were taken, with 1 indicating that the fractal did
5+not diverge.
--- a/hive/fun/inv-img.cammy
+++ /dev/null
@@ -1,3 +0,0 @@
1-(curry (fun/apppair fst (comp snd @0)))
2-
3-The inverse-image functor for an arbitrary classifier.
--- /dev/null
+++ b/hive/fun/postcomp.cammy
@@ -0,0 +1,4 @@
1+(curry (comp fun/app @0))
2+
3+Postcompose a function type with an arrow. This is like one version of the
4+Yoneda embedding for the given arrow.
--- /dev/null
+++ b/hive/fun/precomp.cammy
@@ -0,0 +1,5 @@
1+(curry (fun/apppair fst (comp snd @0)))
2+
3+Precompose a function type with an arrow. This is like the inverse-image
4+functor for an arbitrary classifier. It is also like one version of the Yoneda
5+embedding for the given arrow.
--- /dev/null
+++ b/hive/iter-fractal-fast.cammy
@@ -0,0 +1,5 @@
1+(fun/unname (comp @1
2+ (step-iter (fractal-maybe
3+ (fun/unname (comp (comp f-zero pair/dup) @0))))))
4+
5+Given a maximum number of steps, iterate the given fractal.
--- /dev/null
+++ b/hive/iter-maybe-count.cammy
@@ -0,0 +1,13 @@
1+(pr @0 @1)
2+
3+@0: 1 -> [X, X + N]
4+@1: [X, X + N] -> [X, X + N]
5+(uncurry @1) : [X, X + N] x X -> X + N
6+
7+Given X -> X + 1
8+N -> [X, X + N]
9+max iterations -> [x,
10+ x after max iterations + number of iterations before failing]
11+
12+Intermediate state is more like:
13+X x N -> X x N + N
--- /dev/null
+++ b/hive/list/tail.cammy
@@ -0,0 +1,3 @@
1+(comp list/uncons (case snd nil))
2+
3+The tail of a list. The tail of the empty list is the empty list.
--- /dev/null
+++ b/hive/magma/about.md
@@ -0,0 +1,2 @@
1+The function type $X^{X \times X}$ can be interpreted as the type of magmas on
2+$X$. A magma is a binary operation.
--- /dev/null
+++ b/hive/magma/braid.cammy
@@ -0,0 +1,3 @@
1+(fun/precomp pair/swap)
2+
3+Braid a magma, obtaining its opposite magma.
--- /dev/null
+++ b/hive/magma/square.cammy
@@ -0,0 +1,3 @@
1+(fun/precomp pair/dup)
2+
3+A magma can be squared to produce an endomorphism.
--- /dev/null
+++ b/hive/maybe-step.cammy
@@ -0,0 +1 @@
1+(comp @0 (case (comp (pair id (fun/const (comp zero succ))) left) (comp zero right)))
--- /dev/null
+++ b/hive/monads/int-comp.cammy
@@ -0,0 +1,3 @@
1+(curry (comp (pair (fun/apppair (comp fst snd) snd) (comp fst fst)) @0))
2+
3+Internal composition in any monad, built from the internal bind.
--- /dev/null
+++ b/hive/monads/iter.cammy
@@ -0,0 +1,12 @@
1+(pr
2+ (fun/name @0)
3+ (comp (pair id (fun/const (fun/name @2))) (monads/int-comp @1)))
4+
5+Given a unit and internal bind in some monad, iterate an endomorphism in that
6+monad.
7+
8+For example, to iterate in the maybe monad:
9+
10+ (monads/iter monads/maybe/unit monads/maybe/int-bind @0)
11+
12+Iteration can terminate early in short-circuiting monads.
--- /dev/null
+++ b/hive/monads/maybe/int-bind.cammy
@@ -0,0 +1,3 @@
1+(comp fun/distribl (case (comp pair/swap fun/app) (comp ignore right)))
2+
3+An internal version of bind for the maybe monad.
--- /dev/null
+++ b/hive/monoids/endo/about.md
@@ -0,0 +1,2 @@
1+For a given type $X$, the function type $X^X$ of
2+[endomorphisms](https://ncatlab.org/nlab/show/endomorphism) forms a monoid.
--- /dev/null
+++ b/hive/monoids/endo/add.cammy
@@ -0,0 +1,3 @@
1+fun/int-comp
2+
3+Addition in an endomorphism monoid.
--- /dev/null
+++ b/hive/monoids/endo/zero.cammy
@@ -0,0 +1,3 @@
1+(fun/name id)
2+
3+The zero of an endomorphism monoid.
--- a/hive/nat/256.cammy
+++ b/hive/nat/256.cammy
@@ -1 +1 @@
1-(comp (pair nat/2 nat/8) nat/exp)
1+(comp (pair nat/8 nat/2) nat/exp)
--- a/hive/nat/8.cammy
+++ b/hive/nat/8.cammy
@@ -1 +1 @@
1-(comp (pair nat/2 nat/3) nat/exp)
1+(comp (pair nat/3 nat/2) nat/exp)
--- /dev/null
+++ b/hive/nat/81.cammy
@@ -0,0 +1,3 @@
1+(comp (pair nat/2 nat/9) nat/exp)
2+
3+
--- /dev/null
+++ b/hive/nat/9.cammy
@@ -0,0 +1 @@
1+(comp (pair nat/2 nat/3) nat/exp)
--- /dev/null
+++ b/hive/step-bind.cammy
@@ -0,0 +1,11 @@
1+(comp fun/distribl
2+ (case
3+ (comp
4+ (pair (fun/apppair snd (comp fst fst)) (comp fst snd))
5+ (comp fun/distribl
6+ (case
7+ (comp (comp pair/assr (pair/mapsnd nat/add)) left)
8+ (comp nat/add right))))
9+ (comp fst right)))
10+
11+(X x N + N) x [X, Y x N + N] -> Y x N + N
--- /dev/null
+++ b/hive/step-comp.cammy
@@ -0,0 +1,10 @@
1+(curry
2+ (comp
3+ (comp
4+ (pair (comp fst fst) (fun/apppair (comp fst snd) snd))
5+ fun/distribr)
6+ (case fun/app (comp snd right))))
7+
8+([Y x N, Z x N + N] x [X x N, Y x N + N]) x (X x N) -> Z x N + N
9+
10+[Y x N, Z x N + N] x [X x N, Y x N + N] -> [X x N, Z x N + N]
--- /dev/null
+++ b/hive/step-iter.cammy
@@ -0,0 +1,3 @@
1+(monads/iter step-unit step-bind (maybe-step @0))
2+
3+
--- /dev/null
+++ b/hive/step-unit.cammy
@@ -0,0 +1,3 @@
1+(comp (pair id (fun/const zero)) left)
2+
3+The unit of a sort of step monad.
--- /dev/null
+++ b/hive/take-step.cammy
@@ -0,0 +1,3 @@
1+(comp (comp (pair/tensor @0 succ) fun/distribl) (case left (comp snd right)))
2+
3+Take a step along a partial arrow, incrementing the number of steps taken.
--- a/hive/yoneda/embed.cammy
+++ b/hive/yoneda/embed.cammy
@@ -1,3 +1,3 @@
1-(curry (comp fun/app @0))
1+(fun/postcomp @0)
22
33 The Yoneda embedding of an arrow.
--- a/shell.nix
+++ b/shell.nix
@@ -16,6 +16,8 @@ in pkgs.stdenv.mkDerivation {
1616 pythonPackages.pyflakes
1717 # using cammy-comb
1818 graphviz
19+ # using cammy-draw
20+ pv
1921 # using cammy-weave
2022 pandoc texlive.combined.scheme-small
2123 # using cammy-repl
--- a/todo.txt
+++ b/todo.txt
@@ -6,7 +6,6 @@
66 * More list manipulations
77 * list/eq : [X × X, 2] → [[X] × [X], 2]
88 * list/zip : [X] × [Y] → [X × Y]
9- * list/tail : [X] → [X]
109 * rat
1110 * Using binary quote notation?
1211 * Continued fractions?
@@ -19,12 +18,11 @@
1918 * Euler's constant e
2019 * Euler's constant gamma
2120 * Double-negation monad for CPS?
22- * Cont is available but not fully developed
23- * monads/cont/join, monads/cont/bind, etc.
2421 * fun/name should always start from 1
2522 * Requires changing some callers
2623 * Might not be feasible unless we switch to stricter monoidal categories
2724 * We'd need an equivalence X × 1 → X
25+ * swap and dup would need to be primitive again too
2826 * Prove that nat/add is smallest with its behavior
2927 * Streams
3028 * Monad: X → [N, X]
@@ -44,14 +42,6 @@
4442 * hook the hive to load a specialized routine during codeload?
4543 * pair/swap is builtin to movelist and jelly
4644 * this might be fundamental to symmetric monoidal categories or CCCs
47-* Search for more optimizations
48- * Some arrows have inferred types that suggest they should be simplifiable
49- * X -> X: should be id?
50- * X -> 1: should be ignore
51- * 1 -> 2: should be t or f
52-* Refactoring tools
53- * Find all leaves in a hive
54- * Rename an expression and change all callers
5545 * Maybe allow inductive datatype definitions during toolchain build time
5646 * Start by factoring out N and lists into inductive datatype modules somehow
5747 * Then add trees, etc.