-
Notifications
You must be signed in to change notification settings - Fork 0
/
Maps.v
1731 lines (1522 loc) · 54.7 KB
/
Maps.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
(* *********************************************************************)
(* *)
(* The Compcert verified compiler *)
(* *)
(* Xavier Leroy, INRIA Paris-Rocquencourt *)
(* *)
(* Copyright Institut National de Recherche en Informatique et en *)
(* Automatique. All rights reserved. This file is distributed *)
(* under the terms of the GNU General Public License as published by *)
(* the Free Software Foundation, either version 2 of the License, or *)
(* (at your option) any later version. This file is also distributed *)
(* under the terms of the INRIA Non-Commercial License Agreement. *)
(* *)
(* *********************************************************************)
(** Applicative finite maps are the main data structure used in this
project. A finite map associates data to keys. The two main operations
are [set k d m], which returns a map identical to [m] except that [d]
is associated to [k], and [get k m] which returns the data associated
to key [k] in map [m]. In this library, we distinguish two kinds of maps:
- Trees: the [get] operation returns an option type, either [None]
if no data is associated to the key, or [Some d] otherwise.
- Maps: the [get] operation always returns a data. If no data was explicitly
associated with the key, a default data provided at map initialization time
is returned.
In this library, we provide efficient implementations of trees and
maps whose keys range over the type [positive] of binary positive
integers or any type that can be injected into [positive]. The
implementation is based on radix-2 search trees (uncompressed
Patricia trees) and guarantees logarithmic-time operations. An
inefficient implementation of maps as functions is also provided.
*)
Require Import Equivalence EquivDec.
Require Import Coqlib.
(* To avoid useless definitions of inductors in extracted code. *)
Local Unset Elimination Schemes.
Local Unset Case Analysis Schemes.
Set Implicit Arguments.
(** * The abstract signatures of trees *)
Module Type TREE.
Parameter elt: Type.
Parameter elt_eq: forall (a b: elt), {a = b} + {a <> b}.
Parameter t: Type -> Type.
Parameter empty: forall (A: Type), t A.
Parameter get: forall (A: Type), elt -> t A -> option A.
Parameter set: forall (A: Type), elt -> A -> t A -> t A.
Parameter remove: forall (A: Type), elt -> t A -> t A.
(** The ``good variables'' properties for trees, expressing
commutations between [get], [set] and [remove]. *)
Axiom gempty:
forall (A: Type) (i: elt), get i (empty A) = None.
Axiom gss:
forall (A: Type) (i: elt) (x: A) (m: t A), get i (set i x m) = Some x.
Axiom gso:
forall (A: Type) (i j: elt) (x: A) (m: t A),
i <> j -> get i (set j x m) = get i m.
Axiom gsspec:
forall (A: Type) (i j: elt) (x: A) (m: t A),
get i (set j x m) = if elt_eq i j then Some x else get i m.
(* We could implement the following, but it's not needed for the moment.
Hypothesis gsident:
forall (A: Type) (i: elt) (m: t A) (v: A),
get i m = Some v -> set i v m = m.
Hypothesis grident:
forall (A: Type) (i: elt) (m: t A) (v: A),
get i m = None -> remove i m = m.
*)
Axiom grs:
forall (A: Type) (i: elt) (m: t A), get i (remove i m) = None.
Axiom gro:
forall (A: Type) (i j: elt) (m: t A),
i <> j -> get i (remove j m) = get i m.
Axiom grspec:
forall (A: Type) (i j: elt) (m: t A),
get i (remove j m) = if elt_eq i j then None else get i m.
(** Extensional equality between trees. *)
Parameter beq: forall (A: Type), (A -> A -> bool) -> t A -> t A -> bool.
Axiom beq_correct:
forall (A: Type) (eqA: A -> A -> bool) (t1 t2: t A),
beq eqA t1 t2 = true <->
(forall (x: elt),
match get x t1, get x t2 with
| None, None => True
| Some y1, Some y2 => eqA y1 y2 = true
| _, _ => False
end).
(** Applying a function to all data of a tree. *)
Parameter map:
forall (A B: Type), (elt -> A -> B) -> t A -> t B.
Axiom gmap:
forall (A B: Type) (f: elt -> A -> B) (i: elt) (m: t A),
get i (map f m) = option_map (f i) (get i m).
(** Same as [map], but the function does not receive the [elt] argument. *)
Parameter map1:
forall (A B: Type), (A -> B) -> t A -> t B.
Axiom gmap1:
forall (A B: Type) (f: A -> B) (i: elt) (m: t A),
get i (map1 f m) = option_map f (get i m).
(** Applying a function pairwise to all data of two trees. *)
Parameter combine:
forall (A B C: Type), (option A -> option B -> option C) -> t A -> t B -> t C.
Axiom gcombine:
forall (A B C: Type) (f: option A -> option B -> option C),
f None None = None ->
forall (m1: t A) (m2: t B) (i: elt),
get i (combine f m1 m2) = f (get i m1) (get i m2).
(** Enumerating the bindings of a tree. *)
Parameter elements:
forall (A: Type), t A -> list (elt * A).
Axiom elements_correct:
forall (A: Type) (m: t A) (i: elt) (v: A),
get i m = Some v -> In (i, v) (elements m).
Axiom elements_complete:
forall (A: Type) (m: t A) (i: elt) (v: A),
In (i, v) (elements m) -> get i m = Some v.
Axiom elements_keys_norepet:
forall (A: Type) (m: t A),
list_norepet (List.map (@fst elt A) (elements m)).
Axiom elements_extensional:
forall (A: Type) (m n: t A),
(forall i, get i m = get i n) ->
elements m = elements n.
Axiom elements_remove:
forall (A: Type) i v (m: t A),
get i m = Some v ->
exists l1 l2, elements m = l1 ++ (i,v) :: l2 /\ elements (remove i m) = l1 ++ l2.
(** Folding a function over all bindings of a tree. *)
Parameter fold:
forall (A B: Type), (B -> elt -> A -> B) -> t A -> B -> B.
Axiom fold_spec:
forall (A B: Type) (f: B -> elt -> A -> B) (v: B) (m: t A),
fold f m v =
List.fold_left (fun a p => f a (fst p) (snd p)) (elements m) v.
(** Same as [fold], but the function does not receive the [elt] argument. *)
Parameter fold1:
forall (A B: Type), (B -> A -> B) -> t A -> B -> B.
Axiom fold1_spec:
forall (A B: Type) (f: B -> A -> B) (v: B) (m: t A),
fold1 f m v =
List.fold_left (fun a p => f a (snd p)) (elements m) v.
End TREE.
(** * The abstract signatures of maps *)
Module Type MAP.
Parameter elt: Type.
Parameter elt_eq: forall (a b: elt), {a = b} + {a <> b}.
Parameter t: Type -> Type.
Parameter init: forall (A: Type), A -> t A.
Parameter get: forall (A: Type), elt -> t A -> A.
Parameter set: forall (A: Type), elt -> A -> t A -> t A.
Axiom gi:
forall (A: Type) (i: elt) (x: A), get i (init x) = x.
Axiom gss:
forall (A: Type) (i: elt) (x: A) (m: t A), get i (set i x m) = x.
Axiom gso:
forall (A: Type) (i j: elt) (x: A) (m: t A),
i <> j -> get i (set j x m) = get i m.
Axiom gsspec:
forall (A: Type) (i j: elt) (x: A) (m: t A),
get i (set j x m) = if elt_eq i j then x else get i m.
Axiom gsident:
forall (A: Type) (i j: elt) (m: t A), get j (set i (get i m) m) = get j m.
Parameter map: forall (A B: Type), (A -> B) -> t A -> t B.
Axiom gmap:
forall (A B: Type) (f: A -> B) (i: elt) (m: t A),
get i (map f m) = f(get i m).
End MAP.
(** * An implementation of trees over type [positive] *)
Module PTree <: TREE.
Definition elt := positive.
Definition elt_eq := peq.
Inductive tree (A : Type) : Type :=
| Leaf : tree A
| Node : tree A -> option A -> tree A -> tree A.
Arguments Leaf [A].
Arguments Node [A] _ _ _.
Scheme tree_ind := Induction for tree Sort Prop.
Definition t := tree.
Definition empty (A : Type) := (Leaf : t A).
Fixpoint get (A : Type) (i : positive) (m : t A) {struct i} : option A :=
match m with
| Leaf => None
| Node l o r =>
match i with
| xH => o
| xO ii => get ii l
| xI ii => get ii r
end
end.
Fixpoint set (A : Type) (i : positive) (v : A) (m : t A) {struct i} : t A :=
match m with
| Leaf =>
match i with
| xH => Node Leaf (Some v) Leaf
| xO ii => Node (set ii v Leaf) None Leaf
| xI ii => Node Leaf None (set ii v Leaf)
end
| Node l o r =>
match i with
| xH => Node l (Some v) r
| xO ii => Node (set ii v l) o r
| xI ii => Node l o (set ii v r)
end
end.
Fixpoint remove (A : Type) (i : positive) (m : t A) {struct i} : t A :=
match i with
| xH =>
match m with
| Leaf => Leaf
| Node Leaf o Leaf => Leaf
| Node l o r => Node l None r
end
| xO ii =>
match m with
| Leaf => Leaf
| Node l None Leaf =>
match remove ii l with
| Leaf => Leaf
| mm => Node mm None Leaf
end
| Node l o r => Node (remove ii l) o r
end
| xI ii =>
match m with
| Leaf => Leaf
| Node Leaf None r =>
match remove ii r with
| Leaf => Leaf
| mm => Node Leaf None mm
end
| Node l o r => Node l o (remove ii r)
end
end.
Theorem gempty:
forall (A: Type) (i: positive), get i (empty A) = None.
Proof.
induction i; simpl; auto.
Qed.
Theorem gss:
forall (A: Type) (i: positive) (x: A) (m: t A), get i (set i x m) = Some x.
Proof.
induction i; destruct m; simpl; auto.
Qed.
Lemma gleaf : forall (A : Type) (i : positive), get i (Leaf : t A) = None.
Proof. exact gempty. Qed.
Theorem gso:
forall (A: Type) (i j: positive) (x: A) (m: t A),
i <> j -> get i (set j x m) = get i m.
Proof.
induction i; intros; destruct j; destruct m; simpl;
try rewrite <- (gleaf A i); auto; try apply IHi; congruence.
Qed.
Theorem gsspec:
forall (A: Type) (i j: positive) (x: A) (m: t A),
get i (set j x m) = if peq i j then Some x else get i m.
Proof.
intros.
destruct (peq i j); [ rewrite e; apply gss | apply gso; auto ].
Qed.
Theorem gsident:
forall (A: Type) (i: positive) (m: t A) (v: A),
get i m = Some v -> set i v m = m.
Proof.
induction i; intros; destruct m; simpl; simpl in H; try congruence.
rewrite (IHi m2 v H); congruence.
rewrite (IHi m1 v H); congruence.
Qed.
Theorem set2:
forall (A: Type) (i: elt) (m: t A) (v1 v2: A),
set i v2 (set i v1 m) = set i v2 m.
Proof.
induction i; intros; destruct m; simpl; try (rewrite IHi); auto.
Qed.
Lemma rleaf : forall (A : Type) (i : positive), remove i (Leaf : t A) = Leaf.
Proof. destruct i; simpl; auto. Qed.
Theorem grs:
forall (A: Type) (i: positive) (m: t A), get i (remove i m) = None.
Proof.
induction i; destruct m.
simpl; auto.
destruct m1; destruct o; destruct m2 as [ | ll oo rr]; simpl; auto.
rewrite (rleaf A i); auto.
cut (get i (remove i (Node ll oo rr)) = None).
destruct (remove i (Node ll oo rr)); auto; apply IHi.
apply IHi.
simpl; auto.
destruct m1 as [ | ll oo rr]; destruct o; destruct m2; simpl; auto.
rewrite (rleaf A i); auto.
cut (get i (remove i (Node ll oo rr)) = None).
destruct (remove i (Node ll oo rr)); auto; apply IHi.
apply IHi.
simpl; auto.
destruct m1; destruct m2; simpl; auto.
Qed.
Theorem gro:
forall (A: Type) (i j: positive) (m: t A),
i <> j -> get i (remove j m) = get i m.
Proof.
induction i; intros; destruct j; destruct m;
try rewrite (rleaf A (xI j));
try rewrite (rleaf A (xO j));
try rewrite (rleaf A 1); auto;
destruct m1; destruct o; destruct m2;
simpl;
try apply IHi; try congruence;
try rewrite (rleaf A j); auto;
try rewrite (gleaf A i); auto.
cut (get i (remove j (Node m2_1 o m2_2)) = get i (Node m2_1 o m2_2));
[ destruct (remove j (Node m2_1 o m2_2)); try rewrite (gleaf A i); auto
| apply IHi; congruence ].
destruct (remove j (Node m1_1 o0 m1_2)); simpl; try rewrite (gleaf A i);
auto.
destruct (remove j (Node m2_1 o m2_2)); simpl; try rewrite (gleaf A i);
auto.
cut (get i (remove j (Node m1_1 o0 m1_2)) = get i (Node m1_1 o0 m1_2));
[ destruct (remove j (Node m1_1 o0 m1_2)); try rewrite (gleaf A i); auto
| apply IHi; congruence ].
destruct (remove j (Node m2_1 o m2_2)); simpl; try rewrite (gleaf A i);
auto.
destruct (remove j (Node m1_1 o0 m1_2)); simpl; try rewrite (gleaf A i);
auto.
Qed.
Theorem grspec:
forall (A: Type) (i j: elt) (m: t A),
get i (remove j m) = if elt_eq i j then None else get i m.
Proof.
intros. destruct (elt_eq i j). subst j. apply grs. apply gro; auto.
Qed.
Section BOOLEAN_EQUALITY.
Variable A: Type.
Variable beqA: A -> A -> bool.
Fixpoint bempty (m: t A) : bool :=
match m with
| Leaf => true
| Node l None r => bempty l && bempty r
| Node l (Some _) r => false
end.
Fixpoint beq (m1 m2: t A) {struct m1} : bool :=
match m1, m2 with
| Leaf, _ => bempty m2
| _, Leaf => bempty m1
| Node l1 o1 r1, Node l2 o2 r2 =>
match o1, o2 with
| None, None => true
| Some y1, Some y2 => beqA y1 y2
| _, _ => false
end
&& beq l1 l2 && beq r1 r2
end.
Lemma bempty_correct:
forall m, bempty m = true <-> (forall x, get x m = None).
Proof.
induction m; simpl.
split; intros. apply gleaf. auto.
destruct o; split; intros.
congruence.
generalize (H xH); simpl; congruence.
destruct (andb_prop _ _ H). rewrite IHm1 in H0. rewrite IHm2 in H1.
destruct x; simpl; auto.
apply andb_true_intro; split.
apply IHm1. intros; apply (H (xO x)).
apply IHm2. intros; apply (H (xI x)).
Qed.
Lemma beq_correct:
forall m1 m2,
beq m1 m2 = true <->
(forall (x: elt),
match get x m1, get x m2 with
| None, None => True
| Some y1, Some y2 => beqA y1 y2 = true
| _, _ => False
end).
Proof.
induction m1; intros.
- simpl. rewrite bempty_correct. split; intros.
rewrite gleaf. rewrite H. auto.
generalize (H x). rewrite gleaf. destruct (get x m2); tauto.
- destruct m2.
+ unfold beq. rewrite bempty_correct. split; intros.
rewrite H. rewrite gleaf. auto.
generalize (H x). rewrite gleaf. destruct (get x (Node m1_1 o m1_2)); tauto.
+ simpl. split; intros.
* destruct (andb_prop _ _ H). destruct (andb_prop _ _ H0).
rewrite IHm1_1 in H3. rewrite IHm1_2 in H1.
destruct x; simpl. apply H1. apply H3.
destruct o; destruct o0; auto || congruence.
* apply andb_true_intro. split. apply andb_true_intro. split.
generalize (H xH); simpl. destruct o; destruct o0; tauto.
apply IHm1_1. intros; apply (H (xO x)).
apply IHm1_2. intros; apply (H (xI x)).
Qed.
End BOOLEAN_EQUALITY.
Fixpoint prev_append (i j: positive) {struct i} : positive :=
match i with
| xH => j
| xI i' => prev_append i' (xI j)
| xO i' => prev_append i' (xO j)
end.
Definition prev (i: positive) : positive :=
prev_append i xH.
Lemma prev_append_prev i j:
prev (prev_append i j) = prev_append j i.
Proof.
revert j. unfold prev.
induction i as [i IH|i IH|]. 3: reflexivity.
intros j. simpl. rewrite IH. reflexivity.
intros j. simpl. rewrite IH. reflexivity.
Qed.
Lemma prev_involutive i :
prev (prev i) = i.
Proof (prev_append_prev i xH).
Lemma prev_append_inj i j j' :
prev_append i j = prev_append i j' -> j = j'.
Proof.
revert j j'.
induction i as [i Hi|i Hi|]; intros j j' H; auto;
specialize (Hi _ _ H); congruence.
Qed.
Fixpoint xmap (A B : Type) (f : positive -> A -> B) (m : t A) (i : positive)
{struct m} : t B :=
match m with
| Leaf => Leaf
| Node l o r => Node (xmap f l (xO i))
(match o with None => None | Some x => Some (f (prev i) x) end)
(xmap f r (xI i))
end.
Definition map (A B : Type) (f : positive -> A -> B) m := xmap f m xH.
Lemma xgmap:
forall (A B: Type) (f: positive -> A -> B) (i j : positive) (m: t A),
get i (xmap f m j) = option_map (f (prev (prev_append i j))) (get i m).
Proof.
induction i; intros; destruct m; simpl; auto.
Qed.
Theorem gmap:
forall (A B: Type) (f: positive -> A -> B) (i: positive) (m: t A),
get i (map f m) = option_map (f i) (get i m).
Proof.
intros A B f i m.
unfold map.
rewrite xgmap. repeat f_equal. exact (prev_involutive i).
Qed.
Fixpoint map1 (A B: Type) (f: A -> B) (m: t A) {struct m} : t B :=
match m with
| Leaf => Leaf
| Node l o r => Node (map1 f l) (option_map f o) (map1 f r)
end.
Theorem gmap1:
forall (A B: Type) (f: A -> B) (i: elt) (m: t A),
get i (map1 f m) = option_map f (get i m).
Proof.
induction i; intros; destruct m; simpl; auto.
Qed.
Definition Node' (A: Type) (l: t A) (x: option A) (r: t A): t A :=
match l, x, r with
| Leaf, None, Leaf => Leaf
| _, _, _ => Node l x r
end.
Lemma gnode':
forall (A: Type) (l r: t A) (x: option A) (i: positive),
get i (Node' l x r) = get i (Node l x r).
Proof.
intros. unfold Node'.
destruct l; destruct x; destruct r; auto.
destruct i; simpl; auto; rewrite gleaf; auto.
Qed.
Fixpoint filter1 (A: Type) (pred: A -> bool) (m: t A) {struct m} : t A :=
match m with
| Leaf => Leaf
| Node l o r =>
let o' := match o with None => None | Some x => if pred x then o else None end in
Node' (filter1 pred l) o' (filter1 pred r)
end.
Theorem gfilter1:
forall (A: Type) (pred: A -> bool) (i: elt) (m: t A),
get i (filter1 pred m) =
match get i m with None => None | Some x => if pred x then Some x else None end.
Proof.
intros until m. revert m i. induction m; simpl; intros.
rewrite gleaf; auto.
rewrite gnode'. destruct i; simpl; auto. destruct o; auto.
Qed.
Section COMBINE.
Variables A B C: Type.
Variable f: option A -> option B -> option C.
Hypothesis f_none_none: f None None = None.
Fixpoint xcombine_l (m : t A) {struct m} : t C :=
match m with
| Leaf => Leaf
| Node l o r => Node' (xcombine_l l) (f o None) (xcombine_l r)
end.
Lemma xgcombine_l :
forall (m: t A) (i : positive),
get i (xcombine_l m) = f (get i m) None.
Proof.
induction m; intros; simpl.
repeat rewrite gleaf. auto.
rewrite gnode'. destruct i; simpl; auto.
Qed.
Fixpoint xcombine_r (m : t B) {struct m} : t C :=
match m with
| Leaf => Leaf
| Node l o r => Node' (xcombine_r l) (f None o) (xcombine_r r)
end.
Lemma xgcombine_r :
forall (m: t B) (i : positive),
get i (xcombine_r m) = f None (get i m).
Proof.
induction m; intros; simpl.
repeat rewrite gleaf. auto.
rewrite gnode'. destruct i; simpl; auto.
Qed.
Fixpoint combine (m1: t A) (m2: t B) {struct m1} : t C :=
match m1 with
| Leaf => xcombine_r m2
| Node l1 o1 r1 =>
match m2 with
| Leaf => xcombine_l m1
| Node l2 o2 r2 => Node' (combine l1 l2) (f o1 o2) (combine r1 r2)
end
end.
Theorem gcombine:
forall (m1: t A) (m2: t B) (i: positive),
get i (combine m1 m2) = f (get i m1) (get i m2).
Proof.
induction m1; intros; simpl.
rewrite gleaf. apply xgcombine_r.
destruct m2; simpl.
rewrite gleaf. rewrite <- xgcombine_l. auto.
repeat rewrite gnode'. destruct i; simpl; auto.
Qed.
End COMBINE.
Lemma xcombine_lr :
forall (A B: Type) (f g : option A -> option A -> option B) (m : t A),
(forall (i j : option A), f i j = g j i) ->
xcombine_l f m = xcombine_r g m.
Proof.
induction m; intros; simpl; auto.
rewrite IHm1; auto.
rewrite IHm2; auto.
rewrite H; auto.
Qed.
Theorem combine_commut:
forall (A B: Type) (f g: option A -> option A -> option B),
(forall (i j: option A), f i j = g j i) ->
forall (m1 m2: t A),
combine f m1 m2 = combine g m2 m1.
Proof.
intros A B f g EQ1.
assert (EQ2: forall (i j: option A), g i j = f j i).
intros; auto.
induction m1; intros; destruct m2; simpl;
try rewrite EQ1;
repeat rewrite (xcombine_lr f g);
repeat rewrite (xcombine_lr g f);
auto.
rewrite IHm1_1.
rewrite IHm1_2.
auto.
Qed.
Fixpoint xelements (A : Type) (m : t A) (i : positive)
(k: list (positive * A)) {struct m}
: list (positive * A) :=
match m with
| Leaf => k
| Node l None r =>
xelements l (xO i) (xelements r (xI i) k)
| Node l (Some x) r =>
xelements l (xO i)
((prev i, x) :: xelements r (xI i) k)
end.
Definition elements (A: Type) (m : t A) := xelements m xH nil.
Remark xelements_append:
forall A (m: t A) i k1 k2,
xelements m i (k1 ++ k2) = xelements m i k1 ++ k2.
Proof.
induction m; intros; simpl.
- auto.
- destruct o; rewrite IHm2; rewrite <- IHm1; auto.
Qed.
Remark xelements_leaf:
forall A i, xelements (@Leaf A) i nil = nil.
Proof.
intros; reflexivity.
Qed.
Remark xelements_node:
forall A (m1: t A) o (m2: t A) i,
xelements (Node m1 o m2) i nil =
xelements m1 (xO i) nil
++ match o with None => nil | Some v => (prev i, v) :: nil end
++ xelements m2 (xI i) nil.
Proof.
intros. simpl. destruct o; simpl; rewrite <- xelements_append; auto.
Qed.
Lemma xelements_incl:
forall (A: Type) (m: t A) (i : positive) k x,
In x k -> In x (xelements m i k).
Proof.
induction m; intros; simpl.
auto.
destruct o.
apply IHm1. simpl; right; auto.
auto.
Qed.
Lemma xelements_correct:
forall (A: Type) (m: t A) (i j : positive) (v: A) k,
get i m = Some v -> In (prev (prev_append i j), v) (xelements m j k).
Proof.
induction m; intros.
rewrite (gleaf A i) in H; congruence.
destruct o; destruct i; simpl; simpl in H.
apply xelements_incl. right. auto.
auto.
inv H. apply xelements_incl. left. reflexivity.
apply xelements_incl. auto.
auto.
inv H.
Qed.
Theorem elements_correct:
forall (A: Type) (m: t A) (i: positive) (v: A),
get i m = Some v -> In (i, v) (elements m).
Proof.
intros A m i v H.
generalize (xelements_correct m i xH nil H). rewrite prev_append_prev. exact id.
Qed.
Lemma in_xelements:
forall (A: Type) (m: t A) (i k: positive) (v: A) ,
In (k, v) (xelements m i nil) ->
exists j, k = prev (prev_append j i) /\ get j m = Some v.
Proof.
induction m; intros.
- rewrite xelements_leaf in H. contradiction.
- rewrite xelements_node in H. rewrite ! in_app_iff in H. destruct H as [P | [P | P]].
+ exploit IHm1; eauto. intros (j & Q & R). exists (xO j); auto.
+ destruct o; simpl in P; intuition auto. inv H. exists xH; auto.
+ exploit IHm2; eauto. intros (j & Q & R). exists (xI j); auto.
Qed.
Theorem elements_complete:
forall (A: Type) (m: t A) (i: positive) (v: A),
In (i, v) (elements m) -> get i m = Some v.
Proof.
unfold elements. intros A m i v H. exploit in_xelements; eauto. intros (j & P & Q).
rewrite prev_append_prev in P. change i with (prev_append 1 i) in P.
exploit prev_append_inj; eauto. intros; congruence.
Qed.
Definition xkeys (A: Type) (m: t A) (i: positive) :=
List.map (@fst positive A) (xelements m i nil).
Remark xkeys_leaf:
forall A i, xkeys (@Leaf A) i = nil.
Proof.
intros; reflexivity.
Qed.
Remark xkeys_node:
forall A (m1: t A) o (m2: t A) i,
xkeys (Node m1 o m2) i =
xkeys m1 (xO i)
++ match o with None => nil | Some v => prev i :: nil end
++ xkeys m2 (xI i).
Proof.
intros. unfold xkeys. rewrite xelements_node. rewrite ! map_app. destruct o; auto.
Qed.
Lemma in_xkeys:
forall (A: Type) (m: t A) (i k: positive),
In k (xkeys m i) ->
(exists j, k = prev (prev_append j i)).
Proof.
unfold xkeys; intros.
apply (list_in_map_inv) in H. destruct H as ((j, v) & -> & H).
exploit in_xelements; eauto. intros (k & P & Q). exists k; auto.
Qed.
Lemma xelements_keys_norepet:
forall (A: Type) (m: t A) (i: positive),
list_norepet (xkeys m i).
Proof.
induction m; intros.
- rewrite xkeys_leaf; constructor.
- assert (NOTIN1: ~ In (prev i) (xkeys m1 (xO i))).
{ red; intros. exploit in_xkeys; eauto. intros (j & EQ).
rewrite prev_append_prev in EQ. simpl in EQ. apply prev_append_inj in EQ. discriminate. }
assert (NOTIN2: ~ In (prev i) (xkeys m2 (xI i))).
{ red; intros. exploit in_xkeys; eauto. intros (j & EQ).
rewrite prev_append_prev in EQ. simpl in EQ. apply prev_append_inj in EQ. discriminate. }
assert (DISJ: forall x, In x (xkeys m1 (xO i)) -> In x (xkeys m2 (xI i)) -> False).
{ intros. exploit in_xkeys. eexact H. intros (j1 & EQ1).
exploit in_xkeys. eexact H0. intros (j2 & EQ2).
rewrite prev_append_prev in *. simpl in *. rewrite EQ2 in EQ1. apply prev_append_inj in EQ1. discriminate. }
rewrite xkeys_node. apply list_norepet_append. auto.
destruct o; simpl; auto. constructor; auto.
red; intros. red; intros; subst y. destruct o; simpl in H0.
destruct H0. subst x. tauto. eauto. eauto.
Qed.
Theorem elements_keys_norepet:
forall (A: Type) (m: t A),
list_norepet (List.map (@fst elt A) (elements m)).
Proof.
intros. apply (xelements_keys_norepet m xH).
Qed.
Remark xelements_empty:
forall (A: Type) (m: t A) i, (forall i, get i m = None) -> xelements m i nil = nil.
Proof.
induction m; intros.
auto.
rewrite xelements_node. rewrite IHm1, IHm2. destruct o; auto.
generalize (H xH); simpl; congruence.
intros. apply (H (xI i0)).
intros. apply (H (xO i0)).
Qed.
Theorem elements_canonical_order':
forall (A B: Type) (R: A -> B -> Prop) (m: t A) (n: t B),
(forall i, option_rel R (get i m) (get i n)) ->
list_forall2
(fun i_x i_y => fst i_x = fst i_y /\ R (snd i_x) (snd i_y))
(elements m) (elements n).
Proof.
intros until n. unfold elements. generalize 1%positive. revert m n.
induction m; intros.
- simpl. rewrite xelements_empty. constructor.
intros. specialize (H i). rewrite gempty in H. inv H; auto.
- destruct n as [ | n1 o' n2 ].
+ rewrite (xelements_empty (Node m1 o m2)). simpl; constructor.
intros. specialize (H i). rewrite gempty in H. inv H; auto.
+ rewrite ! xelements_node. repeat apply list_forall2_app.
apply IHm1. intros. apply (H (xO i)).
generalize (H xH); simpl; intros OR; inv OR.
constructor.
constructor. auto. constructor.
apply IHm2. intros. apply (H (xI i)).
Qed.
Theorem elements_canonical_order:
forall (A B: Type) (R: A -> B -> Prop) (m: t A) (n: t B),
(forall i x, get i m = Some x -> exists y, get i n = Some y /\ R x y) ->
(forall i y, get i n = Some y -> exists x, get i m = Some x /\ R x y) ->
list_forall2
(fun i_x i_y => fst i_x = fst i_y /\ R (snd i_x) (snd i_y))
(elements m) (elements n).
Proof.
intros. apply elements_canonical_order'.
intros. destruct (get i m) as [x|] eqn:GM.
exploit H; eauto. intros (y & P & Q). rewrite P; constructor; auto.
destruct (get i n) as [y|] eqn:GN.
exploit H0; eauto. intros (x & P & Q). congruence.
constructor.
Qed.
Theorem elements_extensional:
forall (A: Type) (m n: t A),
(forall i, get i m = get i n) ->
elements m = elements n.
Proof.
intros.
exploit (@elements_canonical_order' _ _ (fun (x y: A) => x = y) m n).
intros. rewrite H. destruct (get i n); constructor; auto.
induction 1. auto. destruct a1 as [a2 a3]; destruct b1 as [b2 b3]; simpl in *.
destruct H0. congruence.
Qed.
Lemma xelements_remove:
forall (A: Type) v (m: t A) i j,
get i m = Some v ->
exists l1 l2,
xelements m j nil = l1 ++ (prev (prev_append i j), v) :: l2
/\ xelements (remove i m) j nil = l1 ++ l2.
Proof.
induction m; intros.
- rewrite gleaf in H; discriminate.
- assert (REMOVE: xelements (remove i (Node m1 o m2)) j nil =
xelements (match i with
| xH => Node m1 None m2
| xO ii => Node (remove ii m1) o m2
| xI ii => Node m1 o (remove ii m2) end)
j nil).
{
destruct i; simpl remove.
destruct m1; auto. destruct o; auto. destruct (remove i m2); auto.
destruct o; auto. destruct m2; auto. destruct (remove i m1); auto.
destruct m1; auto. destruct m2; auto.
}
rewrite REMOVE. destruct i; simpl in H.
+ destruct (IHm2 i (xI j) H) as (l1 & l2 & EQ & EQ').
exists (xelements m1 (xO j) nil ++
match o with None => nil | Some x => (prev j, x) :: nil end ++
l1);
exists l2; split.
rewrite xelements_node, EQ, ! app_ass. auto.
rewrite xelements_node, EQ', ! app_ass. auto.
+ destruct (IHm1 i (xO j) H) as (l1 & l2 & EQ & EQ').
exists l1;
exists (l2 ++
match o with None => nil | Some x => (prev j, x) :: nil end ++
xelements m2 (xI j) nil);
split.
rewrite xelements_node, EQ, ! app_ass. auto.
rewrite xelements_node, EQ', ! app_ass. auto.
+ subst o. exists (xelements m1 (xO j) nil); exists (xelements m2 (xI j) nil); split.
rewrite xelements_node. rewrite prev_append_prev. auto.
rewrite xelements_node; auto.
Qed.
Theorem elements_remove:
forall (A: Type) i v (m: t A),
get i m = Some v ->
exists l1 l2, elements m = l1 ++ (i,v) :: l2 /\ elements (remove i m) = l1 ++ l2.
Proof.
intros. exploit xelements_remove. eauto. instantiate (1 := xH).
rewrite prev_append_prev. auto.
Qed.
Fixpoint xfold (A B: Type) (f: B -> positive -> A -> B)
(i: positive) (m: t A) (v: B) {struct m} : B :=
match m with
| Leaf => v
| Node l None r =>
let v1 := xfold f (xO i) l v in
xfold f (xI i) r v1
| Node l (Some x) r =>
let v1 := xfold f (xO i) l v in
let v2 := f v1 (prev i) x in
xfold f (xI i) r v2
end.
Definition fold (A B : Type) (f: B -> positive -> A -> B) (m: t A) (v: B) :=
xfold f xH m v.
Lemma xfold_xelements:
forall (A B: Type) (f: B -> positive -> A -> B) m i v l,
List.fold_left (fun a p => f a (fst p) (snd p)) l (xfold f i m v) =
List.fold_left (fun a p => f a (fst p) (snd p)) (xelements m i l) v.
Proof.
induction m; intros.
simpl. auto.
destruct o; simpl.
rewrite <- IHm1. simpl. rewrite <- IHm2. auto.
rewrite <- IHm1. rewrite <- IHm2. auto.
Qed.
Theorem fold_spec:
forall (A B: Type) (f: B -> positive -> A -> B) (v: B) (m: t A),
fold f m v =
List.fold_left (fun a p => f a (fst p) (snd p)) (elements m) v.
Proof.
intros. unfold fold, elements. rewrite <- xfold_xelements. auto.
Qed.
Fixpoint fold1 (A B: Type) (f: B -> A -> B) (m: t A) (v: B) {struct m} : B :=
match m with
| Leaf => v
| Node l None r =>
let v1 := fold1 f l v in
fold1 f r v1
| Node l (Some x) r =>
let v1 := fold1 f l v in
let v2 := f v1 x in
fold1 f r v2
end.
Lemma fold1_xelements:
forall (A B: Type) (f: B -> A -> B) m i v l,
List.fold_left (fun a p => f a (snd p)) l (fold1 f m v) =
List.fold_left (fun a p => f a (snd p)) (xelements m i l) v.
Proof.
induction m; intros.
simpl. auto.
destruct o; simpl.
rewrite <- IHm1. simpl. rewrite <- IHm2. auto.
rewrite <- IHm1. rewrite <- IHm2. auto.
Qed.
Theorem fold1_spec:
forall (A B: Type) (f: B -> A -> B) (v: B) (m: t A),
fold1 f m v =
List.fold_left (fun a p => f a (snd p)) (elements m) v.
Proof.
intros. apply fold1_xelements with (l := @nil (positive * A)).
Qed.
End PTree.
(** * An implementation of maps over type [positive] *)
Module PMap <: MAP.
Definition elt := positive.
Definition elt_eq := peq.
Definition t (A : Type) : Type := (A * PTree.t A)%type.
Definition init (A : Type) (x : A) :=
(x, PTree.empty A).
Definition get (A : Type) (i : positive) (m : t A) :=
match PTree.get i (snd m) with
| Some x => x
| None => fst m
end.
Definition set (A : Type) (i : positive) (x : A) (m : t A) :=
(fst m, PTree.set i x (snd m)).
Theorem gi:
forall (A: Type) (i: positive) (x: A), get i (init x) = x.
Proof.
intros. unfold init. unfold get. simpl. rewrite PTree.gempty. auto.
Qed.
Theorem gss:
forall (A: Type) (i: positive) (x: A) (m: t A), get i (set i x m) = x.
Proof.
intros. unfold get. unfold set. simpl. rewrite PTree.gss. auto.
Qed.
Theorem gso:
forall (A: Type) (i j: positive) (x: A) (m: t A),
i <> j -> get i (set j x m) = get i m.
Proof.
intros. unfold get. unfold set. simpl. rewrite PTree.gso; auto.
Qed.