-
Notifications
You must be signed in to change notification settings - Fork 0
/
closure_conversion_corresp.v
1442 lines (1260 loc) · 57.8 KB
/
closure_conversion_corresp.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
(* Correctness of the implementation of closure conversion w.r.t.
the inductive relation
*)
From SFS Require Import cps cps_util set_util identifiers ctx
Ensembles_util List_util hoare functions tactics.
From SFS Require Import closure_conversion closure_conversion_util.
From SFS Require Import Coqlib.
From Coq Require Import ZArith.Znumtheory ArithRing Relations.Relations Arith.Wf_nat
Lists.List MSets.MSets MSets.MSetRBT Numbers.BinNums
NArith.BinNat PArith.BinPos Sets.Ensembles Omega.
From ExtLib Require Import Structures.Monads Data.Monads.StateMonad.
Import ListNotations.
Import MonadNotation.
Open Scope ctx_scope.
Open Scope fun_scope.
Close Scope Z_scope.
(** * Correspondence of the relational and the computational definitions of closure conversion *)
Section CC_sound.
Variable (clo_tag : cTag).
Definition Scope_st fvmap Scope :=
[ set x | M.get x fvmap = Some BoundVar ] <--> Scope.
Definition Funs_st fvmap Scope Funs :=
[ set x | exists env, M.get x fvmap = Some (MRFun env) ] <--> (Funs \\ Scope).
Definition fenv_st fvmap fenv :=
forall x env, M.get x fvmap = Some (MRFun env) -> env = fenv x.
Definition FV_st fvmap Scope Funs FVs :=
(forall N x, M.get x fvmap = Some (FVar N) -> nthN FVs N = Some x) /\
(forall x N, nthN FVs N = Some x -> ~ x \in Scope :|: Funs -> M.get x fvmap = Some (FVar N)).
Definition CCState Scope Funs fenv FVs s :=
Scope_st s Scope /\ Funs_st s Scope Funs /\ fenv_st s fenv /\ FV_st s Scope Funs FVs.
Instance Proper_CCState1 : Proper (Same_set _ ==> eq ==> eq ==> eq ==> eq ==> iff) CCState.
Proof.
intros s1 s2 Hseq ? ? ? ? ? ? ? ? ? ? ?? ; subst. unfold CCState, Scope_st, Funs_st, FV_st.
split; intros [H1 [H2 [H3 [H4 H5]]]].
- split; [| split; [| split; [| split ]]]; eauto.
rewrite <- Hseq. eauto.
rewrite <- Hseq. eauto.
setoid_rewrite <- Hseq. eauto.
- split; [| split; [| split; [| split ]]]; eauto.
rewrite Hseq. eauto.
rewrite Hseq. eauto.
setoid_rewrite Hseq. eauto.
Qed.
Instance Proper_CCState2 Scope : Proper (Same_set _ ==> eq ==> eq ==> eq ==> iff) (CCState Scope).
Proof.
intros s1 s2 Hseq ? ? ? ? ? ? ? ? ?; subst. unfold CCState, Scope_st, Funs_st, FV_st.
split; intros [H1 [H2 [H3 [H4 H5]]]].
- split; [| split; [| split; [| split ]]]; eauto.
rewrite <- Hseq. eauto.
setoid_rewrite <- Hseq. eauto.
- split; [| split; [| split; [| split ]]]; eauto.
rewrite Hseq. eauto.
setoid_rewrite Hseq. eauto.
Qed.
Instance Proper_CCState_f_eq Scope Funs : Proper (f_eq ==> eq ==> eq ==> iff) (CCState Scope Funs).
Proof.
intros s1 s2 Hseq ? ? ? ? ? ?; subst. unfold CCState, fenv_st.
split; intros [H1 [H2 [H3 [H4 H5]]]].
- split; [| split; [| split; [| split ]]]; eauto.
intros. rewrite <- Hseq. eauto.
- split; [| split; [| split; [| split ]]]; eauto.
intros. rewrite Hseq. eauto.
Qed.
Lemma binding_in_map_in_FV Scope Funs FVs fenv s :
CCState Scope Funs fenv FVs s ->
binding_in_map (FV Scope Funs FVs) s.
Proof.
intros Hbin x Hin. inv Hin. inv H.
- destruct Hbin as [Hbin _]. eapply Hbin in H0. eauto.
- eapply Hbin in H0.
destruct H0 as [e Hget1]. eauto.
- inv H. edestruct In_nthN as [N Hn]. eapply H0.
eapply Hbin in Hn; eauto.
Qed.
Lemma CCState_Funs_setminus Scope Funs FVs fenv s x :
~ x \in Funs ->
CCState Scope (Funs \\ [set x]) fenv FVs s <->
CCState Scope Funs fenv FVs s.
Proof.
intros Hnin. rewrite <- (Included_Setminus_Disjoint Funs [set x]).
reflexivity.
eapply Disjoint_Singleton_r. eassumption.
Qed.
Lemma CCState_Funs_setminus_l Scope Funs FVs fenv s x :
~ x \in Funs ->
CCState Scope (Funs \\ [set x]) fenv FVs s ->
CCState Scope Funs fenv FVs s.
Proof.
intros. eapply CCState_Funs_setminus in H.
eapply H; eauto.
Qed.
Lemma CCState_Funs_setminus_r Scope Funs FVs fenv s x :
~ x \in Funs ->
CCState Scope Funs fenv FVs s ->
CCState Scope (Funs \\ [set x]) fenv FVs s.
Proof.
intros. eapply CCState_Funs_setminus in H.
eapply H; eauto.
Qed.
Lemma CCState_Funs_Union_Setminus_l Scope Funs FVs fenv s x :
CCState (x |: Scope) (Funs \\ [set x]) fenv FVs s ->
CCState (x |: Scope) Funs fenv FVs s.
Proof.
intros [H1 [H2 [H3 H4]]].
unfold CCState in *.
split; [| split; [| split ]]; try eassumption.
unfold Funs_st in *.
rewrite Setminus_Union in H2. rewrite Union_Same_set in H2; edb.
unfold FV_st in *.
setoid_rewrite (Union_Setminus_Included _ _ [set x]) in H4; tci; edb.
Qed.
Lemma CCState_Funs_Union_Setminus_r Scope Funs FVs fenv s x :
CCState (x |: Scope) Funs fenv FVs s ->
CCState (x |: Scope) (Funs \\ [set x]) fenv FVs s.
Proof.
intros [H1 [H2 [H3 H4]]].
unfold CCState in *.
split; [| split; [| split ]]; try eassumption.
unfold Funs_st in *.
rewrite Setminus_Union. rewrite Union_Same_set; edb.
unfold FV_st in *.
setoid_rewrite (Union_Setminus_Included _ _ [set x]) ; tci; edb.
Qed.
Lemma FV_Union_Scope1 Scope Funs FVs x :
x \in Funs -> ~ x \in Scope ->
FV Scope Funs FVs <--> FV (x |: Scope) (Funs \\ [set x]) FVs.
Proof.
intros Hin Hnin. unfold FV.
rewrite !(Union_commut [set x] Scope) at 2.
rewrite (Union_Setminus_Included _ _ [set x]); eauto with Ensembles_DB typeclass_instances.
rewrite Setminus_Union.
rewrite (Union_Same_set [set x] (Scope :|: [set x])); eauto with Ensembles_DB typeclass_instances.
rewrite <- (Setminus_Union Funs ).
rewrite (Union_Setminus_Included _ _ [set x]); eauto with Ensembles_DB typeclass_instances.
rewrite <- (Union_assoc Scope [set x] Funs).
rewrite (Union_Same_set [set x] Funs); eauto with Ensembles_DB typeclass_instances.
rewrite (Union_commut [set x]). rewrite <- (Union_assoc _ [set x] (Funs \\ Scope)).
rewrite (Union_Same_set [set x] (Funs \\ Scope)); eauto with Ensembles_DB typeclass_instances.
Qed.
Lemma FV_Union_Scope2 Scope Funs FVs x :
x \in FromList FVs -> ~ x \in Scope :|: Funs ->
FV Scope Funs FVs <--> FV (x |: Scope) Funs FVs.
Proof.
intros Hin Hnin. unfold FV.
rewrite !(Union_commut [set x] Scope) at 2.
rewrite <- (Union_assoc Scope [set x] Funs).
rewrite !(Union_commut [set x] Funs) at 1.
rewrite (Union_assoc Scope Funs [set x]).
rewrite <- (Setminus_Union (FromList FVs) (Scope :|: Funs)).
rewrite (Union_Setminus_Included _ _ [set x]);
eauto with Ensembles_DB typeclass_instances.
rewrite <- (Setminus_Union Funs ).
rewrite (Union_Setminus_Included _ _ [set x]); eauto with Ensembles_DB typeclass_instances.
rewrite <- !(Union_assoc [set x]).
rewrite (Union_Same_set [set x] _). reflexivity.
eapply Singleton_Included. right.
constructor; eauto.
Qed.
Lemma FV_Union1_l Scope Funs FVs x :
x |: FV Scope Funs FVs \subset FV (x |: Scope) Funs FVs.
Proof.
unfold FV. eapply Union_Included.
now eauto with Ensembles_DB.
rewrite !(Union_commut [set x] Scope) at 2.
rewrite <- (Union_assoc Scope [set x] Funs).
rewrite !(Union_commut [set x] Funs) at 1.
rewrite (Union_assoc Scope Funs [set x]).
rewrite <- (Setminus_Union (FromList FVs) (Scope :|: Funs)).
rewrite (Union_Setminus_Included _ _ [set x]);
eauto with Ensembles_DB typeclass_instances.
rewrite <- (Setminus_Union Funs ).
rewrite (Union_Setminus_Included _ _ [set x]); eauto with Ensembles_DB typeclass_instances.
Qed.
Lemma FV_Setminus_Union_l Scope Funs FVs S `{ToMSet S}:
S :|: FV Scope Funs FVs \subset FV (Scope \\ S) (S :|: Funs) FVs.
Proof.
unfold FV. eapply Union_Included.
now edb.
rewrite !(Union_commut (Scope \\ S)).
rewrite !(Union_Setminus_Included _ _ S); tci; edb.
rewrite <- (Union_assoc S Funs Scope).
rewrite !(Union_commut S), <- !Setminus_Union.
rewrite !(Union_Setminus_Included _ _ S); tci; edb.
eapply Union_Included; edb.
rewrite !Setminus_Union. edb.
Qed.
Lemma FV_Setminus2_l Scope Funs FVs x :
FV Scope Funs FVs \subset x |: FV Scope (Funs \\ [set x]) FVs.
Proof with now eauto with Ensembles_DB.
unfold FV.
rewrite !Setminus_Union.
rewrite !(Union_commut [set x] Scope).
rewrite <- !Setminus_Union. rewrite !Union_assoc.
rewrite (Union_Setminus_Included _ _ [set x]); eauto with Ensembles_DB typeclass_instances.
Qed.
(** Get var entry lemmas *)
Lemma get_var_entry_preserves_prop x P :
{{ fun s => P s }} get_var_entry x {{ fun _ _ s => P s }}.
Proof.
eapply pre_post_mp_l. eapply bind_triple. eapply get_triple.
intros [x1 c1 f1 e1 m1] [x2 c2 f2 e2 m2].
simpl.
destruct (Maps.PTree.get x x1); simpl; eapply return_triple;
intros ? H ?; inv H; eauto.
Qed.
Lemma get_var_entry_binding_in_map x P :
{{ fun s => binding_in_map [set x] (var_map s) /\ P s }}
get_var_entry x
{{ fun _ o s => P s /\ M.get x (var_map s) = o /\ exists v, o = Some v }}.
Proof.
eapply pre_post_mp_l. eapply bind_triple. eapply get_triple.
intros [x1 c1 f1 e1 m1] [x2 c2 f2 e2 m2].
simpl.
destruct (Maps.PTree.get x x1) eqn:Hget; simpl; eapply return_triple;
intros ? H [? ?]; inv H; split; eauto;
inv H2; edestruct H0 as [v' Hget'].
reflexivity.
simpl. split. congruence. eexists; reflexivity.
reflexivity. congruence.
Qed.
(** Set var entry lemmas *)
Lemma set_var_entry_bound x Scope Funs fenv FVs :
{{ fun s => True }}
set_var_entry x BoundVar
{{ fun s _ s' => binding_in_map [set x] (var_map s') /\
(CCState Scope Funs fenv FVs (var_map s) ->
CCState (x |: Scope) Funs fenv FVs (var_map s')) /\
next_var s = next_var s'
}}.
Proof.
eapply bind_triple. eapply get_triple.
intros [x1 c1 f1 e1 m1] [x2 c2 f2 e2 m2].
eapply pre_post_mp_l. eapply bind_triple. eapply put_triple.
intros.
eapply return_triple. intros ? ? [H1 H2]. subst. inv H1; eauto.
split; [| split ].
- simpl. eexists; eauto. inv H. rewrite M.gss; eauto.
- unfold CCState. intros (H1 & H2 & H3 & H4).
split; [| split; [| split] ].
+ unfold Scope_st in *. rewrite <- H1.
simpl. split; intros x1; unfold In; simpl.
* destruct (var_dec x1 x); subst.
intros. now left.
rewrite M.gso. intros. right. eassumption.
eassumption.
* intros H.
destruct (var_dec x1 x); subst.
rewrite M.gss; eauto.
inv H; eauto. inv H0; contradiction.
rewrite M.gso; eauto.
+ unfold Funs_st in *. rewrite Union_commut, <- Setminus_Union, <- H2. simpl. split; intros x1; unfold In; simpl.
* destruct (var_dec x1 x); subst.
intros [? H]. rewrite M.gss in H. congruence.
intros [? H]. rewrite M.gso in H; eauto.
constructor.
now eauto. intros Hc; inv Hc. congruence.
* destruct (var_dec x1 x); subst.
intros [? H]. exfalso; eauto.
intros [[env Hyp1] Hyp2]. eexists.
rewrite M.gso. eassumption. intros Hc; inv Hc. eauto.
+ unfold fenv_st in *. simpl.
intros x1 e Hget.
destruct (var_dec x1 x); subst.
rewrite M.gss in Hget. congruence.
rewrite M.gso in Hget. eauto. eassumption.
+ unfold FV_st in *. split.
* intros N x1 Hget.
destruct (var_dec x1 x); subst; simpl in *.
rewrite M.gss in Hget. congruence.
rewrite M.gso in Hget. eapply H4. eassumption. eassumption.
* intros x1 N Hn Hnin. simpl.
destruct (var_dec x1 x); subst; simpl in *.
exfalso; eauto.
rewrite M.gso; eauto. eapply H4. eassumption.
intros Hc. eapply Hnin. inv Hc; eauto.
- reflexivity.
Qed.
Lemma set_var_entry_bound' x Scope Funs fenv FVs Q :
{{ fun s => CCState Scope Funs fenv FVs (var_map s) /\ Q (next_var s) }}
set_var_entry x BoundVar
{{ fun s _ s' => CCState (x |: Scope) Funs fenv FVs (var_map s') /\ Q (next_var s') }}.
Proof.
eapply pre_post_mp_l.
eapply post_weakening; [| eapply set_var_entry_bound ].
intros s [] s' _ [Hin [Hst Heq]] [Hst' Hq].
split; eauto. now rewrite <- Heq.
Qed.
Lemma set_var_entry_fun x Scope Funs fenv FVs env :
{{ fun s => True }}
set_var_entry x (MRFun env)
{{ fun s _ s' => binding_in_map [set x] (var_map s') /\
(CCState Scope Funs fenv FVs (var_map s) ->
CCState (Scope \\ [set x]) (x |: Funs) (fenv{x ~> env}) FVs (var_map s')) /\
next_var s = next_var s'
}}.
Proof.
eapply bind_triple. eapply get_triple.
intros [x1 c1 f1 e1 m1] [x2 c2 f2 e2 m2].
eapply pre_post_mp_l. eapply bind_triple. eapply put_triple.
intros.
eapply return_triple. intros ? ? [H1 H2]. subst. inv H1; eauto.
split; [| split ].
- simpl. eexists; eauto. inv H. rewrite M.gss; eauto.
- unfold CCState. intros (H1 & H2 & H3 & H4).
split; [| split; [| split] ].
+ unfold Scope_st in *. rewrite <- H1. simpl.
split; intros x1; unfold In; simpl.
* destruct (var_dec x1 x); subst.
rewrite M.gss. congruence.
rewrite M.gso; eauto. constructor; eauto.
intros Hc; inv Hc; eauto.
* intros H.
destruct (var_dec x1 x); subst.
now inv H; exfalso; eauto.
rewrite M.gso; eauto.
inv H; eauto.
+ unfold Funs_st in *. simpl in *. split; intros x1; unfold In; simpl.
* destruct (var_dec x1 x); subst; eauto.
intros. constructor; eauto. intros Hc; inv Hc; eauto.
intros H. setoid_rewrite M.gso in H; eauto. eapply H2 in H.
inv H; eauto. constructor; eauto. intros Hc; inv Hc; eauto.
* destruct (var_dec x1 x); subst.
intros [? H]. setoid_rewrite M.gss; eauto.
intros [? H]. setoid_rewrite M.gso; eauto.
eapply H2. inv H0; try (now inv H5; eauto).
constructor; eauto. intros Hc. eapply H; constructor; eauto.
intros Hc'; inv Hc'. eauto.
+ unfold fenv_st in *. simpl.
intros x1 e Hget.
destruct (var_dec x1 x); subst.
rewrite M.gss in Hget. rewrite extend_gss. congruence.
rewrite M.gso in Hget. eauto.
rewrite extend_gso; eauto. eassumption.
+ unfold FV_st in *. split.
* intros N x1 Hget.
destruct (var_dec x1 x); subst; simpl in *.
rewrite M.gss in Hget. congruence.
rewrite M.gso in Hget. eapply H4. eassumption. eassumption.
* intros x1 N Hn Hnin. simpl.
destruct (var_dec x1 x); subst; simpl in *.
exfalso; eauto.
rewrite M.gso; eauto. eapply H4. eassumption.
intros Hc. eapply Hnin. inv Hc.
constructor; eauto. now constructor; eauto.
right. now right.
- reflexivity.
Qed.
Lemma set_var_entry_fv x Scope Funs fenv FVs N :
~ x \in Scope :|: Funs ->
N.of_nat (length FVs) = N ->
~ x \in FromList FVs ->
{{ fun s => True }}
set_var_entry x (FVar N)
{{ fun s _ s' => binding_in_map [set x] (var_map s') /\
(CCState Scope Funs fenv FVs (var_map s) ->
CCState Scope Funs fenv (@app positive FVs [ x ]) (var_map s')) /\
next_var s = next_var s'
}}.
Proof.
intros Hnin Hlen Hnin'.
eapply bind_triple. eapply get_triple.
intros [x1 c1 f1 e1 m1] [x2 c2 f2 e2 m2].
eapply pre_post_mp_l. eapply bind_triple. now eapply put_triple.
intros.
eapply return_triple. intros s1 Heq [H1 H2]. subst. inv H1; eauto.
split; [| split ].
- simpl. eexists; eauto. inv H. rewrite M.gss; eauto.
- unfold CCState. intros (H1 & H2 & H3 & H4).
split; [| split; [| split] ].
+ unfold Scope_st in *. rewrite <- H1. simpl.
split; intros x1; unfold In; simpl.
* destruct (var_dec x1 x); subst.
rewrite M.gss. congruence.
rewrite M.gso; eauto.
* intros H. eapply H1 in H.
destruct (var_dec x1 x); subst.
now exfalso; eauto.
rewrite M.gso; eauto. eapply H1; eauto.
+ unfold Funs_st in *. simpl in *. split; intros x1; unfold In; simpl.
* destruct (var_dec x1 x); subst; eauto.
intros H. rewrite M.gss in H. destruct H; congruence.
intros H. rewrite M.gso in H; eauto. eapply H2. eassumption.
* destruct (var_dec x1 x); subst.
intros Hin. inv Hin. now exfalso; eauto.
intros Hin. setoid_rewrite M.gso; eauto.
eapply H2; eauto.
+ unfold fenv_st in *. simpl.
intros x1 e Hget.
destruct (var_dec x1 x); subst.
rewrite M.gss in Hget. congruence.
rewrite M.gso in Hget. eauto. eassumption.
+ unfold FV_st in *. simpl in *. split.
* intros N x1 Hget.
destruct (var_dec x1 x); subst; simpl in *.
rewrite M.gss in Hget. inv Hget.
rewrite nthN_app_geq; eauto. rewrite N.sub_diag.
reflexivity. reflexivity.
rewrite M.gso in Hget; eauto. eapply H4 in Hget.
eapply nthN_is_Some_app. eassumption.
* intros x1 N Hn Hnin1. simpl.
assert (Hor := @nthN_app positive FVs [x] N).
destruct (var_dec x1 x); subst; simpl.
rewrite M.gss. destruct Hor as [H | [H H']].
rewrite H in Hn.
eapply nthN_In in Hn. now exfalso; eauto.
rewrite H in Hn. eapply nthN_is_Some_length in Hn.
simpl in Hn. eapply N.lt_1_r in Hn. f_equal. f_equal.
eapply N.sub_0_le in Hn. eapply N_as_OT.le_antisymm; eassumption.
rewrite M.gso; eauto. eapply H4; eauto.
destruct Hor as [H | [H H']]. congruence.
rewrite Hn in H. eapply nthN_is_Some_length in Hn.
simpl in H.
destruct (N - N.of_nat (@length positive FVs))%N eqn:Hneq; try congruence.
- reflexivity.
Qed.
(** Get var spec *)
Lemma get_var_sound S x c Γ Scope Funs fenv FVs :
x \in FV Scope Funs FVs ->
exists Scope' Funs',
FV Scope Funs FVs <--> FV Scope' Funs' FVs /\
{{ fun s : state_contents =>
CCState Scope Funs fenv FVs (var_map s) /\
fresh S (next_var s)
}}
get_var clo_tag x c Γ
{{ fun s C s' =>
project_var clo_tag Scope Funs fenv c Γ FVs x C Scope' Funs' /\
CCState Scope' Funs' fenv FVs (var_map s') /\
fresh S (next_var s')
}}.
Proof with (eauto with Ensembles_DB).
intros Hin. inv Hin. inv H.
- (* Scope *)
eexists Scope, Funs. split. reflexivity.
eapply pre_strenghtening
with (P := fun s => binding_in_map [set x] (var_map s) /\
M.get x (var_map s) = Some BoundVar /\
CCState Scope Funs fenv FVs (var_map s) /\
fresh S (next_var s)).
+ intros s [Hs1 Hs2]. split; eauto. eapply binding_in_map_antimon; [| eapply binding_in_map_in_FV; eauto ].
eapply Singleton_Included. left. now left.
split; eauto.
eapply Hs1. eassumption.
+ unfold get_var. eapply bind_triple.
eapply get_var_entry_binding_in_map.
intros v [x2 c2 f2 e2 m2]. eapply pre_post_mp_l.
destruct v as [ [ | | ] | ].
* eapply bind_triple. eapply (set_var_entry_bound x Scope Funs fenv FVs).
intros r i. eapply return_triple.
intros s'. intros [Hbin [Hcc Hv]] [[Hcc' Hf] [v Hb]].
congruence.
* eapply bind_triple. eapply (set_var_entry_bound x Scope Funs fenv FVs).
intros r i. eapply return_triple.
intros s'. intros [Hbin [Hcc Hv]] [[Hcc' Hf] [v' Hb]].
congruence.
* eapply return_triple.
intros s'. intros _ [[Hget [Hcc Hf]] [Hcc' Hf']].
split; eauto. constructor. eassumption.
* eapply return_triple.
intros s'. intros _ [[Hg1 _] [Hg2 _]].
congruence.
- (* Funs *)
eexists (x |: Scope), (Funs \\ [set x]). split.
inv H0. eapply FV_Union_Scope1; now eauto.
eapply pre_strenghtening
with (P := fun s => binding_in_map [set x] (var_map s) /\
M.get x (var_map s) = Some (MRFun (fenv x)) /\
CCState Scope Funs fenv FVs (var_map s) /\
fresh S (next_var s)).
+ intros s [Hs1 Hs2]. split; eauto.
eapply binding_in_map_antimon; [| eapply binding_in_map_in_FV; eauto ].
eapply Singleton_Included. left. now right.
split; eauto.
edestruct Hs1 as [Hss [Hhf [Hen Hfv]]].
eapply Hhf in H0. edestruct H0 as [e' Hgetf].
assert (Hgetf' := Hgetf). eapply Hen in Hgetf. congruence.
+ unfold get_var. eapply bind_triple.
eapply get_var_entry_binding_in_map.
intros v [x2 c2 f2 e2 m2]. eapply pre_post_mp_l.
destruct v as [ [ | | ] | ].
* eapply bind_triple. eapply (set_var_entry_bound x Scope Funs fenv FVs).
intros r i. eapply return_triple.
intros s'. intros [Hbin [Hcc Hv]] [[Hget [Hcc' Hf]] [v Hb]].
congruence.
* eapply bind_triple. eapply (set_var_entry_bound x Scope Funs fenv FVs).
intros r i. eapply return_triple.
intros s'. intros [Hbin [Hcc Hv]] [[Hgetd [Hcc' Hf]] [v' Hb]].
split; eauto.
repeat subst_exp. inv H0. now econstructor; eauto.
repeat subst_exp.
split; eauto. eapply CCState_Funs_Union_Setminus_r. now eauto.
rewrite <- Hv. eassumption.
* eapply return_triple.
intros s'. intros _ [[Hget [Hcc Hf]] [Hcc' Hf']].
congruence.
* eapply return_triple.
intros s'. intros _ [[Hg1 _] [Hg2 _]].
congruence.
- (* FVs *)
inv H.
edestruct (@In_nthN var) as [N Hnth]. eassumption.
eexists (x |: Scope), Funs.
split. apply FV_Union_Scope2; now eauto.
eapply pre_strenghtening
with (P := fun s => binding_in_map [set x] (var_map s) /\
M.get x (var_map s) = Some (FVar N) /\
CCState Scope Funs fenv FVs (var_map s) /\
fresh S (next_var s)).
+ intros s [Hs1 Hs2]. split; eauto.
eapply binding_in_map_antimon; [| eapply binding_in_map_in_FV; eauto ].
eapply Singleton_Included. now right.
split; eauto. eapply Hs1; eauto.
+ unfold get_var. eapply bind_triple.
eapply get_var_entry_binding_in_map.
intros v [x2 c2 f2 e2 m2]. eapply pre_post_mp_l.
destruct v as [ [ | | ] | ].
* eapply bind_triple. eapply (set_var_entry_bound x Scope Funs fenv FVs).
intros r i. eapply return_triple.
intros s'. intros [Hbin [Hcc Hv]] [[Hget [Hcc' Hf]] [v Hb]].
repeat subst_exp.
split; eauto. repeat subst_exp.
now econstructor; eauto.
split; eauto.
rewrite <- Hv. eassumption.
* eapply bind_triple. eapply (set_var_entry_bound x Scope Funs fenv FVs).
intros r i. eapply return_triple.
intros s'. intros [Hbin [Hcc Hv]] [[Hgetd [Hcc' Hf]] [v' Hb]]. congruence.
* eapply return_triple.
intros s'. intros _ [[Hget [Hcc Hf]] [Hcc' Hf']].
congruence.
* eapply return_triple.
intros s'. intros _ [[Hg1 _] [Hg2 _]].
congruence.
Qed.
(** Get_vars spec *)
Lemma get_vars_sound S xs c Γ Scope Funs fenv FVs :
FromList xs \subset FV Scope Funs FVs ->
exists Scope' Funs',
FV Scope Funs FVs <--> FV Scope' Funs' FVs /\
{{ fun s : state_contents =>
CCState Scope Funs fenv FVs (var_map s) /\
fresh S (next_var s)
}}
get_vars clo_tag xs c Γ
{{ fun s C s' =>
project_vars clo_tag Scope Funs fenv c Γ FVs xs C Scope' Funs' /\
CCState Scope' Funs' fenv FVs (var_map s') /\
fresh S (next_var s')
}}.
Proof with (eauto with Ensembles_DB).
revert Scope Funs. induction xs as [| x xs IHxs ]; intros Scope Funs; normalize_sets; intros Hin.
- eexists Scope, Funs. split. reflexivity.
eapply return_triple. intros s [H1 H2]; split; eauto.
constructor.
- assert (Hin' := Hin).
eapply Union_Included_l in Hin.
eapply Union_Included_r in Hin'.
edestruct get_var_sound as [Scope1 [Funs1 [Heq Hcomp']]]. eapply Hin. reflexivity.
edestruct (IHxs Scope1 Funs1) as [Scope2 [Funs2 [Heq' Hcomp]]].
rewrite <- Heq. eassumption.
eexists Scope2, Funs2.
split. rewrite Heq, Heq'. reflexivity.
unfold get_vars. eapply bind_triple.
eassumption.
intros C1 s1. eapply bind_triple.
eapply pre_strenghtening with (P := fun s' : state_contents =>
project_var clo_tag Scope Funs fenv c Γ FVs x C1 Scope1 Funs1 /\
CCState Scope1 Funs1 fenv FVs (var_map s') /\
fresh S (next_var s')).
clear. now firstorder.
eapply frame_rule. eassumption.
intros C2 s2. eapply return_triple.
intros s'. intros [H1 [H2 [H3 H4]]]. split; eauto.
econstructor; eassumption.
Qed.
Opaque bind ret.
(** Peak push pop var_map specs *)
Lemma peak_var_map_spec P Q :
{{ fun s => P (var_map s) /\ Q (next_var s) }} peak_var_map tt {{ fun _ vm s' => P vm /\ P (var_map s') /\ Q (next_var s')}}.
Proof.
unfold peak_var_map. eapply pre_post_mp_l.
eapply bind_triple.
- eapply get_triple.
- intros [vmap ? ? ? ? ?] s. eapply return_triple.
intros s'. intros [H1 H2] [H3 H4].
subst. split; eauto.
Qed.
Lemma push_var_map_spec (P : VarInfoMap -> Prop) Q vm :
P vm ->
{{ fun s => Q (next_var s) }} push_var_map vm {{ fun _ _ s' => P (var_map s') /\ Q (next_var s') }}.
Proof.
unfold push_var_map. intros HP. eapply pre_post_mp_l.
eapply bind_triple.
- eapply get_triple.
- intros [vmap ? ? ? ? ?] s.
eapply pre_post_mp_l.
eapply bind_triple.
+ eapply put_triple.
+ intros [] s'. eapply return_triple.
intros s'' Heq [Heq' Heq'']. subst. split; eassumption.
Qed.
Lemma pop_var_map_spec P (P' : VarInfoMap -> Prop) Q :
P' (M.empty _) ->
{{ fun s => P (var_map s) /\ Q (next_var s) }} pop_var_map tt {{ fun _ vm s' => P vm /\ P' (var_map s') /\ Q (next_var s') }}.
Proof.
intros Hin. unfold peak_var_map. eapply pre_post_mp_l.
eapply bind_triple.
- eapply get_triple.
- intros [vmap ? ? ? ? ?] s. eapply pre_curry_l. intros Heq; subst.
eapply pre_post_mp_l. eapply bind_triple.
eapply put_triple.
intros x s'. eapply return_triple. intros s'' Heq1 Heq2 [H1 H2].
subst. split; eauto.
Qed.
(** VarInfoMap properties *)
Lemma var_map_emtpy :
CCState (Empty_set _) (Empty_set _) id [] (M.empty _).
Proof.
split; [| split; [| split; [| split ]]].
- unfold Scope_st.
split; intros x; try (now intros []). unfold In.
rewrite M.gempty. congruence.
- unfold Funs_st.
split; intros x; try (now intros []). unfold In.
rewrite M.gempty. intros [? ?]; congruence.
- unfold fenv_st.
setoid_rewrite M.gempty. congruence.
- setoid_rewrite M.gempty. congruence.
- intros x N Hnin. inv Hnin.
Qed.
Lemma add_params_spec Scope Funs fenv FVs xs P :
{{ fun s => CCState Scope Funs fenv FVs (var_map s) /\ P (next_var s) }}
add_params xs
{{ fun _ _ s' => CCState (FromList xs :|: Scope) Funs fenv FVs (var_map s') /\
P (next_var s') }}.
Proof.
revert Scope; induction xs; intros Scope.
- simpl. eapply return_triple. intros s. normalize_sets.
rewrite Union_Empty_set_neut_l. eauto.
- eapply bind_triple.
+ eapply pre_transfer_r.
eapply pre_strenghtening_true.
eapply set_var_entry_bound.
+ intros [] s1.
eapply pre_strenghtening with
(P0 := fun i => CCState (a |: Scope) Funs fenv FVs (var_map i) /\ P (next_var i)).
{ intros s2. intros [[H1 H1'] [H2 [H3 H4]]]. split; eauto.
now rewrite <- H4. }
eapply post_weakening; [| eapply IHxs ].
intros s3 x s4. normalize_sets. rewrite Union_assoc, (Union_commut _ [set a]).
now firstorder.
Qed.
Lemma add_fvs_spec Scope Funs fenv FVs fvs N P :
N.of_nat (length FVs) = N ->
NoDup fvs ->
Disjoint _ (FromList FVs) (FromList fvs) ->
Disjoint _ (FromList FVs :|: FromList fvs) (Scope :|: Funs) ->
{{ fun s => CCState Scope Funs fenv FVs (var_map s) /\ P (next_var s) }}
add_fvs fvs N
{{ fun _ _ s' => CCState Scope Funs fenv (FVs ++ fvs) (var_map s') /\
P (next_var s') }}.
Proof with now eauto with Ensembles_DB functions_BD.
revert N FVs. induction fvs as [ | v fvs Hfvs ]; intros N FVs Hleq Hnd Hd1 Hd2.
- simpl. eapply return_triple. intros s. normalize_sets.
rewrite app_nil_r. eauto.
- eapply bind_triple.
+ eapply pre_transfer_r.
eapply pre_strenghtening_true.
eapply (set_var_entry_fv v Scope Funs); try eassumption.
intros Hc. eapply Hd2. constructor; [| eassumption ].
normalize_sets...
intros Hc. eapply Hd1. constructor; [ eassumption | ].
normalize_sets...
+ intros [] s1.
eapply pre_strenghtening with
(P0 := fun i => CCState Scope Funs fenv (@app positive FVs (@cons var v (@nil var))) (var_map i) /\ P (next_var i)).
{ intros s2. intros [[H1 H1'] [H2 [H3 H4]]]. split; eauto. now rewrite <- H4. }
eapply post_weakening; [| eapply Hfvs ].
intros s3 x s4. normalize_sets. intros _. rewrite app_assoc_reverse; eauto.
simpl.
rewrite app_length. simpl. zify. omega.
inv Hnd; eauto.
inv Hnd. rewrite FromList_app. normalize_sets.
eapply Union_Disjoint_l. eapply Disjoint_Included_r; [| eassumption ].
normalize_sets...
normalize_sets. rewrite Union_Empty_set_neut_r. eapply Disjoint_Singleton_l.
eassumption.
rewrite FromList_app. repeat normalize_sets.
eapply Disjoint_Included_l; [| eassumption ]...
Qed.
Lemma add_funs_spec Scope Funs fenv FVs B Γ P :
{{ fun s => CCState Scope Funs fenv FVs (var_map s) /\ P (next_var s) }}
add_funs B Γ
{{ fun _ _ s' => CCState (Scope \\ name_in_fundefs B) (name_in_fundefs B :|: Funs) (extend_fundefs' fenv B Γ) FVs (var_map s') /\
P (next_var s') }}.
Proof.
revert Scope Funs fenv; induction B; intros Scope Funs fenv.
- eapply bind_triple.
+ eapply pre_transfer_r.
eapply pre_strenghtening_true.
eapply set_var_entry_fun.
+ intros [] s1.
eapply pre_strenghtening with
(P0 := fun i => CCState (Scope \\ [set v]) (v |: Funs) (fenv {v ~> Γ}) FVs (var_map i) /\ P (next_var i)).
{ intros s2. intros [[H1 H1'] [H2 [H3 H4]]]. split; eauto.
now rewrite <- H4. }
eapply post_weakening; [| eapply IHB ].
intros s3 x s4. simpl. intros _. rewrite Setminus_Union.
rewrite (Union_commut [set v] (name_in_fundefs B)) at 2.
rewrite <- !Union_assoc.
assert (Hfeq : f_eq (extend_fundefs' (fenv {v ~> Γ}) B Γ) (extend_fundefs' fenv (Fcons v f l e B) Γ)).
{ intros x1.
destruct (Decidable_name_in_fundefs B). destruct (Dec x1).
rewrite !extend_fundefs'_get_s; eauto.
now right.
rewrite extend_fundefs'_get_o; eauto.
destruct (peq x1 v); subst.
rewrite extend_fundefs'_get_s; eauto. rewrite extend_gss. reflexivity.
now left.
rewrite extend_fundefs'_get_o; eauto.
rewrite extend_gso; eauto. intros Hc; inv Hc; eauto. inv H; eauto. }
rewrite <- Hfeq. eauto.
- simpl. eapply return_triple. intros s. normalize_sets.
rewrite Setminus_Empty_set_neut_r.
assert (Hfeq : f_eq (extend_fundefs' fenv Fnil Γ) fenv).
{ intros x1. rewrite extend_fundefs'_get_o; eauto. intros Hc; inv Hc. }
rewrite Hfeq. eauto.
Qed.
(** Spec for [get_name_entry] *)
Lemma get_name_entry_preserves_prop x P Q :
{{ fun s => Q (var_map s) /\ P (next_var s) }} get_name_entry x {{ fun _ _ s => Q (var_map s) /\ P (next_var s) }}.
Proof.
eapply pre_post_mp_l. eapply bind_triple. eapply get_triple.
intros [x1 c1 f1 e1 m1] [x2 c2 f2 e2 m2].
destruct (Maps.PTree.get x name_env);
eapply return_triple; intros ? H ?; inv H; eauto.
Qed.
(** Spec for [set_name_entry] *)
Lemma set_name_entry_preserves_prop x s P Q :
{{ fun s => Q (var_map s) /\ P (next_var s) }} set_name_entry x s
{{ fun _ _ s => Q (var_map s) /\ P (next_var s) }}.
Proof.
eapply pre_post_mp_l. eapply bind_triple. eapply get_triple.
intros [x1 c1 f1 e1 m1] [x2 c2 f2 e2 m2].
eapply pre_post_mp_l. eapply bind_triple.
eapply put_triple. intros.
eapply return_triple. intros ? ? [H1 H2]. inv H1; eauto.
Qed.
(** Specs for adding names *)
Lemma add_name_preserves_prop x y P Q :
{{ fun s => Q (var_map s) /\ P (next_var s) }}
add_name x y
{{ fun _ _ s => Q (var_map s) /\ P (next_var s) }}.
Proof.
eapply set_name_entry_preserves_prop.
Qed.
Lemma add_name_suff_preserves_prop x y s P Q :
{{ fun s => Q (var_map s) /\ P (next_var s) }}
add_name_suff x y s
{{ fun _ _ s => Q (var_map s) /\ P (next_var s) }}.
Proof.
eapply bind_triple. now apply get_name_entry_preserves_prop.
intros n s1. destruct n; now apply set_name_entry_preserves_prop.
Qed.
Lemma make_record_cTag_preserves_prop n P Q :
{{ fun s => Q (var_map s) /\ P (next_var s) }} make_record_cTag n {{ fun _ _ s => Q (var_map s) /\ P (next_var s) }}.
Proof.
eapply pre_post_mp_l. eapply bind_triple. eapply get_triple.
intros [x1 c1 f1 e1 m1] [x2 c2 f2 e2 m2].
apply pre_post_mp_l. eapply bind_triple. now apply put_triple.
intros x s. eapply return_triple. intros s'. intros <-. intros [H1 H2]; subst. inv H1.
eauto.
Qed.
Opaque put get.
(** Get_name spec *)
Lemma get_name_spec Q S x str :
{{ fun s => Q (var_map s) /\ fresh S (next_var s) }}
get_name x str
{{ fun _ y s' => y \in S /\ Q (var_map s') /\ fresh (S \\ [set y]) (next_var s') }}.
Proof.
eapply bind_triple.
+ eapply pre_transfer_r. eapply pre_strenghtening_true. now eapply get_triple.
+ intros [x1 x2 x3 x4 x5 x6] [y1 y2 y3 y4 y5 y6]. simpl.
eapply pre_curry_l. intros [HQ Hf]. eapply pre_curry_l. intros H. inv H.
eapply pre_post_mp_l. eapply bind_triple. now eapply put_triple.
intros [] s.
eapply pre_strenghtening with (P := fun s => var_map s = y1 /\ next_var s = (y2 + 1)%positive).
intros s' Heq; subst. now firstorder.
eapply bind_triple.
eapply add_name_suff_preserves_prop with (Q := fun s => s = y1) (P := fun s => s= (y2 + 1)%positive).
intros. eapply return_triple. intros s2 [H1 H2] H3; eauto. subst.
split; eauto.
eapply Hf. reflexivity. split; eauto.
rewrite H2. intros z Heq. constructor; eauto.
eapply Hf. zify; omega.
intros Hc. inv Hc. zify; omega.
Qed.
(** Get_name_n0_suff spec *)
Lemma get_name_no_suff_spec Q S str :
{{ fun s => Q (var_map s) /\ fresh S (next_var s) }}
get_name_no_suff str
{{ fun _ y s' => y \in S /\ Q (var_map s') /\ fresh (S \\ [set y]) (next_var s') }}.
Proof.
eapply bind_triple.
+ eapply pre_transfer_r. eapply pre_strenghtening_true. now eapply get_triple.
+ intros [x1 x2 x3 x4 x5 x6] [y1 y2 y3 y4 y5 y6]. simpl.
eapply pre_curry_l. intros [HQ Hf]. eapply pre_curry_l. intros H. inv H.
eapply pre_post_mp_l. eapply bind_triple. now eapply put_triple.
intros [] s.
eapply pre_strenghtening with (P := fun s => var_map s = y1 /\ next_var s = (y2 + 1)%positive).
intros s' Heq; subst. now firstorder.
eapply bind_triple.
eapply add_name_preserves_prop with (Q := fun s => s = y1) (P := fun s => s= (y2 + 1)%positive).
intros. eapply return_triple. intros s2 [H1 H2] H3; eauto. subst.
split; eauto.
eapply Hf. reflexivity. split; eauto.
rewrite H2. intros z Heq. constructor; eauto.
eapply Hf. zify; omega.
intros Hc. inv Hc. zify; omega.
Qed.
Lemma make_env_spec S Scope Funs fenv FVs fvs c Γ Γ' :
FromList fvs \subset FV Scope Funs FVs ->
exists Scope' Funs',
FV Scope Funs FVs <--> FV Scope' Funs' FVs /\
{{ fun s => CCState Scope Funs fenv FVs (var_map s) /\ fresh S (next_var s) }}
make_env clo_tag fvs c Γ' Γ
{{ fun _ cC s' =>
let '(c', C) := cC in
(exists C', comp_ctx_f C' (Econstr_c Γ' c' fvs Hole_c) = C /\
project_vars clo_tag Scope Funs fenv c Γ FVs fvs C' Scope' Funs') /\
CCState Scope' Funs' fenv FVs (var_map s') /\
fresh S (next_var s')
}}.
Proof.
intros Hsub.
edestruct get_vars_sound as [Scope' [Funs' [Hfveq Hc]]].
- eassumption.
- do 2 eexists. split. eassumption.
eapply bind_triple. eassumption.
intros C1 s1.
eapply pre_curry_l. intros Hvars.
eapply bind_triple.
eapply make_record_cTag_preserves_prop.
intros c' s'. eapply return_triple.
intros s'' [Hcc Hf]. split; eauto.
Qed.