-
Notifications
You must be signed in to change notification settings - Fork 0
/
identifiers.v
4608 lines (4301 loc) · 155 KB
/
identifiers.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
(* Library for identifiers. Part of the CertiCoq project.
* Author: Zoe Paraskevopoulou, 2016
*)
From Coq Require Import Lists.List Lists.SetoidList NArith.BinNat PArith.BinPos
MSets.MSetRBT Sets.Ensembles Omega Sorting.Permutation Logic.Decidable.
From SFS Require Import Coqlib.
From SFS Require Import cps cps_util ctx set_util Ensembles_util List_util tactics.
Import ListNotations.
Import PS.
Open Scope ctx_scope.
Definition FVSet := PS.t.
(** * Function definitions *)
(** [name_in_fundefs B] is the set of the function names in [B] *)
Fixpoint name_in_fundefs (B : fundefs) : Ensemble var :=
match B with
| Fnil => Empty_set var
| Fcons f' _ _ _ B =>
Union var (Singleton var f') (name_in_fundefs B)
end.
(** [fun_in_fundefs B] is the set of functions defined in [B] *)
Fixpoint fun_in_fundefs (B : fundefs) : Ensemble (var * fTag * list var * exp) :=
match B with
| Fnil => Empty_set _
| Fcons f tau xs e B =>
Union _ (Singleton _ (f, tau, xs, e))
(fun_in_fundefs B)
end.
(** The set of function names is decidable *)
Instance Decidable_name_in_fundefs (B : fundefs) :
Decidable (name_in_fundefs B).
Proof.
constructor.
induction B; intros x.
- destruct (peq x v); subst.
left. left. eauto.
destruct (IHB x). left. right; eauto.
right. intros Hc. inv Hc. inv H; congruence.
eauto.
- right. intros Hc; inv Hc.
Qed.
(** Lemmas about [split_fds] and [name_in_fundefs] *)
Lemma split_fds_name_in_fundefs B1 B2 B3 :
split_fds B1 B2 B3 ->
Same_set var (name_in_fundefs B3)
(Union var (name_in_fundefs B1) (name_in_fundefs B2)).
Proof with eauto with Ensembles_DB.
intros Hspl. induction Hspl; simpl; try rewrite IHHspl...
Qed.
Lemma fundefs_append_name_in_fundefs B1 B2 B3 :
fundefs_append B1 B2 = B3 ->
Same_set var (name_in_fundefs B3)
(Union var (name_in_fundefs B1) (name_in_fundefs B2)).
Proof with eauto with Ensembles_DB.
revert B3. induction B1; intros B3 Heq; simpl.
- destruct B3. simpl in Heq. inv Heq. simpl.
rewrite IHB1; eauto... simpl. inv Heq.
- inv Heq. simpl; symmetry...
Qed.
Lemma split_fds_fun_in_fundefs B1 B2 B3 :
split_fds B1 B2 B3 ->
Same_set _ (fun_in_fundefs B3)
(Union _ (fun_in_fundefs B1) (fun_in_fundefs B2)).
Proof with eauto with Ensembles_DB.
intros Hspl1. induction Hspl1; simpl; try rewrite IHHspl1...
Qed.
Lemma fundefs_append_fun_in_fundefs B1 B2 B3 :
fundefs_append B1 B2 = B3 ->
Same_set _ (fun_in_fundefs B3)
(Union _ (fun_in_fundefs B1) (fun_in_fundefs B2)).
Proof.
intros H. eapply split_fds_fun_in_fundefs.
eapply fundefs_append_split_fds; eauto.
Qed.
Lemma getlist_def_funs_Disjoint xs B B' rho :
Disjoint _ (FromList xs) (name_in_fundefs B) ->
getlist xs (def_funs B' B rho rho) = getlist xs rho.
Proof with now eauto with Ensembles_DB.
induction B; intros Hd.
- simpl.
rewrite getlist_set_neq.
erewrite IHB...
intros Hc; eapply Hd. constructor; eauto. now left.
- reflexivity.
Qed.
(** Names of function in a function definition evaluation context *)
Lemma name_in_fundefs_ctx B e1 e2 :
Same_set _ (name_in_fundefs (B <[ e1 ]>)) (name_in_fundefs (B <[ e2 ]>)).
Proof.
induction B; simpl;
(apply Same_set_Union_compat; [ now apply Same_set_refl |]).
now apply Same_set_refl.
eassumption.
Qed.
(** Alternative definition of [name_in_fundefs] using set comprehension:
[names_in_fundefs b] = $\{ f ~|~ \exists ~xs ~tau ~e,~(f, ~xs, ~tau, ~e) \in B \}$ *)
Lemma name_in_fundefs_big_cup_fun_in_fundefs B :
Same_set var (name_in_fundefs B) (big_cup (fun_in_fundefs B)
(fun p =>
let '(x, _, _, _) := p in
Singleton var x)).
Proof with eauto with Ensembles_DB.
induction B; simpl in *.
- rewrite Union_big_cup, big_cup_Singleton...
- symmetry...
Qed.
Lemma fun_in_fundefs_name_in_fundefs f tau xs e B :
fun_in_fundefs B (f, tau, xs, e) ->
name_in_fundefs B f.
Proof.
intros H. eapply name_in_fundefs_big_cup_fun_in_fundefs.
repeat eexists; eauto. constructor.
Qed.
(** ** Lemmas about [find_def] and [def_funs] *)
Lemma find_def_name_in_fundefs f B v:
find_def f B = Some v ->
name_in_fundefs B f.
Proof.
induction B; simpl; intros H; try now inv H.
destruct (M.elt_eq f v0); inv H.
left; eauto. right; eauto.
Qed.
Lemma name_not_in_fundefs_find_def_None f B:
~ name_in_fundefs B f ->
find_def f B = None.
Proof.
induction B; simpl; intros H; eauto.
destruct (M.elt_eq f v); subst.
- exfalso. apply H. now left.
- eapply IHB. intros Hc. apply H. now right.
Qed.
Lemma name_in_fundefs_find_def_is_Some f B :
Ensembles.In _ (name_in_fundefs B) f ->
exists ft xs e, find_def f B = Some (ft, xs, e).
Proof.
intros Hin. induction B.
- destruct (peq v f); simpl; subst.
+ repeat eexists; eauto.
rewrite peq_true. reflexivity.
+ inv Hin. inv H; congruence.
rewrite peq_false; eauto.
- inv Hin.
Qed.
(** [find_def] is sound w.r.t. [fun_in_fundefs] *)
Lemma find_def_correct f B tau xs e :
find_def f B = Some (tau, xs, e) ->
fun_in_fundefs B (f, tau, xs, e).
Proof.
induction B; simpl; intros H; try discriminate.
destruct (M.elt_eq f v).
- inv H. left; eauto.
- right; eauto.
Qed.
Lemma def_funs_spec x v B B' rho rho' :
M.get x (def_funs B' B rho rho') = Some v ->
(name_in_fundefs B x /\ v = cps.Vfun rho B' x) \/
(~ name_in_fundefs B x /\ M.get x rho' = Some v).
Proof.
induction B; intros Hget.
- simpl in Hget. rewrite M.gsspec in Hget. destruct (peq x v0).
+ inv Hget. left. split; eauto. constructor; eauto.
+ destruct (IHB Hget) as [[H1 H2] | [H1 H2]]; eauto.
* left. split; eauto. constructor 2; eauto.
* right. split; eauto. intros Hc. inv Hc; try (inv H; congruence); eauto.
- simpl in Hget. right. split; eauto. intros Hc; inv Hc.
Qed.
Lemma def_funs_eq x B B' rho rho' :
name_in_fundefs B x ->
M.get x (def_funs B' B rho rho') = Some (cps.Vfun rho B' x).
Proof.
induction B; intros Hin; inv Hin.
- simpl. inv H; rewrite M.gss. eauto.
- simpl. rewrite M.gsspec. destruct (peq x v); subst; eauto.
Qed.
Lemma def_funs_neq x B B' rho rho' :
~ name_in_fundefs B x ->
M.get x (def_funs B' B rho rho') = M.get x rho'.
Proof.
induction B; intros Hin; simpl; eauto.
rewrite M.gsspec. destruct (peq x v); subst; eauto.
exfalso. apply Hin. constructor; eauto. eapply IHB.
intros Hc. eapply Hin. constructor 2; eauto.
Qed.
Lemma get_fundefs y v B B' rho :
M.get y rho = Some v -> ~ name_in_fundefs B y ->
M.get y (def_funs B' B rho rho) = Some v.
Proof.
intros Hget Hn.
rewrite def_funs_neq; eauto.
Qed.
Lemma getlist_fundefs ys vs B B' rho :
getlist ys rho = Some vs ->
(forall y, List.In y ys -> ~ name_in_fundefs B y) ->
getlist ys (def_funs B' B rho rho) = Some vs.
Proof.
revert rho vs. induction ys; intros rho vs Hget Hall.
- now inv Hget.
- simpl in Hget.
destruct (M.get a rho) eqn:Heq1; try discriminate.
destruct (getlist ys rho) eqn:Heq2; try discriminate. inv Hget.
simpl. erewrite IHys; eauto. erewrite get_fundefs; eauto.
intros Hc. eapply Hall; eauto. left; eauto.
intros y Hin Hc. eapply Hall; eauto. right; eauto.
Qed.
(** Extend the environment with a block of functions and put them in the set *)
Lemma binding_in_map_def_funs B' B rho S :
binding_in_map S rho ->
binding_in_map (Union _ (name_in_fundefs B) S) (def_funs B' B rho rho).
Proof.
intros H x' Hs.
destruct (Decidable_name_in_fundefs B). destruct (Dec x').
- eexists. rewrite def_funs_eq; eauto.
- destruct Hs; try contradiction.
edestruct H; eauto.
eexists. erewrite def_funs_neq; eauto.
Qed.
Instance ToMSet_name_in_fundefs B : ToMSet (name_in_fundefs B).
Proof.
induction B.
- destruct IHB as [s1 Hseq1].
eexists (PS.add v s1).
simpl. erewrite FromSet_add, Hseq1. reflexivity.
- eexists PS.empty. rewrite FromSet_empty. reflexivity.
Qed.
(** * Free variables, inductive definitions *)
(** [occurs_free e] is the set of free variables of [e] *)
Inductive occurs_free : exp -> Ensemble var :=
| Free_Econstr1 :
forall y x t ys e,
List.In y ys ->
occurs_free (Econstr x t ys e) y
| Free_Econstr2 :
forall y x t ys e,
~ x = y ->
occurs_free e y ->
occurs_free (Econstr x t ys e) y
| Free_Ecase1 :
forall x ys,
occurs_free (Ecase x ys) x
| Free_Ecase2 :
forall y x c e ys,
occurs_free e y ->
occurs_free (Ecase x ((c, e) :: ys)) y
| Free_Ecase3 :
forall y x c e ys,
occurs_free (Ecase x ys) y ->
occurs_free (Ecase x ((c, e) :: ys)) y
| Free_Eproj1 :
forall y x tau n e,
occurs_free (Eproj x tau n y e) y
| Free_Eproj2 :
forall y x tau n y' e,
x <> y ->
occurs_free e y ->
occurs_free (Eproj x tau n y' e) y
| Free_Efun1 :
forall y defs e,
~ (name_in_fundefs defs y) ->
occurs_free e y ->
occurs_free (Efun defs e) y
| Free_Efun2 :
forall y defs e,
occurs_free_fundefs defs y ->
occurs_free (Efun defs e) y
| Free_Eapp1 :
forall x ys f,
occurs_free (Eapp x f ys) x
| Free_Eapp2 :
forall y x f ys,
List.In y ys ->
occurs_free (Eapp x f ys) y
| Free_Eprim1 :
forall y x p ys e,
List.In y ys ->
occurs_free (Eprim x p ys e) y
| Free_Eprim2 :
forall y x p ys e,
x <> y ->
occurs_free e y ->
occurs_free (Eprim x p ys e) y
| Free_Ehalt :
forall x, occurs_free (Ehalt x) x
with occurs_free_fundefs : fundefs -> Ensemble var :=
| Free_Fcons1 :
forall x f tau ys e defs,
x <> f ->
~ (List.In x ys) ->
~ (name_in_fundefs defs x) ->
occurs_free e x ->
occurs_free_fundefs (Fcons f tau ys e defs) x
| Free_Fcons2 :
forall x f tau ys e defs,
occurs_free_fundefs defs x ->
x <> f ->
occurs_free_fundefs (Fcons f tau ys e defs) x.
Hint Constructors occurs_free.
Hint Constructors occurs_free_fundefs.
(** [occurs_free_applied e] is the set of free variables of [e] that appear in applied position *)
Inductive occurs_free_applied : exp -> Ensemble var :=
| FreeApp_Econstr :
forall y x t ys e,
~ x = y ->
occurs_free_applied e y ->
occurs_free_applied (Econstr x t ys e) y
| FreeApp_Ecase1 :
forall y x c e ys,
occurs_free_applied e y ->
occurs_free_applied (Ecase x ((c, e) :: ys)) y
| FreeApp_Ecase2 :
forall y x c e ys,
occurs_free_applied (Ecase x ys) y ->
occurs_free_applied (Ecase x ((c, e) :: ys)) y
| FreeApp_Eproj :
forall y x tau n y' e,
x <> y ->
occurs_free_applied e y ->
occurs_free_applied (Eproj x tau n y' e) y
| FreeApp_Efun1 :
forall y defs e,
~ (name_in_fundefs defs y) ->
occurs_free_applied e y ->
occurs_free_applied (Efun defs e) y
| FreeApp_Efun2 :
forall y defs e,
occurs_free_applied_fundefs defs y ->
occurs_free_applied (Efun defs e) y
| FreeApp_Eapp :
forall x ys f,
occurs_free_applied (Eapp x f ys) x
| FreeApp_Eprim :
forall y x p ys e,
x <> y ->
occurs_free_applied e y ->
occurs_free_applied (Eprim x p ys e) y
with occurs_free_applied_fundefs : fundefs -> Ensemble var :=
| FreeApp_Fcons1 :
forall x f tau ys e defs,
x <> f ->
~ (List.In x ys) ->
~ (name_in_fundefs defs x) ->
occurs_free_applied e x ->
occurs_free_applied_fundefs (Fcons f tau ys e defs) x
| FreeApp_Fcons2 :
forall x f tau ys e defs,
occurs_free_applied_fundefs defs x ->
x <> f ->
occurs_free_applied_fundefs (Fcons f tau ys e defs) x.
(** sanity check : The names of the functions cannot appear free in a fundefs block *)
Lemma fun_names_not_free_in_fundefs f defs :
name_in_fundefs defs f ->
~ occurs_free_fundefs defs f.
Proof.
intros Hname Hfree.
induction Hfree; inversion Hname; eauto.
inv H3. eauto. inv H0; eauto.
Qed.
(** ** Useful set equalities *)
Lemma occurs_free_Econstr x t ys e :
Same_set var (occurs_free (Econstr x t ys e))
(Union _ (FromList ys) (Setminus var (occurs_free e) (Singleton var x))).
Proof.
split; intros x' H; inv H; eauto.
right. constructor; eauto. intros H. inv H; eauto.
inv H0. constructor 2; eauto. intros Hc. subst. eauto.
Qed.
Lemma occurs_free_Eprim x f ys e :
Same_set var (occurs_free (Eprim x f ys e))
(Union _ (FromList ys) (Setminus var (occurs_free e) (Singleton var x))).
Proof.
split; intros x' H; inv H; eauto.
right. constructor; eauto. intros H. inv H; eauto.
inv H0. eapply Free_Eprim2; eauto. intros Hc. subst. eauto.
Qed.
Lemma occurs_free_Eproj x tag n y e :
Same_set var (occurs_free (Eproj x tag n y e))
(Union _ (Singleton var y) (Setminus var (occurs_free e) (Singleton var x))).
Proof.
split; intros x' H; inv H; eauto.
right. constructor; eauto. intros H. inv H; eauto.
inv H0. eauto.
inv H0. constructor; eauto.
intros Hc. subst. eauto.
Qed.
Lemma occurs_free_Ehalt x :
Same_set var (occurs_free (Ehalt x)) (Singleton _ x).
Proof.
split; intros x' H; inv H; eauto.
Qed.
Lemma occurs_free_Eapp f ft ys :
Same_set var (occurs_free (Eapp f ft ys))
(Union _ (FromList ys) (Singleton var f)).
Proof.
split; intros x' H; inv H; eauto. inv H0; eauto.
Qed.
Lemma occurs_free_Efun B e :
Same_set var (occurs_free (Efun B e))
(Union _ (occurs_free_fundefs B)
(Setminus _ (occurs_free e) (name_in_fundefs B))).
Proof.
split; intros x' H; inv H; eauto.
right; eauto. constructor; eauto.
inv H0. eauto.
Qed.
Lemma occurs_free_Ecase_nil x :
Same_set var (occurs_free (Ecase x []))
(Singleton _ x).
Proof.
split; intros x' H; inv H; eauto.
Qed.
Lemma occurs_free_Ecase_cons x c e P :
Same_set var (occurs_free (Ecase x ((c, e) :: P)))
(Union _ (Singleton _ x)
(Union _ (occurs_free e)
(occurs_free (Ecase x P)))).
Proof.
split; intros x' H; inv H; eauto.
inv H0; eauto. inv H0; eauto.
Qed.
Lemma occurs_free_Ecase_app x c e P P' :
Same_set var (occurs_free (Ecase x (P' ++ ((c, e) :: P))))
(Union _ (Singleton _ x)
(Union _ (occurs_free (Ecase x P'))
(Union _ (occurs_free e)
(occurs_free (Ecase x P))))).
Proof with eauto with Ensembles_DB.
induction P' as [| [c' e'] P' IHP']; simpl.
- rewrite occurs_free_Ecase_nil, occurs_free_Ecase_cons...
- rewrite !occurs_free_Ecase_cons, IHP', <- !Union_assoc...
Qed.
Lemma occurs_free_fundefs_Fcons f t xs e B :
Same_set var (occurs_free_fundefs (Fcons f t xs e B))
(Union var (Setminus var (occurs_free e)
(Union var (Singleton var f)
(Union var (FromList xs)
(name_in_fundefs B))))
(Setminus var (occurs_free_fundefs B) (Singleton var f))).
Proof.
split; intros x H; inv H.
- left. constructor; eauto. intros Hin. inv Hin; eauto.
inv H. congruence. inv H; eauto.
- right. constructor; eauto. intros H. inv H. congruence.
- inv H0. constructor; eauto.
intros Hc. subst. eauto.
- inv H0. constructor 2; eauto. intros Hc; subst; eauto.
Qed.
Lemma occurs_free_fundefs_Fnil :
Same_set var (occurs_free_fundefs Fnil) (Empty_set var).
Proof.
split; intros x H; inv H.
Qed.
Ltac normalize_occurs_free :=
match goal with
| [|- context[occurs_free (Econstr _ _ _ _)]] =>
rewrite occurs_free_Econstr
| [|- context[occurs_free (Eproj _ _ _ _ _)]] =>
rewrite occurs_free_Eproj
| [|- context[occurs_free (Ecase _ [])]] =>
rewrite occurs_free_Ecase_nil
| [|- context[occurs_free (Ecase _ (_ :: _))]] =>
rewrite occurs_free_Ecase_cons
| [|- context[occurs_free (Ecase _ (_ ++ _))]] =>
rewrite occurs_free_Ecase_app
| [|- context[occurs_free (Efun _ _)]] =>
rewrite occurs_free_Efun
| [|- context[occurs_free (Eapp _ _ _)]] =>
rewrite occurs_free_Eapp
| [|- context[occurs_free (Eprim _ _ _ _)]] =>
rewrite occurs_free_Eprim
| [|- context[occurs_free_fundefs (Fcons _ _ _ _ _)]] =>
rewrite occurs_free_fundefs_Fcons
| [|- context[occurs_free_fundefs Fnil]] =>
rewrite occurs_free_fundefs_Fnil
end.
Ltac normalize_occurs_free_in_ctx :=
match goal with
| [ H : context[occurs_free (Econstr _ _ _ _)] |- _ ] =>
rewrite occurs_free_Econstr in H
| [ H : context[occurs_free (Eproj _ _ _ _ _)] |- _ ] =>
rewrite occurs_free_Eproj in H
| [ H : context[occurs_free (Ecase _ [])] |- _ ] =>
rewrite occurs_free_Ecase_nil in H
| [ H : context[occurs_free (Ecase _ (_ :: _))] |- _ ] =>
rewrite occurs_free_Ecase_cons in H
| [ H : context[occurs_free (Ecase _ (_ ++ _))] |- _ ] =>
rewrite occurs_free_Ecase_app in H
| [ H : context[occurs_free (Efun _ _)] |- _ ] =>
rewrite occurs_free_Efun in H
| [ H : context[occurs_free (Eapp _ _ _)] |- _ ] =>
rewrite occurs_free_Eapp in H
| [ H : context[occurs_free (Eprim _ _ _ _)] |- _ ] =>
rewrite occurs_free_Eprim in H
| [ H : context[occurs_free_fundefs (Fcons _ _ _ _ _)] |- _ ] =>
rewrite occurs_free_fundefs_Fcons in H
| [ H : context[occurs_free_fundefs Fnil] |- _ ] =>
rewrite occurs_free_fundefs_Fnil in H
end.
Lemma occurs_free_fundefs_name_in_fundefs_Disjoint (defs : fundefs) :
Disjoint _ (name_in_fundefs defs) (occurs_free_fundefs defs).
Proof with now eauto with Ensembles_DB.
induction defs.
- simpl. rewrite occurs_free_fundefs_Fcons.
eapply Union_Disjoint_l...
- simpl. rewrite occurs_free_fundefs_Fnil...
Qed.
(** ** Useful set inclusions *)
Lemma occurs_free_fundefs_Fcons_Included f tau xs e B :
Included var (occurs_free_fundefs B)
(Union _ (occurs_free_fundefs (Fcons f tau xs e B)) (Singleton var f)).
Proof.
intros x H. destruct (var_dec f x); subst; now eauto.
Qed.
Lemma occurs_free_Econstr_Included x t ys e :
Included var (occurs_free e)
(Union var (occurs_free (Econstr x t ys e)) (Singleton var x)).
Proof.
intros x' Hin.
destruct (var_dec x x'); subst; eauto.
Qed.
Lemma occurs_free_Eproj_Included x tau t y e :
Included var (occurs_free e)
(Union var (occurs_free (Eproj x tau t y e)) (Singleton var x)).
Proof.
intros x' Hin.
destruct (var_dec x x'); subst; eauto.
Qed.
Lemma occurs_free_Eprim_Included x f ys e :
Included var (occurs_free e)
(Union var (occurs_free (Eprim x f ys e)) (Singleton var x)).
Proof.
intros x' Hin.
destruct (var_dec x x'); subst; eauto.
Qed.
Lemma occurs_free_Efun_Included B e:
Included var (occurs_free e)
(Union var (occurs_free (Efun B e)) (name_in_fundefs B)).
Proof.
intros x' Hin.
destruct (@Dec _ _ (Decidable_name_in_fundefs B) x'); subst; eauto.
Qed.
Lemma occurs_free_Ecase_Included P c x e :
List.In (c, e) P ->
Included var (occurs_free e)
(occurs_free (Ecase x P)).
Proof.
intros Hin x' Hin'. induction P as [ | [c' e'] P IHP]; try now inv Hin.
inv Hin.
- inv H. constructor; eauto.
- eapply Free_Ecase3. eapply IHP; eauto.
Qed.
Lemma occurs_free_in_fun f tau xs e B :
fun_in_fundefs B (f, tau, xs, e) ->
Included var (occurs_free e)
(Union var (FromList xs) (Union var (name_in_fundefs B)
(occurs_free_fundefs B))).
Proof with eauto with Ensembles_DB.
induction B; intros H; inv H.
- inv H0. intros x H.
destruct (peq x f); simpl; subst; eauto.
destruct (in_dec var_dec x xs); eauto; subst.
destruct (@Dec _ _ (Decidable_name_in_fundefs B) x); eauto.
- intros x H. destruct (peq x v); subst; simpl...
edestruct (IHB H0 x) as [H'| H']; eauto.
inv H1...
Qed.
(** FV(B) = $\bigcup_{(f, xs e) \in B}(FV(e) \setminus xs \setminus names(B))$ *)
Lemma occurs_free_fundefs_big_cup B :
Same_set _ (occurs_free_fundefs B)
(big_cup (fun_in_fundefs B)
(fun p =>
(Setminus _ (let '(f, _, xs, e) := p in
(Setminus _ (occurs_free e) (FromList xs)))
(name_in_fundefs B)))).
Proof with eauto with Ensembles_DB.
induction B; simpl.
- rewrite occurs_free_fundefs_Fcons. rewrite IHB.
rewrite Union_big_cup, big_cup_Singleton, Setminus_Union.
apply Same_set_Union_compat...
rewrite <- Setminus_big_cup.
apply Same_Set_big_cup_r...
- rewrite occurs_free_fundefs_Fnil.
symmetry...
Qed.
Lemma split_fds_occurs_free_fundefs B1 B2 B3 :
split_fds B1 B2 B3 ->
Same_set var (occurs_free_fundefs B3)
(Union _ (Setminus _ (occurs_free_fundefs B1) (name_in_fundefs B2))
(Setminus _ (occurs_free_fundefs B2) (name_in_fundefs B1))).
Proof with eauto 6 with Ensembles_DB.
intros H1.
rewrite !occurs_free_fundefs_big_cup, <- !Setminus_big_cup.
eapply Same_set_trans with
(s2 := Union var
(big_cup (fun_in_fundefs B1) _)
(big_cup (fun_in_fundefs B2) _)).
- rewrite <- Union_big_cup. rewrite split_fds_fun_in_fundefs; eauto...
- eapply Same_set_Union_compat; eapply Same_Set_big_cup_r.
intros [[[f tau] xs] e]; rewrite split_fds_name_in_fundefs; eauto...
intros [[[f tau] xs] e].
rewrite split_fds_name_in_fundefs; eauto...
Qed.
Lemma Same_set_fun_in_fundefs_Same_set_occurs_free_fundefs B1 B2 :
Same_set _ (fun_in_fundefs B1) (fun_in_fundefs B2) ->
Same_set _ (occurs_free_fundefs B1) (occurs_free_fundefs B2).
Proof with eauto with Ensembles_DB.
rewrite !occurs_free_fundefs_big_cup. intros H.
apply Same_Set_big_cup; eauto.
intros [[[f tau] xs] e'].
rewrite !name_in_fundefs_big_cup_fun_in_fundefs...
Qed.
Lemma occurs_free_dec_exp :
(forall e, Decidable (occurs_free e)).
(* with occurs_free_dec_fundefs : *)
(* (forall B, Decidable (occurs_free_fundefs B)). *)
Proof.
eapply exp_mut with (P0 := fun B => Decidable (occurs_free_fundefs B)); intros.
+ constructor; intros x.
destruct (in_dec var_dec x l); eauto.
destruct (var_dec x v); subst.
* right. intros Hc. inv Hc; eauto.
* destruct (Dec x); eauto.
right. intros Hc. inv Hc; eauto.
+ constructor; intros x.
destruct (var_dec x v); subst; eauto.
right; intros Hc. inv Hc; congruence.
+ constructor; intros x.
destruct (var_dec x v); subst; eauto.
destruct H as [Dec1]. destruct H0 as [Dec2].
destruct (Dec1 x); eauto.
destruct (Dec2 x); eauto.
right. intros Hc. inv Hc; eauto.
+ constructor; intros x.
destruct (var_dec x v0); subst; eauto.
destruct (var_dec x v); subst. right. intros Hc. inv Hc; eauto.
destruct (Dec x); eauto.
right. intros Hc. inv Hc; eauto.
+ constructor; intros x. destruct (Decidable_name_in_fundefs f2).
destruct (Dec x).
* right. intros Hc. inv Hc; eauto. eapply fun_names_not_free_in_fundefs; eauto.
* destruct H0 as [Dec1]. destruct H as [Dec2].
destruct (Dec1 x); eauto.
destruct (Dec2 x); eauto.
right. intros Hc. inv Hc; eauto.
+ constructor; intros x.
destruct (in_dec var_dec x l); eauto.
destruct (var_dec x v); subst; eauto.
right. intros Hc. inv Hc; eauto.
+ constructor; intros x.
destruct (in_dec var_dec x l); eauto.
destruct (var_dec x v); subst.
* right. intros Hc. inv Hc; eauto.
* destruct (Dec x); eauto.
right. intros Hc. inv Hc; eauto.
+ constructor; intros x. destruct (var_dec x v); subst; eauto.
right. intros Hc; inv Hc. congruence.
+ constructor; intros x.
destruct (Decidable_name_in_fundefs f5).
destruct H as [Dec1]; destruct H0 as [Dec2].
destruct (var_dec x v); subst; eauto.
* right. intros Hc. inv Hc; eauto.
* destruct (Dec x); eauto.
right. intros Hc. inv Hc; eauto.
now eapply fun_names_not_free_in_fundefs; eauto.
destruct (Dec2 x); eauto.
destruct (in_dec var_dec x l). right. intros Hc. inv Hc; eauto.
destruct (Dec1 x); eauto.
right. intros Hc. inv Hc; eauto.
+ constructor; intros x. right; intros Hc; inv Hc.
Qed.
Lemma occurs_free_dec_fundefs :
forall B, Decidable (occurs_free_fundefs B).
Proof.
eapply fundefs_mut with (P := fun e => Decidable (occurs_free e)); intros.
+ constructor; intros x.
destruct (in_dec var_dec x l); eauto.
destruct (var_dec x v); subst.
* right. intros Hc. inv Hc; eauto.
* destruct (Dec x); eauto.
right. intros Hc. inv Hc; eauto.
+ constructor; intros x.
destruct (var_dec x v); subst; eauto.
right; intros Hc. inv Hc; congruence.
+ constructor; intros x.
destruct (var_dec x v); subst; eauto.
destruct H as [Dec1]. destruct H0 as [Dec2].
destruct (Dec1 x); eauto.
destruct (Dec2 x); eauto.
right. intros Hc. inv Hc; eauto.
+ constructor; intros x.
destruct (var_dec x v0); subst; eauto.
destruct (var_dec x v); subst. right. intros Hc. inv Hc; eauto.
destruct (Dec x); eauto.
right. intros Hc. inv Hc; eauto.
+ constructor; intros x. destruct (Decidable_name_in_fundefs f2).
destruct (Dec x).
* right. intros Hc. inv Hc; eauto. eapply fun_names_not_free_in_fundefs; eauto.
* destruct H0 as [Dec1]. destruct H as [Dec2].
destruct (Dec1 x); eauto.
destruct (Dec2 x); eauto.
right. intros Hc. inv Hc; eauto.
+ constructor; intros x.
destruct (in_dec var_dec x l); eauto.
destruct (var_dec x v); subst; eauto.
right. intros Hc. inv Hc; eauto.
+ constructor; intros x.
destruct (in_dec var_dec x l); eauto.
destruct (var_dec x v); subst.
* right. intros Hc. inv Hc; eauto.
* destruct (Dec x); eauto.
right. intros Hc. inv Hc; eauto.
+ constructor; intros x. destruct (var_dec x v); subst; eauto.
right. intros Hc; inv Hc. congruence.
+ constructor; intros x.
destruct (Decidable_name_in_fundefs f5).
destruct H as [Dec1]; destruct H0 as [Dec2].
destruct (var_dec x v); subst; eauto.
* right. intros Hc. inv Hc; eauto.
* destruct (Dec x); eauto.
right. intros Hc. inv Hc; eauto.
now eapply fun_names_not_free_in_fundefs; eauto.
destruct (Dec2 x); eauto.
destruct (in_dec var_dec x l). right. intros Hc. inv Hc; eauto.
destruct (Dec1 x); eauto.
right. intros Hc. inv Hc; eauto.
+ constructor; intros x. right; intros Hc; inv Hc.
Qed.
(** FV(e) is decidable *)
Instance Decidable_occurs_free e : Decidable (occurs_free e).
Proof.
now apply occurs_free_dec_exp.
Qed.
(** FV(B) is decidable *)
Instance Decidable_occurs_free_fundefs e : Decidable (occurs_free_fundefs e).
Proof.
now apply occurs_free_dec_fundefs.
Qed.
Lemma split_fds_same_occurs_free_fundefs_Same_set B1 B2 B3 B3' :
split_fds B1 B2 B3 ->
split_fds B1 B2 B3' ->
Same_set _ (occurs_free_fundefs B3) (occurs_free_fundefs B3').
Proof with eauto with Ensembles_DB.
intros Hspl1 Hspl2. rewrite !occurs_free_fundefs_big_cup.
apply Same_Set_big_cup.
- intros [[[f tau] xs] e'].
rewrite !split_fds_name_in_fundefs; [| eassumption ].
rewrite (split_fds_name_in_fundefs B1 B2 B3'); [| eassumption ]...
- rewrite !split_fds_fun_in_fundefs; [| eassumption ].
rewrite (split_fds_fun_in_fundefs B1 B2 B3'); [| eassumption ]...
Qed.
(** Compatibility with context application *)
Lemma occurs_free_ctx_mut :
(forall c e e', Same_set _ (occurs_free e) (occurs_free e') ->
Same_set _ (occurs_free (c |[ e ]|))
(occurs_free (c |[ e' ]|))) /\
(forall B e e', Same_set _ (occurs_free e) (occurs_free e') ->
Same_set _ (occurs_free_fundefs (B <[ e ]>))
(occurs_free_fundefs (B <[ e' ]>))).
Proof with eauto with Ensembles_DB.
exp_fundefs_ctx_induction IHc IHf; eauto; simpl;
intros; repeat normalize_occurs_free;
try (rewrite IHc; [| eassumption ]); try (rewrite IHB; [| eassumption ]);
eauto with Ensembles_DB.
rewrite name_in_fundefs_ctx...
rewrite name_in_fundefs_ctx...
Qed.
Corollary occurs_free_exp_ctx :
forall c e e', Same_set _ (occurs_free e) (occurs_free e') ->
Same_set _ (occurs_free (c |[ e ]|))
(occurs_free (c |[ e' ]|)).
Proof.
apply occurs_free_ctx_mut.
Qed.
Corollary occurs_free_fundefs_ctx_c :
forall B e e', Same_set _ (occurs_free e) (occurs_free e') ->
Same_set _ (occurs_free_fundefs (B <[ e ]>))
(occurs_free_fundefs (B <[ e' ]>)).
Proof.
apply occurs_free_ctx_mut.
Qed.
Lemma occurs_free_Ecase_ctx_app_mut :
(forall C x c e P,
Included _ (occurs_free (C |[ Ecase x ((c, e) :: P)]|))
(Union _ (occurs_free (C |[ e ]|)) (occurs_free (C |[ Ecase x P ]|)))) /\
(forall Cf x c e P,
Included _ (occurs_free_fundefs (Cf <[ Ecase x ((c, e) :: P)]>))
(Union _ (occurs_free_fundefs (Cf <[ e ]>)) (occurs_free_fundefs (Cf <[ Ecase x P ]>)))).
Proof with eauto with Ensembles_DB.
exp_fundefs_ctx_induction IHC IHCf; simpl; intros;
repeat normalize_occurs_free; try now eauto with Ensembles_DB;
try (now eapply Included_trans;
[ apply Included_Union_compat;
[ now apply Included_refl |
apply Included_Setminus_compat; [ now eapply IHC | now apply Included_refl ] ] |
rewrite Setminus_Union_distr; eauto with Ensembles_DB]).
- eapply Included_trans.
apply Included_Union_compat. now apply Included_refl.
apply Included_Union_compat. now apply Included_refl.
apply Included_Union_compat. now eauto. now apply Included_refl.
do 4 (apply Union_Included; eauto with Ensembles_DB).
- rewrite Union_assoc.
apply Included_Union_compat.
eapply Included_trans. now eauto.
now apply Union_Included; eauto with Ensembles_DB.
rewrite name_in_fundefs_ctx...
- eapply Included_trans.
apply Included_Union_compat; [| now apply Included_refl ].
apply Included_Setminus_compat. now eauto. now apply Included_refl.
rewrite Setminus_Union_distr...
- eapply Included_trans.
apply Included_Union_compat; [ now apply Included_refl |].
apply Included_Setminus_compat. now eauto. now apply Included_refl.
rewrite name_in_fundefs_ctx, !Setminus_Union_distr...
Qed.
Corollary occurs_free_Ecase_ctx_app C x c e P :
Included _ (occurs_free (C |[ Ecase x ((c, e) :: P)]|))
(Union _ (occurs_free (C |[ e ]|)) (occurs_free (C |[ Ecase x P ]|))).
Proof.
now apply occurs_free_Ecase_ctx_app_mut.
Qed.
Lemma occurs_free_Ecase_ctx_app_mut' :
(forall C x c e P,
Included _ (occurs_free (C |[ Ecase x ((c, e) :: P)]|))
(Union _ (occurs_free e) (occurs_free (C |[ Ecase x P ]|)))) /\
(forall Cf x c e P,
Included _ (occurs_free_fundefs (Cf <[ Ecase x ((c, e) :: P)]>))
(Union _ (occurs_free e) (occurs_free_fundefs (Cf <[ Ecase x P ]>)))).
Proof with eauto with Ensembles_DB.
exp_fundefs_ctx_induction IHC IHCf; simpl; intros;
repeat normalize_occurs_free; try now eauto with Ensembles_DB;
try (now eapply Included_trans;
[ apply Included_Union_compat;
[ now apply Included_refl |
apply Included_Setminus_compat; [ now eapply IHC | now apply Included_refl ] ] |
rewrite Setminus_Union_distr; eauto with Ensembles_DB]).
- eapply Included_trans.
apply Included_Union_compat. now apply Included_refl.
apply Included_Union_compat. now apply Included_refl.
apply Included_Union_compat. now eauto. now apply Included_refl.
do 4 (apply Union_Included; eauto with Ensembles_DB).
- rewrite Union_assoc.
apply Included_Union_compat.
eapply Included_trans. now eauto.
now apply Union_Included; eauto with Ensembles_DB.
rewrite name_in_fundefs_ctx...
- eapply Included_trans.
apply Included_Union_compat; [| now apply Included_refl ].
apply Included_Setminus_compat. now eauto. now apply Included_refl.
rewrite Setminus_Union_distr...
- eapply Included_trans.
apply Included_Union_compat; [ now apply Included_refl |].
apply Included_Setminus_compat. now eauto. now apply Included_refl.
rewrite name_in_fundefs_ctx, !Setminus_Union_distr...
Qed.
Corollary occurs_free_Ecase_ctx_app' C x c e P :
Included _ (occurs_free (C |[ Ecase x ((c, e) :: P)]|))
(Union _ (occurs_free e) (occurs_free (C |[ Ecase x P ]|))).
Proof.
now apply occurs_free_Ecase_ctx_app_mut'.
Qed.
(** ** Closed expressions *)
(** An expression is closed if it has no free variables *)
Definition closed_exp (e : exp) : Prop :=
Same_set var (occurs_free e) (Empty_set var).
Definition closed_fundefs (defs : fundefs) : Prop :=
Same_set var (occurs_free_fundefs defs) (Empty_set var).
Lemma same_split_fds_closed_fundefs B1 B2 B3 B3' :
split_fds B1 B2 B3 ->
split_fds B1 B2 B3' ->
closed_fundefs B3 -> closed_fundefs B3'.
Proof.
intros Hspl1 Hspl2 Hcl. unfold closed_fundefs in *.
rewrite split_fds_same_occurs_free_fundefs_Same_set; eauto.
Qed.
Lemma split_fds_closed_fundefs B1 B2 B3 :
split_fds B1 B2 B3 ->
closed_fundefs B1 ->
closed_fundefs B2 ->
closed_fundefs B3.
Proof with eauto with Ensembles_DB.
intros H1 H2 H3. unfold closed_fundefs in *.
rewrite split_fds_occurs_free_fundefs; eauto.
rewrite H2, H3...
Qed.
(** * Function blocks defined inside in expressions and function blocks *)
(** [funs_in_exp B e] iff [B] is a block of functions in [e] *)
Inductive funs_in_exp : fundefs -> exp -> Prop :=
| In_Econstr :
forall fs e x t ys,
funs_in_exp fs e ->
funs_in_exp fs (Econstr x t ys e)
| In_Case :
forall fs x c e P,
funs_in_exp fs e ->
List.In (c, e) P ->
funs_in_exp fs (Ecase x P)
| In_Eproj :
forall fs e x tau N ys,
funs_in_exp fs e ->
funs_in_exp fs (Eproj x tau N ys e)
| In_Fun1 :
forall fs e,
funs_in_exp fs (Efun fs e)
| In_Fun2 :
forall fs fs' e,
funs_in_exp fs e ->
funs_in_exp fs (Efun fs' e)
| In_Fun3 :
forall fs fs' e,
funs_in_fundef fs fs' ->
funs_in_exp fs (Efun fs' e)
| In_Eprim :
forall fs e x p ys,
funs_in_exp fs e ->
funs_in_exp fs (Eprim x p ys e)
with funs_in_fundef : fundefs -> fundefs -> Prop :=