-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinterpreter.v
More file actions
473 lines (397 loc) · 9.56 KB
/
interpreter.v
File metadata and controls
473 lines (397 loc) · 9.56 KB
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
Load properties.
(*
Syntax
*)
(*
nil := λ x y. y
cons v vs = λ x y. x v vs
(x = λ a. a F0 F1)
B0 := λ x y. x
B1 := λ x y. y
*)
Definition nil := λ, λ, var 0.
(* Definition cons v vs := λ, λ, var 1 ⋅ v ⋅ vs. *)
Definition B0 := λ, λ, var 1.
Definition B1 := λ, λ, var 0.
(*
F = Y (λ e c s. s (λ a. a F0 F1))
F0 = λ t. t (λ b. c (b K S))
F1 = e (λ x. e (λ y. c (x y)))
F C (encode M :: N)
~> C M N
F0 C (b::xs)
~> C (b K S) xs
F1 C F (enc A ++ enc B ++ ys)
~> F (λ x. F (λ y. C (x y))) (enc A ++ enc B ++ ys)
~> ...
~> F (λ y. C (A y)) (enc B ++ ys)
~> (λ y. C (A y)) B ys
~> C (A B) ys
*)
Definition F0 C :=
λ, var 0 ⋅ (λ, (lift0 2 C) ⋅ (var 0 ⋅ lam_K ⋅ lam_S)).
Definition F1 F C :=
F ⋅ (λ, (lift0 1 F) ⋅ (λ, (lift0 2 C) ⋅ (var 1 ⋅ var 0))).
Definition F' :=
(* λ f c s. s *)
(λ, λ, λ, var 0 ⋅
(* λ a. a F0 F1 *)
(* F0 C, F1 F C *)
(λ, var 0 ⋅ (F0 (var 2)) ⋅ (F1 (var 3) (var 2)))).
Definition F := lam_Y ⋅ F'.
(*
Helper functions
*)
Fixpoint encode (s : SK) : list bool :=
match s with
| K => [false;false]
| S => [false;true]
| SK_app s1 s2 => true::encode s1 ++ encode s2
end.
Fixpoint encode_list (l : list SK) : list bool :=
match l with
| [] => []
| x::xs => encode x ++ encode_list xs
end.
Fixpoint to_lam (s : SK) : lambda :=
match s with
| K => lam_K
| S => lam_S
| SK_app s1 s2 => app (to_lam s1) (to_lam s2)
end.
(*
nested pair approach
[] => λ x y. y
[x,y, ..., z] => <x, <y, ..., <z, nil>>>
<a,b> => λ z. z a b
*)
Definition tuple a b := λ, var 0 ⋅ a ⋅ b.
Fixpoint embed_lam_list (xs : list lambda) : lambda :=
match xs with
| [] => λ, λ, var 0
| x::xs => tuple x (embed_lam_list xs)
end.
Definition embed_list (l : list bool) : lambda :=
embed_lam_list (map (fun (x:bool) => if x then B1 else B0) l).
(*
Specification:
*)
Definition F0_spec C b xs :=
(F0 C) ⋅ (tuple b xs) ≡ C ⋅ (b ⋅ lam_K ⋅ lam_S) ⋅ xs.
Definition F1_spec C A B xs :=
(F1 F C) ⋅
embed_list (encode A ++ encode B ++ encode_list xs) ≡
C ⋅ (to_lam A ⋅ to_lam B) ⋅ (embed_list (encode_list xs)).
Definition F_spec C (x:SK) (xs:list SK) :=
F ⋅ C ⋅ (embed_list (encode x ++ encode_list xs)) ≡ C ⋅ (to_lam x) ⋅ (embed_list(encode_list xs)).
(*
Aux lemmas
*)
Lemma closed_SK x: closed0 (to_lam x).
Proof.
induction x;cbn;try easy;lia.
Qed.
Lemma closed_embed_lam_list xs:
(* (forall x, In x xs -> closed0 x) -> *)
Forall (closed 0) xs ->
closed0 (embed_lam_list xs).
Proof.
induction 1;cbn;resolve_closed.
Qed.
Corollary closed_embed_list xs:
closed0 (embed_list xs).
Proof.
unfold embed_list.
eapply closed_embed_lam_list.
induction xs;constructor;auto.
destruct a;cbn;resolve_closed.
Qed.
Hint Resolve closed_SK.
Hint Resolve closed_embed_lam_list.
Hint Resolve closed_embed_list.
Lemma B0_select a b:
closed0 a -> closed0 b ->
B0 ⋅ a ⋅ b ≡ a.
Proof.
(* could be strengthened => a should only be forbidden from binding b *)
intros.
eexists;split;[|apply steps_refl].
eapply steps_trans.
apply steps_app_left,steps_beta. cbn;clear_lift_subst.
eapply steps_trans.
apply steps_beta. cbn;clear_lift_subst.
apply steps_refl.
Qed.
Lemma B1_select a b:
closed0 a -> closed0 b ->
B1 ⋅ a ⋅ b ≡ b.
Proof.
(* could be strengthened => a should only be forbidden from binding b *)
intros.
eexists;split;[|apply steps_refl].
eapply steps_trans.
apply steps_app_left,steps_beta. cbn;clear_lift_subst.
eapply steps_trans.
apply steps_beta. cbn;clear_lift_subst.
apply steps_refl.
Qed.
(*
Main lemma chain
*)
Lemma F0_spec_proof C a xs:
closed0 C -> closed0 a -> closed0 xs ->
F0_spec C a xs.
Proof.
unfold F0_spec.
intros HC_C HC_a HC_xs.
unfold F0.
(* remember lam_K as K.
remember lam_S as S. *)
eexists;split.
2: apply steps_refl.
- eapply steps_trans.
apply steps_beta.
cbn;clear_lift_subst.
eapply steps_trans.
apply steps_beta.
cbn;clear_lift_subst.
eapply steps_trans.
apply steps_app_left, steps_beta.
cbn;clear_lift_subst.
fold lam_K lam_S.
apply steps_refl.
Qed.
Lemma F1_spec_proof C A B xs:
(* (forall C x xs, closed0 C -> F_spec C x xs) -> *)
(forall C, closed0 C -> F_spec C A (B::xs) ) ->
(forall C, closed0 C -> F_spec C B (xs) ) ->
closed0 C ->
F1_spec C A B xs.
Proof.
intros F_spec_proof_A F_spec_proof_B HC_C.
unfold F1_spec, F1.
remember (to_lam A) as Al.
remember (to_lam B) as Bl.
eapply equiv_trans.
{
remember (λ, (lift0 1 F) ⋅ (λ, (lift0 2 C) ⋅ (var 1 ⋅ var 0))) as Cont.
pose proof (F_spec_proof_A Cont).
unfold F_spec in H; cbn -[lift0] in H.
subst.
apply H.
clear_lift_subst.
}
eapply equiv_trans.
{
eexists;split;[|apply steps_refl].
apply steps_app_left.
apply steps_beta.
}
cbn -[F]. clear_lift_subst.
eapply equiv_trans.
{
apply F_spec_proof_B.
clear_lift_subst.
}
eexists;split;[|apply steps_refl].
apply steps_app_left.
eapply steps_step.
1: apply step_beta.
cbn;clear_lift_subst.
now subst; apply steps_refl.
(* or leave open using Defined for transparency check of recursion *)
Qed.
Lemma F_fix C:
closed0 C ->
F ⋅ C ≡ F' ⋅ F ⋅ C.
Proof.
intros HC_C.
unfold F.
eapply equiv_trans.
{
apply equiv_app_left.
apply fixpoint.
resolve_closed.
}
eexists;split;[|apply steps_refl].
apply steps_refl.
Qed.
Lemma F_unfold C xs:
closed0 C ->
closed0 xs ->
F ⋅ C ⋅ xs ≡
xs ⋅ (λ, (var 0) ⋅ (F0 C) ⋅ (F1 F C)).
Proof.
intros HC_C HC_xs.
eapply equiv_trans.
{
apply equiv_app_left.
now apply F_fix.
}
eexists;split;[|apply steps_refl].
(* inline F and C *)
eapply steps_trans.
{
do 2 apply steps_app_left.
apply steps_beta.
}
cbn -[lam_Y F']; clear_lift_subst.
(* cbn -[lam_Y F0 F']; clear_lift_subst. *)
eapply steps_trans.
{
apply steps_app_left.
apply steps_beta.
}
cbn -[lam_Y F']; clear_lift_subst.
(* fold F0 C *)
pose (X := F0 C).
assert (X=F0 C) by reflexivity.
unfold F0 in H.
rewrite closed0_lift in H;[|assumption].
setoid_rewrite <- H;subst X;clear H.
pose (X := F1 F C).
assert (X=F1 F C) by reflexivity.
unfold F1, F in H.
do 2 rewrite closed0_lift in H;try resolve_closed.
setoid_rewrite <- H;subst X.
(* reduce app tuple *)
eapply steps_trans.
{
apply steps_beta.
}
cbn -[lam_Y F0 F1 F' tuple]; clear_lift_subst.
2-3: cbn;clear_lift_subst.
apply steps_refl.
Qed.
Lemma F_app_head C b xs:
closed0 C ->
closed0 b -> closed0 xs ->
F ⋅ C ⋅ (tuple b xs) ≡ b ⋅ (F0 C) ⋅ (F1 F C) ⋅ xs.
Proof.
intros HC_C HC_b HC_xs.
eapply equiv_trans.
apply F_unfold;cbn;try resolve_closed.
eexists;split;[|apply steps_refl].
(* apply tuple *)
eapply steps_trans.
{
apply steps_beta.
}
cbn -[lam_Y F0 F1 F']; clear_lift_subst.
2-3: cbn;clear_lift_subst.
(* apply head *)
eapply steps_trans.
{
apply steps_app_left.
apply steps_beta.
}
cbn -[lam_Y F0 F1 F']; clear_lift_subst.
2-3: cbn;clear_lift_subst.
apply steps_refl.
Qed.
Corollary F_app_0 C xs:
closed0 C ->
closed0 xs ->
F ⋅ C ⋅ (tuple B0 xs) ≡ F0 C ⋅ xs.
Proof.
intros.
eapply equiv_trans.
apply F_app_head;try assumption;clear_lift_subst.
apply equiv_app_left, B0_select.
all: cbn;clear_lift_subst.
Qed.
Corollary F_app_1 C xs:
closed0 C ->
closed0 xs ->
F ⋅ C ⋅ (tuple B1 xs) ≡ F1 F C ⋅ xs.
Proof.
intros.
eapply equiv_trans.
apply F_app_head;try assumption;clear_lift_subst.
apply equiv_app_left, B1_select.
all: cbn;clear_lift_subst.
Qed.
(*
F0 0, F0 1
=> F⋅K, F⋅S
*)
Corollary F0_app_0 C xs:
closed0 C -> closed0 xs ->
F0 C ⋅ (tuple B0 xs) ≡ C ⋅ lam_K ⋅ xs.
Proof.
intros.
eapply equiv_trans.
apply F0_spec_proof;resolve_closed.
apply equiv_app_left, equiv_app_right, B0_select;resolve_closed.
Qed.
Corollary F0_app_1 C xs:
closed0 C -> closed0 xs ->
F0 C ⋅ (tuple B1 xs) ≡ C ⋅ lam_S ⋅ xs.
Proof.
intros.
eapply equiv_trans.
apply F0_spec_proof;resolve_closed.
apply equiv_app_left, equiv_app_right, B1_select;resolve_closed.
Qed.
Corollary F_app_K C xs:
closed0 C ->
F ⋅ C ⋅ (embed_list (encode K ++ encode_list xs)) ≡ C ⋅ lam_K ⋅ (embed_list (encode_list xs)).
Proof.
intros.
cbn.
eapply equiv_trans.
apply F_app_0.
3: apply F0_app_0.
all: cbn;firstorder.
apply closed_inc, closed_embed_list.
Qed.
Corollary F_app_S C xs:
closed0 C ->
F ⋅ C ⋅ (embed_list (encode S ++ encode_list xs)) ≡ C ⋅ lam_S ⋅ (embed_list (encode_list xs)).
Proof.
intros.
cbn.
eapply equiv_trans.
apply F_app_0.
3: apply F0_app_1.
all: cbn;firstorder.
apply closed_inc, closed_embed_list.
Qed.
(*
Now app
*)
Lemma F_app_app C A B xs:
(forall C : lambda, closed0 C -> F_spec C A (B :: xs)) ->
(forall C : lambda, closed0 C -> F_spec C B xs) ->
closed0 C ->
(F ⋅ C) ⋅ embed_list (encode (SK_app A B) ++ encode_list xs)
≡ (C ⋅ to_lam (SK_app A B)) ⋅ embed_list (encode_list xs).
Proof.
intros.
cbn.
rewrite <- app_assoc.
eapply equiv_trans.
apply F_app_1;cbn;clear_lift_subst.
1: apply closed_embed_list.
now apply F1_spec_proof;cbn;clear_lift_subst.
Qed.
Lemma F_nil C:
closed0 C ->
F ⋅ C ⋅ nil ≡ λ, var 0.
Proof.
intros HC_C.
eapply equiv_trans.
apply F_unfold;try resolve_closed.
eexists;split;[|apply steps_refl].
apply steps_beta.
Qed.
Lemma F_spec_proof C x xs:
closed0 C ->
F_spec C x xs.
Proof.
intros HC_C.
induction x in C,HC_C,xs |-*.
+ now apply F_app_S.
+ now apply F_app_K.
+ apply F_app_app;eauto.
Qed.