-
Notifications
You must be signed in to change notification settings - Fork 2
/
lambda-js.ts
690 lines (550 loc) · 14.7 KB
/
lambda-js.ts
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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
// A parser and evaluator for lambda calculus implemented using JavaScript code.
// #parser
export type Token =
| { readonly type: "backslash"; readonly name: string }
| { readonly type: "name"; readonly name: string }
| { readonly type: "leftParen" }
| { readonly type: "rightParen" }
const whitespace = /\s/
export function transform(text: string): string {
const aliases: [name: string, value: string][] = []
text = text.replace(/^([^\s()\\λ]+)\s+=\s+([^;]+);/gm, (_, name, value) => {
aliases.push([name, value])
return ""
})
for (const alias of aliases.reverse()) {
text = `(\\${alias[0]} ${text})(${alias[1]})`
}
return text
}
export function lex(text: string): readonly Token[] {
const tokens: (
| { type: "backslash" }
| { type: "leftParen" }
| { type: "rightParen" }
| { type: "name"; name: string }
)[] = []
let currentWord = ""
for (const char of text) {
if (
char == "\\" ||
char == "λ" ||
char == "(" ||
char == ")" ||
whitespace.test(char)
) {
if (currentWord) {
tokens.push({ type: "name", name: currentWord })
currentWord = ""
}
if (char == "\\" || char == "λ" || char == "(" || char == ")") {
tokens.push({
type:
char == "\\" || char == "λ"
? "backslash"
: char == "("
? "leftParen"
: "rightParen",
})
}
} else {
currentWord += char
}
}
if (currentWord) {
tokens.push({ type: "name", name: currentWord })
currentWord = ""
}
const output: Token[] = []
let backslash: { type: "backslash"; name: string } | undefined
for (const token of tokens) {
if (backslash) {
if (token.type == "name") {
backslash.name = token.name
output.push(backslash)
backslash = undefined
} else {
throw new Error(`Expected name; received ${token.type}.`)
}
} else if (token.type == "backslash") {
backslash = {
type: "backslash",
name: "",
}
} else {
output.push(token)
}
}
if (backslash) {
throw new Error("Expected name; received EOF.")
}
return output
}
export type PartialTree =
| Name<string>
| (readonly PartialTree[] & { type?: undefined })
| {
readonly type: "backslash"
readonly name: string
}
export function partialTree(tokens: readonly Token[]): PartialTree {
let currentBranch: PartialTree[] = []
const branches: PartialTree[][] = [currentBranch]
for (const token of tokens) {
switch (token.type) {
case "name":
currentBranch.push(new Name(token.name))
break
case "backslash":
currentBranch.push(token)
break
case "leftParen": {
const nextBranch: PartialTree[] = []
currentBranch.push(nextBranch)
branches.push(nextBranch)
currentBranch = nextBranch
break
}
case "rightParen": {
branches.pop()
const nextBranch = branches.at(-1)
if (!nextBranch || branches.length < 1) {
throw new Error("Found unmatched closing parenthesis.")
}
currentBranch = nextBranch
}
}
}
return currentBranch
}
export type Tree =
| Name<string>
| readonly Tree[]
| {
readonly type: "backslash"
readonly name: string
readonly body: Tree[]
}
export function tree(partialTree: PartialTree): Tree {
if (partialTree instanceof Name) {
return partialTree
}
if (partialTree.type == "backslash") {
throw new Error("'tree' cannot parse a lone backslash.")
}
let currentOutput: Tree[] = []
const output: Tree[] = currentOutput
for (const token of partialTree) {
if (token.type == "backslash") {
const nextOutput: Tree[] = []
const lambda: Tree = {
type: "backslash",
body: nextOutput,
name: token.name,
}
currentOutput.push(lambda)
currentOutput = nextOutput
} else {
currentOutput.push(tree(token))
}
}
return output
}
export function node(tree: Tree): Node {
if (tree instanceof Name) {
return tree
}
if (Array.isArray(tree)) {
if (tree.length == 0) {
throw new Error("Expected token; received rightParen.")
}
let output = node(tree[0]!)
for (let index = 1; index < tree.length; index++) {
output = new Application(output, node(tree[index]!))
}
return output
}
return new Lambda(tree.name, node(tree.body))
}
export function parse(text: string) {
return node(tree(partialTree(lex(transform(text)))))
}
function indent(text: string): string {
return text.split("\n").join("\n ")
}
function mergeLocals(a: readonly string[], b: readonly string[]): string[] {
const output = a.slice()
for (const word of b) {
if (!output.includes(word)) {
output.push(word)
}
}
return output
}
export type PartialString = {
readonly content: string
readonly endsWithLambda: boolean
readonly hasTopLevelApplication: boolean
}
const alphabet = "abcdefghijklmnopqrstuvwxyz"
export abstract class Node {
declare type?: undefined
abstract readonly canEval: boolean
abstract readonly locals: readonly string[]
abstract eval(): Node
abstract isBound(name: string): boolean
abstract isFree(name: string): boolean
abstract rename(oldName: string, newName: Name<string>): Node
abstract replace(name: string, node: Node): Node
abstract toJS(): string
abstract toPartialCompactString(): PartialString
abstract toPartialString(): PartialString
ski(): Node {
return this.replace("S", S).replace("K", K).replace("I", I)
}
toCompactString(): string {
return this.toPartialCompactString().content
}
toString(): string {
return this.toPartialString().content
}
uniqueName(avoid: readonly string[]): string {
avoid = mergeLocals(this.locals, avoid)
for (const letter of alphabet) {
if (!avoid.includes(letter)) {
return letter
}
}
for (let index = 1; ; index++) {
if (!avoid.includes("x" + index)) {
return "x" + index
}
}
}
}
export class Name<T extends string> extends Node {
static escape(name: string) {
return name.replace(/^\d|[^A-Za-z$_]/g, (char) => "_" + char.charCodeAt(0))
}
readonly canEval = false
readonly locals: readonly string[]
constructor(readonly name: T) {
super()
this.locals = [name]
}
eval(): Node {
return this
}
isBound(): boolean {
return false
}
isFree(name: string): boolean {
return name == this.name
}
replace(name: string, node: Node): Node {
if (name == this.name) {
return node
}
return this
}
rename(oldName: string, newName: Name<string>): Node {
if (oldName == this.name) {
return newName
}
return this
}
toJS(): string {
return `$.lazy(() => ${Name.escape(this.name)})`
}
toPartialCompactString(): PartialString {
return {
content: this.name,
endsWithLambda: false,
hasTopLevelApplication: false,
}
}
toPartialString(): PartialString {
return {
content: this.name,
endsWithLambda: false,
hasTopLevelApplication: false,
}
}
}
export class Application<L extends Node, R extends Node> extends Node {
readonly canEval: boolean
readonly locals: readonly string[]
constructor(readonly left: L, readonly right: R) {
super()
this.canEval =
this.left instanceof Lambda || this.left.canEval || this.right.canEval
this.locals = mergeLocals(this.left.locals, this.right.locals)
}
eval(): Node {
if (!this.canEval) {
return this
}
if (this.left instanceof Lambda) {
return this.left.apply(this.right)
}
if (this.left.canEval) {
return new Application(this.left.eval(), this.right)
}
if (this.right.canEval) {
return new Application(this.left, this.right.eval())
}
return this
}
isBound(name: string): boolean {
return this.left.isBound(name) || this.right.isBound(name)
}
isFree(name: string): boolean {
return this.left.isFree(name) || this.right.isFree(name)
}
replace(name: string, node: Node): Node {
return new Application(
this.left.replace(name, node),
this.right.replace(name, node),
)
}
rename(oldName: string, newName: Name<string>): Node {
return new Application(
this.left.rename(oldName, newName),
this.right.rename(oldName, newName),
)
}
toJS(): string {
return `$.lazy(() => $.apply(${this.left.toJS()}, ${this.right.toJS()}))`
}
toPartialCompactString(): PartialString {
const left = this.left.toPartialCompactString()
const right = this.right.toPartialCompactString()
const content =
(left.endsWithLambda ? `(${left.content})` : left.content) +
(right.hasTopLevelApplication ? `(${right.content})` : right.content)
return {
content,
endsWithLambda: right.endsWithLambda,
hasTopLevelApplication: true,
}
}
toPartialString(): PartialString {
const left = this.left.toPartialString()
const right = this.right.toPartialString()
const content =
(left.endsWithLambda ? `(${left.content})` : left.content) +
" " +
(right.hasTopLevelApplication ? `(${right.content})` : right.content)
return {
content,
endsWithLambda: right.endsWithLambda,
hasTopLevelApplication: true,
}
}
}
export class Lambda<T extends Node> extends Node {
readonly canEval: boolean
readonly locals: readonly string[]
constructor(readonly name: string, readonly body: T) {
super()
this.canEval = body.canEval
this.locals = this.body.locals
.filter((local) => local !== name)
.concat(name)
}
apply(value: Node): Node {
return this.body.replace(this.name, value)
}
eval(): Node {
if (!this.canEval) {
return this
}
return new Lambda(this.name, this.body.eval())
}
isBound(name: string): boolean {
return name == this.name || this.body.isBound(name)
}
isFree(name: string): boolean {
return name != this.name && this.body.isFree(name)
}
replace(name: string, node: Node): Node {
if (name == this.name) {
return this
}
let self: Lambda<Node> = this
if (node.isFree(this.name)) {
const newName = this.uniqueName(node.locals)
self = new Lambda(newName, this.body.rename(this.name, new Name(newName)))
}
return new Lambda(self.name, self.body.replace(name, node))
}
rename(oldName: string, newName: Name<string>): Node {
if (this.name == oldName) {
return this
}
return new Lambda(this.name, this.body.rename(oldName, newName))
}
toJS(): string {
return `((${Name.escape(this.name)}) => (${this.body.toJS()}))`
}
toPartialCompactString(): PartialString {
const body = this.body.toPartialCompactString()
const content = body.content.startsWith("λ")
? "λ" + this.name + body.content.slice(1)
: "λ" + this.name + "." + body.content
return {
content,
endsWithLambda: true,
hasTopLevelApplication: false,
}
}
toPartialString(): PartialString {
const body = this.body.toPartialString()
const content = `λ${this.name} ` + body.content
return {
content,
endsWithLambda: true,
hasTopLevelApplication: false,
}
}
}
export const S = new Lambda(
"x",
new Lambda(
"y",
new Lambda(
"z",
new Application(
new Application(new Name("x"), new Name("z")),
new Application(new Name("y"), new Name("z")),
),
),
),
)
export const K = new Lambda("x", new Lambda("y", new Name("x")))
export const I = new Lambda("x", new Name("x"))
export namespace $ {
export class Lazy {
private value?: unknown
constructor(private readonly fn: () => unknown) {}
getValue() {
if (this.value) {
return this.value
}
const value = this.fn()
this.value = value
return value
}
}
export function lazy(fn: () => unknown) {
return new Lazy(fn)
}
export function unwrap(value: unknown) {
while (value instanceof Lazy) {
value = value.getValue()
}
return value
}
export function apply(a: unknown, b: unknown) {
return new Lazy(() => {
return (unwrap(a) as Function)(unwrap(b))
})
}
}
Object.assign(globalThis, { $ })
export type Value =
| { type: "nil" }
| { type: "true" }
| { type: "false/0" }
| { type: "pair"; left: Value; right: Value }
| { type: "numeral"; value: number }
| { type: "unknown" }
export function getValue(value: unknown): Value {
const { apply, unwrap } = $
// https://docs.google.com/spreadsheets/d/1JrqoqQ2Kkxk3VWvJCrBQG_5GvtfZY1oi_koqeg84jIs/edit?usp=sharing
// has a table of different lambda calculus types. It also shows different
// ways to figure out what a given type is. This script assumes `value` is
// either nil (\x true), a pair, true, false, or a Church numeral.
try {
// Check #3: finds `nil` and `true`
const output = unwrap(
apply(
apply(
apply(value, (x: number) => x + 1),
true,
),
false,
),
)
if (output === true) {
return { type: "nil" }
}
if (output === 1) {
return { type: "true" }
}
} catch (error) {
if (error instanceof ReferenceError) {
throw error
}
}
try {
// Check #4: finds `0` and `false`, which are then indistinguishable
const output = unwrap(apply(apply(value, true), false))
if (output === false) {
return { type: "false/0" }
}
} catch (error) {
if (error instanceof ReferenceError) {
throw error
}
}
try {
// Check #5: finds `pair`
let a: unknown, b: unknown
const symbol = Symbol()
const output = unwrap(
apply(value, (_a: unknown) => (_b: unknown) => {
a = _a
b = _b
return symbol
}),
)
if (output === symbol) {
return { type: "pair", left: getValue(a), right: getValue(b) }
}
} catch (error) {
if (error instanceof ReferenceError) {
throw error
}
}
try {
// Check #6: finds `integer`
const output = unwrap(
apply(
apply(value, (x: number) => x + 1),
0,
),
)
if (typeof output == "number") {
return { type: "numeral", value: output }
}
} catch (error) {
if (error instanceof ReferenceError) {
throw error
}
}
return { type: "unknown" }
}
export function valueToString(value: Value): string {
switch (value.type) {
case "nil":
case "true":
case "false/0":
case "unknown":
return value.type
case "numeral":
return "" + value.value
case "pair":
return `(${valueToString(value.left)}, ${valueToString(value.right)})`
}
}