forked from PrincetonUniversity/VST
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredblack.v
4543 lines (4216 loc) · 139 KB
/
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
(* Implementation and proof of Red-Black Trees,
matching the Coq Library MSets interface.
Author: Andrew W. Appel, 2011.
The design decisions behind this implementation are described here:
Efficient Verified Red-Black Trees, by Andrew W. Appel, September 2011.
http://www.cs.princeton.edu/~appel/papers/redblack.pdf
Additional suggested reading:
Red-Black Trees in a Functional Setting by Chris Okasaki.
Journal of Functional Programming, 9(4):471-477, July 1999.
http://www.eecs.usma.edu/webs/people/okasaki/jfp99redblack.pdf
Red-black trees with types, by Stefan Kahrs.
Journal of Functional Programming, 11(4), 425-432, 2001.
Functors for Proofs and Programs, by J.-C. Filliatre and P. Letouzey.
ESOP'04: European Symposium on Programming, pp. 370-384, 2004.
http://www.lri.fr/~filliatr/ftp/publis/fpp.ps.gz
*)
Require MSets.
Require Coq.Structures.Orders.
Require Coq.Structures.OrdersFacts.
Require Import Morphisms.
Require Import ProofIrrelevance.
Require Import Sorted.
Require Import SetoidList.
Require Import NArith Lists.List.
Import Pnat.
Require Omega.
Module Type MSetPlus.
Include MSetInterface.S.
Parameter delete_min : t -> option (elt * t).
Axiom delete_min_spec1: forall (s: t) k s',
delete_min s = Some (k,s') <->
(min_elt s = Some k /\ remove k s = s').
Axiom delete_min_spec2: forall s, delete_min s = None <-> Empty s.
Parameter mem_add: elt -> t -> option t.
Axiom mem_add_spec:
forall x s, mem_add x s = if mem x s then None else Some (add x s).
End MSetPlus.
Module Make (K: Orders.OrderedType) : MSetPlus with Module E:=K.
Module OTT := OrdersFacts.OrderedTypeTest K.
Import OTT MO.
Local Notation "'key'" := K.t.
Module E := K.
Inductive color := Red | Black.
Inductive tree : Type :=
| EE : tree
| T: color -> tree -> key -> tree -> tree.
Fixpoint member (x: key) (t : tree) : bool :=
match t with
| EE => false
| T _ tl k tr => match K.compare x k with
| Lt => member x tl
| Eq => true
| Gt => member x tr
end
end.
Definition balance color t1 k t2 :=
match color with Red => T Red t1 k t2
| _ =>
match t1, t2 with
| T Red (T Red a x b) y c, d => T Red (T Black a x b) y (T Black c k d)
| T Red a x (T Red b y c), d => T Red (T Black a x b) y (T Black c k d)
| a, T Red (T Red b y c) z d => T Red (T Black a k b) y (T Black c z d)
| a, T Red b y (T Red c z d) => T Red (T Black a k b) y (T Black c z d)
| _, _ => T Black t1 k t2
end
end.
Fixpoint ins x s :=
match s with
| EE => T Red EE x EE
| T c a y b => match K.compare x y with
| Lt => balance c (ins x a) y b
| Eq => T c a x b
| Gt => balance c a y (ins x b)
end
end.
Definition makeBlack t :=
match t with
| EE => EE
| T _ a x b => T Black a x b
end.
Definition insert x s := makeBlack (ins x s).
Fixpoint ins' x s :=
match s with
| EE => Some (T Red EE x EE)
| T c a y b => match K.compare x y with
| Lt => match ins' x a with
| None => None
| Some s' => Some (balance c s' y b)
end
| Eq => None
| Gt => match ins' x b with
| None => None
| Some s' => Some (balance c a y s')
end
end
end.
Definition mem_add' x s :=
match ins' x s with
| None => None
| Some s' => Some (makeBlack s')
end.
(* The next several definitions are to handle deletion, which is quite complex! *)
Definition balance' tl k tr :=
match tl , tr with
| T Red (T Red a x b) y c, d => T Red (T Black a x b) y (T Black c k d)
| T Red a x (T Red b y c), d => T Red (T Black a x b) y (T Black c k d)
| a, T Red b y (T Red c z d) => T Red (T Black a k b) y (T Black c z d)
| a, T Red (T Red b y c) z d => T Red (T Black a k b) y (T Black c z d)
| _ , _ => T Black tl k tr
end.
Definition sub1 t :=
match t with
| EE => EE
| T _ a x b => T Red a x b
end.
Definition balleft tl k tr :=
match tl with
| T Red a x b => T Red (T Black a x b) k tr
| _ => match tr with
| T Black a y b => balance' tl k (T Red a y b)
| T Red (T Black a y b) z c => T Red (T Black tl k a) y (balance' b z (sub1 c))
| _ => T Red tl k tr (* impossible *)
end
end.
Definition balright tl k tr :=
match tr with
| T Red b y c => T Red tl k (T Black b y c)
| _ => match tl with
| T Black a x b => balance' (T Red a x b) k tr
| T Red a x (T Black b y c) => T Red (balance' (sub1 a) x b) y (T Black c k tr)
| _ => T Red tl k tr (* impossible *)
end
end.
Fixpoint size t := match t with EE => 0 | T _ tl _ tr => 1 + size tl + size tr end.
Require Recdef Omega.
Function append (tlr: tree*tree) {measure (fun tlr => size (fst tlr) + size (snd tlr)) tlr} : tree :=
match tlr with
| (EE, x) => x
| (x, EE) => x
| (T Red a x b, T Red c y d) =>
match append (b,c) with
| T Red b' z c' => T Red (T Red a x b') z (T Red c' y d)
| bc => T Red a x (T Red bc y d)
end
| (T Black a x b, T Black c y d) =>
match append (b,c) with
| T Red b' z c' => T Red (T Black a x b') z (T Black c' y d)
| bc => balleft a x (T Black bc y d)
end
| (a, T Red b x c) => T Red (append(a,b)) x c
| (T Red a x b, c) => T Red a x (append(b,c))
end.
Proof.
intros; subst; simpl; omega.
intros; subst; simpl; omega.
intros; subst; simpl; omega.
intros; subst; simpl; omega.
Defined.
Fixpoint del x t :=
match t with
| EE => EE
| T _ a y b =>
match K.compare x y with
| Lt => match a with
| T Black _ _ _ => balleft (del x a) y b
| _ => T Red (del x a) y b
end
| Gt => match b with
| T Black _ _ _ => balright a y (del x b)
| _ => T Red a y (del x b)
end
| Eq => append(a,b)
end
end.
Definition delete x t :=
match del x t with
| T _ a y b => T Black a y b
| EE => EE
end.
Fixpoint choose' (t : tree) : option key :=
match t with
| EE => None
| T _ EE k tr => Some k
| T _ tl k tr => choose' tl
end.
Fixpoint max_elt' (t : tree) : option key :=
match t with
| EE => None
| T _ tl k EE => Some k
| T _ tl k tr => max_elt' tr
end.
Fixpoint delmin a y b : (key * tree) :=
match a with
| EE => (y,b)
| T Black aa ya ba =>
match delmin aa ya ba with
(k, a') => (k,balleft a' y b)
end
| T Red aa ya ba =>
match delmin aa ya ba with
(k,a') => (k, T Red a' y b)
end
end.
Definition delete'_min t : option (key * tree) :=
match t with
| EE => None
| T _ a y b =>
match delmin a y b with
| (k, T _ a y b) => Some (k, T Black a y b)
| (k, EE) => Some (k,EE)
end
end.
Definition bogus : tree * list key := (EE, nil).
Fixpoint treeify_f (n: positive) (l: list key) : tree * list key:=
match n with
| xH => match l with x::l1 => (T Red EE x EE, l1) | _ => bogus end
| xO n' => match treeify_f n' l with
| (t1, x::l2) => let (t2,l3) := treeify_g n' l2 in (T Black t1 x t2, l3)
| _ => bogus
end
| xI n' => match treeify_f n' l with
| (t1, x::l2) => let (t2,l3) := treeify_f n' l2 in (T Black t1 x t2, l3)
| _ => bogus
end
end
with treeify_g (n: positive) (l: list key) : tree * list key :=
match n with
| xH => (EE,l)
| xO n' => match treeify_g n' l with
| (t1, x::l2) => let (t2,l3) := treeify_g n' l2 in (T Black t1 x t2, l3)
| _ => bogus
end
| xI n' => match treeify_f n' l with
| (t1, x::l2) => let (t2,l3) := treeify_g n' l2 in (T Black t1 x t2, l3)
| _ => bogus
end
end.
Fixpoint poslength {A} (l: list A) :=
match l with nil => 1%positive | _::tl => Psucc (poslength tl) end.
Definition treeify (l: list key) : tree := fst (treeify_g (poslength l) l).
(* Now that the program has been defined, it's time to prove its properties.
A red-black tree has two kinds of properties:
[searchtree]: the keys in each left subtree are all less than the node's key,
and the keys in each right subtree are greater
[redblack]: there is the same number of black nodes on any path from
the root to each leaf; and there are never two red nodes in a row.
First, we'll treat the [searchtree] property.
*)
Ltac inv H := inversion H; clear H; subst.
Hint Immediate eq_sym.
Ltac do_compare x y := destruct (CompSpec2Type (K.compare_spec x y)).
Ltac do_ins_not_EE :=
repeat match goal with
| |- match ?A with Red => _ | Black => _ end <> _=> destruct A
| |- match ?A with EE => _ | T _ _ _ _ => _ end <> _=>destruct A
end; intro Hx; inversion Hx.
Lemma ins_not_EE: forall x s, ins x s <> EE.
Proof.
intros. induction s; simpl. intro Hx; inversion Hx.
do_compare x t.
congruence.
destruct c; simpl; repeat do_ins_not_EE.
destruct c; simpl; repeat do_ins_not_EE.
Qed.
Definition ltopt (x y : option key) :=
match x, y with
| Some x', Some y' => K.lt x' y'
| _, _ => True
end.
Inductive searchtree: option key -> option key -> tree -> Prop :=
| STE: forall lo hi, ltopt lo hi -> searchtree lo hi EE
| STT: forall c tl k tr lo hi,
searchtree lo (Some k) tl ->
searchtree (Some k) hi tr ->
searchtree lo hi (T c tl k tr) .
Ltac do_searchtree :=
assumption ||
constructor ||
match goal with
| |- searchtree _ _ (match ?C with Red => _ | Black => _ end) => destruct C
| |- searchtree _ _ (match ?C with EE => _ | T _ _ _ _ => _ end) => destruct C
| H: searchtree _ _ EE |- _ => inv H
| H: searchtree _ _ (T _ _ _ _) |- _ => inv H
| |- ltopt _ _ => unfold ltopt in *; auto
| |- match ?A with Some _ => _ | None => _ end => destruct A
| H: K.lt ?A ?B |- K.lt ?A ?C =>
try solve [apply lt_trans with B; assumption]; clear H
end.
Lemma searchtree_balance:
forall c s1 t s2 lo hi,
ltopt lo (Some t) -> ltopt (Some t) hi ->
searchtree lo (Some t) s1-> searchtree (Some t) hi s2 ->
searchtree lo hi (balance c s1 t s2).
Proof.
intros.
unfold balance.
repeat do_searchtree.
Qed.
Definition eqopt (a b : option key) :=
match a, b with
| Some x, Some y => K.eq x y
| _ , _ => False
end.
Lemma searchtree_expand_left:
forall k t lo hi, searchtree (Some k) hi t ->
(eqopt lo (Some k) \/ ltopt lo (Some k)) -> searchtree lo hi t.
Proof.
induction t; intros.
constructor.
inv H.
destruct H0; destruct lo, hi; simpl in *; auto. rewrite H; auto.
apply lt_trans with k; auto.
destruct H0.
inv H. constructor; auto.
inv H. constructor; auto.
Qed.
Lemma searchtree_expand_right:
forall k t lo hi, searchtree lo (Some k) t ->
(eqopt (Some k) hi \/ ltopt (Some k) hi) -> searchtree lo hi t.
Proof.
induction t; intros.
constructor.
inv H.
destruct H0; destruct lo, hi; simpl in *; auto. rewrite H in H1; auto.
apply lt_trans with k; auto.
destruct H0.
inv H. constructor; auto.
inv H. constructor; auto.
Qed.
Lemma searchtree_None:
forall s lo hi, searchtree lo hi s -> searchtree None None s.
Proof.
intros.
destruct hi.
apply searchtree_expand_right with t; simpl in *; auto.
destruct lo; auto.
apply searchtree_expand_left with t0; simpl in *; auto.
destruct lo; auto.
apply searchtree_expand_left with t; simpl in *; auto.
Qed.
Lemma searchtree_mid_congr:
forall c c' s1 x y s2 lo hi,
searchtree lo hi (T c s1 x s2) -> K.eq x y -> searchtree lo hi (T c' s1 y s2).
Proof.
intros.
inv H; constructor; auto.
eapply searchtree_expand_right; eauto.
eapply searchtree_expand_left; eauto.
simpl; auto.
Qed.
Lemma searchtree_proper_bounds: forall lo hi t, searchtree lo hi t -> ltopt lo hi.
Proof.
induction 1; auto.
destruct lo; simpl in *; auto. destruct hi; simpl in *; auto. apply lt_trans with k; auto.
Qed.
Lemma ins_is_searchtree:
forall x s lo hi, ltopt lo (Some x) -> ltopt (Some x) hi ->
searchtree lo hi s ->
searchtree lo hi (ins x s).
Proof.
induction s; simpl; intros.
repeat constructor; auto.
do_compare x t; auto.
eapply searchtree_mid_congr; eauto.
inv H1.
apply searchtree_balance; auto; eapply searchtree_proper_bounds; eauto.
inv H1.
apply searchtree_balance; auto; eapply searchtree_proper_bounds; eauto.
Qed.
Lemma insert_searchtree: forall x s,
searchtree None None s -> searchtree None None (insert x s).
Proof.
unfold insert; intros.
apply (ins_is_searchtree x) in H; simpl; auto.
destruct (ins x s); simpl in *; auto.
inv H. constructor; auto.
Qed.
Inductive interp: tree -> (key -> Prop) :=
| member_here: forall x y c tl tr, K.eq x y -> interp (T c tl y tr) x
| member_left: forall x y c tl tr, interp tl x -> interp (T c tl y tr) x
| member_right: forall x y c tl tr, interp tr x -> interp (T c tl y tr) x.
Lemma interp_empty: forall x, ~ interp EE x.
Proof.
intros; intro H; inv H.
Qed.
Hint Constructors interp.
Lemma generalize_bounds:
forall t, searchtree None None t -> exists lo, exists hi, searchtree lo hi t.
Proof.
intros; eauto.
Qed.
Lemma interp_range:
forall x tr lo hi, searchtree lo hi tr -> interp tr x -> ltopt lo (Some x) /\ ltopt (Some x) hi.
Proof.
induction 1; intros.
inv H0.
inv H1.
split.
destruct lo; simpl; auto. rewrite H7; apply searchtree_proper_bounds in H; auto.
destruct hi; simpl; auto. rewrite H7; apply searchtree_proper_bounds in H0; auto.
destruct (IHsearchtree1 H7); split; auto.
destruct hi; simpl; auto. apply lt_trans with k; auto.
apply searchtree_proper_bounds in H0; auto.
destruct (IHsearchtree2 H7); split; auto.
destruct lo; simpl; auto.
apply lt_trans with k; eauto.
apply searchtree_proper_bounds in H; auto.
Qed.
Ltac etac :=
match goal with
| H: interp EE _ |- _ => inv H
| H: InA _ _ nil |- _ => inv H
| H: false = true |- _ => inv H
| H: true = false |- _ => inv H
| _ => auto
end.
Lemma interp_member:
forall x t,
searchtree None None t ->
(member x t = true <-> interp t x).
Proof.
intros.
case_eq (member x t); intuition.
clear - H0; induction t; simpl in *. inv H0.
do_compare x t2; [constructor 1 | constructor 2 | constructor 3]; auto.
inv H1.
elimtype False; revert H0 H1; induction H; intros; simpl in *.
inv H1.
inv H2.
rewrite H8 in H1; rewrite compare_refl in H1; inv H1.
destruct (interp_range _ _ _ _ H H8).
simpl in H3; rewrite <- compare_lt_iff in H3; rewrite H3 in H1; auto.
destruct (interp_range _ _ _ _ H0 H8).
simpl in H2; rewrite <- compare_gt_iff in H2; rewrite H2 in H1; auto.
Qed.
Lemma interp_makeBlack:
forall t x, interp t x <-> interp (makeBlack t) x.
Proof.
destruct t; simpl; intuition;
(inv H; [ constructor; auto | constructor 2; auto | constructor 3; auto]).
Qed.
Ltac do_interp_balance :=
match goal with
| |- interp (match ?A with Red => _ | Black => _ end) _ => destruct A
| |- interp (match ?A with EE => _ | T _ _ _ _ => _ end) _ => destruct A
| H: interp (T _ _ _ _) _ |- _ => inv H
| H: interp (match ?A with Red => _ | Black => _ end) _ |- _ => destruct A
| H: interp (match ?A with EE => _ | T _ _ _ _ => _ end) _ |- _ => destruct A
end; auto.
Lemma interp_balance:
forall c tl k tr y, interp (balance c tl k tr) y <-> interp (T c tl k tr) y.
Proof.
destruct c, tl, tr; unfold balance; intuition; repeat do_interp_balance.
Qed.
Lemma ins_ok:
forall x y t lo hi,
searchtree lo hi t ->
((K.eq x y \/ interp t x) <-> interp (ins y t) x).
Proof.
induction 1; simpl; intuition;
[inv H0; auto | ..];
try solve [do_compare y k; auto; apply interp_balance; auto].
do_compare y k; auto.
inv H8; auto. constructor 1; auto. apply eq_trans with k; auto.
apply interp_balance; auto.
inv H8; auto.
apply interp_balance; auto.
inv H8; auto.
do_compare y k; auto.
inv H3; auto.
apply interp_balance in H3.
inv H3; auto.
apply H2 in H13.
destruct H13; auto.
apply interp_balance in H3. inv H3; auto.
apply H4 in H13.
destruct H13; auto.
Qed.
Lemma interp_insert:
forall x y s,
searchtree None None s ->
((K.eq x y \/ interp s x) <-> interp (insert y s) x).
Proof.
unfold insert; intros.
destruct (ins_ok x y s None None H).
intuition; try apply -> interp_makeBlack; auto.
apply <- interp_makeBlack in H0; auto.
Qed.
Inductive is_redblack : tree -> color -> nat -> Prop :=
| IsRB_leaf: forall c, is_redblack EE c 0
| IsRB_r: forall tl k tr n,
is_redblack tl Red n ->
is_redblack tr Red n ->
is_redblack (T Red tl k tr) Black n
| IsRB_b: forall c tl k tr n,
is_redblack tl Black n ->
is_redblack tr Black n ->
is_redblack (T Black tl k tr) c (S n).
Lemma is_redblack_toblack:
forall s n, is_redblack s Red n -> is_redblack s Black n.
Proof.
intros; inversion H; clear H; subst; try constructor; auto.
Qed.
Hint Resolve is_redblack_toblack.
Lemma is_redblack_Black_to_Red:
forall s n, is_redblack s Black n ->
exists n, is_redblack (makeBlack s) Red n.
Proof.
intros.
inv H; repeat econstructor; eauto.
Qed.
Inductive infrared : tree -> nat -> Prop :=
(* This predicate expresses, "the tree is a red-black tree, except that
it is permitted to have two red nodes in a row at the very root (only)." *)
| infrared_e: infrared EE 0
| infrared_r: forall tl k tr n,
is_redblack tl Black n ->
is_redblack tr Black n ->
infrared (T Red tl k tr) n
| infrared_b: forall tl k tr n,
is_redblack tl Black n ->
is_redblack tr Black n ->
infrared (T Black tl k tr) (S n).
Ltac infrared_tac :=
repeat (
auto ||
constructor ||
match goal with
| |- infrared (match ?A with
| EE => _
| T _ _ _ _ => _
end) _ =>
match goal with
| H: is_redblack A _ _ |- _ => inv H
| H: infrared A _ |- _ => inv H
end
| |- is_redblack (match ?A with
| EE => _
| T _ _ _ _ => _
end) _ _ =>
match goal with
| H: is_redblack A _ _ |- _ => inv H
| H: infrared A _ |- _ => inv H
end
| |- infrared (match K.compare ?A ?B with Eq => _ | Lt => _ | Gt => _ end) _ =>
do_compare A B
| |- is_redblack (match K.compare ?A ?B with Eq => _ | Lt => _ | Gt => _ end) _ _ =>
do_compare A B
end).
Lemma ins_is_redblack:
forall x s n,
(is_redblack s Black n -> infrared (ins x s) n) /\
(is_redblack s Red n -> is_redblack (ins x s) Black n).
(* This one is tedious with proof automation,
but extremely tedious without proof automation *)
Proof.
induction s; simpl; split; intros; inversion H; clear H; subst.
repeat (constructor; auto).
repeat (constructor; auto).
destruct (IHs1 n); clear IHs1.
destruct (IHs2 n); clear IHs2.
specialize (H2 H6).
specialize (H0 H5).
clear H H1.
do_compare x t; constructor; auto.
rename n0 into n.
destruct (IHs1 n); clear IHs1.
destruct (IHs2 n); clear IHs2.
specialize (H H6); specialize (H1 H7).
clear H0 H2.
simpl.
infrared_tac.
rename n0 into n.
destruct (IHs1 n); clear IHs1.
destruct (IHs2 n); clear IHs2.
specialize (H H6).
specialize (H1 H7).
clear H0 H2.
simpl; infrared_tac.
Qed.
Lemma insert_is_redblack:
forall x s n, is_redblack s Red n ->
exists n', is_redblack (insert x s) Red n'.
Proof.
intros.
unfold insert.
destruct (ins_is_redblack x s n).
apply is_redblack_Black_to_Red with n; auto.
Qed.
Definition valid (x: tree) := searchtree None None x /\
(exists n, is_redblack x Red n).
(* NOW FOR THE HARD PART, DELETION!
Here we follow the deletion algorithm of Stefan Kahrs (2001),
and in particular,
http://www.cs.kent.ac.uk/people/staff/smk/redblack/Untyped.hs
*)
Ltac hax :=
repeat (auto; try solve [repeat constructor; auto];
match goal with
| H: is_redblack _ Red _ |- _ => inv H
| H: is_redblack _ _ 0 |- _ => inv H
| H: is_redblack _ _ (S _) |- _ => inv H
| H: infrared _ 0 |- _ => inv H
| H: infrared _ (S _) |- _ => inv H
end).
Lemma balance'_shape:
forall n tl k tr,
(is_redblack tl Black n /\ infrared tr n \/ infrared tl n /\ is_redblack tr Black n) ->
is_redblack (balance' tl k tr) Black (S n).
Proof.
induction n; intros; hax.
destruct H as [[? ?] | [? ?]]; hax.
destruct H as [[? ?] | [? ?]]; hax.
Qed.
Lemma searchtree_balance':
forall s1 t s2 lo hi,
searchtree lo (Some t) s1 -> searchtree (Some t) hi s2 ->
searchtree lo hi (balance' s1 t s2).
Proof.
intros.
unfold balance'.
repeat do_searchtree.
Qed.
Lemma searchtree_append_aux:
forall n tl tr, size tl + size tr < n ->
forall lo k hi,
searchtree lo (Some k) tl -> searchtree (Some k) hi tr ->
searchtree lo hi (append(tl,tr)).
Proof.
induction n; intros.
destruct tl; destruct tr; elimtype False; omega.
rewrite append_equation.
inv H0.
eapply searchtree_expand_left; eauto.
destruct c.
assert (searchtree (Some k0) hi (append(tr0,tr))).
apply IHn with k; auto.
simpl in H; omega.
inv H1.
constructor; auto.
rewrite append_equation in H0.
destruct tr0; try destruct c; auto.
destruct c.
assert (searchtree (Some k0) (Some k1) (append(tr0,tl))).
apply IHn with k; auto.
simpl in H; omega.
inv H1.
repeat constructor; auto.
destruct c; repeat constructor; auto.
constructor; auto.
inv H1.
repeat constructor; auto.
eapply searchtree_expand_right; eauto.
destruct c.
repeat constructor; auto.
apply IHn with k; auto.
simpl in H|-*; omega.
repeat constructor; auto.
assert (searchtree (Some k0) (Some k1) (append(tr0,tl))).
apply IHn with k; auto.
simpl in H|-*; omega.
inv H1.
unfold balleft.
inv H2.
apply searchtree_balance'; auto.
repeat constructor; auto.
repeat constructor; auto.
destruct c.
repeat constructor; auto.
apply searchtree_balance'; auto.
repeat constructor; auto.
repeat constructor; auto.
destruct c.
repeat constructor; auto.
unfold balleft.
inv H2.
apply searchtree_balance'; auto.
constructor; auto.
repeat constructor; auto.
destruct c.
repeat constructor; auto.
apply searchtree_balance'; auto.
repeat constructor; auto.
repeat constructor; auto.
Qed.
Lemma searchtree_append:
forall tl tr lo k hi,
searchtree lo (Some k) tl -> searchtree (Some k) hi tr->
searchtree lo hi (append(tl,tr)).
Proof.
intros.
apply searchtree_append_aux with (S (size tl + size tr)) k; auto.
Qed.
Lemma searchtree_sub1: forall t lo hi, searchtree lo hi t -> searchtree lo hi (sub1 t).
Proof.
destruct t; intros; inv H; repeat constructor; auto.
Qed.
Lemma del_is_searchtree:
forall x t lo hi,
searchtree lo hi t -> searchtree lo hi (del x t).
Proof.
induction 1; simpl; intros.
constructor; auto.
do_compare x k.
(* Case 1 *)
apply searchtree_append with k; auto.
(* Case 2*)
inv H.
constructor; auto.
destruct c0.
constructor; auto.
remember (del x (T Black tl0 k0 tr0)) as t.
clear - H1 H2 H0 IHsearchtree1.
unfold balleft.
inv IHsearchtree1.
inv H0.
hax.
destruct c.
inv H3.
hax.
destruct c.
hax.
repeat constructor; auto.
apply searchtree_balance'; auto.
apply searchtree_sub1; auto.
apply searchtree_balance'; auto.
constructor; auto.
repeat constructor; auto.
destruct c.
repeat constructor; auto.
inv H0.
hax.
destruct c.
inv H4.
hax.
destruct c.
hax.
repeat constructor; auto.
apply searchtree_balance'; auto.
apply searchtree_sub1; auto.
apply searchtree_balance'; auto.
repeat constructor; auto.
repeat constructor; auto.
(* Case 3 *)
inv H0.
constructor; auto.
destruct c0.
constructor; auto.
remember (del x (T Black tl0 k0 tr0)) as t.
clear - H1 H2 H IHsearchtree2.
rename H into H0.
unfold balright.
inv IHsearchtree2.
inv H0.
hax.
destruct c.
inv H4.
hax.
destruct c.
hax.
repeat constructor; auto.
apply searchtree_balance'; auto.
apply searchtree_sub1; auto.
apply searchtree_balance'; auto.
repeat constructor; auto.
constructor; auto.
destruct c.
repeat constructor; auto.
inv H0.
hax.
destruct c.
inv H5.
hax.
destruct c.
hax.
repeat constructor; auto.
apply searchtree_balance'; auto.
apply searchtree_sub1; auto.
apply searchtree_balance'; auto.
repeat constructor; auto.
repeat constructor; auto.
Qed.
Definition is_red_or_empty t :=
match t with T Black _ _ _ => False | _ => True end.
Definition is_black t :=
match t with T Black _ _ _ => True | _ => False end.
Lemma append_shape_aux:
forall sz tl tr, size tl + size tr < sz ->
(forall n, is_redblack tl Black n -> is_redblack tr Black n -> infrared (append (tl,tr)) n) /\
(forall n, is_redblack tl Red n -> is_redblack tr Red n -> is_redblack (append (tl,tr)) Black n).
Proof.
induction sz; intros.
elimtype False; omega.
split; intros; rewrite append_equation.
inv H0. hax.
inv H2; inv H3.
inv H1. hax.
inv H0; inv H2.
rewrite append_equation; simpl. hax.
inv H1.
inv H2; inv H3.
assert (is_redblack (append (T Black tl0 k1 tr2, T Black tl2 k3 tr)) Black (S n0)).
apply IHsz. simpl in H|-*; omega.
hax. hax.
inv H1.
hax. hax.
constructor; auto. hax.
apply IHsz. simpl in H|-*; omega.
hax. hax.
inv H1.
hax.
constructor; auto.
apply IHsz. simpl in H|-*; omega.
hax. hax. hax.
assert (infrared (append(tr0,tl)) n0).
apply IHsz. simpl in H|-*; omega.
hax. hax.
inv H0.
unfold balleft.
inv H2.
simpl.
inv H7. hax. hax. hax. hax.
inv H2; hax.
inv H0; inv H1.
hax.
assert (infrared (append(tr0,tl)) n0).
apply IHsz; auto. simpl in H|-*; omega.
inv H0.
hax.
hax.
hax.
Qed.
Lemma infrared_append:
forall tl tr n, is_redblack tl Black n -> is_redblack tr Black n ->
infrared (append (tl,tr)) n.
Proof.
intros.
apply append_shape_aux with (S (size tl + size tr)); auto.
Qed.
Lemma append_is_redblack:
forall tl tr n, is_redblack tl Red n -> is_redblack tr Red n ->
is_redblack (append (tl,tr)) Black n.
Proof.
intros.
apply append_shape_aux with (S (size tl + size tr)); auto.
Qed.
Lemma balance'_shape2:
forall n tl k tr,
is_redblack tl Black n -> is_redblack tr Black n ->
is_redblack (balance' tl k tr) Red (S n).
Proof.
induction n; intros; hax.
Qed.
Lemma del_shape:
forall x t,
(forall n, is_redblack t Red (S n) -> is_black t -> infrared (del x t) n) /\
(forall n, is_redblack t Black n -> is_red_or_empty t -> is_redblack (del x t) Black n).
Proof.
intros x t.
pose (H:=True).
induction t; split.
intros; inv H0.
intros; inv H0.
simpl. constructor.
pose (H0:=True); pose (H1:=True); pose (H2:=True).
intros.
simpl in H4; destruct c; try contradiction; clear H4.
inv H3.
simpl.
rename t2 into k.
do_compare x k.
(* Case 1*)
apply infrared_append; auto.
(* Case 2 *)
destruct IHt1 as [ST1a ST1b]; destruct IHt2 as [ST2a ST2b].
inv H8.
repeat constructor; auto.
assert (is_redblack (del x (T Red tl k0 tr)) Black n).
apply ST1b. constructor; auto. simpl; auto.
remember (del x (T Red tl k0 tr)) as t.
repeat constructor; auto.
remember (del x (T Black tl k0 tr)) as t.
assert (infrared t n0).
apply ST1a; repeat constructor; auto.
inv H5.
inv H10.
simpl.
inv H5; repeat constructor; auto.
apply balance'_shape.
left; split; auto.
clear - H7; inv H7; simpl in *; constructor; auto.
simpl.
inv H7.
inv H11.
repeat constructor; auto.
repeat constructor; auto.
inv H11; repeat constructor; auto.
simpl.
repeat constructor; auto.
simpl.
inv H10; repeat constructor; auto.
inv H5; repeat constructor; auto.
apply balance'_shape.