| author | wenzelm | 
| Wed, 11 Oct 2023 12:37:11 +0200 | |
| changeset 78761 | 6b3f13d39612 | 
| parent 78199 | d6e6618db929 | 
| child 80628 | 161286c9d426 | 
| 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: 
62130diff
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 | |
| 70274 | 34 | datatype 'a upI = TI "'a tree23" | OF "'a tree23" 'a "'a tree23" | 
| 61640 | 35 | |
| 70274 | 36 | fun treeI :: "'a upI \<Rightarrow> 'a tree23" where | 
| 37 | "treeI (TI t) = t" | | |
| 38 | "treeI (OF l a r) = Node2 l a r" | |
| 61640 | 39 | |
| 70274 | 40 | fun ins :: "'a::linorder \<Rightarrow> 'a tree23 \<Rightarrow> 'a upI" 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 | |
| 70274 | 46 | TI l' => TI (Node2 l' a r) | | 
| 47 | OF l1 b l2 => TI (Node3 l1 b l2 a r)) | | |
| 72805 | 48 | EQ \<Rightarrow> TI (Node2 l a r) | | 
| 61678 | 49 | GT \<Rightarrow> | 
| 50 | (case ins x r of | |
| 70274 | 51 | TI r' => TI (Node2 l a r') | | 
| 52 | OF r1 b r2 => TI (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 | |
| 70274 | 57 | TI l' => TI (Node3 l' a m b r) | | 
| 58 | OF l1 c l2 => OF (Node2 l1 c l2) a (Node2 m b r)) | | |
| 59 | EQ \<Rightarrow> TI (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 | |
| 70274 | 64 | TI r' => TI (Node3 l a m b r') | | 
| 65 | OF r1 c r2 => OF (Node2 l a m) b (Node2 r1 c r2)) | | |
| 66 | EQ \<Rightarrow> TI (Node3 l a m b r) | | |
| 61678 | 67 | LT \<Rightarrow> | 
| 68 | (case ins x m of | |
| 70274 | 69 | TI m' => TI (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: 
62130diff
changeset | 74 | definition insert :: "'a::linorder \<Rightarrow> 'a tree23 \<Rightarrow> 'a tree23" where | 
| 70274 | 75 | "insert x t = treeI(ins x t)" | 
| 61640 | 76 | |
| 70274 | 77 | datatype 'a upD = TD "'a tree23" | UF "'a tree23" | 
| 61640 | 78 | |
| 70274 | 79 | fun treeD :: "'a upD \<Rightarrow> 'a tree23" where | 
| 80 | "treeD (TD t) = t" | | |
| 81 | "treeD (UF t) = t" | |
| 61640 | 82 | |
| 83 | (* Variation: return None to signal no-change *) | |
| 84 | ||
| 70274 | 85 | fun node21 :: "'a upD \<Rightarrow> 'a \<Rightarrow> 'a tree23 \<Rightarrow> 'a upD" where | 
| 86 | "node21 (TD t1) a t2 = TD(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) = TD(Node2 (Node2 t1 a t2) b (Node2 t3 c t4))" | |
| 61640 | 89 | |
| 70274 | 90 | fun node22 :: "'a tree23 \<Rightarrow> 'a \<Rightarrow> 'a upD \<Rightarrow> 'a upD" where | 
| 91 | "node22 t1 a (TD t2) = TD(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) = TD(Node2 (Node2 t1 b t2) c (Node2 t3 a t4))" | |
| 61640 | 94 | |
| 70274 | 95 | fun node31 :: "'a upD \<Rightarrow> 'a \<Rightarrow> 'a tree23 \<Rightarrow> 'a \<Rightarrow> 'a tree23 \<Rightarrow> 'a upD" where | 
| 96 | "node31 (TD t1) a t2 b t3 = TD(Node3 t1 a t2 b t3)" | | |
| 97 | "node31 (UF t1) a (Node2 t2 b t3) c t4 = TD(Node2 (Node3 t1 a t2 b t3) c t4)" | | |
| 98 | "node31 (UF t1) a (Node3 t2 b t3 c t4) d t5 = TD(Node3 (Node2 t1 a t2) b (Node2 t3 c t4) d t5)" | |
| 61640 | 99 | |
| 70274 | 100 | fun node32 :: "'a tree23 \<Rightarrow> 'a \<Rightarrow> 'a upD \<Rightarrow> 'a \<Rightarrow> 'a tree23 \<Rightarrow> 'a upD" where | 
| 101 | "node32 t1 a (TD t2) b t3 = TD(Node3 t1 a t2 b t3)" | | |
| 102 | "node32 t1 a (UF t2) b (Node2 t3 c t4) = TD(Node2 t1 a (Node3 t2 b t3 c t4))" | | |
| 103 | "node32 t1 a (UF t2) b (Node3 t3 c t4 d t5) = TD(Node3 t1 a (Node2 t2 b t3) c (Node2 t4 d t5))" | |
| 61640 | 104 | |
| 70274 | 105 | fun node33 :: "'a tree23 \<Rightarrow> 'a \<Rightarrow> 'a tree23 \<Rightarrow> 'a \<Rightarrow> 'a upD \<Rightarrow> 'a upD" where | 
| 78199 | 106 | "node33 t1 a t2 b (TD t3) = TD(Node3 t1 a t2 b t3)" | | 
| 70274 | 107 | "node33 t1 a (Node2 t2 b t3) c (UF t4) = TD(Node2 t1 a (Node3 t2 b t3 c t4))" | | 
| 108 | "node33 t1 a (Node3 t2 b t3 c t4) d (UF t5) = TD(Node3 t1 a (Node2 t2 b t3) c (Node2 t4 d t5))" | |
| 61640 | 109 | |
| 70274 | 110 | fun split_min :: "'a tree23 \<Rightarrow> 'a * 'a upD" where | 
| 111 | "split_min (Node2 Leaf a Leaf) = (a, UF Leaf)" | | |
| 112 | "split_min (Node3 Leaf a Leaf b Leaf) = (a, TD(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: 
70628diff
changeset | 117 | in which case completeness implies that so are the others. Exercise.\<close> | 
| 67038 | 118 | |
| 70274 | 119 | fun del :: "'a::linorder \<Rightarrow> 'a tree23 \<Rightarrow> 'a upD" where | 
| 120 | "del x Leaf = TD Leaf" | | |
| 61678 | 121 | "del x (Node2 Leaf a Leaf) = | 
| 70274 | 122 | (if x = a then UF Leaf else TD(Node2 Leaf a Leaf))" | | 
| 61678 | 123 | "del x (Node3 Leaf a Leaf b Leaf) = | 
| 70274 | 124 | TD(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: 
62130diff
changeset | 142 | definition delete :: "'a::linorder \<Rightarrow> 'a tree23 \<Rightarrow> 'a tree23" where | 
| 70274 | 143 | "delete x t = treeD(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: | |
| 70274 | 157 | "sorted(inorder t) \<Longrightarrow> inorder(treeI(ins x t)) = ins_list x (inorder t)" | 
| 158 | by(induction t) (auto simp: ins_list_simps split: upI.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> | |
| 70274 | 168 | inorder (treeD (node21 l' a r)) = inorder (treeD l') @ a # inorder r" | 
| 61640 | 169 | by(induct l' a r rule: node21.induct) auto | 
| 170 | ||
| 171 | lemma inorder_node22: "height l > 0 \<Longrightarrow> | |
| 70274 | 172 | inorder (treeD (node22 l a r')) = inorder l @ a # inorder (treeD r')" | 
| 61640 | 173 | by(induct l a r' rule: node22.induct) auto | 
| 174 | ||
| 175 | lemma inorder_node31: "height m > 0 \<Longrightarrow> | |
| 70274 | 176 | inorder (treeD (node31 l' a m b r)) = inorder (treeD 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> | |
| 70274 | 180 | inorder (treeD (node32 l a m' b r)) = inorder l @ a # inorder (treeD 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> | |
| 70274 | 184 | inorder (treeD (node33 l a m b r')) = inorder l @ a # inorder m @ b # inorder (treeD 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> | 
| 70274 | 192 | x # inorder(treeD 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> | 
| 70274 | 197 | inorder(treeD (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: 
70628diff
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 | |
| 72805 | 213 | fun hI :: "'a upI \<Rightarrow> nat" where | 
| 214 | "hI (TI t) = height t" | | |
| 215 | "hI (OF l a r) = height l" | |
| 61640 | 216 | |
| 72805 | 217 | lemma complete_ins: "complete t \<Longrightarrow> complete (treeI(ins a t)) \<and> hI(ins a t) = height t" | 
| 70274 | 218 | by (induct t) (auto split!: if_split upI.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: 
70628diff
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 | 
| 70274 | 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>TI 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 | |
| 70274 | 266 | fun full\<^sub>i :: "nat \<Rightarrow> 'a upI \<Rightarrow> bool" where | 
| 267 | "full\<^sub>i n (TI 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)" | |
| 70274 | 271 | by (induct rule: full.induct) (auto split: upI.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 | ||
| 72805 | 286 | fun hD :: "'a upD \<Rightarrow> nat" where | 
| 287 | "hD (TD t) = height t" | | |
| 288 | "hD (UF t) = height t + 1" | |
| 61640 | 289 | |
| 70274 | 290 | lemma complete_treeD_node21: | 
| 72805 | 291 | "\<lbrakk>complete r; complete (treeD l'); height r = hD l' \<rbrakk> \<Longrightarrow> complete (treeD (node21 l' a r))" | 
| 61640 | 292 | by(induct l' a r rule: node21.induct) auto | 
| 293 | ||
| 70274 | 294 | lemma complete_treeD_node22: | 
| 72805 | 295 | "\<lbrakk>complete(treeD r'); complete l; hD r' = height l \<rbrakk> \<Longrightarrow> complete (treeD (node22 l a r'))" | 
| 61640 | 296 | by(induct l a r' rule: node22.induct) auto | 
| 297 | ||
| 70274 | 298 | lemma complete_treeD_node31: | 
| 72805 | 299 | "\<lbrakk> complete (treeD l'); complete m; complete r; hD l' = height r; height m = height r \<rbrakk> | 
| 70274 | 300 | \<Longrightarrow> complete (treeD (node31 l' a m b r))" | 
| 61640 | 301 | by(induct l' a m b r rule: node31.induct) auto | 
| 302 | ||
| 70274 | 303 | lemma complete_treeD_node32: | 
| 72805 | 304 | "\<lbrakk> complete l; complete (treeD m'); complete r; height l = height r; hD m' = height r \<rbrakk> | 
| 70274 | 305 | \<Longrightarrow> complete (treeD (node32 l a m' b r))" | 
| 61640 | 306 | by(induct l a m' b r rule: node32.induct) auto | 
| 307 | ||
| 70274 | 308 | lemma complete_treeD_node33: | 
| 72805 | 309 | "\<lbrakk> complete l; complete m; complete(treeD r'); height l = hD r'; height m = hD r' \<rbrakk> | 
| 70274 | 310 | \<Longrightarrow> complete (treeD (node33 l a m b r'))" | 
| 61640 | 311 | by(induct l a m b r' rule: node33.induct) auto | 
| 312 | ||
| 70274 | 313 | lemmas completes = complete_treeD_node21 complete_treeD_node22 | 
| 314 | complete_treeD_node31 complete_treeD_node32 complete_treeD_node33 | |
| 61640 | 315 | |
| 316 | lemma height'_node21: | |
| 72805 | 317 | "height r > 0 \<Longrightarrow> hD(node21 l' a r) = max (hD l') (height r) + 1" | 
| 61640 | 318 | by(induct l' a r rule: node21.induct)(simp_all) | 
| 319 | ||
| 320 | lemma height'_node22: | |
| 72805 | 321 | "height l > 0 \<Longrightarrow> hD(node22 l a r') = max (height l) (hD r') + 1" | 
| 61640 | 322 | by(induct l a r' rule: node22.induct)(simp_all) | 
| 323 | ||
| 324 | lemma height'_node31: | |
| 72805 | 325 | "height m > 0 \<Longrightarrow> hD(node31 l a m b r) = | 
| 326 | max (hD 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: | |
| 72805 | 330 | "height r > 0 \<Longrightarrow> hD(node32 l a m b r) = | 
| 331 | max (height l) (max (hD 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: | |
| 72805 | 335 | "height m > 0 \<Longrightarrow> hD(node33 l a m b r) = | 
| 336 | max (height l) (max (height m) (hD 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: | 
| 72805 | 343 | "split_min t = (x, t') \<Longrightarrow> height t > 0 \<Longrightarrow> complete t \<Longrightarrow> hD t' = height t" | 
| 68020 | 344 | by(induct t arbitrary: x t' rule: split_min.induct) | 
| 61640 | 345 | (auto simp: heights split: prod.splits) | 
| 346 | ||
| 72805 | 347 | lemma height_del: "complete t \<Longrightarrow> hD(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: | 
| 70274 | 352 | "\<lbrakk> split_min t = (x, t'); complete t; height t > 0 \<rbrakk> \<Longrightarrow> complete (treeD 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 | |
| 70274 | 356 | lemma complete_treeD_del: "complete t \<Longrightarrow> complete(treeD(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)" | 
| 70274 | 361 | by(simp add: delete_def complete_treeD_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 |