-
Notifications
You must be signed in to change notification settings - Fork 0
/
heap.v
1460 lines (1285 loc) · 48.8 KB
/
heap.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
(* Heaps for L6 space semantics. 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 Sorting.Permutation.
From SFS Require Import Ensembles_util functions List_util cps set_util.
From SFS Require Import Coqlib.
Import ListNotations.
Close Scope Z_scope.
(** ** Heap module type **)
(** The memory model used in the L6 heap semantics *)
(** In heap_impl.v we provide a concrete implementation that inhabits the module type **)
Module Type Heap.
Definition loc := positive.
Parameter heap : Type -> Type.
Parameter emp : forall {A : Type}, heap A.
Parameter get : forall {A : Type}, loc -> heap A -> option A.
Parameter set : forall {A : Type}, A -> loc -> heap A -> option (heap A).
Parameter alloc : forall {A : Type}, A -> heap A -> loc * heap A.
Parameter emp_get :
forall (A : Type) (l : loc), get l (@emp A) = None.
Parameter gss :
forall A (x : A) (l : loc) (H H' : heap A),
set x l H = Some H' ->
get l H' = Some x.
Parameter gso :
forall A (x : A) (l l' : loc) (H H' : heap A),
set x l H = Some H' ->
l <> l' ->
get l' H' = get l' H.
Parameter gas :
forall A (x : A) (l : loc) (H H' : heap A),
alloc x H = (l, H') ->
get l H' = Some x.
Parameter gao :
forall A (x : A) (l1 l2 : loc) (H H' : heap A),
l1 <> l2 ->
alloc x H = (l1, H') ->
get l2 H' = get l2 H.
Parameter alloc_fresh :
forall A (x : A) (l : loc) (H H' : heap A),
alloc x H = (l, H') ->
get l H = None.
(** Subheap *)
Definition subheap {A} (H1 H2 : heap A) :=
forall x v, get x H1 = Some v -> get x H2 = Some v.
Infix "⊑" := subheap (at level 70, no associativity).
(** Extensional equality of heaps *)
Definition heap_eq {A} (S : Ensemble loc) (H1 H2 : heap A) :=
forall x, x \in S -> get x H1 = get x H2.
Notation "S |- H1 ≡ H2" := (heap_eq S H1 H2) (at level 70, no associativity).
(** Domain *)
Definition dom {A} (H : heap A) : Ensemble loc :=
domain (fun l => get l H).
(** The restriction of a heap in a given domain *)
Parameter restrict : forall {A}, Ensemble loc -> heap A -> heap A -> Prop.
Parameter restrict_subheap :
forall A S (H1 H2 : heap A),
restrict S H1 H2 ->
H2 ⊑ H1.
Parameter restrict_In :
forall A (l : loc) (S : Ensemble loc) (H H' : heap A),
restrict S H H' ->
l \in S ->
get l H' = get l H.
Parameter restrict_notIn :
forall A (l : loc) (S : Ensemble loc) (H H' : heap A),
restrict S H H' ->
~ l \in S ->
get l H' = None.
(** Restriction in a decidable domain exists. Useful for garbage collection *)
Parameter restrict_exists :
forall A S (H : heap A),
Decidable S ->
exists H', restrict S H H'.
(** Elements *)
Parameter heap_elements : forall {A}, heap A -> list (loc * A).
Parameter heap_elements_sound :
forall (A : Type) (h : heap A) (l : loc) (v : A),
List.In (l, v) (heap_elements h) -> get l h = Some v.
Parameter heap_elements_complete :
forall (A : Type) (h : heap A) (l : loc) (v : A),
get l h = Some v -> List.In (l, v) (heap_elements h).
Parameter heap_elements_NoDup :
forall (A : Type) (h : heap A),
NoDup (heap_elements h).
(** Elements filter *)
Parameter heap_elements_filter :
forall {A} (H : Ensemble loc) {_ : ToMSet H}, heap A -> list (loc * A).
Parameter heap_elements_filter_sound :
forall (A : Type) (S : Ensemble loc) {H : ToMSet S} (h : heap A) (l : loc) (v : A),
List.In (l, v) (heap_elements_filter S h) -> get l h = Some v /\ l \in S.
Parameter heap_elements_filter_complete :
forall (A : Type) (S : Ensemble loc) {H : ToMSet S} (h : heap A) (l : loc) (v : A),
get l h = Some v -> l \in S -> List.In (l, v) (heap_elements_filter S h).
Parameter heap_elements_filter_NoDup :
forall (A : Type) (S : Ensemble loc) {H : ToMSet S} (h : heap A),
NoDup (heap_elements_filter S h).
Parameter heap_elements_filter_set_Equal :
forall (A : Type) (S1 : Ensemble loc) {H1 : ToMSet S1} (S2 : Ensemble loc) {H2 : ToMSet S2} (h : heap A),
S1 <--> S2 ->
heap_elements_filter S1 h = heap_elements_filter S2 h.
Parameter heap_elements_minus : forall {A} (H : Ensemble loc) {_ : ToMSet H}, heap A -> list (loc * A).
Parameter heap_elements_minus_sound :
forall (A : Type) (S : Ensemble loc) {H : ToMSet S} (h : heap A) (l : loc) (v : A),
List.In (l, v) (heap_elements_minus S h) -> get l h = Some v /\ ~ l \in S.
Parameter heap_elements_minus_complete :
forall (A : Type) (S : Ensemble loc) {H : ToMSet S} (h : heap A) (l : loc) (v : A),
get l h = Some v -> ~ l \in S -> List.In (l, v) (heap_elements_minus S h).
Parameter heap_elements_minus_NoDup :
forall (A : Type) (S : Ensemble loc) {H : ToMSet S} (h : heap A),
NoDup (heap_elements_minus S h).
Parameter heap_elements_minus_set_Equal :
forall (A : Type) (S1 : Ensemble loc) {H1 : ToMSet S1} (S2 : Ensemble loc) {H2 : ToMSet S2} (h : heap A),
S1 <--> S2 ->
heap_elements_minus S1 h = heap_elements_minus S2 h.
(** Size of a heap *)
(** The cardinality of the domain *)
Definition size {A : Type} (h : heap A) : nat := List.length (heap_elements h).
Definition size_with_measure {A : Type} (f : A -> nat) (h : heap A) : nat :=
fold_left (fun acc h => acc + f (snd h)) (heap_elements h) 0%nat.
Definition size_with_measure_filter {A : Type}
(f : A -> nat) (S : Ensemble loc) {H : ToMSet S} (h : heap A) : nat :=
fold_left (fun acc h => acc + f (snd h)) (heap_elements_filter S h) 0%nat.
Definition max_with_measure {A : Type} (f : A -> nat) (h : heap A) : nat :=
fold_left (fun acc h => max acc (f (snd h))) (heap_elements h) 0%nat.
Definition max_with_measure_filter {A : Type}
(f : A -> nat) (S : Ensemble loc) {H : ToMSet S} (h : heap A) : nat :=
fold_left (fun acc h => max acc (f (snd h))) (heap_elements_filter S h) 0%nat.
Definition size_with_measure_minus {A : Type}
(f : A -> nat) (S : Ensemble loc) {H : ToMSet S} (h : heap A) : nat :=
fold_left (fun acc h => acc + f (snd h)) (heap_elements_minus S h) 0%nat.
End Heap.
Module HeapLemmas (H : Heap).
Import H.
Lemma loc_dec : forall (l1 l2 : loc), { l1 = l2 } + { l1 <> l2 }.
Proof.
intros l1 l2.
eapply Pos.eq_dec.
Qed.
(** Allocation lemmas *)
Lemma alloc_In_dom {A} H1 (v :A) l H2 :
alloc v H1 = (l, H2) ->
l \in dom H2.
Proof.
intros. eexists. erewrite gas; eauto.
Qed.
Lemma alloc_not_In_dom A {b : A} H1 l1 H1' :
alloc b H1 = (l1, H1') ->
~ l1 \in dom H1.
Proof.
intros Ha [v1 Hget1]. erewrite alloc_fresh in Hget1; eauto.
congruence.
Qed.
Lemma alloc_subheap {A} (H1 H1' : heap A) l v :
alloc v H1 = (l, H1') ->
H1 ⊑ H1'.
Proof.
intros Ha x v' Hget. destruct (loc_dec x l); subst.
+ erewrite alloc_fresh in Hget; eauto; congruence.
+ erewrite gao; eauto.
Qed.
Lemma alloc_dom {A} H (v : A) l H' :
alloc v H = (l, H') ->
dom H' <--> Union _ [set l] (dom H).
Proof.
intros Ha; split; intros l' Hl'.
- destruct Hl' as [v' Hget].
destruct (loc_dec l l'); subst; eauto.
right. eexists. erewrite <- gao; eauto.
- inv Hl'.
+ inv H0. eexists; erewrite gas; eauto.
+ destruct H0 as [v' Hget].
eexists v'. erewrite gao; eauto.
intros Hc; subst. erewrite alloc_fresh in Hget; eauto.
discriminate.
Qed.
(** [subheap] lemmas *)
Lemma subheap_refl {A} (H : heap A) :
H ⊑ H.
Proof.
firstorder.
Qed.
Lemma subheap_trans {A} (H1 H2 H3 : heap A):
H1 ⊑ H2 ->
H2 ⊑ H3 ->
H1 ⊑ H3.
Proof.
firstorder.
Qed.
Lemma subheap_heap_eq {A} (H1 H2 : heap A) :
H1 ⊑ H2 ->
dom H1 |- H1 ≡ H2.
Proof.
intros Hsub l [v Hget]. rewrite Hget.
eapply Hsub in Hget. congruence.
Qed.
Lemma dom_subheap {A} (H1 H2 : heap A) :
H1 ⊑ H2 ->
dom H1 \subset dom H2.
Proof.
firstorder.
Qed.
Instance Preorder_subheap A : @PreOrder (heap A) subheap.
Proof.
constructor.
- intros x. eapply subheap_refl.
- intros x y z. eapply subheap_trans.
Qed.
(** [heap_eq] lemmas *)
Instance Equivalence_heap_eq {A} S : Equivalence (@heap_eq A S).
Proof.
constructor. now firstorder. now firstorder.
intros x y z H1 H2 l Hin. rewrite H1; firstorder.
Qed.
Instance Proper_heap_eq {A} : Proper (Same_set loc ==> eq ==> eq ==> iff) (@heap_eq A).
Proof.
intros S1 S2 Heq x1 x2 Heq1 y1 y2 Heq2; subst. firstorder.
Qed.
Lemma heap_eq_antimon {A} S1 S2 (H1 H2 : heap A) :
S1 \subset S2 ->
S2 |- H1 ≡ H2 ->
S1 |- H1 ≡ H2.
Proof.
firstorder.
Qed.
Lemma heap_eq_dom {A} S (H1 H2 : heap A) S' :
S |- H1 ≡ H2 ->
S' \subset dom H1 ->
S' \subset S ->
S' \subset dom H2.
Proof.
intros Heq Hsub1 Hsub2 l Hin.
specialize (Hsub1 l Hin). destruct Hsub1 as [v Hget].
rewrite Heq in Hget; eauto. eexists; eauto.
Qed.
(** Lemmas about [restrict] *)
Lemma restrict_domain :
forall A S (H1 H2 : heap A),
Decidable S ->
restrict S H1 H2 ->
dom H2 <--> (dom H1 :&: S).
Proof.
intros A S H1 H2 HD HR. split.
- intros l Hd2. constructor.
eapply dom_subheap. eapply restrict_subheap. eassumption.
eassumption.
destruct HD. destruct (Dec l) as [Hin | Hnin]; eauto.
eapply restrict_notIn in Hnin; [| eassumption ].
destruct Hd2. congruence.
- intros l Hi. inv Hi.
eapply restrict_In in H0; [| eassumption ].
destruct H as [v Hget]. exists v. congruence.
Qed.
Lemma restrict_heap_eq A S (H1 H2 : heap A) :
restrict S H1 H2 ->
S |- H1 ≡ H2.
Proof.
intros Hr x Hin. symmetry. eapply restrict_In; eauto.
Qed.
(** [heap_elements] lemmas *)
Lemma heap_elements_empty (A : Type) :
@heap_elements A emp = [].
Proof.
destruct (@heap_elements _ emp) as [| [l v] ls] eqn:Hh.
reflexivity.
assert (Hd : get l emp = Some v).
{ eapply heap_elements_sound. rewrite Hh. now constructor. }
rewrite emp_get in Hd. discriminate.
Qed.
Lemma heap_elements_alloc (A : Type) (h h' : heap A) (l : loc) (v : A) :
alloc v h = (l, h') ->
Permutation (heap_elements h') ((l, v) :: (heap_elements h)) .
Proof.
intros Ha.
eapply NoDup_Permutation.
- eapply heap_elements_NoDup.
- constructor.
intros Hin. eapply heap_elements_sound in Hin.
eapply alloc_fresh in Ha. congruence.
eapply heap_elements_NoDup.
- intros [l' v']; split; intros Hin.
+ eapply heap_elements_sound in Hin.
destruct (loc_dec l l'); subst.
* erewrite gas in Hin; [| eassumption ].
inv Hin. now constructor.
* erewrite gao in Hin; eauto.
constructor 2. eapply heap_elements_complete.
eassumption.
+ inv Hin.
* inv H.
eapply heap_elements_complete.
erewrite gas; [| eassumption ].
reflexivity.
* eapply heap_elements_sound in H.
eapply heap_elements_complete.
erewrite gao; eauto.
intros Hc. subst.
erewrite alloc_fresh in H; eauto.
congruence.
Qed.
Lemma subheap_Subperm (A : Type) (h1 h2 : heap A) :
h1 ⊑ h2 ->
Subperm (heap_elements h1) (heap_elements h2).
Proof.
intros Hsub.
eapply incl_Subperm.
now eapply heap_elements_NoDup.
intros [l v] Hin. eapply heap_elements_sound in Hin.
eapply heap_elements_complete.
eauto.
Qed.
(** [heap_elements_filter] lemmas *)
Lemma heap_elements_filter_empty (A : Type) (S : Ensemble loc) {H : ToMSet S}:
@heap_elements_filter A S H emp = [].
Proof.
destruct (@heap_elements_filter _ _ _ emp) as [| [l v] ls] eqn:Hh.
reflexivity.
assert (Hd : get l emp = Some v).
{ eapply heap_elements_filter_sound with (S := S). rewrite Hh. now constructor. }
rewrite emp_get in Hd. discriminate.
Qed.
Lemma heap_elements_filter_alloc (A : Type) (S : Ensemble loc) {H : ToMSet S}
(h h' : heap A) (l : loc) (v : A) :
alloc v h = (l, h') ->
l \in S ->
Permutation (heap_elements_filter S h') ((l, v) :: (heap_elements_filter S h)).
Proof.
intros Ha Hin.
eapply NoDup_Permutation.
- eapply heap_elements_filter_NoDup.
- constructor.
intros Hin'. eapply heap_elements_filter_sound in Hin'.
inv Hin'.
eapply alloc_fresh in Ha. congruence.
eapply heap_elements_filter_NoDup.
- intros [l' v']; split; intros Hin'.
+ eapply heap_elements_filter_sound in Hin'.
destruct (loc_dec l l'); subst.
* erewrite gas in Hin'; [| eassumption ].
inv Hin'. inv H0. now constructor.
* erewrite gao in Hin'; eauto.
inv Hin'. constructor 2. eapply heap_elements_filter_complete.
eassumption. eassumption.
+ inv Hin'.
* inv H0.
eapply heap_elements_filter_complete.
erewrite gas; [| eassumption ].
reflexivity. eassumption.
* eapply heap_elements_filter_sound in H0. inv H0.
eapply heap_elements_filter_complete.
erewrite gao; eauto.
intros Hc. subst.
erewrite alloc_fresh in H1; eauto.
congruence. eassumption.
Qed.
Lemma heap_elements_filter_alloc_nin (A : Type)
(S : Ensemble loc) {H : ToMSet S}
(h h' : heap A) (l : loc) (v : A) :
alloc v h = (l, h') ->
~ l \in S ->
Permutation (heap_elements_filter S h') (heap_elements_filter S h).
Proof.
intros Ha Hin.
eapply NoDup_Permutation.
- eapply heap_elements_filter_NoDup.
- eapply heap_elements_filter_NoDup.
- intros [l' v']; split; intros Hin'.
+ eapply heap_elements_filter_sound in Hin'.
destruct (loc_dec l l'); subst.
* erewrite gas in Hin'; [| eassumption ].
inv Hin'. contradiction.
* erewrite gao in Hin'; eauto.
inv Hin'. eapply heap_elements_filter_complete.
eassumption. eassumption.
+ eapply heap_elements_filter_sound in Hin'.
inv Hin'.
eapply heap_elements_filter_complete; eauto.
erewrite gao; eauto.
intros Hc; subst; contradiction.
Qed.
Lemma heap_elements_filter_add (A : Type) (S : Ensemble loc) {HS : ToMSet S}
(H : heap A) (l : loc) {HS' : ToMSet (l |: S)} (v : A) :
get l H = Some v ->
~ l \in S ->
Permutation (@heap_elements_filter _ (l |: S) HS' H) ((l, v) :: (heap_elements_filter S H)) .
Proof.
intros Hget Hnin.
eapply NoDup_Permutation.
- eapply heap_elements_filter_NoDup.
- constructor.
intros Hin. eapply Hnin.
edestruct heap_elements_filter_sound as [Hin1 Hin2];
eassumption.
eapply heap_elements_filter_NoDup.
- intros [l' v']. split.
+ intros Hin. edestruct heap_elements_filter_sound as [Hin1 Hin2].
eassumption. inv Hin2.
* inv H0.
rewrite Hget in Hin1. inv Hin1. now constructor.
* constructor 2. eapply heap_elements_filter_complete; eassumption.
+ intros Hin. inv Hin.
* inv H0.
eapply heap_elements_filter_complete. eassumption. now left.
* edestruct heap_elements_filter_sound as [Hin1 Hin2].
eassumption.
eapply heap_elements_filter_complete. eassumption.
now right.
Qed.
Lemma heap_elements_filter_add_not_In (A : Type) (S : Ensemble loc) {HS : ToMSet S}
(H : heap A) (l : loc) {HS' : ToMSet (l |: S)}:
get l H = None ->
~ l \in S ->
Permutation (@heap_elements_filter _ (l |: S) HS' H) ((heap_elements_filter S H)).
Proof.
intros Hget Hnin.
eapply NoDup_Permutation.
- eapply heap_elements_filter_NoDup.
- eapply heap_elements_filter_NoDup.
- intros [l' v']. split.
+ intros Hin. edestruct heap_elements_filter_sound as [Hin1 Hin2].
eassumption. inv Hin2.
* inv H0.
rewrite Hget in Hin1. inv Hin1.
* eapply heap_elements_filter_complete; eassumption.
+ intros Hin.
edestruct heap_elements_filter_sound as [Hin1 Hin2].
eassumption.
eapply heap_elements_filter_complete. eassumption.
now right.
Qed.
Lemma heap_elements_filter_Empty_set (A : Type) {HS1 : ToMSet (Empty_set _)}
(H : heap A) :
@heap_elements_filter A (Empty_set _) HS1 H = [].
Proof.
eapply Permutation_nil. symmetry.
eapply NoDup_Permutation.
- eapply heap_elements_filter_NoDup.
- constructor.
- intros [l v]; split; intros Hin; [| now inv Hin ].
edestruct heap_elements_filter_sound as [Hin1 Hin2].
eassumption. now inv Hin2.
Qed.
Lemma heap_elements_filter_Union (A : Type) (S1 : Ensemble loc) {HS1 : ToMSet S1}
(S2 : Ensemble loc) {HS2 : ToMSet S2}
{HS' : ToMSet (S1 :|: S2)}
(H : heap A) :
Disjoint _ S1 S2 ->
Permutation (@heap_elements_filter A (S1 :|: S2) HS' H)
((heap_elements_filter S1 H) ++ (heap_elements_filter S2 H)) .
Proof.
intros HD.
eapply NoDup_Permutation.
- eapply heap_elements_filter_NoDup.
- eapply NoDup_app.
+ eapply heap_elements_filter_NoDup.
+ eapply heap_elements_filter_NoDup.
+ constructor. intros x Hc. destruct Hc as [[l v] H1' H2'].
unfold FromList, In in *.
edestruct heap_elements_filter_sound with (S := S1) as [Hin1' Hin2'];
try eassumption.
edestruct heap_elements_filter_sound with (S := S2) as [Hin1'' Hin2''];
try eassumption.
eapply HD; constructor; eauto.
- intros [l' v']. split.
+ intros Hin. edestruct heap_elements_filter_sound as [Hin1 Hin2].
eassumption. inv Hin2.
* eapply Coqlib.in_app. left.
eapply heap_elements_filter_complete; try eassumption.
* eapply Coqlib.in_app. right.
eapply heap_elements_filter_complete; try eassumption.
+ intros Hin. eapply Coqlib.in_app in Hin. inv Hin.
* edestruct heap_elements_filter_sound as [Hin1 Hin2].
eassumption.
eapply heap_elements_filter_complete. eassumption. now left.
* edestruct heap_elements_filter_sound as [Hin1 Hin2].
eassumption.
eapply heap_elements_filter_complete. eassumption. now right.
Qed.
Lemma subheap_filter_Subperm (A : Type) (S : Ensemble loc) {HS : ToMSet S}
(h1 h2 : heap A) :
h1 ⊑ h2 ->
Subperm (heap_elements_filter S h1) (heap_elements_filter S h2).
Proof.
intros Hsub.
eapply incl_Subperm.
now eapply heap_elements_filter_NoDup.
intros [l v] Hin. eapply heap_elements_filter_sound in Hin.
inv Hin. eapply heap_elements_filter_complete; eauto.
Qed.
Lemma heap_elements_filter_monotonic {A} S1 `{HS1 : ToMSet S1} S2 `{HS2 : ToMSet S2} (H : heap A):
S1 \subset S2 ->
Subperm (heap_elements_filter S1 H) (heap_elements_filter S2 H).
Proof.
intros Hsub. eapply incl_Subperm.
eapply heap_elements_filter_NoDup.
intros [l1 b] Hin. eapply heap_elements_filter_sound in Hin.
destruct Hin as [Hget Hin].
eapply heap_elements_filter_complete; eauto.
Qed.
Instance ToMSet_dom {A} (H1 : heap A) : ToMSet (dom H1).
Proof.
eapply ToMSet_Same_set with (S1 := FromList (map fst (heap_elements H1)));
eauto with typeclass_instances.
- rewrite FromList_map_image_FromList. split; intros x.
+ intros [[x1 x2] [Hin1 Heq1]]. simpl; subst.
unfold FromList, In in *. simpl in Hin1.
eapply heap_elements_sound in Hin1. eexists; eauto.
+ intros [l Hget].
eexists (x, l). split; eauto. eapply heap_elements_complete.
eassumption.
Qed.
Lemma heap_elements_filter_dom A (H1 : heap A) :
Permutation (heap_elements H1) (heap_elements_filter (dom H1) H1).
Proof.
eapply NoDup_Permutation.
- eapply heap_elements_NoDup.
- eapply heap_elements_filter_NoDup.
- intros [l x]; split; intros Hin.
eapply heap_elements_sound in Hin.
eapply heap_elements_filter_complete.
eassumption. eexists; eassumption.
eapply heap_elements_filter_sound in Hin. inv Hin.
eapply heap_elements_complete.
eassumption.
Qed.
Lemma heap_elements_filter_weaken A (S : Ensemble loc) {HS : ToMSet S} (H1 : heap A) :
dom H1 \subset S ->
Permutation (heap_elements_filter S H1) (heap_elements_filter (dom H1) H1).
Proof.
intros Hin.
eapply NoDup_Permutation.
eapply heap_elements_filter_NoDup.
eapply heap_elements_filter_NoDup.
intros [x1 x2]. split; intros Hin'.
eapply heap_elements_filter_sound in Hin'. destruct Hin' as [Hin1 Hin2].
eapply heap_elements_filter_complete. eassumption. eexists. eassumption.
eapply heap_elements_filter_sound in Hin'. destruct Hin' as [Hin1 Hin2].
eapply heap_elements_filter_complete. eassumption. eapply Hin. eassumption.
Qed.
Lemma heap_elements_filter_subheap_dom {A} S {Hs : ToMSet S} (H1 H2 : heap A) :
S \subset dom H1 ->
H1 ⊑ H2 ->
Permutation (heap_elements_filter S H1) (heap_elements_filter S H2).
Proof.
intros Ha Hin.
eapply NoDup_Permutation.
- eapply heap_elements_filter_NoDup.
- eapply heap_elements_filter_NoDup.
- intros [l' v']; split; intros Hin'.
+ eapply heap_elements_filter_sound in Hin'.
destruct Hin' as [Hin1 Hin2].
eapply heap_elements_filter_complete.
eapply Hin. eassumption. eassumption.
+ eapply heap_elements_filter_sound in Hin'.
destruct Hin' as [Hin1 Hin2].
eapply heap_elements_filter_complete; [| eassumption ].
eapply Ha in Hin2. edestruct Hin2 as [v Hget1].
rewrite Hget1. eapply Hin in Hget1. congruence.
Qed.
Lemma heap_elements_filter_dom_sup {A} S1 `{HS1 : ToMSet S1} (H : heap A) :
Subperm (heap_elements_filter S1 H) (heap_elements_filter (dom H) H).
Proof.
eapply incl_Subperm.
now eapply heap_elements_filter_NoDup.
intros [l v] Hin. eapply heap_elements_filter_sound in Hin.
destruct Hin as [H1 H2].
eapply heap_elements_filter_complete.
eassumption. eexists. eassumption.
Qed.
(** [heap_elements_minus] lemmas *)
Lemma heap_elements_minus_empty (A : Type) (S : Ensemble loc) {H : ToMSet S}:
@heap_elements_minus A S H emp = [].
Proof.
destruct (@heap_elements_minus _ _ _ emp) as [| [l v] ls] eqn:Hh.
reflexivity.
assert (Hd : get l emp = Some v).
{ eapply heap_elements_minus_sound with (S := S). rewrite Hh. now constructor. }
rewrite emp_get in Hd. discriminate.
Qed.
Lemma heap_elements_minus_alloc (A : Type) (S : Ensemble loc) {H : ToMSet S}
(h h' : heap A) (l : loc) (v : A) :
alloc v h = (l, h') ->
~ l \in S ->
Permutation (heap_elements_minus S h') ((l, v) :: (heap_elements_minus S h)).
Proof.
intros Ha Hin.
eapply NoDup_Permutation.
- eapply heap_elements_minus_NoDup.
- constructor.
intros Hin'. eapply heap_elements_minus_sound in Hin'.
inv Hin'.
eapply alloc_fresh in Ha. congruence.
eapply heap_elements_minus_NoDup.
- intros [l' v']; split; intros Hin'.
+ eapply heap_elements_minus_sound in Hin'.
destruct (loc_dec l l'); subst.
* erewrite gas in Hin'; [| eassumption ].
inv Hin'. inv H0. now constructor.
* erewrite gao in Hin'; eauto.
inv Hin'. constructor 2. eapply heap_elements_minus_complete.
eassumption. eassumption.
+ inv Hin'.
* inv H0.
eapply heap_elements_minus_complete.
erewrite gas; [| eassumption ].
reflexivity. eassumption.
* eapply heap_elements_minus_sound in H0. inv H0.
eapply heap_elements_minus_complete.
erewrite gao; eauto.
intros Hc. subst.
erewrite alloc_fresh in H1; eauto.
congruence. eassumption.
Qed.
Lemma heap_elements_minus_alloc_in (A : Type)
(S : Ensemble loc) {H : ToMSet S}
(h h' : heap A) (l : loc) (v : A) :
alloc v h = (l, h') ->
l \in S ->
Permutation (heap_elements_minus S h') (heap_elements_minus S h).
Proof.
intros Ha Hin.
eapply NoDup_Permutation.
- eapply heap_elements_minus_NoDup.
- eapply heap_elements_minus_NoDup.
- intros [l' v']; split; intros Hin'.
+ eapply heap_elements_minus_sound in Hin'.
destruct (loc_dec l l'); subst.
* erewrite gas in Hin'; [| eassumption ].
inv Hin'. contradiction.
* erewrite gao in Hin'; eauto.
inv Hin'. eapply heap_elements_minus_complete.
eassumption. eassumption.
+ eapply heap_elements_minus_sound in Hin'.
inv Hin'.
eapply heap_elements_minus_complete; eauto.
erewrite gao; eauto.
intros Hc; subst; contradiction.
Qed.
Lemma heap_elements_minus_add (A : Type) (S : Ensemble loc) {HS : ToMSet S}
(H : heap A) (l : loc) {HS' : ToMSet S} (v : A) :
get l H = Some v ->
~ l \in S ->
Permutation (@heap_elements_minus _ S _ H) ((l, v) :: (heap_elements_minus (l |: S) H)) .
Proof.
intros Hget Hnin.
eapply NoDup_Permutation.
- eapply heap_elements_minus_NoDup.
- constructor.
intros Hin. eapply Hnin.
edestruct heap_elements_minus_sound as [Hin1 Hin2];
try eassumption.
exfalso. eapply Hin2; eauto.
eapply heap_elements_minus_NoDup.
- intros [l' v']. split.
+ intros Hin. edestruct heap_elements_minus_sound as [Hin1 Hin2].
eassumption.
destruct (peq l' l). subst.
* rewrite Hget in Hin1; inv Hin1. now constructor.
* constructor 2. eapply heap_elements_minus_complete; try eassumption.
intros Hc. inv Hc; eauto. inv H0; eauto.
+ intros Hin. inv Hin.
* inv H0.
eapply heap_elements_minus_complete; eassumption.
* edestruct heap_elements_minus_sound as [Hin1 Hin2];
try eassumption.
eapply heap_elements_minus_complete; try eassumption.
intros Hc. eauto.
Qed.
Lemma heap_elements_minus_add_not_In (A : Type) (S : Ensemble loc) {HS : ToMSet S}
(H : heap A) (l : loc):
get l H = None ->
l \in S ->
Permutation (@heap_elements_minus _ S _ H) ((heap_elements_minus S H)).
Proof.
intros Hget Hnin.
eapply NoDup_Permutation.
- eapply heap_elements_minus_NoDup.
- eapply heap_elements_minus_NoDup.
- intros [l' v']. split.
+ intros Hin. edestruct heap_elements_minus_sound as [Hin1 Hin2].
eassumption.
eapply heap_elements_minus_complete; try eassumption.
+ intros Hin.
destruct (peq l' l); subst.
* edestruct heap_elements_minus_sound as [Hin1 Hin2].
eassumption. exfalso; eauto.
* edestruct heap_elements_minus_sound as [Hin1 Hin2].
eassumption.
eapply heap_elements_minus_complete; eassumption.
Qed.
(** [size] lemmas *)
Lemma size_emp :
forall (A : Type), @size A emp = 0%nat.
Proof.
intros. unfold size. simpl.
rewrite heap_elements_empty. reflexivity.
Qed.
Lemma size_alloc (A : Type) (x : A) (H : heap A) (H' : heap A) (l : loc) (s : nat):
size H = s ->
alloc x H = (l, H') ->
size H' = (s + 1)%nat.
Proof.
intros Hs1 Ha.
unfold size in *. eapply heap_elements_alloc in Ha.
erewrite Permutation_length; [| eassumption ].
simpl. omega.
Qed.
Lemma size_subheap :
forall A (H1 H2 : heap A), H1 ⊑ H2 -> size H1 <= size H2.
Proof.
intros A H1 H2 Hsub.
eapply Subperm_length.
eapply subheap_Subperm.
eassumption.
Qed.
(** [size_with_measure] lemmas *)
Lemma size_with_measure_emp (A : Type) f :
@size_with_measure A f emp = 0%nat.
Proof.
unfold size_with_measure.
rewrite heap_elements_empty. reflexivity.
Qed.
Lemma size_with_measure_alloc
(A : Type) f (x : A) (H : heap A) (H' : heap A) (l : loc) (s : nat) :
size_with_measure f H = s ->
alloc x H = (l, H') ->
size_with_measure f H' = (s + f x)%nat.
Proof.
intros Hs1 Ha.
unfold size_with_measure in *. eapply heap_elements_alloc in Ha.
erewrite fold_permutation; [| now firstorder | eassumption ].
simpl.
replace (f x) with ((fun acc h => acc + f (snd h)) 0 (l, x)); [| reflexivity ].
erewrite List_util.fold_left_comm with (f0 := fun acc h => acc + f (snd h)); [| now firstorder ].
omega.
Qed.
Lemma size_with_measure_subheap :
forall A f (H1 H2 : heap A),
H1 ⊑ H2 ->
size_with_measure f H1 <= size_with_measure f H2.
Proof.
intros A f H1 H2 Hsub. unfold size_with_measure.
eapply fold_left_subperm; eauto.
now firstorder.
now firstorder.
now firstorder.
now firstorder.
eapply subheap_Subperm. eassumption.
Qed.
Lemma size_with_measure_get {A} (H1 : heap A) l f b :
get l H1 = Some b ->
f b <= size_with_measure f H1.
Proof.
intros Hget. unfold size_with_measure.
eapply heap_elements_complete in Hget.
generalize (heap_elements H1), Hget. clear Hget.
intros ls Hin. induction ls.
+ inv Hin.
+ simpl.
inv Hin.
* simpl. eapply fold_left_extensive.
intros [l1 x1] x2. omega.
* eapply le_trans. eapply IHls. eassumption.
eapply fold_left_monotonic; [| omega ].
intros x1 x2 [l1 y1] Hleq. simpl.
omega.
Qed.
(** [max_with_measure] lemmas *)
Lemma max_with_measure_emp (A : Type) f :
@max_with_measure A f emp = 0%nat.
Proof.
unfold max_with_measure.
rewrite heap_elements_empty. reflexivity.
Qed.
Lemma max_with_measure_alloc
(A : Type) f (x : A) (H : heap A) (H' : heap A) (l : loc) (s : nat) :
max_with_measure f H = s ->
alloc x H = (l, H') ->
max_with_measure f H' = max s (f x).
Proof.
intros Hs1 Ha.
unfold max_with_measure in *. eapply heap_elements_alloc in Ha.
erewrite fold_permutation; [| | eassumption ].
simpl.
replace (f x) with ((fun acc h => max acc (f (snd h))) 0 (l, x)); [| reflexivity ].
erewrite List_util.fold_left_comm with (f0 := fun acc h => max acc (f (snd h))).
rewrite Hs1. reflexivity.
intros y [l1 x1] [l2 x2]; simpl.
rewrite <- !Max.max_assoc, (Max.max_comm (f x1)). reflexivity.
intros [l1 x1] [l2 x2] y; simpl.
rewrite <- !Max.max_assoc, (Max.max_comm (f x1)). reflexivity.
Qed.
Lemma max_with_measure_subheap :
forall A f (H1 H2 : heap A),
H1 ⊑ H2 ->
max_with_measure f H1 <= max_with_measure f H2.
Proof.
intros A f H1 H2 Hsub. unfold max_with_measure.
eapply fold_left_subperm; eauto.
+ now firstorder.
+ intros x1 x2 [l1 x3] Hleq. simpl.
eapply le_trans. eassumption. eapply Max.le_max_l.
+ intros x1 x2 [l1 x3] Hleq. simpl.
eapply Nat.max_le_compat_r. eassumption.
+ intros [l1 x1] [l2 x2] y; simpl.
rewrite <- !Max.max_assoc, (Max.max_comm (f x1)).
reflexivity.
+ eapply subheap_Subperm. eassumption.
Qed.
Lemma max_with_measure_get {A} (H1 : heap A) l f b :
get l H1 = Some b ->
f b <= max_with_measure f H1.
Proof.
intros Hget. unfold max_with_measure.
eapply heap_elements_complete in Hget.
generalize (heap_elements H1), Hget. clear Hget.
intros ls Hin. induction ls.
+ inv Hin.
+ simpl.
inv Hin.
* simpl. eapply fold_left_extensive.
intros [l1 x1] x2. eapply Max.le_max_l.
* eapply le_trans. eapply IHls. eassumption.
eapply fold_left_monotonic; [| omega ].
intros x1 x2 [l1 y1] Hleq. simpl.
eapply Nat.max_le_compat_r. eassumption.
Qed.
(** [size_with_measure_filter] lemmas *)
Lemma size_with_measure_filter_emp (A : Type) (S : Ensemble loc) {HS : ToMSet S} f :
@size_with_measure_filter A f S _ emp = 0%nat.
Proof.
unfold size_with_measure_filter.
rewrite heap_elements_filter_empty. reflexivity.
Qed.
Lemma size_with_measure_filter_alloc
(A : Type) f (S : Ensemble loc) {HS : ToMSet S}
(x : A) (H : heap A) (H' : heap A) (l : loc) (m : nat) :
size_with_measure_filter f S H = m ->
alloc x H = (l, H') ->
l \in S ->
size_with_measure_filter f S H' = (m + f x)%nat.
Proof.
intros Hs1 Ha Hin.
unfold size_with_measure_filter in *. eapply heap_elements_filter_alloc in Ha; [| eassumption ].
erewrite fold_permutation; [| | eassumption ].
simpl.
replace (f x) with ((fun acc h => acc + f (snd h)) 0 (l, x)); [| reflexivity ].
erewrite List_util.fold_left_comm with (f0 := fun acc h => acc + f (snd h)); [| now firstorder ].
omega. intros. omega.
Qed.
Lemma size_with_measure_filter_alloc_in
(A : Type) f (S : Ensemble loc) {HS : ToMSet S}
(x : A) (H : heap A) (H' : heap A) (l : loc) (m : nat) :
size_with_measure_filter f S H = m ->
alloc x H = (l, H') ->
~ l \in S ->
size_with_measure_filter f S H' = m.
Proof.
intros Hs1 Ha Hnin.
unfold size_with_measure_filter in *. eapply heap_elements_filter_alloc_nin in Ha; [| eassumption ].
erewrite fold_permutation; [| | eassumption ].
eassumption. intros; omega.
Qed.
Lemma size_with_measure_filter_subheap :