-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinArray.v
1500 lines (1332 loc) · 46.6 KB
/
BinArray.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
(*Add LoadPath "..".*)
Require Import PosAux.
Require Import EqBoolAux.
Require Import Bool.
Require Export BinArrayBot.
Section dom.
Variable A B : Type.
Fixpoint inv_pos (p1 p2:positive) {struct p1} : positive :=
match p1 with
| xH => p2
| (xO p) => (inv_pos p (xO p2))
| (xI p) => (inv_pos p (xI p2))
end.
Fixpoint elements_rec (t : tree (option A)) (p:positive) (acc:list (positive*A)) {struct t} : list (positive*A) :=
match t with
| leaf _ => acc
| node _ None tO tI =>
elements_rec tI (xI p) (elements_rec tO (xO p) acc)
| node _ (Some a) tO tI =>
elements_rec tI (xI p) (cons ((inv_pos p xH),a) (elements_rec tO (xO p) acc))
end.
Lemma in_elements_rec_in_acc :
forall (t1 : tree (option A)) p q acc,
In q acc ->
In q (elements_rec t1 p acc).
Proof.
induction t1 as [| a t0 IHt0 t2 IHt2]; simpl in |- *;
intros.
auto.
destruct a; apply IHt2; auto.
right; auto.
Qed.
Lemma in_elements_rec_in_dom :
forall (t1 : tree _) p q acc a,
apply_tree _ None t1 p = Some a ->
In ((inv_pos q p),a) (elements_rec t1 q acc).
Proof.
induction t1 as [| a t0 IHt0 t2 IHt2]; simpl in |- *;
intros.
discriminate H.
destruct a.
destruct p.
replace (inv_pos q (xI p)) with (inv_pos (xI q) p); auto.
apply in_elements_rec_in_acc; right; auto.
replace (inv_pos q (xO p)) with (inv_pos (xO q) p); auto.
apply in_elements_rec_in_acc; left; auto.
injection H; intros; subst; auto.
destruct p.
replace (inv_pos q (xI p)) with (inv_pos (xI q) p); auto.
apply in_elements_rec_in_acc; auto.
replace (inv_pos q (xO p)) with (inv_pos (xO q) p); auto.
discriminate H.
Qed.
Lemma in_elements_rec_in_dom_inv :
forall (t1 : tree _) p' a' q acc,
In (p',a') (elements_rec t1 q acc) ->
(exists p, p' = (inv_pos q p)
/\ apply_tree _ None t1 p = Some a')
\/ In (p',a') acc.
Proof.
induction t1 as [| a t0 IHt0 t2 IHt2]; simpl in |- *;
intros.
intuition.
destruct a.
destruct (IHt2 _ _ _ _ H).
destruct H0 as [p [H1 H2]].
left; exists (xI p); split; auto.
destruct H0.
injection H0; intros.
left; exists xH; intuition.
subst; auto.
destruct (IHt0 _ _ _ _ H0); clear H0.
destruct H1 as [p [H3 H2]].
left; exists (xO p); split; auto.
auto.
destruct (IHt2 _ _ _ _ H).
destruct H0 as [p [H1 H2]].
left; exists (xI p); split; auto.
destruct (IHt0 _ _ _ _ H0); clear H0.
destruct H1 as [p [H3 H2]].
left; exists (xO p); split; auto.
auto.
Qed.
Definition elements (t : tree (option A)) := elements_rec t xH nil.
Lemma in_elements_prop1 : forall t1 p a,
apply_tree _ None t1 p = Some a ->
In (p,a) (elements t1).
Proof.
unfold elements; intros.
replace p with (inv_pos 1 p); auto.
apply in_elements_rec_in_dom; auto.
Qed.
Lemma in_elements_prop2 : forall t1 p a,
In (p,a) (elements t1) ->
apply_tree _ None t1 p = Some a.
Proof.
unfold elements; intros.
destruct (in_elements_rec_in_dom_inv _ _ _ _ _ H).
destruct H0 as [q [H1 H2]].
simpl in H1.
subst; auto.
elim H0.
Qed.
Variable f : positive -> A -> B -> B.
Fixpoint fold_rec (t : tree (option A)) (p:positive) (acc:B) {struct t} : B :=
match t with
| leaf _ => acc
| node _ None tO tI =>
fold_rec tI (xI p) (fold_rec tO (xO p) acc)
| node _ (Some a) tO tI =>
fold_rec tI (xI p) (f (inv_pos p xH) a (fold_rec tO (xO p) acc))
end.
Let f' := (fun (pa : positive * A) (b0 : B) => let (p0, a) := pa in f p0 a b0).
Lemma fold_rec_prop : forall t l b1 b2 p,
fold_right f' b1 l = b2 ->
fold_right f' b1 (elements_rec t p l)= fold_rec t p b2.
Proof.
induction t; intros; simpl; auto.
destruct a; auto.
apply IHt2; simpl.
replace (fold_rec t1 (xO p) b2)
with (fold_right f' b1 (elements_rec t1 (xO p) l)); auto.
Qed.
Definition fold (t : tree (option A)) b :=
fold_rec t xH b.
Lemma fold_prop : forall t b,
fold_right f' b (elements t)= fold t b.
Proof.
unfold fold, elements; intros.
apply fold_rec_prop; auto.
Qed.
End dom.
Section compare.
Variable A : Type.
Variable test2 : option A -> option A -> bool.
Section forall_test.
Variable test1 : positive -> option A -> bool.
Definition forall_test (t1 : tree (option A)) : bool :=
fold _ _ (fun p a b => test1 p (Some a) && b) t1 true.
Lemma forall_test_fold_right_correct : forall l,
if fold_right
(fun (pa:positive*A) b0 =>let (p0, a) := pa in test1 p0 (Some a) && b0) true
l
then forall p a, In (p,a) l -> test1 p (Some a) = true
else ~ forall p a, In (p,a) l -> test1 p (Some a) = true.
Proof.
induction l; simpl.
intuition.
destruct a as [p a].
case_eq (test1 p (Some a)); simpl; intros.
destruct (fold_right
(fun (pa : positive * A) (b0 : bool) =>
let (p0, a) := pa in test1 p0 (Some a) && b0) true l); simpl in *; intros.
destruct H0; auto.
inversion H0; subst; auto.
intro; elim IHl; clear IHl; intros; auto.
intro H1; rewrite H1 in H; auto; discriminate.
Qed.
Lemma forall_test_correct : forall t1,
(forall k, test1 k None = true) ->
if forall_test t1
then forall k, test1 k (apply_tree _ None t1 k) = true
else ~ forall k, test1 k (apply_tree _ None t1 k) = true.
Proof.
unfold forall_test; intros.
rewrite <- fold_prop.
generalize (forall_test_fold_right_correct (elements A t1)).
destruct (fold_right
(fun (pa : positive * A) (b0 : bool) =>
let (p0, a) := pa in test1 p0 (Some a) && b0) true
(elements A t1)); intros.
case_eq (apply_tree (option A) None t1 k); simpl; auto.
intros; apply H0.
eapply in_elements_prop1; eauto.
intro; elim H0; clear H0; intros.
rewrite <- (in_elements_prop2 _ _ _ _ H0); auto.
Qed.
End forall_test.
Fixpoint compare (t1 t2 : tree (option A)) {struct t1} : bool :=
match t1,t2 with
| leaf _,leaf _ => true
| leaf _,_ => forall_test (fun p => test2 None) t2
| node _ _ _ _,leaf _ => forall_test (fun p x => test2 x None) t1
| node _ a1 l1 r1, node _ a2 l2 r2 =>
test2 a1 a2 && compare l1 l2 && compare r1 r2
end.
Lemma compare_correct : forall t1 t2,
test2 None None = true ->
if compare t1 t2
then forall k, test2 (apply_tree _ None t1 k) (apply_tree _ None t2 k) = true
else ~ forall k, test2 (apply_tree _ None t1 k) (apply_tree _ None t2 k) = true.
Proof.
induction t1; destruct t2; simpl; intros; auto.
generalize (forall_test_correct (fun _ => test2 None)
(node (option A) o t2_1 t2_2));
destruct (forall_test (fun _ => test2 None)
(node (option A) o t2_1 t2_2)); intros.
destruct k.
apply H0 with (k:=xI k); auto.
apply H0 with (k:=xO k); auto.
apply H0 with (k:=xH); auto.
intro; elim H0; auto; clear H0.
generalize (forall_test_correct (fun _ x => test2 x None)
(node (option A) a t1_1 t1_2));
destruct (forall_test (fun _ x => test2 x None)
(node (option A) a t1_1 t1_2)); intros.
destruct k.
apply H0 with (k:=xI k); auto.
apply H0 with (k:=xO k); auto.
apply H0 with (k:=xH); auto.
intro; elim H0; auto; clear H0.
case_eq (test2 a o); simpl; intros.
generalize (IHt1_1 t2_1); destruct (compare t1_1 t2_1); simpl; intros; clear IHt1_1.
generalize (IHt1_2 t2_2); destruct (compare t1_2 t2_2); simpl; intros; clear IHt1_2.
destruct k; auto.
intro; elim H2; clear H2; intros; auto.
apply H3 with (k:=xI k); auto.
intro; elim H1; clear H1; intros; auto.
apply H2 with (k:=xO k); auto.
intros H1; rewrite (H1 xH) in H0; discriminate.
Qed.
End compare.
Lemma positive_dec: forall p1 p2:positive, {p1=p2}+{p1<>p2}.
Proof.
decide equality.
Qed.
Module BinMap_Base <: MAP_BASE with Definition key := positive.
Definition t A := tree (option A).
Definition key := positive.
Definition eq_key := Peq.
Lemma eq_key_spec : forall x y, if eq_key x y then x = y else x <> y.
Proof. exact Peq_spec. Qed.
Lemma key_dec : forall k1 k2:key, k1=k2 \/ ~k1=k2.
Proof.
intros k1 k2;generalize (eq_key_spec k1 k2);destruct (eq_key k1 k2);auto.
Qed.
Definition get (A:Type) := apply_tree (option A) None.
Definition modify (A:Type) := modify_tree (option A) None.
Lemma get_modify1 : forall A t k f,
get A (modify A t k f) k = f (get A t k).
Proof.
intros; unfold get, modify; rewrite apply_modify_tree1; auto.
Qed.
Lemma get_modify2 : forall A t k1 k2 f,
k1 <> k2 ->
get A (modify A t k2 f) k1 = get A t k1.
Proof.
intros; unfold get, modify; rewrite apply_modify_tree2; auto.
Qed.
Definition empty (A:Type) := leaf (option A).
Lemma get_empty : forall A k, get A (empty A) k = None.
Proof.
unfold empty, get; simpl; intros; reflexivity.
Qed.
Definition elements : forall A, t A -> list (key*A) :=
elements.
Lemma in_elements_get_some : forall A m p a,
In (p,a) (elements A m) -> get A m p = Some a.
Proof in_elements_prop2.
Lemma get_some_in_elements : forall A m p a,
get A m p = Some a -> In (p,a) (elements A m).
Proof in_elements_prop1.
Definition fold : forall (A B:Type),
(key->A->B->B) -> t A -> B -> B := fold.
Lemma fold_prop : forall (A B:Type) f t b,
fold_right (fun (pa : key * A) (b0 : B) => let (p0, a) := pa in f p0 a b0)
b (elements A t) = fold A B f t b.
Proof fold_prop.
Definition compare : forall A, (option A->option A->bool) -> t A -> t A -> bool :=
compare.
Lemma compare_correct : forall A f t1 t2,
f None None = true ->
if compare A f t1 t2
then forall k, f (get A t1 k) (get A t2 k) = true
else ~ forall k, f (get A t1 k) (get A t2 k) = true.
Proof compare_correct.
End BinMap_Base.
Module BinMap <: MAP with Definition key := positive :=
Map_Of_MapBase BinMap_Base.
Module BinNatMap_Base <: MAP_BASE with Definition key := N.
Definition t A := (option A*BinMap_Base.t A)%type.
Definition key := N.
Definition eq_key := Neq.
Lemma eq_key_spec : forall k1 k2, if eq_key k1 k2 then k1 = k2 else k1 <> k2.
Proof. exact Neq_spec. Qed.
Lemma key_dec : forall k1 k2:key, k1=k2 \/ ~k1=k2.
Proof.
intros k1 k2;generalize (eq_key_spec k1 k2);destruct (eq_key k1 k2);auto.
Qed.
Definition get (A:Type)(m:t A)(k:key) :=
match k with
N0 => fst m
| Npos p => BinMap_Base.get _ (snd m) p
end.
Definition modify (A:Type)(m:t A)(k:key)(f:option A->option A) :=
let (v',m') := m in
match k with
N0 => (f v',m')
| Npos p => (v',BinMap_Base.modify _ m' p f)
end.
Lemma get_modify1 : forall A t k f,
get A (modify A t k f) k = f (get A t k).
Proof.
destruct k; destruct t0; simpl; intros; auto.
apply BinMap_Base.get_modify1.
Qed.
Lemma get_modify2 : forall A t k1 k2 f,
k1 <> k2 ->
get A (modify A t k2 f) k1 = get A t k1.
Proof.
destruct k1; destruct k2; destruct t0; simpl; intros; auto.
elim H; auto.
apply BinMap_Base.get_modify2.
intro; elim H; subst; auto.
Qed.
Definition empty (A:Type) := (None (A:=A),BinMap_Base.empty A).
Lemma get_empty : forall A k, get A (empty A) k = None.
Proof.
unfold empty, get; destruct k; intros; simpl;trivial.
Qed.
Definition f' (A:Type) := (fun pa:positive*A => let (p,a) := pa in (Npos p,a)).
Definition elements : forall A, t A -> list (key*A) :=
fun A m => let (v,m) := m in
match v with
None => map (f' A) (BinMap_Base.elements _ m)
| Some a => (N0,a)::(map (f' A) (BinMap_Base.elements _ m))
end.
Lemma in_elements_get_some : forall A m p a,
In (p,a) (elements A m) -> get A m p = Some a.
Proof.
unfold elements, get; intros.
destruct m; destruct p; simpl in *.
destruct o.
destruct H.
injection H; intros; subst; auto.
destruct (in_map_inv _ _ _ _ _ H); clear H.
destruct H0; unfold f' in *.
destruct x; discriminate H.
destruct (in_map_inv _ _ _ _ _ H); clear H.
destruct H0; unfold f' in *.
destruct x; discriminate H.
destruct o.
destruct H.
discriminate H.
destruct (in_map_inv _ _ _ _ _ H); clear H.
destruct H0; unfold f' in *.
destruct x; injection H; clear H; intros; subst.
apply BinMap_Base.in_elements_get_some; auto.
destruct (in_map_inv _ _ _ _ _ H); clear H.
destruct H0; unfold f' in *.
destruct x; injection H; clear H; intros; subst.
apply BinMap_Base.in_elements_get_some; auto.
Qed.
Lemma get_some_in_elements : forall A m p a,
get A m p = Some a -> In (p,a) (elements A m).
Proof.
unfold get, elements; intros.
destruct p; destruct m; simpl in *; subst.
left; auto.
destruct o; try right;
apply (in_map (f' A) (BinMap_Base.elements _ t0) (p,a));
apply BinMap_Base.get_some_in_elements; auto.
Qed.
Definition fold (A B:Type) (f:key->A->B->B) (m:t A) (b:B) : B :=
let (v,m) := m in
match v with
None => BinMap_Base.fold _ _ (fun p a b => f (Npos p) a b) m b
| Some v => f N0 v (BinMap_Base.fold _ _ (fun p a b => f (Npos p) a b) m b)
end.
Lemma fold_prop : forall (A B:Type) f t b,
fold_right (fun (pa : key * A) (b0 : B) => let (p0, a) := pa in f p0 a b0)
b (elements A t) = fold A B f t b.
Proof.
unfold fold, elements; intros.
destruct t0.
destruct o; simpl;
try apply f_equal with (f := (f N0 a));
rewrite <- BinMap_Base.fold_prop;
rewrite fold_right_map; apply fold_right_eq;
intros (p,a'); simpl; auto.
Qed.
Definition compare : forall A, (option A->option A->bool) -> t A -> t A -> bool :=
fun A f m1 m2 =>
let (a1,m1) := m1 in
let (a2,m2) := m2 in
f a1 a2 && BinMap_Base.compare A f m1 m2.
Lemma compare_correct : forall A f t1 t2,
f None None = true ->
if compare A f t1 t2
then forall k, f (get A t1 k) (get A t2 k) = true
else ~ forall k, f (get A t1 k) (get A t2 k) = true.
Proof.
destruct t1; destruct t2; simpl.
case_eq (f o o0); simpl.
generalize (BinMap_Base.compare_correct _ f t0 t1);
destruct (BinMap_Base.compare _ f t0 t1); simpl; intros.
destruct k; simpl; auto.
intro; elim H; clear H; intros; auto.
apply (H2 (Npos k)).
red; intros.
generalize (H1 N0); simpl; intro.
congruence.
Qed.
End BinNatMap_Base.
Module BinNatMap <: MAP with Definition key := N :=
Map_Of_MapBase BinNatMap_Base.
Module MapPair_Base (M1 M2:MAP_BASE) <: MAP_BASE
with Definition key := (M1.key * M2.key)%type.
Definition t (A:Type) := M1.t (M2.t A).
Definition key := (M1.key*M2.key)%type.
Definition get (A:Type)(m:t A)(k:key) :=
match (M1.get m (fst k)) with
None => None
| Some m => M2.get m (snd k)
end.
Definition modify (A:Type)(m:t A)(k:key)(f:option A->option A) :=
M1.modify m (fst k)
(fun m => match m with
None => Some (M2.modify (@M2.empty _) (snd k) f)
| Some m => Some (M2.modify m (snd k) f)
end).
Definition empty (A:Type) := M1.empty (M2.t A).
Lemma get_modify1 : forall A t k f,
get A (modify A t k f) k = f (get A t k).
Proof.
intros; unfold get, modify.
rewrite M1.get_modify1.
destruct (M1.get t0 (fst k));
rewrite M2.get_modify1; auto.
rewrite M2.get_empty; auto.
Qed.
Lemma get_modify2 : forall A t k1 k2 f,
k1 <> k2 ->
get A (modify A t k2 f) k1 = get A t k1.
Proof.
intros; unfold get, modify.
destruct (M1.key_dec (fst k1) (fst k2)).
rewrite H0.
rewrite M1.get_modify1; auto.
destruct (M1.get t0 (fst k2));
rewrite M2.get_modify2; auto.
intro; elim H.
destruct k1; destruct k2; simpl in *; congruence.
rewrite M2.get_empty; auto.
intro; elim H.
destruct k1; destruct k2; simpl in *; congruence.
rewrite M1.get_modify2; auto.
Qed.
Lemma get_empty : forall A k, get A (empty A) k = None.
Proof.
intros; unfold get, empty.
rewrite M1.get_empty.
reflexivity.
Qed.
Definition eq_key := prod_eq _ M1.eq_key _ M2.eq_key.
Lemma eq_key_spec : forall k1 k2, if eq_key k1 k2 then k1 = k2 else k1 <> k2.
Proof.
exact (prod_eq_spec _ M1.eq_key M1.eq_key_spec _ M2.eq_key M2.eq_key_spec).
Qed.
Lemma key_dec : forall k1 k2:key, k1=k2 \/ ~k1=k2.
Proof. exact (Aeq_dec _ eq_key eq_key_spec). Qed.
Definition elements (A:Type) (m:t A) : list (key*A) :=
flat_map (fun km1:M1.key*_ => let (k1,m) := km1 in
(map (fun ka2:M2.key*A => let (k2,a) := ka2 in ((k1,k2),a)) (M2.elements m)))
(M1.elements m).
Lemma in_elements_get_some : forall A m p a,
In (p,a) (elements A m) -> get A m p = Some a.
Proof.
unfold elements, get; intros.
destruct (in_flat_map_inv _ _ _ _ _ H); clear H.
destruct x; destruct H0.
destruct (in_map_inv _ _ _ _ _ H).
destruct x.
destruct H1; injection H1; intros; subst.
simpl.
rewrite (M1.in_elements_get_some _ _ _ _ H0).
apply (M2.in_elements_get_some _ _ _ _ H2).
Qed.
Lemma get_some_in_elements : forall A m p a,
get A m p = Some a -> In (p,a) (elements A m).
Proof.
unfold elements, get; intros.
generalize (M1.get_some_in_elements _ m (fst p)) H; clear H.
case (M1.get m (fst p)); intros.
apply in_flat_map with (a:=(fst p,t0)); auto.
destruct p; simpl in *.
apply (in_map (fun ka2 : M2.key * A => let (k2, a0) := ka2 in (k, k2, a0))
(M2.elements t0) (k0,a)).
apply M2.get_some_in_elements; auto.
discriminate.
Qed.
Definition fold (A B:Type) (f:key->A->B->B) (m:t A) (b:B) : B :=
M1.fold (fun k1 m b => M2.fold (fun k2 => f (k1,k2)) m b) m b.
Lemma fold_prop : forall (A B:Type) f t b,
fold_right (fun (pa : key * A) (b0 : B) => let (p0, a) := pa in f p0 a b0)
b (elements A t) = fold A B f t b.
Proof.
unfold fold, elements; intros.
rewrite fold_right_flat_map.
rewrite <- M1.fold_prop.
apply fold_right_eq2; intros.
destruct a.
rewrite <- M2.fold_prop.
rewrite fold_right_map; apply fold_right_eq;
intros (p,a'); simpl; auto.
Qed.
Module MM2 := Map_Of_MapBase M2.
Definition compare : forall A, (option A->option A->bool) -> t A -> t A -> bool :=
fun A f =>
M1.compare
(fun m1 m2 =>
match m1 ,m2 with
| None, None => true
| None, Some m2 => MM2.for_all _ (fun _ a => f None (Some a)) m2
| Some m1, None => MM2.for_all _ (fun _ a => f (Some a) None) m1
|Some m1, Some m2 => M2.compare f m1 m2
end).
Lemma compare_correct : forall A f t1 t2,
f None None = true ->
if compare A f t1 t2
then forall k, f (get A t1 k) (get A t2 k) = true
else ~ forall k, f (get A t1 k) (get A t2 k) = true.
Proof.
unfold compare, get; intros.
match goal with
[ |- context[M1.compare ?f ?m1 ?m2] ] =>
generalize (M1.compare_correct _ f m1 m2);
destruct (M1.compare f m1 m2)
end; intros.
destruct k as [k1 k2]; simpl.
generalize (H0 (refl_equal _) k1);
destruct (M1.get t1 k1); destruct (M1.get t2 k1); simpl; intros; auto.
generalize (M2.compare_correct A f t0 t3); rewrite H1; auto.
generalize (MM2.for_all_spec _ (fun (_ : MM2.key) (a : A) => f (Some a) None) t0);
rewrite H1; intros.
generalize (H2 k2); unfold MM2.get; destruct (M2.get t0 k2); simpl; auto.
generalize (MM2.for_all_spec _ (fun (_ : MM2.key) (a : A) => f None (Some a)) t0);
rewrite H1; intros.
generalize (H2 k2); unfold MM2.get; destruct (M2.get t0 k2); simpl; auto.
intro; elim H0; clear H0; intros; auto.
case_eq (M1.get t1 k); case_eq (M1.get t2 k); intros; auto.
generalize (M2.compare_correct A f t3 t0); destruct (M2.compare f t3 t0); auto.
intros H3; elim H3; clear H3; auto; intros.
generalize (H1 (k,k0)); simpl; rewrite H0; rewrite H2; auto.
generalize (MM2.for_all_spec _ (fun (_ : MM2.key) (a : A) => f (Some a) None) t0);
destruct (MM2.for_all _ (fun (_ : MM2.key) (a : A) => f (Some a) None) t0); auto; intros.
elim H3; intros; clear H3.
generalize (H1 (k,k0)); simpl; rewrite H0; rewrite H2.
unfold MM2.get in H4; rewrite H4; auto.
generalize (MM2.for_all_spec _ (fun (_ : MM2.key) (a : A) => f None (Some a)) t0);
destruct (MM2.for_all _ (fun (_ : MM2.key) (a : A) => f None (Some a)) t0); auto; intros.
elim H3; intros; clear H3.
generalize (H1 (k,k0)); simpl; rewrite H0; rewrite H2.
unfold MM2.get in H4; rewrite H4; auto.
Qed.
End MapPair_Base.
Module MapOption_Base (M:MAP_BASE) <: MAP_BASE with Definition key := option M.key.
Definition t A := (option A*M.t A)%type.
Definition key := option M.key.
Definition eq_key x y :=
match x, y with
| None, None => true
| Some x, Some y => M.eq_key x y
| _, _ => false
end.
Lemma eq_key_spec : forall k1 k2, if eq_key k1 k2 then k1 = k2 else k1 <> k2.
Proof.
destruct k1; destruct k2; simpl; try auto || discriminate.
generalize (M.eq_key_spec k k0); destruct (M.eq_key k k0); try congruence.
Qed.
Lemma key_dec : forall k1 k2:key, k1=k2 \/ ~k1=k2.
Proof.
intros k1 k2;generalize (eq_key_spec k1 k2);destruct (eq_key k1 k2);auto.
Qed.
Definition get (A:Type)(m:t A)(k:key) :=
match k with
None => fst m
| Some p => M.get (snd m) p
end.
Definition modify (A:Type)(m:t A)(k:key)(f:option A->option A) :=
let (v',m') := m in
match k with
None => (f v',m')
| Some p => (v',M.modify m' p f)
end.
Lemma get_modify1 : forall A t k f,
get A (modify A t k f) k = f (get A t k).
Proof.
destruct k; destruct t0; simpl; intros; auto.
apply M.get_modify1.
Qed.
Lemma get_modify2 : forall A t k1 k2 f,
k1 <> k2 ->
get A (modify A t k2 f) k1 = get A t k1.
Proof.
destruct k1; destruct k2; destruct t0; simpl; intros; auto.
apply M.get_modify2.
intro; elim H; subst; auto.
elim H; auto.
Qed.
Definition empty (A:Type) := (None (A:=A),M.empty A).
Lemma get_empty : forall A k, get A (empty A) k = None.
Proof.
unfold empty, get; destruct k; intros; simpl; auto.
apply M.get_empty.
Qed.
Definition f' (A:Type) := (fun pa:M.key*A => let (p,a) := pa in (Some p,a)).
Definition elements : forall A, t A -> list (key*A) :=
fun A m => let (v,m) := m in
match v with
None => map (f' A) (M.elements m)
| Some a => (None,a)::(map (f' A) (M.elements m))
end.
Lemma in_elements_get_some : forall A m p a,
In (p,a) (elements A m) -> get A m p = Some a.
Proof.
unfold elements, get; intros.
destruct m; destruct p; simpl in *.
destruct o.
destruct H.
discriminate H.
destruct (in_map_inv _ _ _ _ _ H); clear H.
destruct H0; unfold f' in *.
destruct x.
injection H; intros; subst.
apply M.in_elements_get_some; auto.
destruct (in_map_inv _ _ _ _ _ H); clear H.
destruct H0; unfold f' in *.
destruct x.
injection H; intros; subst.
apply M.in_elements_get_some; auto.
destruct o.
destruct H.
injection H; intros; subst; auto.
destruct (in_map_inv _ _ _ _ _ H); clear H.
destruct H0; unfold f' in *.
destruct x; discriminate.
destruct (in_map_inv _ _ _ _ _ H); clear H.
destruct H0; unfold f' in *.
destruct x; discriminate.
Qed.
Lemma get_some_in_elements : forall A m p a,
get A m p = Some a -> In (p,a) (elements A m).
Proof.
unfold get, elements; intros.
destruct p; destruct m; simpl in *; subst.
destruct o; try right;
apply (in_map (f' A) (M.elements t0) (k,a));
apply M.get_some_in_elements; auto.
left; auto.
Qed.
Definition fold (A B:Type) (f:key->A->B->B) (m:t A) (b:B) : B :=
let (v,m) := m in
match v with
None => M.fold (fun p a b => f (Some p) a b) m b
| Some v => f (@None _) v (M.fold (fun p a b => f (Some p) a b) m b)
end.
Lemma fold_prop : forall (A B:Type) f t b,
fold_right (fun (pa : key * A) (b0 : B) => let (p0, a) := pa in f p0 a b0)
b (elements A t) = fold A B f t b.
Proof.
unfold fold, elements; intros.
destruct t0.
destruct o; simpl;
try apply f_equal with (f := (f None a));
rewrite <- M.fold_prop;
rewrite fold_right_map; apply fold_right_eq;
intros (p,a'); simpl; auto.
Qed.
Definition compare : forall A, (option A->option A->bool) -> t A -> t A -> bool :=
fun A f m1 m2 =>
let (a1,m1) := m1 in
let (a2,m2) := m2 in
f a1 a2 && M.compare f m1 m2.
Lemma compare_correct : forall A f t1 t2,
f None None = true ->
if compare A f t1 t2
then forall k, f (get A t1 k) (get A t2 k) = true
else ~ forall k, f (get A t1 k) (get A t2 k) = true.
Proof.
destruct t1; destruct t2; simpl.
case_eq (f o o0); simpl.
generalize (M.compare_correct _ f t0 t1);
destruct (M.compare f t0 t1); simpl; intros.
destruct k; simpl; auto.
intro; elim H; clear H; intros; auto.
apply (H2 (Some k)).
red; intros.
generalize (H1 None); simpl; intro.
congruence.
Qed.
End MapOption_Base.
Module Type HASH_TYPE.
Parameter t : Set.
Parameter eq_t : t -> t -> bool.
Parameter eq_t_spec : forall t1 t2, if eq_t t1 t2 then t1 = t2 else t1 <> t2.
Parameter eq_dec : forall x y:t, x=y \/ ~x=y.
Parameter key : Set.
Parameter hash : t -> key.
End HASH_TYPE.
Module MapHash_Base
(H:HASH_TYPE) (M:MAP_BASE with Definition key := H.key) <:
MAP_BASE with Definition key := H.t.
Record hashtab (A:Type) : Type := mk_hashtab {
tab :> M.t (dlist H.t A);
tab_inv :> forall k l, M.get tab k = Some l ->
forall x a, In (x,a) l -> H.hash x = k
}.
Definition t A := hashtab A.
Definition key := H.t.
Definition eq_key := H.eq_t.
Definition eq_key_spec := H.eq_t_spec.
Lemma key_dec : forall k1 k2:key, k1=k2 \/ ~k1=k2.
Proof H.eq_dec.
Definition get (A:Type) (m:t A) (k:key) : option A :=
match (M.get m (H.hash k)) with
None => None
| Some l => dassoc H.eq_t k l
end.
Definition modify_aux (A:Type) (m:t A) (k:key) (f:option A->option A) : M.t (dlist H.t A) :=
M.modify m (H.hash k)
(fun ol => match ol with
None =>
match f None with
None => Some (@dnil _ _)
| Some b => Some (dmodify_assoc H.eq_t H.eq_t_spec k (@dnil _ _) (fun _ => Some b))
end
| Some l => Some (dmodify_assoc H.eq_t H.eq_t_spec k l f)
end).
Lemma modify_aux_prop : forall A m k f,
forall (k0 : M.key) (l : dlist H.t A),
M.get (modify_aux A m k f) k0 = Some l ->
forall (x : H.t) (a : A), In (x, a) l -> H.hash x = k0.
intros A m k f; unfold modify_aux; intros k0 l.
destruct (M.key_dec (H.hash k) k0).
rewrite H.
rewrite M.get_modify1.
destruct m as [m Hm]; simpl in *.
generalize (Hm k0); clear Hm.
destruct (M.get m k0); intros.
inversion_clear H1 in H2.
generalize (in_dassoc_prop1 _ _ H.eq_t H.eq_t_spec _ _ _ H2); intros.
destruct (H.eq_dec k x); subst; auto.
rewrite assoc_dmodify_assoc2 in H1; auto.
generalize (in_dassoc_prop2 _ _ H.eq_t H.eq_t_spec _ _ _ H1); intros.
apply H0 with d a; auto.
clear H0; destruct (f None).
inversion_clear H1 in H2.
simpl in H2.
destruct H2.
injection H0; intros; subst; auto.
elim H0.
inversion_clear H1 in H2; inversion_clear H2.
rewrite M.get_modify2; auto.
destruct m; simpl in *; eauto.
Qed.
Definition modify (A:Type) (m:t A) (k:key) (f:option A->option A) : t A :=
mk_hashtab _ (modify_aux A m k f) (modify_aux_prop A m k f).
Lemma get_modify1 : forall A t k f,
get A (modify A t k f) k = f (get A t k).
Proof.
intros; unfold get, modify, modify_aux; simpl.
rewrite M.get_modify1; auto.
destruct (M.get t0 (H.hash k)).
apply assoc_dmodify_assoc1.
destruct f; simpl; auto.
rewrite assoc_dmodify_assoc1; auto.
Qed.
Lemma get_modify2 : forall A t k1 k2 f,
k1 <> k2 ->
get A (modify A t k2 f) k1 = get A t k1.
Proof.
intros; unfold get, modify, modify_aux; simpl.
destruct (M.key_dec (H.hash k1) (H.hash k2)).
rewrite H0; rewrite M.get_modify1.
destruct (M.get t0 (H.hash k2)); simpl.
apply assoc_dmodify_assoc2; auto.
destruct f; simpl; auto.
rewrite assoc_dmodify_assoc2; auto.
rewrite M.get_modify2; auto.
Qed.
Lemma empty_prop : forall A,
forall (k0 : M.key) (l : dlist H.t A),
M.get (@M.empty _) k0 = Some l ->
forall (x : H.t) (a : A), In (x, a) l -> H.hash x = k0.
Proof.
intros.
rewrite M.get_empty in H.
discriminate H.
Qed.
Definition empty (A:Type) : t A := mk_hashtab _ (@M.empty _) (empty_prop _).
Lemma get_empty : forall A k, get A (empty A) k = None.
Proof.
unfold empty, get; simpl; intros.
rewrite M.get_empty; auto.
Qed.
Definition elements A (m:t A) : list (key*A) :=
flat_map (fun c => dlist_list _ _ (snd c)) (M.elements m).
Lemma in_elements_get_some : forall A m p a,
In (p,a) (elements A m) -> get A m p = Some a.
Proof.
unfold elements, get; intros.
destruct (in_flat_map_inv _ _ _ _ _ H).
rewrite (M.in_elements_get_some _ m (H.hash p) (snd x)).
apply in_dassoc_prop1; intuition.
exact (H.eq_t_spec x0 y).
destruct m; simpl in *.
assert (H.hash p = fst x).
apply tab_inv0 with (snd x) a; intuition.
destruct x; simpl in *.
apply M.in_elements_get_some; auto.
rewrite H1; destruct x; intuition.
Qed.
Lemma get_some_in_elements : forall A m p a,
get A m p = Some a -> In (p,a) (elements A m).
Proof.
unfold elements, get; intros.
generalize (refl_equal (M.get m (H.hash p)));
pattern (M.get m (H.hash p)) at -1;
case (M.get m (H.hash p)); intros.
rewrite H0 in H.
apply in_flat_map with (H.hash p,d); simpl.
eapply in_dassoc_prop2; eauto.
exact H.eq_t_spec.
apply M.get_some_in_elements; auto.
rewrite H0 in H; discriminate.
Qed.
Definition fold (A B:Type)(f:key->A->B->B) (m:t A) (b:B) :B :=
fold_right (fun (pa : key * A) (b0 : B) => let (p0, a) := pa in f p0 a b0)
b (elements _ m).
Lemma fold_prop : forall (A B:Type) f t b,
fold_right (fun (pa : key * A) (b0 : B) => let (p0, a) := pa in f p0 a b0)
b (elements A t) = fold A B f t b.
Proof.
reflexivity.
Qed.
Definition compare : forall A, (option A->option A->bool) -> t A -> t A -> bool :=
fun A f =>
M.compare
(fun ol1 ol2 =>
match ol1 ,ol2 with
| None, None => true
| None, Some l2 => ListAux.for_all _ (fun p => f None (Some (snd p))) l2
| Some l1, None => ListAux.for_all _ (fun p => f (Some (snd p)) None) l1
| Some l1, Some l2 => ListAux.compare _ _ H.eq_t f l1 l2
end).
Lemma compare_correct : forall A f t1 t2,
f None None = true ->
if compare A f t1 t2
then forall k, f (get A t1 k) (get A t2 k) = true
else ~ forall k, f (get A t1 k) (get A t2 k) = true.
Proof.
unfold compare, get; intros.
match goal with
[ |- if M.compare ?f ?t1 ?t2 then _ else _ ] =>
generalize (M.compare_correct _ f t1 t2);
destruct (M.compare f t1 t2)
end; intros.
generalize (H0 (refl_equal _) (H.hash k)); clear H0; intros.
destruct (M.get t1 (H.hash k)).
destruct (M.get t2 (H.hash k)).
generalize (ListAux.compare_correct H.t A H.eq_t H.eq_t_spec f d d0); rewrite H0; auto.
generalize (ListAux.for_all_correct _ (fun p : H.t * A => f (Some (snd p)) None) d);
rewrite H0; intros.
case_eq (dassoc H.eq_t k d); intros; auto.
apply (H1 (k,a)).
eapply ListAux.in_dassoc_prop2; eauto.
apply H.eq_t_spec.
destruct (M.get t2 (H.hash k)); auto.
generalize (ListAux.for_all_correct _ (fun p : H.t * A => f None (Some (snd p))) d);
rewrite H0; intros.
case_eq (dassoc H.eq_t k d); intros; auto.
apply (H1 (k,a)).
eapply ListAux.in_dassoc_prop2; eauto.
apply H.eq_t_spec.
intro; elim H0; auto; clear H0; intros.
case_eq (M.get t1 k); intros;
case_eq (M.get t2 k); intros.
generalize (ListAux.compare_correct H.t A H.eq_t H.eq_t_spec f d d0);
destruct (ListAux.compare H.t A H.eq_t f d d0); auto; intros.
elim H3; clear H3; intros; auto.
case_eq (dassoc H.eq_t k0 d); intros.
assert (H.hash k0 = k).
apply (tab_inv _ t1 k d H0) with a.
eapply ListAux.in_dassoc_prop2; eauto.
apply H.eq_t_spec.
generalize (H1 k0); rewrite H4; clear H1; rewrite H0; rewrite H2.
rewrite H3; auto.