forked from gboduljak/stanford-compilers-coursework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lam.cl
477 lines (428 loc) · 10.2 KB
/
lam.cl
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
(* A program for
1. Representing lambda terms
2. Interpreting lambda terms
3. Compiling lambda calculus programs to Cool
The lambda calculus is described by the following grammar:
e ::= x a variable
| \x.e a function with argument x
| e1@e2 apply function e1 to argument e2
Jeff Foster ([email protected])
March 24, 2000
*)
(*
* A list of variables. We use this to do de Bruijn numbering
*
*)
class VarList inherits IO {
isNil() : Bool { true };
head() : Variable { { abort(); new Variable; } };
tail() : VarList { { abort(); new VarList; } };
add(x : Variable) : VarList { (new VarListNE).init(x, self) };
print() : SELF_TYPE { out_string("\n") };
};
class VarListNE inherits VarList {
x : Variable;
rest : VarList;
isNil() : Bool { false };
head() : Variable { x };
tail() : VarList { rest };
init(y : Variable, r : VarList) : VarListNE { { x <- y; rest <- r; self; } };
print() : SELF_TYPE { { x.print_self(); out_string(" ");
rest.print(); self; } };
};
(*
* A list of closures we need to build. We need to number (well, name)
* the closures uniquely.
*)
class LambdaList {
isNil() : Bool { true };
headE() : VarList { { abort(); new VarList; } };
headC() : Lambda { { abort(); new Lambda; } };
headN() : Int { { abort(); 0; } };
tail() : LambdaList { { abort(); new LambdaList; } };
add(e : VarList, x : Lambda, n : Int) : LambdaList {
(new LambdaListNE).init(e, x, n, self)
};
};
class LambdaListNE inherits LambdaList {
lam : Lambda;
num : Int;
env : VarList;
rest : LambdaList;
isNil() : Bool { false };
headE() : VarList { env };
headC() : Lambda { lam };
headN() : Int { num };
tail() : LambdaList { rest };
init(e : VarList, l : Lambda, n : Int, r : LambdaList) : LambdaListNE {
{
env <- e;
lam <- l;
num <- n;
rest <- r;
self;
}
};
};
class LambdaListRef {
nextNum : Int <- 0;
l : LambdaList;
isNil() : Bool { l.isNil() };
headE() : VarList { l.headE() };
headC() : Lambda { l.headC() };
headN() : Int { l.headN() };
reset() : SELF_TYPE {
{
nextNum <- 0;
l <- new LambdaList;
self;
}
};
add(env : VarList, c : Lambda) : Int {
{
l <- l.add(env, c, nextNum);
nextNum <- nextNum + 1;
nextNum - 1;
}
};
removeHead() : SELF_TYPE {
{
l <- l.tail();
self;
}
};
};
(*
* Lambda expressions
*
*)
-- A pure virtual class representing any expression
class Expr inherits IO {
-- Print this lambda term
print_self() : SELF_TYPE {
{
out_string("\nError: Expr is pure virtual; can't print self\n");
abort();
self;
}
};
-- Do one step of (outermost) beta reduction to this term
beta() : Expr {
{
out_string("\nError: Expr is pure virtual; can't beta-reduce\n");
abort();
self;
}
};
-- Replace all occurrences of x by e
substitute(x : Variable, e : Expr) : Expr {
{
out_string("\nError: Expr is pure virtual; can't substitute\n");
abort();
self;
}
};
-- Generate Cool code to evaluate this expression
gen_code(env : VarList, closures : LambdaListRef) : Object {
{
out_string("\nError: Expr is pure virtual; can't gen_code\n");
abort();
self;
}
};
};
(*
* Variables
*)
class Variable inherits Expr {
name : String;
init(n:String) : Variable {
{
name <- n;
self;
}
};
print_self() : SELF_TYPE {
out_string(name)
};
beta() : Expr { self };
substitute(x : Variable, e : Expr) : Expr {
if x = self then e else self fi
};
gen_code(env : VarList, closures : LambdaListRef) : Object {
let cur_env : VarList <- env in
{ while (if cur_env.isNil() then
false
else
not (cur_env.head() = self)
fi) loop
{ out_string("get_parent().");
cur_env <- cur_env.tail();
}
pool;
if cur_env.isNil() then
{ out_string("Error: free occurrence of ");
print_self();
out_string("\n");
abort();
self;
}
else
out_string("get_x()")
fi;
}
};
};
(*
* Functions
*)
class Lambda inherits Expr {
arg : Variable;
body : Expr;
init(a:Variable, b:Expr) : Lambda {
{
arg <- a;
body <- b;
self;
}
};
print_self() : SELF_TYPE {
{
out_string("\\");
arg.print_self();
out_string(".");
body.print_self();
self;
}
};
beta() : Expr { self };
apply(actual : Expr) : Expr {
body.substitute(arg, actual)
};
-- We allow variables to be reused
substitute(x : Variable, e : Expr) : Expr {
if x = arg then
self
else
let new_body : Expr <- body.substitute(x, e),
new_lam : Lambda <- new Lambda in
new_lam.init(arg, new_body)
fi
};
gen_code(env : VarList, closures : LambdaListRef) : Object {
{
out_string("((new Closure");
out_int(closures.add(env, self));
out_string(").init(");
if env.isNil() then
out_string("new Closure))")
else
out_string("self))") fi;
self;
}
};
gen_closure_code(n : Int, env : VarList,
closures : LambdaListRef) : SELF_TYPE {
{
out_string("class Closure");
out_int(n);
out_string(" inherits Closure {\n");
out_string(" apply(y : EvalObject) : EvalObject {\n");
out_string(" { out_string(\"Applying closure ");
out_int(n);
out_string("\\n\");\n");
out_string(" x <- y;\n");
body.gen_code(env.add(arg), closures);
out_string(";}};\n");
out_string("};\n");
}
};
};
(*
* Applications
*)
class App inherits Expr {
fun : Expr;
arg : Expr;
init(f : Expr, a : Expr) : App {
{
fun <- f;
arg <- a;
self;
}
};
print_self() : SELF_TYPE {
{
out_string("((");
fun.print_self();
out_string(")@(");
arg.print_self();
out_string("))");
self;
}
};
beta() : Expr {
case fun of
l : Lambda => l.apply(arg); -- Lazy evaluation
e : Expr =>
let new_fun : Expr <- fun.beta(),
new_app : App <- new App in
new_app.init(new_fun, arg);
esac
};
substitute(x : Variable, e : Expr) : Expr {
let new_fun : Expr <- fun.substitute(x, e),
new_arg : Expr <- arg.substitute(x, e),
new_app : App <- new App in
new_app.init(new_fun, new_arg)
};
gen_code(env : VarList, closures : LambdaListRef) : Object {
{
out_string("(let x : EvalObject <- ");
fun.gen_code(env, closures);
out_string(",\n");
out_string(" y : EvalObject <- ");
arg.gen_code(env, closures);
out_string(" in\n");
out_string(" case x of\n");
out_string(" c : Closure => c.apply(y);\n");
out_string(" o : Object => { abort(); new EvalObject; };\n");
out_string(" esac)");
}
};
};
(*
* Term: A class for building up terms
*
*)
class Term inherits IO {
(*
* The basics
*)
var(x : String) : Variable {
let v : Variable <- new Variable in
v.init(x)
};
lam(x : Variable, e : Expr) : Lambda {
let l : Lambda <- new Lambda in
l.init(x, e)
};
app(e1 : Expr, e2 : Expr) : App {
let a : App <- new App in
a.init(e1, e2)
};
(*
* Some useful terms
*)
i() : Expr {
let x : Variable <- var("x") in
lam(x,x)
};
k() : Expr {
let x : Variable <- var("x"),
y : Variable <- var("y") in
lam(x,lam(y,x))
};
s() : Expr {
let x : Variable <- var("x"),
y : Variable <- var("y"),
z : Variable <- var("z") in
lam(x,lam(y,lam(z,app(app(x,z),app(y,z)))))
};
};
(*
*
* The main method -- build up some lambda terms and try things out
*
*)
class Main inherits Term {
-- Beta-reduce an expression, printing out the term at each step
beta_reduce(e : Expr) : Expr {
{
out_string("beta-reduce: ");
e.print_self();
let done : Bool <- false,
new_expr : Expr in
{
while (not done) loop
{
new_expr <- e.beta();
if (new_expr = e) then
done <- true
else
{
e <- new_expr;
out_string(" =>\n");
e.print_self();
}
fi;
}
pool;
out_string("\n");
e;
};
}
};
eval_class() : SELF_TYPE {
{
out_string("class EvalObject inherits IO {\n");
out_string(" eval() : EvalObject { { abort(); self; } };\n");
out_string("};\n");
}
};
closure_class() : SELF_TYPE {
{
out_string("class Closure inherits EvalObject {\n");
out_string(" parent : Closure;\n");
out_string(" x : EvalObject;\n");
out_string(" get_parent() : Closure { parent };\n");
out_string(" get_x() : EvalObject { x };\n");
out_string(" init(p : Closure) : Closure {{ parent <- p; self; }};\n");
out_string(" apply(y : EvalObject) : EvalObject { { abort(); self; } };\n");
out_string("};\n");
}
};
gen_code(e : Expr) : Object {
let cl : LambdaListRef <- (new LambdaListRef).reset() in
{
out_string("Generating code for ");
e.print_self();
out_string("\n------------------cut here------------------\n");
out_string("(*Generated by lam.cl (Jeff Foster, March 2000)*)\n");
eval_class();
closure_class();
out_string("class Main {\n");
out_string(" main() : EvalObject {\n");
e.gen_code(new VarList, cl);
out_string("\n};\n};\n");
while (not (cl.isNil())) loop
let e : VarList <- cl.headE(),
c : Lambda <- cl.headC(),
n : Int <- cl.headN() in
{
cl.removeHead();
c.gen_closure_code(n, e, cl);
}
pool;
out_string("\n------------------cut here------------------\n");
}
};
main() : Int {
{
i().print_self();
out_string("\n");
k().print_self();
out_string("\n");
s().print_self();
out_string("\n");
beta_reduce(app(app(app(s(), k()), i()), i()));
beta_reduce(app(app(k(),i()),i()));
gen_code(app(i(), i()));
gen_code(app(app(app(s(), k()), i()), i()));
gen_code(app(app(app(app(app(app(app(app(i(), k()), s()), s()),
k()), s()), i()), k()), i()));
gen_code(app(app(i(), app(k(), s())), app(k(), app(s(), s()))));
0;
}
};
};