A categorical programming language
修訂 | 1531cf447f5e70bb057715d9a060945323a839ad (tree) |
---|---|
時間 | 2022-10-10 03:31:36 |
作者 | Corbin <cds@corb...> |
Commiter | Corbin |
Stop fucking around with the trampoline for now.
@@ -39,7 +39,7 @@ function rescueNaN(f) { return isNaN(f) ? [true, f] : [false, null]; } | ||
39 | 39 | // X -> Tramp X |
40 | 40 | function done(result) { return ["pure", result]; } |
41 | 41 | // [1,Tramp X] -> Tramp X |
42 | -// function cont(thunk) { return ["cont", thunk]; } | |
42 | +function cont(thunk) { return ["cont", thunk]; } | |
43 | 43 | // Tramp X × [X,Tramp Y] -> Tramp Y |
44 | 44 | function bind(action, f) { return ["bind", action, f]; } |
45 | 45 | // function join(action) { return cont(() => bind(action, ma => ma)); } |
@@ -63,9 +63,9 @@ function trampoline(action) { | ||
63 | 63 | stack = [register[2], stack]; |
64 | 64 | register = register[1]; |
65 | 65 | break; |
66 | - // case "cont": | |
67 | - // register = register[1](); | |
68 | - // break; | |
66 | + case "cont": | |
67 | + register = register[1](); | |
68 | + break; | |
69 | 69 | default: |
70 | 70 | console.log("stack", stack); |
71 | 71 | throw new Error("unknown trampoline action " + register[0]); |
@@ -122,13 +122,13 @@ function compile(expr) { | ||
122 | 122 | } |
123 | 123 | case "curry": { |
124 | 124 | const [f] = l; |
125 | - // XXX messy? | |
126 | - return x => done(y => trampoline(f([x, y]))); | |
125 | + // XXX slightly wrong... | |
126 | + return x => done(y => f([x, y])); | |
127 | 127 | } |
128 | 128 | case "uncurry": { |
129 | 129 | const [f] = l; |
130 | - // XXX should use cont? | |
131 | - return ([x, y]) => bind(f(x), g => done(g(y))); | |
130 | + // XXX ...but also wrong here | |
131 | + return ([x, y]) => bind(f(x), g => cont(() => g(y))); | |
132 | 132 | } |
133 | 133 | case "pr": { |
134 | 134 | const [z, s] = l; |