-
Notifications
You must be signed in to change notification settings - Fork 3
/
normalize.c
362 lines (329 loc) · 9.89 KB
/
normalize.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
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
#include "term.h"
#include "normalize.h"
#include "printing.h"
#define FUEL 1000
static term* elim_over_intro(typing_context* Delta, term* t);
term* whnf_no_unfold(context *Sigma, typing_context* Delta, term* t);
term* whnf_no_unfold_and_free(context *Sigma, typing_context* Delta, term* t) {
term* ans = whnf_no_unfold(Sigma, Delta, t);
free_term(t);
return ans;
}
term* whnf_no_unfold(context *Sigma, typing_context* Delta, term* t) {
if (t == NULL) return NULL;
switch (t->tag) {
case VAR:
{
return term_dup(t);
}
case APP:
{
term* l = whnf_no_unfold(Sigma, Delta, t->left);
if (l->tag == LAM) {
term* subs = substitute(l->var, t->right, l->right);
free_term(l);
return whnf_no_unfold_and_free(Sigma, Delta, subs);
}
return make_app(l, term_dup(t->right));
}
case ELIM:
{
term* last = t->args[t->num_args - 1];
term* nlast = whnf_no_unfold(Sigma, Delta, last);
term* c = term_dup(t);
free_term(c->args[c->num_args - 1]);
c->args[c->num_args - 1] = nlast;
if (nlast->tag == INTRO) {
return whnf_no_unfold_and_free(Sigma, Delta, elim_over_intro(Delta, c));
} else {
return c;
}
}
case HOLE:
case DATATYPE:
case TYPE:
case LAM:
case INTRO:
case PI:
case IMPLICIT:
return term_dup(t);
}
}
term* whnf_and_free(context *Sigma, typing_context* Delta, term* t) {
term* ans = whnf(Sigma, Delta, t);
free_term(t);
return ans;
}
term* whnf(context *Sigma, typing_context* Delta, term* t) {
if (t == NULL) return NULL;
switch (t->tag) {
case VAR:
{
term* defn = context_lookup(t->var, Sigma);
if (defn == NULL) {
return term_dup(t);
}
return whnf(Sigma, Delta, defn);
}
case APP:
{
term* l = whnf(Sigma, Delta, t->left);
if (l->tag == LAM) {
term* subs = substitute(l->var, t->right, l->right);
free_term(l);
return whnf_and_free(Sigma, Delta, subs);
}
return make_app(l, term_dup(t->right));
}
case ELIM:
{
term* last = t->args[t->num_args - 1];
term* nlast = whnf(Sigma, Delta, last);
term* c = term_dup(t);
free_term(c->args[c->num_args - 1]);
c->args[c->num_args - 1] = nlast;
if (nlast->tag == INTRO) {
return whnf_and_free(Sigma, Delta, elim_over_intro(Delta, c));
} else {
return c;
}
}
case HOLE:
case DATATYPE:
case TYPE:
case LAM:
case INTRO:
case PI:
case IMPLICIT:
return term_dup(t);
}
}
term* normalize_no_unfold_and_free(typing_context* Delta, term* t) {
term* ans = normalize_no_unfold(Delta, t);
free_term(t);
return ans;
}
term* normalize_no_unfold(typing_context* Delta, term* t) {
if (t == NULL) return NULL;
switch (t->tag) {
case VAR:
{
return term_dup(t);
}
case APP:
{
term* l = normalize_no_unfold(Delta, t->left);
term* r = normalize_no_unfold(Delta, t->right);
if (l->tag == LAM) {
term* subs = substitute(l->var, r, l->right);
free_term(l);
free_term(r);
return normalize_no_unfold_and_free(Delta, subs);
}
return make_app(l, r);
}
case IMPLICIT:
case HOLE:
case TYPE:
case LAM:
case PI:
return term_dup(t);
default:
sentinel("unexpected tag %d");
}
error:
return NULL;
}
static term * normalize_fuel(context *Sigma, typing_context* Delta, term* t, int fuel);
static term* normalize_and_free_fuel(context *Sigma, typing_context* Delta, term* t, int fuel) {
term* ans = normalize_fuel(Sigma, Delta, t, fuel);
free_term(t);
return ans;
}
term *normalize_and_free(context *Sigma, typing_context* Delta, term* t) {
return normalize_and_free_fuel(Sigma, Delta, t, FUEL);
}
static term* normalize_fuel_app(context *Sigma, typing_context* Delta, term* t, int fuel) {
term *f = normalize_fuel(Sigma, Delta, t->left, fuel-1);
term *x = normalize_fuel(Sigma, Delta, t->right, fuel-1);
if (!f || !x) goto error;
if (f->tag == LAM) {
term* subs = substitute(f->var, x, f->right);
free_term(f);
free_term(x);
term* ans = normalize_fuel(Sigma, Delta, subs, fuel-1);
free_term(subs);
return ans;
}
return make_app(f, x);
error:
free_term(f);
free_term(x);
return NULL;
}
term* normalize_fuel_lambda(context *Sigma, typing_context* Delta, term* t, int fuel) {
term* b = NULL;
term* A = normalize_fuel(Sigma, Delta, t->left, fuel-1);
context* extend = context_add(variable_dup(t->var), NULL, Sigma);
b = normalize_fuel(extend, Delta, t->right, fuel-1);
context_pop(extend);
if (!b) goto error;
return make_lambda(variable_dup(t->var), A, b);
error:
free_term(A);
free_term(b);
return NULL;
}
term* normalize_fuel_pi(context *Sigma, typing_context* Delta, term* t, int fuel) {
term* B = NULL;
term* A = normalize_fuel(Sigma, Delta, t->left, fuel-1);
if (!A) goto error;
context* extend = context_add(variable_dup(t->var), NULL, Sigma);
B = normalize_fuel(extend, Delta, t->right, fuel-1);
context_pop(extend);
if (!B) goto error;
return make_pi(variable_dup(t->var), A, B);
error:
free_term(A);
free_term(B);
return NULL;
}
static term* elim_over_intro(typing_context* Delta, term* t) {
check(t && t->args && t->args[t->num_args - 1], "ill formed term");
check(t->tag == ELIM && t->args[t->num_args - 1]->tag == INTRO,
"elim_over_intro must be called on an eliminator applied to a constructor");
term* last = t->args[t->num_args - 1];
datatype* T = elim_to_datatype(t->var, Delta);
int index = datatype_intro_index(last->var, T);
check(index != -1, "bad intro index while evaluating %W", t, print_term);
term *app = term_dup(t->args[index + 1]);
int i;
for (i = 0; i < last->num_args; i++) {
app = make_app(app, term_dup(last->args[i]));
if (constructor_arg_is_inductive(T, last->var, i)) {
term *inductive = term_dup(t);
free_term(inductive->args[inductive->num_args - 1]);
inductive->args[inductive->num_args - 1] = term_dup(last->args[i]);
app = make_app(app, inductive);
}
}
free_term(t);
t = NULL;
return app;
error:
return NULL;
}
term* normalize_fuel_elim(context *Sigma, typing_context* Delta, term* t, int fuel) {
term* ans = NULL;
term* last = normalize_fuel(Sigma, Delta, t->args[t->num_args - 1], fuel-1);
if (!last) goto error;
if (last->tag == INTRO) {
term* c = term_dup(t);
free_term(c->args[c->num_args - 1]);
c->args[c->num_args - 1] = last;
return normalize_and_free_fuel(Sigma, Delta, elim_over_intro(Delta, c), fuel-1);
} else {
ans = make_elim(variable_dup(t->var), t->num_args, t->num_params);
int i;
for (i = 0; i < t->num_params; i++) {
ans->params[i] = normalize_fuel(Sigma, Delta, t->params[i], fuel-1);
if (!ans->params[i]) goto error;
}
for (i = 0; i < t->num_args - 1; i++) {
ans->args[i] = normalize_fuel(Sigma, Delta, t->args[i], fuel-1);
if (!ans->args[i]) goto error;
}
ans->args[t->num_args-1] = last;
return ans;
}
error:
free_term(last);
free_term(ans);
return NULL;
}
term* normalize_fuel_intro(context *Sigma, typing_context* Delta, term* t, int fuel) {
term* ans = make_intro(variable_dup(t->var),
normalize_fuel(Sigma, Delta, t->left, fuel-1),
t->num_args,
t->num_params,
t->num_indices);
int i;
for (i = 0; i < t->num_indices; i++) {
ans->indices[i] = normalize_fuel(Sigma, Delta, t->indices[i], fuel-1);
}
for (i = 0; i < t->num_params; i++) {
ans->params[i] = normalize_fuel(Sigma, Delta, t->params[i], fuel-1);
}
for (i = 0; i < t->num_args; i++) {
// FIXME: this leaks on error --jrw
ans->args[i] = normalize_fuel(Sigma, Delta, t->args[i], fuel-1);
if (!ans->args[i]) goto error;
}
return ans;
error:
free_term(ans);
return NULL;
}
term* normalize_fuel_datatype(context *Sigma, typing_context* Delta, term* t, int fuel) {
term* ans = make_datatype_term(variable_dup(t->var), t->num_params, t->num_indices);
int i;
for (i = 0; i < t->num_params; i++) {
ans->params[i] = normalize_fuel(Sigma, Delta, t->params[i], fuel-1);
}
for (i = 0; i < t->num_indices; i++) {
ans->indices[i] = normalize_fuel(Sigma, Delta, t->indices[i], fuel-1);
}
return ans;
}
term* normalize_fuel_var(context *Sigma, typing_context* Delta, term* t, int fuel) {
term* defn = context_lookup(t->var, Sigma);
if (defn == NULL) {
return term_dup(t);
}
return normalize_fuel(Sigma, Delta, defn, fuel-1);
}
term* normalize_fuel(context *Sigma, typing_context* Delta, term* t, int fuel) {
if (t == NULL) return NULL;
check(term_locally_well_formed(t), "%W must be locally well formed", t, print_term);
check(fuel >= 0, "Stack depth exceeded")
switch (t->tag) {
case VAR:
return normalize_fuel_var(Sigma, Delta, t, fuel);
case APP:
return normalize_fuel_app(Sigma, Delta, t, fuel);
case LAM:
return normalize_fuel_lambda(Sigma, Delta, t, fuel);
case PI:
return normalize_fuel_pi(Sigma, Delta, t, fuel);
case ELIM:
return normalize_fuel_elim(Sigma, Delta, t, fuel);
case INTRO:
return normalize_fuel_intro(Sigma, Delta, t, fuel);
case DATATYPE:
return normalize_fuel_datatype(Sigma, Delta, t, fuel);
default:
return term_dup(t);
}
error:
return NULL;
}
term* normalize(context *Sigma, typing_context* Delta, term* t) {
return normalize_fuel(Sigma, Delta, t, FUEL);
}
int definitionally_equal(context *Sigma, typing_context* Delta, term* a, term* b) {
term* na = normalize(Sigma, Delta, a);
term* nb = normalize(Sigma, Delta, b);
int ans = syntactically_identical(na, nb);
free_term(na);
free_term(nb);
return ans;
}
int is_pi_returning(context *Sigma, typing_context *Delta, term *t, term *val) {
if (definitionally_equal(Sigma, Delta, t, val)) {
return 1;
}
if (t->tag == PI) {
return is_pi_returning(Sigma, Delta, t->right, val);
}
return 0;
}