-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheap_impl.v
590 lines (489 loc) · 20.4 KB
/
heap_impl.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
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 heap.
From SFS Require Import Coqlib.
Import ListNotations.
Close Scope Z_scope.
Module MHeap : Heap.
Definition loc := positive.
Record heap' (block : Type) :=
mkHeap
{ mheap : M.t block;
mdom : Ensemble positive;
mdom_spec : forall x, x \in mdom <-> exists v, M.get x mheap = Some v;
next : positive;
next_spec : forall x, (next <= x)%positive -> M.get x mheap = None }.
Definition heap block := heap' block.
Program Definition emp (A : Type) := mkHeap A (M.empty A) (Empty_set _) _ 1 _.
Next Obligation.
split; intros; try (zify; omega).
inv H.
destruct H as [v Hgetv].
rewrite M.gempty in Hgetv. congruence.
Qed.
Next Obligation.
rewrite M.gempty. congruence.
Qed.
Definition get {A : Type} l (h : heap A) : option A :=
M.get l (mheap A h).
Definition in_dom {A : Type} l h : {l \in mdom A h} + {~ l \in mdom A h}.
Proof.
destruct (get l h) eqn:Hget.
- destruct h. unfold get in *. left. simpl in *.
eapply mdom_spec0. eexists; eauto.
- destruct h. unfold get in *. right. simpl in *.
intros Hc. eapply mdom_spec0 in Hc. destruct Hc.
congruence.
Qed.
Program Definition set {A : Type} v l h : option (heap A) :=
match in_dom l h with
| left Hin =>
Some (mkHeap A (M.set l v (mheap A h)) (mdom A h) _ (next A h) _)
| right Hnin => None
end.
Next Obligation.
clear Heq_anonymous.
destruct (peq x l); subst.
- rewrite M.gss. split; eauto.
- rewrite M.gso; [| eassumption ].
eapply h.
Qed.
Next Obligation.
clear Heq_anonymous.
destruct (peq x l); subst.
- destruct h as [h1 d1 dspec1 f1 fspec1].
simpl in *. eapply fspec1 in H. eapply dspec1 in Hin.
destruct Hin. congruence.
- rewrite M.gso; eauto. eapply h. eassumption.
Qed.
Program Definition alloc {A : Type} (v : A) (h : heap A) :=
let l := next A h in
(l, mkHeap A (M.set l v (mheap A h)) (l |: mdom A h) _ ((next A h) + 1) _).
Next Obligation.
destruct (peq x (next A h)); subst.
- rewrite M.gss. split; eauto.
- rewrite M.gso; eauto. split; intros H.
inv H. inv H0; contradiction.
eapply h; eauto.
right; eapply h; eauto.
Qed.
Next Obligation.
destruct (peq x (next A h)); subst.
- zify; omega.
- rewrite M.gso; eauto. eapply h.
zify; omega.
Qed.
Lemma emp_get A l : @get A l (@emp A) = None.
Proof.
unfold emp. unfold get. simpl. rewrite M.gempty. reflexivity.
Qed.
Lemma gss A v l h h' :
@set A v l h = Some h' ->
@get A l h' = Some v.
Proof.
intros Hs. unfold set in Hs.
destruct (in_dom l h).
- inv Hs. unfold get. simpl. rewrite M.gss.
reflexivity.
- congruence.
Qed.
Lemma gso A v l l' h h' :
@set A v l h = Some h' ->
l <> l' ->
@get A l' h' = @get A l' h.
Proof.
intros Hs Hneq. unfold set in Hs.
destruct (in_dom l h).
- inv Hs. unfold get. simpl. rewrite M.gso.
reflexivity. intros Hc; subst; eauto.
- congruence.
Qed.
Lemma gas (A : Type) (x : A) l (H H' : heap A) :
alloc x H = (l, H') -> get l H' = Some x.
Proof.
intros Ha.
unfold alloc, get in *. inv Ha. simpl.
rewrite M.gss. reflexivity.
Qed.
Lemma gao (A : Type) (x : A) l1 l2 (H H' : heap A) :
l1 <> l2 ->
alloc x H = (l1, H') ->
get l2 H' = get l2 H.
Proof.
intros Hneq Ha.
unfold alloc, get in *. inv Ha. simpl.
rewrite M.gso. reflexivity. eauto.
Qed.
Lemma alloc_fresh (A : Type) (x : A) (l : loc) (H H' : heap A) :
alloc x H = (l, H') -> get l H = None.
Proof.
intros Ha.
unfold alloc, get in *. inv Ha. eapply H.
zify; omega.
Qed.
(** 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).
Definition restrict (A : Type) S (H1 H2 : heap A) : Prop :=
H2 ⊑ H1 /\
dom H2 <--> dom H1 :&: S.
Lemma restrict_subheap (A : Type) (S : Ensemble loc) (H1 H2 : heap A) :
restrict A S H1 H2 -> H2 ⊑ H1.
Proof.
intros Hr. destruct Hr; eauto.
Qed.
Lemma restrict_In (A : Type) (l : loc) (S : Ensemble loc) (H H' : heap A) :
restrict A S H H' -> In loc S l -> get l H' = get l H.
Proof.
intros [Hsub Hdom] Hin. unfold subheap in *.
destruct (get l H') as [b1|] eqn:Hget.
- symmetry. eauto.
- destruct (get l H) as [b2|] eqn:Hget'; eauto.
assert (Hc : l \in dom H :&: S).
{ constructor; eauto. eexists; eauto. }
eapply Hdom in Hc.
destruct Hc as [l' Hgetc']. congruence.
Qed.
Lemma restrict_notIn (A : Type) (l : loc) (S : Ensemble loc) (H H' : heap A) :
restrict A S H H' -> ~ In loc S l -> get l H' = None.
Proof.
intros [Hsub Hdom] Hin. unfold subheap in *.
destruct (get l H') as [b1|] eqn:Hget; eauto.
assert (Hc : l \in dom H :&: S).
{ eapply Hdom. eexists; eauto. }
inv Hc; contradiction.
Qed.
Definition restrict_map {A : Type} (S : Ensemble loc) {Hs : Decidable S} (m : M.t A) :=
M.fold (fun mr x v => if @Dec _ S _ x then mr else M.remove x mr) m m.
Lemma Sublist_refl A (l : list A) :
Sublist l l.
Proof.
induction l; eauto.
- now constructor.
- constructor 3; eauto.
Qed.
Lemma restrict_map_submap_strong (A : Type) (S : Ensemble loc) {Hs : Decidable S} (m : M.t A) x v l :
incl l (M.elements m) ->
list_norepet (map fst l) ->
M.get x (fold_left
(fun (a : M.t A) (p : positive * A) =>
if Dec (fst p) then a else M.remove (fst p) a) l m) = Some v ->
M.get x m = Some v.
Proof.
revert m.
induction l as [| [l1 v1] l IHl1]; intros m Hspec Hr Hget.
- eassumption.
- simpl in Hget. simpl in *.
destruct (@Dec positive S Hs l1).
* inv Hr. eapply IHl1; eauto.
intros y Hin.
assert (Hin' : List.In y ((l1, v1) :: l)) by now right.
eapply Hspec in Hin'. eassumption.
* { eapply IHl1 in Hget; try eauto.
- destruct (peq x l1).
subst. rewrite M.grs in Hget. congruence.
rewrite M.gro in Hget; eassumption.
- intros y Hin.
assert (Hin' : List.In y ((l1, v1) :: l)) by now right.
eapply Hspec in Hin'.
destruct y as [l2 v2].
eapply M.elements_complete in Hin'.
eapply M.elements_correct.
rewrite M.gro. eassumption.
intros Hceq; subst. inv Hr. eapply H1.
eapply in_map with (f := fst) in Hin. eassumption.
- inv Hr. eassumption. }
Qed.
Lemma restrict_map_same_strong (A : Type) (S : Ensemble loc) {Hs : Decidable S} (m : M.t A) x v l :
incl l (M.elements m) ->
list_norepet (map fst l) ->
x \in S ->
M.get x m = Some v ->
M.get x (fold_left
(fun (a : M.t A) (p : positive * A) =>
if Dec (fst p) then a else M.remove (fst p) a) l m) = Some v.
Proof.
revert m.
induction l as [| [l1 v1] l IHl1]; intros m Hspec Hr Hin Hget.
- eassumption.
- simpl in Hget. simpl in *.
destruct (@Dec positive S Hs l1).
* inv Hr. eapply IHl1; eauto.
intros y Hin'.
assert (Hin'' : List.In y ((l1, v1) :: l)) by now right.
eapply Hspec in Hin''. eassumption.
* { eapply IHl1; try eauto.
- intros y Hin1.
assert (Hin' : List.In y ((l1, v1) :: l)) by now right.
eapply Hspec in Hin'.
destruct y as [l2 v2].
eapply M.elements_complete in Hin'.
eapply M.elements_correct.
rewrite M.gro. eassumption.
intros Hceq; subst. inv Hr. eapply H1.
eapply in_map with (f := fst) in Hin1. eassumption.
- inv Hr. eassumption.
- rewrite M.gro. eassumption. intros Hc; subst. contradiction. }
Qed.
Corollary restrict_map_submap (A : Type) (S : Ensemble loc) {Hs : Decidable S} (m : M.t A) x v :
M.get x (restrict_map S m) = Some v -> M.get x m = Some v.
Proof.
unfold restrict_map. rewrite M.fold_spec.
eapply restrict_map_submap_strong. now (clear; firstorder).
eapply Maps.PTree.elements_keys_norepet.
Qed.
Lemma restrict_map_spec (A : Type) (S : Ensemble loc) {Hs : Decidable S} (m : M.t A) x v :
M.get x (restrict_map S m) = Some v <-> x \in S /\ M.get x m = Some v.
Proof.
assert (Hsuf :
(x, v) \in FromList (M.elements m) /\ M.get x (restrict_map S m) = Some v <->
(x, v) \in FromList (M.elements m) /\ x \in S /\ M.get x m = Some v).
{ unfold restrict_map. rewrite M.fold_spec.
assert (Hsub : incl (M.elements m) (M.elements m)) by (clear; firstorder).
assert (Hnd : list_norepet (map fst (M.elements m))).
{ eapply Maps.PTree.elements_keys_norepet. }
revert Hsub Hnd. generalize (M.elements m) at 1 3 4 5 6. intros l. revert m.
induction l as [| [l1 v1] l IHl1]; intros m Hspec Hr.
- simpl. rewrite FromList_nil. split; intros [H1 H2]; now inv H1.
- simpl. rewrite FromList_cons.
inv Hr. split.
+ destruct (@Dec _ S _ l1).
* { intros [Hget1 Hget2].
inv Hget1.
- inv H. subst. split; eauto. split; eauto.
eapply M.elements_complete. eapply Hspec. now constructor.
- split. now right. eapply IHl1; try eassumption.
intros y Hin.
assert (Hin' : List.In y ((l1, v1) :: l)) by now right.
eapply Hspec in Hin'. eassumption.
split. eassumption. eassumption. }
* { simpl. intros [Hget1 Hget2].
destruct (peq l1 x); subst.
- (* extra lemma + congruence *)
eapply restrict_map_submap_strong in Hget2; try eassumption.
rewrite M.grs in Hget2. congruence.
intros [l2 v2] Hin2.
assert (Hin' : List.In (l2, v2) ((x, v1) :: l)) by now right.
eapply Hspec in Hin'. eapply M.elements_complete in Hin'.
eapply M.elements_correct. rewrite M.gro. eassumption.
intros Hc; subst. eapply H1. eapply in_map with (f := fst) in Hin2.
eassumption.
- inv Hget1. inv H; contradiction.
split. now right.
assert (Ha := conj H Hget2).
eapply IHl1 in Ha; try eassumption.
destruct Ha as [_ [HinS Heq]]. split; eauto. rewrite M.gro in Heq. eassumption.
intros Hc; subst. rewrite M.grs in Heq. congruence.
intros [l2 v2] Hin.
assert (Hin' : List.In (l2, v2) ((l1, v1) :: l)) by now right.
eapply M.elements_correct. rewrite M.gro.
eapply M.elements_complete. eapply Hspec. now right.
intros Hc. subst. eapply H1. eapply in_map with (f := fst) (x := (l1, v2)).
eassumption. }
+ intros [Hin1 [Hin2 Hget1]].
inv Hin1.
* inv H.
destruct (@Dec _ S _ x); try contradiction.
split. now left.
eapply restrict_map_same_strong; try eassumption.
intros [l2 v2] Hin. eapply Hspec. now right.
* assert (Hc : x <> l1).
{ intros Hc. subst. eapply H1. eapply in_map with (f := fst) (x := (l1, v)).
eassumption. }
split. now right.
eapply IHl1; try eassumption.
intros [l2 v2] Hin.
assert (Hin' : List.In (l2, v2) ((l1, v1) :: l)) by now right.
eapply Hspec in Hin'. eapply M.elements_complete in Hin'.
eapply M.elements_correct.
destruct (@Dec _ S _ l1); [ eassumption | ]. rewrite M.gro. eassumption.
intros Hc'; subst.
eapply H1. eapply in_map with (f := fst) (x := (l1, v2)).
eassumption.
split. eassumption. split. eassumption.
destruct (@Dec _ S _ l1); [ eassumption | ]. rewrite M.gro; eassumption. }
split; intros H; eapply Hsuf.
- split; eauto.
eapply M.elements_correct. eapply restrict_map_submap. eassumption.
- split; eauto. destruct H.
eapply M.elements_correct. eassumption.
Qed.
Program Definition restrict_heap A (S : Ensemble loc) {Hs : Decidable S} (h : heap A) : heap A :=
mkHeap A (restrict_map S (mheap A h)) (mdom A h :&: S) _ (next A h) _.
Next Obligation.
split.
- intros [y H1 H2].
eapply h in H1. destruct H1 as [v Hget2].
assert (Hand := conj H2 Hget2). eapply restrict_map_spec in Hand.
eexists; eassumption.
- intros [v Hin]. eapply restrict_map_spec in Hin. destruct Hin. split; eauto.
eapply h. eexists; eauto.
Qed.
Next Obligation.
eapply h in H.
destruct (M.get x (restrict_map S (mheap A h))) eqn:Hget; eauto.
eapply restrict_map_spec in Hget. destruct Hget. congruence.
Qed.
Lemma restrict_exists (A : Type) (S : Ensemble loc) (H : heap A) :
Decidable S -> exists H' : heap A, restrict A S H H'.
Proof.
intros Hs. eexists (restrict_heap A S H).
split.
- intros x v Hget. eapply restrict_map_spec. eassumption.
- split.
+ intros x [c Hget]. eapply restrict_map_spec in Hget.
destruct Hget. split; eauto. eexists; eauto.
+ intros y [l [v Hd] Hin]. eexists.
eapply restrict_map_spec. split; eauto.
Qed.
Definition heap_elements {A : Type} (H : heap A) : list (loc * A) :=
M.elements (mheap A H).
Lemma heap_elements_sound (A : Type) (h : heap A) (l : loc) (v : A) :
List.In (l, v) (heap_elements h) -> get l h = Some v.
Proof.
intros Hin. unfold heap_elements, get in *.
eapply M.elements_complete. eassumption.
Qed.
Lemma heap_elements_complete (A : Type) (h : heap A) (l : loc) (v : A) :
get l h = Some v -> List.In (l, v) (heap_elements h).
Proof.
intro Hget. unfold heap_elements, get in *. eapply M.elements_correct.
eassumption.
Qed.
Lemma list_norepet_NoDup A B (l : list (A * B)) :
list_norepet (map fst l) ->
NoDup l.
Proof.
induction l; intros Hnr.
now constructor.
simpl in Hnr. inv Hnr. constructor; [| now eauto ].
intros Hc. eapply H1. eapply in_map. eassumption.
Qed.
Lemma heap_elements_NoDup (A : Type) (h : heap A) : NoDup (heap_elements h).
Proof.
unfold heap_elements.
eapply list_norepet_NoDup. eapply M.elements_keys_norepet.
Qed.
Lemma NoDup_filter (A : Type) P (l : list A) :
NoDup l -> NoDup (filter P l).
Proof.
intros Hnd; induction l. now constructor.
inv Hnd. simpl. destruct (P a); eauto. constructor; eauto.
intros Hc. eapply H1.
eapply filter_In. eassumption.
Qed.
Lemma filter_eq A P1 P2 (l : list A) :
(forall x, P1 x = P2 x) ->
filter P1 l = filter P2 l.
Proof.
intros Heq. induction l; eauto.
simpl. rewrite Heq, IHl. reflexivity.
Qed.
Definition heap_elements_filter {A : Type} S {Hs : ToMSet S} (h : heap A) : list (loc * A) :=
filter (fun p => @Dec _ S _ (fst p)) (heap_elements h).
Lemma heap_elements_filter_sound (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 /\ In loc S l.
Proof.
intros Hin. unfold heap_elements_filter in *. eapply filter_In in Hin.
destruct Hin as [Hin Hdec]. simpl in *.
split. eapply heap_elements_sound; eassumption.
eapply proj_sumbool_true in Hdec. eassumption.
Qed.
Lemma heap_elements_filter_complete (A : Type) (S : Ensemble loc) (H : ToMSet S)
(h : heap A) (l : loc) (v : A) :
get l h = Some v ->
In loc S l -> List.In (l, v) (heap_elements_filter S h).
Proof.
intros Hget Hin. unfold heap_elements_filter in *. eapply filter_In.
split. eapply heap_elements_complete; eassumption.
simpl.
eapply proj_sumbool_is_true. eassumption.
Qed.
Lemma heap_elements_filter_NoDup (A : Type) (S : Ensemble loc) (H : ToMSet S) (h : heap A) :
NoDup (heap_elements_filter S h).
Proof.
eapply NoDup_filter. eapply heap_elements_NoDup; eassumption.
Qed.
Lemma heap_elements_filter_set_Equal (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.
Proof.
intros Heq. eapply filter_eq. intros x.
destruct (@Dec positive S1 _ (fst x));
destruct (@Dec positive S2 _ (fst x)); eauto.
- assert (Hs' : S2 (fst x)). eapply Heq. eassumption.
contradiction.
- assert (Hs' : S1 (fst x)). eapply Heq. eassumption.
contradiction.
Qed.
Definition heap_elements_minus {A : Type} S {Hs : ToMSet S} (h : heap A) : list (loc * A) :=
filter (fun p => match @Dec _ S _ (fst p) with
| left _ => false
| right _ => true
end) (heap_elements h).
Lemma heap_elements_minus_sound (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 /\ ~ In loc S l.
Proof.
intros Hin. unfold heap_elements_minus in *. eapply filter_In in Hin.
destruct Hin as [Hin Hdec]. simpl in *.
split. eapply heap_elements_sound; eassumption.
destruct (@Dec positive S _ l); eauto. congruence.
Qed.
Lemma heap_elements_minus_complete (A : Type) (S : Ensemble loc) (H : ToMSet S)
(h : heap A) (l : loc) (v : A) :
get l h = Some v ->
~ In loc S l -> List.In (l, v) (heap_elements_minus S h).
Proof.
intros Hget Hin. unfold heap_elements_minus in *. eapply filter_In.
split. eapply heap_elements_complete; eassumption.
simpl.
destruct (@Dec positive S _ l); eauto.
Qed.
Lemma heap_elements_minus_NoDup (A : Type) (S : Ensemble loc) (H : ToMSet S) (h : heap A) :
NoDup (heap_elements_minus S h).
Proof.
eapply NoDup_filter. eapply heap_elements_NoDup; eassumption.
Qed.
Lemma heap_elements_minus_set_Equal (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.
Proof.
intros Heq. eapply filter_eq. intros x.
destruct (@Dec positive S1 _ (fst x));
destruct (@Dec positive S2 _ (fst x)); eauto.
- assert (Hs' : S2 (fst x)). eapply Heq. eassumption.
contradiction.
- assert (Hs' : S1 (fst x)). eapply Heq. eassumption.
contradiction.
Qed.
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 MHeap.