-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda.typ
218 lines (190 loc) · 5.58 KB
/
lambda.typ
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
#import "printing.typ": expr-to-str
#import "parsing.typ": parse-literal
#let free-vars(expr) = {
if expr.type == "value" {
return (expr.name,)
} else if expr.type == "application" {
return (free-vars(expr.fn) + free-vars(expr.param)).dedup()
} else if expr.type == "abstraction" {
return free-vars(expr.body).filter(it => it != expr.param)
}
}
#let alpha-conversion-impl(expr, old, new) = {
if expr.type == "abstraction" {
if expr.param == old {
return expr
} else {
expr.body = alpha-conversion-impl(expr.body, old, new)
return expr
}
} else if expr.type == "application" {
expr.fn = alpha-conversion-impl(expr.fn, old, new)
expr.param = alpha-conversion-impl(expr.param, old, new)
return expr
} else if expr.type == "value" {
expr.name = new
return expr
}
}
#let alpha-conversion(expr, new) = {
if type(new) != str {
panic("New parameter name has to be of type str")
}
parse-literal(new.codepoints())
if expr.type != "abstraction" {
panic("Can only apply λ-Calculus alpha-conversion on abstraction, got: '" + expr.type + "'")
}
if free-vars(expr).contains(new) {
panic("Cannot apply λ-Calculus alpha-conversion (new variable name '" + new + "' already bound): '" + expr-to-str(expr) + "'")
}
let old = expr.param
expr.param = new
return alpha-conversion-impl(expr, old, new)
}
#let beta-reduction-impl(expr, param, value) = {
if expr.type == "value" {
if expr.name == param {
return value
} else {
return expr
}
} else if expr.type == "application" {
expr.fn = beta-reduction-impl(expr.fn, param, value)
expr.param = beta-reduction-impl(expr.param, param, value)
return expr
} else if expr.type == "abstraction" {
if expr.param == param {
return expr
} else {
if expr.param in free-vars(value) {
panic("Cannot apply λ-Calculus beta-reduction (free variable '" + expr.param + "' would be bound): '" + expr-to-str(expr) + "'")
} else {
expr.body = beta-reduction-impl(expr.body, param, value)
return expr
}
}
}
}
#let beta-reduction(expr) = {
if expr.type != "application" {
panic("Can only apply λ-Calculus beta-reduction on applications, got: '" + expr.type + "'")
}
if expr.fn.type != "abstraction" {
panic("Can only apply λ-Calculus beta-reduction on applications on abstractions, was: '" + expr.fn.type + "'")
}
return beta-reduction-impl(expr.fn.body, expr.fn.param, expr.param)
}
#let eta-reduction(expr) = {
if expr.type != "abstraction" {
panic("Can only apply λ-Calculus eta-reduction on abstractions, got: '" + expr.type + "'")
}
if expr.body.type != "application" {
panic("Can only apply λ-Calculus eta-reduction on abstractions with an application body, got: '" + expr.body.type + "'")
}
if expr.body.param.type != "value" or expr.body.param.name != expr.param {
panic("Can only apply λ-Calculus eta-reduction on abstractions with an application body with the abstraction variable on the right side, got: '" + expr.body.type + "'")
}
return expr.body.fn
}
#let normalize-is-reducable(expr) = {
if expr.type == "value" {
return false
} else if expr.type == "application" {
if expr.fn.type == "abstraction" {
return true
} else {
return normalize-is-reducable(expr.fn) or normalize-is-reducable(expr.param)
}
} else if expr.type == "abstraction" {
return normalize-is-reducable(expr.body)
}
}
#let normalize-reduce(expr) = {
if expr.type == "value" {
return expr
} else if expr.type == "application" {
if expr.fn.type == "abstraction" {
return beta-reduction(expr)
} else {
if normalize-is-reducable(expr.fn) {
expr.fn = normalize-reduce(expr.fn)
return expr
} else {
expr.param = normalize-reduce(expr.param)
return expr
}
}
} else if expr.type == "abstraction" {
expr.body = normalize-reduce(expr.body)
return expr
}
}
#let normalization-steps(expr) = {
let steps = (expr,)
while normalize-is-reducable(expr) {
expr = normalize-reduce(expr)
if expr in steps {
panic("λ-Calculus expression not normalizable")
}
steps.push(expr)
}
return steps
}
#let normalize(expr) = {
return normalization-steps(expr).last()
}
#let is-normalizable(expr) = {
let prev = (expr,)
while normalize-is-reducable(expr) {
expr = normalize-reduce(expr)
if expr in prev {
return false
}
prev.push(expr)
}
return true
}
#let is-normalform(expr) = {
if is-normalizable(expr) {
return normalize(expr) == expr
} else {
return false
}
}
#let count-binds(expr) = {
if expr.type == "application" {
return count-binds(expr.fn) + count-binds(expr.param)
} else if expr.type == "abstraction" {
return 1 + count-binds(expr.body)
} else if expr.type == "value" {
return 0
}
}
#let tag(
expr,
bound-ranks: (:),
index: 0,
) = {
let _tag(expr, bound-ranks, index) = tag(
expr,
bound-ranks: bound-ranks,
index: index,
)
if expr.type == "value" {
if expr.name in bound-ranks {
expr.insert("index", bound-ranks.at(expr.name))
}
return expr
} else if expr.type == "application" {
expr.fn = _tag(expr.fn, bound-ranks, index)
expr.param = _tag(expr.param, bound-ranks, index + count-binds(expr.fn))
return expr
} else if expr.type == "abstraction" {
bound-ranks.insert(expr.param, index)
expr.insert("index", index)
expr.body = _tag(expr.body, bound-ranks, index + 1)
return expr
} else {
panic("Not a valid λ-Calculus type: '" + expr.type + "'")
}
}