author | nipkow |
Wed, 31 Jul 2024 10:36:28 +0200 | |
changeset 80628 | 161286c9d426 |
parent 78199 | d6e6618db929 |
permissions | -rw-r--r-- |
61640 | 1 |
(* Author: Tobias Nipkow *) |
2 |
||
62130 | 3 |
section \<open>2-3 Tree Implementation of Sets\<close> |
61640 | 4 |
|
5 |
theory Tree23_Set |
|
6 |
imports |
|
7 |
Tree23 |
|
8 |
Cmp |
|
67965 | 9 |
Set_Specs |
61640 | 10 |
begin |
11 |
||
68109 | 12 |
declare sorted_wrt.simps(2)[simp del] |
13 |
||
68431 | 14 |
definition empty :: "'a tree23" where |
15 |
"empty = Leaf" |
|
16 |
||
63411
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62130
diff
changeset
|
17 |
fun isin :: "'a::linorder tree23 \<Rightarrow> 'a \<Rightarrow> bool" where |
61640 | 18 |
"isin Leaf x = False" | |
19 |
"isin (Node2 l a r) x = |
|
61678 | 20 |
(case cmp x a of |
21 |
LT \<Rightarrow> isin l x | |
|
22 |
EQ \<Rightarrow> True | |
|
23 |
GT \<Rightarrow> isin r x)" | |
|
61640 | 24 |
"isin (Node3 l a m b r) x = |
61678 | 25 |
(case cmp x a of |
26 |
LT \<Rightarrow> isin l x | |
|
27 |
EQ \<Rightarrow> True | |
|
28 |
GT \<Rightarrow> |
|
29 |
(case cmp x b of |
|
30 |
LT \<Rightarrow> isin m x | |
|
31 |
EQ \<Rightarrow> True | |
|
32 |
GT \<Rightarrow> isin r x))" |
|
61640 | 33 |
|
80628 | 34 |
datatype 'a up\<^sub>i = Eq\<^sub>i "'a tree23" | Of "'a tree23" 'a "'a tree23" |
61640 | 35 |
|
80628 | 36 |
fun tree\<^sub>i :: "'a up\<^sub>i \<Rightarrow> 'a tree23" where |
37 |
"tree\<^sub>i (Eq\<^sub>i t) = t" | |
|
38 |
"tree\<^sub>i (Of l a r) = Node2 l a r" |
|
61640 | 39 |
|
80628 | 40 |
fun ins :: "'a::linorder \<Rightarrow> 'a tree23 \<Rightarrow> 'a up\<^sub>i" where |
41 |
"ins x Leaf = Of Leaf x Leaf" | |
|
61640 | 42 |
"ins x (Node2 l a r) = |
43 |
(case cmp x a of |
|
61678 | 44 |
LT \<Rightarrow> |
45 |
(case ins x l of |
|
80628 | 46 |
Eq\<^sub>i l' => Eq\<^sub>i (Node2 l' a r) | |
47 |
Of l1 b l2 => Eq\<^sub>i (Node3 l1 b l2 a r)) | |
|
48 |
EQ \<Rightarrow> Eq\<^sub>i (Node2 l a r) | |
|
61678 | 49 |
GT \<Rightarrow> |
50 |
(case ins x r of |
|
80628 | 51 |
Eq\<^sub>i r' => Eq\<^sub>i (Node2 l a r') | |
52 |
Of r1 b r2 => Eq\<^sub>i (Node3 l a r1 b r2)))" | |
|
61640 | 53 |
"ins x (Node3 l a m b r) = |
54 |
(case cmp x a of |
|
61678 | 55 |
LT \<Rightarrow> |
56 |
(case ins x l of |
|
80628 | 57 |
Eq\<^sub>i l' => Eq\<^sub>i (Node3 l' a m b r) | |
58 |
Of l1 c l2 => Of (Node2 l1 c l2) a (Node2 m b r)) | |
|
59 |
EQ \<Rightarrow> Eq\<^sub>i (Node3 l a m b r) | |
|
61678 | 60 |
GT \<Rightarrow> |
61 |
(case cmp x b of |
|
62 |
GT \<Rightarrow> |
|
63 |
(case ins x r of |
|
80628 | 64 |
Eq\<^sub>i r' => Eq\<^sub>i (Node3 l a m b r') | |
65 |
Of r1 c r2 => Of (Node2 l a m) b (Node2 r1 c r2)) | |
|
66 |
EQ \<Rightarrow> Eq\<^sub>i (Node3 l a m b r) | |
|
61678 | 67 |
LT \<Rightarrow> |
68 |
(case ins x m of |
|
80628 | 69 |
Eq\<^sub>i m' => Eq\<^sub>i (Node3 l a m' b r) | |
70 |
Of m1 c m2 => Of (Node2 l a m1) c (Node2 m2 b r))))" |
|
61640 | 71 |
|
72 |
hide_const insert |
|
73 |
||
63411
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62130
diff
changeset
|
74 |
definition insert :: "'a::linorder \<Rightarrow> 'a tree23 \<Rightarrow> 'a tree23" where |
80628 | 75 |
"insert x t = tree\<^sub>i(ins x t)" |
61640 | 76 |
|
80628 | 77 |
datatype 'a up\<^sub>d = Eq\<^sub>d "'a tree23" | Uf "'a tree23" |
61640 | 78 |
|
80628 | 79 |
fun tree\<^sub>d :: "'a up\<^sub>d \<Rightarrow> 'a tree23" where |
80 |
"tree\<^sub>d (Eq\<^sub>d t) = t" | |
|
81 |
"tree\<^sub>d (Uf t) = t" |
|
61640 | 82 |
|
83 |
(* Variation: return None to signal no-change *) |
|
84 |
||
80628 | 85 |
fun node21 :: "'a up\<^sub>d \<Rightarrow> 'a \<Rightarrow> 'a tree23 \<Rightarrow> 'a up\<^sub>d" where |
86 |
"node21 (Eq\<^sub>d t1) a t2 = Eq\<^sub>d(Node2 t1 a t2)" | |
|
87 |
"node21 (Uf t1) a (Node2 t2 b t3) = Uf(Node3 t1 a t2 b t3)" | |
|
88 |
"node21 (Uf t1) a (Node3 t2 b t3 c t4) = Eq\<^sub>d(Node2 (Node2 t1 a t2) b (Node2 t3 c t4))" |
|
61640 | 89 |
|
80628 | 90 |
fun node22 :: "'a tree23 \<Rightarrow> 'a \<Rightarrow> 'a up\<^sub>d \<Rightarrow> 'a up\<^sub>d" where |
91 |
"node22 t1 a (Eq\<^sub>d t2) = Eq\<^sub>d(Node2 t1 a t2)" | |
|
92 |
"node22 (Node2 t1 b t2) a (Uf t3) = Uf(Node3 t1 b t2 a t3)" | |
|
93 |
"node22 (Node3 t1 b t2 c t3) a (Uf t4) = Eq\<^sub>d(Node2 (Node2 t1 b t2) c (Node2 t3 a t4))" |
|
61640 | 94 |
|
80628 | 95 |
fun node31 :: "'a up\<^sub>d \<Rightarrow> 'a \<Rightarrow> 'a tree23 \<Rightarrow> 'a \<Rightarrow> 'a tree23 \<Rightarrow> 'a up\<^sub>d" where |
96 |
"node31 (Eq\<^sub>d t1) a t2 b t3 = Eq\<^sub>d(Node3 t1 a t2 b t3)" | |
|
97 |
"node31 (Uf t1) a (Node2 t2 b t3) c t4 = Eq\<^sub>d(Node2 (Node3 t1 a t2 b t3) c t4)" | |
|
98 |
"node31 (Uf t1) a (Node3 t2 b t3 c t4) d t5 = Eq\<^sub>d(Node3 (Node2 t1 a t2) b (Node2 t3 c t4) d t5)" |
|
61640 | 99 |
|
80628 | 100 |
fun node32 :: "'a tree23 \<Rightarrow> 'a \<Rightarrow> 'a up\<^sub>d \<Rightarrow> 'a \<Rightarrow> 'a tree23 \<Rightarrow> 'a up\<^sub>d" where |
101 |
"node32 t1 a (Eq\<^sub>d t2) b t3 = Eq\<^sub>d(Node3 t1 a t2 b t3)" | |
|
102 |
"node32 t1 a (Uf t2) b (Node2 t3 c t4) = Eq\<^sub>d(Node2 t1 a (Node3 t2 b t3 c t4))" | |
|
103 |
"node32 t1 a (Uf t2) b (Node3 t3 c t4 d t5) = Eq\<^sub>d(Node3 t1 a (Node2 t2 b t3) c (Node2 t4 d t5))" |
|
61640 | 104 |
|
80628 | 105 |
fun node33 :: "'a tree23 \<Rightarrow> 'a \<Rightarrow> 'a tree23 \<Rightarrow> 'a \<Rightarrow> 'a up\<^sub>d \<Rightarrow> 'a up\<^sub>d" where |
106 |
"node33 t1 a t2 b (Eq\<^sub>d t3) = Eq\<^sub>d(Node3 t1 a t2 b t3)" | |
|
107 |
"node33 t1 a (Node2 t2 b t3) c (Uf t4) = Eq\<^sub>d(Node2 t1 a (Node3 t2 b t3 c t4))" | |
|
108 |
"node33 t1 a (Node3 t2 b t3 c t4) d (Uf t5) = Eq\<^sub>d(Node3 t1 a (Node2 t2 b t3) c (Node2 t4 d t5))" |
|
61640 | 109 |
|
80628 | 110 |
fun split_min :: "'a tree23 \<Rightarrow> 'a * 'a up\<^sub>d" where |
111 |
"split_min (Node2 Leaf a Leaf) = (a, Uf Leaf)" | |
|
112 |
"split_min (Node3 Leaf a Leaf b Leaf) = (a, Eq\<^sub>d(Node2 Leaf b Leaf))" | |
|
68020 | 113 |
"split_min (Node2 l a r) = (let (x,l') = split_min l in (x, node21 l' a r))" | |
114 |
"split_min (Node3 l a m b r) = (let (x,l') = split_min l in (x, node31 l' a m b r))" |
|
61640 | 115 |
|
68020 | 116 |
text \<open>In the base cases of \<open>split_min\<close> and \<open>del\<close> it is enough to check if one subtree is a \<open>Leaf\<close>, |
72566
831f17da1aab
renamed "balanced" -> "acomplete" because balanced has other meanings in the literature
nipkow
parents:
70628
diff
changeset
|
117 |
in which case completeness implies that so are the others. Exercise.\<close> |
67038 | 118 |
|
80628 | 119 |
fun del :: "'a::linorder \<Rightarrow> 'a tree23 \<Rightarrow> 'a up\<^sub>d" where |
120 |
"del x Leaf = Eq\<^sub>d Leaf" | |
|
61678 | 121 |
"del x (Node2 Leaf a Leaf) = |
80628 | 122 |
(if x = a then Uf Leaf else Eq\<^sub>d(Node2 Leaf a Leaf))" | |
61678 | 123 |
"del x (Node3 Leaf a Leaf b Leaf) = |
80628 | 124 |
Eq\<^sub>d(if x = a then Node2 Leaf b Leaf else |
61678 | 125 |
if x = b then Node2 Leaf a Leaf |
126 |
else Node3 Leaf a Leaf b Leaf)" | |
|
127 |
"del x (Node2 l a r) = |
|
128 |
(case cmp x a of |
|
129 |
LT \<Rightarrow> node21 (del x l) a r | |
|
130 |
GT \<Rightarrow> node22 l a (del x r) | |
|
70272 | 131 |
EQ \<Rightarrow> let (a',r') = split_min r in node22 l a' r')" | |
61678 | 132 |
"del x (Node3 l a m b r) = |
133 |
(case cmp x a of |
|
134 |
LT \<Rightarrow> node31 (del x l) a m b r | |
|
68020 | 135 |
EQ \<Rightarrow> let (a',m') = split_min m in node32 l a' m' b r | |
61678 | 136 |
GT \<Rightarrow> |
137 |
(case cmp x b of |
|
61640 | 138 |
LT \<Rightarrow> node32 l a (del x m) b r | |
68020 | 139 |
EQ \<Rightarrow> let (b',r') = split_min r in node33 l a m b' r' | |
61640 | 140 |
GT \<Rightarrow> node33 l a m b (del x r)))" |
141 |
||
63411
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62130
diff
changeset
|
142 |
definition delete :: "'a::linorder \<Rightarrow> 'a tree23 \<Rightarrow> 'a tree23" where |
80628 | 143 |
"delete x t = tree\<^sub>d(del x t)" |
61640 | 144 |
|
145 |
||
146 |
subsection "Functional Correctness" |
|
147 |
||
148 |
subsubsection "Proofs for isin" |
|
149 |
||
67929 | 150 |
lemma isin_set: "sorted(inorder t) \<Longrightarrow> isin t x = (x \<in> set (inorder t))" |
70628 | 151 |
by (induction t) (auto simp: isin_simps) |
61640 | 152 |
|
153 |
||
154 |
subsubsection "Proofs for insert" |
|
155 |
||
156 |
lemma inorder_ins: |
|
80628 | 157 |
"sorted(inorder t) \<Longrightarrow> inorder(tree\<^sub>i(ins x t)) = ins_list x (inorder t)" |
158 |
by(induction t) (auto simp: ins_list_simps split: up\<^sub>i.splits) |
|
61640 | 159 |
|
160 |
lemma inorder_insert: |
|
161 |
"sorted(inorder t) \<Longrightarrow> inorder(insert a t) = ins_list a (inorder t)" |
|
162 |
by(simp add: insert_def inorder_ins) |
|
163 |
||
164 |
||
165 |
subsubsection "Proofs for delete" |
|
166 |
||
167 |
lemma inorder_node21: "height r > 0 \<Longrightarrow> |
|
80628 | 168 |
inorder (tree\<^sub>d (node21 l' a r)) = inorder (tree\<^sub>d l') @ a # inorder r" |
61640 | 169 |
by(induct l' a r rule: node21.induct) auto |
170 |
||
171 |
lemma inorder_node22: "height l > 0 \<Longrightarrow> |
|
80628 | 172 |
inorder (tree\<^sub>d (node22 l a r')) = inorder l @ a # inorder (tree\<^sub>d r')" |
61640 | 173 |
by(induct l a r' rule: node22.induct) auto |
174 |
||
175 |
lemma inorder_node31: "height m > 0 \<Longrightarrow> |
|
80628 | 176 |
inorder (tree\<^sub>d (node31 l' a m b r)) = inorder (tree\<^sub>d l') @ a # inorder m @ b # inorder r" |
61640 | 177 |
by(induct l' a m b r rule: node31.induct) auto |
178 |
||
179 |
lemma inorder_node32: "height r > 0 \<Longrightarrow> |
|
80628 | 180 |
inorder (tree\<^sub>d (node32 l a m' b r)) = inorder l @ a # inorder (tree\<^sub>d m') @ b # inorder r" |
61640 | 181 |
by(induct l a m' b r rule: node32.induct) auto |
182 |
||
183 |
lemma inorder_node33: "height m > 0 \<Longrightarrow> |
|
80628 | 184 |
inorder (tree\<^sub>d (node33 l a m b r')) = inorder l @ a # inorder m @ b # inorder (tree\<^sub>d r')" |
61640 | 185 |
by(induct l a m b r' rule: node33.induct) auto |
186 |
||
187 |
lemmas inorder_nodes = inorder_node21 inorder_node22 |
|
188 |
inorder_node31 inorder_node32 inorder_node33 |
|
189 |
||
68020 | 190 |
lemma split_minD: |
70273 | 191 |
"split_min t = (x,t') \<Longrightarrow> complete t \<Longrightarrow> height t > 0 \<Longrightarrow> |
80628 | 192 |
x # inorder(tree\<^sub>d t') = inorder t" |
68020 | 193 |
by(induction t arbitrary: t' rule: split_min.induct) |
61640 | 194 |
(auto simp: inorder_nodes split: prod.splits) |
195 |
||
70273 | 196 |
lemma inorder_del: "\<lbrakk> complete t ; sorted(inorder t) \<rbrakk> \<Longrightarrow> |
80628 | 197 |
inorder(tree\<^sub>d (del x t)) = del_list x (inorder t)" |
61640 | 198 |
by(induction t rule: del.induct) |
68020 | 199 |
(auto simp: del_list_simps inorder_nodes split_minD split!: if_split prod.splits) |
61640 | 200 |
|
70273 | 201 |
lemma inorder_delete: "\<lbrakk> complete t ; sorted(inorder t) \<rbrakk> \<Longrightarrow> |
61640 | 202 |
inorder(delete x t) = del_list x (inorder t)" |
203 |
by(simp add: delete_def inorder_del) |
|
204 |
||
205 |
||
72566
831f17da1aab
renamed "balanced" -> "acomplete" because balanced has other meanings in the literature
nipkow
parents:
70628
diff
changeset
|
206 |
subsection \<open>Completeness\<close> |
61640 | 207 |
|
208 |
||
209 |
subsubsection "Proofs for insert" |
|
210 |
||
70273 | 211 |
text\<open>First a standard proof that \<^const>\<open>ins\<close> preserves \<^const>\<open>complete\<close>.\<close> |
61640 | 212 |
|
80628 | 213 |
fun h\<^sub>i :: "'a up\<^sub>i \<Rightarrow> nat" where |
214 |
"h\<^sub>i (Eq\<^sub>i t) = height t" | |
|
215 |
"h\<^sub>i (Of l a r) = height l" |
|
61640 | 216 |
|
80628 | 217 |
lemma complete_ins: "complete t \<Longrightarrow> complete (tree\<^sub>i(ins a t)) \<and> h\<^sub>i(ins a t) = height t" |
218 |
by (induct t) (auto split!: if_split up\<^sub>i.split) (* 15 secs in 2015 *) |
|
61640 | 219 |
|
67406 | 220 |
text\<open>Now an alternative proof (by Brian Huffman) that runs faster because |
72566
831f17da1aab
renamed "balanced" -> "acomplete" because balanced has other meanings in the literature
nipkow
parents:
70628
diff
changeset
|
221 |
two properties (completeness and height) are combined in one predicate.\<close> |
61640 | 222 |
|
223 |
inductive full :: "nat \<Rightarrow> 'a tree23 \<Rightarrow> bool" where |
|
224 |
"full 0 Leaf" | |
|
225 |
"\<lbrakk>full n l; full n r\<rbrakk> \<Longrightarrow> full (Suc n) (Node2 l p r)" | |
|
226 |
"\<lbrakk>full n l; full n m; full n r\<rbrakk> \<Longrightarrow> full (Suc n) (Node3 l p m q r)" |
|
227 |
||
228 |
inductive_cases full_elims: |
|
229 |
"full n Leaf" |
|
230 |
"full n (Node2 l p r)" |
|
231 |
"full n (Node3 l p m q r)" |
|
232 |
||
233 |
inductive_cases full_0_elim: "full 0 t" |
|
234 |
inductive_cases full_Suc_elim: "full (Suc n) t" |
|
235 |
||
236 |
lemma full_0_iff [simp]: "full 0 t \<longleftrightarrow> t = Leaf" |
|
237 |
by (auto elim: full_0_elim intro: full.intros) |
|
238 |
||
239 |
lemma full_Leaf_iff [simp]: "full n Leaf \<longleftrightarrow> n = 0" |
|
240 |
by (auto elim: full_elims intro: full.intros) |
|
241 |
||
242 |
lemma full_Suc_Node2_iff [simp]: |
|
243 |
"full (Suc n) (Node2 l p r) \<longleftrightarrow> full n l \<and> full n r" |
|
244 |
by (auto elim: full_elims intro: full.intros) |
|
245 |
||
246 |
lemma full_Suc_Node3_iff [simp]: |
|
247 |
"full (Suc n) (Node3 l p m q r) \<longleftrightarrow> full n l \<and> full n m \<and> full n r" |
|
248 |
by (auto elim: full_elims intro: full.intros) |
|
249 |
||
250 |
lemma full_imp_height: "full n t \<Longrightarrow> height t = n" |
|
251 |
by (induct set: full, simp_all) |
|
252 |
||
70273 | 253 |
lemma full_imp_complete: "full n t \<Longrightarrow> complete t" |
61640 | 254 |
by (induct set: full, auto dest: full_imp_height) |
255 |
||
70273 | 256 |
lemma complete_imp_full: "complete t \<Longrightarrow> full (height t) t" |
61640 | 257 |
by (induct t, simp_all) |
258 |
||
70273 | 259 |
lemma complete_iff_full: "complete t \<longleftrightarrow> (\<exists>n. full n t)" |
260 |
by (auto elim!: complete_imp_full full_imp_complete) |
|
61640 | 261 |
|
69597 | 262 |
text \<open>The \<^const>\<open>insert\<close> function either preserves the height of the |
80628 | 263 |
tree, or increases it by one. The constructor returned by the \<^term>\<open>insert\<close> function determines which: A return value of the form \<^term>\<open>Eq\<^sub>i t\<close> indicates that the height will be the same. A value of the |
264 |
form \<^term>\<open>Of l p r\<close> indicates an increase in height.\<close> |
|
61640 | 265 |
|
80628 | 266 |
fun full\<^sub>i :: "nat \<Rightarrow> 'a up\<^sub>i \<Rightarrow> bool" where |
267 |
"full\<^sub>i n (Eq\<^sub>i t) \<longleftrightarrow> full n t" | |
|
268 |
"full\<^sub>i n (Of l p r) \<longleftrightarrow> full n l \<and> full n r" |
|
61640 | 269 |
|
270 |
lemma full\<^sub>i_ins: "full n t \<Longrightarrow> full\<^sub>i n (ins a t)" |
|
80628 | 271 |
by (induct rule: full.induct) (auto split: up\<^sub>i.split) |
61640 | 272 |
|
70273 | 273 |
text \<open>The \<^const>\<open>insert\<close> operation preserves completeance.\<close> |
61640 | 274 |
|
70273 | 275 |
lemma complete_insert: "complete t \<Longrightarrow> complete (insert a t)" |
276 |
unfolding complete_iff_full insert_def |
|
61640 | 277 |
apply (erule exE) |
278 |
apply (drule full\<^sub>i_ins [of _ _ a]) |
|
279 |
apply (cases "ins a t") |
|
280 |
apply (auto intro: full.intros) |
|
281 |
done |
|
282 |
||
283 |
||
284 |
subsection "Proofs for delete" |
|
285 |
||
80628 | 286 |
fun h\<^sub>d :: "'a up\<^sub>d \<Rightarrow> nat" where |
287 |
"h\<^sub>d (Eq\<^sub>d t) = height t" | |
|
288 |
"h\<^sub>d (Uf t) = height t + 1" |
|
61640 | 289 |
|
80628 | 290 |
lemma complete_tree\<^sub>d_node21: |
291 |
"\<lbrakk>complete r; complete (tree\<^sub>d l'); height r = h\<^sub>d l' \<rbrakk> \<Longrightarrow> complete (tree\<^sub>d (node21 l' a r))" |
|
61640 | 292 |
by(induct l' a r rule: node21.induct) auto |
293 |
||
80628 | 294 |
lemma complete_tree\<^sub>d_node22: |
295 |
"\<lbrakk>complete(tree\<^sub>d r'); complete l; h\<^sub>d r' = height l \<rbrakk> \<Longrightarrow> complete (tree\<^sub>d (node22 l a r'))" |
|
61640 | 296 |
by(induct l a r' rule: node22.induct) auto |
297 |
||
80628 | 298 |
lemma complete_tree\<^sub>d_node31: |
299 |
"\<lbrakk> complete (tree\<^sub>d l'); complete m; complete r; h\<^sub>d l' = height r; height m = height r \<rbrakk> |
|
300 |
\<Longrightarrow> complete (tree\<^sub>d (node31 l' a m b r))" |
|
61640 | 301 |
by(induct l' a m b r rule: node31.induct) auto |
302 |
||
80628 | 303 |
lemma complete_tree\<^sub>d_node32: |
304 |
"\<lbrakk> complete l; complete (tree\<^sub>d m'); complete r; height l = height r; h\<^sub>d m' = height r \<rbrakk> |
|
305 |
\<Longrightarrow> complete (tree\<^sub>d (node32 l a m' b r))" |
|
61640 | 306 |
by(induct l a m' b r rule: node32.induct) auto |
307 |
||
80628 | 308 |
lemma complete_tree\<^sub>d_node33: |
309 |
"\<lbrakk> complete l; complete m; complete(tree\<^sub>d r'); height l = h\<^sub>d r'; height m = h\<^sub>d r' \<rbrakk> |
|
310 |
\<Longrightarrow> complete (tree\<^sub>d (node33 l a m b r'))" |
|
61640 | 311 |
by(induct l a m b r' rule: node33.induct) auto |
312 |
||
80628 | 313 |
lemmas completes = complete_tree\<^sub>d_node21 complete_tree\<^sub>d_node22 |
314 |
complete_tree\<^sub>d_node31 complete_tree\<^sub>d_node32 complete_tree\<^sub>d_node33 |
|
61640 | 315 |
|
316 |
lemma height'_node21: |
|
80628 | 317 |
"height r > 0 \<Longrightarrow> h\<^sub>d(node21 l' a r) = max (h\<^sub>d l') (height r) + 1" |
61640 | 318 |
by(induct l' a r rule: node21.induct)(simp_all) |
319 |
||
320 |
lemma height'_node22: |
|
80628 | 321 |
"height l > 0 \<Longrightarrow> h\<^sub>d(node22 l a r') = max (height l) (h\<^sub>d r') + 1" |
61640 | 322 |
by(induct l a r' rule: node22.induct)(simp_all) |
323 |
||
324 |
lemma height'_node31: |
|
80628 | 325 |
"height m > 0 \<Longrightarrow> h\<^sub>d(node31 l a m b r) = |
326 |
max (h\<^sub>d l) (max (height m) (height r)) + 1" |
|
61640 | 327 |
by(induct l a m b r rule: node31.induct)(simp_all add: max_def) |
328 |
||
329 |
lemma height'_node32: |
|
80628 | 330 |
"height r > 0 \<Longrightarrow> h\<^sub>d(node32 l a m b r) = |
331 |
max (height l) (max (h\<^sub>d m) (height r)) + 1" |
|
61640 | 332 |
by(induct l a m b r rule: node32.induct)(simp_all add: max_def) |
333 |
||
334 |
lemma height'_node33: |
|
80628 | 335 |
"height m > 0 \<Longrightarrow> h\<^sub>d(node33 l a m b r) = |
336 |
max (height l) (max (height m) (h\<^sub>d r)) + 1" |
|
61640 | 337 |
by(induct l a m b r rule: node33.induct)(simp_all add: max_def) |
338 |
||
339 |
lemmas heights = height'_node21 height'_node22 |
|
340 |
height'_node31 height'_node32 height'_node33 |
|
341 |
||
68020 | 342 |
lemma height_split_min: |
80628 | 343 |
"split_min t = (x, t') \<Longrightarrow> height t > 0 \<Longrightarrow> complete t \<Longrightarrow> h\<^sub>d t' = height t" |
68020 | 344 |
by(induct t arbitrary: x t' rule: split_min.induct) |
61640 | 345 |
(auto simp: heights split: prod.splits) |
346 |
||
80628 | 347 |
lemma height_del: "complete t \<Longrightarrow> h\<^sub>d(del x t) = height t" |
61640 | 348 |
by(induction x t rule: del.induct) |
68020 | 349 |
(auto simp: heights max_def height_split_min split: prod.splits) |
61640 | 350 |
|
70273 | 351 |
lemma complete_split_min: |
80628 | 352 |
"\<lbrakk> split_min t = (x, t'); complete t; height t > 0 \<rbrakk> \<Longrightarrow> complete (tree\<^sub>d t')" |
68020 | 353 |
by(induct t arbitrary: x t' rule: split_min.induct) |
70273 | 354 |
(auto simp: heights height_split_min completes split: prod.splits) |
61640 | 355 |
|
80628 | 356 |
lemma complete_tree\<^sub>d_del: "complete t \<Longrightarrow> complete(tree\<^sub>d(del x t))" |
61640 | 357 |
by(induction x t rule: del.induct) |
70273 | 358 |
(auto simp: completes complete_split_min height_del height_split_min split: prod.splits) |
61640 | 359 |
|
70273 | 360 |
corollary complete_delete: "complete t \<Longrightarrow> complete(delete x t)" |
80628 | 361 |
by(simp add: delete_def complete_tree\<^sub>d_del) |
61640 | 362 |
|
363 |
||
364 |
subsection \<open>Overall Correctness\<close> |
|
365 |
||
68440 | 366 |
interpretation S: Set_by_Ordered |
68431 | 367 |
where empty = empty and isin = isin and insert = insert and delete = delete |
70273 | 368 |
and inorder = inorder and inv = complete |
61640 | 369 |
proof (standard, goal_cases) |
370 |
case 2 thus ?case by(simp add: isin_set) |
|
371 |
next |
|
372 |
case 3 thus ?case by(simp add: inorder_insert) |
|
373 |
next |
|
374 |
case 4 thus ?case by(simp add: inorder_delete) |
|
375 |
next |
|
70273 | 376 |
case 6 thus ?case by(simp add: complete_insert) |
61640 | 377 |
next |
70273 | 378 |
case 7 thus ?case by(simp add: complete_delete) |
68431 | 379 |
qed (simp add: empty_def)+ |
61640 | 380 |
|
381 |
end |