Skip to content

Commit 238b2e8

Browse files
committed
complete playground for lambda-calculus (still not ideal though)
1 parent e8e14d0 commit 238b2e8

3 files changed

Lines changed: 20 additions & 16 deletions

File tree

content/posts/lambda_calculus.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title = "无类型 λ 演算"
33
description = "最经典的类型论例子:无类型 λ 演算的规则、使用和合流性。"
44
date = 2025-03-22
5-
updated = 2025-04-12
5+
updated = 2026-03-23
66

77
[extra]
88
math = true
@@ -20,7 +20,7 @@ tags = ["笔记", "数学", "计算机", "函数式编程", "可运行"]
2020

2121
本节讨论的是 Alonzo Church 发明的 lambda calculus **无类型 λ 演算**[^paper-invention]
2222

23-
本站提供了一个[在线演绎器](/playground/lambda_calculus.html)
23+
本站提供了一个[在线演绎器](/playground/lambda_calculus.html),其中使用 `@eval` 命令做正则次序的求值并尝试简化,输出使用 de Brujin 无名表示(`#i` 表示从里到外第 $i$ 层函数声明对应的参数)。想要自行编写解释器的读者可参考其仓库 [Rratic/my-lam](https://github.com/Rratic/my-lam).
2424

2525
## 语法 {#grammar}
2626
Lambda 演算的语法形式极其简单。一种可理解的形式文法如下:

static/playground/lambda_calculus.html

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,45 +13,46 @@
1313
<body>
1414
<div id="terminal">
1515
<div class="output stream"></div>
16-
<div class="line input"><span id="to-click" class="prompt">>>> </span><span id="to-input" class="command" contenteditable="true">// ===== Standard Library =====
17-
// SK Pack
16+
<div class="line input"><span id="to-click" class="prompt">>>> </span><span id="to-input" class="command" contenteditable="true"># ===== Standard Library =====
17+
# SK Pack
1818
S = λx. λy. λz. x z (y z)
1919
K = λx. λy. x
2020
I = S K K
2121
ι = λf. (f S) K
2222

23-
// Boolean Pack
23+
# Boolean Pack
2424
T = λx. λy. x
2525
F = λx. λy. y
2626
IfThenElse = λe. λu. λv.e u v
2727

28-
// List Pack
28+
# List Pack
2929
Nil = λc. λn. n
3030
Cons = λh. λt. λc. λn. c h (t c n)
3131

32-
// Natural Number Pack
32+
# Natural Number Pack
3333
0 = λf. λs. s
3434
1 = λf. λs. f s
3535
2 = λf. λs. f (f s)
3636
3 = λf. λs. f (f (f s))
3737

3838
IsZero = λn. n (λx. F) T
3939
Succ = λn. λf. λs. f (n f s)
40-
Plus = λn. λm. λf. λx. n f (m f x)
41-
Mul = λn. λm. λf. λx. n (m f) x
40+
Plus = λn. λm. n Succ m
41+
Mul = λn. λm. n (Plus m) 0
4242

43-
// High Order Functions
44-
// TODO
43+
Prev = λn. λf. λx. n (λg. λh. h (g f)) (λu. x) λu. u
4544

46-
// Recursion
45+
# High Order Functions
46+
# TODO
47+
48+
# Recursion
4749
Ω = λx. x x
4850
Y = λf. (λx. f (x x)) λx. f (x x)
4951
Z = λf. (λx. f (λy. (x x) y)) (λx. f (λy. (x x) y))
5052

51-
P = λn. λf. λx. n (λg. λh. h (g f)) (λu. x) λu. u
52-
Fact = λf. λn. IsZero n 1 (Mul n (f (P n)))
53+
Fact = λf. λn. IfThenElse (IsZero n) 1 (Mul n (f (Prev n)))
5354

54-
// ===== Test =====
55+
# ===== Test =====
5556
@eval write_your_expr_here</span>
5657
</div>
5758
</div>
@@ -82,7 +83,10 @@
8283
else {
8384
let span = document.createElement("span");
8485
span.innerText = token;
85-
if (token == "λ") {
86+
if (token[0] == "#") {
87+
span.style.color = "green";
88+
}
89+
else if (token == "λ") {
8690
span.style.color = "purple";
8791
}
8892
else if (token == "@") {

static/script/lambda/stt_bg.wasm

60.9 KB
Binary file not shown.

0 commit comments

Comments
 (0)