| author | nipkow |
| Fri, 27 Jan 2017 17:28:10 +0100 | |
| changeset 64952 | f11e974b47e0 |
| parent 64951 | 140addd19343 |
| child 64953 | f9cfb10761ff |
| permissions | -rw-r--r-- |
|
64951
140addd19343
removed contribution by Daniel Stuewe, too detailed.
nipkow
parents:
64950
diff
changeset
|
1 |
(* Author: Tobias Nipkow *) |
| 61224 | 2 |
|
3 |
section \<open>Red-Black Tree Implementation of Sets\<close> |
|
4 |
||
5 |
theory RBT_Set |
|
6 |
imports |
|
| 64950 | 7 |
Complex_Main |
| 61224 | 8 |
RBT |
| 61581 | 9 |
Cmp |
| 61224 | 10 |
Isin2 |
11 |
begin |
|
12 |
||
|
63411
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
13 |
fun ins :: "'a::linorder \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt" where |
| 61749 | 14 |
"ins x Leaf = R Leaf x Leaf" | |
15 |
"ins x (B l a r) = |
|
| 61678 | 16 |
(case cmp x a of |
| 61749 | 17 |
LT \<Rightarrow> bal (ins x l) a r | |
18 |
GT \<Rightarrow> bal l a (ins x r) | |
|
| 61678 | 19 |
EQ \<Rightarrow> B l a r)" | |
| 61749 | 20 |
"ins x (R l a r) = |
| 61678 | 21 |
(case cmp x a of |
| 61749 | 22 |
LT \<Rightarrow> R (ins x l) a r | |
23 |
GT \<Rightarrow> R l a (ins x r) | |
|
| 61678 | 24 |
EQ \<Rightarrow> R l a r)" |
| 61224 | 25 |
|
|
63411
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
26 |
definition insert :: "'a::linorder \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt" where |
| 61749 | 27 |
"insert x t = paint Black (ins x t)" |
28 |
||
|
63411
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
29 |
fun del :: "'a::linorder \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt" |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
30 |
and delL :: "'a::linorder \<Rightarrow> 'a rbt \<Rightarrow> 'a \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt" |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
31 |
and delR :: "'a::linorder \<Rightarrow> 'a rbt \<Rightarrow> 'a \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt" |
| 61224 | 32 |
where |
| 61749 | 33 |
"del x Leaf = Leaf" | |
34 |
"del x (Node _ l a r) = |
|
| 61678 | 35 |
(case cmp x a of |
| 61749 | 36 |
LT \<Rightarrow> delL x l a r | |
37 |
GT \<Rightarrow> delR x l a r | |
|
| 61678 | 38 |
EQ \<Rightarrow> combine l r)" | |
| 61749 | 39 |
"delL x (B t1 a t2) b t3 = balL (del x (B t1 a t2)) b t3" | |
40 |
"delL x l a r = R (del x l) a r" | |
|
41 |
"delR x t1 a (B t2 b t3) = balR t1 a (del x (B t2 b t3))" | |
|
42 |
"delR x l a r = R l a (del x r)" |
|
43 |
||
|
63411
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
44 |
definition delete :: "'a::linorder \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt" where |
| 61749 | 45 |
"delete x t = paint Black (del x t)" |
| 61224 | 46 |
|
47 |
||
48 |
subsection "Functional Correctness Proofs" |
|
49 |
||
| 61749 | 50 |
lemma inorder_paint: "inorder(paint c t) = inorder t" |
| 62526 | 51 |
by(cases t) (auto) |
| 61749 | 52 |
|
| 61224 | 53 |
lemma inorder_bal: |
54 |
"inorder(bal l a r) = inorder l @ a # inorder r" |
|
| 62526 | 55 |
by(cases "(l,a,r)" rule: bal.cases) (auto) |
| 61224 | 56 |
|
| 61749 | 57 |
lemma inorder_ins: |
58 |
"sorted(inorder t) \<Longrightarrow> inorder(ins x t) = ins_list x (inorder t)" |
|
59 |
by(induction x t rule: ins.induct) (auto simp: ins_list_simps inorder_bal) |
|
60 |
||
| 61224 | 61 |
lemma inorder_insert: |
| 61749 | 62 |
"sorted(inorder t) \<Longrightarrow> inorder(insert x t) = ins_list x (inorder t)" |
63 |
by (simp add: insert_def inorder_ins inorder_paint) |
|
| 61224 | 64 |
|
65 |
lemma inorder_balL: |
|
66 |
"inorder(balL l a r) = inorder l @ a # inorder r" |
|
| 62526 | 67 |
by(cases "(l,a,r)" rule: balL.cases)(auto simp: inorder_bal inorder_paint) |
| 61224 | 68 |
|
69 |
lemma inorder_balR: |
|
70 |
"inorder(balR l a r) = inorder l @ a # inorder r" |
|
| 62526 | 71 |
by(cases "(l,a,r)" rule: balR.cases) (auto simp: inorder_bal inorder_paint) |
| 61224 | 72 |
|
73 |
lemma inorder_combine: |
|
74 |
"inorder(combine l r) = inorder l @ inorder r" |
|
75 |
by(induction l r rule: combine.induct) |
|
| 61231 | 76 |
(auto simp: inorder_balL inorder_balR split: tree.split color.split) |
| 61224 | 77 |
|
| 61749 | 78 |
lemma inorder_del: |
79 |
"sorted(inorder t) \<Longrightarrow> inorder(del x t) = del_list x (inorder t)" |
|
80 |
"sorted(inorder l) \<Longrightarrow> inorder(delL x l a r) = |
|
| 61678 | 81 |
del_list x (inorder l) @ a # inorder r" |
| 61749 | 82 |
"sorted(inorder r) \<Longrightarrow> inorder(delR x l a r) = |
| 61224 | 83 |
inorder l @ a # del_list x (inorder r)" |
| 61749 | 84 |
by(induction x t and x l a r and x l a r rule: del_delL_delR.induct) |
| 61231 | 85 |
(auto simp: del_list_simps inorder_combine inorder_balL inorder_balR) |
| 61224 | 86 |
|
| 61749 | 87 |
lemma inorder_delete: |
88 |
"sorted(inorder t) \<Longrightarrow> inorder(delete x t) = del_list x (inorder t)" |
|
89 |
by (auto simp: delete_def inorder_del inorder_paint) |
|
90 |
||
| 61581 | 91 |
|
|
63411
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
92 |
subsection \<open>Structural invariants\<close> |
| 61224 | 93 |
|
| 64952 | 94 |
text\<open>The proofs are due to Markus Reiter and Alexander Krauss.\<close> |
| 61754 | 95 |
|
96 |
fun color :: "'a rbt \<Rightarrow> color" where |
|
97 |
"color Leaf = Black" | |
|
98 |
"color (Node c _ _ _) = c" |
|
99 |
||
100 |
fun bheight :: "'a rbt \<Rightarrow> nat" where |
|
101 |
"bheight Leaf = 0" | |
|
|
64951
140addd19343
removed contribution by Daniel Stuewe, too detailed.
nipkow
parents:
64950
diff
changeset
|
102 |
"bheight (Node c l x r) = (if c = Black then bheight l + 1 else bheight l)" |
| 61754 | 103 |
|
|
63411
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
104 |
fun invc :: "'a rbt \<Rightarrow> bool" where |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
105 |
"invc Leaf = True" | |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
106 |
"invc (Node c l a r) = |
| 64947 | 107 |
(invc l \<and> invc r \<and> (c = Red \<longrightarrow> color l = Black \<and> color r = Black))" |
| 61754 | 108 |
|
|
63411
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
109 |
fun invc_sons :: "'a rbt \<Rightarrow> bool" \<comment> \<open>Weaker version\<close> where |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
110 |
"invc_sons Leaf = True" | |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
111 |
"invc_sons (Node c l a r) = (invc l \<and> invc r)" |
| 61754 | 112 |
|
|
63411
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
113 |
fun invh :: "'a rbt \<Rightarrow> bool" where |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
114 |
"invh Leaf = True" | |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
115 |
"invh (Node c l x r) = (invh l \<and> invh r \<and> bheight l = bheight r)" |
| 61754 | 116 |
|
|
63411
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
117 |
lemma invc_sonsI: "invc t \<Longrightarrow> invc_sons t" |
| 61754 | 118 |
by (cases t) simp+ |
119 |
||
120 |
definition rbt :: "'a rbt \<Rightarrow> bool" where |
|
|
63411
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
121 |
"rbt t = (invc t \<and> invh t \<and> color t = Black)" |
| 61754 | 122 |
|
123 |
lemma color_paint_Black: "color (paint Black t) = Black" |
|
124 |
by (cases t) auto |
|
125 |
||
126 |
theorem rbt_Leaf: "rbt Leaf" |
|
127 |
by (simp add: rbt_def) |
|
128 |
||
|
63411
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
129 |
lemma paint_invc_sons: "invc_sons t \<Longrightarrow> invc_sons (paint c t)" |
| 61754 | 130 |
by (cases t) auto |
131 |
||
|
63411
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
132 |
lemma invc_paint_Black: "invc_sons t \<Longrightarrow> invc (paint Black t)" |
| 61754 | 133 |
by (cases t) auto |
134 |
||
|
63411
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
135 |
lemma invh_paint: "invh t \<Longrightarrow> invh (paint c t)" |
| 61754 | 136 |
by (cases t) auto |
137 |
||
| 64952 | 138 |
lemma invc_bal: |
139 |
"\<lbrakk>invc l \<and> invc_sons r \<or> invc_sons l \<and> invc r\<rbrakk> \<Longrightarrow> invc (bal l a r)" |
|
| 61754 | 140 |
by (induct l a r rule: bal.induct) auto |
141 |
||
142 |
lemma bheight_bal: |
|
143 |
"bheight l = bheight r \<Longrightarrow> bheight (bal l a r) = Suc (bheight l)" |
|
144 |
by (induct l a r rule: bal.induct) auto |
|
145 |
||
|
63411
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
146 |
lemma invh_bal: |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
147 |
"\<lbrakk> invh l; invh r; bheight l = bheight r \<rbrakk> \<Longrightarrow> invh (bal l a r)" |
| 61754 | 148 |
by (induct l a r rule: bal.induct) auto |
149 |
||
150 |
||
151 |
subsubsection \<open>Insertion\<close> |
|
152 |
||
|
63411
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
153 |
lemma invc_ins: assumes "invc t" |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
154 |
shows "color t = Black \<Longrightarrow> invc (ins x t)" "invc_sons (ins x t)" |
| 61754 | 155 |
using assms |
|
63411
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
156 |
by (induct x t rule: ins.induct) (auto simp: invc_bal invc_sonsI) |
| 61754 | 157 |
|
|
63411
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
158 |
lemma invh_ins: assumes "invh t" |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
159 |
shows "invh (ins x t)" "bheight (ins x t) = bheight t" |
| 61754 | 160 |
using assms |
|
63411
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
161 |
by (induct x t rule: ins.induct) (auto simp: invh_bal bheight_bal) |
| 61754 | 162 |
|
|
63411
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
163 |
theorem rbt_insert: "rbt t \<Longrightarrow> rbt (insert x t)" |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
164 |
by (simp add: invc_ins invh_ins color_paint_Black invc_paint_Black invh_paint |
| 61754 | 165 |
rbt_def insert_def) |
166 |
||
|
63411
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
167 |
|
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
168 |
subsubsection \<open>Deletion\<close> |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
169 |
|
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
170 |
lemma bheight_paint_Red: |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
171 |
"color t = Black \<Longrightarrow> bheight (paint Red t) = bheight t - 1" |
| 61754 | 172 |
by (cases t) auto |
173 |
||
|
63411
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
174 |
lemma balL_invh_with_invc: |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
175 |
assumes "invh lt" "invh rt" "bheight lt + 1 = bheight rt" "invc rt" |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
176 |
shows "bheight (balL lt a rt) = bheight lt + 1" "invh (balL lt a rt)" |
| 61754 | 177 |
using assms |
|
63411
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
178 |
by (induct lt a rt rule: balL.induct) |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
179 |
(auto simp: invh_bal invh_paint bheight_bal bheight_paint_Red) |
| 61754 | 180 |
|
|
63411
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
181 |
lemma balL_invh_app: |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
182 |
assumes "invh lt" "invh rt" "bheight lt + 1 = bheight rt" "color rt = Black" |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
183 |
shows "invh (balL lt a rt)" |
| 61754 | 184 |
"bheight (balL lt a rt) = bheight rt" |
185 |
using assms |
|
|
63411
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
186 |
by (induct lt a rt rule: balL.induct) (auto simp add: invh_bal bheight_bal) |
| 61754 | 187 |
|
|
63411
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
188 |
lemma balL_invc: "\<lbrakk>invc_sons l; invc r; color r = Black\<rbrakk> \<Longrightarrow> invc (balL l a r)" |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
189 |
by (induct l a r rule: balL.induct) (simp_all add: invc_bal) |
| 61754 | 190 |
|
|
63411
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
191 |
lemma balL_invc_sons: "\<lbrakk> invc_sons lt; invc rt \<rbrakk> \<Longrightarrow> invc_sons (balL lt a rt)" |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
192 |
by (induct lt a rt rule: balL.induct) (auto simp: invc_bal paint_invc_sons invc_sonsI) |
| 61754 | 193 |
|
|
63411
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
194 |
lemma balR_invh_with_invc: |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
195 |
assumes "invh lt" "invh rt" "bheight lt = bheight rt + 1" "invc lt" |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
196 |
shows "invh (balR lt a rt) \<and> bheight (balR lt a rt) = bheight lt" |
| 61754 | 197 |
using assms |
|
63411
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
198 |
by(induct lt a rt rule: balR.induct) |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
199 |
(auto simp: invh_bal bheight_bal invh_paint bheight_paint_Red) |
| 61754 | 200 |
|
|
63411
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
201 |
lemma invc_balR: "\<lbrakk>invc a; invc_sons b; color a = Black\<rbrakk> \<Longrightarrow> invc (balR a x b)" |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
202 |
by (induct a x b rule: balR.induct) (simp_all add: invc_bal) |
| 61754 | 203 |
|
|
63411
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
204 |
lemma invc_sons_balR: "\<lbrakk> invc lt; invc_sons rt \<rbrakk> \<Longrightarrow>invc_sons (balR lt x rt)" |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
205 |
by (induct lt x rt rule: balR.induct) (auto simp: invc_bal paint_invc_sons invc_sonsI) |
| 61754 | 206 |
|
|
63411
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
207 |
lemma invh_combine: |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
208 |
assumes "invh lt" "invh rt" "bheight lt = bheight rt" |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
209 |
shows "bheight (combine lt rt) = bheight lt" "invh (combine lt rt)" |
| 61754 | 210 |
using assms |
211 |
by (induct lt rt rule: combine.induct) |
|
|
63411
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
212 |
(auto simp: balL_invh_app split: tree.splits color.splits) |
| 61754 | 213 |
|
|
63411
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
214 |
lemma invc_combine: |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
215 |
assumes "invc lt" "invc rt" |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
216 |
shows "color lt = Black \<Longrightarrow> color rt = Black \<Longrightarrow> invc (combine lt rt)" |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
217 |
"invc_sons (combine lt rt)" |
| 61754 | 218 |
using assms |
219 |
by (induct lt rt rule: combine.induct) |
|
|
63411
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
220 |
(auto simp: balL_invc invc_sonsI split: tree.splits color.splits) |
| 61754 | 221 |
|
222 |
||
|
63411
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
223 |
lemma assumes "invh lt" "invc lt" |
| 61754 | 224 |
shows |
|
63411
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
225 |
del_invc_invh: "invh (del x lt) \<and> (color lt = Red \<and> bheight (del x lt) = bheight lt \<and> invc (del x lt) |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
226 |
\<or> color lt = Black \<and> bheight (del x lt) = bheight lt - 1 \<and> invc_sons (del x lt))" |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
227 |
and "\<lbrakk>invh rt; bheight lt = bheight rt; invc rt\<rbrakk> \<Longrightarrow> |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
228 |
invh (delL x lt k rt) \<and> |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
229 |
bheight (delL x lt k rt) = bheight lt \<and> |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
230 |
(color lt = Black \<and> color rt = Black \<and> invc (delL x lt k rt) \<or> |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
231 |
(color lt \<noteq> Black \<or> color rt \<noteq> Black) \<and> invc_sons (delL x lt k rt))" |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
232 |
and "\<lbrakk>invh rt; bheight lt = bheight rt; invc rt\<rbrakk> \<Longrightarrow> |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
233 |
invh (delR x lt k rt) \<and> |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
234 |
bheight (delR x lt k rt) = bheight lt \<and> |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
235 |
(color lt = Black \<and> color rt = Black \<and> invc (delR x lt k rt) \<or> |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
236 |
(color lt \<noteq> Black \<or> color rt \<noteq> Black) \<and> invc_sons (delR x lt k rt))" |
| 61754 | 237 |
using assms |
|
63411
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
238 |
proof (induct x lt and x lt k rt and x lt k rt rule: del_delL_delR.induct) |
| 61754 | 239 |
case (2 y c _ y') |
240 |
have "y = y' \<or> y < y' \<or> y > y'" by auto |
|
241 |
thus ?case proof (elim disjE) |
|
242 |
assume "y = y'" |
|
|
63411
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
243 |
with 2 show ?thesis |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
244 |
by (cases c) (simp_all add: invh_combine invc_combine) |
| 61754 | 245 |
next |
246 |
assume "y < y'" |
|
|
63411
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
247 |
with 2 show ?thesis by (cases c) (auto simp: invc_sonsI) |
| 61754 | 248 |
next |
249 |
assume "y' < y" |
|
|
63411
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
250 |
with 2 show ?thesis by (cases c) (auto simp: invc_sonsI) |
| 61754 | 251 |
qed |
252 |
next |
|
|
63411
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
253 |
case (3 y lt z rta y' bb) |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
254 |
thus ?case by (cases "color (Node Black lt z rta) = Black \<and> color bb = Black") (simp add: balL_invh_with_invc balL_invc balL_invc_sons)+ |
| 61754 | 255 |
next |
|
63411
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
256 |
case (5 y a y' lt z rta) |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
257 |
thus ?case by (cases "color a = Black \<and> color (Node Black lt z rta) = Black") (simp add: balR_invh_with_invc invc_balR invc_sons_balR)+ |
| 61754 | 258 |
next |
|
63411
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
259 |
case ("6_1" y a y') thus ?case by (cases "color a = Black \<and> color Leaf = Black") simp+
|
| 61754 | 260 |
qed auto |
261 |
||
|
63411
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
262 |
theorem rbt_delete: "rbt t \<Longrightarrow> rbt (delete k t)" |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
263 |
by (metis delete_def rbt_def color_paint_Black del_invc_invh invc_paint_Black invc_sonsI invh_paint) |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
264 |
|
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
265 |
text \<open>Overall correctness:\<close> |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
266 |
|
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
267 |
interpretation Set_by_Ordered |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
268 |
where empty = Leaf and isin = isin and insert = insert and delete = delete |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
269 |
and inorder = inorder and inv = rbt |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
270 |
proof (standard, goal_cases) |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
271 |
case 1 show ?case by simp |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
272 |
next |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
273 |
case 2 thus ?case by(simp add: isin_set) |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
274 |
next |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
275 |
case 3 thus ?case by(simp add: inorder_insert) |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
276 |
next |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
277 |
case 4 thus ?case by(simp add: inorder_delete) |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
278 |
next |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
279 |
case 5 thus ?case by (simp add: rbt_Leaf) |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
280 |
next |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
281 |
case 6 thus ?case by (simp add: rbt_insert) |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
282 |
next |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
283 |
case 7 thus ?case by (simp add: rbt_delete) |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
284 |
qed |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
285 |
|
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
286 |
|
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
287 |
subsection \<open>Height-Size Relation\<close> |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
288 |
|
| 64950 | 289 |
lemma neq_Black[simp]: "(c \<noteq> Black) = (c = Red)" |
290 |
by (cases c) auto |
|
291 |
||
292 |
lemma rbt_height_bheight_if_nat: "invc t \<Longrightarrow> invh t \<Longrightarrow> |
|
293 |
height t \<le> (if color t = Black then 2 * bheight t else 2 * bheight t + 1)" |
|
294 |
by(induction t) (auto split: if_split_asm) |
|
295 |
||
296 |
lemma rbt_height_bheight_if: "invc t \<Longrightarrow> invh t \<Longrightarrow> |
|
297 |
(if color t = Black then height t / 2 else (height t - 1) / 2) \<le> bheight t" |
|
298 |
by(induction t) (auto split: if_split_asm) |
|
299 |
||
300 |
lemma rbt_height_bheight: "rbt t \<Longrightarrow> height t / 2 \<le> bheight t " |
|
301 |
by(auto simp: rbt_def dest: rbt_height_bheight_if) |
|
302 |
||
303 |
lemma bheight_size_bound: "invc t \<Longrightarrow> invh t \<Longrightarrow> size1 t \<ge> 2 ^ (bheight t)" |
|
304 |
by (induction t) auto |
|
305 |
||
306 |
lemma rbt_height_le: assumes "rbt t" shows "height t \<le> 2 * log 2 (size1 t)" |
|
307 |
proof - |
|
308 |
have "2 powr (height t / 2) \<le> 2 powr bheight t" |
|
309 |
using rbt_height_bheight[OF assms] by (simp) |
|
310 |
also have "\<dots> \<le> size1 t" using assms |
|
311 |
by (simp add: powr_realpow bheight_size_bound rbt_def) |
|
312 |
finally have "2 powr (height t / 2) \<le> size1 t" . |
|
313 |
hence "height t / 2 \<le> log 2 (size1 t)" |
|
314 |
by(simp add: le_log_iff size1_def del: Int.divide_le_eq_numeral1(1)) |
|
315 |
thus ?thesis by simp |
|
316 |
qed |
|
317 |
||
| 61224 | 318 |
end |