-
Notifications
You must be signed in to change notification settings - Fork 0
/
heap_defs.v
3001 lines (2645 loc) · 103 KB
/
heap_defs.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
(* Heap definitions for L6 intermediate language. Part of the CertiCoq project.
* Author: Zoe Paraskevopoulou, 2016
*)
From Coq Require Import NArith.BinNat Relations.Relations MSets.MSets
MSets.MSetRBT Lists.List omega.Omega Sets.Ensembles Relations.Relations
Classes.Morphisms.
From SFS Require Import cps cps_util List_util Ensembles_util functions
identifiers tactics set_util map_util.
From SFS Require Import heap.
From SFS Require Import Coqlib.
Import ListNotations.
Open Scope Ensembles_scope.
Close Scope Z_scope.
Create HintDb Heap_sets_DB.
Ltac solve_sets := eauto with Ensembles_DB Heap_sets_DB typeclass_instances.
Module HeapDefs (H : Heap) .
Module HL := HeapLemmas H.
Import H HL.
(** * Heap definitions *)
(** A value is a location or a function pointer offset *)
Inductive value : Type :=
| Loc : loc -> value
| FunPtr : fundefs -> var -> value.
Definition env : Type := M.t value.
(** A block in the heap *)
Inductive block :=
(* A constructed value *)
| Constr : cTag -> list value -> block
(* A closure pair -- Only used in pre-closure conversion semantics *)
| Clos : value -> value -> block
(* Env -- heap allocated to account for environment sharing between mutually rec. functions *)
| Env : env -> block.
(** The result of evaluation *)
Definition res : Type := value * heap block.
Inductive ans : Type :=
| Res : res -> ans
| OOT : ans (* out of time *).
(** * Location sets *)
Definition val_loc (v : value) : Ensemble loc :=
match v with
| Loc l => [set l]
| FunPtr _ _ => Empty_set _
end.
Definition val_loc_set (v : value) : PS.t :=
match v with
| Loc l => PS.singleton l
| FunPtr _ _ => PS.empty
end.
Definition env_locs (rho : env) (S : Ensemble var) : Ensemble loc :=
\bigcup_(x in S) (match M.get x rho with
| Some v => val_loc v
| None => Empty_set _
end).
Definition env_locs_set (rho : env) (s : PS.t) : PS.t :=
PS.fold (fun x s' =>
match M.get x rho with
| Some (Loc l) => PS.add l s'
| Some (FunPtr _ _) | None => s'
end)
s PS.empty.
Fixpoint env_locs_set_full (rho : M.t value) : PS.t :=
match rho with
| M.Leaf => PS.empty
| M.Node t1 (Some (Loc l)) t2 =>
PS.add l (PS.union (env_locs_set_full t1) (env_locs_set_full t2))
| M.Node t1 _ t2 =>
PS.union (env_locs_set_full t1) (env_locs_set_full t2)
end.
(** The locations that appear on a block *)
Definition locs (v : block) : Ensemble loc :=
match v with
| Constr t ls => Union_list (map val_loc ls)
| Clos v1 v2 => val_loc v1 :|: val_loc v2
| Env rho => env_locs rho (Full_set _)
end.
Fixpoint to_locs (vs : list value) : list loc :=
match vs with
| [] => []
| (Loc l) :: vs => l :: (to_locs vs)
| (FunPtr _ _) :: vs => to_locs vs
end.
(** The locations that appear on a block *)
Definition locs_set (v : block) : PS.t :=
match v with
| Constr t ls => union_list PS.empty (to_locs ls)
| Clos v1 v2 => PS.union (val_loc_set v1) (val_loc_set v2)
| Env rho => env_locs_set_full rho
end.
(** The locations that are pointed by the locations in S *)
Definition post (H : heap block) (S : Ensemble loc) :=
[ set l : loc | exists l' v, l' \in S /\ get l' H = Some v /\ l \in locs v].
(** Computational definition of [post] *)
Definition post_set (H : heap block) (s : PS.t) :=
PS.fold (fun l r =>
match get l H with
| Some v => PS.union (locs_set v) r
| None => r
end)
s PS.empty.
(** Least fixed point *)
Definition lfp {A} (f : Ensemble A -> Ensemble A) :=
\bigcup_( n : nat ) ((f ^ n) (Empty_set A)).
(** Least fixed point characterization of heap reachability. *)
Definition reach (H : heap block) (Si : Ensemble loc) : Ensemble loc :=
lfp (fun S => Union _ Si (post H S)).
(** Alternative characterization of heap reachability. *)
Definition reach' (H : heap block) (Si : Ensemble loc) : Ensemble loc :=
\bigcup_(n : nat) (((post H) ^ n) (Si)).
Definition reach_res (r : res) : Ensemble loc :=
let '(v, H) := r in reach' H (val_loc v).
Definition reach_ans (a : ans) : Ensemble loc :=
match a with
| Res r => reach_res r
| _ => Empty_set _
end.
Definition reach_var_env (S : Ensemble var) (rho : env) (H : heap block) : Ensemble loc :=
reach' H (env_locs rho S).
(** N-reachability. *)
Definition reach_n (H : heap block) (n : nat) (Si : Ensemble loc) : Ensemble loc :=
\bigcup_(m in (fun m => m <= n)) (((post H) ^ m) (Si)).
Fixpoint dfs (roots visited : PS.t) (H : heap block) (size : nat) : PS.t :=
match size with
| 0 => PS.union roots visited
| S n =>
match PS.cardinal roots with
| 0 => visited
| S c =>
let roots' := post_set H roots in (* compute the new set of roots *)
let visited' := PS.union visited roots in (* mark roots as visited *)
dfs (PS.diff roots' visited') visited' H n
end
end.
(** Computational definition of reachable location *)
Definition reach_set (H : heap block) (s : PS.t) : PS.t :=
dfs s PS.empty H (size H).
(** Reachability paths *)
Inductive path (H : heap block) : list loc -> loc -> nat -> Prop :=
| Path_Singl :
forall ld,
path H [] ld 0
| Path_Multi :
forall l ls ld n b,
path H ls l n ->
~ List.In l ls ->
get l H = Some b ->
ld \in locs b ->
path H (l :: ls) ld (S n).
(* The two definitions should be equivalent. *)
Lemma reach_equiv H Si :
reach H Si <--> reach' H Si.
Proof.
Abort.
Definition size_env {a} (rho : M.t a) : nat :=
PS.cardinal (@mset (key_set rho) _).
(** Size of heap blocks *)
Definition size_val (v : block) : nat :=
match v with
| Constr t ls => (* The size of the constructor representation *)
1 + length ls
| Clos _ _ => 3
| Env rho => 1 + size_env rho
end.
(** Size of the heap *)
Definition size_heap (H : heap block) : nat :=
size_with_measure size_val H.
(** * Wel-formedness definitions *)
(** A heap is well-formed if there are not dangling pointers in the stored values *)
Definition well_formed (S : Ensemble loc) (H : heap block) :=
forall l v, l \in S -> get l H = Some v -> locs v \subset dom H.
(** Well-formedness lifted to the environments. NOT USED *)
Definition well_formed_env (S: Ensemble var) (H : heap block) (rho : env):=
env_locs rho S \subset dom H.
(** A heap is closed in S if the locations of its image on S remain in S *)
Definition closed (S : Ensemble loc) (H : heap block) : Prop :=
forall l, l \in S -> exists v, get l H = Some v /\ locs v \subset S.
(** * Functions used in the semantics *)
(** Add a block of function definitions in the heap and the environment *)
Fixpoint def_closures (B B0: fundefs) (rho : env) (H : heap block) (cenv : value) {struct B}
: heap block * env :=
match B with
| Fcons f _ _ _ B' =>
let '(H', rho') := def_closures B' B0 rho H cenv in
(* construct the closure *)
let clo := Clos (FunPtr B0 f) cenv in
(* add it to heap *)
let '(l, H'') := alloc clo H' in
(* extend the environment *)
(H'', M.set f (Loc l) rho')
| Fnil => (H, rho)
end.
Fixpoint def_funs (B B0 : fundefs) rho :=
match B with
| Fcons f _ _ _ B =>
M.set f (FunPtr B0 f) (def_funs B B0 rho)
| Fnil => rho
end.
(** * Location renaming *)
Definition subst_val b (v : value) : value :=
match v with
| Loc l => Loc (b l)
| FunPtr _ _ => v
end.
Definition subst_env b rho := M.map (fun _ => subst_val b) rho.
Definition subst_block f b : block :=
match b with
| Constr c vs => Constr c (map (subst_val f) vs)
| Clos v1 v2 => Clos (subst_val f v1) (subst_val f v2)
| Env rho => Env (subst_env f rho)
end.
(** * Restriction of environments *)
Definition restrict_env (s : PS.t) (rho : env) : env :=
filter (fun i _ => PS.mem i s) rho.
(** [rho'] is the restriction of [rho] in [S] *)
Definition Restrict_env (S : Ensemble var) (rho rho' : env) : Prop :=
(forall x, x \in S -> M.get x rho = M.get x rho') /\
sub_map rho' rho /\ key_set rho' \subset S.
(** * Lemmas about [post] and [reach'] *)
(** Set monotonicity lemmas *)
Lemma post_set_monotonic S1 S2 H :
S1 \subset S2 ->
post H S1 \subset post H S2.
Proof.
unfold post. intros Hsub l [l' [v [Hin [Hget Hin']]]].
exists l', v. split; eauto.
Qed.
Lemma post_n_set_monotonic n (H : heap block) (S1 S2 : Ensemble loc) :
S1 \subset S2 -> (post H ^ n) S1 \subset (post H ^ n) S2.
Proof.
induction n; simpl; eauto with Ensembles_DB.
intros Hsub. eapply Included_trans.
eapply post_set_monotonic. now eauto.
reflexivity.
Qed.
Lemma reach'_set_monotonic H S1 S2 :
S1 \subset S2 ->
reach' H S1 \subset reach' H S2.
Proof.
intros Hsub; intros x [i [_ Hin]].
exists i. split. constructor. eapply app_monotonic.
+ intros. now eapply post_set_monotonic.
+ eassumption.
+ eassumption.
Qed.
Hint Resolve post_set_monotonic
post_n_set_monotonic
reach'_set_monotonic : Heap_sets_DB.
(** Heap monotonicity lemmas *)
Lemma post_heap_monotonic H1 H2 S :
H1 ⊑ H2 ->
post H1 S \subset post H2 S.
Proof.
unfold post. intros Hsub l [l' [v [Hin [Hget Hin']]]].
exists l', v. split; eauto.
Qed.
Lemma post_n_heap_monotonic n (H1 H2 : heap block) (S : Ensemble loc) :
H1 ⊑ H2 -> (post H1 ^ n) S \subset (post H2 ^ n) S.
Proof.
induction n; simpl; eauto with Ensembles_DB.
intros Hsub. eapply Included_trans.
eapply post_set_monotonic. now eauto.
now eapply post_heap_monotonic.
Qed.
Lemma reach'_heap_monotonic (H1 H2 : heap block) (S : Ensemble loc) :
H1 ⊑ H2 -> reach' H1 S \subset reach' H2 S.
Proof.
intros Hsub l [n [HT Hp]]. exists n; split; eauto.
eapply post_n_heap_monotonic; eauto.
Qed.
(** [reach'] is extensive *)
Lemma reach'_extensive H S :
S \subset reach' H S.
Proof.
intros x Hin. exists 0; split; eauto.
now constructor.
Qed.
(** [reach'] includes [post] *)
Lemma Included_post_reach' H S :
(post H S) \subset reach' H S.
Proof.
intros x Hin. exists 1; split; eauto.
now constructor.
Qed.
Lemma Included_post_n_reach' (H : heap block) (S : Ensemble loc) (n : nat) :
(post H ^ n) S \subset reach' H S.
Proof.
intros l Hp. eexists; eauto. split; eauto. constructor.
Qed.
Hint Immediate reach'_extensive
Included_post_reach'
Included_post_n_reach' : Heap_sets_DB.
Lemma reach_unfold H S :
(reach' H S) <--> (Union _ S (reach' H (post H S))).
Proof.
split; intros x.
- intros [i [_ Hin]].
destruct i.
+ eauto.
+ right. exists i. split. now constructor.
replace ((post H ^ i) (post H S))
with (((post H ^ i) ∘ (post H ^ 1)) S) by eauto.
rewrite <- app_plus. rewrite plus_comm. eassumption.
- intros Hin. destruct Hin as [ x Hin | x [i [_ Hin]]].
+ now eapply reach'_extensive.
+ exists (i+1). split. now constructor.
rewrite app_plus. eassumption.
Qed.
(** reach is a post fixed point of post. Post is monotonic so
it is also a fixed point *)
Lemma reach'_post_fixed_point H S :
post H (reach' H S) \subset reach' H S.
Proof.
unfold post, reach'; simpl; intros x [l [v [[i [_ Hin]] Hin']]].
exists (i + 1). split. now constructor.
rewrite plus_comm, app_plus.
exists l, v. split; eauto.
Qed.
Lemma reach'_post_fixed_point_n n H S :
(post H ^ n) (reach' H S) \subset reach' H S.
Proof.
induction n.
- reflexivity.
- replace (Datatypes.S n) with (n + 1). rewrite app_plus.
eapply Included_trans.
eapply app_monotonic. now intros; eapply post_set_monotonic.
now apply reach'_post_fixed_point. eassumption.
omega.
Qed.
(** [reach'] is idempotent *)
Lemma reach'_idempotent H S :
reach' H S <--> reach' H (reach' H S).
Proof.
unfold reach'. split; intros x Hin.
- exists 0. split. now constructor.
simpl. eassumption.
- rewrite <- bigcup_merge.
destruct Hin as [m [_ Hin]].
apply reach'_post_fixed_point_n in Hin.
exists 0. split; eauto. now constructor.
Qed.
Lemma reach_spec H S S' :
S \subset S' ->
S' \subset reach' H S ->
reach' H S <--> reach' H S'.
Proof.
intros Hsub1 Hsub2. split.
- eapply reach'_set_monotonic. eassumption.
- rewrite (reach'_idempotent H S).
apply reach'_set_monotonic. eassumption.
Qed.
(** Proper instances and the like *)
Instance Proper_post : Proper (eq ==> Same_set loc ==> Same_set loc) post.
Proof.
intros x1 x2 Heq S1 S2 Heq'; subst; split; intros z [l [v [Hin [Hget Hin']]]].
repeat eexists; eauto; now rewrite <- Heq'; eauto.
repeat eexists; eauto; now rewrite Heq'; eauto.
Qed.
Lemma proper_post_n H n S1 S2 :
S1 <--> S2 ->
((post H) ^ n) S1 <--> ((post H) ^ n) S2.
Proof.
induction n; eauto; intros Heq; simpl.
rewrite IHn; eauto. reflexivity.
Qed.
Instance Proper_reach' : Proper (eq ==> Same_set _ ==> Same_set _) reach'.
Proof.
intros H1 H2 heq S1 S2 Hseq; subst; split; intros x [n [Hn Hin]].
- eexists; split; eauto. eapply proper_post_n; eauto.
now symmetry.
- eexists; split; eauto. eapply proper_post_n; eauto.
Qed.
Lemma post_heap_eq S H1 H2 :
S |- H1 ≡ H2 ->
post H1 S <--> post H2 S.
Proof.
intros Heq; split; intros x [l [v [Hin [Hget Hin']]]].
repeat eexists; eauto; now rewrite <- Heq; eauto.
repeat eexists; eauto; now rewrite Heq; eauto.
Qed.
Lemma post_n_heap_eq n P H1 H2 :
reach' H1 P |- H1 ≡ H2 ->
(post H1 ^ n) P <--> (post H2 ^ n) P.
Proof.
induction n; intros Heq; try reflexivity.
simpl. rewrite IHn; solve_sets. apply post_heap_eq.
rewrite <- IHn.
eapply heap_eq_antimon; [| eassumption ]. eapply Included_post_n_reach'.
eassumption.
Qed.
Lemma post_n_heap_eq' n P H1 H2 :
reach' H2 P |- H1 ≡ H2 ->
(post H1 ^ n) P <--> (post H2 ^ n) P.
Proof.
induction n; intros Heq; try reflexivity.
simpl. rewrite IHn; eauto. apply post_heap_eq.
eapply heap_eq_antimon; eauto. eapply Included_post_n_reach'.
Qed.
Lemma reach'_heap_eq P H1 H2 :
reach' H1 P |- H1 ≡ H2 ->
reach' H1 P <--> reach' H2 P.
Proof.
intros Heq; split; intros l [n [HT Hp]]; eexists; split; eauto;
try now eapply post_n_heap_eq; eauto.
eapply post_n_heap_eq'; eauto.
symmetry ; eauto.
Qed.
Lemma reach'_post S H :
reach' H S <--> reach' H (S :|: (post H S)).
Proof.
rewrite <- reach_spec with (S' := Union loc S (post H S)).
reflexivity.
now eauto with Ensembles_DB.
apply Union_Included. now apply reach'_extensive.
now apply Included_post_reach'.
Qed.
(** Set lemmas *)
Lemma post_Empty_set :
forall (H : heap block), post H (Empty_set _) <--> Empty_set _.
Proof.
intros H.
unfold post. split; intros l H1; try now inv H1.
destruct H1 as [l' [b [Hin _]]]. inv Hin.
Qed.
Lemma post_Union H S1 S2 :
post H (Union _ S1 S2) <--> Union _ (post H S1) (post H S2).
Proof with now eauto with Ensembles_DB.
split; intros l Hp.
- destruct Hp as [l' [v [Hin [Hget Hin']]]].
inv Hin; [left | right ]; repeat eexists; eauto.
- destruct Hp as [ Hp | Hp ];
eapply post_set_monotonic; eauto...
Qed.
Lemma post_Singleton (H1 : heap block) (l : loc) (b : block) :
get l H1 = Some b ->
post H1 [set l] <--> locs b.
Proof.
intros Hget; split; intros l1.
- intros [l2 [b2 [Hin [Hget' Hin']]]]. inv Hin.
rewrite Hget in Hget'. inv Hget'; eauto.
- intros Hin. repeat eexists; eassumption.
Qed.
Lemma post_Singleton_None (H1 : heap block) (l : loc) :
get l H1 = None -> post H1 [set l] <--> Empty_set _.
Proof.
intros Hget; split; intros l1.
- intros [l2 [b2 [Hin [Hget' Hin']]]]. inv Hin.
rewrite Hget in Hget'. inv Hget'; eauto.
- intros Hin. inv Hin.
Qed.
Lemma post_n_Empty_set n H :
(post H ^ n) (Empty_set _) <--> Empty_set _.
Proof.
induction n; try reflexivity.
simpl. rewrite IHn. rewrite post_Empty_set.
reflexivity.
Qed.
Lemma post_n_Singleton_None H n l :
get l H = None ->
(post H ^ S n) [set l] <--> Empty_set _.
Proof.
intros Hnone. induction n.
- simpl. rewrite post_Singleton_None. reflexivity. eassumption.
- simpl. rewrite IHn. rewrite post_Empty_set. reflexivity.
Qed.
Lemma post_n_Union n H S1 S2 :
(post H ^ n) (Union _ S1 S2) <--> Union _ ((post H ^ n) S1) ((post H ^ n) S2).
Proof with now eauto with Ensembles_DB.
induction n; try now firstorder.
simpl. rewrite IHn, post_Union. reflexivity.
Qed.
Lemma reach'_Empty_set H :
reach' H (Empty_set positive) <--> Empty_set _.
Proof.
split; [| now firstorder ].
intros x [n [_ Hin]].
revert x Hin. induction n; intros x Hin. simpl in Hin.
inv Hin.
destruct Hin as [y [v [Hin [Hget Hin']]]].
eapply IHn in Hin. inv Hin.
Qed.
Lemma reach'_Union H S1 S2 :
reach' H (S1 :|: S2) <--> (reach' H S1) :|: (reach' H S2).
Proof.
split; intros l Hp.
- destruct Hp as [n [HT Hp]].
eapply post_n_Union in Hp. destruct Hp as [Hp | Hp ].
now left; firstorder. now right; firstorder.
- destruct Hp as [ l [n [HT Hp]] | l [n [HT Hp]] ];
repeat eexists; eauto; eapply post_n_Union; eauto.
Qed.
Lemma post_size_H_O S H :
size H = 0 ->
post H S <--> Empty_set _.
Proof.
intros Heq.
split; intros x.
- intros [y [v [Hin [Hget Hin']]]].
unfold size in Heq. eapply length_zero_iff_nil in Heq.
eapply heap_elements_complete in Hget.
rewrite Heq in Hget. inv Hget.
- intros Hin; inv Hin.
Qed.
Lemma reach_size_H_O S H :
size H = 0 ->
reach' H S <--> S.
Proof.
rewrite reach_unfold. intros Heq.
rewrite post_size_H_O; eauto.
rewrite reach'_Empty_set.
rewrite Union_Empty_set_neut_r.
reflexivity.
Qed.
(** Disjointedness lemmas *)
Lemma post_Disjoint S H :
Disjoint _ S (dom H) ->
post H S <--> Empty_set _.
Proof.
intros Hd; split; eauto with Ensembles_DB.
intros l [l' [v [Hin [Hget _]]]].
exfalso. eapply Hd. constructor; eauto.
eexists; eauto.
Qed.
Lemma post_n_Disjoint n S H :
Disjoint _ S (dom H) ->
(post H ^ n) S \subset S.
Proof with now eauto with Ensembles_DB.
revert S; induction n; intros S Hd; eauto with Ensembles_DB.
replace (Datatypes.S n) with (n + 1) by omega.
rewrite app_plus. simpl. unfold compose.
eapply Included_trans. eapply proper_post_n.
rewrite post_Disjoint; eauto. reflexivity.
eapply Included_trans; [ eapply IHn | ]...
Qed.
Lemma reach'_Disjoint S H :
Disjoint _ S (dom H) ->
reach' H S <--> S.
Proof.
split.
- intros l [n [_ Hp]]. eapply post_n_Disjoint; eauto.
- apply reach'_extensive.
Qed.
(** allocation lemmas *)
Lemma post_alloc S H v l H' :
alloc v H = (l, H') ->
post H' S \subset (Union _ (post H S) (locs v)).
Proof.
intros Ha l' Hp.
destruct Hp as [l2 [v' [Hin2 [Hget Hin1]]]].
destruct (loc_dec l l2); subst; eauto.
+ repeat eexists; eauto. erewrite gas in Hget; eauto.
inv Hget. eauto.
+ left; repeat eexists; eauto. erewrite <-gao; eauto.
Qed.
Lemma post_alloc' H v l H' :
alloc v H = (l, H') ->
post H' [set l] \subset locs v.
Proof.
intros Ha l' Hp.
destruct Hp as [l2 [v' [Hin2 [Hget Hin1]]]].
inv Hin2.
repeat eexists; eauto. erewrite gas in Hget; eauto.
inv Hget. eauto.
Qed.
Lemma post_n_alloc n S H v l H' :
alloc v H = (l, H') ->
locs v \subset reach' H S ->
(post H' ^ n) S \subset reach' H S.
Proof.
revert S.
induction n; intros S Ha Hin; simpl.
- eapply reach'_extensive.
- eapply Included_trans. eapply post_set_monotonic.
now eauto. eapply Included_trans. now eapply post_alloc; eauto.
eapply Union_Included. now eapply reach'_post_fixed_point_n with (n := 1).
eapply Included_trans; eauto. reflexivity.
Qed.
Lemma reach'_alloc S H v l H' :
alloc v H = (l, H') ->
locs v \subset reach' H S ->
reach' H' S <--> reach' H S.
Proof.
intros Ha Hin.
split.
- intros l' [n [_ Hp]]. eapply post_n_alloc; eauto.
- eapply reach'_heap_monotonic. now eapply alloc_subheap; eauto.
Qed.
(** * Lemmas about [path] *)
Lemma path_NoDup H ls ld n :
path H ls ld n -> NoDup ls.
Proof.
intros Hp; induction Hp; constructor; eauto.
Qed.
Lemma path_length H ls ld n :
path H ls ld n -> length ls = n.
Proof.
intros Hp; induction Hp; eauto.
simpl. congruence.
Qed.
Lemma path_heap_elements H ls ld n :
path H ls ld n ->
Subperm ls (map fst (heap_elements H)).
Proof.
intros Hp; induction Hp.
- eexists; split; [| reflexivity ].
eapply Sublist_nil.
- destruct IHHp as [elems [Hsub Hperm]].
eapply heap_elements_complete in H1.
eapply in_map with (f := fst) in H1.
eapply Permutation.Permutation_in in H1; [| symmetry ; eassumption ].
edestruct in_split as [l1 [l2 Heq]]; eauto.
rewrite Heq in *. simpl in *.
eexists (l :: l1 ++ l2).
split.
+ eapply sublist_skip. eapply Sublist_cons_app; eauto.
+ eapply Permutation.Permutation_trans; try eassumption.
eapply Permutation.Permutation_cons_app. reflexivity.
Qed.
Lemma path_post_n H l ls ld n :
path H (ls ++ [l]) ld n ->
ld \in ((post H ^ n) [set l]).
Proof.
revert n ld; induction ls; intros n ld Hp; inv Hp.
- simpl. inv H2. repeat eexists; eauto.
- do 2 eexists. split.
+ eapply IHls. eassumption.
+ split; eauto.
Qed.
Lemma path_prev H l1 l2 l l' m:
path H (l1 ++ l :: l2) l' m ->
path H l2 l (length l2) /\ ~ List.In l l2.
Proof.
revert l' m; induction l1; simpl; intros l' m Hpath; inv Hpath.
- erewrite <- path_length in H2; eauto.
- eapply IHl1. simpl. eassumption.
Qed.
Lemma post_path_n (S : Ensemble loc) H ld n :
ld \in (post H ^ (1 + n)) S ->
exists l ls m, l \in S /\ m <= (1 + n) /\ path H (ls ++ [l]) ld m.
Proof.
revert ld; induction n; intros ld Hpost.
- destruct Hpost as [l' [v [Hin1 [Hget Hin2]]]]. simpl in *.
exists l', [], 1. repeat split; eauto. simpl.
econstructor. now constructor.
now intros Hc; inv Hc. eassumption. eassumption.
- destruct Hpost as [l' [v [Hin1 [Hget Hpost]]]].
eapply IHn in Hin1. edestruct Hin1 as [l'' [ls [m [Hin [Hleq Hpath]]]]].
destruct (in_dec loc_dec l' (ls ++ [l''])) as [Hinl | Hninl].
+ edestruct in_split as [ll [lr Happ]]; try eassumption.
destruct (destruct_last lr) as [| [lr' [r Heq]]]; subst.
* simpl in Happ. assert (l' = l'') by (eapply app_snoc; eauto). subst.
eexists l'', [], 1. repeat split; eauto. omega.
simpl. econstructor; eauto. now constructor.
* assert (l'' = r).
{ replace (ll ++ l' :: lr' ++ [r]) with ((ll ++ l' :: lr') ++ [r]) in Happ
by (rewrite <- app_assoc; reflexivity).
eapply app_snoc; eauto. }
subst. rewrite Happ in Hpath.
erewrite <- path_length with (n := m) in Hleq, Hpath; eauto.
eapply path_prev in Hpath; destruct Hpath as [Hpath Hnin].
exists r, (l' :: lr'), (length (l' :: lr' ++ [r])).
repeat split; eauto.
simpl. rewrite !app_length.
repeat (simpl in Hleq; rewrite !app_length in Hleq).
simpl in *. omega.
simpl. econstructor; eauto.
+ eexists l'', (l' :: ls), (1 + m).
split; eauto. split. omega.
econstructor; eauto.
Qed.
Lemma post_exists_Singleton S H l :
post H S l ->
exists l', l' \in S /\ post H [set l'] l.
Proof.
intros [l' [v [Hin Hp]]]. eexists; split; eauto.
eexists. eexists. split; eauto.
Qed.
Lemma post_n_exists_Singleton S1 H n l :
(post H ^ n) S1 l ->
exists l', l' \in S1 /\ (post H ^ n) [set l'] l.
Proof.
revert l S1. induction n; intros l S1.
- simpl. intros. eexists; split; eauto. reflexivity.
- intros Hp.
simpl in *. eapply post_exists_Singleton in Hp.
destruct Hp as [l' [Hin Hp]].
eapply IHn in Hin. destruct Hin as [l'' [Hinl'' Hp'']].
eexists. split; eauto.
destruct Hp as [l1 [b1 [Heq [Hget Hin]]]]. inv Heq.
eexists. eexists. split; eauto.
Qed.
(** * Lemmas about [reach_n] *)
Lemma reach_0 S H :
reach_n H 0 S <--> S.
Proof.
split.
- intros x [y [H1 H2]].
destruct y; try omega. eauto.
- intros y Hin. eexists 0. split; eauto.
Qed.
Lemma reach_S_n S H m :
reach_n H (m + 1) S <--> reach_n H m S :|: (post H ^ (m + 1)) S.
Proof.
split.
+ intros x Hin. destruct Hin as [k [Hleq Hin]].
destruct (Nat.eq_dec k (m + 1)); subst.
* now right.
* left. eexists. split; [| eassumption ]. omega.
+ intros x Hin. destruct Hin as [ x Hin | x Hin].
* destruct Hin as [k [Hleq Hin]].
eexists. split; [| eassumption ]. omega.
* eexists. split; [| eassumption ]. omega.
Qed.
Lemma reach_n_in_reach (H : heap block) (n : nat) (S : Ensemble loc):
reach_n H n S \subset reach' H S.
Proof.
intros x [n' [_ Hin]]. eexists; split; eauto. constructor.
Qed.
(** [reach_n] is extensive *)
Lemma reach_n_extensive H n S :
S \subset reach_n H n S.
Proof.
intros x Hin. exists 0; split; eauto.
omega.
Qed.
Lemma reach_n_unfold H n S :
(reach_n H (1 + n) S) <--> (Union _ S (reach_n H n (post H S))).
Proof.
split; intros x.
- intros [i [Hleq Hin]].
destruct i.
+ eauto.
+ right. exists i. split. omega.
replace ((post H ^ i) (post H S))
with (((post H ^ i) ∘ (post H ^ 1)) S) by eauto.
rewrite <- app_plus. rewrite plus_comm. eassumption.
- intros Hin. destruct Hin as [ x Hin | x [i [Hleq Hin]]].
+ now eapply reach_n_extensive.
+ exists (i+1). split. omega.
rewrite app_plus. eassumption.
Qed.
Lemma reach_n_Empty_set n H :
reach_n H n (Empty_set _) <--> Empty_set _.
Proof.
split; intros x Hin; try now inv Hin.
destruct Hin as [m [Hin1 Hin2]].
eapply post_n_Empty_set in Hin2. inv Hin2.
Qed.
Lemma reach_n_Union (H : heap block) (S1 S2 : Ensemble loc) (n : nat) :
reach_n H n (S1 :|: S2) <--> reach_n H n S1 :|: reach_n H n S2.
Proof.
induction n.
- rewrite !reach_0. reflexivity.
- replace (S n) with (n + 1) by omega. rewrite !reach_S_n.
rewrite post_n_Union, IHn.
rewrite <- !Union_assoc.
eapply Same_set_Union_compat. reflexivity.
rewrite !Union_assoc.
eapply Same_set_Union_compat; [| reflexivity].
rewrite Union_commut. reflexivity.
Qed.
Instance Proper_reach_n : Proper (eq ==> eq ==> Same_set _ ==> Same_set _) reach_n.
Proof.
intros H1 H2 heq S1 S2 Hseq; subst; split; intros z [n [Hn Hin]].
- eexists; split; eauto. eapply proper_post_n; eauto.
now symmetry.
- eexists; split; eauto. eapply proper_post_n; eauto.
Qed.
Lemma reach_n_monotonic S H m n :
m <= n ->
reach_n H m S \subset reach_n H n S.
Proof.
revert n S. induction m; intros n S Hleq.
- rewrite reach_0. eapply reach_n_extensive.
- destruct n. omega.
rewrite !reach_n_unfold.
eapply Included_Union_compat. reflexivity.
eapply IHm. omega.
Qed.
Lemma reach_n_fixed_point_aux S H m :
(post H ^ (m + 1)) S \subset (reach_n H m S) ->
reach_n H (m + 1) S <--> reach_n H m S.
Proof.
intros Hsub.
split.
- intros x Hin. destruct Hin as [k [Hleq Hin]].
edestruct le_lt_eq_dec as [Hlt | Heq]; eauto.
+ eexists k.
split. omega.
repeat eexists; try eassumption.
+ subst. eapply Hsub.
eassumption.
- eapply reach_n_monotonic. omega.
Qed.
Lemma post_fixed_aux S H m n :
(post H ^ (m + 1)) S \subset reach_n H m S ->
(post H ^ (n + m + 1)) S \subset reach_n H m S.
Proof.
revert m S. induction n; eauto; intros m S Hsub.
simpl.
intros x [y [b [Hin1 [Hget Hin2]]]].
eapply IHn in Hin1; [| eassumption ].
destruct Hin1 as [k [Hleq Hin]].
edestruct le_lt_eq_dec as [Hlt | Heq]; eauto.
- eexists (1 + k).
split. omega.
repeat eexists; try eassumption.
- subst. eapply Hsub.
replace (m + 1) with (1 + m) by omega.
simpl. eexists. repeat eexists; try eassumption.
Qed.
Lemma post_fixed S H m n :
(post H ^ (m + 1)) S \subset reach_n H m S ->
n >= m ->
(post H ^ n) S \subset reach_n H m S.
Proof.
intros Hsub1 Hleq.
edestruct Nat.le_exists_sub as [n' [Hsum Hleq']]; subst.
eassumption.
subst.
destruct n'.
- simpl. intros x Hin. eexists; eauto.
- replace (Datatypes.S n' + m) with (n' + m + 1) by omega.
eapply post_fixed_aux. eassumption.
Qed.
Lemma reach_n_fixed_point S H m n :
(post H ^ (m+1)) S \subset reach_n H m S ->
reach_n H (n + m) S <--> reach_n H m S.
Proof.
revert m. induction n; intros m Hsub.
- reflexivity.
- replace (Datatypes.S n + m) with ((n + m) + 1) by omega.
rewrite reach_n_fixed_point_aux.
eapply IHn.
eassumption. eapply Included_trans. eapply post_fixed_aux.
eassumption. eapply reach_n_monotonic. omega.
Qed.
Lemma size_heap_fixed_point S H:
(post H ^ (1 + (size H))) S \subset reach_n H (size H) S.
Proof.
intros x Hpost.
edestruct post_path_n as [lr [ls [len [Hin [Hleq Hpath]]]]].
eassumption.
edestruct le_lt_eq_dec as [Hlt | Heq]; eauto.
- eapply path_post_n in Hpath.
eexists len. split; eauto. omega.
eapply post_n_set_monotonic; try eassumption.
eapply Singleton_Included. eassumption.
- erewrite <- (path_length _ _ _ len) in Heq, Hpath; eauto.
subst. eapply path_heap_elements in Hpath.
eapply Subperm_length in Hpath. rewrite map_length in Hpath.
rewrite Heq in Hpath. unfold size in Hpath. omega.
Qed.