-
Notifications
You must be signed in to change notification settings - Fork 0
/
AVL.v
1246 lines (1162 loc) · 40.2 KB
/
AVL.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
Require Import Nat Arith Omega.
Require Sumbool.
Require Import Relations RelationClasses Basics.
Require Import Orders OrdersFacts.
Set Implicit Arguments.
Ltac destruct_first H a := destruct H as [a H].
Ltac rewrite_maxlr_with a b :=
first [
((rewrite (Z.max_l a b) in *) + (rewrite (Z.max_r a b) in *)); [| solve [omega]]
].
Ltac rewrite_maxlr_hyp :=
repeat multimatch reverse goal with
| [ H : ?P |- _ ] =>
repeat match P with
context A [Z.max ?x ?y] => rewrite_maxlr_with x y
end
end.
Ltac rewrite_maxlr_goal :=
repeat match goal with
| [ |- ?A ]=>
match A with
context B [Z.max ?x ?y] => rewrite_maxlr_with x y
end
end.
Ltac rewrite_maxlr := rewrite_maxlr_hyp; rewrite_maxlr_goal.
Module Type AVL.
Declare Module A : UsualOrderedTypeFull'.
Module F := OrderedTypeFacts A.
Inductive Tree : Type :=
| leaf : Tree
| node (value : A.t) (balance : Z) (lc rc : Tree) : Tree.
Definition balance (t : Tree) :=
match t with
| leaf => 0%Z
| node _ b _ _ => b
end.
Inductive TIn : A.t -> Tree -> Prop :=
| tin_top : forall x c l r, TIn x (node x c l r)
| tin_left : forall x v c l r, TIn x l -> TIn x (node v c l r)
| tin_right : forall x v c l r, TIn x r -> TIn x (node v c l r).
Hint Constructors TIn.
Lemma tin_child : forall x v c l r, TIn x l \/ TIn x r -> TIn x (node v c l r).
Proof.
intros.
destruct H; auto.
Qed.
Hint Resolve tin_child.
Lemma tin_child_iff : forall x v c l r,
x = v \/ TIn x l \/ TIn x r <-> TIn x (node v c l r).
Proof.
intros.
split; intro H.
- repeat destruct H as [H | H]; subst; auto.
- inversion H; auto.
Qed.
Definition TIn_dec : forall (x : A.t) (t : Tree), {TIn x t} + {~TIn x t}.
refine (fix f (x : A.t) (t : Tree) :=
match t with
| leaf => right _
| node v d lc rc =>
if A.eq_dec x v
then left _
else if Sumbool.sumbool_or _ _ _ _ (f x lc) (f x rc)
then left _
else right _
end).
- inversion 1.
- subst.
constructor.
- auto.
- intro Hin.
inversion Hin; subst; intuition.
Defined.
Fixpoint depth (t : Tree) : Z :=
match t with
| leaf => 0
| node _ _ l r => 1 + Z.max (depth l) (depth r)
end.
Arguments depth _ : simpl nomatch.
Functional Scheme depth_ind := Induction for depth Sort Prop.
Lemma depth_positive : forall (t : Tree), (depth t >= 0)%Z.
Proof.
intros.
functional induction (depth t).
- intuition.
- destruct (Zmax_spec (depth l) (depth r)); omega.
Qed.
Lemma depth_node : forall x c l r, depth (node x c l r) = (1 + Z.max (depth l) (depth r))%Z.
Proof.
intros.
reflexivity.
Qed.
Ltac try_decide_depth_node := repeat rewrite depth_node in *; rewrite_maxlr.
(* has correct balancefactor *)
Inductive Correct : Tree -> Prop :=
| c_leaf : Correct (leaf)
| c_node : forall v c l r,
Correct l -> Correct r ->
c = (depth l - depth r)%Z ->
Correct (node v c l r).
Derive Inversion_clear correct_node_inv with (forall v c l r, Correct (node v c l r)) Sort Prop.
Lemma correct_node_iff : forall v c l r,
Correct (node v c l r) <->
Correct l /\ Correct r /\ (c = depth l - depth r)%Z.
Proof.
intros v c l r.
split.
- inversion 1; auto.
- constructor; intuition.
Qed.
Lemma Correct_ind' : forall P : Tree -> Prop,
P leaf ->
(forall v l r,
Correct l -> P l -> Correct r -> P r -> depth l = depth r -> P (node v 0 l r)) ->
(forall v l r,
Correct l -> P l -> Correct r -> P r -> (depth l < depth r)%Z -> P (node v (depth l - depth r) l r)) ->
(forall v l r,
Correct l -> P l -> Correct r -> P r -> (depth l > depth r)%Z -> P (node v (depth l - depth r) l r)) ->
forall t, Correct t -> P t.
Proof.
intros.
induction H3; auto.
subst.
destruct Z.lt_total with (depth l) (depth r) as [Hc | Hc]; [ | destruct Hc as [Hc | Hc]];
intuition.
rewrite Hc.
rewrite Z.sub_diag.
auto.
Qed.
Inductive Balanced : Tree -> Prop :=
| bl_lefa : Balanced leaf
| bl_node : forall v c l r,
Balanced l -> Balanced r ->
(Z.abs c <= 1)%Z ->
Balanced (node v c l r).
Derive Inversion_clear balanced_node_inv with (forall v c l r, Balanced (node v c l r)) Sort Prop.
Inductive Valid : Tree -> Prop := valid t : Correct t -> Balanced t -> Valid t.
Lemma Valid_ind'
: forall P : Tree -> Prop,
P leaf ->
(forall (v : A.t) (c : Z) (l r : Tree),
Valid l -> P l -> Valid r -> P r -> (Z.abs c <= 1)%Z -> P (node v c l r)) ->
forall t : Tree, Valid t -> P t.
Proof.
intros P Hl Hn t Hv.
destruct Hv as [t Hc Hb].
induction Hb; auto.
inversion_clear Hc.
apply Hn; auto.
- constructor; auto.
- constructor; auto.
Qed.
Lemma valid_node_inv
: forall (v : A.t) (c : Z) (l r : Tree) (P : A.t -> Z -> Tree -> Tree -> Prop),
(Valid l -> Valid r -> c = (depth l - depth r)%Z -> (Z.abs c <= 1)%Z -> P v c l r) ->
Valid (node v c l r) -> P v c l r.
Proof.
intros v c l r P H.
inversion_clear 1.
inversion_clear H1.
inversion_clear H2.
apply H; repeat constructor; intuition.
Qed.
Fixpoint flip_rec (t : Tree) : Tree :=
match t with
| leaf => leaf
| node v c l r => node v (-c) (flip_rec r) (flip_rec l)
end.
Functional Scheme flip_rec_ind := Induction for flip_rec Sort Prop.
Lemma flip_rec_depth : forall t, depth (flip_rec t) = depth t.
Proof.
intros.
induction t; simpl; auto.
repeat rewrite depth_node.
rewrite Z.max_comm.
congruence.
Qed.
Lemma flip_rec_correct : forall t, Correct (flip_rec t) <-> Correct t.
Proof.
intros.
functional induction (flip_rec t).
- reflexivity.
- repeat rewrite correct_node_iff.
repeat rewrite IHt0.
repeat rewrite IHt1.
repeat rewrite flip_rec_depth.
intuition.
Qed.
Lemma flip_rec_balanced : forall t, Balanced (flip_rec t) <-> Balanced t.
Proof.
intros.
functional induction (flip_rec t).
- reflexivity.
- split; intro Hb; constructor; inversion Hb using balanced_node_inv; intuition.
+ rewrite Z.abs_opp in *.
assumption.
+ rewrite Z.abs_opp.
assumption.
Qed.
Lemma flip_rec_valid : forall t, Valid (flip_rec t) <-> Valid t.
Proof.
intros.
split; inversion 1; constructor.
- rewrite <- flip_rec_correct; auto.
- rewrite <- flip_rec_balanced; auto.
- rewrite flip_rec_correct; auto.
- rewrite flip_rec_balanced; auto.
Qed.
Lemma flip_rec_involutive : forall (t : Tree), flip_rec (flip_rec t) = t.
Proof.
induction t; simpl; intuition.
rewrite Z.opp_involutive.
congruence.
Qed.
Lemma flip_rec_in : forall t x, TIn x (flip_rec t) <-> TIn x t.
Proof.
intros.
functional induction (flip_rec t); intuition.
- inversion_clear H3; auto.
- inversion_clear H3; auto.
Qed.
Definition TForall (P : A.t -> Prop) (t : Tree) : Prop := (forall x, TIn x t -> P x).
Inductive TForall' : (A.t -> Prop) -> Tree -> Prop :=
| tforall_leaf : forall P, TForall' P leaf
| tforall_node : forall P v c l r , TForall' P l -> TForall' P r -> P v -> TForall' P (node v c l r).
Hint Unfold TForall.
Lemma TForall_iff : forall P t, TForall P t <-> TForall' P t.
Proof.
intros P t.
split; intro H.
- unfold TForall in *.
induction t; constructor; auto.
- unfold TForall.
intro x.
induction H; inversion 1; auto.
Qed.
Lemma flip_TForall : forall P t, TForall P (flip_rec t) <-> TForall P t.
Proof.
intros.
unfold TForall in *.
split; intro H.
- intros x Hin.
apply H.
rewrite flip_rec_in; auto.
- intros x Hin.
apply H.
rewrite <- flip_rec_in; auto.
Qed.
Inductive RelTree (R : relation A.t) : Tree -> Prop :=
| rt_leaf : RelTree R leaf
| rt_node : forall v c l r,
RelTree R l -> RelTree R r ->
TForall (fun y => R y v) l -> TForall (fun y => R v y) r -> RelTree R (node v c l r).
Derive Inversion_clear reltree_node_inv with (forall R v c l r, RelTree R (node v c l r))
Sort Prop.
Hint Constructors RelTree.
Definition STree t := RelTree A.lt t.
Hint Unfold STree.
Lemma flip_rec_reltree : forall R t, RelTree (flip R) (flip_rec t) <-> RelTree R t.
Proof.
intros.
functional induction (flip_rec t).
- intuition.
- split; inversion 1;
(rewrite flip_TForall in * + rewrite <- flip_TForall in *); intuition.
Qed.
Fixpoint search (x : A.t) (t : Tree) : bool :=
match t with
| leaf => false
| node v c l r =>
match A.compare x v with
| Eq => true
| Lt => search x l
| Gt => search x r
end
end.
Functional Scheme search_ind := Induction for search Sort Prop.
Lemma search_spec : forall x t, STree t -> search x t = true <-> TIn x t.
Proof.
intros x t.
functional induction (search x t); intro Hst.
- split; inversion 1.
- split; auto.
rewrite F.compare_eq_iff in e0; subst.
constructor.
- rewrite F.compare_lt_iff in e0.
inversion Hst as [| v' c l' r' Hsl Hsr_ Hlt Hgt]; subst.
rewrite IHb; auto.
split; auto.
inversion 1; subst; auto; try F.order.
specialize (Hgt _ H2). simpl in Hgt.
F.order.
- rewrite F.compare_gt_iff in e0.
inversion Hst as [| v' c l' r' Hsl Hsr_ Hlt Hgt]; subst.
rewrite IHb; auto.
split; auto.
inversion 1; subst; auto; try F.order.
specialize (Hlt _ H2). simpl in Hlt.
F.order.
Qed.
Definition AVL t := Valid t /\ STree t.
Hint Unfold AVL.
Definition rrotate_help_comp (x y : Z) : Z * Z :=
(if Z_lt_le_dec 0 y
then if Z_lt_le_dec (x - 1 - y) 0 then (x - 2, x - 1 - y) else (y - 1, x - 1 - y)
else if Z_lt_le_dec (x - 1) 0 then (x + y - 2, x - 1) else (y - 1, x - 1)
)%Z.
Arguments rrotate_help_comp _ _ : simpl nomatch.
Functional Scheme rrotate_help_comp_ind := Induction for rrotate_help_comp Sort Prop.
Lemma rrotate_help_comp_ind'
: (forall (x y : Z) (P : Z * Z -> Prop),
(forall (Hy : 0 < y) (Hx : x - 1 - y < 0), P (x - 2, x - 1 - y)) ->
(forall (Hy : 0 < y) (Hx : 0 <= x - 1 - y), P (y - 1, x - 1 - y)) ->
(forall (Hy : y <= 0) (Hx : x - 1 < 0), P (x + y - 2, x - 1)) ->
(forall (Hy : y <= 0) (Hx : 0 <= x - 1), P (y - 1, x - 1)) ->
P (rrotate_help_comp x y))%Z.
Proof.
intros.
functional induction (rrotate_help_comp x y); auto.
Qed.
Definition rrotate_help v c lv lc ll lr r :=
let (cx, cy) := rrotate_help_comp c lc in
node lv cx ll (node v cy lr r).
Lemma rrotate_help_rel (R : relation A.t) `{Tr : Transitive _ R} : forall v c lv lc ll lr r,
RelTree R (node v c (node lv lc ll lr) r) -> RelTree R (rrotate_help v c lv lc ll lr r).
Proof.
intros.
unfold rrotate_help.
destruct rrotate_help_comp as [cx cy].
inversion H as [| v' c' l' r' Hsl Hsr Hlt Hgt]; subst.
inversion Hsl as [| lv' lc' ll' lr' Hsll Hslr Hllt Hlgt]; subst.
constructor; auto.
intro x.
inversion 1; eauto.
Qed.
Lemma correct_child_depth_l : forall v c l r,
(Correct (node v c l r) -> 0 <= c ->
depth l = depth (node v c l r) - 1 /\ depth r = depth (node v c l r) - 1 - c)%Z.
Proof.
intros v c l r Hc Hlt.
inversion Hc as [| ?v ?c ?l ?r Hcl Hcr Hceq]; subst; try discriminate.
remember (depth (node _ _ _ _)) as d.
rewrite depth_equation in Heqd.
rewrite Z.max_l in Heqd; try omega.
Qed.
Lemma correct_child_depth_r : forall v c l r,
(Correct (node v c l r) -> c <= 0 ->
depth l = depth (node v c l r) - 1 + c /\ depth r = depth (node v c l r) - 1)%Z.
Proof.
intros v c l r Hc Hlt.
inversion Hc as [| ?v ?c ?l ?r Hcl Hcr Hceq]; subst; try discriminate.
remember (depth (node _ _ _ _)) as d.
rewrite depth_equation in Heqd.
rewrite Z.max_r in Heqd; try omega.
Qed.
Lemma valid_depth_case : forall v c l r,
Valid (node v c l r) ->
(c = 0 /\ depth l = depth r /\ depth (node v c l r) = depth l + 1)%Z \/
(c = 1 /\ depth l = depth r + 1 /\ depth (node v c l r) = depth l + 1)%Z \/
(c = -1 /\ depth l = depth r - 1 /\ depth (node v c l r) = depth l + 2)%Z.
Proof.
assert (forall (P Q R : Prop), P -> (P -> Q) -> (P -> Q -> R) -> P /\ Q /\ R) as and3_help.
- intros.
intuition.
- intros.
inversion_clear H as [?t Hc Hb].
rewrite correct_node_iff in Hc.
repeat apply proj2 in Hc.
inversion Hb using balanced_node_inv; clear Hb.
intros Hbl Hbr Habs.
rewrite depth_node.
destruct (Z.abs_spec c) as [Habs' | Habs']; rewrite_maxlr; omega.
Qed.
Ltac solve_correct_child_depth a b c :=
first [
destruct (correct_child_depth_l a) as [b c]; [solve[omega]|] |
destruct (correct_child_depth_r a) as [b c]; [solve[omega]|]
].
Lemma rrotate_help_correct : forall v c lv lc ll lr r,
(0 <= c)%Z ->
Correct (node v c (node lv lc ll lr) r) ->
Correct (rrotate_help v c lv lc ll lr r).
Proof.
intros v c lv lc ll lr r.
intros Hc Hcor.
unfold rrotate_help.
solve_correct_child_depth Hcor H1 H2.
rewrite correct_node_iff in Hcor.
functional induction (rrotate_help_comp c lc) using rrotate_help_comp_ind'.
- destruct Hcor as [Hcorl Hcor]; destruct Hcor as [Hcorr Hdep1].
solve_correct_child_depth Hcorl H3 H4.
rewrite correct_node_iff in Hcorl.
repeat constructor; intuition.
try_decide_depth_node; omega.
- destruct Hcor as [Hcorl Hcor]; destruct Hcor as [Hcorr Hdep1].
solve_correct_child_depth Hcorl H3 H4.
rewrite correct_node_iff in Hcorl.
repeat constructor; intuition.
try_decide_depth_node; omega.
- destruct Hcor as [Hcorl Hcor]; destruct Hcor as [Hcorr Hdep1].
solve_correct_child_depth Hcorl H3 H4.
rewrite correct_node_iff in Hcorl.
repeat constructor; intuition.
try_decide_depth_node; omega.
- destruct Hcor as [Hcorl Hcor]; destruct Hcor as [Hcorr Hdep1].
solve_correct_child_depth Hcorl H3 H4.
rewrite correct_node_iff in Hcorl.
repeat constructor; intuition.
try_decide_depth_node; omega.
Qed.
Definition lrotate_help v c l rv rc rl rr :=
match (rrotate_help v (-c)%Z rv (-rc)%Z rr rl l) with
| node v' c' l' (node rv' rc' rl' rr') => node v' (-c') (node rv' (-rc')%Z rr' rl') l'
| _ => leaf
end.
Functional Scheme lrotate_help_ind := Induction for lrotate_help Sort Prop.
Lemma lrotate_help_ind' : forall (v : A.t) (c : Z) (l : Tree) (rv : A.t) (rc : Z) (rl rr : Tree) (P : Tree -> Prop),
(forall (v' : A.t) (c' : Z) (l' rc0 : Tree),
rrotate_help v (- c) rv (- rc) rr rl l = node v' c' l' rc0 ->
forall (rv' : A.t) (rc' : Z) (rl' rr' : Tree),
rc0 = node rv' rc' rl' rr' -> P (node v' (- c') (node rv' (- rc') rr' rl') l')) ->
P (lrotate_help v c l rv rc rl rr).
Proof.
intros.
functional induction (lrotate_help v c l rv rc rl rr); eauto.
- unfold rrotate_help in e.
destruct (rrotate_help_comp (-c) (-rc))%Z.
discriminate.
- unfold rrotate_help in e.
destruct (rrotate_help_comp (-c) (-rc))%Z.
discriminate.
Qed.
Lemma lrotate_help_correct : forall v c l rv rc rl rr,
(c <= 0)%Z ->
Correct (node v c l (node rv rc rl rr)) ->
Correct (lrotate_help v c l rv rc rl rr).
Proof.
intros.
functional induction (lrotate_help v c l rv rc rl rr) using lrotate_help_ind'.
cut (Correct (rrotate_help v (- c) rv (-rc) rr rl l)).
- intro Hcor.
rewrite H1 in Hcor.
repeat rewrite correct_node_iff in Hcor.
repeat constructor; intuition.
rewrite depth_node in *.
rewrite Z.max_comm.
omega.
- apply rrotate_help_correct; try omega.
repeat rewrite correct_node_iff in H0.
repeat constructor; intuition.
rewrite depth_node in *.
rewrite Z.max_comm.
omega.
Qed.
Lemma lrotate_help_rel (R : relation A.t) `{Tr : Transitive _ R} : forall v c l rv rc rl rr,
RelTree R (node v c l (node rv rc rl rr)) ->
RelTree R (lrotate_help v c l rv rc rl rr).
Proof.
intros.
functional induction (lrotate_help v c l rv rc rl rr) using lrotate_help_ind'.
unfold rrotate_help in H0.
destruct (rrotate_help_comp _ _ ) as [a b].
inversion H0; subst; clear H0.
inversion H using reltree_node_inv; intros HRrr HRrl Hlt Hgt.
inversion HRrl using reltree_node_inv; intros HRrl' HRl' Hlt' Hgt'.
repeat constructor; auto.
intro x.
inversion 1; eauto.
Qed.
Definition rrotate t : Tree :=
match t with
| node v c (node lv lc ll lr) r => rrotate_help v c lv lc ll lr r
| _ => t
end.
Arguments rrotate t : simpl nomatch.
Functional Scheme rrotate_ind := Induction for rrotate Sort Prop.
Lemma rrotate_reltree (R : relation A.t) `{Tr : Transitive _ R} : forall t, RelTree R t -> RelTree R (rrotate t).
Proof. intros. functional induction (rrotate t); eauto using rrotate_help_rel. Qed.
Lemma rrotate_in : forall t x, TIn x (rrotate t) <-> TIn x t.
Proof.
intros.
functional induction (rrotate t); try solve [intuition].
unfold rrotate_help.
destruct rrotate_help_comp as [cx cy].
repeat rewrite <- tin_child_iff.
intuition; auto.
Qed.
Definition lrotate t : Tree :=
match t with
| node v c l (node rv rc rl rr) => lrotate_help v c l rv rc rl rr
| _ => t
end.
Arguments lrotate t : simpl nomatch.
Functional Scheme lrotate_ind := Induction for lrotate Sort Prop.
Lemma lrotate_reltree (R : relation A.t) `{Tr : Transitive _ R} : forall t, RelTree R t -> RelTree R (lrotate t).
Proof. intros. functional induction (lrotate t); eauto using lrotate_help_rel. Qed.
Lemma lrotate_flip_eq : forall t, lrotate t = flip_rec (rrotate (flip_rec t)).
Proof.
intros.
induction t; simpl; auto.
unfold lrotate, rrotate.
destruct t2; simpl.
- rewrite Z.opp_involutive, flip_rec_involutive.
reflexivity.
- unfold lrotate_help.
unfold rrotate_help.
destruct (rrotate_help_comp _ _) as [cx cy].
simpl.
repeat rewrite flip_rec_involutive.
reflexivity.
Qed.
Lemma lrotate_in : forall t x, TIn x (lrotate t) <-> TIn x t.
Proof.
intros.
rewrite lrotate_flip_eq.
rewrite flip_rec_in.
rewrite rrotate_in.
rewrite flip_rec_in.
reflexivity.
Qed.
Definition rrot_balance v c l r :=
if (Z_lt_ge_dec (balance l) 0)%Z then rrotate (node v c (lrotate l) r) else rrotate (node v c l r).
Lemma rrot_balance_reltree R `{Tr : Transitive A.t R} : forall v c l r,
RelTree R (node v c l r) -> RelTree R (rrot_balance v c l r).
Proof.
intros.
unfold rrot_balance.
destruct Z_lt_ge_dec.
- destruct l as[| lv lc ll lr].
+ simpl in l0.
discriminate.
+ simpl in l0.
apply rrotate_reltree; auto.
inversion_clear H as [|?v ?c ?l ?r Hsl Hsr Hlt Hgt]; subst.
inversion Hsl as [|?v ?c ?l ?r Hsl' Hsr' Hlt' Hgt']; subst.
constructor; eauto using lrotate_reltree.
intro x.
rewrite lrotate_in.
eauto.
- apply rrotate_reltree; auto.
Qed.
Lemma rrotate_balance_depth_single :
forall v l r,
(balance l = 1)%Z -> Valid l -> Valid r -> Correct (node v 2 l r) ->
Valid (rrot_balance v 2 l r) /\
depth (rrot_balance v 2 l r) = (depth (node v 2 l r) - 1)%Z.
Proof.
intros v l r Hball HVl HVr HCt.
inversion_clear HVl as [?t HCl HBl].
inversion_clear HVr as [?t HCr HBr].
destruct l as [| lv lc ll lr]; simpl in Hball; try discriminate.
subst.
unfold rrot_balance.
simpl.
inversion HCl using correct_node_inv; intros HCll HClr Hlceq.
inversion HBl using balanced_node_inv; intros HBll HBlr Hlcabs.
unfold rrotate. unfold rrotate_help.
simpl.
assert (depth lr = depth r) as Heq. {
repeat rewrite correct_node_iff in HCt.
try_decide_depth_node; omega.
}
split.
- repeat constructor; simpl; eauto; try omega.
try_decide_depth_node; omega.
- try_decide_depth_node; omega.
Qed.
Lemma rrotate_balance_depth_double :
forall v l r, (balance l = -1)%Z -> Valid l -> Valid r -> Correct (node v 2 l r) ->
Valid (rrot_balance v 2 l r) /\
depth (rrot_balance v 2 l r) = (depth (node v 2 l r) - 1)%Z.
Proof.
intros v l r Hball HVl HVr HCt.
inversion_clear HVl as [?t HCl HBl].
inversion_clear HVr as [?t HCr HBr].
inversion HCt using correct_node_inv; clear HCt.
intros _ _ Hdept.
destruct l as [| lv lc ll lr]; simpl in Hball; try discriminate.
subst.
inversion HBl using balanced_node_inv; clear HBl.
intros HBll HBlr _.
inversion HCl using correct_node_inv; clear HCl.
intros HCll HClr Hdepl.
set (depth_positive ll) as Hpos; clearbody Hpos.
destruct lr as [| lrv lrc lrl lrr]; simpl in Hdepl; try omega.
inversion HBlr using balanced_node_inv.
intros HBlrl HBlrr Hlrcabs.
inversion HClr using correct_node_inv.
intros HClrl HClrr Hdeplr.
set (valid_depth_case (valid HClr HBlr)) as Hd; clearbody Hd; repeat destruct Hd as [Hd | Hd].
- destruct_first Hd Hlrc.
destruct Hd as [Hd1 Hd2].
subst.
unfold rrot_balance, rrotate_help; simpl.
unfold rrotate_help; simpl.
repeat rewrite depth_node in Hdept; rewrite_maxlr.
rewrite Hlrc in *.
simpl.
unfold rrotate_help; simpl.
try_decide_depth_node.
repeat constructor; eauto; try solve [omega | simpl; omega].
try_decide_depth_node; omega.
- destruct_first Hd Hlrc.
destruct Hd as [Hd1 Hd2].
subst.
unfold rrot_balance; simpl.
repeat unfold rrotate_help; simpl.
repeat rewrite depth_node in Hdept; rewrite_maxlr.
rewrite Hlrc in *.
simpl.
unfold rrotate_help; simpl.
try_decide_depth_node.
repeat constructor; eauto; try solve [omega | simpl; omega].
try_decide_depth_node; omega.
- destruct_first Hd Hlrc.
destruct Hd as [Hd1 Hd2].
subst.
unfold rrot_balance; simpl.
repeat unfold rrotate_help; simpl.
repeat rewrite depth_node in Hdept; rewrite_maxlr.
rewrite Hlrc in *.
simpl.
unfold rrotate_help; simpl.
try_decide_depth_node.
repeat constructor; eauto; try solve [omega | simpl; omega].
try_decide_depth_node; omega.
Qed.
Lemma rrotate_balance_depth :
forall v l r, Valid l -> Valid r ->
(balance l <> 0)%Z ->
Correct (node v 2 l r) ->
Valid (rrot_balance v 2 l r) /\
depth (rrot_balance v 2 l r) = (depth (node v 2 l r) - 1)%Z.
Proof.
intros.
cut (balance l = -1 \/ balance l = 1)%Z.
- intro Hp.
destruct Hp; eauto using rrotate_balance_depth_single, rrotate_balance_depth_double.
- destruct l as [| lv lc ll lr]; inversion H; simpl in *; try omega; subst.
apply valid_depth_case in H.
intuition.
Qed.
Definition lrot_balance v c l r :=
if (Z_gt_le_dec (balance r) 0)%Z then lrotate (node v c l (rrotate r)) else lrotate (node v c l r).
Lemma lrot_balance_reltree R `{Tr : Transitive A.t R} : forall v c l r, RelTree R (node v c l r) -> RelTree R (lrot_balance v c l r).
Proof.
intros.
unfold lrot_balance.
destruct Z_gt_le_dec.
- destruct r as[| rv rc rl rr].
+ simpl in g.
discriminate.
+ simpl in g.
apply lrotate_reltree; auto.
inversion_clear H as [|?v ?c ?l ?r Hsl Hsr Hlt Hgt]; subst.
inversion Hsr as [|?v ?c ?l ?r Hsl' Hsr' Hlt' Hgt']; subst.
constructor; eauto using rrotate_reltree.
intro x.
rewrite rrotate_in.
auto.
- apply lrotate_reltree; auto.
Qed.
Lemma lrotate_balance_flip_eq : forall v l r,
lrot_balance v (-2) l r = flip_rec (rrot_balance v 2 (flip_rec r) (flip_rec l)).
Proof.
intros.
destruct r as [| rv rc rl rr]; simpl; auto.
- unfold lrot_balance; simpl.
rewrite flip_rec_involutive.
reflexivity.
- unfold lrot_balance, rrot_balance.
simpl.
destruct Z_gt_le_dec, Z_lt_ge_dec; try omega.
+ rewrite lrotate_flip_eq.
simpl.
f_equal.
rewrite lrotate_flip_eq.
simpl.
repeat f_equal; eauto using flip_rec_involutive.
omega.
+ unfold lrotate_help.
simpl.
unfold rrotate_help.
destruct rrotate_help_comp as [cx cy].
simpl.
repeat rewrite flip_rec_involutive.
reflexivity.
Qed.
Lemma rrotate_balance_flip_eq : forall v l r,
rrot_balance v 2 l r = flip_rec (lrot_balance v (-2) (flip_rec r) (flip_rec l)).
Proof.
intros.
rewrite lrotate_balance_flip_eq.
repeat rewrite flip_rec_involutive.
reflexivity.
Qed.
Lemma lrotate_balance_depth :
forall v l r, Valid l -> Valid r -> Correct (node v (-2) l r) ->
(balance r <> 0)%Z ->
Valid (lrot_balance v (-2) l r) /\
depth (lrot_balance v (-2) l r) = (depth (node v (-2) l r) - 1)%Z.
Proof.
intros.
rewrite lrotate_balance_flip_eq.
rewrite flip_rec_valid.
rewrite flip_rec_depth.
replace (depth (node v (-2)%Z l r)) with (depth (node v 2 (flip_rec r) (flip_rec l))).
- apply rrotate_balance_depth.
+ rewrite flip_rec_valid; assumption.
+ rewrite flip_rec_valid; assumption.
+ destruct r; simpl in *; omega.
+ rewrite <- flip_rec_correct in H1.
assumption.
- repeat rewrite depth_node.
repeat rewrite flip_rec_depth.
rewrite Z.max_comm.
reflexivity.
Qed.
Definition balance_node v c l r :=
if (Z.abs c <=? 1)%Z
then (node v c l r, negb (c =? 0)%Z)
else if (0 <=? c)%Z
then (rrot_balance v 2 l r, false)
else (lrot_balance v (-2) l r, false).
Functional Scheme balance_node_ind := Induction for balance_node Sort Prop.
(*
match c with
| Z0 => (node v c l r, false)
| 1%Z => (node v c l r, true)
| 2%Z => (rrot_balance v 2 l r, false)
| (-1)%Z => (node v c l r, true)
| (-2)%Z => (lrot_balance v (-2) l r, false)
| _ => (node v c l r, true)
end.
*)
Lemma balance_node_flip_eq : forall v c l r,
balance_node v c l r = let (t, b) := balance_node v (-c) (flip_rec r) (flip_rec l) in (flip_rec t, b).
Proof.
assert (forall (x : Z), (x =? 0) = (-x =? 0))%Z as neg_zero. {
intros.
case_eq (x =? 0)%Z.
- rewrite Z.eqb_eq.
intro; subst.
reflexivity.
- rewrite Z.eqb_neq.
intros.
apply eq_sym.
rewrite Z.eqb_neq.
omega.
}
intros.
unfold balance_node.
rewrite Z.abs_opp.
case_eq (Z.abs c <=? 1)%Z.
- rewrite neg_zero.
simpl.
repeat rewrite flip_rec_involutive.
rewrite Z.opp_involutive.
reflexivity.
- case_eq (0 <=? c)%Z; case_eq (0 <=? -c)%Z;
repeat rewrite Z.leb_le; repeat rewrite Z.leb_gt; intros.
+ exfalso.
cut (c = 0)%Z; try omega.
intro; subst.
simpl in *.
omega.
+ rewrite rrotate_balance_flip_eq.
reflexivity.
+ rewrite lrotate_balance_flip_eq.
reflexivity.
+ omega.
Qed.
Lemma balance_node_in : forall v c l r x, TIn x (fst (balance_node v c l r)) <->
TIn x (node v c l r).
Proof.
intros.
functional induction (balance_node v c l r); simpl; try solve [intuition].
- unfold rrot_balance.
destruct Z_lt_ge_dec.
+ rewrite rrotate_in.
repeat rewrite <- tin_child_iff.
rewrite lrotate_in.
reflexivity.
+ rewrite rrotate_in.
repeat rewrite <- tin_child_iff.
reflexivity.
- unfold lrot_balance.
destruct Z_gt_le_dec.
+ rewrite lrotate_in.
repeat rewrite <- tin_child_iff.
rewrite rrotate_in.
reflexivity.
+ rewrite lrotate_in.
repeat rewrite <- tin_child_iff.
reflexivity.
Qed.
Lemma balance_node_reltree R `{Tr : Transitive A.t R} : forall v c l r,
RelTree R (node v c l r) -> RelTree R (fst (balance_node v c l r)).
Proof.
intros.
functional induction (balance_node v c l r); try solve [intuition].
- apply rrot_balance_reltree; auto.
inversion H; constructor; auto.
- apply lrot_balance_reltree; auto.
inversion H; constructor; auto.
Qed.
Fixpoint insert' (cmp : A.t -> A.t -> comparison) (t : Tree) (x : A.t) : (Tree * bool) :=
match t with
| leaf => (node x 0%Z leaf leaf, true)
| node v c l r =>
match cmp x v with
| Eq => (t, false)
| Lt => let (l', grow) := insert' cmp l x in
if grow then balance_node v (c + 1)%Z l' r else (node v c l' r, false)
| Gt => let (r', grow) := insert' cmp r x in
if grow then balance_node v (c - 1)%Z l r' else (node v c l r', false)
end
end.
Functional Scheme insert'_ind := Induction for insert' Sort Prop.
Lemma flip_rec_insert' : forall t x,
let (t', grow) := insert' (fun x y => (CompOpp (A.compare x y))) (flip_rec t) x in
(flip_rec t', grow) = insert' A.compare t x.
Proof.
intros t x.
induction t as [| v c l IHl r IHr].
- reflexivity.
- simpl.
destruct (insert' _ (flip_rec l) x) as [l' lg].
destruct (insert' _ (flip_rec r) x) as [r' rg].
rewrite <- IHl, <- IHr.
case A.compare; simpl.
+ repeat rewrite flip_rec_involutive.
repeat rewrite Z.opp_involutive.
reflexivity.
+ destruct lg.
* rewrite balance_node_flip_eq.
replace (- (-c -1))%Z with (c + 1)%Z; try omega.
rewrite flip_rec_involutive.
destruct balance_node.
rewrite flip_rec_involutive.
reflexivity.
* simpl.
repeat rewrite flip_rec_involutive.
rewrite Z.opp_involutive.
reflexivity.
+ destruct rg.
* rewrite balance_node_flip_eq.
replace (- (- c + 1))%Z with (c - 1)%Z; try omega.
rewrite flip_rec_involutive.
destruct balance_node.
rewrite flip_rec_involutive.
reflexivity.
* simpl.
repeat rewrite flip_rec_involutive.
rewrite Z.opp_involutive.
reflexivity.
Qed.
Definition insert t x := fst (insert' A.compare t x).
Lemma insert_in : forall cmp t x y,
(forall x y, cmp x y = Eq <-> x = y) ->
TIn y (fst(insert' cmp t x)) <-> x = y \/ TIn y t.
Proof.
intros cmp t x y Hspec.
functional induction (insert' cmp t x); simpl.
- rewrite <- tin_child_iff.
intuition.
- rewrite Hspec in e0; subst.
rewrite <- tin_child_iff.
intuition.
- rewrite balance_node_in.
repeat rewrite <- tin_child_iff in *.
rewrite e1 in *.
intuition.
- repeat rewrite <- tin_child_iff in *.
rewrite e1 in *.
intuition.
- rewrite balance_node_in.
repeat rewrite <- tin_child_iff in *.
rewrite e1 in *.
intuition.
- repeat rewrite <- tin_child_iff in *.
rewrite e1 in *.
intuition.
Qed.
Lemma insert_reltree : forall t x,
STree t -> STree (fst (insert' A.compare t x)).
Proof.
destruct A.lt_strorder.
intros t x.
functional induction (insert' A.compare t x); intro H; try solve [intuition].
- repeat constructor.
+ rewrite TForall_iff; constructor.
+ rewrite TForall_iff; constructor.
- rewrite F.compare_lt_iff in e0.
apply balance_node_reltree; auto.
rewrite e1 in IHp.
simpl in IHp.
inversion_clear H.
constructor; intuition.
intros y Hin.
fold (fst (l', true)) in Hin.
rewrite <- e1 in Hin.
rewrite insert_in in Hin; eauto using F.compare_eq_iff.
destruct Hin; subst; auto.
- rewrite F.compare_lt_iff in e0.
inversion_clear H.
constructor; intuition.
+ rewrite e1 in H.
assumption.
+ intros y Hin.
fold (fst (l', false)) in Hin.
rewrite <- e1 in Hin.
rewrite insert_in in Hin; eauto using F.compare_eq_iff.
destruct Hin; subst; auto.
- rewrite F.compare_gt_iff in e0.
apply balance_node_reltree; auto.
rewrite e1 in IHp.
simpl in IHp.
inversion_clear H.
constructor; intuition.
intros y Hin.
fold (fst (r', true)) in Hin.
rewrite <- e1 in Hin.