-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMap.v
1218 lines (1129 loc) · 27.3 KB
/
Map.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
Require Coq.FSets.FMapFacts.
Require Coq.FSets.FMapInterface.
Require Import Coq.Lists.SetoidList.
Require Coq.Structures.OrderedType.
Require Aniceto.List.
Lemma ina_to_in:
forall {A:Type} (x:A) l,
(InA eq x l <-> In x l).
Proof.
intros.
split.
- intros.
rewrite InA_altdef in H.
rewrite Exists_exists in H.
destruct H as (x', (Hin, Heq)).
subst; assumption.
- intros.
rewrite InA_altdef.
rewrite Exists_exists.
exists x.
intuition.
Qed.
Module MapUtil (Import M:FMapInterface.WS).
Module F := FMapFacts.Facts M.
Module P := FMapFacts.Properties M.
Import F.
Import P.
Lemma mapsto_to_in:
forall elt k e m,
MapsTo (elt:=elt) k e m ->
In k m.
Proof.
intros.
unfold In.
exists e.
assumption.
Qed.
Lemma in_to_mapsto : forall (elt:Type) m x,
In x m -> exists (e:elt), MapsTo x e m.
Proof.
intros.
unfold In in H.
assumption.
Qed.
Lemma add_neq_mapsto:
forall {elt:Type} k k' e (e':elt) m,
~ E.eq k k' ->
MapsTo k e (add k' e' m) ->
MapsTo k e m.
Proof.
intros.
rewrite add_mapsto_iff in H0.
destruct H0 as [(Hr,Hn)|(_,Hr)].
subst.
contradiction H.
auto.
auto.
Qed.
Lemma add_in:
forall {elt:Type} x y (e:elt) m,
In x m ->
In x (add y e m).
Proof.
intros.
unfold In in *.
destruct H as (e', H).
destruct (eq_dec x y).
- exists e.
apply add_1.
auto.
- apply add_2 with (x:=y) (e':=e) in H.
exists e'.
auto.
auto.
Qed.
Lemma remove_in:
forall {elt:Type} x y m,
In x (@remove elt y m) ->
In x m.
Proof.
intros.
unfold In in *.
destruct H as (e, Hmt).
eauto using remove_3.
Qed.
Let add_not_in:
forall {elt:Type} k k' (e:elt) m,
~ In k (add k' e m) ->
~ In k m.
Proof.
intros.
intuition.
apply add_in with (y:=k') (e0:=e) in H0.
apply H in H0.
trivial.
Qed.
Lemma add_inv:
forall {elt:Type} (x y:key) (e:elt) m1 m2,
Add x e m1 m2 ->
forall y,
(exists e', MapsTo y e' m2 /\ MapsTo y e' (add x e m1)) \/
(~ In y m2 /\ ~ In y (add x e m1)).
Proof.
intros.
unfold Add in *.
assert (Hr := H y0).
remember (find y0 m2) as r.
symmetry in Heqr.
symmetry in Hr.
destruct r.
rewrite <- find_mapsto_iff in Heqr.
rewrite <- find_mapsto_iff in Hr.
left.
exists e0. intuition.
(* negative case *)
rewrite <- not_find_in_iff in Hr.
rewrite <- not_find_in_iff in Heqr.
right.
intuition.
Qed.
Lemma add_mapsto_neq:
forall {elt:Type} k k' e (e':elt) m1 m2,
MapsTo k e m1 ->
Add k' e' m1 m2 ->
~ E.eq k k' ->
MapsTo k e m2.
Proof.
intros.
apply add_inv with (y0:=k) in H0.
destruct H0 as [(e1, (H2, H3))|(H2,H3)].
apply add_neq_mapsto in H3.
apply MapsTo_fun with (e:=e) in H3.
subst. trivial.
assumption.
assumption.
(* absurd case *)
assert (Hin: In k m1).
unfold In.
exists e.
assumption.
(* end of assert *)
apply add_not_in in H3.
contradiction H3.
auto.
Qed.
Lemma add_mapsto_eq:
forall {elt:Type} k k' (e':elt) m1 m2,
Add k' e' m1 m2 ->
E.eq k k' ->
MapsTo k e' m2.
Proof.
intros.
assert (Heq : E.eq k' k') by auto.
apply add_eq_o with (elt:=elt) (m:=m1) (e:=e') in Heq.
unfold Add in H.
assert (Hf := H k'). clear H.
remember (find k' m2) as f.
destruct f.
- symmetry in H0.
rewrite Heq in Hf.
inversion Hf.
subst.
symmetry in Heqf.
rewrite <- find_mapsto_iff in Heqf.
rewrite <- H0.
assumption.
- rewrite <- Hf in Heq.
inversion Heq.
Qed.
Lemma in_elements_impl_maps_to:
forall {elt:Type} k (e:elt) m,
List.In (k, e) (elements (elt:=elt) m) ->
MapsTo k e m.
Proof.
intros.
apply elements_2.
apply In_InA; auto.
apply Build_Equivalence; unfold eq_key_elt.
- unfold Reflexive; destruct x; intuition.
- unfold Symmetric.
intros.
destruct x, y, H0.
simpl in *.
auto.
- unfold Transitive.
intros.
destruct x, y, z, H0, H1.
simpl in *.
subst.
intuition.
apply E.eq_trans with (y:=k1); repeat assumption.
Qed.
Lemma maps_to_impl_in_elements:
forall {elt:Type} k (e:elt) m,
MapsTo k e m ->
exists k', E.eq k k' /\ List.In (k', e) (elements (elt:=elt) m).
Proof.
intros.
apply elements_1 in H.
apply InA_alt in H.
destruct H as ((k', e'), (Heq, Hin)).
inversion Heq.
simpl in *.
subst.
exists k'.
intuition.
Qed.
Lemma maps_to_iff_in_elements:
forall {elt:Type} k (e:elt) m,
(forall k k', E.eq k k' -> k = k') ->
(MapsTo k e m <->
List.In (k, e) (elements (elt:=elt) m)).
Proof.
intros.
split.
- intros. apply maps_to_impl_in_elements in H0.
destruct H0 as (k', (Heq, Hin)).
apply H in Heq.
subst.
assumption.
- apply in_elements_impl_maps_to.
Qed.
Lemma in_to_ina_eq_key_elt (k_eq: forall k k', E.eq k k' <-> k = k'):
forall {elt:Type} k v (l: list (key * elt)),
List.In (k,v) l -> InA (eq_key_elt (elt:=elt)) (k,v) l.
Proof.
intros.
rewrite InA_altdef.
rewrite Exists_exists.
exists (k, v).
intuition.
unfold eq_key_elt.
simpl.
rewrite k_eq.
intuition.
Qed.
Lemma in_elements_to_in:
forall {elt:Type} k e (m: t elt),
List.In (k, e) (elements m) ->
In k m.
Proof.
intros.
rewrite F.elements_in_iff.
exists e.
apply InA_altdef.
apply Exists_exists.
exists (k,e).
intuition.
unfold eq_key_elt.
intuition.
Qed.
Lemma of_list_in_iff
{elt:Type}
(k_eq: forall k k', E.eq k k' <-> k = k'):
forall (k:E.t) (v:elt) (l:list (E.t * elt)),
NoDupA (eq_key (elt:=elt)) l ->
(MapsTo k v (of_list l) <-> List.In (k, v) l).
Proof.
intros.
split.
- intros.
apply of_list_1 in H0.
apply InA_alt in H0.
destruct H0 as (kv, (Heq, Hin)).
destruct kv.
unfold eq_key_elt in Heq.
destruct Heq.
simpl in *.
apply k_eq in H0.
subst.
assumption.
assumption.
- intros.
apply of_list_1; auto.
apply in_to_ina_eq_key_elt; repeat auto.
Qed.
Lemma to_list_of_list
{elt:Type}
(k_eq: forall k k', E.eq k k' <-> k = k'):
forall (k:E.t) (v:elt) (l:list (E.t * elt)),
NoDupA (eq_key (elt:=elt)) l ->
List.In (k, v) l ->
List.In (k, v) (to_list (of_list l)).
Proof.
intros.
apply (in_to_ina_eq_key_elt k_eq) in H0.
apply (of_list_1 (l:=l) k v H) in H0.
unfold to_list.
apply maps_to_impl_in_elements in H0.
destruct H0 as (k', (Heq, Hin)).
apply k_eq in Heq; subst.
assumption.
Qed.
Lemma empty_to_mapsto:
forall {elt:Type} k (e:elt) m,
Empty m ->
~ MapsTo k e m.
Proof.
intros.
unfold Empty in *.
apply H.
Qed.
(* *** filter for map **** *)
Definition filter_elements {elt:Type} (f:(key * elt)%type -> bool) (m:t elt) := List.filter f (elements m).
Lemma filter_elements_spec
{elt:Type}
(k_eq: forall k k', E.eq k k' <-> k = k'):
forall (f:(key * elt)%type -> bool) (k:key) (e:elt) (m:t elt),
List.In (k, e) (filter_elements f m) <-> MapsTo k e m /\ f (k, e) = true.
Proof.
intros.
unfold filter_elements.
rewrite filter_In.
rewrite maps_to_iff_in_elements; repeat intuition.
apply k_eq; assumption.
Qed.
Lemma filter_elements_nil_to_forall
{elt:Type}
(k_eq: forall k k', E.eq k k' <-> k = k'):
forall (f:(key * elt)%type -> bool) (m:t elt),
filter_elements f m = nil -> (forall k e, MapsTo k e m -> f (k, e) = false).
Proof.
unfold filter_elements; intros.
rewrite List.filter_forall_false in H.
apply maps_to_impl_in_elements in H0.
destruct H0 as (?, (Heq, Hi)).
rewrite k_eq in *; subst.
auto.
Qed.
Lemma forall_to_filter_elements_nil
{elt:Type}
(k_eq: forall k k', E.eq k k' <-> k = k'):
forall (f:(key * elt)%type -> bool) (m:t elt),
(forall k e, MapsTo k e m -> f (k, e) = false) ->
filter_elements f m = nil.
Proof.
unfold filter_elements; intros.
apply List.forall_to_filter_nil; intros.
destruct x as (k, e).
auto using in_elements_impl_maps_to.
Qed.
Lemma filter_elements_nil_spec
{elt:Type}
(k_eq: forall k k', E.eq k k' <-> k = k'):
forall (f:(key * elt)%type -> bool) (m:t elt),
filter_elements f m = nil <-> (forall k e, MapsTo k e m -> f (k, e) = false).
Proof.
split; eauto using forall_to_filter_elements_nil, filter_elements_nil_to_forall.
Qed.
Lemma filter_preserves_ina {elt:Type}:
forall a f l,
InA (eq_key (elt:=elt)) a (List.filter f l) ->
InA (eq_key (elt:=elt)) a l.
Proof.
intros.
induction l.
- trivial.
- simpl in *.
remember (f a0).
destruct b.
+ inversion H.
* subst.
apply InA_cons_hd; repeat auto.
* subst.
apply IHl in H1.
apply InA_cons_tl.
assumption.
+ apply IHl in H; clear IHl.
apply InA_cons_tl.
assumption.
Qed.
Lemma filter_preserves_nodupa {elt:Type}:
forall l f,
NoDupA (eq_key (elt:=elt)) l ->
NoDupA (eq_key (elt:=elt)) (List.filter f l).
Proof.
intros.
induction l.
- trivial. (* base case *)
- inversion H.
subst.
apply IHl in H3.
simpl.
remember (f a).
destruct b.
+ apply NoDupA_cons; repeat auto.
intuition.
apply H2.
apply filter_preserves_ina with (f0:=f).
assumption.
+ assumption.
Qed.
Lemma filter_elements_nodupa:
forall {elt:Type} (f:(key * elt)%type -> bool) (k:key) (e:elt) (m:t elt),
NoDupA (eq_key (elt:=elt)) (filter_elements f m).
Proof.
intros.
unfold filter_elements.
apply filter_preserves_nodupa.
apply elements_3w.
Qed.
Definition filter {elt:Type} (f: key -> elt -> bool) (m:t elt) : t elt :=
of_list (filter_elements (fun p => let (k, e) := p in f k e) m).
Lemma filter_spec
{elt:Type}
(k_eq: forall k k', E.eq k k' <-> k = k'):
forall (f:key -> elt -> bool) (k:key) (e:elt) (m:t elt),
MapsTo k e (filter f m) <-> MapsTo k e m /\ f k e = true.
Proof.
intros.
unfold filter.
rewrite of_list_in_iff.
- rewrite filter_elements_spec.
intuition.
auto.
- apply k_eq.
- apply filter_elements_nodupa; repeat auto.
Qed.
Lemma filter_empty_to_forall
{elt:Type}
(k_eq: forall k k', E.eq k k' <-> k = k'):
forall (f:key -> elt -> bool) (m:t elt),
Empty (filter f m) -> (forall k e, MapsTo k e m -> f k e = false).
Proof.
unfold Empty; intros.
remember (f k e).
symmetry in Heqb.
destruct b; auto.
assert (Hi: MapsTo k e m /\ f k e = true) by auto.
apply filter_spec in Hi; auto.
apply H in Hi.
contradiction.
Qed.
Lemma forall_to_filter_empty
{elt:Type}
(k_eq: forall k k', E.eq k k' <-> k = k'):
forall (f:key -> elt -> bool) (m:t elt),
(forall k e, MapsTo k e m -> f k e = false) ->
Empty (filter f m).
Proof.
unfold Empty, not; intros.
rewrite filter_spec in H0; auto.
destruct H0.
apply H in H0.
rewrite H0 in *.
inversion H1.
Qed.
Lemma empty_filter_spec
{elt:Type}
(k_eq: forall k k', E.eq k k' <-> k = k'):
forall (f:key -> elt -> bool) (m:t elt),
Empty (filter f m) <-> forall k e, MapsTo k e m -> f k e = false.
Proof.
split; eauto using filter_empty_to_forall, forall_to_filter_empty.
Qed.
(** Decidable in *)
Lemma any_in_dec:
forall {elt:Type} (m:t elt),
{ k | In k m } + { _ : unit | Empty m }.
Proof.
intros elt.
apply map_induction with (elt:=elt); intros. {
right.
exists tt.
assumption.
}
left.
exists x.
unfold In.
eauto using add_mapsto_eq.
Defined.
Lemma in_choice:
forall {elt:Type} (m:t elt),
{ exists k, In k m } + { ~ exists k, In k m }.
Proof.
intros elt.
apply map_induction with (elt:=elt).
- intuition.
right.
intros.
destruct H0 as (k, (e, Hmt)).
unfold Empty in *.
apply H in Hmt.
assumption.
- intros.
left.
exists x.
unfold In.
exists e.
apply add_mapsto_eq with (k:=x) in H1.
assumption.
auto.
Defined.
Lemma nonempty_in:
forall {elt:Type} (m:t elt),
~ Empty m <->
exists k, In k m.
Proof.
split.
+ intros.
remember (elements m).
destruct l.
- symmetry in Heql.
apply elements_Empty in Heql.
tauto.
- destruct p as (k, e).
assert (List.In (k, e) (elements m)). {
rewrite <- Heql.
auto with *.
}
exists k.
eauto using in_elements_to_in.
+ intros.
unfold Empty.
intuition.
unfold In in *.
destruct H as (k, (e, Hmt)).
eauto using H0.
Qed.
(**
Given a predicate, pick an element from the map, otherwise the predicate
is not matched for all elements in the map.
*)
Lemma pred_choice:
forall {elt:Type} (m:t elt) f,
Proper (E.eq ==> eq ==> eq) f ->
{exists k v, MapsTo k v m /\ f k v = true } +
{forall k v, MapsTo k v m -> f k v = false}.
Proof.
intros.
remember (fst (P.partition f m)) as y.
destruct (in_choice y).
- left.
destruct e as (k, Hin).
apply in_to_mapsto in Hin.
destruct Hin as (e, Hin).
exists k.
exists e.
apply P.partition_iff_1 with (k:=k) (e:=e) in Heqy; auto with *.
intuition.
- right.
intros.
remember (f k v) as b.
destruct b; auto.
symmetry in Heqb.
assert (exists k, In k y). {
exists k.
unfold In.
exists v.
apply P.partition_iff_1 with (k:=k) (e:=v) in Heqy; auto.
rewrite Heqy.
intuition.
}
contradiction H1.
Defined.
(** Decidable exists: either there is an element such that [f k v = true],
in which case we return that [(k,v)], otherwise, we return unit
and the proof that there is no element that satisfies [f k v = true]. *)
Definition exists_dec {elt:Type} (_ : forall k k' : E.t, E.eq k k' <-> k = k')
(f: key -> elt -> bool)
(m:t elt)
:
{ x : (key * elt) | MapsTo (fst x) (snd x) m /\ f (fst x) (snd x) = true }
+
{ _ : unit | forall k v, MapsTo k v m -> f k v = false }.
Proof.
intros.
destruct (any_in_dec (filter f m)). {
left.
remember (find (proj1_sig s) (filter f m)).
symmetry in Heqo.
destruct o. {
exists (proj1_sig s, e).
destruct s; simpl in *.
unfold In in i.
destruct i as (i, Hmt).
rewrite <- find_mapsto_iff in *.
assert (i = e) by eauto using F.MapsTo_fun; subst.
apply filter_spec in Hmt; auto.
}
apply not_find_in_iff in Heqo.
contradiction Heqo.
destruct s.
auto.
}
right.
exists tt.
intros.
destruct s.
rewrite empty_filter_spec in e; auto.
Defined.
Definition lookup_dec {elt:Type} (X : forall k k' : E.t, E.eq k k' <-> k = k')
(k: key)
(m:t elt)
:
{ v : elt | MapsTo k v m }
+
{ _ : unit | ~ In k m }.
Proof.
destruct (exists_dec X (fun y e => if eq_dec k y then true else false) m). {
left.
destruct s as (p, (mt,?)).
destruct (F.eq_dec k (fst p)). {
apply X in e; subst.
eauto using exist.
}
inversion H.
}
right.
destruct s.
apply exist with (x:=tt).
unfold not; intros.
unfold In in *.
destruct H as (?, mt).
apply e in mt.
destruct (F.eq_dec k k). {
inversion mt.
}
contradiction n.
rewrite X.
trivial.
Defined.
Definition keys {elt:Type} (m:t elt) : list key := fst (split (elements m)).
Lemma keys_spec_1:
forall {elt:Type} (m:t elt) (k:key),
List.In k (keys m) -> In k m.
Proof.
intros.
unfold keys in *.
apply List.in_fst_split in H.
destruct H as (e, H).
apply in_elements_to_in with (e0:=e).
assumption.
Qed.
Lemma keys_spec_2:
forall {elt:Type} (m:t elt) (k:key),
In k m ->
exists k', E.eq k k' /\ List.In k' (keys m).
Proof.
intros.
unfold keys in *.
destruct H as (e, H).
apply maps_to_impl_in_elements in H.
destruct H as (k', (Heq, Hin)).
apply in_split_l in Hin.
exists k'.
intuition.
Qed.
Lemma keys_spec (k_eq: forall k k', E.eq k k' <-> k = k'):
forall {elt:Type} (m:t elt) (k:key),
In k m <-> List.In k (keys m).
Proof.
intros.
split.
- intros.
apply keys_spec_2 in H.
destruct H as (k', (Heq, H)).
apply k_eq in Heq; subst.
assumption.
- apply keys_spec_1.
Qed.
Lemma ina_fst_split_alt:
forall {elt:Type} (k:key) (e:elt) (l:list (key*elt)%type),
InA E.eq k (fst (List.split_alt l)) ->
InA (eq_key (elt:=elt)) (k, e) l.
Proof.
intros.
induction l.
- inversion H. (* absurd *)
- inversion H.
+ subst.
destruct a as (k', e').
simpl in *.
inversion H0.
subst.
clear H0.
apply InA_cons_hd.
unfold eq_key.
auto.
+
destruct a as (k', e').
simpl in *.
inversion H0.
subst.
clear H0.
apply IHl in H1; clear IHl.
apply InA_cons_tl.
assumption.
Qed.
Lemma fst_split_nodupa:
forall {elt:Type} (l:list (key*elt)%type),
NoDupA (eq_key (elt:=elt)) l ->
NoDupA E.eq (fst (split l)).
Proof.
intros; induction l.
- simpl. apply NoDupA_nil.
- inversion H; clear H.
subst.
apply IHl in H3; clear IHl.
destruct a as (k,e).
rewrite List.split_alt_spec.
simpl.
apply NoDupA_cons.
+ intuition.
apply ina_fst_split_alt with (e0:=e) in H.
contradiction H.
+ rewrite List.split_alt_spec in H3.
assumption.
Qed.
Lemma keys_nodupa:
forall {elt:Type} (m:t elt),
NoDupA E.eq (keys m).
Proof.
intros.
unfold keys.
apply fst_split_nodupa.
apply elements_3w.
Qed.
Lemma keys_nodup (k_eq: forall k k', E.eq k k' <-> k = k'):
forall {elt:Type} (m:t elt),
NoDup (keys m).
Proof.
intros.
assert (Hx := keys_nodupa m).
apply List.nodupa_nodup_iff in Hx.
assumption.
apply k_eq.
Qed.
Lemma in_dec {elt:Type} (is_eq:forall k k' : E.t, E.eq k k' <-> k = k'):
forall k (m:t elt),
{ In k m } + { ~ In k m }.
Proof.
intros.
assert (eq_dec : forall x y:E.t, { x = y } + { x <> y }). {
intros.
destruct (E.eq_dec x y).
- left.
rewrite <- is_eq.
assumption.
- right.
intuition.
rewrite is_eq in *.
tauto.
}
destruct (List.in_dec eq_dec k (keys m)).
- left.
auto using keys_spec_1.
- right.
intuition.
assert (List.In k (keys m)). {
apply keys_spec; auto.
}
contradiction H0.
Defined.
Definition values {elt:Type} (m:t elt) : list elt := snd (split (elements m)).
(** Tail-recursive version of values. *)
Definition values_tr {elt:Type} (m:t elt) : list elt := snd (List.split_tr (elements m)).
Lemma values_tr_rw:
forall elt (m:t elt),
values m = values_tr m.
Proof.
intros.
unfold values, values_tr.
rewrite List.split_tr_rw.
trivial.
Qed.
Lemma values_spec_1:
forall {elt:Type} (m:t elt) (e:elt),
List.In e (values m) ->
exists k, MapsTo k e m.
Proof.
intros.
unfold values in *.
apply List.in_snd_split in H.
destruct H as (k, Hin).
apply in_elements_impl_maps_to in Hin.
exists k.
assumption.
Qed.
Lemma values_spec_2:
forall {elt:Type} (m:t elt) (k:key) (e:elt),
MapsTo k e m ->
List.In e (values m).
Proof.
intros.
unfold values.
apply maps_to_impl_in_elements in H.
destruct H as (k', (Heq, Hin)).
apply in_split_r in Hin.
simpl in *.
assumption.
Qed.
Lemma values_spec:
forall {elt:Type} (m:t elt) (e:elt),
List.In e (values m) <-> exists k, MapsTo k e m.
Proof.
intros.
split.
apply values_spec_1.
intros.
destruct H as (k, H).
apply values_spec_2 with (k0:=k).
assumption.
Qed.
Lemma eq_key_unfold:
forall {elt:Type} (k k':E.t) (e e':elt),
eq_key (k, e) (k', e') <-> E.eq k k'.
Proof.
unfold eq_key.
tauto.
Qed.
Lemma eq_key_in_to_ina:
forall {elt:Type} (k k':E.t) (e e':elt) l,
E.eq k' k ->
List.In (k, e) l ->
InA (eq_key (elt:=elt)) (k', e') l.
Proof.
intros.
apply InA_alt.
exists (k, e).
intuition.
Qed.
Lemma find_rw:
forall {A:Type} k (m: t A),
{ find k m = None /\ ~ In k m }
+ { exists e, find k m = Some e /\ MapsTo k e m }.
Proof.
intros.
remember (find _ _) as o.
symmetry in Heqo.
destruct o as [e|].
- right.
exists e.
intuition.
- left; intuition.
apply not_find_in_iff in Heqo.
contradiction.
Qed.
Section OMap.
Set Implicit Arguments.
Variable A: Type.
Variable B: Type.
(** omap changes the element only if there is some return value. *)
Variable f : E.t -> A -> option B.
Variable m:t A.
Let adapt_f (p:E.t * A) :=
let (k, v) := p in
match f k v with
| Some v => Some (k,v)
| None => None
end.
Definition omap := of_list (List.omap adapt_f (to_list m)).
Let notina_inv_cons:
forall x a l,
~ InA (eq_key (elt:=A)) x (a :: l) ->
~ InA (eq_key (elt:=A)) x l.
Proof.
intros.
intuition.
Qed.
Let notina_cons:
forall k k' e b l,
~ E.eq k k' ->
~ InA (eq_key (elt:=B)) (k, e) l ->
~ InA (eq_key (elt:=B)) (k, e) ((k', b) :: l).
Proof.
intros.
intuition.
inversion H1; subst.
- compute in H3.
contradiction.
- contradiction.
Qed.
Let notina_omap:
forall l k e e',
~ InA (eq_key (elt:=A)) (k, e') l ->
~ InA (eq_key (elt:=B)) (k, e) (List.omap adapt_f l).
Proof.
induction l.
- intros.
simpl.
intuition.
inversion H0.
- intros.
simpl.
remember (adapt_f a).
destruct o.
+ simpl.
destruct a as (k', a).
simpl in Heqo.
destruct (f k' a).
* inversion Heqo; subst; clear Heqo.
destruct (E.eq_dec k k'). {
contradiction H.
eauto using InA_eqA.
}
assert (~ InA (eq_key (elt:=B)) (k, e) (List.omap adapt_f l)). {
eauto.
}
eauto.
* inversion Heqo.
+ eauto.
Qed.
Let nodupa_omap:
NoDupA (eq_key (elt:=B)) (List.omap adapt_f (elements m)).
Proof.
intros.
assert (NoDupA (eq_key (elt:=A)) (elements m))
by eauto using elements_3w.
induction H.
- simpl.
apply NoDupA_nil.
- simpl.
remember (adapt_f x).
destruct o.
+ simpl.
destruct x as (k', v).
simpl in Heqo.
destruct (f k' v). {
inversion Heqo; subst.
apply NoDupA_cons; auto.
eauto.
}
inversion Heqo.
+ auto.
Qed.
Variable eq_rw:
forall k k',
E.eq k k' <-> k = k'.