-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRBT.v
1707 lines (1466 loc) · 47.8 KB
/
RBT.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
(** * Finite Modular Maps: Red-Black Trees *)
(** Author: Pierre Letouzey (Université de Paris - INRIA),
adapted from earlier works in Coq Standard Library, see README.md.
License: LGPL-2.1-only, see file LICENSE. *)
(** This module implements finite maps using Red-Black trees.
This code is based on [MSetRBT.v] in Coq's Stdlib, due initially to
Andrew W. Appel, 2011. See initial comment at the beginning
of [MSetRBT.v].
Note that we only prove here that the operations below preserve
the binary search tree invariant ([Bst], a.k.a [Ok] predicate here),
but *not* the Red/Black balancing invariant. Indeed, the former
is enough to implement the desired interface [S], and ensure
observational correctness. And proceeding this way is quite lighter.
For the proofs of RBT balancing, see [MMaps.RBTproofs].
*)
From Coq Require Rtauto.
From Coq Require Import Bool BinPos Pnat Setoid SetoidList PeanoNat.
From Coq Require Import Orders OrdersFacts OrdersLists.
From MMaps Require Import Utils Interface OrdList GenTree.
Local Set Implicit Arguments.
Local Unset Strict Implicit.
Import ListNotations.
(* For nicer extraction, we create inductive principles
only when needed *)
Local Unset Elimination Schemes.
(** The type of color annotation. *)
Inductive color := Red | Black.
Module Color.
Definition t := color.
End Color.
(** Functor of pure functions + separate proofs of invariant
preservation *)
Module MakeRaw (K: OrderedType) <: Raw.S K.
(** ** Functions *)
(** *** Generic trees instantiated with Red/Black colors *)
(** We reuse a generic definition of trees where the information
parameter is a [Color.t]. Functions like mem or fold are also
provided by this generic functor. *)
Include MMaps.GenTree.Ops K Color.
Import GenTree.PairNotations. (* #1 and #2 for fst and snd *)
Local Open Scope lazy_bool_scope.
Local Notation color := Color.t.
Local Arguments Leaf {elt}.
Local Notation Rd := (@Node _ Red).
Local Notation Bk := (@Node _ Black).
Section Elt.
Variable elt : Type.
Local Notation t := (tree elt).
Implicit Types l r m : t.
Implicit Types e : elt.
(** *** Basic tree *)
Definition singleton (k : key) (v : elt) : t :=
Node Black Leaf k v Leaf.
(** *** Changing root color *)
Definition makeBlack m : t :=
match m with
| Leaf => Leaf
| Node _ a x v b => Bk a x v b
end.
Definition makeRed m : t :=
match m with
| Leaf => Leaf
| Node _ a x v b => Rd a x v b
end.
(** *** Balancing *)
(** We adapt when one side is not a true red-black tree.
Both sides have the same black depth. *)
Definition lbal l k vk r :=
match l with
| Rd (Rd a x vx b) y vy c => Rd (Bk a x vx b) y vy (Bk c k vk r)
| Rd a x vx (Rd b y vy c) => Rd (Bk a x vx b) y vy (Bk c k vk r)
| _ => Bk l k vk r
end.
Definition rbal l k vk r :=
match r with
| Rd (Rd b y vy c) z vz d => Rd (Bk l k vk b) y vy (Bk c z vz d)
| Rd b y vy (Rd c z vz d) => Rd (Bk l k vk b) y vy (Bk c z vz d)
| _ => Bk l k vk r
end.
(** A variant of [rbal], with reverse pattern order. *)
Definition rbal' l k vk r :=
match r with
| Rd b y vy (Rd c z vz d) => Rd (Bk l k vk b) y vy (Bk c z vz d)
| Rd (Rd b y vy c) z vz d => Rd (Bk l k vk b) y vy (Bk c z vz d)
| _ => Bk l k vk r
end.
(** Balancing with different black depth.
One side is almost a red-black tree, while the other is
a true red-black tree, but with black depth + 1.
Used in deletion. *)
Definition lbalS l k vk r :=
match l with
| Rd a x vx b => Rd (Bk a x vx b) k vk r
| _ =>
match r with
| Bk a y vy b => rbal' l k vk (Rd a y vy b)
| Rd (Bk a y vy b) z vz c => Rd (Bk l k vk a) y vy (rbal' b z vz (makeRed c))
| _ => Rd l k vk r (* impossible *)
end
end.
Definition rbalS l k vk r :=
match r with
| Rd b y vy c => Rd l k vk (Bk b y vy c)
| _ =>
match l with
| Bk a x vx b => lbal (Rd a x vx b) k vk r
| Rd a x vx (Bk b y vy c) => Rd (lbal (makeRed a) x vx b) y vy (Bk c k vk r)
| _ => Rd l k vk r (* impossible *)
end
end.
(** *** Insertion *)
Fixpoint ins x vx s :=
match s with
| Leaf => Rd Leaf x vx Leaf
| Node c l y vy r =>
match K.compare x y with
| Eq => Node c l y vx r
| Lt =>
match c with
| Red => Rd (ins x vx l) y vy r
| Black => lbal (ins x vx l) y vy r
end
| Gt =>
match c with
| Red => Rd l y vy (ins x vx r)
| Black => rbal l y vy (ins x vx r)
end
end
end.
Definition add x vx s := makeBlack (ins x vx s).
(** *** Deletion *)
Fixpoint append (l:t) : t -> t :=
match l with
| Leaf => fun r => r
| Node lc ll lx lv lr =>
fix append_l (r:t) : t :=
match r with
| Leaf => l
| Node rc rl rx rv rr =>
match lc, rc with
| Red, Red =>
let lrl := append lr rl in
match lrl with
| Rd lr' x v rl' => Rd (Rd ll lx lv lr') x v (Rd rl' rx rv rr)
| _ => Rd ll lx lv (Rd lrl rx rv rr)
end
| Black, Black =>
let lrl := append lr rl in
match lrl with
| Rd lr' x v rl' => Rd (Bk ll lx lv lr') x v (Bk rl' rx rv rr)
| _ => lbalS ll lx lv (Bk lrl rx rv rr)
end
| Black, Red => Rd (append_l rl) rx rv rr
| Red, Black => Rd ll lx lv (append lr r)
end
end
end.
Fixpoint del x m : t :=
match m with
| Leaf => Leaf
| Node _ a y v b =>
match K.compare x y with
| Eq => append a b
| Lt =>
match a with
| Bk _ _ _ _ => lbalS (del x a) y v b
| _ => Rd (del x a) y v b
end
| Gt =>
match b with
| Bk _ _ _ _ => rbalS a y v (del x b)
| _ => Rd a y v (del x b)
end
end
end.
Definition remove x m := makeBlack (del x m).
(** *** Tree-ification
We rebuild a tree of size [if pred then n-1 else n] as soon
as the list [l] has enough elements *)
Definition klist A := list (key * A).
Local Notation treeify_t := (klist elt -> t * klist elt).
Definition bogus : t * klist elt := (Leaf, nil).
Definition treeify_zero : treeify_t :=
fun acc => (Leaf,acc).
Definition treeify_one : treeify_t :=
fun acc => match acc with
| (x,v)::acc => (Rd Leaf x v Leaf, acc)
| _ => bogus
end.
Definition treeify_cont (f g : treeify_t) : treeify_t :=
fun acc =>
match f acc with
| (l, (x,v)::acc) =>
match g acc with
| (r, acc) => (Bk l x v r, acc)
end
| _ => bogus
end.
Fixpoint treeify_aux (pred:bool)(n: positive) : treeify_t :=
match n with
| xH => if pred then treeify_zero else treeify_one
| xO n => treeify_cont (treeify_aux pred n) (treeify_aux true n)
| xI n => treeify_cont (treeify_aux false n) (treeify_aux pred n)
end.
Fixpoint plength_aux (l:klist elt)(p:positive) := match l with
| nil => p
| _::l => plength_aux l (Pos.succ p)
end.
Definition plength (l:klist elt) := plength_aux l 1.
Definition treeify (l:klist elt) :=
fst (treeify_aux true (plength l) l).
End Elt.
(** *** Map with removal *)
Definition ocons {A B} (a:A) (o:option B) (l:list (A*B)) :=
match o with
| None => l
| Some b => (a,b)::l
end.
Section Mapo.
Variable elt elt' : Type.
Variable f : key -> elt -> option elt'.
Fixpoint mapoL (l : klist elt)(acc : klist elt') : klist elt' :=
match l with
| nil => acc
| (k,v)::l => mapoL l (ocons k (f k v) acc)
end.
Definition mapo (m : t elt) : t elt' :=
treeify (mapoL (rev_bindings m) nil).
End Mapo.
(** *** Merge *)
Section Merge.
Variable elt elt' elt'' : Type.
Variable f : key -> option elt -> option elt' -> option elt''.
Fixpoint mergeL (l1:klist elt) :
klist elt' -> klist elt'' -> klist elt'' :=
match l1 with
| nil => mapoL (fun k v' => f k None (Some v'))
| (x,vx)::l1' =>
fix mergeL_l1 l2 acc :=
match l2 with
| nil => mapoL (fun k v => f k (Some v) None) l1 acc
| (y,vy)::l2' =>
match K.compare x y with
| Eq => mergeL l1' l2' (ocons x (f x (Some vx) (Some vy)) acc)
| Lt => mergeL_l1 l2' (ocons y (f y None (Some vy)) acc)
| Gt => mergeL l1' l2 (ocons x (f x (Some vx) None) acc)
end
end
end.
Definition merge (m1 : t elt) (m2 : t elt') : t elt'' :=
treeify (mergeL (rev_bindings m1) (rev_bindings m2) nil).
End Merge.
(** ** Correctness proofs *)
Include MMaps.GenTree.Props K Color.
Import Ind.
Local Infix "∈" := In (at level 70).
Local Infix "==" := K.eq (at level 70).
Local Infix "<" := lessthan.
Local Infix "===" := Equal (at level 70).
Local Notation "m @ x ↦ e" := (MapsTo x e m)
(at level 9, format "m '@' x '↦' e").
Local Hint Constructors tree MapsTo Bst : map.
Local Hint Unfold Ok Bst_Ok In : map.
Local Hint Immediate F.eq_sym : map.
Local Hint Resolve F.eq_refl : map.
Local Hint Resolve above_notin above_trans below_notin below_trans : map.
Local Hint Resolve above_leaf below_leaf : map.
Ltac rtauto := Rtauto.rtauto.
Ltac autorew := autorewrite with map.
Tactic Notation "autorew" "in" ident(H) := autorewrite with map in H.
Ltac treeorder := try intro; autorew; intuition_in; order.
Ltac induct m :=
induction m as [|c l IHl x' vx' r IHr]; simpl; intros;
[|case K.compare_spec; intros; invok; chok].
Ltac destmatch :=
match goal with
| |- context [match K.compare _ _ with _ => _ end] =>
case K.compare_spec; destmatch
| |- context [match ?x with _ => _ end] =>
destruct x; destmatch
| _ => idtac
end.
Section Elt.
Variable elt : Type.
Implicit Types m : t elt.
Implicit Types k x y : key.
Implicit Types v vx vy : elt.
(** *** Singleton set *)
Lemma singleton_spec x (e:elt) : bindings (singleton x e) = (x,e)::nil.
Proof.
unfold singleton. now rewrite bindings_node.
Qed.
#[export] Instance singleton_ok x vx : Ok (singleton x vx).
Proof.
unfold singleton. ok.
Qed.
(** *** makeBlack and makeRed *)
#[export] Instance makeBlack_ok m `{!Ok m} : Ok (makeBlack m).
Proof.
destruct m; simpl; ok.
Qed.
#[export] Instance makeRed_ok m `{!Ok m} : Ok (makeRed m).
Proof.
destruct m; simpl; ok.
Qed.
Lemma makeBlack_mapsto m x v : (makeBlack m)@x ↦ v <-> m@x ↦ v.
Proof.
destruct m; simpl; intuition_m.
Qed.
Lemma makeRed_mapsto m x v : (makeRed m)@x ↦ v <-> m@x ↦ v.
Proof.
destruct m; simpl; intuition_m.
Qed.
Lemma makeBlack_in m x : x ∈ (makeBlack m) <-> x ∈ m.
Proof.
destruct m; simpl; intuition_in.
Qed.
Lemma makeRed_in m x : x ∈ (makeRed m) <-> x ∈ m.
Proof.
destruct m; simpl; intuition_in.
Qed.
Lemma makeBlack_find m : makeBlack m === m.
Proof.
now destruct m.
Qed.
Lemma makeRed_find m : makeRed m === m.
Proof.
now destruct m.
Qed.
(** *** Generic handling for red-matching and red-red-matching *)
Definition isblack m :=
match m with Bk _ _ _ _ => True | _ => False end.
Definition notblack m :=
match m with Bk _ _ _ _ => False | _ => True end.
Definition notred m :=
match m with Rd _ _ _ _ => False | _ => True end.
Definition rcase {A} f g m : A :=
match m with
| Rd a x v b => f a x v b
| _ => g m
end.
Inductive rspec {A} f g : t elt -> A -> Prop :=
| rred a x v b : rspec f g (Rd a x v b) (f a x v b)
| relse t : notred t -> rspec f g t (g t).
Fact rmatch {A} f g m : rspec (A:=A) f g m (rcase f g m).
Proof.
unfold rcase. destmatch; now constructor.
Qed.
Definition rrcase {A} f g m : A :=
match m with
| Rd (Rd a x vx b) y vy c => f a x vx b y vy c
| Rd a x vx (Rd b y vy c) => f a x vx b y vy c
| _ => g m
end.
Notation notredred := (rrcase (fun _ _ _ _ _ _ _ => False) (fun _ => True)).
Inductive rrspec {A} f g : t elt -> A -> Prop :=
| rrleft a x vx b y vy c :
rrspec f g (Rd (Rd a x vx b) y vy c) (f a x vx b y vy c)
| rrright a x vx b y vy c :
rrspec f g (Rd a x vx (Rd b y vy c)) (f a x vx b y vy c)
| rrelse m : notredred m -> rrspec f g m (g m).
Fact rrmatch {A} f g m : rrspec (A:=A) f g m (rrcase f g m).
Proof.
unfold rrcase. destmatch; now constructor.
Qed.
Definition rrcase' {A} f g m : A :=
match m with
| Rd a x vx (Rd b y vy c) => f a x vx b y vy c
| Rd (Rd a x vx b) y vy c => f a x vx b y vy c
| _ => g m
end.
Fact rrmatch' {A} f g m : rrspec (A:=A) f g m (rrcase' f g m).
Proof.
unfold rrcase'. destmatch; now constructor.
Qed.
(** Balancing operations are instances of generic match *)
Fact lbal_match l k v r :
rrspec
(fun a x vx b y vy c => Rd (Bk a x vx b) y vy (Bk c k v r))
(fun l => Bk l k v r)
l
(lbal l k v r).
Proof.
exact (rrmatch _ _ _).
Qed.
Fact rbal_match l k v r :
rrspec
(fun a x vx b y vy c => Rd (Bk l k v a) x vx (Bk b y vy c))
(fun r => Bk l k v r)
r
(rbal l k v r).
Proof.
exact (rrmatch _ _ _).
Qed.
Fact rbal'_match l k v r :
rrspec
(fun a x vx b y vy c => Rd (Bk l k v a) x vx (Bk b y vy c))
(fun r => Bk l k v r)
r
(rbal' l k v r).
Proof.
exact (rrmatch' _ _ _).
Qed.
Fact lbalS_match l x vx r :
rspec
(fun a y vy b => Rd (Bk a y vy b) x vx r)
(fun l =>
match r with
| Bk a y vy b => rbal' l x vx (Rd a y vy b)
| Rd (Bk a y vy b) z vz c =>
Rd (Bk l x vx a) y vy (rbal' b z vz (makeRed c))
| _ => Rd l x vx r
end)
l
(lbalS l x vx r).
Proof.
exact (rmatch _ _ _).
Qed.
Fact rbalS_match l x vx r :
rspec
(fun a y vy b => Rd l x vx (Bk a y vy b))
(fun r =>
match l with
| Bk a y vy b => lbal (Rd a y vy b) x vx r
| Rd a y vy (Bk b z vz c) =>
Rd (lbal (makeRed a) y vy b) z vz (Bk c x vx r)
| _ => Rd l x vx r
end)
r
(rbalS l x vx r).
Proof.
exact (rmatch _ _ _).
Qed.
(** *** Balancing for insertion *)
#[export] Instance lbal_ok l x v r `(!Ok l, !Ok r, l < x, x < r) :
Ok (lbal l x v r).
Proof.
destruct (lbal_match l x v r); ok; invlt; intuition; eautom.
Qed.
#[export] Instance rbal_ok l x v r `(!Ok l, !Ok r, l < x, x < r) :
Ok (rbal l x v r).
Proof.
destruct (rbal_match l x v r); ok; invlt; intuition; eautom.
Qed.
#[export] Instance rbal'_ok l x v r `(!Ok l, !Ok r, l < x, x < r) :
Ok (rbal' l x v r).
Proof.
destruct (rbal'_match l x v r); ok; invlt; intuition; eautom.
Qed.
(** Note : Rd is arbitrary here and in all similar results below *)
Lemma lbal_mapsto l x vx r y vy :
(lbal l x vx r)@y ↦ vy <-> (Rd l x vx r)@y ↦ vy.
Proof.
case lbal_match; intuition_m.
Qed.
Lemma rbal_mapsto l x vx r y vy :
(rbal l x vx r)@y ↦ vy <-> (Rd l x vx r)@y ↦ vy.
Proof.
case rbal_match; intuition_m.
Qed.
Lemma rbal'_mapsto l x vx r y vy :
(rbal' l x vx r)@y ↦ vy <-> (Rd l x vx r)@y ↦ vy.
Proof.
case rbal'_match; intuition_m.
Qed.
Lemma lbal_in l x v r y :
y ∈ (lbal l x v r) <-> y == x \/ y ∈ l \/ y ∈ r.
Proof.
unfold "∈". setoid_rewrite lbal_mapsto. setoid_rewrite node_mapsto.
firstorder. exists v. firstorder.
Qed.
Lemma rbal_in l x v r y :
y ∈ (rbal l x v r) <-> y == x \/ y ∈ l \/ y ∈ r.
Proof.
unfold "∈". setoid_rewrite rbal_mapsto; setoid_rewrite node_mapsto.
firstorder. exists v. firstorder.
Qed.
Lemma rbal'_in l x r v y :
y ∈ (rbal' l x v r) <-> y == x \/ y ∈ l \/ y ∈ r.
Proof.
unfold "∈". setoid_rewrite rbal'_mapsto; setoid_rewrite node_mapsto.
firstorder. exists v. firstorder.
Qed.
Lemma lbal_find l x v r `{!Ok l, !Ok r} :
l < x -> x < r ->
lbal l x v r === Rd l x v r.
Proof.
intros Hl Hr y. apply find_mapsto_equiv; auto using lbal_mapsto; autok.
Qed.
Lemma rbal_find l x v r `{!Ok l, !Ok r} :
l < x -> x < r ->
rbal l x v r === Rd l x v r.
Proof.
intros Hl Hr y. apply find_mapsto_equiv; auto using rbal_mapsto; autok.
Qed.
Lemma rbal'_find l x v r `{!Ok l, !Ok r} :
l < x -> x < r ->
rbal' l x v r === Rd l x v r.
Proof.
intros Hl Hr y. apply find_mapsto_equiv; auto using rbal'_mapsto; autok.
Qed.
Hint Rewrite (@in_node elt) makeRed_in makeBlack_in : map.
Hint Rewrite lbal_in rbal_in rbal'_in : map.
(** *** Insertion *)
Lemma ins_in m x v y :
y ∈ (ins x v m) <-> y == x \/ y ∈ m.
Proof.
induct m; destmatch; autorew; rewrite ?IHl, ?IHr; intuition_in.
setoid_replace y with x; eauto.
Qed.
Hint Rewrite ins_in : map.
Lemma ins_above m x v y : m < y -> x < y -> ins x v m < y.
Proof.
intros. treeorder.
Qed.
Lemma ins_below m x v y : y < m -> y < x -> y < ins x v m.
Proof.
intros. treeorder.
Qed.
Local Hint Resolve ins_above ins_below : map.
#[export] Instance ins_ok m x v `{!Ok m} : Ok (ins x v m).
Proof.
induct m; auto; destmatch;
(eapply lbal_ok || eapply rbal_ok || ok); auto; treeorder.
Qed.
Lemma ins_spec1 m x v `{!Ok m} : find x (ins x v m) = Some v.
Proof.
induct m; simpl; destmatch;
rewrite ?lbal_find, ?rbal_find by eauto with *;
simpl; destmatch; auto; try order.
Qed.
Lemma ins_spec2 m x y v `{!Ok m} : ~ x == y ->
find y (ins x v m) = find y m.
Proof.
induct m; simpl; destmatch;
rewrite ?lbal_find, ?rbal_find by eauto with *;
simpl; destmatch; auto; try order.
Qed.
Lemma ins_find m x y v `{!Ok m} :
find y (ins x v m) =
match K.compare y x with Eq => Some v | _ => find y m end.
Proof.
destmatch; intros E.
- apply find_spec; autok. rewrite E. apply find_spec; autok.
now apply ins_spec1.
- apply ins_spec2; trivial; order.
- apply ins_spec2; trivial; order.
Qed.
#[export] Instance add_ok m x v `{!Ok m} : Ok (add x v m).
Proof.
unfold add; autok.
Qed.
Lemma add_in m x v y :
y ∈ (add x v m) <-> y == x \/ y ∈ m.
Proof.
unfold add. now autorew.
Qed.
Hint Rewrite add_in : map.
Lemma add_spec1 m x v `{!Ok m} : find x (add x v m) = Some v.
Proof.
unfold add. rewrite makeBlack_find. now apply ins_spec1.
Qed.
Lemma add_spec2 m x y v `{!Ok m} : ~ x == y ->
find y (add x v m) = find y m.
Proof.
unfold add. rewrite makeBlack_find. now apply ins_spec2.
Qed.
(** *** Balancing for deletion *)
#[export] Instance lbalS_ok l x v r :
forall `(!Ok l, !Ok r, l < x, x < r), Ok (lbalS l x v r).
Proof.
case lbalS_match; intros; destmatch; try treeorder; invok; invlt.
- ok; intuition auto.
- constructor; chok; try treeorder. apply rbal'_ok; treeorder.
- apply rbal'_ok; treeorder.
Qed.
#[export] Instance rbalS_ok l x v r :
forall `(!Ok l, !Ok r, l < x, x < r), Ok (rbalS l x v r).
Proof.
case rbalS_match; intros; destmatch; try treeorder; invok; invlt.
- ok; intuition.
- constructor; chok; try treeorder. apply lbal_ok; treeorder.
- apply lbal_ok; treeorder.
Qed.
Lemma lbalS_mapsto l x v r y vy :
(lbalS l x v r)@y ↦ vy <-> (Rd l x v r)@y ↦ vy.
Proof.
case lbalS_match; intros.
- intuition_m.
- destmatch; intuition_m;
rewrite ?rbal'_mapsto in *; intuition_m;
rewrite ?makeRed_mapsto in *; intuition_m;
apply MapsRight; rewrite rbal'_mapsto; intuition_m.
apply MapsRight. now rewrite makeRed_mapsto.
Qed.
Lemma rbalS_mapsto l x v r y vy :
(rbalS l x v r)@y ↦ vy <-> (Rd l x v r)@y ↦ vy.
Proof.
case rbalS_match; intros.
- intuition_m.
- destmatch; intuition_m;
rewrite ?lbal_mapsto in *; intuition_m;
rewrite ?makeRed_mapsto in *; intuition_m;
apply MapsLeft; rewrite lbal_mapsto; intuition_m.
apply MapsLeft. now rewrite makeRed_mapsto.
Qed.
Lemma lbalS_in l x v r y :
y ∈ (lbalS l x v r) <-> y == x \/ y ∈ l \/ y ∈ r.
Proof.
unfold "∈"; setoid_rewrite lbalS_mapsto; setoid_rewrite node_mapsto.
firstorder. exists v. firstorder.
Qed.
Lemma rbalS_in l x r v y :
y ∈ (rbalS l x v r) <-> y == x \/ y ∈ l \/ y ∈ r.
Proof.
unfold "∈"; setoid_rewrite rbalS_mapsto; setoid_rewrite node_mapsto.
firstorder. exists v. firstorder.
Qed.
Lemma lbalS_find l x v r `{!Ok l, !Ok r} : l < x -> x < r ->
lbalS l x v r === Rd l x v r.
Proof.
intros Hl Hr y. apply find_mapsto_equiv; auto using lbalS_mapsto; autok.
Qed.
Lemma rbalS_find l x v r `{!Ok l, !Ok r} : l < x -> x < r ->
rbalS l x v r === Rd l x v r.
Proof.
intros Hl Hr y. apply find_mapsto_equiv; auto using rbalS_mapsto; autok.
Qed.
Hint Rewrite lbalS_in rbalS_in : map.
(** *** Append for deletion *)
Ltac append_tac l r :=
induction l as [| lc ll _ lx lv lr IHlr];
[intro r; simpl
|induction r as [| rc rl IHrl rx rv rr _];
[simpl
|destruct lc, rc;
[specialize (IHlr rl); clear IHrl
|simpl;
assert (Hr:notred (Bk rl rx rv rr)) by (simpl; trivial);
set (r:=Bk rl rx rv rr) in *; clearbody r; clear IHrl rl rx rv rr;
specialize (IHlr r)
|change (append _ _) with (Rd (append (Bk ll lx lv lr) rl) rx rv rr);
assert (Hl:notred (Bk ll lx lv lr)) by (simpl; trivial);
set (l:=Bk ll lx lv lr) in *; clearbody l; clear IHlr ll lx lv lr
|specialize (IHlr rl); clear IHrl]]].
Lemma mapsto_2levels c cl ll lx lv lr x v cr rl rx rv rr y e :
(Node c (Node cl ll lx lv lr) x v (Node cr rl rx rv rr))@y ↦ e <->
(Node cl ll lx lv Leaf)@y ↦ e \/
(Node c lr x v rl)@y ↦ e \/
(Node cr Leaf rx rv rr)@y ↦ e.
Proof.
rewrite !node_mapsto, !leaf_mapsto. rtauto.
Qed.
Lemma append_mapsto l r y v :
(append l r)@y ↦ v <-> l@y ↦ v \/ r@y ↦ v.
Proof.
revert r.
append_tac l r; try (intuition_m; fail).
- (* Rd / Rd *)
simpl. destmatch.
+ intuition_m.
+ rewrite mapsto_2levels. factornode m.
rewrite !node_mapsto, !leaf_mapsto. rtauto.
+ factornode m. rewrite !node_mapsto. rtauto.
- (* Bk / Bk *)
simpl. destmatch; rewrite ?lbalS_mapsto.
+ intuition_m.
+ rewrite mapsto_2levels. factornode m.
rewrite !node_mapsto, !leaf_mapsto. rtauto.
+ factornode m. rewrite !node_mapsto. rtauto.
Qed.
Lemma append_in (l r : t elt) x :
x ∈ (append l r) <-> x ∈ l \/ x ∈ r.
Proof.
unfold "∈". setoid_rewrite append_mapsto. firstorder.
Qed.
Hint Rewrite append_in : map.
#[export] Instance append_ok : forall l r `{!Ok l, !Ok r},
l < r -> Ok (@append elt l r).
Proof.
append_tac l r; trivial; intros OK OK' AP;
rewrite ?apart_node_l, ?apart_node_r in AP;
decompose [and] AP; clear AP; ok; try treeorder.
- (* Rd / Rd *)
assert (U : Ok (append lr rl)) by auto.
assert (V : lx < append lr rl) by (invlt;treeorder).
assert (W : append lr rl < rx) by (invlt;treeorder).
simpl. destmatch; invlt; ok; intuition; eautom.
- (* Bk / Bk *)
assert (U : Ok (append lr rl)) by auto.
assert (V : lx < append lr rl) by (invlt;treeorder).
assert (W : append lr rl < rx) by (invlt;treeorder).
simpl. destmatch; try apply lbalS_ok; invlt; ok; intuition; eautom.
Qed.
(** *** Deletion *)
Lemma del_in m x y `{!Ok m} :
y ∈ (del x m) <-> y ∈ m /\ ~ y==x.
Proof.
induct m; destmatch; autorew; try rewrite IHl; try rewrite IHr;
try clear IHl IHr; treeorder.
Qed.
Hint Rewrite del_in : map.
#[export] Instance del_ok m x `{!Ok m} : Ok (del x m).
Proof.
induct m.
- trivial.
- eapply append_ok; eauto using between.
- assert (del x l < x') by treeorder.
destmatch; ok.
- assert (x' < del x r) by treeorder.
destmatch; ok.
Qed.
Lemma del_spec1 m x `{!Ok m} : find x (del x m) = None.
Proof.
apply not_find_iff; ok. rewrite del_in; treeorder.
Qed.
Lemma del_spec2 m x y `{!Ok m} : ~ x == y ->
find y (del x m) = find y m.
Proof.
intros.
apply find_mapsto_equiv; ok.
induct m.
- easy.
- rewrite append_mapsto, node_mapsto. treeorder.
- destmatch.
+ simpl. intuition_m.
+ rewrite 2 node_mapsto. factornode l. now rewrite IHl by ok.
+ rewrite lbalS_mapsto, 2 node_mapsto. factornode l.
now rewrite IHl by ok.
- destmatch.
+ simpl. intuition_m.
+ rewrite 2 node_mapsto. factornode r. now rewrite IHr by ok.
+ rewrite rbalS_mapsto, 2 node_mapsto. factornode r.
now rewrite IHr by ok.
Qed.
Lemma remove_in m x y `{!Ok m} :
y ∈ (remove x m) <-> y ∈ m /\ ~ y==x.
Proof.
unfold remove. now autorew.
Qed.
Hint Rewrite remove_in : map.
#[export] Instance remove_ok m x `{!Ok m} : Ok (remove x m).
Proof.
unfold remove; ok.
Qed.
Lemma remove_spec1 m x `{!Ok m} : find x (remove x m) = None.
Proof.
unfold remove. rewrite makeBlack_find. now apply del_spec1.
Qed.
Lemma remove_spec2 m x y `{!Ok m} : ~ x == y ->
find y (remove x m) = find y m.
Proof.
unfold remove. rewrite makeBlack_find. now apply del_spec2.
Qed.
(** *** Treeify *)
Local Notation ifpred p n := (if p then pred n else n%nat).
Local Notation treeify_t := (klist elt -> t elt * klist elt).
Definition treeify_invariant size (f:treeify_t) :=
forall acc,
size <= length acc ->
let (m,acc') := f acc in
cardinal m = size /\ acc = bindings m ++ acc'.
Lemma treeify_zero_spec : treeify_invariant 0 (@treeify_zero elt).
Proof.
intro. simpl. auto.
Qed.
Lemma treeify_one_spec : treeify_invariant 1 (@treeify_one elt).
Proof.
intros [|x acc]; simpl; auto. inversion 1.
destruct x. simpl; auto.
Qed.
Lemma treeify_cont_spec f g size1 size2 size :
treeify_invariant size1 f ->
treeify_invariant size2 g ->
size = S (size1 + size2) ->
treeify_invariant size (treeify_cont f g).
Proof.
intros Hf Hg EQ acc LE. unfold treeify_cont.
specialize (Hf acc).
destruct (f acc) as (t1,acc1).
destruct Hf as (Hf1,Hf2).
{ transitivity size; trivial. subst. auto with arith. }
destruct acc1 as [|x acc1].
{ exfalso. revert LE. apply Nat.lt_nge. subst.
rewrite app_nil_r, <- cardinal_spec; auto with arith. }
specialize (Hg acc1).
destruct (g acc1) as (t2,acc2).
destruct Hg as (Hg1,Hg2).
{ revert LE. subst.
rewrite app_length, <- cardinal_spec. simpl.
rewrite Nat.add_succ_r, <- Nat.succ_le_mono.
apply Nat.add_le_mono_l. }
destruct x; simpl.
rewrite bindings_node_acc. subst; auto.
Qed.
Lemma treeify_aux_spec n (p:bool) :
treeify_invariant (ifpred p (Pos.to_nat n)) (treeify_aux p n).
Proof.
revert p.
induction n as [n|n|]; intros p; simpl treeify_aux.
- eapply treeify_cont_spec; [ apply (IHn false) | apply (IHn p) | ].
rewrite Pos2Nat.inj_xI.
assert (H := Pos2Nat.is_pos n). apply Nat.neq_0_lt_0 in H.
destruct p; simpl; intros; rewrite Nat.add_0_r; trivial.
now rewrite <- Nat.add_succ_r, Nat.succ_pred; trivial.
- eapply treeify_cont_spec; [ apply (IHn p) | apply (IHn true) | ].
rewrite Pos2Nat.inj_xO.
assert (H := Pos2Nat.is_pos n). apply Nat.neq_0_lt_0 in H.
rewrite <- Nat.add_succ_r, Nat.succ_pred by trivial.
destruct p; simpl; intros; rewrite Nat.add_0_r; trivial.
symmetry. now apply Nat.add_pred_l.
- destruct p; [ apply treeify_zero_spec | apply treeify_one_spec ].
Qed.
Lemma plength_aux_spec l p :
Pos.to_nat (@plength_aux elt l p) = length l + Pos.to_nat p.
Proof.
revert p. induction l; trivial. simpl plength_aux.
intros. now rewrite IHl, Pos2Nat.inj_succ, Nat.add_succ_r.
Qed.
Lemma plength_spec l : Pos.to_nat (@plength elt l) = S (length l).
Proof.
unfold plength. rewrite plength_aux_spec. apply Nat.add_1_r.
Qed.
Lemma treeify_bindings l : bindings (@treeify elt l) = l.
Proof.
assert (H := @treeify_aux_spec (plength l) true l).
unfold treeify. destruct treeify_aux as (t,acc); simpl in *.
destruct H as (H,H'). { now rewrite plength_spec. }
subst l. rewrite plength_spec, app_length, <- cardinal_spec in *.
destruct acc.
* now rewrite app_nil_r.
* exfalso. revert H. simpl.
rewrite Nat.add_succ_r, Nat.add_comm.
apply Nat.succ_add_discr.
Qed.