-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
532 lines (477 loc) · 12.8 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
let MAXRES = 2;
let errors = [];
const error = (s) => {
errors.push(s);
console.error(s);
window.error.innerText = "invalid term: " + errors.toReversed().join(", ");
};
const clearErrors = () => {
errors = [];
window.error.innerText = "";
};
/* canvas */
const WHITE = 0;
const BLACK = 1;
const UNKNOWN = 2;
const drawAt = (worker, x, y, color) => {
worker.postMessage([
color == WHITE ? "white" : color == BLACK ? "black" : "grey",
x[0],
y[0],
x[1] - x[0],
y[1] - y[0],
]);
};
const drawTopLeft = (worker, ctx, color) => {
const newX = [ctx.x[0], ctx.x[0] + (ctx.x[1] - ctx.x[0]) / 2];
const newY = [ctx.y[0], ctx.y[0] + (ctx.y[1] - ctx.y[0]) / 2];
drawAt(worker, newX, newY, color);
return { x: newX, y: newY };
};
const drawTopRight = (worker, ctx, color) => {
const newX = [ctx.x[0] + (ctx.x[1] - ctx.x[0]) / 2, ctx.x[1]];
const newY = [ctx.y[0], ctx.y[0] + (ctx.y[1] - ctx.y[0]) / 2];
drawAt(worker, newX, newY, color);
return { x: newX, y: newY };
};
const drawBottomLeft = (worker, ctx, color) => {
const newX = [ctx.x[0], ctx.x[0] + (ctx.x[1] - ctx.x[0]) / 2];
const newY = [ctx.y[0] + (ctx.y[1] - ctx.y[0]) / 2, ctx.y[1]];
drawAt(worker, newX, newY, color);
return { x: newX, y: newY };
};
const drawBottomRight = (worker, ctx, color) => {
const newX = [ctx.x[0] + (ctx.x[1] - ctx.x[0]) / 2, ctx.x[1]];
const newY = [ctx.y[0] + (ctx.y[1] - ctx.y[0]) / 2, ctx.y[1]];
drawAt(worker, newX, newY, color);
return { x: newX, y: newY };
};
/* lambda calculus */
// ---
// `return null` and `if (foo === null) return null` are the monads of JavaScript!!
// ---
const allHashes = {};
const hash = (s) => {
let h = 0;
for (let i = 0; i < s.length; i++) {
const chr = s.charCodeAt(i);
h = (h << 5) - h + chr;
h |= 0;
}
while (h in allHashes && allHashes[h] !== s) {
console.warn("hash collision");
h += 1;
}
allHashes[h] = s;
return h;
};
const abs = (body) => {
if (body === null) return null;
const t = { type: "abs", body };
t.hash = hash("abs" + body.hash);
return t;
};
const app = (left) => (right) => {
if (left === null || right === null) return null;
const t = { type: "app", left, right };
t.hash = hash("app" + left.hash + right.hash);
return t;
};
const idx = (idx) => {
if (idx === null) return null;
const t = { type: "idx", idx };
t.hash = hash("idx" + idx);
return t;
};
const def = (name) => {
const t = { type: "def", name };
t.hash = hash("def" + name);
return t;
};
const show = (t) => {
if (t === null) return "";
switch (t.type) {
case "abs":
// return `\\${show(t.body)}`;
return `[${show(t.body)}]`;
case "app":
return `(${show(t.left)} ${show(t.right)})`;
case "idx":
return `${t.idx}`;
case "def":
return t.name;
}
};
const isOpen = (t) => {
const go = (t, d) => {
if (t === null) return true;
switch (t.type) {
case "abs":
return go(t.body, d + 1);
case "app":
return go(t.left, d) || go(t.right, d);
case "idx":
return t.idx >= d;
case "def":
return false;
}
};
return go(t, 0);
};
const parseLam = (str) => {
// default to left-associative application
const folded = (s) => {
const init = parseLam(s);
if (!init[1] || ")]".includes(init[1][0])) return init;
const go = (acc, rst) => {
const parsed = parseLam(rst);
const chain = app(acc)(parsed[0]);
if (!parsed[1] || ")]".includes(parsed[1][0])) return [chain, parsed[1]];
return go(chain, parsed[1]);
};
return go(init[0], init[1]);
};
if (!str) {
error("in parseLam");
return [{}, ""];
}
const head = str[0];
const tail = str.slice(1);
switch (head) {
case "λ":
case "\\":
case "[": // bruijn
const [body, _tail] = parseLam(tail.trim());
return [
abs(body),
head == "[" ? _tail.trim().slice(1).trim() : _tail.trim(),
];
case "(":
const [chain, _tail1] = folded(tail.trim());
return [chain, _tail1.trim().slice(1).trim()];
case ")":
case "]":
error("in parseLam");
return [];
default:
if (head == " ") return folded(tail);
if (head >= "a" && head <= "z") {
// substitution
let name = "";
while (str && str[0] >= "a" && str[0] <= "z") {
name += str[0];
str = str.slice(1);
}
return [def(name), str.trim()];
} else {
// de Bruijn index
let num = "";
while (str && str[0] >= "0" && str[0] <= "9") {
num += str[0];
str = str.slice(1);
}
return [idx(parseInt(num)), str.trim()];
}
}
};
const parseBLC = (str) => {
if (!str) {
error("in parseBLC");
return [{}, ""];
}
if (str.slice(0, 2) === "00") {
const [body, tail] = parseBLC(str.slice(2));
return [abs(body), tail];
}
if (str.slice(0, 2) === "01") {
const [left, tail1] = parseBLC(str.slice(2));
const [right, tail2] = parseBLC(tail1);
return [app(left)(right), tail2];
}
const cnt = str.slice(1).indexOf("0");
return [idx(cnt), str.slice(cnt + 2)];
};
const parseTerm = (str) => {
const t = /^[01]+$/.test(str) ? parseBLC(str)[0] : parseLam(str)[0];
if (isOpen(t)) {
error("is open");
return null;
} else {
return t;
}
};
const substDef = (i, t, n) => {
switch (t.type) {
case "idx":
return t;
case "app":
return app(substDef(i, t.left, n))(substDef(i, t.right, n));
case "abs":
return abs(substDef(i + 1, t.body, n));
case "def":
return t.name === n ? idx(i) : t;
}
};
const resolveTerm = (_t, defs) => {
if (_t === null) return null;
let final = _t;
let len = Object.keys(defs).length;
for (let i = len; i > 0; i--) {
final = abs(final);
}
let d = len;
Object.entries(defs).forEach(([n, t]) => {
final = app(substDef(--d - len, final, n))(t);
});
return final;
};
const parse = (str) => {
const defs = {};
let t;
str
.trim()
.split(/\r?\n/)
.every((line) => {
if (line.startsWith("--") || line.length === 0) return true;
if (!line.includes("=")) {
t = resolveTerm(parseTerm(line), defs);
return false;
}
[n, _t] = line.split("=");
defs[n.trim()] = resolveTerm(parseTerm(_t.trim()), defs);
return true;
});
return t;
};
/* lambda screen */
// [[1]]=w, [[0]]=b, other=g
const toColor = (t) => {
if (t.type === "abs" && t.body.type === "abs" && t.body.body.type === "idx")
return t.body.body.idx === 1
? WHITE
: t.body.body.idx === 0
? BLACK
: UNKNOWN;
return UNKNOWN;
};
// [((((0 tl) tr) bl) br)]
const seemsScreeny = (t) =>
t.type === "abs" &&
t.body.type === "app" &&
t.body.left.type === "app" &&
t.body.left.left.type === "app" &&
t.body.left.left.left.type === "app" &&
t.body.left.left.left.left.type === "idx" &&
t.body.left.left.left.left.idx === 0;
const clearScreen = (worker) => {
worker.postMessage("clear");
};
/* beta reduction */
let MAX = 0;
let depth = 0;
let canceled = false;
const cancelReduction = () => {
if (depth++ > MAX && !canceled) {
MAX **= 1.3;
if (
!confirm(
`This takes awfully long (${depth} steps!). The reduction potentially won't converge to a valid screen (or at all!). Do you want to continue?\nWarning: This might crash your browser!`,
)
) {
canceled = true;
return true;
}
}
return canceled;
};
const incCache = {};
const inc = (i, t) => {
if (cancelReduction() || t === null) {
error("in inc");
return null;
}
if (t.hash in incCache && i in incCache[t.hash]) return incCache[t.hash][i];
let newT;
switch (t.type) {
case "idx":
newT = idx(i <= t.idx ? t.idx + 1 : t.idx);
break;
case "app":
newT = app(inc(i, t.left))(inc(i, t.right));
break;
case "abs":
newT = abs(inc(i + 1, t.body));
break;
case "def":
error("unexpected def");
return null;
}
if (!(t.hash in incCache)) incCache[t.hash] = {};
incCache[t.hash][i] = newT;
return newT;
};
const substCache = {};
const subst = (i, t, s) => {
if (cancelReduction() || t === null) {
error("in subst");
return null;
}
const h = hash("" + t.hash + s.hash);
if (h in substCache && i in substCache[h]) return substCache[h][i];
let newT;
switch (t.type) {
case "idx":
newT = i == t.idx ? s : idx(t.idx > i ? t.idx - 1 : t.idx);
break;
case "app":
newT = app(subst(i, t.left, s))(subst(i, t.right, s));
break;
case "abs":
newT = abs(subst(i + 1, t.body, inc(0, s)));
break;
case "def":
error("unexpected def");
return null;
}
if (!(h in substCache)) substCache[h] = {};
substCache[h][i] = newT;
return newT;
};
// guaranteed normal form
// only use if sure that t is not a (potentially diverging) screen
const gnf = (t) => {
if (cancelReduction() || t === null) {
error("in gnf");
return null;
}
switch (t.type) {
case "app":
const _left = gnf(t.left);
if (_left === null) return null;
return _left.type === "abs"
? gnf(subst(0, _left.body, t.right))
: app(_left)(gnf(t.right));
case "abs":
return abs(gnf(t.body));
case "def":
error("unexpected def");
return null;
default:
return t;
}
};
// weak head normal form
const whnfCache = {};
const whnf = (t) => {
if (cancelReduction() || t === null) {
error("in whnf");
return null;
}
if (t.hash in whnfCache) return whnfCache[t.hash];
let newT;
switch (t.type) {
case "app":
const _left = whnf(t.left);
if (_left === null) return null;
newT =
_left.type === "abs"
? whnf(subst(0, _left.body, t.right))
: app(_left)(t.right);
break;
case "def":
error("unexpected def");
return null;
default:
newT = t;
break;
}
whnfCache[t.hash] = newT;
return newT;
};
// screen normal form
// one of [((((0 tl) tr) bl) br)], [[0]], [[1]]
const snfCache = {};
const snf = (_t) => {
if (_t !== null && _t.hash in snfCache) return snfCache[_t.hash];
let t = whnf(_t);
if (t === null || t.type !== "abs") {
error("not a screen/pixel");
return null;
}
t = abs(whnf(t.body));
if (t.body.type === "abs") return gnf(t); // not a screen, probably a pixel
while (t !== null && !seemsScreeny(t)) {
switch (t.type) {
case "app":
const _left = whnf(t.left);
t =
_left.type === "abs"
? subst(0, _left.body, t.right)
: app(_left)(whnf(t.right));
break;
case "abs":
t = abs(whnf(t.body));
break;
case "def":
error("unexpected def");
return null;
default:
error("type");
return null;
}
}
snfCache[_t.hash] = t;
return t;
};
const reduceLoop = (worker, root, _t) => {
const stack = [{ ctx: root, t: _t }];
for (let i = 0; stack.length > 0; i++) {
// console.log(i, stack.length);
// let [{ ctx, t }] = stack.splice(
// Math.floor(Math.random() * stack.length),
// 1,
// );
let { ctx, t } = stack.shift();
// let { ctx, t } = stack.pop();
// TODO: priority queue on context size
if (toColor(t) !== UNKNOWN) continue;
// could loop in gnf, therefore limit depth
MAX = 10000000;
depth = 0;
canceled = false;
try {
t = snf(t);
} catch (e) {
if (e.message == "too much recursion")
error(
"your term most probably has some logical error (e.g. by not converging to a screen); if not, lmk",
);
error(e);
return null;
}
if (t === null) {
error("in reduceLoop");
return null;
}
// smaller resolutions apparently crash the browser tab lol
if (ctx.x[1] - ctx.x[0] < MAXRES) continue;
if (seemsScreeny(t)) {
const tl = t.body.left.left.left.right;
stack.push({ ctx: drawTopLeft(worker, ctx, toColor(tl)), t: tl });
const tr = t.body.left.left.right;
stack.push({ ctx: drawTopRight(worker, ctx, toColor(tr)), t: tr });
const bl = t.body.left.right;
stack.push({ ctx: drawBottomLeft(worker, ctx, toColor(bl)), t: bl });
const br = t.body.right;
stack.push({ ctx: drawBottomRight(worker, ctx, toColor(br)), t: br });
} else {
// TODO: could we risk gnfing here?
drawAt(worker, ctx.x, ctx.y, toColor(t));
}
}
};
function helpSyntax() {
alert(
"The syntax uses standard notations for de Bruijn indexed lambda calculus. The de Bruijn indices start at 0. You can use `\\`, `λ`, or `[..]` for abstractions. Applications use parenthesis and assume left associativity by default. All terms can also be replaced by a string of binary lambda calculus (BLC) - useful if you're not comfortable with de Bruijn indices (e.g. by using John Tromp's `lam` compiler).\nYou can define substitutions (like in the presets) using `=`.\n\nHave fun!",
);
}