-
Notifications
You must be signed in to change notification settings - Fork 1
/
dynamic_redblack.v
1448 lines (1264 loc) · 48.1 KB
/
dynamic_redblack.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
From mathcomp Require Import ssreflect ssrbool ssrfun eqtype ssrnat div seq.
From mathcomp Require Import choice fintype prime tuple finfun finset bigop.
Require Import tree_traversal rank_select insert_delete set_clear dynamic.
(** * A formalization of succinct dynamic bit vectors *)
(** OUTLINE:
0. Section btree
1. Section dtree
Definition daccess, drank, dselect_1 and dselect_0.
2. Section insert
3. Section dinsert
4. Section set_clear
5. Section delete
6. Section ddelete
7. Section example
*)
Set Implicit Arguments.
Unset Strict Implicit.
Unset Printing Implicit Defensive.
Ltac decompose_rewrite :=
let H := fresh "H" in
case/andP || (move=>H; rewrite ?H ?(eqP H)).
Ltac decomp ok := move: ok => /=; repeat decompose_rewrite.
Section btree.
Variables D A : Type.
Inductive btree : Type :=
| Bnode : color -> btree -> D -> btree -> btree
| Bleaf : A -> btree.
End btree.
Section dtree.
Definition dtree := btree (nat * nat) (seq bool).
Fixpoint dflatten (B : dtree) :=
match B with
| Bnode _ l _ r => dflatten l ++ dflatten r
| Bleaf s => s
end.
Notation size_df t := (size (dflatten t)).
Notation ones_df t := (count_mem true (dflatten t)).
Variables low high : nat. (* (w ^ 2)./2 and (w ^ 2).*2 *)
Fixpoint wf_dtree (B : dtree) :=
match B with
| Bnode _ l (num, ones) r =>
[&& num == size_df l, ones == ones_df l, wf_dtree l & wf_dtree r]
| Bleaf arr => low <= size arr < high
end.
Lemma dtree_ind (P : dtree -> Prop) :
(forall c (l r : dtree) num ones,
num = size_df l -> ones = ones_df l -> wf_dtree l /\ wf_dtree r ->
P l -> P r -> P (Bnode c l (num, ones) r)) ->
(forall s, low <= size s < high -> P (Bleaf _ s)) ->
forall B, wf_dtree B -> P B.
Proof.
move=> HN HL; elim => [c l IHl [num ones] r IHr | s] //=.
move/andP => [/eqP Hones] /andP [/eqP Hnum] /andP [wfl wfr].
apply: HN; auto.
by apply: HL.
Qed.
Fixpoint dsize (B : dtree) :=
match B with
| Bnode _ l (n, _) r => n + dsize r
| Bleaf s => size s
end.
Fixpoint daccess (B : dtree) (i : nat) :=
match B with
| Bnode _ l (num, ones) r =>
if i < num then daccess l i else daccess r (i - num)
| Bleaf s =>
nth false s i
end.
Fixpoint drank (B : dtree) (i : nat) :=
match B with
| Bnode _ l (num, ones) r =>
if i < num then drank l i else ones + drank r (i - num)
| Bleaf s =>
rank true i s
end.
Definition drank_size B := drank B (dsize B).
Fixpoint dselect_1 (B : dtree) (i : nat) :=
match B with
| Bnode _ l (num, ones) r =>
if i <= ones then dselect_1 l i else num + dselect_1 r (i - ones)
| Bleaf s => select true i s
end.
Fixpoint dselect_0 (B : dtree) (i : nat) :=
match B with
| Bnode _ l (num, ones) r =>
let zeroes := num - ones
in if i <= zeroes then dselect_0 l i else num + dselect_0 r (i - zeroes)
| Bleaf s => select false i s
end.
Fixpoint dones (B : dtree) :=
match B with
| Bnode _ l (_, o) r => o + dones r
| Bleaf s => count_mem true s
end.
Definition access (s : seq bool) i := nth false s i.
Lemma daccessE (B : dtree) : wf_dtree B -> daccess B =1 access (dflatten B).
Proof.
move: B.
apply: dtree_ind => // c l r num ones -> -> _ IHl IHr i /=.
by rewrite IHl IHr /access nth_cat.
Qed.
Lemma drankE (B : dtree) i : wf_dtree B ->
drank B i = rank true i (dflatten B).
Proof.
move=> wf; move: B wf i.
apply: dtree_ind => // c l r num ones -> -> _ IHl IHr i /=.
rewrite rank_cat ltn_neqAle IHl IHr (rank_size _ _ _ erefl).
by case: ifP.
Qed.
Lemma dselect_1E (B : dtree) i : wf_dtree B ->
dselect_1 B i = select true i (dflatten B).
Proof.
move=> wf; move: B wf i.
apply: dtree_ind => // c l r num ones -> -> _ IHl IHr i /=.
by rewrite select_cat IHl IHr.
Qed.
Lemma predC_bool b : predC (pred1 b) =1 pred1 (negb b).
Proof. by case. Qed.
Lemma count_mem_false_true (s : seq bool) :
count_mem false s + count_mem true s = size s.
Proof.
by rewrite -(count_predC (pred1 false)) (eq_count (predC_bool false)).
Qed.
Lemma dselect_0E (B : dtree) i : wf_dtree B ->
dselect_0 B i = select false i (dflatten B).
Proof.
move=> wf; move: B wf i.
apply: dtree_ind => // c l r num ones -> -> _ IHl IHr i /=.
by rewrite select_cat -IHl -IHr -[in X in X - _]count_mem_false_true addnK.
Qed.
Lemma dsizeE (B : dtree) : wf_dtree B -> dsize B = size_df B.
Proof.
move=> wf; move: B wf.
apply: dtree_ind => // c l r num ones Hnum Hones _ IHl IHr /=.
by rewrite Hnum IHr size_cat.
Qed.
Lemma donesE (B : dtree) : wf_dtree B -> dones B = ones_df B.
Proof.
move=> wf; move: B wf.
apply: dtree_ind => // c l r num ones Hnum Hones _ IHl IHr /=.
by rewrite Hones IHr count_cat.
Qed.
Corollary drank_all (B : dtree) :
wf_dtree B -> drank B (dsize B) = ones_df B.
Proof. move => wf. by rewrite drankE // /rank dsizeE // take_size. Qed.
End dtree.
Notation size_df t := (size (dflatten t)).
Notation ones_df t := (count_mem true (dflatten t)).
Section insert.
Variables D A : Type.
Variable addD : D -> D -> D.
Variable subD : D -> D -> D.
Definition balance col (l r : btree D A) dl : btree D A :=
match col with
| Red => Bnode Red l dl r
| Black => match l, r with
| Bnode Red (Bnode Red a da b) dab c, d =>
Bnode Red (Bnode Black a da b) dab
(Bnode Black c (subD dl dab) d)
| Bnode Red a da (Bnode Red b db c), d =>
Bnode Red (Bnode Black a da b) (addD da db)
(Bnode Black c (subD (subD dl da) db) d)
| a, Bnode Red (Bnode Red b db c) dbc d =>
Bnode Red (Bnode Black a dl b) (addD dl db)
(Bnode Black c (subD dbc db) d)
| a, Bnode Red b db (Bnode Red c dc d) =>
Bnode Red (Bnode Black a dl b) (addD dl db)
(Bnode Black c dc d)
| _, _ => Bnode Black l dl r
end
end.
Definition balanceL col (l r : btree D A) dl : btree D A :=
match col with
| Red => Bnode Red l dl r
| Black => match l with
| Bnode Red (Bnode Red a da b) dab c =>
Bnode Red (Bnode Black a da b) dab
(Bnode Black c (subD dl dab) r)
| Bnode Red a da (Bnode Red b db c) =>
Bnode Red (Bnode Black a da b) (addD da db)
(Bnode Black c (subD (subD dl da) db) r)
| _ => Bnode Black l dl r
end
end.
Definition balanceR col (l r : btree D A) dl : btree D A :=
match col with
| Red => Bnode Red l dl r
| Black => match r with
| Bnode Red (Bnode Red b db c) dbc d =>
Bnode Red (Bnode Black l dl b) (addD dl db)
(Bnode Black c (subD dbc db) d)
| Bnode Red b db (Bnode Red c dc d) =>
Bnode Red (Bnode Black l dl b) (addD dl db)
(Bnode Black c dc d)
| _ => Bnode Black l dl r
end
end.
Variable bins_leaf : A -> bool -> nat -> btree D A.
Variable lt_index : nat -> D -> bool.
Variable right_index : nat -> D -> nat.
Variable insD : D -> bool -> D.
Fixpoint bins (B : btree D A) b i : btree D A :=
match B with
| Bleaf s => bins_leaf s b i
| Bnode c l d r =>
if lt_index i d
then balanceL c (bins l b i) r (insD d b)
else balanceR c l (bins r b (right_index i d)) d
end.
Definition binsert (B : btree D A) b i : btree D A :=
match bins B b i with
| Bleaf s => Bleaf _ s
| Bnode _ l d r => Bnode Black l d r
end.
(*
* Following Appel (2011), pp. 6 - 8
*
* ctxt = "color context", or the color of
* the parent node
*
* bh = "black height", i.e. # of black nodes on the
* path from the root
*)
Fixpoint is_redblack (B : btree D A) ctxt bh :=
match B with
| Bleaf _ => bh == 0
| Bnode c l _ r =>
match c, ctxt with
| Red, Red => false
| Red, Black => is_redblack l Red bh
&& is_redblack r Red bh
| Black, _ => (bh > 0) && is_redblack l Black (bh.-1) &&
is_redblack r Black (bh.-1)
end
end.
Definition nearly_redblack B bh :=
match B with
| Bnode Red l _ r => is_redblack l Black bh && is_redblack r Black bh
| _ => is_redblack B Black bh
end.
Hypothesis Hbins_leaf : forall a b i, is_redblack (bins_leaf a b i) Black 0.
Lemma is_redblack_Red_Black B n :
is_redblack B Red n -> is_redblack B Black n.
Proof. by case: B => /= [[]|]. Qed.
Lemma balanceL_Black_nearly_is_redblack l r n b :
nearly_redblack l n -> is_redblack r Black n ->
is_redblack (balanceL Black l r b) Black n.+1.
Proof.
case: l => [[[[] lll ? llr|?] ? [[] lrl ? lrr|?]|ll ? lr]|?] /=;
repeat decompose_rewrite => //;
by rewrite !is_redblack_Red_Black -?(eqP H1).
Qed.
Lemma balanceR_Black_nearly_is_redblack l r n b :
nearly_redblack r n -> is_redblack l Black n ->
is_redblack (balanceR Black l r b) Black n.+1.
Proof.
case: r => [[[[] rll ? rlr|?] ? [[] rrl ? rrr|?]|rl ? rr]|?] /=;
repeat decompose_rewrite => //;
by rewrite !is_redblack_Red_Black -?(eqP H1).
Qed.
Lemma is_redblack_nearly_redblack B c n :
is_redblack B c n -> nearly_redblack B n.
Proof.
case: B => //= -[]; case: c => // l p r /andP [Hl Hr].
by rewrite !is_redblack_Red_Black.
Qed.
Lemma bins_is_redblack B b i n :
(is_redblack B Black n -> nearly_redblack (bins B b i) n) /\
(is_redblack B Red n -> is_redblack (bins B b i) Black n).
Proof.
elim: B i n => [c l IHl d r IHr | a] i n; last first.
split => /= /eqP -> //; exact: is_redblack_nearly_redblack.
have Hbk : is_redblack (Bnode Black l d r) Black n ->
is_redblack (bins (Bnode Black l d r) b i) Black n.
rewrite {3}[Black]lock /= -lock => /andP [/andP [/prednK <- Hl] Hr].
case: ifP => Hi.
rewrite balanceL_Black_nearly_is_redblack //; by apply IHl.
rewrite balanceR_Black_nearly_is_redblack //; by apply IHr.
split; case: c => //.
+ move=> /= /andP [Hl Hr].
case: ifP => Hi /=; [move: Hr | move: Hl] => /is_redblack_Red_Black ->;
rewrite /= ?andbT; by [apply IHl | apply IHr].
+ move/Hbk; exact: is_redblack_nearly_redblack.
Qed.
Definition is_red D A (B : btree D A) :=
if B is Bnode Red _ _ _ then true else false.
Lemma binsert_is_redblack B b i n :
is_redblack B Red n ->
is_redblack (binsert B b i) Red (n + is_red (bins B b i)).
Proof.
move/(proj2 (bins_is_redblack _ b i _)).
rewrite /binsert addnC.
destruct bins => //=.
case: c => //= /andP [Hd1 Hd2].
by rewrite !is_redblack_Red_Black.
Qed.
Corollary binsert_is_redblack' B b i n :
is_redblack B Red n -> exists n', is_redblack (binsert B b i) Red n'.
Proof. esplit; apply /binsert_is_redblack /H. Qed.
End insert.
Section dinsert.
Variables low high : nat. (* (w ^ 2)./2 and (w ^ 2).*2 *)
Hypothesis Hlow : low.*2 <= high.
Hypothesis Hhigh : 1 < high.
Definition addD d1 d2 := (d1.1 + d2.1, d1.2 + d2.2).
Definition subD d1 d2 := (d1.1 - d2.1, d1.2 - d2.2).
Local Notation balance := (balance addD subD).
Local Notation balanceL := (balanceL addD subD).
Local Notation balanceR := (balanceR addD subD).
Local Notation wf_dtree_l := (wf_dtree low high).
Definition dins_leaf s b i :=
let s' := insert1 s b i in
if size s + 1 == high then
let n := size s' %/ 2 in let sl := take n s' in let sr := drop n s' in
Bnode Red (Bleaf _ sl) (n, count_mem true sl) (Bleaf _ sr)
else Bleaf _ s'.
Definition lt_index i (d : nat * nat) := i < fst d.
Definition right_index i (d : nat * nat) := i - fst d.
Definition insD (d : nat * nat) b := (d.1.+1, d.2+b).
Definition dins : dtree -> bool -> nat -> dtree :=
bins addD subD dins_leaf lt_index right_index insD.
Definition dinsert : dtree -> bool -> nat -> dtree :=
binsert addD subD dins_leaf lt_index right_index insD.
Lemma dins_leaf_is_redblack a b i : is_redblack (dins_leaf a b i) Black 0.
Proof. rewrite /dins_leaf; by case: ifP. Qed.
(* Red-blackness invariant *)
Corollary dinsert_is_redblack B b i n :
is_redblack B Red n -> exists n', is_redblack (dinsert B b i) Red n'.
Proof. apply /binsert_is_redblack' /dins_leaf_is_redblack. Qed.
(* Correctness of balance *)
Lemma dflatten_node c l d r :
dflatten (Bnode c l d r) = dflatten l ++ dflatten r.
Proof. by []. Qed.
Lemma dflatten_balance c l r d : dflatten (balance c l r d) = dflatten l ++ dflatten r.
Proof.
rewrite /balance. case: c. exact: dflatten_node.
case: l => [[[[] lll llD llr|llA] lD [[] lrl lrD lrr|lrA]|ll lD lr]|lA] /=;
case: r => [[[[] rll rlD rlr|rlA] rD [[] rrl rrD rrr|rrA]|rl rD rr]|rA] /=;
try done; by rewrite !catA.
Qed.
Lemma dflatten_balanceL c l r d: dflatten (balanceL c l r d) = dflatten l ++ dflatten r.
Proof.
rewrite /balanceL. case: c. exact: dflatten_node.
case: l => [[[[] lll llD llr|llA] lD [[] lrl lrD lrr|lrA]|ll lD lr]|lA] //=;
by rewrite !catA.
Qed.
Lemma dflatten_balanceR c l r d: dflatten (balanceR c l r d) = dflatten l ++ dflatten r.
Proof.
rewrite /balanceR. case: c. exact: dflatten_node.
case: r => [[[[] rll rlD rlr|rlA] rD [[] rrl rrD rrr|rrA]|rl rD rr]|rA] //=;
by rewrite !catA.
Qed.
(* Correctness of dinsert *)
Definition wf_dtree' t :=
if t is Bleaf s then size s < high else wf_dtree_l t.
Lemma wf_dtree_dtree' t : wf_dtree_l t -> wf_dtree' t.
Proof. by case: t => //= s /andP[_ ->]. Qed.
Lemma wf_dtree'_dtree t : wf_dtree' t -> wf_dtree 0 high t.
Proof.
elim: t => //= c l IHl [n o] r IHr; repeat decompose_rewrite => /=.
by rewrite !(IHl,IHr,wf_dtree_dtree').
Qed.
Lemma wf_dtree'_node c l d r :
wf_dtree' (Bnode c l d r) = wf_dtree low high (Bnode c l d r).
Proof. by []. Qed.
Lemma dins_leafE s b i :
size s < high -> dflatten (dins_leaf s b i) = insert1 s b i.
Proof.
rewrite /dins /dins_leaf. case: ifP => Hi Hs //.
by rewrite dflatten_node /dflatten cat_take_drop.
Qed.
Lemma dinsE (B : dtree) b i :
wf_dtree_l B -> dflatten (dins B b i) = insert1 (dflatten B) b i.
Proof.
move => wf; move: B wf b i. apply: dtree_ind => //.
+ move => c l r num ones Hnum Hones _ IHl IHr /= b i.
case: ifPn => ?.
- by rewrite dflatten_balanceL IHl /insert1 insert_catL -?Hnum.
- by rewrite dflatten_balanceR IHr /insert1 insert_catR -?Hnum // leqNgt.
+ move => s /andP [_] Hs b i /=.
by rewrite dins_leafE.
Qed.
Lemma dinsertE (B : dtree) b i :
wf_dtree' B -> dflatten (dinsert B b i) = insert1 (dflatten B) b i.
Proof.
case: B => [c l d r | s] wf.
rewrite /dinsert -(dinsE b i) /binsert /dins //; by case: bins.
rewrite /dinsert /binsert /dins -dins_leafE /=; by case: dins_leaf.
Qed.
(* Well-formedness lemmas
* Show that dinsert always returns a well-formed tree
*)
Lemma balanceL_wf c (l r : dtree) d :
wf_dtree_l (Bnode c l d r) -> wf_dtree_l (balanceL c l r d).
Proof.
case: c d => /= -[n o] /andP[Hn] /andP[Ho] /andP[wfl wfr].
by rewrite wfl wfr Hn Ho.
case: l wfl Hn Ho => [[[[] ? [??] ?|?] [??] [[] ? [??] ?|?] | ? [??] ?]|?] /=;
rewrite wfr; repeat decompose_rewrite;
by rewrite ?(size_cat,count_cat,addKn,eqxx).
Qed.
Lemma balanceR_wf c (l r : dtree) d :
wf_dtree_l (Bnode c l d r) -> wf_dtree_l (balanceR c l r d).
Proof.
case: c d => /= -[n o] /andP[Hn] /andP[Ho] /andP[wfl wfr].
by rewrite wfl wfr Hn Ho.
case: r wfr Hn Ho => [[[[] ? [??] ?|?] [??] [[] ? [??] ?|?] | ? [??] ?]|?] /=;
rewrite wfl; repeat decompose_rewrite;
by rewrite ?(size_cat,count_cat,addKn,eqxx).
Qed.
Lemma leq_half n : n./2 <= n.
Proof. by rewrite -{2}(odd_double_half n) -addnn addnA leq_addl. Qed.
Lemma dins_leaf_wf s b i : size s < high -> wf_dtree' (dins_leaf s b i).
Proof.
move=> Hs.
rewrite /dins_leaf addn1 divn2.
case: ifP => Hsize /=.
rewrite ?(eqxx,size_insert1,(eqP Hsize),size_drop,size_takel,leq_half) //.
have:= half_leq Hlow; rewrite doubleK => Hlow'.
set hhigh := high./2.
rewrite Hlow' -(odd_double_half high) -addnn addnA addnK.
have Hup : 0 < odd high + high./2.
by rewrite -addn1 leq_add // (half_leq Hhigh).
rewrite -add1n leq_add // (leq_trans Hlow' (leq_addl _ _)).
by rewrite -addn1 leq_add // (half_leq Hhigh).
move: Hs.
by rewrite size_insert1 leq_eqVlt Hsize /= => ->.
Qed.
Lemma dins_wf (B : dtree) b i :
wf_dtree low high B -> wf_dtree low high (dins B b i).
Proof.
move => wf; move: B wf b i.
apply: dtree_ind =>
[c l r num ones -> -> [wfl wfr] IHl IHr b i | s Hs b i] /=.
case: ifP => Hi.
apply: balanceL_wf => /=;
by rewrite IHl wfr dinsE // size_insert1 count_insert1 eqb_id !eqxx.
apply: balanceR_wf => /=; by rewrite IHr wfl !eqxx.
have/andP[Hs1 Hs2]:= Hs.
have:= dins_leaf_wf b i Hs2.
case Hins: dins_leaf => [//|s'] /= ->.
move: Hins; rewrite /dins_leaf.
case: ifP => // _ [] <-.
by rewrite size_insert1 (leq_trans Hs1).
Qed.
Lemma recolor_node_wf c c' d (l r : dtree) :
wf_dtree' (Bnode c l d r) -> wf_dtree' (Bnode c' l d r).
Proof. by []. Qed.
Definition is_leaf (t : dtree) := if t is Bleaf _ then true else false.
Lemma dins_leaf_leaf t b i : is_leaf (dins t b i) ==> is_leaf t.
Proof.
apply/implyP.
case: t => //= c l [n o] r.
case: ifP => _ /=; rewrite /balanceL /balanceR; case: c => //=;
by case: dins => //= -[] // [[] ? ? ?|?] [n' o'] [[] ? ? ?|?].
Qed.
Lemma dinsert_wf (B : dtree) b i :
wf_dtree' B -> wf_dtree' (dinsert B b i).
Proof.
rewrite /dinsert /binsert -/dins => wf.
have:= @dins_leaf_leaf B b i.
case Hins: (dins B b i) => [c' l' [? ?] r' | s'] Hlf.
apply (@recolor_node_wf c').
case: B wf Hins {Hlf} => [c l [num ones] r | s] wf Hins.
apply wf_dtree_dtree'.
by rewrite -Hins dins_wf.
rewrite -Hins /=.
by apply dins_leaf_wf.
case: B wf Hins Hlf => //= s Hs Hins b'.
move: (dins_leaf_wf b i Hs) => /=.
by rewrite Hins.
Qed.
(* Interaction with other operations *)
Definition dinsert_wf0 B b i wf := wf_dtree'_dtree (@dinsert_wf B b i wf).
Lemma dinsert_rank (B : dtree) b i j :
wf_dtree' B -> drank (dinsert B b i) j =
rank true j (insert1 (dflatten B) b i).
Proof.
move => wf; by rewrite -dinsertE // (@drankE 0 high) // dinsert_wf0.
Qed.
Lemma dinsert_select1 (B : dtree) b i j : wf_dtree' B ->
dselect_1 (dinsert B b i) j = select true j (insert1 (dflatten B) b i).
Proof.
move => wf; by rewrite -dinsertE // (@dselect_1E 0 high) // dinsert_wf0.
Qed.
Lemma dinsert_select0 (B : dtree) b i j : wf_dtree' B ->
dselect_0 (dinsert B b i) j = select false j (insert1 (dflatten B) b i).
Proof.
move => wf; by rewrite -dinsertE // (@dselect_0E 0 high) // dinsert_wf0.
Qed.
End dinsert.
Section set_clear.
Variables low high : nat.
Hypothesis Hlow : low.*2 <= high.
Hypothesis Hhigh : 1 < high.
Local Notation wf_dtree' := (wf_dtree' low high).
Fixpoint bset (B : dtree) i : (dtree * bool) :=
match B with
| Bleaf s => (Bleaf _ (bit_set s i), ~~ (nth true s i))
| Bnode c l (num, ones) r =>
if i < num
then let (l', flip) := bset l i
in (Bnode c l' (num, ones + flip) r, flip)
else let (r', flip) := bset r (i - num)
in (Bnode c l (num, ones) r', flip)
end.
Fixpoint bclear (B : dtree) i : (dtree * bool) :=
match B with
| Bleaf s => (Bleaf _ (bit_clear s i), nth true s i)
| Bnode c l (num, ones) r =>
if i < num
then let (l', flip) := bclear l i
in (Bnode c l' (num, ones - flip) r, flip)
else let (r', flip) := bclear r (i - num)
in (Bnode c l (num, ones) r', flip)
end.
Definition dbitset (B : dtree) i := fst (bset B i).
Definition dbitclear (B : dtree) i := fst (bclear B i).
Lemma dbitsetE (B : dtree) i :
wf_dtree' B -> dflatten (dbitset B i) = bit_set (dflatten B) i.
Proof.
move=> /wf_dtree'_dtree wf; move: B wf i; rewrite /bit_set.
apply: dtree_ind => // c l r num ones -> -> _ IHl IHr i /=.
rewrite update_cat -IHl -IHr /dbitset /=.
by case: ifP; case: bset => // l' [].
Qed.
Lemma dbitclearE (B : dtree) i :
wf_dtree' B -> dflatten (dbitclear B i) = bit_clear (dflatten B) i.
Proof.
move=> /wf_dtree'_dtree wf; move: B wf i; rewrite /bit_clear.
apply: dtree_ind => // c l r num ones -> -> _ IHl IHr i /=.
rewrite update_cat -IHl -IHr /dbitclear /=.
by case: ifP; case: bclear => // l' [].
Qed.
Lemma size_bset (B : dtree) i : size_df (bset B i).1 = size_df B.
Proof.
elim: B i => [c l IHl [num ones] r IHr | s] //= i; last by rewrite -size_update.
case: ifP; by rewrite (surjective_pairing (bset _ _)) /= !size_cat (IHl,IHr).
Qed.
Lemma size_bclear (B : dtree) i : size_df (bclear B i).1 = size_df B.
Proof.
elim: B i => [c l IHl [num ones] r IHr | s] //= i; last by rewrite -size_update.
case: ifP; by rewrite (surjective_pairing (bclear _ _)) /= !size_cat (IHl,IHr).
Qed.
Lemma size_dbitset (B : dtree) i : size_df (dbitset B i) = size_df B.
Proof. by rewrite /dbitset size_bset. Qed.
Lemma size_dbitclear (B : dtree) i : size_df (dbitclear B i) = size_df B.
Proof. by rewrite /dbitclear size_bclear. Qed.
Lemma is_redblack_dbitset (B : dtree) i c n :
is_redblack B c n = is_redblack (dbitset B i) c n.
Proof.
rewrite /dbitset.
elim: B i c n => //= cB l IHl [nn o] r IHr i c n //.
case: ifP; by rewrite (surjective_pairing (bset _ _)) /= -!(IHl,IHr).
Qed.
Lemma is_redblack_dbitclear (B : dtree) i c n :
is_redblack B c n = is_redblack (dbitclear B i) c n.
Proof.
rewrite /dbitclear.
elim: B i c n => //= cB l IHl [nn o] r IHr i c n //.
case: ifP; by rewrite (surjective_pairing (bclear _ _)) /= -!(IHl,IHr).
Qed.
Lemma ones_dbitset (B : dtree) i :
wf_dtree low high B -> i < size_df B ->
ones_df B + (bset B i).2 = ones_df (bset B i).1.
Proof.
rewrite /dbitset.
move=> wf Hsize; move: B wf i Hsize.
apply: dtree_ind => //= [ c l r num ones -> -> [wfl wfr] IHl IHr i /= Hi
| s Hs i Hi ]; last by rewrite addnC -count_bit_set.
case: ifP => Hil; rewrite (surjective_pairing (bset _ _)) /= !count_cat.
- by rewrite addnAC IHl.
- rewrite -!addnA IHr // -add1n.
by rewrite -(ltn_add2l (size_df l)) -size_cat subnKC // leqNgt Hil.
Qed.
Lemma flip_bit_bclear (B : dtree) i :
wf_dtree low high B -> i < size_df B -> (bclear B i).2 = daccess B i.
Proof.
move=> wf; move: B wf i.
apply: dtree_ind; last by move => ????; apply set_nth_default.
move => c l r num ones Hnum Hones [wfl wfr] IHl IHr i /=.
rewrite size_cat Hnum => Hsz.
case: ifP => Hi; rewrite (surjective_pairing (bclear _ _)) (IHl,IHr) //.
by rewrite -subSn ?leq_subLR // leqNgt Hi.
Qed.
Lemma ones_dbitclear (B : dtree) i :
wf_dtree low high B -> i < size_df B ->
ones_df (dbitclear B i) = ones_df B - daccess B i.
Proof.
rewrite /dbitclear.
move=> wf Hsize; move: B wf i Hsize.
apply: dtree_ind => //=
[c l r num ones -> -> [wfl wfr] IHl IHr i /= Hi | s Hs i Hi];
last by rewrite -count_bit_clear.
case: ifP => Hil; rewrite (surjective_pairing (bclear _ _)) /= !count_cat.
- by rewrite IHl // addnC [in RHS]addnC -addnBA // (daccessE wfl) true_count_pos.
- have Hilr: i - size_df l < size_df r.
by rewrite -(ltn_add2l (size_df l)) -size_cat subnKC // leqNgt Hil.
by rewrite IHr // addnBA // (daccessE wfr) true_count_pos.
Qed.
Lemma wf_dbitset (B : dtree) i :
wf_dtree' B -> wf_dtree' (dbitset B i).
Proof.
case: B => [c l d r | s] wf; last by rewrite /= size_bit_set.
apply wf_dtree_dtree'.
move: wf i; rewrite wf_dtree'_node; move: {c l d r} (Bnode _ _ _ _).
apply: dtree_ind => //= [c l r num ones -> -> [wfl wfr] IHl IHr|s Hs] i;
last by rewrite size_bit_set.
rewrite /dbitset /=.
case: ifP => Hil; rewrite (surjective_pairing (bset _ _)) /= (wfl,wfr).
- by rewrite size_bset ones_dbitset // !eqxx IHl.
- by rewrite IHr !eqxx.
Qed.
Lemma wf_dbitclear (B : dtree) i :
wf_dtree' B -> wf_dtree' (dbitclear B i).
Proof.
case: B => [c l d r | s] wf; last by rewrite /= size_bit_clear.
apply wf_dtree_dtree'.
move: wf i; rewrite wf_dtree'_node; move: {c l d r} (Bnode _ _ _ _).
apply: dtree_ind => [c l r num ones -> -> [wfl wfr] IHl IHr|s Hs] i /=;
last by rewrite size_bit_clear.
rewrite /dbitclear /=.
case: ifP => Hil; rewrite (surjective_pairing (bclear _ _)) /= (wfl,wfr).
- by rewrite size_bclear ones_dbitclear // flip_bit_bclear // !eqxx IHl.
- by rewrite IHr !eqxx.
Qed.
End set_clear.
Section delete.
Variables D A : Type.
Variable mkD : btree D A -> D.
Variable addD : D -> D -> D.
Variable subD : D -> D -> D.
Definition rbnode c l r := Bnode c l (mkD l) r.
Definition bnode l r := Bnode Black l (mkD l) r.
Definition rnode l r := Bnode Red l (mkD l) r.
Local Notation leaf a := (Bleaf _ a : btree D A).
Record deleted_btree: Type := MkD
{ d_tree :> btree D A; d_down: bool; d_del: D }.
Definition balanceL' col (l : deleted_btree) d r : deleted_btree :=
let d' := subD d (d_del l) in
let stay tr := MkD tr false (d_del l) in
let down tr := MkD tr true (d_del l) in
if ~~ d_down l
then stay (Bnode col l d' r)
else match col, r with
| _, Bnode Black (Bnode Red rll rld rlr) rd rr =>
stay (Bnode col
(Bnode Black l d' rll)
(addD rld d')
(Bnode Black rlr (subD rd rld) rr))
| Red, Bnode Black (Bleaf _ as rl) rd rr
| Red, Bnode Black (Bnode Black _ _ _ as rl) rd rr =>
stay (Bnode Black (Bnode Red l d' rl) (addD d' rd) rr)
| Black, Bnode Red (Bnode Black (Bnode Black _ _ _ as rll) rld rlr) rd rr
| Black, Bnode Red (Bnode Black (Bleaf _ as rll) rld rlr) rd rr =>
stay (Bnode Black
(Bnode Black
(Bnode Red l d' rll)
(addD d' rld)
rlr)
(addD d' rd)
rr)
| Black, Bnode Red (Bnode Black (Bnode Red rlll rlld rllr) rld rlr) rd rr =>
stay (Bnode Black
(Bnode Black l d' rlll)
(addD rlld d')
(Bnode Red
(Bnode Black rllr (subD rld rlld) rlr)
(subD rd rlld)
rr))
| Black, Bnode Black (Bleaf _ as rl) rd rr
| Black, Bnode Black (Bnode Black _ _ _ as rl) rd rr =>
down (Bnode Black (Bnode Red l d' rl) (addD d' rd) rr)
| _, _ => stay (Bnode col l d' r)
end.
Definition balanceR' col l d (r : deleted_btree) : deleted_btree :=
let stay tr := MkD tr false (d_del r) in
let down tr := MkD tr true (d_del r) in
if ~~ d_down r
then stay (Bnode col l d r)
else match col, l with
| _, Bnode Black ll ld (Bnode Red lrl lrd lrr) =>
stay (Bnode col
(Bnode Black ll ld lrl)
(addD ld lrd)
(Bnode Black lrr (subD d (addD ld lrd)) r))
| Red, Bnode Black ll ld (Bleaf _ as lr)
| Red, Bnode Black ll ld (Bnode Black _ _ _ as lr) =>
stay (Bnode Black ll ld (Bnode Red lr (subD d ld) r))
| Black, Bnode Red ll ld (Bnode Black lrl lrd (Bnode Black _ _ _ as lrr))
| Black, Bnode Red ll ld (Bnode Black lrl lrd (Bleaf _ as lrr)) =>
stay (Bnode
Black
ll
ld
(Bnode Black lrl lrd (Bnode Red lrr (subD d (addD ld lrd)) r)))
| Black, Bnode Red ll ld (Bnode Black lrl lrd (Bnode Red lrrl lrrd lrrr)) =>
stay (Bnode Black (Bnode Red ll ld (Bnode Black lrl lrd lrrl))
(addD (addD ld lrd) lrrd)
(Bnode Black lrrr (subD d (addD (addD ld lrd) lrrd)) r))
| Black, Bnode Black ll ld (Bleaf _ as lr)
| Black, Bnode Black ll ld (Bnode Black _ _ _ as lr) =>
down (Bnode Black ll ld (Bnode Red lr (subD d ld) r))
| _, _ => stay (Bnode col l d r)
end.
Lemma balanceL'_d_delE c (l : deleted_btree) d r :
d_del (balanceL' c l d r) = d_del l.
Proof.
case: l c r => l [] ? [] [[] rl ??|?] //=;
case: rl => [[] rll ??|?] //=;
case: rll => [[]???|?] //=.
Qed.
Lemma balanceR'_d_delE c l d (r : deleted_btree) :
d_del (balanceR' c l d r) = d_del r.
Proof.
case: r c l => r [] ? [] [[]?? lr|?] //=;
case: lr => [[]?? lrr|?] //=;
case: lrr => [[]???|?] //=.
Qed.
Variable lt_index : nat -> D -> bool.
Variable right_index : nat -> D -> nat.
Variable delete_leaf : A -> nat -> A * D.
Variable delete_from_leaves : color -> A -> A -> nat -> deleted_btree.
Fixpoint bdel B (i : nat) { struct B } : deleted_btree :=
match B with
| Bnode c (Bleaf l) d (Bleaf r) => delete_from_leaves c l r i
| Bnode Black (Bnode Red (Bleaf ll) ld (Bleaf lr) as l) d (Bleaf r) =>
if lt_index i d
then balanceL' Black (bdel l i) d (Bleaf _ r)
else balanceR' Black (Bleaf _ ll) ld
(delete_from_leaves Red lr r (right_index i ld))
| Bnode Black (Bleaf l) ld (Bnode Red (Bleaf rl) d (Bleaf rr) as r) =>
if lt_index (right_index i ld) d
then balanceL' Black (delete_from_leaves Red l rl i)
(addD ld d) (Bleaf _ rr)
else balanceR' Black (Bleaf _ l) ld (bdel r (right_index i ld))
| Bnode c l d r =>
if lt_index i d
then balanceL' c (bdel l i) d r
else balanceR' c l d (bdel r (right_index i d))
| Bleaf x =>
let (leaf, ret) := delete_leaf x i in
MkD (Bleaf _ leaf) false ret
end.
Definition is_deleted_redblack tr c bh :=
if d_down tr
then is_redblack tr Red bh.-1
else is_redblack tr c bh.
Hypothesis Hdelete_from_leaves : forall c l d r i c' n,
is_redblack (Bnode c (Bleaf D l) d (Bleaf D r)) c' n ->
is_deleted_redblack (delete_from_leaves c l r i) c' n.
Lemma is_deleted_redblack_Red_Black B n :
is_deleted_redblack B Red n -> is_deleted_redblack B Black n.
Proof. by case: B => [[[]???|?] [] ?]. Qed.
Lemma balanceL'_Black_deleted_is_redblack l d r n c :
0 < n -> is_deleted_redblack l Black n.-1 -> is_redblack r Black n.-1 ->
is_deleted_redblack (balanceL' Black l d r) c n.
Proof.
case: l => [l [] ?] Hn okl okr;
case: c n r l okr okl Hn => [] [//|n]
[[[[] [[] rlll ? rllr|?] ? rlr|?] ? rr| [[] rll ? rlr| ?] ? rr]|?] l;
rewrite /balanceL' /is_deleted_redblack //=; repeat decompose_rewrite;
by rewrite // !is_redblack_Red_Black.
Qed.
Lemma balanceL'_Red_deleted_is_redblack l d r n :
is_deleted_redblack l Red n -> is_redblack r Red n ->
is_deleted_redblack (balanceL' Red l d r) Black n.
Proof.
case: l => [l [] ?] okl okr;
case: n r l okr okl => [//|n]
[[[[] [[] rlll ? rllr|?] ? rlr|?] ? rr| [[] rll ? rlr| ?] ? rr]|?] l;
rewrite /balanceL' /is_deleted_redblack //=; repeat decompose_rewrite;
by rewrite // !is_redblack_Red_Black.
Qed.
Lemma balanceR'_Black_deleted_is_redblack l d r n c :
0 < n -> is_redblack l Black n.-1 -> is_deleted_redblack r Black n.-1 ->
is_deleted_redblack (balanceR' Black l d r) c n.
Proof.
case: r => [r [] ?];
case: c n l => [] [//|n] [[[[] lll ? llr|?] ?
[[] lrl ? [[] lrrl ? lrrr|?]|?]|ll ? [[] lrl ? lrr|?]]|?] /=;
rewrite /balanceR' /is_deleted_redblack //=; repeat decompose_rewrite;
by rewrite // !is_redblack_Red_Black.
Qed.
Lemma balanceR'_Red_deleted_is_redblack l d r n :
is_redblack l Red n -> is_deleted_redblack r Red n ->
is_deleted_redblack (balanceR' Red l d r) Black n.
Proof.
case: r => [r [] ?];
case: l => [[[[] lll ? llr|?] ?
[[] lrl ? [[] lrrl ? lrrr|?]|?]|ll ? [[] lrl ? lrr|?]]|?] /=;
rewrite /balanceR' /is_deleted_redblack //=; repeat decompose_rewrite;
by rewrite // !is_redblack_Red_Black.
Qed.
(* This tactic is not necessary,
but if it is removed, a proof becomes more slower. *)
Ltac close_branch d H IHl IHr :=
rewrite /=;
try case:ifP=>?;
repeat (apply balanceL'_Red_deleted_is_redblack ||
apply balanceL'_Red_deleted_is_redblack ||
apply balanceR'_Red_deleted_is_redblack ||
apply balanceL'_Black_deleted_is_redblack ||
apply balanceR'_Black_deleted_is_redblack ||
apply IHl ||
apply IHr ||
apply (Hdelete_from_leaves (d:=d)));
decomp H.
Lemma bdel_is_deleted_redblack B i n c :
is_redblack B c n -> is_deleted_redblack (bdel B i) c n.
Proof.
elim: B c i n => [c l IHl d r IHr |a] p i n H //.
time (case: p c l IHl H => [] []// [[]//[[]//???|?]?[[]//???|?]|?] IHl H;
try (by close_branch d H IHl IHr);
case: r IHr H => [[]//[[]//???|?]?[[]//???|?]|?] IHr H;
close_branch d H IHl IHr => //).
rewrite /is_deleted_redblack /=;
case: (delete_leaf a i) => //=.
(* 153s -> 73s by rewrite ? -> repeat apply || *)
Qed.
End delete.
Section ddelete.
Variables low high : nat.
Hypothesis Hlow : low.*2 <= high.
Hypothesis Hlow1 : low >= 1.
Let Hhigh : 1 < high.
Proof. by rewrite (leq_trans _ Hlow) // (leq_double 1 low). Qed.
Local Notation wf_dtree' := (wf_dtree' low high).
Local Notation wf_dtree_l := (wf_dtree low high).
Local Notation donesE' := (@donesE low high).
Local Notation dsizeE' := (@dsizeE low high).
Local Notation leaf a := (Bleaf _ a : dtree).
Definition mkD l := (dsize l, dones l).
Local Notation rbnode := (rbnode mkD).
Definition deleted_dtree := deleted_btree (nat * nat) (seq bool).
Local Notation balanceL' c B d b := (balanceL' addD subD c B d b).
Local Notation balanceR' c B d b := (balanceR' addD subD c B d b).
Definition delete_from_leaves (p : color) l r (i : nat) : deleted_dtree :=
if i < size l
then match low == size l, low == size r with
| true, true =>
MkD (leaf ((rcons (delete l i) (access r 0)) ++ (delete r 0)))
true
(1, access l i : nat)
| true, false =>
MkD (rbnode p (leaf (rcons (delete l i) (access r 0)))
(leaf (delete r 0)))
false
(1, access l i : nat)
| false, _ =>
MkD (rbnode p (leaf (delete l i)) (leaf r))
false
(1, access l i : nat)
end
else match low == size l, low == size r with
| true, true =>
MkD (leaf (l ++ (delete r (i - (size l)))))
true
(1, access r (i - (size l)) : nat)
| false, true =>
MkD (rbnode p (leaf (delete l (size l).-1))
(leaf (access l (size l).-1 :: delete r (i - size l))))
false
(1, access r (i - (size l)) : nat)
| _, false =>