-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlambda-eval.c
331 lines (281 loc) · 7.58 KB
/
lambda-eval.c
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
#include <stdio.h>
#include <stdlib.h>
// lambda expressions
typedef enum {
APP, // e1 (e2)
LAM , // \x. e
PTR , // (x) (in body of a fn)
BASE // constant
} expr_tag;
// constants (base type data, represented as strings)
struct base {
char* data;
};
struct base b0 = { "zero" };
struct base b1 = { "one" };
struct base unit = {"()"};
// abstract binding trees
struct expr {
expr_tag tag;
union {
struct expr** p; // tag=PTR
struct app { // tag=APP
struct expr* fun;
struct expr* arg;
} a;
struct lam { // tag=LAM
struct expr** var;
struct expr* body;
} l;
struct base* b; // tag=BASE
} data;
};
// pretty printing
char* tag2s(expr_tag c) {
switch(c) {
case APP: return "APP";
case LAM: return "LAM";
case PTR: return "PTR";
case BASE: return "BASE";
}
}
void indent(int n) {
// printf("\n-- indenting %d tabs\n", n);
for(int i=0; i < n; i++) {
printf("\t");
}
}
// TODO currently prints, want to produce string
// need sprintf and dynamic allocation or sthg
void expr2s(struct expr* e, int tabs) {
printf("\n");
indent(tabs);
printf("@%p: %s { ", e, tag2s(e->tag));
switch(e->tag) {
case LAM: {
printf("\n");
indent(tabs+1);
printf("var: %p,\n", e->data.l.var);
indent(tabs+1);
printf("body: ");
expr2s(e->data.l.body, tabs+1);
indent(tabs);
printf("}\n ");
return;
}
case APP: {
printf("\n");
indent(tabs+1);
printf("fun: ");
expr2s(e->data.a.fun, tabs+1);
indent(tabs+1);
printf("arg: ");
expr2s(e->data.a.arg, tabs+1);
indent(tabs);
printf("}\n");
return;
}
case PTR: {
// indent(tabs);
printf("%p -> ", e->data.p);
// if we point to something,
// recursively print it
if(*(e->data.p)) {
expr2s(*(e->data.p), tabs+1);
indent(tabs);
printf("}\n");
} else {
printf("0 }\n");
}
return;
}
case BASE: {
// indent(tabs);
printf("%s }\n", e->data.b->data);
}
}
// return s;
}
/* Evaluation */
// not actually used, but isolates core logic
// for beta reduction
struct expr* step_app_lam(struct lam* l, struct expr* arg) {
*(l->var) = arg;
return (l->body);
}
// single (small) step of evaluation
struct expr* step(struct expr* e){
// lambdas, constants, and null ptrs (vars) are values.
// apps take steps. ptrs to things step to those things.
switch(e->tag) {
case LAM: return NULL;
case PTR: {
if(*(e->data.p)) {
return(*(e->data.p));
}
return NULL;
}
case BASE: return NULL;
case APP: {
struct expr* f2 = step(e->data.a.fun);
if(f2) { // function takes a step
e->data.a.fun = f2;
return e;
} else {
struct expr* arg2 = step(e->data.a.arg);
if(arg2) { // arg takes a step
e->data.a.arg = arg2;
return e;
} else { // hopefully, beta reduction
if(e->data.a.fun->tag == LAM) {
struct lam l = e->data.a.fun->data.l;
*(l.var) = e->data.a.arg;
return (l.body);
} else { // stuck state
return NULL;
} // function isn't lam
} // arg is value
} // function is value
} // end app case
} // end switch
}
struct expr* eval(struct expr* e) {
struct expr* e2 = e; // current value
struct expr* e3 = e; // next value
int iters = 0;
printf("\nIteration 0:");
while(1) {
iters++;
printf("evaluating expression ");
expr2s(e2,0);
e3 = step(e2); // get next value
if (e3) { // if not null, not done yet
printf("\nIteration %d: ", iters);
e2 = e3; // so set current to next and continue
} else { // if null, then current value is final
printf("done\n");
return e2;
}
}
}
// TODO: construct lambdas from more readable notation?
// \x.\f. f x
// lam { var:0, body { lam { var:1, app {fn: ref 1, arg {ref 0}}}}}
/* Util functions for constructing exprs */
struct expr makeApp(struct expr* f, struct expr* arg) {
struct app a;
a.fun = f;
a.arg = arg;
struct expr e;
e.tag = APP;
e.data.a = a;
return e;
}
struct expr makeLam(struct expr** var, struct expr* body) {
// printf("makin a lam\n");
struct lam l;
l.var = var;
l.body = body;
struct expr e;
e.tag = LAM;
e.data.l = l;
return e;
}
/* Tests */
// lambda x. x
struct expr makeIdFn() {
struct expr** var = malloc(sizeof(struct expr*));
*var = NULL; // bound var doesn't point to anything
struct expr* body = malloc(sizeof(struct expr));
body->tag = PTR;
body->data.p = var;
return makeLam(var, body);
}
// lambda x. ()
struct expr makeConstFn() {
struct expr** var = malloc(sizeof(struct expr*));
*var = NULL;
struct base* u = &unit;
struct expr* body = malloc(sizeof(struct expr));
body->tag = BASE;
body->data.b = u;
return makeLam(var, body);
}
// \x.\y. x
struct expr makeLamTrue() {
struct expr** x = malloc(sizeof(struct expr*));
*x = NULL;
struct expr** y = malloc(sizeof(struct expr*));
*y = NULL;
struct expr* inner = malloc(sizeof(struct expr));
inner->tag = PTR;
inner->data.p = x;
struct expr* outer = malloc(sizeof(struct expr));
*outer = makeLam(y, inner);
return makeLam(x, outer);
}
// \x.\y. y
struct expr makeLamFalse() {
struct expr** x = malloc(sizeof(struct expr*));
*x = NULL;
struct expr** y = malloc(sizeof(struct expr*));
*y = NULL;
struct expr* inner = malloc(sizeof(struct expr));
inner->tag = PTR;
inner->data.p = y;
struct expr* outer = malloc(sizeof(struct expr));
*outer = makeLam(y, inner);
return makeLam(x, outer);
}
// application of id to const
void test1() {
struct expr lam_id = makeIdFn();
struct expr lam_const = makeConstFn();
// printf("%s\n", expr2s(&lam_id));
printf("identity fn:\n");
expr2s(&lam_id,0);
printf("const fn:\n");
expr2s(&lam_const,0);
printf("application of id to const:\n");
struct expr app_id_const = makeApp(&lam_id, &lam_const);
expr2s(&app_id_const,0);
// evaluation
printf("------------------------------\n");
printf("Evaluating id applied to const\n");
struct expr* eval_result = eval(&app_id_const);
printf("result of eval:\n");
expr2s(eval_result,0);
printf("\n");
}
// application of true to id
void test2() {
printf("------------------------------\n");
printf("Evaluating true applied to id\n");
struct expr lam_id = makeIdFn();
struct expr lam_true = makeLamTrue();
struct expr app_true_id = makeApp(&lam_true, &lam_id);
printf("Constructed expression:");
expr2s(&app_true_id,0);
struct expr* eval_result = eval(&app_true_id);
printf("Result of eval:\n");
expr2s(eval_result,0);
printf("\n");
}
// application of false to id
void test3() {
printf("------------------------------\n");
printf("Evaluating false applied to id\n");
struct expr lam_id = makeIdFn();
struct expr lam_false = makeLamFalse();
struct expr app_false_id = makeApp(&lam_false, &lam_id);
printf("Constructed expression:");
expr2s(&app_false_id,0);
struct expr* eval_result = eval(&app_false_id);
printf("Result of eval:\n");
expr2s(eval_result,0);
printf("\n");
}
int main(int argc, char** argv) {
test2();
return 0;
}