A categorical programming language
修订版 | fa785ac4caa51c1cba07ad64cd38331ede22201a (tree) |
---|---|
时间 | 2022-07-13 00:40:27 |
作者 | Corbin <cds@corb...> |
Commiter | Corbin |
Display trails in the REPL when available.
@@ -139,6 +139,7 @@ def repl(hive, stdin, stdout): | ||
139 | 139 | print "Not a full command" |
140 | 140 | command(hive, line[1], line[3:], context) |
141 | 141 | continue |
142 | + lineIsAtom = not line.startswith("(") | |
142 | 143 | sexp, trail = parse(line) |
143 | 144 | # Save the original expr for next time. |
144 | 145 | mostRecentExpr = sexp |
@@ -163,6 +164,14 @@ def repl(hive, stdin, stdout): | ||
163 | 164 | print "Couldn't build arrow:", bp.message |
164 | 165 | except UnificationFailed as uf: |
165 | 166 | print "Couldn't check type:", uf.message |
167 | + # Try to print trails for atoms in the hive. | |
168 | + if lineIsAtom: | |
169 | + # Clean the line to improve the odds of success. | |
170 | + line = line.split(" ", 1)[0].strip() | |
171 | + try: | |
172 | + print "Trail:", hive.exprs[line][2].strip() | |
173 | + except KeyError: | |
174 | + pass | |
166 | 175 | return 0 |
167 | 176 | |
168 | 177 | def doREPL(hivepath): |
@@ -0,0 +1 @@ | ||
1 | +(comp (pair (fun/const 3b1b-newton-poly) id) v2/complex/horner) | |
\ No newline at end of file |
@@ -0,0 +1 @@ | ||
1 | +(l v2/complex/one (l v2/complex/zero (l v2/complex/zero (l v2/complex/one (l (pair f/-1 f-zero) (l v2/complex/one nil)))))) | |
\ No newline at end of file |
@@ -0,0 +1 @@ | ||
1 | +(comp f-one f-negate) | |
\ No newline at end of file |
@@ -0,0 +1 @@ | ||
1 | +(pair/of cons @0 @1) | |
\ No newline at end of file |
@@ -0,0 +1,3 @@ | ||
1 | +(poly/horner zero nat/add nat/mul) | |
2 | + | |
3 | +Evaluate a polynomial in the natural numbers with Horner's rule. |
@@ -1,11 +1,15 @@ | ||
1 | 1 | (comp |
2 | 2 | (pair/mapfst list/reverse) |
3 | 3 | (uncurry (fold |
4 | - (fun/name (fun/const zero)) | |
4 | + (fun/name (fun/const @0)) | |
5 | 5 | (curry (comp |
6 | 6 | (pair (pair fstfst (fun/apppair fstsnd snd)) snd) |
7 | - (pair/of nat/add | |
8 | - (pair/of nat/mul fstsnd snd) | |
7 | + (pair/of @1 | |
8 | + (pair/of @2 fstsnd snd) | |
9 | 9 | fstfst)))))) |
10 | 10 | |
11 | -Evaluate a polynomial at an input coordinate using Horner's rule. | |
11 | +Evaluate a polynomial at an input coordinate using Horner's rule, given: | |
12 | + | |
13 | +* A target coordinate for the zero polynomial | |
14 | +* An addition for coefficients | |
15 | +* A multiplication for coordinates |
@@ -0,0 +1 @@ | ||
1 | +(pair/mapsnd f-negate) | |
\ No newline at end of file |
@@ -0,0 +1 @@ | ||
1 | +(poly/horner v2/complex/zero v2/complex/add v2/complex/mul) | |
\ No newline at end of file |
@@ -91,8 +91,7 @@ | ||
91 | 91 | * Would be nice to know how long it takes, too |
92 | 92 | * Parameterize by number of iterations? |
93 | 93 | * Native code generation with QBE |
94 | - * Strategy: Emit one procedure for each expression under a (curry) or | |
95 | - (uncurry), defunctionalizing | |
94 | + * Strategy: Defunctionalize each (curry) and emit a native procedure | |
96 | 95 | * Should arity be implied, so that these procedures can take their pairs |
97 | 96 | as two arguments? |
98 | 97 | * Execution proceeds by compiling our CAM code into QBE templates |
@@ -169,7 +168,6 @@ | ||
169 | 168 | * while-loops and mutable variables |
170 | 169 | * More jets from natural bytecodes |
171 | 170 | * snoc = (comp swap cons) |
172 | - * dup | |
173 | 171 | * foldr = (comp list/reverse list/foldl) |
174 | 172 | * but also, foldl = (comp list/reverse list/foldr) |
175 | 173 | * so we effectively have fast foldl and fast reverse? |
@@ -177,12 +175,11 @@ | ||
177 | 175 | * reverse is implemented as a tight loop in linear time, but it still |
178 | 176 | does a linear number of heap allocations |
179 | 177 | * fun/app = (uncurry id) |
178 | + * done | |
180 | 179 | * uncurry distills down to fun/app, as in the literature |
181 | 180 | * (uncurry @0) could be defined as (comp (pair/mapfst @0) fun/app) |
182 | - * Done for nat/add and nat/pred-maybe | |
183 | - * Makes Project Euler (6) possible, accelerates demos | |
184 | - * pair/swap = (pair snd fst) | |
185 | - * this might be fundamental to symmetric monoidal categories or CCCs | |
181 | + * list/uncons | |
182 | + * has particularly simple bytecode, becoming a term op | |
186 | 183 | * Peephole optimization misses opportunities |
187 | 184 | * Assume two rules |
188 | 185 | * QUOTE; DUP -> QUOTE (term op) |
@@ -201,3 +198,9 @@ | ||
201 | 198 | * (comp f-log1p f-exp) => (comp (pair (fun/const f-one) id) f/add) |
202 | 199 | * Lentz's algorithm |
203 | 200 | * https://en.wikipedia.org/wiki/Lentz's_algorithm |
201 | +* What's a parser again? | |
202 | + * A parser from strings of X to Y: [X] -> Y × [X] | |
203 | + * An instance of state monad | |
204 | + * Multiple results: [X] -> [Y × [X]] | |
205 | + * blends non-determinism and state | |
206 | +* Render 3b1b's simple iterated Newton fractal |