-
Notifications
You must be signed in to change notification settings - Fork 2
/
Translation.v
1459 lines (1283 loc) · 51.6 KB
/
Translation.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
(** A formalization of the Section 4 of the paper [Reasoning about the garden of
forking paths].
The main results of the file are (1) a formalization of our monadic translation
and the operational semantic presented in Hackett & Hutton (2019); (2) a proof
of equivalence between the two semantics.
The Hackett & Hutton (2019) paper can be found at:
[https://dl.acm.org/doi/10.1145/3341718] (The paper is open access.)
Semantics:
- [BigStep]: Hackett & Hutton's Clairvoyant CBV (operational semantics)
- [eval]: our monadic translation
Equivalence:
- [soundness]: [BigStep] semantics are a subset of [eval] semantics.
- [adequacy]: [eval] semantics are a subset of [BigStep] semantics.
Putting those together, the equivalence is made explicit in
[soundness_and_adequacy]. *)
Set Implicit Arguments.
Set Contextual Implicit.
From Coq Require Import Arith Psatz Setoid Morphisms.
From Clairvoyance Require Import Clairvoyance.
(** * Axioms
We use functional and propositional extensionality. *)
From Coq Require Import FunctionalExtensionality.
(* Propositional extensionality *)
Axiom prop_ext : forall P Q, (P <-> Q) -> P = Q.
(* We spent some effort to minimize the dependency on axioms,
that's why you'll see us defining some extensional equality relations
explicitly, even though they're equivalent to Leibniz equality [eq]
under those two axioms. *)
Declare Scope ctx_scope.
(** * Helper functions. *)
(** The [fmap] function for the thunk datatype [T]. *)
Definition mapT {a b} (f : a -> b) (t : T a) : T b :=
match t with
| Undefined => Undefined
| Thunk x => Thunk (f x)
end.
Definition T_prop {a} (f : a -> Prop) (t : T a) : Prop :=
match t with
| Undefined => True
| Thunk x => f x
end.
(** * Extensional equality and inequality on [M]. *)
(** Two sets [u] and [v] are equal when they have the same elements.
Since we've assumed functional and propositional extensionality,
this is equivalent to Leibniz equality [eq]. Otherwise we would have
to sprinkle [eq_M] all over the place. *)
Definition eq_M {a} (u v : M a) : Prop := forall x n, u x n <-> v x n.
(** A set [t], viewed as a computation, "costs less" than another computation
[t'] when it can produce (at least) all same results as [t] for less.
This is only used for facts about "simplifying ticks" (Section 4.4),
unrelated to soundness and adequacy. *)
Definition le_M {a} (t t' : M a) : Prop :=
forall x n, t' x n -> exists n', t x n' /\ n' <= n.
Declare Scope M_scope.
Delimit Scope M_scope with M.
Infix "<=" := le_M : M_scope.
Infix "=" := eq_M : M_scope.
(** The extensional equality [eq_M] is reflexive. *)
Lemma Reflexive_eq_M {a} (u : M a) : eq_M u u.
Proof. firstorder. Qed.
(** The extensional equality [eq_M] is symmetric. *)
Lemma Symmetric_eq_M {a} (u v : M a) : eq_M u v -> eq_M v u.
Proof. firstorder. Qed.
(** [le_M] is reflexive. *)
Lemma Reflexive_le_M {a} (u : M a) : (u <= u)%M.
Proof.
red; firstorder.
Qed.
(** * Lemmas for [eq_M]. *)
(** Respectfulness lemmas: [eq_M] is preserved by the
various operations we've defined on [M]. *)
(** A reasoning rule for extensional equality on [M]: if [u1] and [u2] are
equal, and [k1] and [k2] are equal, then [bind u1 k1] and [bind u2 k2] are
also equal. *)
Lemma eq_M_bind {a b} (u1 u2 : M a) (k1 k2 : a -> M b)
: (u1 = u2)%M ->
(forall x, (k1 x = k2 x)%M) ->
(bind u1 k1 = bind u2 k2)%M.
Proof.
unfold eq_M. firstorder.
repeat eexists; eauto. apply H; eauto. apply H0; eauto.
repeat eexists; eauto. apply H; eauto. apply H0; eauto.
Qed.
(** Similar to the above theorem but for [thunk]s. *)
Lemma eq_M_thunk {a} (u1 u2 : M a) :
(u1 = u2)%M -> (thunk u1 = thunk u2)%M.
Proof.
intros H [] ?; cbn; auto. reflexivity.
Qed.
(** Similar to the above theorem but for [ret]s. *)
Lemma eq_M_ret {a} (x1 x2 : a) :
x1 = x2 -> (ret x1 = ret x2)%M.
Proof.
unfold eq_M. firstorder congruence.
Qed.
(** * Lemmas for [le_M]. *)
Lemma bind_le {a b} (u1 u2 : M a) (k1 k2 : a -> M b)
: (u1 <= u2)%M ->
(forall x, k1 x <= k2 x)%M ->
(bind u1 k1 <= bind u2 k2)%M.
Proof.
intros Hu Hk y n (x & mx & my & Hu2 & Hk2 & Hn).
apply Hu in Hu2; apply Hk in Hk2.
destruct Hu2 as (mx1 & ? & ?).
destruct Hk2 as (my1 & ? & ?).
exists (mx1 + my1). split; [ | lia ].
exists x, mx1, my1; eauto.
Qed.
Lemma bind_eq_le {a b} (u : M a) (k1 k2 : a -> M b)
: (forall x, k1 x <= k2 x)%M ->
((bind u k1) <= (bind u k2))%M.
Proof.
apply bind_le. apply Reflexive_le_M.
Qed.
Lemma bind_le_r {a b} (u : M a) (u1 u2 : M b)
: (u1 <= u2)%M -> (u1 <= bind u (fun _ => u2))%M.
Proof.
intros H y n (x & mx & my2 & Hx & Hy2 & Hn).
apply H in Hy2. destruct Hy2 as (my1 & ? & ?).
exists my1; split; [ auto | lia ].
Qed.
Lemma thunk_le {a} (u1 u2 : M a)
: (u1 <= u2)%M -> (thunk u1 <= thunk u2)%M.
Proof.
intros H [y | ] n; cbn; eauto.
Qed.
Lemma forcing_le {a b} (x : T a) (k1 k2 : a -> M b)
: T_prop (fun x => k1 x <= k2 x)%M x -> (forcing x k1 <= forcing x k2)%M.
Proof.
destruct x; cbn; [ auto | ].
unfold le_M. contradiction.
Qed.
(** * Simplifying ticks.
The theorems justify the rewrite rules discussed in Section 4.4.
This is unrelated to soundness and adequacy, which are
the main results of this file. *)
Lemma bind_tick {a b} (t : M a) (k : a -> M b)
: (bind t (fun x => tick >> k x) = tick >> bind t k)%M.
Proof.
unfold bind, tick, eq_M. firstorder.
- eexists tt, 1, _. split; auto.
split; [ eexists _, _, _; split; eauto |]. lia.
- eexists _, _, _; split; [ eauto | ].
split; [ eexists tt, _, _; split; eauto |]. lia.
Qed.
Lemma thunk_tick {a} (u : M a)
: (thunk (tick >> u) <= tick >> thunk u)%M.
Proof.
unfold thunk, bind, tick, le_M; firstorder.
destruct x.
- eexists; split; [ | constructor ]. exists tt. eauto.
- exists 0; split; auto. lia.
Qed.
Module Lambda.
(** * The calculus with folds. *)
(** * Figure 6. *)
(** Types.
Our formalization of the calculus slightly generalizes what we present in
the paper. In particular, (1) we add a product type, and (2) we generalize
the [unit] type of Fig. 6 to an arbitrary set of base types. *)
Inductive Ty : Type :=
| Arr : Ty -> Ty -> Ty (* Functions *)
| Prod : Ty -> Ty -> Ty (* Products *)
| List : Ty -> Ty (* A basic recursive data type: List a = Unit + a * List a *)
| Base : Type -> Ty (* Base types are arbitrary types of the host language (Coq) *)
.
(** Context.
We are using a nameless representation adapted from de Bruijn indices here.
Learn more about de Bruijn indices here:
[https://en.wikipedia.org/wiki/De_Bruijn_index]
Instead of using natural numbers as in de Bruijn indices, we index
variables by the context they belong to, ensuring that variables are
always well-scoped by construction. *)
(** A context is a list of types. *)
Inductive Ctx : Type :=
| NilCtx
| ConsCtx : Ctx -> Ty -> Ctx
.
Infix ":,:" := ConsCtx (at level 50).
(** A variable is an index into a context.
Its type parameters associate it to the context it indexes into,
and the type it points to. It is essentially a natural number (de Bruijn
index) but with type information embedded in it.
[n : V g u] means that the [n]-th element in [g] is [u]. *)
Inductive V : Ctx -> Ty -> Type :=
| Here g x : V (g :,: x) x
| There g x y : V g y -> V (g :,: x) y
.
(** Terms. *)
Inductive Tm (g : Ctx) : Ty -> Type :=
| Let a b : Tm g a -> Tm (g :,: a) b -> Tm g b
| App a b : Tm g (Arr a b) -> V g a -> Tm g b
| Var a : V g a -> Tm g a
| Fun a b : Tm (g :,: a) b -> Tm g (Arr a b)
| Pair a b : V g a -> V g b -> Tm g (Prod a b)
| Fst a b : V g (Prod a b) -> Tm g a
| Snd a b : V g (Prod a b) -> Tm g b
| Nil a : Tm g (List a)
| Cons a : V g a -> V g (List a) -> Tm g (List a)
| Foldr a b : Tm g b -> Tm (g :,: a :,: b) b -> V g (List a) -> Tm g b
| Bas (b : Type) : b -> Tm g (Base b)
.
(* ---------------------- Denotational semantics (our contribution) ---------------------- *)
(** * The approximation of [List]s. *)
Unset Elimination Schemes.
Inductive ListA (a : Type) : Type :=
| NilA : ListA a
| ConsA : T a -> T (ListA a) -> ListA a
.
(* For [ListA], we need to define our own induction principle because Coq cannot
generate the correct induction principles for nested inductive datatypes.
See the [Nested Inductive Types] section in CPDT
(http://adam.chlipala.net/cpdt/html/Cpdt.InductiveTypes.html). *)
Definition ListA_ind {a} (P : ListA a -> Prop)
(H_NilA : P NilA)
(H_ConsA : forall x xs, T_prop P xs -> P (ConsA x xs))
: forall xs, P xs.
Proof.
fix SELF 1; intros [].
- apply H_NilA.
- apply H_ConsA; destruct t0; cbn; [ apply SELF | auto].
Qed.
Set Elimination Schemes.
(** * The type translation. *)
(** * Figure 7. *)
(** We translate the types in the calculus with folds to the types in
Gallina. *)
Fixpoint toType (u : Ty) : Type :=
match u with
| Arr u1 u2 => T (toType u1) -> M (toType u2)
| Prod u1 u2 => (T (toType u1) * T (toType u2))%type
| List a => ListA (toType a)
| Base b => b
end.
(** We translate contexts to environments, which are heterogeneous lists
of elements whose types are given by the context (a list of types).
Concretely, this heterogeneous list is represented by left-nested tuples.
The context binds thunks, hence each component is wrapped in [T]. *)
Fixpoint env (g : Ctx) : Type :=
match g with
| NilCtx => unit
| ConsCtx g1 u => env g1 * T (toType u)
end.
(** As a variable [v : V g u] is an index into a context [g],
it can be used to look up the corresponding element in a heterogeneous list
indexed by [g], which must have type [T (toType u)] by definition of [env g]. *)
Fixpoint lookup {g u} (v : V g u) : env g -> T (toType u) :=
match v with
| Here => fun ex => snd ex
| There v1 => fun ex => lookup v1 (fst ex)
end.
(** * Figure 9. *)
(** * Definitions of [foldrA]. *)
Fixpoint foldrA' {a b} (n : M b) (c : T a -> T b -> M b) (x' : ListA a)
: M b :=
tick >>
match x' with
| NilA => n
| ConsA x1 x2 =>
let~ y2 := foldrA' n c $! x2 in
c x1 y2
end.
Definition foldrA {a b} (n : M b) (c : T a -> T b -> M b)
(x : T (ListA a)) : M b :=
foldrA' n c $! x.
(** * Figure 8. *)
(** * The term translation, aka. a denotational semantics.
The [eval]uation of a term [t] in some environment [e : env g]
is a computation (in the monad [M]) producing a value of type [toType u]. *)
Fixpoint eval {g u} (t : Tm g u) : env g -> M (toType u) := fun e =>
match t with
| Let t1 t2 => fun e =>
tick >>
let~ x := eval t1 e in
eval t2 (e, x)
| App t1 v => fun e =>
tick >>
let! f := eval t1 e in
f (lookup v e)
| Var v => fun e => tick >> force (lookup v e)
| Fun f => fun e => ret (fun x => eval f (e, x))
| Pair v1 v2 => fun e =>
ret (lookup v1 e, lookup v2 e)
| Fst v => fun e => tick >>
let! x := force (lookup v e) in force (fst x)
| Snd v => fun e => tick >>
let! x := force (lookup v e) in force (snd x)
| Nil => fun _ => ret NilA
| Cons v1 v2 => fun e => ret (ConsA (lookup v1 e) (lookup v2 e))
| Foldr n c v1 => fun e =>
foldrA
(eval n e)
(fun x1 y2 => eval c (e, x1, y2))
(lookup v1 e)
| Bas b => fun _ => ret b
end e.
(** Example: append
This is an [appendA] we translate from [append] written in the calculus with
folds. For code presented in Fig. 10, which is a translation from the code
written in Gallina in Fig. 1, refer to the [Clairvoyance.v] file. *)
Notation V0 := Here.
Notation V1 := (There Here).
Definition append {a} : Tm (NilCtx :,: List a :,: List a) (List a) :=
Foldr (Var V0) (Cons V1 V0) V1.
(* Manually translated with simplifications (fewer ticks; see discussion from Section 4.3). *)
Fixpoint appendA_ {a} (xs : ListA a) (ys : T (ListA a)) : M (ListA a) :=
tick >>
match xs with
| NilA => force ys
| ConsA x xs =>
let~ zs := (fun xs => appendA_ xs ys) $! xs in
ret (ConsA x zs)
end.
Definition appendA {a} (xs ys : T (ListA a)) : M (ListA a) :=
(fun xs => appendA_ xs ys) $! xs.
(** The costs of [append] and [appendA] are asymptotically equivalent. Informally:
cost(appendA) <= cost(append) <= 2 * cost(appendA)
The two main theorems are [appendA_le_append] and [append_le_appendA].
*)
Lemma appendA_le_append_ {a} (xs : ListA (toType a)) (ys : T (ListA (toType a)))
: (appendA_ xs ys <= eval (Foldr (g := NilCtx :,: _ :,: _) (Var V0) (Cons V1 V0) V1) (tt, Thunk xs, ys))%M.
Proof.
induction xs; intros; cbn.
- apply bind_eq_le; intros _. apply bind_le_r, Reflexive_le_M.
- apply bind_eq_le; intros _. apply bind_le.
+ apply thunk_le. apply forcing_le.
destruct xs; cbn in *; eauto.
+ intros. apply Reflexive_le_M.
Qed.
(** [appendA] costs at most as much as the denotation of [append]. *)
Theorem appendA_le_append {a} (xs ys : T (ListA (toType a)))
: (appendA xs ys <= eval append (tt, xs, ys))%M.
Proof.
apply forcing_le; destruct xs; cbn; [ | auto ].
eapply appendA_le_append_.
Qed.
(** Multiply the cost of a computation [u] by a constant [c]. *)
Definition costMul {a} (c : nat) (u : M a) : M a :=
fun x n => exists m, u x m /\ c * m = n.
Definition le_costMul {a} (c : nat) (u1 u2 : M a)
: u2 {{ fun x2 n2 => u1 [[ fun x1 n1 => x2 = x1 /\ n1 <= c * n2 ]] }} ->
(u1 <= costMul c u2)%M.
Proof.
intros H x n (ndiv & Hx & Hn).
apply H in Hx. destruct Hx as (y & ny & Hy & <- & Hny).
eexists; split; [ eauto | lia ].
Qed.
Lemma append_le_appendA_ {a} (xs : ListA (toType a)) (ys : T (ListA (toType a)))
: (appendA_ xs ys)
{{ fun x2 n2 =>
(eval (Foldr (g := NilCtx :,: _ :,: _) (Var V0) (Cons V1 V0) V1) (tt, Thunk xs, ys))
[[ fun x1 n1 => x2 = x1 /\ n1 <= 2 * n2 ]] }}.
Proof.
induction xs as [ | x xs IH]; cbn; intros; mgo'.
- destruct x0; cbn in *.
+ mgo'. apply optimistic_thunk_go. mgo'.
+ intros ? ?. inversion 1; subst. mgo'.
apply optimistic_thunk_go. relax. apply (IH x0 n). apply H.
intros; mgo'. destruct H1; subst. intuition.
- apply optimistic_skip. mgo'.
Qed.
(** The denotation of [append] costs at most twice as much as [appendA]. *)
Theorem append_le_appendA {a} (xs ys : T (ListA (toType a)))
: (eval append (tt, xs, ys) <= costMul 2 (appendA xs ys))%M.
Proof.
apply le_costMul.
unfold appendA; cbn; unfold foldrA.
destruct xs.
- cbn. apply append_le_appendA_.
- intros ? ? [].
Qed.
(** * Operational semantics of Hackett & Hutton (2019).
In this part, we formalize the operational semantics presented in Fig. 3 of
Hackett & Hutton (2019). The paper can be found at:
[https://dl.acm.org/doi/10.1145/3341718] *)
(** Syntactic values.
Syntactic values are closures and base values. *)
Variant Vl (g : Ctx) : Ty -> Type :=
| VLam a b : Tm (g :,: a) b -> Vl g (Arr a b)
| VPair a b : V g a -> V g b -> Vl g (Prod a b)
| VNil a : Vl g (List a)
| VCons a : V g a -> V g (List a) -> Vl g (List a)
| VBas b : b -> Vl g (Base b)
.
(** Heaps are lists of syntactic values. *)
Inductive Heap : Ctx -> Type :=
| HNil : Heap NilCtx
| HCons g a : Heap g -> T (Vl g a) -> Heap (g :,: a)
.
Infix ":*:" := HCons (at level 40).
(** General operations on syntax *)
(** Append contexts *)
Fixpoint app_Ctx (g g' : Ctx) : Ctx :=
match g' with
| NilCtx => g
| ConsCtx g' u => ConsCtx (app_Ctx g g') u
end.
Infix "++" := app_Ctx : ctx_scope.
Bind Scope ctx_scope with Ctx.
(** The first element of a non-empty heap. *)
Definition here {g u} (e : Heap (g :,: u)) : T (Vl g u) :=
match e in Heap g0 return
match g0 with
| NilCtx => True
| (_ :,: u) => T (Vl _ u)
end with
| HNil => I
| HCons _ h => h
end.
(** The tail of a non-empty heap. *)
Definition there {g u} (e : Heap (g :,: u)) : Heap g :=
match e in Heap g0 return
match g0 with
| NilCtx => True
| (g :,: _) => Heap g
end with
| HNil => I
| HCons t _ => t
end.
(** Renaming from context [g] to context [g'] (a substitution whose codomain is
variables). *)
Definition Rnm (g g' : Ctx) := forall a, V g a -> V g' a.
(** An eliminator for [V], when the context is explicitly of the form [g :,: a]. *)
Definition elimV {g a b} (v : V (g :,: a) b) {r : Ty -> Type} : (V g b -> r b) -> r a -> r b :=
match v in V g_ b_ return
match g_ with
| (g :,: a) => _
| _ => False
end with
| Here => fun _ y => y
| There v => fun x _ => x v
end.
(** Extend a renaming with a variable (renamed to itself). *)
Definition shift {g g' : Ctx} (s : Rnm g g') {a} : Rnm (g :,: a) (g' :,: a) :=
fun _ v => elimV v (fun v => There (s _ v)) Here.
(** Helper renaming for the operational semantics of [Foldr].
Given [fcons] in context [g1 :,: a :,: b], we rename it by
1. looking up [a] in the context [g1];
2. applying some substitution on [g2]. *)
Definition shiftAlgCons {g1 g2 a b} (v1 : V g1 a) (s : Rnm g1 g2)
: Rnm (g1 :,: a :,: b) (g2 :,: b) :=
shift (fun _ v => s _ (elimV v (fun v => v) v1)).
(** Restriction of a renaming, forgetting one variable in the original context. *)
Definition restr {g g' : Ctx} {a} (s : Rnm (g :,: a) g') : Rnm g g' :=
fun _ v => s _ (There v).
(** Rename a term. *)
Fixpoint rename {g g'} (s : Rnm g g') {a} (t : Tm g a) : Tm g' a :=
match t with
| Let t1 t2 => Let (rename s t1) (rename (shift s) t2)
| App t1 v => App (rename s t1) (s _ v)
| Var v => Var (s _ v)
| Fun t1 => Fun (rename (shift s) t1)
| Pair v1 v2 => Pair (s _ v1) (s _ v2)
| Fst v => Fst (s _ v)
| Snd v => Snd (s _ v)
| Nil => Nil
| Cons v1 v2 => Cons (s _ v1) (s _ v2)
| Foldr t1 t2 v => Foldr (rename s t1) (rename (shift (shift s)) t2) (s _ v)
| Bas b => Bas b
end.
(** Rename a value. *)
Definition renameVl {g g'} (s : Rnm g g') {a} (t : Vl g a) : Vl g' a :=
match t with
| VLam t1 => VLam (rename (shift s) t1)
| VPair v1 v2 => VPair (s _ v1) (s _ v2)
| VNil => VNil
| VCons v1 v2 => VCons (s _ v1) (s _ v2)
| VBas b => VBas b
end.
Definition id_Rnm {g} : Rnm g g :=
fun _ v => v.
Definition cat_Rnm {g g' g''} : Rnm g g' -> Rnm g' g'' -> Rnm g g'' :=
fun s1 s2 _ v => s2 _ (s1 _ v).
Infix ">>>" := cat_Rnm (at level 40).
(** Look up a value in a heap indexed by a variable [v] and rename the result.
This is equivalent to a composition of [lookup'] and [renameVl],
except this function is needed to define [lookup'] in the first place. *)
Fixpoint lookup_' {g g' u} (v : V g u) : Rnm g g' -> Heap g -> T (Vl g' u) :=
match v with
| Here => fun s e => mapT (renameVl (restr s)) (here e)
| There v1 => fun s gx => lookup_' v1 (restr s) (there gx)
end.
(** Look up a value in a heap indexed by a variable [v]. *)
Definition lookup' {g u} (v : V g u) : Heap g -> T (Vl g u) :=
lookup_' v id_Rnm.
(** A helper for factoring the rules for [Foldr], which involves nondeterminism like [Let].
Either run a computation (according to the given [BigStep_] relation,
wrapping the result in a [Thunk]), or do nothing (returning [Undefined]). *)
Inductive LazyStep g u (e : Heap g) (BigStep_ : forall g', Rnm g g' -> Heap g' -> Vl g' u -> nat -> Prop)
: forall g', Rnm g g' -> Heap g' -> T (Vl g' u) -> nat -> Prop :=
| LazyStep_SKIP : LazyStep e BigStep_ id_Rnm e Undefined 0
| LazyStep_STEP g' (s' : Rnm g g') e' r n
: BigStep_ g' s' e' r n ->
LazyStep e BigStep_ s' e' (Thunk r) n
.
Definition vthere {g a} : Rnm g (g :,: a) := fun _ v => There v.
(* A helper for [BigStep]'s induction principle.
Monotonicity of [LazyStep] as a function on relations. *)
Lemma LazyStep_mon g u (e : Heap g)
(BigStep_ BigStep_' : forall g', Rnm g g' -> Heap g' -> Vl g' u -> nat -> Prop)
: (forall g' (s' : Rnm g g') (e' : Heap g') (v : Vl g' u) (n : nat),
BigStep_ g' s' e' v n -> BigStep_' g' s' e' v n) ->
forall g' (s' : Rnm g g') (e' : Heap g') (v : T (Vl g' u)) (n : nat),
LazyStep e BigStep_ s' e' v n -> LazyStep e BigStep_' s' e' v n.
Proof.
intros H * []; constructor; [ apply H; assumption ].
Defined.
Unset Elimination Schemes.
(** [and] lifted to relations. *)
Definition andBS {g u}
(BigStep_ BigStep_' : forall g', Rnm g g' -> Heap g' -> Vl g' u -> nat -> Prop) :=
fun g' s' e' v n => BigStep_ g' s' e' v n /\ BigStep_' g' s' e' v n.
(** [BigStep t1 e1 s2 e2 v2 n2]:
The term [t1] in heap [e1] evaluates to value [v2] in heap [e2] in time [n2],
and [s2] is a mapping from locations in [e1] to locations in [e2].
The extra complexity is mainly due to our intrinsically typed syntax,
for which we have to rename to evaluate some terms in a modified heap. *)
Inductive BigStep : forall g u, Tm g u -> Heap g -> forall g', Rnm g g' -> Heap g' -> Vl g' u -> nat -> Prop :=
| BigStep_Let_SKIP g a b (t1 : Tm g a) (t2 : Tm (g :,: a) b) (e : Heap g) g' (s' : Rnm _ g') e' r n
: BigStep t2 (e :*: Undefined) (g' := g') s' e' r n ->
BigStep (Let t1 t2) e (restr s') e' r (S n)
| BigStep_Let_STEP g a b (t1 : Tm g a) (t2 : Tm (g :,: a) b) (e : Heap g)
g1 (s1 : Rnm _ g1) e1 r1 n1
g2 (s2 : Rnm _ g2) e2 r2 n2
: BigStep t1 e s1 e1 r1 n1 ->
BigStep (rename (shift s1) t2) (e1 :*: Thunk r1) s2 e2 r2 n2 ->
BigStep (Let t1 t2) e (s1 >>> restr s2) e2 r2 (S (n1 + n2))
| BigStep_App g a b (t : Tm g (Arr a b)) (v : V g a) e
g1 (s1 : Rnm _ g1) (e1 : Heap g1) r1 n1
g2 (s2 : Rnm _ g2) (e2 : Heap g2) r2 n2
: BigStep t e s1 e1 (VLam r1) n1 ->
BigStep r1 (e1 :*: lookup' (s1 _ v) e1) s2 e2 r2 n2 ->
BigStep (App t v) e (s1 >>> restr s2) e2 r2 (S (n1 + n2))
| BigStep_Var g a (v : V g a) e r
: Thunk r = lookup' v e ->
BigStep (Var v) e id_Rnm e r 1
| BigStep_Fun g a b (t : Tm (g :,: a) b) e
: BigStep (Fun t) e id_Rnm e (VLam t) 0
| BigStep_Pair g a b (v1 : V g a) (v2 : V g b) e
: BigStep (Pair v1 v2) e id_Rnm e (VPair v1 v2) 0
| BigStep_Fst g a b (v : V g (Prod a b)) e v1 v2 r1
: Thunk (VPair v1 v2) = lookup' v e ->
Thunk r1 = lookup' v1 e ->
BigStep (Fst v) e id_Rnm e r1 1
| BigStep_Snd g a b (v : V g (Prod a b)) e v1 v2 r2
: Thunk (VPair v1 v2) = lookup' v e ->
Thunk r2 = lookup' v2 e ->
BigStep (Snd v) e id_Rnm e r2 1
| BigStep_Nil g a (e : Heap g)
: BigStep (@Nil _ a) e id_Rnm e VNil 0
| BigStep_Cons g a (v1 : V g a) (v2 : V g (List a)) e
: BigStep (Cons v1 v2) e id_Rnm e (VCons v1 v2) 0
| BigStep_Foldr_Nil g a b (fnil : Tm g b) fcons (v : V g (List a)) e
g1 (s1 : Rnm _ g1) (e1 : Heap g1) r1 n1
: Thunk VNil = lookup' v e ->
BigStep fnil e s1 e1 r1 n1 ->
BigStep (Foldr fnil fcons v) e s1 e1 r1 (S n1)
| BigStep_Foldr_Node g a b (fnil : Tm g b) (fcons : Tm (g :,: a :,: b) b) v e v1 v2
g1 (s1 : Rnm _ g1) (e1 : Heap g1) r1 n1
g2 (s2 : Rnm _ g2) (e2 : Heap g2) r2 n2
: Thunk (VCons v1 v2) = lookup' v e ->
LazyStep e (@BigStep g b (Foldr fnil fcons v2) e) s1 e1 r1 n1 ->
BigStep (rename (shiftAlgCons v1 s1) fcons) (e1 :*: r1) s2 e2 r2 n2 ->
BigStep (Foldr fnil fcons v) e (s1 >>> restr s2) e2 r2 (S (n1 + n2))
| BigStep_Base g b (x : b) e
: BigStep (Bas (g := g) x) e id_Rnm e (VBas x) 0
.
(** [BigStep] is a nested inductive type, so we must again define its
induction principle by hand (this is mostly copy-pasted from the wrong
induction principle generated by Coq, and fixing the [BigStep_Foldr_Node] case. *)
Definition BigStep_ind :
forall
P : forall (g : Ctx) (u : Ty),
Tm g u ->
Heap g -> forall g' : Ctx, Rnm g g' -> Heap g' -> Vl g' u -> nat -> Prop,
(forall (g : Ctx) (a b : Ty) (t1 : Tm g a) (t2 : Tm (g :,: a) b)
(e : Heap g) (g' : Ctx) (s' : Rnm (g :,: a) g')
(e' : Heap g') (r : Vl g' b) (n : nat),
BigStep t2 (e :*: Undefined) s' e' r n ->
P (g :,: a) b t2 (e :*: Undefined) g' s' e' r n ->
P g b (Let t1 t2) e g' (restr s') e' r (S n)) ->
(forall (g : Ctx) (a b : Ty) (t1 : Tm g a) (t2 : Tm (g :,: a) b)
(e : Heap g) (g1 : Ctx) (s1 : Rnm g g1) (e1 : Heap g1)
(r1 : Vl g1 a) (n1 : nat) (g2 : Ctx) (s2 : Rnm (g1 :,: a) g2)
(e2 : Heap g2) (r2 : Vl g2 b) (n2 : nat),
BigStep t1 e s1 e1 r1 n1 ->
P g a t1 e g1 s1 e1 r1 n1 ->
BigStep (rename (shift s1) t2) (e1 :*: Thunk r1) s2 e2 r2 n2 ->
P (g1 :,: a) b (rename (shift s1) t2) (e1 :*: Thunk r1) g2 s2 e2 r2 n2 ->
P g b (Let t1 t2) e g2 (s1 >>> restr s2) e2 r2 (S (n1 + n2))) ->
(forall (g : Ctx) (a b : Ty) (t : Tm g (Arr a b))
(v : V g a) (e : Heap g) (g1 : Ctx) (s1 : Rnm g g1)
(e1 : Heap g1) (r1 : Tm (g1 :,: a) b) (n1 : nat)
(g2 : Ctx) (s2 : Rnm (g1 :,: a) g2) (e2 : Heap g2)
(r2 : Vl g2 b) (n2 : nat),
BigStep t e s1 e1 (VLam r1) n1 ->
P g (Arr a b) t e g1 s1 e1 (VLam r1) n1 ->
BigStep r1 (e1 :*: lookup' (s1 a v) e1) s2 e2 r2 n2 ->
P (g1 :,: a) b r1 (e1 :*: lookup' (s1 a v) e1) g2 s2 e2 r2 n2 ->
P g b (App t v) e g2 (s1 >>> restr s2) e2 r2 (S (n1 + n2))) ->
(forall (g : Ctx) (a : Ty) (v : V g a) (e : Heap g) (r : Vl g a),
Thunk r = lookup' v e -> P g a (Var v) e g id_Rnm e r 1) ->
(forall (g : Ctx) (a b : Ty) (t : Tm (g :,: a) b) (e : Heap g),
P g (Arr a b) (Fun t) e g id_Rnm e (VLam t) 0) ->
(forall (g : Ctx) (a b : Ty) (v1 : V g a) (v2 : V g b) (e : Heap g),
P g (Prod a b) (Pair v1 v2) e g id_Rnm e (VPair v1 v2) 0) ->
(forall (g : Ctx) (a b : Ty) (v : V g (Prod a b))
(e : Heap g) (v1 : V g a) (v2 : V g b) (r1 : Vl g a),
Thunk (VPair v1 v2) = lookup' v e ->
Thunk r1 = lookup' v1 e -> P g a (Fst v) e g id_Rnm e r1 1) ->
(forall (g : Ctx) (a b : Ty) (v : V g (Prod a b))
(e : Heap g) (v1 : V g a) (v2 : V g b) (r2 : Vl g b),
Thunk (VPair v1 v2) = lookup' v e ->
Thunk r2 = lookup' v2 e -> P g b (Snd v) e g id_Rnm e r2 1) ->
(forall (g : Ctx) (a : Ty) (e : Heap g), P g (List a) Nil e g id_Rnm e VNil 0) ->
(forall (g : Ctx) (a : Ty) (v1 : V g a) (v2 : V g (List a)) (e : Heap g),
P g (List a) (Cons v1 v2) e g id_Rnm e (VCons v1 v2) 0) ->
(forall (g : Ctx) (a b : Ty) (fnil : Tm g b)
(fcons : Tm ((g :,: a) :,: b) b)
(v : V g (List a)) (e : Heap g) (g1 : Ctx) (s1 : Rnm g g1)
(e1 : Heap g1) (r1 : Vl g1 b) (n1 : nat),
Thunk VNil = lookup' v e ->
BigStep fnil e s1 e1 r1 n1 ->
P g b fnil e g1 s1 e1 r1 n1 -> P g b (Foldr fnil fcons v) e g1 s1 e1 r1 (S n1)) ->
(forall (g : Ctx) (a b : Ty) (fnil : Tm g b)
(fcons : Tm ((g :,: a) :,: b) b)
(v : V g (List a)) (e : Heap g) (v1 : V g a) (v2 : V g (List a)) (g1 : Ctx)
(s1 : Rnm g g1) (e1 : Heap g1) (r1 : T (Vl g1 b))
(n1 : nat) (g2 : Ctx) (s2 : Rnm (g1 :,: b) g2) (e2 : Heap g2)
(r2 : Vl g2 b) (n2 : nat),
Thunk (VCons v1 v2) = lookup' v e ->
LazyStep e (andBS (BigStep (Foldr fnil fcons v2) e) (P _ _ (Foldr fnil fcons v2) e)) s1 e1 r1 n1 ->
BigStep (rename (shiftAlgCons v1 s1) fcons) (e1 :*: r1) s2 e2 r2 n2 ->
P (g1 :,: b) b (rename (shiftAlgCons v1 s1) fcons)
(e1 :*: r1) g2 s2 e2 r2 n2 ->
P g b (Foldr fnil fcons v) e g2 (s1 >>> restr s2) e2 r2 (S (n1 + n2))) ->
(forall (g : Ctx) (b : Type) (x : b) (e : Heap g),
P g (Base b) (Bas x) e g id_Rnm e (VBas x) 0) ->
forall (g : Ctx) (u : Ty) (t : Tm g u) (e : Heap g)
(g' : Ctx) (r : Rnm g g') (e0 : Heap g') (v : Vl g' u)
(n : nat), BigStep t e r e0 v n -> P g u t e g' r e0 v n.
Proof.
intros P.
fix SELF 23; intros;
lazymatch goal with
| [ H : BigStep _ _ _ _ _ _ |- _ ] => destruct H
end.
- apply H; auto.
- eapply H0; eauto.
- eapply H1; eauto.
- eapply H2; eauto.
- eapply H3; eauto.
- eapply H4; eauto.
- eapply H5; eauto.
- eapply H6; eauto.
- eapply H7; eauto.
- eapply H8; eauto.
- eapply H9; eauto.
- eapply H10; eauto.
revert H13; apply LazyStep_mon; constructor; auto.
- eapply H11; eauto.
Qed.
(** * Correspondence between the two semantics *)
(** Auxiliary evaluation functions on syntactic values and heaps. *)
(** Evaluation of a syntactic value [t] in environment [e],
to a (semantic) value of type [toType u]. *)
Definition evalVl {g u} (e : env g) (t : Vl g u) : toType u :=
match t with
| VLam t => fun (x : T (toType _)) => eval t (e, x)
| VPair x y => (lookup x e, lookup y e)
| VNil => NilA
| VCons x1 x2 => ConsA (lookup x1 e) (lookup x2 e)
| VBas b => b
end.
(** Evaluation of heap [e] to an environment. *)
Fixpoint evalHeap {g} (e : Heap g) : env g :=
match e with
| HNil => tt
| HCons e1 v => (evalHeap e1, mapT (evalVl (evalHeap e1)) v)
end.
(** Rename an environment. Environment are (heterogeneous) lists,
which are mappings from indices to values, whereas renamings
are mappings between indices, hence they act contravariantly
on environments. *)
Fixpoint renameEnv {g g' : Ctx} : Rnm g g' -> env g' -> env g :=
match g with
| NilCtx => fun _ _ => tt
| ConsCtx g a => fun s e => (renameEnv (restr s) e, lookup (s _ Here) e)
end.
(** Proofs (soundness, then adequacy). *)
(** The main theorems are [soundness], [adequacy], and
[soundness_and_adequacy], which is a trivial combination of the two.
A big intermediate lemma is [BigStep_heap_increasing], which says
that the heap only ever grows by adding new mappings (never by
mutating old ones).
The other lemmas are mostly small equations,
"commutative diagrams" equating different ways to obtain the same
result using the above functions. We mostly come up with them
as needed when trying to prove the big theorems mentioned just
above.
*)
(** [T] is also a functor on relations. *)
Inductive eq_T {a b} (r : a -> b -> Prop) : T a -> T b -> Prop :=
| eq_T_Discarded : eq_T r Undefined Undefined
| eq_T_Thunk x y : r x y -> eq_T r (Thunk x) (Thunk y)
.
Ltac lucky_forward1 :=
lazymatch goal with
| [ H : optimistic (?t _) _ |- optimistic (?t _) _ ] => eapply optimistic_mon with (1 := H); intros ? ? [<- ->]
| [ H : forall _ _, optimistic (?t _) _ |- optimistic (?t _) _ ] => eapply optimistic_mon with (1 := H _ _); intros ? ? []
end.
Ltac lucky_forward :=
repeat (mforward idtac + lucky_forward1).
(** Eta rule for heaps. *)
Lemma eta_Heap g a (e : Heap (g :,: a))
: e = HCons (there e) (here e).
Proof.
refine
match e in Heap g' return
match g' with (_ :,: _) => fun e => _ | NilCtx => fun _ => True end e
with
| HNil => I
| HCons _ _ => _
end. reflexivity.
Qed.
(** Elimination principle for variables. *)
Lemma V_split {g a} (P : forall b, V (g :,: a) b -> Prop)
: (forall b v, P b (There v)) -> P a Here ->
forall b (v : V (g :,: a) b), P b v.
Proof.
intros H1 H2 b v; revert P H1 H2.
refine
match v in V g b return
match g with
| NilCtx => fun _ => False
| g :,: a => fun v => forall (P : forall b, V (g :,: a) b -> Prop), _ -> _ -> P b v
end v with
| Here => _
| There v => _
end; eauto.
Qed.
(** Extensional equality is Leibniz equality (assuming functional and
propositional extensionality). *)
Lemma eq_M_eq {a} (u1 u2 : M a) : eq_M u1 u2 -> u1 = u2.
Proof.
intros H. do 2 (apply functional_extensionality; intros). apply prop_ext. auto.
Qed.
(** Indexing into a renamed environment is equivalent to
renaming the index first then indexing into the original environment. *)
Lemma lookup_renameEnv {g g'} (s : Rnm g g') (e : env g') {a} (v : V g a)
: lookup v (renameEnv s e) = lookup (s _ v) e.
Proof.
induction v; cbn.
- reflexivity.
- apply IHv.
Qed.
(** [renameEnv] commutes with composition (of renamings vs of functions). *)
Lemma renameEnv_cat {g g' g''} (s : Rnm g g') (s' : Rnm g' g'') (e : env g'')
: renameEnv (s >>> s') e = renameEnv s (renameEnv s' e).
Proof.
induction g; cbn.
- reflexivity.
- f_equal.
+ apply (IHg (fun _ v => s _ (There v))).
+ unfold cat_Rnm. symmetry; apply lookup_renameEnv.
Qed.
(** The above two lemmas make [(env, renameEnv)] a functor... *)
(** A specialized formulation of an extensionality principle environments ("two
environments are equal if they produce the same results by indexing/lookup"). *)
Lemma renameEnv_ext {g g'} (s : Rnm g g') e e'
: (forall a v, lookup (s a v) e = lookup v e') ->
renameEnv s e = e'.
Proof.
induction g; cbn; intros H.
- destruct e'; reflexivity.
- destruct e'; f_equal.
+ apply IHg. intros; apply H.
+ apply H.
Qed.
(** Renaming with the identity renaming is the identity function on environments. *)
Lemma renameEnv_id {g} (e : env g) : renameEnv id_Rnm e = e.
Proof.
apply renameEnv_ext. reflexivity.
Qed.
(** The heap only ever grows by adding new bindings.
Existing bindings persist. So if we restrict the new heap to the domain
of the old heap, then it should coincide with the old heap.
We eventually only care about the denotations of the heap,
so that's what we compare here. (We could try to prove the actual
equality on heaps, but it's just more work.) *)
Theorem BigStep_heap_increasing' : forall g u (t : Tm g u) (e : Heap g) g' (s' : Rnm g g') (e' : Heap g') (v : Vl g' u) (n : nat),
BigStep t e s' e' v n ->
evalHeap e = renameEnv s' (evalHeap e').
Proof.
intros * H; induction H; cbn [eval evalHeap renameEnv] in *; intros.
all: try solve [symmetry; apply renameEnv_id].
- apply (f_equal fst) in IHBigStep; cbn in IHBigStep.
apply IHBigStep.
- rewrite IHBigStep1.
apply (f_equal fst) in IHBigStep2. cbn in IHBigStep2. rewrite IHBigStep2.
symmetry; apply renameEnv_cat.
- rewrite IHBigStep1.
apply (f_equal fst) in IHBigStep2. cbn in IHBigStep2. rewrite IHBigStep2.
symmetry; apply renameEnv_cat.
- auto.
- apply (f_equal fst) in IHBigStep; cbn in IHBigStep.
destruct H0 as [ | ? ? ? ? ? [] ]; cbn in IHBigStep.
+ assumption.
+ rewrite H2.
rewrite renameEnv_cat. f_equal. assumption.
Qed.
(** A variant of the previous lemma composed with applications of [lookup]. *)
Theorem BigStep_heap_increasing : forall g u (t : Tm g u) (e : Heap g) g' (s' : Rnm g g') (e' : Heap g') (v : Vl g' u) (n : nat),
BigStep t e s' e' v n ->
forall a (v : _ a), lookup (s' _ v) (evalHeap e') = lookup v (evalHeap e).
Proof.
intros * W a v; apply BigStep_heap_increasing' in W.
rewrite W; auto using lookup_renameEnv.
Qed.
(** Respectfulness of [foldrA']. Helper for [eq_M_foldrA]. *)
Lemma eq_M_foldrA' {a b} (fnil fnil' : M b) (fcons fcons' : T a -> T b -> M b)
(xs : ListA a)
: eq_M fnil fnil' ->
(forall x y, eq_M (fcons x y) (fcons' x y)) ->
eq_M (foldrA' fnil fcons xs) (foldrA' fnil' fcons' xs).
Proof.
intros J1 J2; induction xs; cbn.
- apply eq_M_bind; [ apply Reflexive_eq_M | intros _; auto ].
- apply eq_M_bind; [ apply Reflexive_eq_M | intros _ ].
apply eq_M_bind; [ | auto ].
apply eq_M_thunk.
destruct xs; cbn; [ | apply Reflexive_eq_M ].
apply H.
Qed.
(** Respectfulness of [foldrA]. *)
Lemma eq_M_foldrA {a b} (fnil fnil' : M b) (fcons fcons' : T a -> T b -> M b)
(xs : T (ListA a))
: eq_M fnil fnil' ->
(forall x y, eq_M (fcons x y) (fcons' x y)) ->
eq_M (foldrA fnil fcons xs) (foldrA fnil' fcons' xs).
Proof.
intros J1 J2; destruct xs; cbn; [ apply eq_M_foldrA'; auto | apply Reflexive_eq_M ].
Qed.
(** Renaming lemma: denotations ([eval]) are invariant under renamings.
Environments must be renamed too, [e = renameEnv s e'],
but we use a more readily available phrasing of that equality. *)
Lemma evalRnm {g g'} u (t : Tm g u) (s : Rnm g g') (e : env g) (e' : env g')
: (forall a v, lookup (s a v) e' = lookup v e) ->
eq_M (eval t e) (eval (rename s t) e').
Proof.
revert g' s e'; induction t; intros g' s e' Hs; cbn.
all: try (apply eq_M_bind; [ apply Reflexive_eq_M | intros _ ]).
- apply eq_M_bind; intros.
{ apply eq_M_thunk. apply IHt1. auto. }
{ apply IHt2 with (s := (shift s)); cbn.
apply V_split; eauto. }
- apply eq_M_bind; intros.