-
Notifications
You must be signed in to change notification settings - Fork 1
/
LambdaLetDollar.v
1350 lines (1186 loc) · 43.1 KB
/
LambdaLetDollar.v
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Require Export Common.
Require Export Coq.Logic.FunctionalExtensionality.
(* ANCHOR λc$
terms e ::= v | p
values v ::= x | λ x. e | S₀
nonvalues p ::= e e' | let x = e in e' | e $ e'
elementary contexts J ::= [] e | v [] | [] $ e
evaluation contexts K ::= [] | let x = K in e
trails T ::= [] | v $ K[T]
(β.v) (λ x. e) v ~> e [x := v]
($.v) v $ v' ~> v v'
(β.$) v $ S₀ w ~> w v
($.let) v $ let x = S₀ w in e ~> (λ x. v $ e) $ S₀ w
(name) J[p] ~> let x = p in J[x]
(β.let) let x = v in e ~> e [x := v]
(assoc) let x = (let y = S₀ w in e2) in e3 ~> let y = S₀ w in let x = e2 in e3
e ~> e'
-------------------- (eval)
K[T[e]] --> K[T[e']]
The `λc$` calculus is a fine-grained version of the `λ$` calculus, meaning:
- instead of capturing the context in a one big step as in the rule `v $ K [S₀ f. e] ~> e [f := λ x. v $ K[x]]`
- we capture it by:
- First, building the context-as-term with many small steps - namely using rules: (name) and (assoc)
- Second, finally capturing the context with the (β.$) rule which is the original `λ$` rule without the evaluation context `K`
The formalization of the `λc$` is designed to perfectly match the calculus introduced in the paper,
which also specified the reduction theory but left out the evaluation strategy.
The goal of this project was to define the evaluation strategy using only the rules from the reduction theory.
The chosen set of rules is a specialized subset of original ones.
- ($.let) and (assoc) are specialized to work only with `S₀ w` instead of any term as in the original presentation
- excluded every η-reduction rule, as expected when working with an evaluation
This evaluation strategy is probably not the only possible solution.
There's the alternative hybrid strategy that abandons the (assoc) rule entirely at the expense that the reduction behaves differently under the delimiter `$`.
The hybrid strategy is yet to be explored, so for now let us focus on the pros and cons of the strategy described here.
SECTION Pros
- The contraction rules together with the formation of the evaluation context are enough to describe the strategy, which is a standard homogeneous format - easy to understand
(whereas the alternative requires special context forms together with different set sof rules that act on these contexts)
- Trivial to support the `control0` operator
SECTION Cons
- The need to specialize original rules with `S₀ w`
- The specialized rule ($.let) always produces a redex
- (name) rule *existing*
*)
(* ANCHOR Terms
*)
Inductive tm' {A} :=
| tm_var' : A → tm'
| tm_s_0' : tm' (* S₀ *)
| tm_abs' : @tm' ^A → tm' (* λ x. e *)
| tm_app' : tm' → tm' → tm' (* e e *)
| tm_dol' : tm' → tm' → tm' (* e $ e *)
| tm_let' : tm' → @tm' ^A → tm' (* let x = e1 in e2 *)
.
Arguments tm' A : clear implicits.
Global Hint Constructors tm' : core.
Inductive val' {A} :=
| val_abs' : @tm' ^A → val'
| val_s_0' : val'
.
Arguments val' A : clear implicits.
Global Hint Constructors val' : core.
Definition val_to_tm' {A} (v : val' A) :=
match v with
| val_abs' e => tm_abs' e
| val_s_0' => tm_s_0'
end.
Coercion val_to_tm' : val' >-> tm'.
Lemma inj_val' : ∀ {A} (v1 v2 : val' A),
val_to_tm' v1 = val_to_tm' v2 →
v1 = v2.
Proof.
intros. destruct v1, v2; cbn in H; auto;
try solve [inversion H; auto].
Qed.
Inductive non' {A} :=
| non_app' : tm' A → tm' A → non' (* e e *)
| non_dol' : tm' A → tm' A → non' (* e $ e *)
| non_let' : tm' A → tm' ^A → non' (* let x = e1 in e2 *)
.
Arguments non' A : clear implicits.
Global Hint Constructors non' : core.
Definition non_to_tm' {A} (p : non' A) :=
match p with
| non_app' e1 e2 => tm_app' e1 e2
| non_dol' e1 e2 => tm_dol' e1 e2
| non_let' e1 e2 => tm_let' e1 e2
end.
Coercion non_to_tm' : non' >-> tm'.
Lemma inj_non' : ∀ {A} (p p' : non' A),
non_to_tm' p = non_to_tm' p' → p = p'.
Proof.
intros; destruct p, p'; cbn in *; inv H; auto.
Qed.
Declare Custom Entry λ_let_dollar_scope.
Notation "<| e |>" := e (at level 1, e custom λ_let_dollar_scope at level 99).
Notation "( x )" := x (in custom λ_let_dollar_scope, x at level 99).
Notation "{ x }" := x (in custom λ_let_dollar_scope at level 0, x constr).
Notation "x" := x (in custom λ_let_dollar_scope at level 0, x constr at level 0).
Notation "'var' v" := (tm_var' v) (in custom λ_let_dollar_scope at level 1, left associativity).
Notation "0" := <| var None |> (in custom λ_let_dollar_scope at level 0).
Notation "1" := <| var {Some None} |> (in custom λ_let_dollar_scope at level 0).
Notation "2" := <| var {Some (Some None)} |> (in custom λ_let_dollar_scope at level 0).
Notation "'λ' e" := (tm_abs' e)
(in custom λ_let_dollar_scope at level 90,
e custom λ_let_dollar_scope at level 99,
right associativity).
Notation "'λv'' e" := (val_abs' e)
(in custom λ_let_dollar_scope at level 90,
e custom λ_let_dollar_scope at level 99,
right associativity).
Notation "'S₀,' e" := (tm_app' tm_s_0' (tm_abs' e))
(in custom λ_let_dollar_scope at level 90,
e custom λ_let_dollar_scope at level 99,
right associativity).
Notation "'S₀'" := (tm_s_0').
Notation "e1 e2" := (tm_app' e1 e2) (in custom λ_let_dollar_scope at level 10, left associativity).
Notation "e1 '$' e2" := (tm_dol' e1 e2) (in custom λ_let_dollar_scope at level 80, right associativity).
Notation "'let' e1 'in' e2" := (tm_let' e1 e2) (in custom λ_let_dollar_scope at level 70, right associativity).
Lemma lambda_to_val' : ∀ {A} (e : tm' ^A),
<| λ e |> = <| λv' e |>.
Proof.
reflexivity.
Qed.
Fixpoint map' {A B : Type} (f : A → B) (e : tm' A) : tm' B :=
match e with
| <| var a |> => <| var {f a} |>
| <| S₀ |> => <| S₀ |>
| <| λ e' |> => <| λ {map' (option_map f) e'} |>
| <| e1 e2 |> => <| {map' f e1} {map' f e2} |>
| <| e1 $ e2 |> => <| {map' f e1} $ {map' f e2} |>
| <| let e1 in e2 |> => tm_let' (map' f e1) (map' (option_map f) e2)
end.
Definition mapV' {A B} (f : A → B) (v : val' A) : val' B :=
match v with
| val_abs' e => val_abs' (map' (option_map f) e)
| val_s_0' => val_s_0'
end.
Definition mapP' {A B : Type} (f : A → B) (p : non' A) : non' B :=
match p with
| non_app' e1 e2 => non_app' (map' f e1) (map' f e2)
| non_dol' e1 e2 => non_dol' (map' f e1) (map' f e2)
| non_let' e1 e2 => non_let' (map' f e1) (map' (option_map f) e2)
end.
Lemma map_val_is_val' : ∀ {A B} (v : val' A) (f : A → B),
∃ w, map' f (val_to_tm' v) = val_to_tm' w.
Proof.
intros. destruct v; cbn.
- eexists <| λv' _ |>. reflexivity.
- exists val_s_0'; reflexivity.
Qed.
Lemma map_non_is_non' : ∀ {A B} (p1 : non' A) (f : A → B),
∃ p2, map' f (non_to_tm' p1) = non_to_tm' p2.
Proof.
intros. destruct p1; cbn.
- eexists (non_app' _ _). reflexivity.
- eexists (non_dol' _ _). reflexivity.
- eexists (non_let' _ _). reflexivity.
Qed.
Lemma mapV_is_map' : ∀ {A B} v (f : A → B),
val_to_tm' (mapV' f v) = map' f (val_to_tm' v).
Proof.
intros. destruct v; auto.
Qed.
Lemma mapP_is_map' : ∀ {A B} p (f : A → B),
non_to_tm' (mapP' f p) = map' f (non_to_tm' p).
Proof.
intros. destruct p; auto.
Qed.
Lemma map_id_law' : ∀ A (e : tm' A),
map' id e = e.
Proof.
induction e; cbn; auto;
repeat rewrite option_map_id_law; f_equal; auto.
Qed.
Lemma map_map_law' : ∀ A e B C (f : A → B) (g : B → C),
map' g (map' f e) = map' (g ∘ f) e.
Proof.
intros. generalize dependent B. generalize dependent C.
induction e; intros; cbn; auto;
try solve [f_equal; rewrite IHe; rewrite option_map_comp_law; auto];
try solve [rewrite IHe1; rewrite IHe2; reflexivity].
rewrite IHe1; f_equal. rewrite IHe2.
rewrite option_map_comp_law; auto.
Qed.
Lemma mapV_mapV_law' : ∀ A v B C (f : A → B) (g : B → C),
mapV' g (mapV' f v) = mapV' (g ∘ f) v.
Proof.
destruct v; intros; cbn; auto.
rewrite map_map_law'.
rewrite option_map_comp_law. reflexivity.
Qed.
Fixpoint bind' {A B : Type} (f : A → tm' B) (e : tm' A) : tm' B :=
match e with
| <| var a |> => f a
| <| S₀ |> => <| S₀ |>
| <| e1 e2 |> => <| {bind' f e1} {bind' f e2} |>
| <| e1 $ e2 |> => <| {bind' f e1} $ {bind' f e2} |>
| <| λ e' |> => tm_abs' (bind' (fun a' =>
match a' with
| None => tm_var' None
| Some a => map' Some (f a)
end) e')
| <| let e1 in e2 |> => tm_let' (bind' f e1) (bind' (fun a' =>
match a' with
| None => tm_var' None
| Some a => map' Some (f a)
end) e2)
end.
Definition bindV' {A B} (f : A → tm' B) (v : val' A) : val' B :=
match v with
| val_s_0' => val_s_0'
| val_abs' e => val_abs' (bind' (fun a' =>
match a' with
| None => tm_var' None
| Some a => map' Some (f a)
end) e)
end.
Definition bindP' {A B : Type} (f : A → tm' B) (p : non' A) : non' B :=
match p with
| non_app' e1 e2 => non_app' (bind' f e1) (bind' f e2)
| non_dol' e1 e2 => non_dol' (bind' f e1) (bind' f e2)
| non_let' e1 e2 => non_let' (bind' f e1) (bind' (fun a' =>
match a' with
| None => tm_var' None
| Some a => map' Some (f a)
end) e2)
end.
Lemma bind_val_is_val' : ∀ {A B} (v : val' A) (f : A → tm' B),
∃ w, bind' f (val_to_tm' v) = val_to_tm' w.
Proof.
intros. destruct v; cbn.
- eexists <| λv' _ |>. reflexivity.
- eexists val_s_0'. reflexivity.
Qed.
Lemma bindV_is_bind' : ∀ {A B} v (f : A → tm' B),
val_to_tm' (bindV' f v) = bind' f (val_to_tm' v).
Proof.
intros. destruct v; auto.
Qed.
Lemma bindP_is_bind' : ∀ {A B} p (f : A → tm' B),
non_to_tm' (bindP' f p) = bind' f (non_to_tm' p).
Proof.
intros. destruct p; auto.
Qed.
Lemma bind_map_law' : ∀ {A B C} (f : B → tm' C) (g : A → B) e,
bind' f (map' g e) = bind' (λ a, f (g a)) e.
Proof.
intros. generalize dependent B. generalize dependent C.
induction e; intros; cbn; auto;
try solve [f_equal; rewrite IHe; f_equal; apply functional_extensionality; intros [a|]; cbn; auto];
try solve [f_equal; apply IHe1 + apply IHe2].
rewrite IHe1; f_equal. rewrite IHe2.
f_equal; apply functional_extensionality; intros [a|]; cbn; auto.
Qed.
Lemma bind_pure' : ∀ {A} (e : tm' A),
bind' (λ a, <| var a |>) e = e.
Proof.
induction e; cbn; auto;
try solve [f_equal; rewrite <- IHe at 2; f_equal; apply functional_extensionality; intros [a|]; cbn; auto];
try solve [f_equal; apply IHe1 + apply IHe2].
rewrite IHe1; f_equal. rewrite <- IHe2 at 2.
f_equal; apply functional_extensionality; intros [a|]; cbn; auto.
Qed.
Lemma map_bind_law' : ∀ {A B C} (f : A → tm' B) (g : B → C) e,
bind' (map' g ∘ f) e = map' g (bind' f e).
Proof.
intros; generalize dependent B; generalize dependent C; induction e; intros; cbn; auto;
try solve [
rewrite <- IHe; repeat f_equal; apply functional_extensionality; intros [a|]; auto; cbn;
change ((map' g ∘ f) a) with (map' g (f a));
repeat rewrite map_map_law'; f_equal];
try solve [f_equal; apply IHe1 + apply IHe2].
rewrite IHe1; f_equal. rewrite <- IHe2.
f_equal; apply functional_extensionality; intros [a|]; cbn; auto.
change ((map' g ∘ f) a) with (map' g (f a)); repeat rewrite map_map_law'; f_equal.
Qed.
Lemma bind_bind_law' : ∀ {A B C} (g : B → tm' C) (f : A → tm' B) (e : tm' A),
bind' g (bind' f e) = bind' (λ a, bind' g (f a)) e.
Proof.
intros. generalize dependent B. generalize dependent C.
induction e; intros; cbn; auto;
try solve [rewrite IHe1; rewrite IHe2; reflexivity].
- rewrite IHe; repeat f_equal.
apply functional_extensionality; intros [a|]; auto.
rewrite bind_map_law'. rewrite <- map_bind_law'. reflexivity.
- rewrite IHe1. rewrite IHe2. repeat f_equal.
apply functional_extensionality; intros [a|]; auto.
rewrite bind_map_law'. rewrite <- map_bind_law'. reflexivity.
Qed.
Lemma map_as_bind' : ∀ {A B} e (f : A → B),
map' f e = bind' (tm_var' ∘ f) e.
Proof.
intros; generalize dependent B; induction e; intros; cbn; auto;
try solve [rewrite IHe; repeat f_equal; apply functional_extensionality; intros [a|]; auto];
try solve [rewrite IHe1; rewrite IHe2; reflexivity].
rewrite IHe1; f_equal. rewrite IHe2; f_equal.
apply functional_extensionality; intros [a|]; auto.
Qed.
Definition var_subst' {V} e' (v:^V) :=
match v with
| None => e'
| Some v => <| var v |>
end.
Definition tm_subst0' {V} (e:tm' ^V) (v:val' V) :=
bind' (var_subst' (val_to_tm' v)) e.
Notation "e [ 0 := v ]" := (tm_subst0' e v)
(in custom λ_let_dollar_scope at level 0,
e custom λ_let_dollar_scope,
v custom λ_let_dollar_scope at level 99).
Class Plug' (E : Type → Type) := plug' : ∀ {A}, E A → tm' A → tm' A.
Notation "E [ e ]" := (plug' E e)
(in custom λ_let_dollar_scope at level 70,
e custom λ_let_dollar_scope at level 99).
Definition tm_dec' (e : tm' ∅) : (val' ∅ + non' ∅) :=
match e with
| <| var a |> => from_void a
| <| S₀ |> => inl val_s_0'
| <| λ e' |> => inl (val_abs' e')
| <| e1 e2 |> => inr (non_app' e1 e2)
| <| e1 $ e2 |> => inr (non_dol' e1 e2)
| <| let e1 in e2 |> => inr (non_let' e1 e2)
end.
Lemma tm_dec_val' : ∀ e v, tm_dec' e = inl v → e = val_to_tm' v.
Proof.
intros; inv e; cbn in *; try destruct a; inv H; auto.
Qed.
Lemma tm_dec_non' : ∀ e p, tm_dec' e = inr p → e = non_to_tm' p.
Proof.
intros; inv e; cbn in *; try destruct a; inv H; auto.
Qed.
Lemma tm_dec_of_val' : ∀ v, tm_dec' (val_to_tm' v) = inl v.
Proof.
intros; destruct v; auto.
Qed.
Lemma tm_dec_of_non' : ∀ p, tm_dec' (non_to_tm' p) = inr p.
Proof.
intros; destruct p; auto.
Qed.
Ltac reason := repeat(
rewrite tm_dec_of_val' in * +
rewrite tm_dec_of_non' in * +
match goal with
| H : ∅ |- _ => destruct H
| H : (_, _) = (_, _) |- _ => inj H
| H : val_to_tm' _ = val_to_tm' _ |- _ => apply inj_val' in H; rewrite H in *
| H : non_to_tm' _ = non_to_tm' _ |- _ => apply inj_non' in H; rewrite H in *
| H : inl ?v = tm_dec' ?e |- _ => rewrite (tm_dec_val' e v (eq_sym H)) in *; auto
| H : inr ?p = tm_dec' ?e |- _ => rewrite (tm_dec_non' e p (eq_sym H)) in *; auto
| H : context C [let (_, _) := ?e in _] |- _ =>
let p := fresh "p" in remember e as p; inv p
| |- context C [let (_, _) := ?e in _] =>
let p := fresh "p" in remember e as p; inv p
| H : context C [match tm_dec' ?e with inl _ => _ | inr _ => _ end] |- _ =>
let td := fresh "td" in
let Hd := fresh "Hd" in
remember (tm_dec' e) as td eqn: Hd; inv td; try rewrite Hd in *
| H : context C [match ?v with val_abs' _ => _ | val_s_0' => _ end] |- _ =>
destruct v; inversion H; clear H; subst
end).
(* ANCHOR Contexts
*)
Section Contexts.
Context {A : Type}.
Inductive J' :=
| J_fun' : tm' A → J' (* [] M *)
| J_arg' : val' A → J' (* V [] *)
| J_dol' : tm' A → J' (* []$M *)
.
Inductive K' :=
| K_nil' : K'
| K_let' : K' → tm' ^A → K' (* let x = K in e *)
.
Inductive T' :=
| T_nil' : T'
| T_cons' : val' A → K' → T' → T' (* (v$K) · T *)
.
Inductive redex' :=
| redex_beta' : tm' ^A → val' A → redex' (* (λ e) v *)
| redex_dollar' : val' A → val' A → redex' (* v $ v' *)
| redex_shift' : val' A → val' A → redex' (* v $ S₀ v' *)
| redex_dol_let' : val' A → val' A → tm' ^A → redex' (* v $ let x = S₀ v1 in e2 *)
| redex_let' : J' → non' A → redex' (* J[p] *)
| redex_let_beta' : val' A → tm' ^A → redex' (* let x = v in e *)
| redex_let_assoc' : val' A → tm' ^A → tm' ^A → redex' (* let x = (let y = S₀ v1 in e2) in e3 *)
.
Inductive dec' :=
| dec_value' : val' A → dec'
| dec_stuck_s_0' : val' A → dec' (* S₀ v *)
| dec_stuck_let' : val' A → tm' ^A → dec' (* let x = S₀ v in e *)
| dec_redex' : K' → T' → redex' → dec' (* K[T[Redex]] *)
.
End Contexts.
Arguments J' A : clear implicits.
Arguments K' A : clear implicits.
Arguments T' A : clear implicits.
Arguments redex' A : clear implicits.
Arguments dec' A : clear implicits.
Definition plugJ' {A} (j : J' A) e' :=
match j with
| J_fun' e => <| e' e |>
| J_arg' v => <| v e' |>
| J_dol' e => <| e' $ e |>
end.
Instance PlugJ' : Plug' J' := @plugJ'.
Fixpoint plugK' {A} (k : K' A) e :=
match k with
| K_nil' => e
| K_let' k1 e2 => <| let {plugK' k1 e} in e2 |>
end.
Instance PlugK' : Plug' K' := @plugK'.
Fixpoint plugT' {A} (trail : T' A) e :=
match trail with
| T_nil' => e
| T_cons' v k t => <| v $ k [{plugT' t e}] |>
end.
Instance PlugT' : Plug' T' := @plugT'.
Definition redex_to_term' {A} (r : redex' A) :=
match r with
| redex_beta' e v => <| (λ e) v |>
| redex_dollar' v1 v2 => <| v1 $ v2 |>
| redex_shift' v v' => <| v $ S₀ v' |>
| redex_dol_let' v v1 e2 => <| v $ let S₀ v1 in e2 |>
| redex_let' j p => <| j[p] |>
| redex_let_beta' v e => <| let v in e |>
| redex_let_assoc' v1 e2 e3 => <| let (let S₀ v1 in e2) in e3 |>
end.
Coercion redex_to_term' : redex' >-> tm'.
(* ANCHOR Decompose
*)
Fixpoint decompose' (e : tm' ∅) : dec' ∅ :=
match e with
| <| var a |> => from_void a
| <| S₀ |> => dec_value' val_s_0'
| <| λ e' |> => dec_value' (val_abs' e')
| <| e1 e2 |> =>
match tm_dec' e1 with
| inr p1 => dec_redex' K_nil' T_nil' (redex_let' (J_fun' e2) p1) (* p1 e2 *)
| inl v1 =>
match tm_dec' e2 with
| inr p2 => dec_redex' K_nil' T_nil' (redex_let' (J_arg' v1) p2) (* v1 p2 *)
| inl v2 =>
match v1 with
| val_abs' t => dec_redex' K_nil' T_nil' (redex_beta' t v2) (* (λ t) v2 *)
| val_s_0' => dec_stuck_s_0' v2 (* S₀ v2 *)
end
end
end
| <| e1 $ e2 |> =>
match tm_dec' e1 with
| inr p1 => dec_redex' K_nil' T_nil' (redex_let' (J_dol' e2) p1) (* p1 $ e2 *)
| inl v1 => (* v1 $ e *)
match decompose' e2 with
| dec_stuck_s_0' v2' => dec_redex' K_nil' (T_nil') (redex_shift' v1 v2') (* v1 $ S₀ v2' *)
| dec_stuck_let' v2_1 e2_2 => dec_redex' K_nil' (T_nil') (redex_dol_let' v1 v2_1 e2_2) (* v1 $ let x = S₀ v2_1 in e2_2 *)
| dec_redex' k t r => dec_redex' K_nil' (T_cons' v1 k t) (r) (* v1 $ K[T[r]] *)
| dec_value' v2 => dec_redex' K_nil' (T_nil') (redex_dollar' v1 v2) (* v1 $ v2 *)
end
end
| <| let e1 in e2 |> =>
match decompose' e1 with
| dec_stuck_s_0' v1' => dec_stuck_let' v1' e2 (* let x = S₀ v1' in e2 *)
| dec_stuck_let' v1_1 e1_2 => dec_redex' (K_nil') T_nil' (redex_let_assoc' v1_1 e1_2 e2) (* let x = (let y = S₀ v1_1 in e1_2) in e2 *)
| dec_redex' k t r => dec_redex' (K_let' k e2) t (r) (* let x = K[T[r]] in e2 *)
| dec_value' v1 => dec_redex' (K_nil') T_nil' (redex_let_beta' v1 e2) (* let x = v1 in e2 *)
end
end.
Ltac inv_decompose_match' H :=
match goal with
| H : (match decompose' ?e with | dec_stuck_s_0' _ | dec_stuck_let' _ _ | dec_redex' _ _ _ | dec_value' _ => _ end = _) |- _ =>
let d := fresh "d" in remember (decompose' e) as d; inv d; inv_decompose_match' H
| H : (let (_,_) := ?e in _) = _ |- _ =>
let d := fresh "d" in remember e as d; inv d; inv_decompose_match' H
| _ => try solve [inversion H; auto]; try inj H
end.
Lemma inj_plug_j' : ∀ {A} (j1 j2 : J' A) (p1 p2 : non' A),
<| j1[p1] |> = <| j2[p2] |> →
j1 = j2 /\ p1 = p2.
Proof.
intros;
destruct j1, j2; cbn in *; inversion H; clear H; reason; subst; auto;
solve [destruct p1 + destruct p2; destruct v; inversion H1; auto].
Qed.
Lemma inj_redex' : ∀ {A} (r r' : redex' A),
redex_to_term' r = redex_to_term' r' → r = r'.
Proof.
intros; destruct r, r'; cbn in *; inversion H; clear H; reason; subst; auto;
try apply inj_val' in H1; try apply inj_val' in H2; subst; auto;
try solve [destruct j; cbn in *; inversion H1; auto];
try solve [destruct j; destruct v; destruct n; cbn in *; inversion H1; auto];
try solve [destruct v0 + destruct v2; inversion H2].
apply inj_plug_j' in H1 as [H1 H2]; subst; auto.
destruct v; inversion H1.
destruct v0; inversion H1.
Qed.
(* ANCHOR Unique Decomposition
*)
(* plug' ∘ decompose' = id *)
Lemma decompose_value_inversion' : ∀ e v,
decompose' e = dec_value' v → e = val_to_tm' v.
Proof.
intros. inv e; cbn in *.
- destruct a.
- destruct v; inversion H; clear H; subst. auto.
- inj H; auto.
- reason; inversion H.
- reason; inv_decompose_match' H.
- inv_decompose_match' H.
Qed.
Lemma decompose_stuck_s_0_inversion' : ∀ e v,
decompose' e = dec_stuck_s_0' v → e = <| S₀ v |>.
Proof.
intros. inv e; cbn in *; reason; auto;
try solve [inversion a | inv H];
inv_decompose_match' H.
Qed.
Lemma decompose_stuck_let_inversion' : ∀ e v1 e2,
decompose' e = dec_stuck_let' v1 e2 → e = <| let S₀ v1 in e2 |>.
Proof.
intros. inv e; cbn in *; reason; auto;
try solve [inversion a | inv H];
inv_decompose_match' H.
rewrite (decompose_stuck_s_0_inversion' e1 v1); auto.
Qed.
Ltac inv_dec' :=
match goal with
| H : dec_value' ?v = decompose' ?e |- _ => rewrite (decompose_value_inversion' e v); cbn; auto
| H : dec_stuck_s_0' ?e' = decompose' ?e |- _ => rewrite (decompose_stuck_s_0_inversion' e e'); cbn; auto
| H : dec_stuck_let' ?e1 ?e2 = decompose' ?e |- _ => rewrite (decompose_stuck_let_inversion' e e1 e2); cbn; auto
end.
Lemma decompose_redex_inversion' : ∀ e t k r,
decompose' e = dec_redex' k t r → e = <| k[t[r]] |>.
Proof.
dependent induction e; intros; cbn in *; reason; auto;
try solve [destruct a | inv H];
inv_decompose_match' H;
repeat inv_dec'; cbn; f_equal;
try solve [apply IHk + apply IHe1 + apply IHe2; auto].
Qed.
(* decompose' ∘ plug' = id *)
Lemma decompose_plug_value' : ∀ v,
decompose' (val_to_tm' v) = dec_value' v.
Proof.
intros; destruct v; auto.
Qed.
Lemma decompose_plug_stuck_s_0' : ∀ (v : val' ∅),
decompose' <| S₀ v |> = dec_stuck_s_0' v.
Proof.
intros; destruct v; cbn; auto.
Qed.
Lemma decompose_plug_stuck_let' : ∀ (v : val' ∅) e,
decompose' <| let S₀ v in e |> = dec_stuck_let' v e.
Proof.
intros; destruct v; cbn; auto.
Qed.
Lemma decompose_plug_redex' : ∀ k t (r : redex' ∅),
decompose' <| k[t[r]] |> = dec_redex' k t r.
Proof with cbn; auto.
intros k t; generalize dependent k; induction t; intros...
- induction k...
+ inv r; cbn; try inv v; cbn; try solve [inv v0; auto]; auto.
inv j; cbn; reason; reflexivity.
+ rewrite IHk; reflexivity.
- induction k0...
+ inv v; rewrite IHt...
+ rewrite IHk0. reflexivity.
Qed.
Instance LiftTm' : Lift tm' := λ {A}, map' Some.
Definition liftV' {A : Type} (v : val' A) : val' ^A := mapV' Some v.
Lemma lift_val_to_tm' : ∀ {A} (v : val' A),
↑ (val_to_tm' v) = val_to_tm' (liftV' v).
Proof.
intros. destruct v; cbn; reflexivity.
Qed.
Definition mapJ' {A B} (f : A → B) (j : J' A) : J' B :=
match j with
| J_fun' e => J_fun' (map' f e)
| J_arg' v => J_arg' (mapV' f v)
| J_dol' e => J_dol' (map' f e)
end.
Lemma mapJ_mapJ_law' : ∀ A j B C (f : A → B) (g : B → C),
mapJ' g (mapJ' f j) = mapJ' (g ∘ f) j.
Proof.
destruct j; intros; cbn;
try rewrite map_map_law';
try rewrite mapV_mapV_law';
auto.
Qed.
Fixpoint mapK' {A B} (f : A → B) (k : K' A) : K' B :=
match k with
| K_nil' => K_nil'
| K_let' k1 e2 => K_let' (mapK' f k1) (map' (option_map f) e2)
end.
Lemma mapK_mapK_law' : ∀ A k B C (f : A → B) (g : B → C),
mapK' g (mapK' f k) = mapK' (g ∘ f) k.
Proof.
induction k; intros; cbn; auto.
rewrite IHk.
rewrite map_map_law'.
rewrite option_map_comp_law.
reflexivity.
Qed.
Instance LiftJ' : Lift J' := λ {A}, mapJ' Some.
Instance LiftK' : Lift K' := λ {A}, mapK' Some.
Definition bindJ' {A B} (f : A → tm' B) (j : J' A) : J' B :=
match j with
| J_fun' e => J_fun' (bind' f e)
| J_arg' v => J_arg' (bindV' f v)
| J_dol' e => J_dol' (bind' f e)
end.
Fixpoint bindK' {A B} (f : A → tm' B) (k : K' A) : K' B :=
match k with
| K_nil' => K_nil'
| K_let' k1 e2 => K_let' (bindK' f k1) (bind' (fun a' =>
match a' with
| None => tm_var' None
| Some a => map' Some (f a)
end) e2)
end.
Lemma bindV_mapV_law' : ∀ {A B C} (f : B → tm' C) (g : A → B) v,
bindV' f (mapV' g v) = bindV' (λ a, f (g a)) v.
Proof.
intros.
apply inj_val'. repeat rewrite bindV_is_bind'. rewrite mapV_is_map'.
apply bind_map_law'.
Qed.
Lemma bindJ_mapJ_law' : ∀ {A B C} (f : B → tm' C) (g : A → B) j,
bindJ' f (mapJ' g j) = bindJ' (λ a, f (g a)) j.
Proof.
intros; destruct j; cbn; auto;
rewrite bind_map_law' + rewrite bindV_mapV_law'; auto.
Qed.
Lemma bindK_mapK_law' : ∀ {A B C} (f : B → tm' C) (g : A → B) k,
bindK' f (mapK' g k) = bindK' (λ a, f (g a)) k.
Proof.
intros. generalize dependent B. generalize dependent C.
induction k; intros; cbn; auto.
rewrite IHk; f_equal.
rewrite bind_map_law'; f_equal.
apply functional_extensionality; intros [a|]; cbn; auto.
Qed.
Lemma mapV_bindV_law' : ∀ {A B C} (f : A → tm' B) (g : B → C) v,
bindV' (map' g ∘ f) v = mapV' g (bindV' f v).
Proof.
intros. apply inj_val'. rewrite mapV_is_map'. repeat rewrite bindV_is_bind'. apply map_bind_law'.
Qed.
Lemma mapJ_bindJ_law' : ∀ {A B C} (f : A → tm' B) (g : B → C) j,
bindJ' (map' g ∘ f) j = mapJ' g (bindJ' f j).
Proof.
intros; destruct j; intros; cbn; auto;
rewrite map_bind_law' + rewrite mapV_bindV_law'; auto.
Qed.
Lemma mapK_bindK_law' : ∀ {A B C} (f : A → tm' B) (g : B → C) k,
bindK' (map' g ∘ f) k = mapK' g (bindK' f k).
Proof.
intros; generalize dependent B; generalize dependent C; induction k; intros; cbn; auto.
rewrite IHk; f_equal.
rewrite <- map_bind_law'; f_equal.
apply functional_extensionality; intros [a|]; cbn; auto.
change ((map' g ∘ f) a) with (map' g (f a)); repeat rewrite map_map_law'; f_equal.
Qed.
(* ANCHOR Evaluation
*)
Definition contract' (r : redex' ∅) : tm' ∅ :=
match r with
(* (λ x. e) v ~> e [x := v] *)
| redex_beta' e v => <| e [0 := v] |>
(* v1 $ v2 ~> v1 v2 *)
| redex_dollar' v1 v2 => <| v1 v2 |>
(* v $ S₀ w ~> w v *)
| redex_shift' v w => <| w v |>
(* v $ let x = S₀ w in e ~> (λ x. v $ e) $ S₀ w *)
| redex_dol_let' v w e => <| (λ {liftV' v} $ e) $ S₀ w |>
(* J[p] ~> let x = p in J[x] *)
| redex_let' j p => <| let p in ↑j[0] |>
(* let x = v in e ~> e [x := v] *)
| redex_let_beta' v e => <| e [0 := v] |>
(* let x = (let y = S₀ v1 in e2) in e3 ~> let y = S₀ v1 in let x = e2 in e3 *)
| redex_let_assoc' v1 e2 e3 => <| let S₀ v1 in let e2 in {map' (option_map Some) e3} |>
end.
Definition optional_step' e :=
match decompose' e with
| dec_redex' k t r => Some <| k[t[{contract' r}]] |>
| _ => None
end.
Reserved Notation "e1 ~>' e2" (at level 40).
Inductive contr' : tm' ∅ → tm' ∅ → Prop :=
| contr_tm' : ∀ r, redex_to_term' r ~>' contract' r
where "e1 ~>' e2" := (contr' e1 e2).
Global Hint Constructors contr' : core.
Reserved Notation "e1 -->' e2" (at level 40).
Inductive step' : tm' ∅ → tm' ∅ → Prop :=
| step_tm' : ∀ (k : K' ∅) (t : T' ∅) (e1 e2 : tm' ∅), e1 ~>' e2 → <| k[t[e1]] |> -->' <| k[t[e2]] |>
where "e1 -->' e2" := (step' e1 e2).
Global Hint Constructors step' : core.
Notation "e1 -->'* e2" := (multi step' e1 e2) (at level 40).
Lemma step_contr' : ∀ {e1 e2},
e1 ~>' e2 →
e1 -->' e2.
Proof.
intros.
apply (step_tm' K_nil' T_nil' e1 e2).
assumption.
Qed.
Lemma multi_contr' : ∀ {e1 e2},
e1 ~>' e2 →
e1 -->'* e2.
Proof.
intros. apply (multi_step _ _ _ _ (step_contr' H)); auto.
Qed.
Lemma multi_contr_multi' : ∀ {e1 e2 e3},
e1 ~>' e2 →
e2 -->'* e3 →
e1 -->'* e3.
Proof.
intros. eapply multi_step; try eapply (step_tm' K_nil' T_nil'); cbn; eassumption.
Qed.
Definition contr_beta' : ∀ e (v : val' ∅), <| (λ e) v |> ~>' <| e [ 0 := v ] |> := λ e v, contr_tm' (redex_beta' e v).
Definition contr_dollar' : ∀ (v1 v2 : val' ∅), <| v1 $ v2 |> ~>' <| v1 v2 |> := λ v1 v2, contr_tm' (redex_dollar' v1 v2).
Definition contr_shift' : ∀ (v w : val' ∅), <| v $ S₀ w |> ~>' <| w v |> := λ v w, contr_tm' (redex_shift' v w).
Definition contr_dol_let' : ∀ (v v1 : val' ∅) e2, <| v $ let S₀ v1 in e2 |> ~>' <| (λ {liftV' v} $ e2) $ S₀ v1 |> := λ v v1 e2, contr_tm' (redex_dol_let' v v1 e2).
Definition contr_let' : ∀ (j : J' ∅) (p : non' ∅), <| j[p] |> ~>' <| let p in ↑j[0] |> := λ j p, contr_tm' (redex_let' j p).
Definition contr_let_beta' : ∀ (v : val' ∅) e, <| let v in e |> ~>' <| e [ 0 := v ] |> := λ v e, contr_tm' (redex_let_beta' v e).
Definition contr_let_assoc' : ∀ (v1 : val' ∅) e2 e3, <| let (let S₀ v1 in e2) in e3 |> ~>' <| let S₀ v1 in let e2 in {map' (option_map Some) e3} |> := λ v1 e2 e3, contr_tm' (redex_let_assoc' v1 e2 e3).
Global Hint Resolve step_contr' contr_beta' contr_dollar' contr_shift' contr_dol_let' contr_let' contr_let_beta' contr_let_assoc' : core.
Lemma deterministic_contr' : ∀ e e1 e2,
e ~>' e1 →
e ~>' e2 →
e1 = e2.
Proof.
intros e e1 e2 H1 H2.
inversion H1; clear H1; subst.
inversion H2; clear H2; subst.
apply inj_redex' in H0; subst; reflexivity.
Qed.
Lemma redex_when_contr' : ∀ e1 e2,
e1 ~>' e2 →
∃ r1, e1 = redex_to_term' r1.
Proof.
intros. inversion H; clear H; subst.
exists r; auto.
Qed.
Lemma deterministic_step' : ∀ e e1 e2,
e -->' e1 →
e -->' e2 →
e1 = e2.
Proof.
intros e e1 e2 H1 H2.
inversion H1; clear H1; subst.
inversion H2; clear H2; subst.
apply redex_when_contr' in H as HH; destruct HH as [r0 HH]; subst.
apply redex_when_contr' in H1 as HH; destruct HH as [r1 HH]; subst.
assert (dec_redex' k0 t0 r1 = dec_redex' k t r0) as Hktr.
{ repeat rewrite <- decompose_plug_redex'. f_equal; assumption. }
inversion Hktr; clear Hktr; subst.
repeat f_equal.
apply (deterministic_contr' r0); assumption.
Qed.
Fixpoint eval' i e :=
match i with
| 0 => e
| S j =>
match optional_step' e with
| Some e' => eval' j e'
| None => e
end
end.
Section Examples.
Definition _id : tm' ∅ := <| λ 0 |>.
Definition _const : tm' ∅ := <| λ λ 1 |>.
Compute (eval' 10 <| _id $ _const $ S₀, 0 |>).
Compute (eval' 10 <| _const $ _id $ S₀, 0 |>).
Definition j1 : J' ∅ := J_fun' <| λ 0 0 |>.
Definition j2 : J' ∅ := J_arg' <| λv' 0 |>.
Definition j3 : J' ∅ := J_dol' <| λ 0 |>.
Definition ej123 := <| j1[j2[j3[S₀, 0]]] |>.
Example from_K_to_K' : eval' 3 ej123 = <|
let
let
let S₀, 0
in ↑j3[0]
in ↑j2[0]
in ↑j1[0]
|>. Proof. auto. Qed.
Example from_K_to_stuck' : eval' 5 ej123 = <|
let S₀, 0 in
let
let ↑j3[0]
in ↑↑j2[0]
in ↑↑j1[0]
|>. Proof. cbn. auto. Qed.
Example ex_dol_let : eval' 6 <| _id $ ej123 |> = <|
(λ ↑_id $
let
let ↑j3[0]
in ↑↑j2[0]
in ↑↑j1[0]
) $ S₀, 0
|>. Proof. cbn. auto. Qed.
Example ex_shift : eval' 8 <| _id $ ej123 |> = <|
λ ↑_id $ let (let ↑j3[0] in ↑↑j2[0]) in ↑↑j1[0]
|>. Proof. cbn. auto. Qed.
Compute (decompose' (eval' 8 <| _id $ ej123 |>)).
End Examples.
Lemma plug_non_is_non' : ∀ (k' : K' ∅) (t' : T' ∅) (p' : non' ∅),
∃ p'', <| k' [t' [p']] |> = non_to_tm' p''.
Proof.
intros; destruct k'; cbn in *.
destruct t'; cbn in *.
eexists; reflexivity.
eexists (non_dol' _ _); reflexivity.
eexists (non_let' _ _); reflexivity.
Qed.
Lemma redex_is_non' : ∀ {A} r', ∃ p', @redex_to_term' A r' = non_to_tm' p'.
Proof.
intros. destruct r'; cbn;
try solve [try destruct j; eexists (non_app' _ _) + eexists (non_dol' _ _) + eexists (non_let' _ _); reflexivity].
Qed.
Lemma non_when_contr' : ∀ e' e'',
e' ~>' e'' →
∃ p', e' = non_to_tm' p'.
Proof.
intros. inversion H; clear H; subst.
apply redex_is_non'.
Qed.
Lemma non_when_step' : ∀ e' e'',
e' -->' e'' →
∃ p', e' = non_to_tm' p'.
Proof.
intros. inversion H; clear H; subst.
apply non_when_contr' in H0 as [p' Hp]; subst.
apply plug_non_is_non'.
Qed.
Lemma non_when_steps_to_non' : ∀ e' (p'' : non' ∅),
e' -->'* p'' →
∃ p', e' = non_to_tm' p'.
Proof.
intros. dependent induction H. exists p''. reflexivity.
destruct (IHmulti p'') as [p' IH]; auto; subst.
apply (non_when_step' x p'); auto.
Qed.
Lemma non_when_steps_to_plug_non' : ∀ e (k' : K' ∅) (t' : T' ∅) (p' : non' ∅),
e -->'* <| k' [t' [p']] |> →
∃ p, e = non_to_tm' p.
Proof.
intros. destruct (plug_non_is_non' k' t' p') as [p'' Hp''].
apply (non_when_steps_to_non' _ p''). rewrite Hp'' in *. assumption.
Qed.
Lemma step_let' : ∀ {e1 e2 e},
e1 -->' e2 →
<| let e1 in e |> -->' <| let e2 in e |>.
Proof.
intros. generalize dependent e.
induction H; auto; intros.
apply (step_tm' (K_let' k e) t e1 e2).
apply H.
Qed.
Lemma multi_let' : ∀ {e1 e2 e},
e1 -->'* e2 →
<| let e1 in e |> -->'* <| let e2 in e |>.
Proof.
intros. generalize dependent e.
induction H; auto; intros.
eapply (multi_step); [idtac | apply IHmulti].
apply step_let'.
apply H.
Qed.
Lemma step_delim' : ∀ {v : val' ∅} {e1 e2},
e1 -->' e2 →
<| v $ e1 |> -->' <| v $ e2 |>.
Proof.
intros. generalize dependent v.
induction H; auto; intros.
apply (step_tm' K_nil' (T_cons' v k t) e1 e2).
apply H.
Qed.