152 "node44 t1 a t2 b t3 c (T\<^sub>d t4) = T\<^sub>d(Node4 t1 a t2 b t3 c t4)" | |
152 "node44 t1 a t2 b t3 c (T\<^sub>d t4) = T\<^sub>d(Node4 t1 a t2 b t3 c t4)" | |
153 "node44 t1 a t2 b (Node2 t3 c t4) d (Up\<^sub>d t5) = T\<^sub>d(Node3 t1 a t2 b (Node3 t3 c t4 d t5))" | |
153 "node44 t1 a t2 b (Node2 t3 c t4) d (Up\<^sub>d t5) = T\<^sub>d(Node3 t1 a t2 b (Node3 t3 c t4 d t5))" | |
154 "node44 t1 a t2 b (Node3 t3 c t4 d t5) e (Up\<^sub>d t6) = T\<^sub>d(Node4 t1 a t2 b (Node2 t3 c t4) d (Node2 t5 e t6))" | |
154 "node44 t1 a t2 b (Node3 t3 c t4 d t5) e (Up\<^sub>d t6) = T\<^sub>d(Node4 t1 a t2 b (Node2 t3 c t4) d (Node2 t5 e t6))" | |
155 "node44 t1 a t2 b (Node4 t3 c t4 d t5 e t6) f (Up\<^sub>d t7) = T\<^sub>d(Node4 t1 a t2 b (Node2 t3 c t4) d (Node3 t5 e t6 f t7))" |
155 "node44 t1 a t2 b (Node4 t3 c t4 d t5 e t6) f (Up\<^sub>d t7) = T\<^sub>d(Node4 t1 a t2 b (Node2 t3 c t4) d (Node3 t5 e t6 f t7))" |
156 |
156 |
157 fun del_min :: "'a tree234 \<Rightarrow> 'a * 'a up\<^sub>d" where |
157 fun split_min :: "'a tree234 \<Rightarrow> 'a * 'a up\<^sub>d" where |
158 "del_min (Node2 Leaf a Leaf) = (a, Up\<^sub>d Leaf)" | |
158 "split_min (Node2 Leaf a Leaf) = (a, Up\<^sub>d Leaf)" | |
159 "del_min (Node3 Leaf a Leaf b Leaf) = (a, T\<^sub>d(Node2 Leaf b Leaf))" | |
159 "split_min (Node3 Leaf a Leaf b Leaf) = (a, T\<^sub>d(Node2 Leaf b Leaf))" | |
160 "del_min (Node4 Leaf a Leaf b Leaf c Leaf) = (a, T\<^sub>d(Node3 Leaf b Leaf c Leaf))" | |
160 "split_min (Node4 Leaf a Leaf b Leaf c Leaf) = (a, T\<^sub>d(Node3 Leaf b Leaf c Leaf))" | |
161 "del_min (Node2 l a r) = (let (x,l') = del_min l in (x, node21 l' a r))" | |
161 "split_min (Node2 l a r) = (let (x,l') = split_min l in (x, node21 l' a r))" | |
162 "del_min (Node3 l a m b r) = (let (x,l') = del_min l in (x, node31 l' a m b r))" | |
162 "split_min (Node3 l a m b r) = (let (x,l') = split_min l in (x, node31 l' a m b r))" | |
163 "del_min (Node4 l a m b n c r) = (let (x,l') = del_min l in (x, node41 l' a m b n c r))" |
163 "split_min (Node4 l a m b n c r) = (let (x,l') = split_min l in (x, node41 l' a m b n c r))" |
164 |
164 |
165 fun del :: "'a::linorder \<Rightarrow> 'a tree234 \<Rightarrow> 'a up\<^sub>d" where |
165 fun del :: "'a::linorder \<Rightarrow> 'a tree234 \<Rightarrow> 'a up\<^sub>d" where |
166 "del k Leaf = T\<^sub>d Leaf" | |
166 "del k Leaf = T\<^sub>d Leaf" | |
167 "del k (Node2 Leaf p Leaf) = (if k=p then Up\<^sub>d Leaf else T\<^sub>d(Node2 Leaf p Leaf))" | |
167 "del k (Node2 Leaf p Leaf) = (if k=p then Up\<^sub>d Leaf else T\<^sub>d(Node2 Leaf p Leaf))" | |
168 "del k (Node3 Leaf p Leaf q Leaf) = T\<^sub>d(if k=p then Node2 Leaf q Leaf |
168 "del k (Node3 Leaf p Leaf q Leaf) = T\<^sub>d(if k=p then Node2 Leaf q Leaf |
173 if k=c then Node3 Leaf a Leaf b Leaf |
173 if k=c then Node3 Leaf a Leaf b Leaf |
174 else Node4 Leaf a Leaf b Leaf c Leaf)" | |
174 else Node4 Leaf a Leaf b Leaf c Leaf)" | |
175 "del k (Node2 l a r) = (case cmp k a of |
175 "del k (Node2 l a r) = (case cmp k a of |
176 LT \<Rightarrow> node21 (del k l) a r | |
176 LT \<Rightarrow> node21 (del k l) a r | |
177 GT \<Rightarrow> node22 l a (del k r) | |
177 GT \<Rightarrow> node22 l a (del k r) | |
178 EQ \<Rightarrow> let (a',t) = del_min r in node22 l a' t)" | |
178 EQ \<Rightarrow> let (a',t) = split_min r in node22 l a' t)" | |
179 "del k (Node3 l a m b r) = (case cmp k a of |
179 "del k (Node3 l a m b r) = (case cmp k a of |
180 LT \<Rightarrow> node31 (del k l) a m b r | |
180 LT \<Rightarrow> node31 (del k l) a m b r | |
181 EQ \<Rightarrow> let (a',m') = del_min m in node32 l a' m' b r | |
181 EQ \<Rightarrow> let (a',m') = split_min m in node32 l a' m' b r | |
182 GT \<Rightarrow> (case cmp k b of |
182 GT \<Rightarrow> (case cmp k b of |
183 LT \<Rightarrow> node32 l a (del k m) b r | |
183 LT \<Rightarrow> node32 l a (del k m) b r | |
184 EQ \<Rightarrow> let (b',r') = del_min r in node33 l a m b' r' | |
184 EQ \<Rightarrow> let (b',r') = split_min r in node33 l a m b' r' | |
185 GT \<Rightarrow> node33 l a m b (del k r)))" | |
185 GT \<Rightarrow> node33 l a m b (del k r)))" | |
186 "del k (Node4 l a m b n c r) = (case cmp k b of |
186 "del k (Node4 l a m b n c r) = (case cmp k b of |
187 LT \<Rightarrow> (case cmp k a of |
187 LT \<Rightarrow> (case cmp k a of |
188 LT \<Rightarrow> node41 (del k l) a m b n c r | |
188 LT \<Rightarrow> node41 (del k l) a m b n c r | |
189 EQ \<Rightarrow> let (a',m') = del_min m in node42 l a' m' b n c r | |
189 EQ \<Rightarrow> let (a',m') = split_min m in node42 l a' m' b n c r | |
190 GT \<Rightarrow> node42 l a (del k m) b n c r) | |
190 GT \<Rightarrow> node42 l a (del k m) b n c r) | |
191 EQ \<Rightarrow> let (b',n') = del_min n in node43 l a m b' n' c r | |
191 EQ \<Rightarrow> let (b',n') = split_min n in node43 l a m b' n' c r | |
192 GT \<Rightarrow> (case cmp k c of |
192 GT \<Rightarrow> (case cmp k c of |
193 LT \<Rightarrow> node43 l a m b (del k n) c r | |
193 LT \<Rightarrow> node43 l a m b (del k n) c r | |
194 EQ \<Rightarrow> let (c',r') = del_min r in node44 l a m b n c' r' | |
194 EQ \<Rightarrow> let (c',r') = split_min r in node44 l a m b n c' r' | |
195 GT \<Rightarrow> node44 l a m b n c (del k r)))" |
195 GT \<Rightarrow> node44 l a m b n c (del k r)))" |
196 |
196 |
197 definition delete :: "'a::linorder \<Rightarrow> 'a tree234 \<Rightarrow> 'a tree234" where |
197 definition delete :: "'a::linorder \<Rightarrow> 'a tree234 \<Rightarrow> 'a tree234" where |
198 "delete x t = tree\<^sub>d(del x t)" |
198 "delete x t = tree\<^sub>d(del x t)" |
199 |
199 |
257 |
257 |
258 lemmas inorder_nodes = inorder_node21 inorder_node22 |
258 lemmas inorder_nodes = inorder_node21 inorder_node22 |
259 inorder_node31 inorder_node32 inorder_node33 |
259 inorder_node31 inorder_node32 inorder_node33 |
260 inorder_node41 inorder_node42 inorder_node43 inorder_node44 |
260 inorder_node41 inorder_node42 inorder_node43 inorder_node44 |
261 |
261 |
262 lemma del_minD: |
262 lemma split_minD: |
263 "del_min t = (x,t') \<Longrightarrow> bal t \<Longrightarrow> height t > 0 \<Longrightarrow> |
263 "split_min t = (x,t') \<Longrightarrow> bal t \<Longrightarrow> height t > 0 \<Longrightarrow> |
264 x # inorder(tree\<^sub>d t') = inorder t" |
264 x # inorder(tree\<^sub>d t') = inorder t" |
265 by(induction t arbitrary: t' rule: del_min.induct) |
265 by(induction t arbitrary: t' rule: split_min.induct) |
266 (auto simp: inorder_nodes split: prod.splits) |
266 (auto simp: inorder_nodes split: prod.splits) |
267 |
267 |
268 lemma inorder_del: "\<lbrakk> bal t ; sorted(inorder t) \<rbrakk> \<Longrightarrow> |
268 lemma inorder_del: "\<lbrakk> bal t ; sorted(inorder t) \<rbrakk> \<Longrightarrow> |
269 inorder(tree\<^sub>d (del x t)) = del_list x (inorder t)" |
269 inorder(tree\<^sub>d (del x t)) = del_list x (inorder t)" |
270 by(induction t rule: del.induct) |
270 by(induction t rule: del.induct) |
271 (auto simp: inorder_nodes del_list_simps del_minD split!: if_split prod.splits) |
271 (auto simp: inorder_nodes del_list_simps split_minD split!: if_split prod.splits) |
272 (* 30 secs (2016) *) |
272 (* 30 secs (2016) *) |
273 |
273 |
274 lemma inorder_delete: "\<lbrakk> bal t ; sorted(inorder t) \<rbrakk> \<Longrightarrow> |
274 lemma inorder_delete: "\<lbrakk> bal t ; sorted(inorder t) \<rbrakk> \<Longrightarrow> |
275 inorder(delete x t) = del_list x (inorder t)" |
275 inorder(delete x t) = del_list x (inorder t)" |
276 by(simp add: delete_def inorder_del) |
276 by(simp add: delete_def inorder_del) |
474 |
474 |
475 lemmas heights = height_node21 height_node22 |
475 lemmas heights = height_node21 height_node22 |
476 height_node31 height_node32 height_node33 |
476 height_node31 height_node32 height_node33 |
477 height_node41 height_node42 height_node43 height_node44 |
477 height_node41 height_node42 height_node43 height_node44 |
478 |
478 |
479 lemma height_del_min: |
479 lemma height_split_min: |
480 "del_min t = (x, t') \<Longrightarrow> height t > 0 \<Longrightarrow> bal t \<Longrightarrow> height t' = height t" |
480 "split_min t = (x, t') \<Longrightarrow> height t > 0 \<Longrightarrow> bal t \<Longrightarrow> height t' = height t" |
481 by(induct t arbitrary: x t' rule: del_min.induct) |
481 by(induct t arbitrary: x t' rule: split_min.induct) |
482 (auto simp: heights split: prod.splits) |
482 (auto simp: heights split: prod.splits) |
483 |
483 |
484 lemma height_del: "bal t \<Longrightarrow> height(del x t) = height t" |
484 lemma height_del: "bal t \<Longrightarrow> height(del x t) = height t" |
485 by(induction x t rule: del.induct) |
485 by(induction x t rule: del.induct) |
486 (auto simp add: heights height_del_min split!: if_split prod.split) |
486 (auto simp add: heights height_split_min split!: if_split prod.split) |
487 |
487 |
488 lemma bal_del_min: |
488 lemma bal_split_min: |
489 "\<lbrakk> del_min t = (x, t'); bal t; height t > 0 \<rbrakk> \<Longrightarrow> bal (tree\<^sub>d t')" |
489 "\<lbrakk> split_min t = (x, t'); bal t; height t > 0 \<rbrakk> \<Longrightarrow> bal (tree\<^sub>d t')" |
490 by(induct t arbitrary: x t' rule: del_min.induct) |
490 by(induct t arbitrary: x t' rule: split_min.induct) |
491 (auto simp: heights height_del_min bals split: prod.splits) |
491 (auto simp: heights height_split_min bals split: prod.splits) |
492 |
492 |
493 lemma bal_tree\<^sub>d_del: "bal t \<Longrightarrow> bal(tree\<^sub>d(del x t))" |
493 lemma bal_tree\<^sub>d_del: "bal t \<Longrightarrow> bal(tree\<^sub>d(del x t))" |
494 by(induction x t rule: del.induct) |
494 by(induction x t rule: del.induct) |
495 (auto simp: bals bal_del_min height_del height_del_min split!: if_split prod.split) |
495 (auto simp: bals bal_split_min height_del height_split_min split!: if_split prod.split) |
496 |
496 |
497 corollary bal_delete: "bal t \<Longrightarrow> bal(delete x t)" |
497 corollary bal_delete: "bal t \<Longrightarrow> bal(delete x t)" |
498 by(simp add: delete_def bal_tree\<^sub>d_del) |
498 by(simp add: delete_def bal_tree\<^sub>d_del) |
499 |
499 |
500 subsection \<open>Overall Correctness\<close> |
500 subsection \<open>Overall Correctness\<close> |