src/HOL/Data_Structures/AA_Set.thy
 author nipkow Mon Apr 23 08:09:50 2018 +0200 (13 months ago) changeset 68023 75130777ece4 parent 67967 5a4280946a25 child 68413 b56ed5010e69 permissions -rw-r--r--
del_max -> split_max
 nipkow@61793 ` 1` ```(* ``` nipkow@63411 ` 2` ```Author: Tobias Nipkow, Daniel StÃ¼we ``` nipkow@61793 ` 3` ```*) ``` nipkow@61793 ` 4` nipkow@62130 ` 5` ```section \AA Tree Implementation of Sets\ ``` nipkow@61793 ` 6` nipkow@61793 ` 7` ```theory AA_Set ``` nipkow@61793 ` 8` ```imports ``` nipkow@61793 ` 9` ``` Isin2 ``` nipkow@61793 ` 10` ``` Cmp ``` nipkow@61793 ` 11` ```begin ``` nipkow@61793 ` 12` nipkow@61793 ` 13` ```type_synonym 'a aa_tree = "('a,nat) tree" ``` nipkow@61793 ` 14` nipkow@61793 ` 15` ```fun lvl :: "'a aa_tree \ nat" where ``` nipkow@61793 ` 16` ```"lvl Leaf = 0" | ``` nipkow@61793 ` 17` ```"lvl (Node lv _ _ _) = lv" ``` nipkow@62496 ` 18` nipkow@61793 ` 19` ```fun invar :: "'a aa_tree \ bool" where ``` nipkow@61793 ` 20` ```"invar Leaf = True" | ``` nipkow@61793 ` 21` ```"invar (Node h l a r) = ``` nipkow@61793 ` 22` ``` (invar l \ invar r \ ``` nipkow@61793 ` 23` ``` h = lvl l + 1 \ (h = lvl r + 1 \ (\lr b rr. r = Node h lr b rr \ h = lvl rr + 1)))" ``` nipkow@62496 ` 24` nipkow@61793 ` 25` ```fun skew :: "'a aa_tree \ 'a aa_tree" where ``` nipkow@61793 ` 26` ```"skew (Node lva (Node lvb t1 b t2) a t3) = ``` nipkow@61793 ` 27` ``` (if lva = lvb then Node lva t1 b (Node lva t2 a t3) else Node lva (Node lvb t1 b t2) a t3)" | ``` nipkow@61793 ` 28` ```"skew t = t" ``` nipkow@61793 ` 29` nipkow@61793 ` 30` ```fun split :: "'a aa_tree \ 'a aa_tree" where ``` nipkow@61793 ` 31` ```"split (Node lva t1 a (Node lvb t2 b (Node lvc t3 c t4))) = ``` wenzelm@67369 ` 32` ``` (if lva = lvb \ lvb = lvc \ \\lva = lvc\ suffices\ ``` nipkow@61793 ` 33` ``` then Node (lva+1) (Node lva t1 a t2) b (Node lva t3 c t4) ``` nipkow@61793 ` 34` ``` else Node lva t1 a (Node lvb t2 b (Node lvc t3 c t4)))" | ``` nipkow@61793 ` 35` ```"split t = t" ``` nipkow@61793 ` 36` nipkow@61793 ` 37` ```hide_const (open) insert ``` nipkow@61793 ` 38` nipkow@63411 ` 39` ```fun insert :: "'a::linorder \ 'a aa_tree \ 'a aa_tree" where ``` nipkow@61793 ` 40` ```"insert x Leaf = Node 1 Leaf x Leaf" | ``` nipkow@61793 ` 41` ```"insert x (Node lv t1 a t2) = ``` nipkow@61793 ` 42` ``` (case cmp x a of ``` nipkow@61793 ` 43` ``` LT \ split (skew (Node lv (insert x t1) a t2)) | ``` nipkow@61793 ` 44` ``` GT \ split (skew (Node lv t1 a (insert x t2))) | ``` nipkow@61793 ` 45` ``` EQ \ Node lv t1 x t2)" ``` nipkow@61793 ` 46` nipkow@61793 ` 47` ```fun sngl :: "'a aa_tree \ bool" where ``` nipkow@61793 ` 48` ```"sngl Leaf = False" | ``` nipkow@61793 ` 49` ```"sngl (Node _ _ _ Leaf) = True" | ``` nipkow@61793 ` 50` ```"sngl (Node lva _ _ (Node lvb _ _ _)) = (lva > lvb)" ``` nipkow@61793 ` 51` nipkow@61793 ` 52` ```definition adjust :: "'a aa_tree \ 'a aa_tree" where ``` nipkow@61793 ` 53` ```"adjust t = ``` nipkow@61793 ` 54` ``` (case t of ``` nipkow@61793 ` 55` ``` Node lv l x r \ ``` nipkow@61793 ` 56` ``` (if lvl l >= lv-1 \ lvl r >= lv-1 then t else ``` nipkow@61793 ` 57` ``` if lvl r < lv-1 \ sngl l then skew (Node (lv-1) l x r) else ``` nipkow@61793 ` 58` ``` if lvl r < lv-1 ``` nipkow@61793 ` 59` ``` then case l of ``` nipkow@61793 ` 60` ``` Node lva t1 a (Node lvb t2 b t3) ``` nipkow@62496 ` 61` ``` \ Node (lvb+1) (Node lva t1 a t2) b (Node (lv-1) t3 x r) ``` nipkow@61793 ` 62` ``` else ``` nipkow@61793 ` 63` ``` if lvl r < lv then split (Node (lv-1) l x r) ``` nipkow@61793 ` 64` ``` else ``` nipkow@61793 ` 65` ``` case r of ``` nipkow@62160 ` 66` ``` Node lvb t1 b t4 \ ``` nipkow@61793 ` 67` ``` (case t1 of ``` nipkow@61793 ` 68` ``` Node lva t2 a t3 ``` nipkow@61793 ` 69` ``` \ Node (lva+1) (Node (lv-1) l x t2) a ``` nipkow@62496 ` 70` ``` (split (Node (if sngl t1 then lva else lva+1) t3 b t4)))))" ``` nipkow@62496 ` 71` wenzelm@67406 ` 72` ```text\In the paper, the last case of @{const adjust} is expressed with the help of an ``` nipkow@62496 ` 73` ```incorrect auxiliary function \texttt{nlvl}. ``` nipkow@62496 ` 74` nipkow@68023 ` 75` ```Function @{text split_max} below is called \texttt{dellrg} in the paper. ``` nipkow@62496 ` 76` ```The latter is incorrect for two reasons: \texttt{dellrg} is meant to delete the largest ``` nipkow@62496 ` 77` ```element but recurses on the left instead of the right subtree; the invariant ``` wenzelm@67406 ` 78` ```is not restored.\ ``` nipkow@62496 ` 79` nipkow@68023 ` 80` ```fun split_max :: "'a aa_tree \ 'a aa_tree * 'a" where ``` nipkow@68023 ` 81` ```"split_max (Node lv l a Leaf) = (l,a)" | ``` nipkow@68023 ` 82` ```"split_max (Node lv l a r) = (let (r',b) = split_max r in (adjust(Node lv l a r'), b))" ``` nipkow@61793 ` 83` nipkow@63411 ` 84` ```fun delete :: "'a::linorder \ 'a aa_tree \ 'a aa_tree" where ``` nipkow@61793 ` 85` ```"delete _ Leaf = Leaf" | ``` nipkow@61793 ` 86` ```"delete x (Node lv l a r) = ``` nipkow@61793 ` 87` ``` (case cmp x a of ``` nipkow@61793 ` 88` ``` LT \ adjust (Node lv (delete x l) a r) | ``` nipkow@61793 ` 89` ``` GT \ adjust (Node lv l a (delete x r)) | ``` nipkow@61793 ` 90` ``` EQ \ (if l = Leaf then r ``` nipkow@68023 ` 91` ``` else let (l',b) = split_max l in adjust (Node lv l' b r)))" ``` nipkow@61793 ` 92` nipkow@62496 ` 93` ```fun pre_adjust where ``` nipkow@62496 ` 94` ```"pre_adjust (Node lv l a r) = (invar l \ invar r \ ``` nipkow@62496 ` 95` ``` ((lv = lvl l + 1 \ (lv = lvl r + 1 \ lv = lvl r + 2 \ lv = lvl r \ sngl r)) \ ``` nipkow@62496 ` 96` ``` (lv = lvl l + 2 \ (lv = lvl r + 1 \ lv = lvl r \ sngl r))))" ``` nipkow@62496 ` 97` nipkow@62496 ` 98` ```declare pre_adjust.simps [simp del] ``` nipkow@62496 ` 99` nipkow@62496 ` 100` ```subsection "Auxiliary Proofs" ``` nipkow@62496 ` 101` nipkow@62496 ` 102` ```lemma split_case: "split t = (case t of ``` nipkow@62496 ` 103` ``` Node lvx a x (Node lvy b y (Node lvz c z d)) \ ``` nipkow@62496 ` 104` ``` (if lvx = lvy \ lvy = lvz ``` nipkow@62496 ` 105` ``` then Node (lvx+1) (Node lvx a x b) y (Node lvx c z d) ``` nipkow@62496 ` 106` ``` else t) ``` nipkow@62496 ` 107` ``` | t \ t)" ``` nipkow@62496 ` 108` ```by(auto split: tree.split) ``` nipkow@62496 ` 109` nipkow@62496 ` 110` ```lemma skew_case: "skew t = (case t of ``` nipkow@62496 ` 111` ``` Node lvx (Node lvy a y b) x c \ ``` nipkow@62496 ` 112` ``` (if lvx = lvy then Node lvx a y (Node lvx b x c) else t) ``` nipkow@62496 ` 113` ``` | t \ t)" ``` nipkow@62496 ` 114` ```by(auto split: tree.split) ``` nipkow@62496 ` 115` nipkow@62496 ` 116` ```lemma lvl_0_iff: "invar t \ lvl t = 0 \ t = Leaf" ``` nipkow@62496 ` 117` ```by(cases t) auto ``` nipkow@62496 ` 118` nipkow@62496 ` 119` ```lemma lvl_Suc_iff: "lvl t = Suc n \ (\ l a r. t = Node (Suc n) l a r)" ``` nipkow@62496 ` 120` ```by(cases t) auto ``` nipkow@62496 ` 121` nipkow@62496 ` 122` ```lemma lvl_skew: "lvl (skew t) = lvl t" ``` nipkow@62526 ` 123` ```by(cases t rule: skew.cases) auto ``` nipkow@62496 ` 124` nipkow@62496 ` 125` ```lemma lvl_split: "lvl (split t) = lvl t \ lvl (split t) = lvl t + 1 \ sngl (split t)" ``` nipkow@62526 ` 126` ```by(cases t rule: split.cases) auto ``` nipkow@62496 ` 127` nipkow@62496 ` 128` ```lemma invar_2Nodes:"invar (Node lv l x (Node rlv rl rx rr)) = ``` nipkow@62496 ` 129` ``` (invar l \ invar \rlv, rl, rx, rr\ \ lv = Suc (lvl l) \ ``` nipkow@62496 ` 130` ``` (lv = Suc rlv \ rlv = lv \ lv = Suc (lvl rr)))" ``` nipkow@62496 ` 131` ```by simp ``` nipkow@62496 ` 132` nipkow@62496 ` 133` ```lemma invar_NodeLeaf[simp]: ``` nipkow@62496 ` 134` ``` "invar (Node lv l x Leaf) = (invar l \ lv = Suc (lvl l) \ lv = Suc 0)" ``` nipkow@62496 ` 135` ```by simp ``` nipkow@62496 ` 136` nipkow@62496 ` 137` ```lemma sngl_if_invar: "invar (Node n l a r) \ n = lvl r \ sngl r" ``` nipkow@62496 ` 138` ```by(cases r rule: sngl.cases) clarsimp+ ``` nipkow@62496 ` 139` nipkow@62496 ` 140` nipkow@62496 ` 141` ```subsection "Invariance" ``` nipkow@62496 ` 142` nipkow@62496 ` 143` ```subsubsection "Proofs for insert" ``` nipkow@62496 ` 144` nipkow@62496 ` 145` ```lemma lvl_insert_aux: ``` nipkow@62496 ` 146` ``` "lvl (insert x t) = lvl t \ lvl (insert x t) = lvl t + 1 \ sngl (insert x t)" ``` nipkow@62496 ` 147` ```apply(induction t) ``` nipkow@62496 ` 148` ```apply (auto simp: lvl_skew) ``` nipkow@62496 ` 149` ```apply (metis Suc_eq_plus1 lvl.simps(2) lvl_split lvl_skew)+ ``` nipkow@62496 ` 150` ```done ``` nipkow@62496 ` 151` nipkow@62496 ` 152` ```lemma lvl_insert: obtains ``` nipkow@62496 ` 153` ``` (Same) "lvl (insert x t) = lvl t" | ``` nipkow@62496 ` 154` ``` (Incr) "lvl (insert x t) = lvl t + 1" "sngl (insert x t)" ``` nipkow@62496 ` 155` ```using lvl_insert_aux by blast ``` nipkow@62496 ` 156` nipkow@62496 ` 157` ```lemma lvl_insert_sngl: "invar t \ sngl t \ lvl(insert x t) = lvl t" ``` nipkow@62526 ` 158` ```proof (induction t rule: insert.induct) ``` nipkow@62496 ` 159` ``` case (2 x lv t1 a t2) ``` nipkow@62496 ` 160` ``` consider (LT) "x < a" | (GT) "x > a" | (EQ) "x = a" ``` nipkow@62496 ` 161` ``` using less_linear by blast ``` nipkow@62496 ` 162` ``` thus ?case proof cases ``` nipkow@62496 ` 163` ``` case LT ``` nipkow@62496 ` 164` ``` thus ?thesis using 2 by (auto simp add: skew_case split_case split: tree.splits) ``` nipkow@62496 ` 165` ``` next ``` nipkow@62496 ` 166` ``` case GT ``` nipkow@62496 ` 167` ``` thus ?thesis using 2 proof (cases t1) ``` nipkow@62496 ` 168` ``` case Node ``` nipkow@62496 ` 169` ``` thus ?thesis using 2 GT ``` nipkow@62496 ` 170` ``` apply (auto simp add: skew_case split_case split: tree.splits) ``` nipkow@62496 ` 171` ``` by (metis less_not_refl2 lvl.simps(2) lvl_insert_aux n_not_Suc_n sngl.simps(3))+ ``` nipkow@62496 ` 172` ``` qed (auto simp add: lvl_0_iff) ``` nipkow@62496 ` 173` ``` qed simp ``` nipkow@62496 ` 174` ```qed simp ``` nipkow@62496 ` 175` nipkow@62496 ` 176` ```lemma skew_invar: "invar t \ skew t = t" ``` nipkow@62526 ` 177` ```by(cases t rule: skew.cases) auto ``` nipkow@62496 ` 178` nipkow@62496 ` 179` ```lemma split_invar: "invar t \ split t = t" ``` nipkow@62526 ` 180` ```by(cases t rule: split.cases) clarsimp+ ``` nipkow@62496 ` 181` nipkow@62496 ` 182` ```lemma invar_NodeL: ``` nipkow@62496 ` 183` ``` "\ invar(Node n l x r); invar l'; lvl l' = lvl l \ \ invar(Node n l' x r)" ``` nipkow@62496 ` 184` ```by(auto) ``` nipkow@62496 ` 185` nipkow@62496 ` 186` ```lemma invar_NodeR: ``` nipkow@62496 ` 187` ``` "\ invar(Node n l x r); n = lvl r + 1; invar r'; lvl r' = lvl r \ \ invar(Node n l x r')" ``` nipkow@62496 ` 188` ```by(auto) ``` nipkow@62496 ` 189` nipkow@62496 ` 190` ```lemma invar_NodeR2: ``` nipkow@62496 ` 191` ``` "\ invar(Node n l x r); sngl r'; n = lvl r + 1; invar r'; lvl r' = n \ \ invar(Node n l x r')" ``` nipkow@62496 ` 192` ```by(cases r' rule: sngl.cases) clarsimp+ ``` nipkow@62496 ` 193` nipkow@62496 ` 194` nipkow@62496 ` 195` ```lemma lvl_insert_incr_iff: "(lvl(insert a t) = lvl t + 1) \ ``` wenzelm@67613 ` 196` ``` (\l x r. insert a t = Node (lvl t + 1) l x r \ lvl l = lvl r)" ``` nipkow@62496 ` 197` ```apply(cases t) ``` nipkow@62496 ` 198` ```apply(auto simp add: skew_case split_case split: if_splits) ``` nipkow@62496 ` 199` ```apply(auto split: tree.splits if_splits) ``` nipkow@62496 ` 200` ```done ``` nipkow@62496 ` 201` nipkow@62496 ` 202` ```lemma invar_insert: "invar t \ invar(insert a t)" ``` nipkow@62496 ` 203` ```proof(induction t) ``` nipkow@67040 ` 204` ``` case N: (Node n l x r) ``` nipkow@62496 ` 205` ``` hence il: "invar l" and ir: "invar r" by auto ``` nipkow@67040 ` 206` ``` note iil = N.IH(1)[OF il] ``` nipkow@67040 ` 207` ``` note iir = N.IH(2)[OF ir] ``` nipkow@62496 ` 208` ``` let ?t = "Node n l x r" ``` nipkow@62496 ` 209` ``` have "a < x \ a = x \ x < a" by auto ``` nipkow@62496 ` 210` ``` moreover ``` nipkow@67040 ` 211` ``` have ?case if "a < x" ``` nipkow@67040 ` 212` ``` proof (cases rule: lvl_insert[of a l]) ``` nipkow@67040 ` 213` ``` case (Same) thus ?thesis ``` nipkow@67040 ` 214` ``` using \a invar_NodeL[OF N.prems iil Same] ``` nipkow@67040 ` 215` ``` by (simp add: skew_invar split_invar del: invar.simps) ``` nipkow@67040 ` 216` ``` next ``` nipkow@67040 ` 217` ``` case (Incr) ``` nipkow@67040 ` 218` ``` then obtain t1 w t2 where ial[simp]: "insert a l = Node n t1 w t2" ``` nipkow@67040 ` 219` ``` using N.prems by (auto simp: lvl_Suc_iff) ``` nipkow@67040 ` 220` ``` have l12: "lvl t1 = lvl t2" ``` nipkow@67040 ` 221` ``` by (metis Incr(1) ial lvl_insert_incr_iff tree.inject) ``` nipkow@67040 ` 222` ``` have "insert a ?t = split(skew(Node n (insert a l) x r))" ``` nipkow@67040 ` 223` ``` by(simp add: \a) ``` nipkow@67040 ` 224` ``` also have "skew(Node n (insert a l) x r) = Node n t1 w (Node n t2 x r)" ``` nipkow@67040 ` 225` ``` by(simp) ``` nipkow@67040 ` 226` ``` also have "invar(split \)" ``` nipkow@67040 ` 227` ``` proof (cases r) ``` nipkow@67040 ` 228` ``` case Leaf ``` nipkow@67040 ` 229` ``` hence "l = Leaf" using N.prems by(auto simp: lvl_0_iff) ``` nipkow@67040 ` 230` ``` thus ?thesis using Leaf ial by simp ``` nipkow@62496 ` 231` ``` next ``` nipkow@67040 ` 232` ``` case [simp]: (Node m t3 y t4) ``` nipkow@67040 ` 233` ``` show ?thesis (*using N(3) iil l12 by(auto)*) ``` nipkow@67040 ` 234` ``` proof cases ``` nipkow@67040 ` 235` ``` assume "m = n" thus ?thesis using N(3) iil by(auto) ``` nipkow@62496 ` 236` ``` next ``` nipkow@67040 ` 237` ``` assume "m \ n" thus ?thesis using N(3) iil l12 by(auto) ``` nipkow@62496 ` 238` ``` qed ``` nipkow@62496 ` 239` ``` qed ``` nipkow@67040 ` 240` ``` finally show ?thesis . ``` nipkow@67040 ` 241` ``` qed ``` nipkow@62496 ` 242` ``` moreover ``` nipkow@67040 ` 243` ``` have ?case if "x < a" ``` nipkow@67040 ` 244` ``` proof - ``` nipkow@62496 ` 245` ``` from \invar ?t\ have "n = lvl r \ n = lvl r + 1" by auto ``` nipkow@67040 ` 246` ``` thus ?case ``` nipkow@62496 ` 247` ``` proof ``` nipkow@62496 ` 248` ``` assume 0: "n = lvl r" ``` nipkow@62496 ` 249` ``` have "insert a ?t = split(skew(Node n l x (insert a r)))" ``` nipkow@62496 ` 250` ``` using \a>x\ by(auto) ``` nipkow@62496 ` 251` ``` also have "skew(Node n l x (insert a r)) = Node n l x (insert a r)" ``` nipkow@67040 ` 252` ``` using N.prems by(simp add: skew_case split: tree.split) ``` nipkow@62496 ` 253` ``` also have "invar(split \)" ``` nipkow@62496 ` 254` ``` proof - ``` nipkow@62496 ` 255` ``` from lvl_insert_sngl[OF ir sngl_if_invar[OF \invar ?t\ 0], of a] ``` nipkow@62496 ` 256` ``` obtain t1 y t2 where iar: "insert a r = Node n t1 y t2" ``` nipkow@67040 ` 257` ``` using N.prems 0 by (auto simp: lvl_Suc_iff) ``` nipkow@67040 ` 258` ``` from N.prems iar 0 iir ``` nipkow@62496 ` 259` ``` show ?thesis by (auto simp: split_case split: tree.splits) ``` nipkow@62496 ` 260` ``` qed ``` nipkow@62496 ` 261` ``` finally show ?thesis . ``` nipkow@62496 ` 262` ``` next ``` nipkow@62496 ` 263` ``` assume 1: "n = lvl r + 1" ``` nipkow@62496 ` 264` ``` hence "sngl ?t" by(cases r) auto ``` nipkow@62496 ` 265` ``` show ?thesis ``` nipkow@62496 ` 266` ``` proof (cases rule: lvl_insert[of a r]) ``` nipkow@62496 ` 267` ``` case (Same) ``` nipkow@67040 ` 268` ``` show ?thesis using \x il ir invar_NodeR[OF N.prems 1 iir Same] ``` nipkow@62496 ` 269` ``` by (auto simp add: skew_invar split_invar) ``` nipkow@62496 ` 270` ``` next ``` nipkow@62496 ` 271` ``` case (Incr) ``` wenzelm@67406 ` 272` ``` thus ?thesis using invar_NodeR2[OF \invar ?t\ Incr(2) 1 iir] 1 \x < a\ ``` nipkow@62496 ` 273` ``` by (auto simp add: skew_invar split_invar split: if_splits) ``` nipkow@62496 ` 274` ``` qed ``` nipkow@62496 ` 275` ``` qed ``` nipkow@67040 ` 276` ``` qed ``` nipkow@67040 ` 277` ``` moreover ``` nipkow@67040 ` 278` ``` have "a = x \ ?case" using N.prems by auto ``` nipkow@62496 ` 279` ``` ultimately show ?case by blast ``` nipkow@62496 ` 280` ```qed simp ``` nipkow@62496 ` 281` nipkow@62496 ` 282` nipkow@62496 ` 283` ```subsubsection "Proofs for delete" ``` nipkow@62496 ` 284` nipkow@62496 ` 285` ```lemma invarL: "ASSUMPTION(invar \lv, l, a, r\) \ invar l" ``` nipkow@62496 ` 286` ```by(simp add: ASSUMPTION_def) ``` nipkow@62496 ` 287` nipkow@62496 ` 288` ```lemma invarR: "ASSUMPTION(invar \lv, l, a, r\) \ invar r" ``` nipkow@62496 ` 289` ```by(simp add: ASSUMPTION_def) ``` nipkow@62496 ` 290` nipkow@62496 ` 291` ```lemma sngl_NodeI: ``` nipkow@62496 ` 292` ``` "sngl (Node lv l a r) \ sngl (Node lv l' a' r)" ``` nipkow@62496 ` 293` ```by(cases r) (simp_all) ``` nipkow@62496 ` 294` nipkow@62496 ` 295` nipkow@62496 ` 296` ```declare invarL[simp] invarR[simp] ``` nipkow@62496 ` 297` nipkow@62496 ` 298` ```lemma pre_cases: ``` nipkow@62496 ` 299` ```assumes "pre_adjust (Node lv l x r)" ``` nipkow@62496 ` 300` ```obtains ``` nipkow@62496 ` 301` ``` (tSngl) "invar l \ invar r \ ``` nipkow@62496 ` 302` ``` lv = Suc (lvl r) \ lvl l = lvl r" | ``` nipkow@62496 ` 303` ``` (tDouble) "invar l \ invar r \ ``` nipkow@62496 ` 304` ``` lv = lvl r \ Suc (lvl l) = lvl r \ sngl r " | ``` nipkow@62496 ` 305` ``` (rDown) "invar l \ invar r \ ``` nipkow@62496 ` 306` ``` lv = Suc (Suc (lvl r)) \ lv = Suc (lvl l)" | ``` nipkow@62496 ` 307` ``` (lDown_tSngl) "invar l \ invar r \ ``` nipkow@62496 ` 308` ``` lv = Suc (lvl r) \ lv = Suc (Suc (lvl l))" | ``` nipkow@62496 ` 309` ``` (lDown_tDouble) "invar l \ invar r \ ``` nipkow@62496 ` 310` ``` lv = lvl r \ lv = Suc (Suc (lvl l)) \ sngl r" ``` nipkow@62496 ` 311` ```using assms unfolding pre_adjust.simps ``` nipkow@62496 ` 312` ```by auto ``` nipkow@62496 ` 313` nipkow@62496 ` 314` ```declare invar.simps(2)[simp del] invar_2Nodes[simp add] ``` nipkow@62496 ` 315` nipkow@62496 ` 316` ```lemma invar_adjust: ``` nipkow@62496 ` 317` ``` assumes pre: "pre_adjust (Node lv l a r)" ``` nipkow@62496 ` 318` ``` shows "invar(adjust (Node lv l a r))" ``` nipkow@62496 ` 319` ```using pre proof (cases rule: pre_cases) ``` nipkow@62496 ` 320` ``` case (tDouble) thus ?thesis unfolding adjust_def by (cases r) (auto simp: invar.simps(2)) ``` nipkow@62496 ` 321` ```next ``` nipkow@62496 ` 322` ``` case (rDown) ``` nipkow@62496 ` 323` ``` from rDown obtain llv ll la lr where l: "l = Node llv ll la lr" by (cases l) auto ``` nipkow@62496 ` 324` ``` from rDown show ?thesis unfolding adjust_def by (auto simp: l invar.simps(2) split: tree.splits) ``` nipkow@62496 ` 325` ```next ``` nipkow@62496 ` 326` ``` case (lDown_tDouble) ``` nipkow@62496 ` 327` ``` from lDown_tDouble obtain rlv rr ra rl where r: "r = Node rlv rl ra rr" by (cases r) auto ``` nipkow@62496 ` 328` ``` from lDown_tDouble and r obtain rrlv rrr rra rrl where ``` nipkow@62496 ` 329` ``` rr :"rr = Node rrlv rrr rra rrl" by (cases rr) auto ``` nipkow@62496 ` 330` ``` from lDown_tDouble show ?thesis unfolding adjust_def r rr ``` nipkow@63636 ` 331` ``` apply (cases rl) apply (auto simp add: invar.simps(2) split!: if_split) ``` nipkow@62496 ` 332` ``` using lDown_tDouble by (auto simp: split_case lvl_0_iff elim:lvl.elims split: tree.split) ``` nipkow@62496 ` 333` ```qed (auto simp: split_case invar.simps(2) adjust_def split: tree.splits) ``` nipkow@62496 ` 334` nipkow@62496 ` 335` ```lemma lvl_adjust: ``` nipkow@62496 ` 336` ``` assumes "pre_adjust (Node lv l a r)" ``` nipkow@62496 ` 337` ``` shows "lv = lvl (adjust(Node lv l a r)) \ lv = lvl (adjust(Node lv l a r)) + 1" ``` nipkow@62496 ` 338` ```using assms(1) proof(cases rule: pre_cases) ``` nipkow@62496 ` 339` ``` case lDown_tSngl thus ?thesis ``` nipkow@62496 ` 340` ``` using lvl_split[of "\lvl r, l, a, r\"] by (auto simp: adjust_def) ``` nipkow@62496 ` 341` ```next ``` nipkow@62496 ` 342` ``` case lDown_tDouble thus ?thesis ``` nipkow@62496 ` 343` ``` by (auto simp: adjust_def invar.simps(2) split: tree.split) ``` nipkow@62496 ` 344` ```qed (auto simp: adjust_def split: tree.splits) ``` nipkow@62496 ` 345` nipkow@62496 ` 346` ```lemma sngl_adjust: assumes "pre_adjust (Node lv l a r)" ``` nipkow@62496 ` 347` ``` "sngl \lv, l, a, r\" "lv = lvl (adjust \lv, l, a, r\)" ``` nipkow@62496 ` 348` ``` shows "sngl (adjust \lv, l, a, r\)" ``` nipkow@62496 ` 349` ```using assms proof (cases rule: pre_cases) ``` nipkow@62496 ` 350` ``` case rDown ``` nipkow@62496 ` 351` ``` thus ?thesis using assms(2,3) unfolding adjust_def ``` nipkow@62496 ` 352` ``` by (auto simp add: skew_case) (auto split: tree.split) ``` nipkow@62496 ` 353` ```qed (auto simp: adjust_def skew_case split_case split: tree.split) ``` nipkow@62496 ` 354` nipkow@62496 ` 355` ```definition "post_del t t' == ``` nipkow@62496 ` 356` ``` invar t' \ ``` nipkow@62496 ` 357` ``` (lvl t' = lvl t \ lvl t' + 1 = lvl t) \ ``` nipkow@62496 ` 358` ``` (lvl t' = lvl t \ sngl t \ sngl t')" ``` nipkow@62496 ` 359` nipkow@62496 ` 360` ```lemma pre_adj_if_postR: ``` nipkow@62496 ` 361` ``` "invar\lv, l, a, r\ \ post_del r r' \ pre_adjust \lv, l, a, r'\" ``` nipkow@62496 ` 362` ```by(cases "sngl r") ``` nipkow@62496 ` 363` ``` (auto simp: pre_adjust.simps post_del_def invar.simps(2) elim: sngl.elims) ``` nipkow@62496 ` 364` nipkow@62496 ` 365` ```lemma pre_adj_if_postL: ``` nipkow@62496 ` 366` ``` "invar\lv, l, a, r\ \ post_del l l' \ pre_adjust \lv, l', b, r\" ``` nipkow@62496 ` 367` ```by(cases "sngl r") ``` nipkow@62496 ` 368` ``` (auto simp: pre_adjust.simps post_del_def invar.simps(2) elim: sngl.elims) ``` nipkow@62496 ` 369` nipkow@62496 ` 370` ```lemma post_del_adjL: ``` nipkow@62496 ` 371` ``` "\ invar\lv, l, a, r\; pre_adjust \lv, l', b, r\ \ ``` nipkow@62496 ` 372` ``` \ post_del \lv, l, a, r\ (adjust \lv, l', b, r\)" ``` nipkow@62496 ` 373` ```unfolding post_del_def ``` nipkow@62496 ` 374` ```by (metis invar_adjust lvl_adjust sngl_NodeI sngl_adjust lvl.simps(2)) ``` nipkow@62496 ` 375` nipkow@62496 ` 376` ```lemma post_del_adjR: ``` nipkow@62496 ` 377` ```assumes "invar\lv, l, a, r\" "pre_adjust \lv, l, a, r'\" "post_del r r'" ``` nipkow@62496 ` 378` ```shows "post_del \lv, l, a, r\ (adjust \lv, l, a, r'\)" ``` nipkow@62496 ` 379` ```proof(unfold post_del_def, safe del: disjCI) ``` nipkow@62496 ` 380` ``` let ?t = "\lv, l, a, r\" ``` nipkow@62496 ` 381` ``` let ?t' = "adjust \lv, l, a, r'\" ``` nipkow@62496 ` 382` ``` show "invar ?t'" by(rule invar_adjust[OF assms(2)]) ``` nipkow@62496 ` 383` ``` show "lvl ?t' = lvl ?t \ lvl ?t' + 1 = lvl ?t" ``` nipkow@62496 ` 384` ``` using lvl_adjust[OF assms(2)] by auto ``` nipkow@62496 ` 385` ``` show "sngl ?t'" if as: "lvl ?t' = lvl ?t" "sngl ?t" ``` nipkow@62496 ` 386` ``` proof - ``` nipkow@62496 ` 387` ``` have s: "sngl \lv, l, a, r'\" ``` nipkow@62496 ` 388` ``` proof(cases r') ``` nipkow@62496 ` 389` ``` case Leaf thus ?thesis by simp ``` nipkow@62496 ` 390` ``` next ``` nipkow@62496 ` 391` ``` case Node thus ?thesis using as(2) assms(1,3) ``` nipkow@62496 ` 392` ``` by (cases r) (auto simp: post_del_def) ``` nipkow@62496 ` 393` ``` qed ``` nipkow@62496 ` 394` ``` show ?thesis using as(1) sngl_adjust[OF assms(2) s] by simp ``` nipkow@62496 ` 395` ``` qed ``` nipkow@62496 ` 396` ```qed ``` nipkow@62496 ` 397` nipkow@62496 ` 398` ```declare prod.splits[split] ``` nipkow@62496 ` 399` nipkow@68023 ` 400` ```theorem post_split_max: ``` nipkow@68023 ` 401` ``` "\ invar t; (t', x) = split_max t; t \ Leaf \ \ post_del t t'" ``` nipkow@68023 ` 402` ```proof (induction t arbitrary: t' rule: split_max.induct) ``` nipkow@62496 ` 403` ``` case (2 lv l a lvr rl ra rr) ``` nipkow@62496 ` 404` ``` let ?r = "\lvr, rl, ra, rr\" ``` nipkow@62496 ` 405` ``` let ?t = "\lv, l, a, ?r\" ``` nipkow@68023 ` 406` ``` from "2.prems"(2) obtain r' where r': "(r', x) = split_max ?r" ``` nipkow@62496 ` 407` ``` and [simp]: "t' = adjust \lv, l, a, r'\" by auto ``` nipkow@62496 ` 408` ``` from "2.IH"[OF _ r'] \invar ?t\ have post: "post_del ?r r'" by simp ``` nipkow@62496 ` 409` ``` note preR = pre_adj_if_postR[OF \invar ?t\ post] ``` nipkow@62496 ` 410` ``` show ?case by (simp add: post_del_adjR[OF "2.prems"(1) preR post]) ``` nipkow@62496 ` 411` ```qed (auto simp: post_del_def) ``` nipkow@62496 ` 412` nipkow@62496 ` 413` ```theorem post_delete: "invar t \ post_del t (delete x t)" ``` nipkow@62496 ` 414` ```proof (induction t) ``` nipkow@62496 ` 415` ``` case (Node lv l a r) ``` nipkow@62496 ` 416` nipkow@62496 ` 417` ``` let ?l' = "delete x l" and ?r' = "delete x r" ``` nipkow@62496 ` 418` ``` let ?t = "Node lv l a r" let ?t' = "delete x ?t" ``` nipkow@62496 ` 419` nipkow@62496 ` 420` ``` from Node.prems have inv_l: "invar l" and inv_r: "invar r" by (auto) ``` nipkow@62496 ` 421` nipkow@62496 ` 422` ``` note post_l' = Node.IH(1)[OF inv_l] ``` nipkow@62496 ` 423` ``` note preL = pre_adj_if_postL[OF Node.prems post_l'] ``` nipkow@62496 ` 424` nipkow@62496 ` 425` ``` note post_r' = Node.IH(2)[OF inv_r] ``` nipkow@62496 ` 426` ``` note preR = pre_adj_if_postR[OF Node.prems post_r'] ``` nipkow@62496 ` 427` nipkow@62496 ` 428` ``` show ?case ``` nipkow@62496 ` 429` ``` proof (cases rule: linorder_cases[of x a]) ``` nipkow@62496 ` 430` ``` case less ``` nipkow@62496 ` 431` ``` thus ?thesis using Node.prems by (simp add: post_del_adjL preL) ``` nipkow@62496 ` 432` ``` next ``` nipkow@62496 ` 433` ``` case greater ``` nipkow@62496 ` 434` ``` thus ?thesis using Node.prems by (simp add: post_del_adjR preR post_r') ``` nipkow@62496 ` 435` ``` next ``` nipkow@62496 ` 436` ``` case equal ``` nipkow@62496 ` 437` ``` show ?thesis ``` nipkow@62496 ` 438` ``` proof cases ``` nipkow@62496 ` 439` ``` assume "l = Leaf" thus ?thesis using equal Node.prems ``` nipkow@62496 ` 440` ``` by(auto simp: post_del_def invar.simps(2)) ``` nipkow@62496 ` 441` ``` next ``` nipkow@62496 ` 442` ``` assume "l \ Leaf" thus ?thesis using equal ``` nipkow@68023 ` 443` ``` by simp (metis Node.prems inv_l post_del_adjL post_split_max pre_adj_if_postL) ``` nipkow@62496 ` 444` ``` qed ``` nipkow@62496 ` 445` ``` qed ``` nipkow@62496 ` 446` ```qed (simp add: post_del_def) ``` nipkow@62496 ` 447` nipkow@62496 ` 448` ```declare invar_2Nodes[simp del] ``` nipkow@62496 ` 449` nipkow@61793 ` 450` nipkow@61793 ` 451` ```subsection "Functional Correctness" ``` nipkow@61793 ` 452` nipkow@62496 ` 453` nipkow@61793 ` 454` ```subsubsection "Proofs for insert" ``` nipkow@61793 ` 455` nipkow@61793 ` 456` ```lemma inorder_split: "inorder(split t) = inorder t" ``` nipkow@61793 ` 457` ```by(cases t rule: split.cases) (auto) ``` nipkow@61793 ` 458` nipkow@61793 ` 459` ```lemma inorder_skew: "inorder(skew t) = inorder t" ``` nipkow@61793 ` 460` ```by(cases t rule: skew.cases) (auto) ``` nipkow@61793 ` 461` nipkow@61793 ` 462` ```lemma inorder_insert: ``` nipkow@61793 ` 463` ``` "sorted(inorder t) \ inorder(insert x t) = ins_list x (inorder t)" ``` nipkow@61793 ` 464` ```by(induction t) (auto simp: ins_list_simps inorder_split inorder_skew) ``` nipkow@61793 ` 465` nipkow@62496 ` 466` nipkow@61793 ` 467` ```subsubsection "Proofs for delete" ``` nipkow@61793 ` 468` nipkow@62496 ` 469` ```lemma inorder_adjust: "t \ Leaf \ pre_adjust t \ inorder(adjust t) = inorder t" ``` nipkow@62526 ` 470` ```by(cases t) ``` nipkow@62496 ` 471` ``` (auto simp: adjust_def inorder_skew inorder_split invar.simps(2) pre_adjust.simps ``` nipkow@62496 ` 472` ``` split: tree.splits) ``` nipkow@62496 ` 473` nipkow@68023 ` 474` ```lemma split_maxD: ``` nipkow@68023 ` 475` ``` "\ split_max t = (t',x); t \ Leaf; invar t \ \ inorder t' @ [x] = inorder t" ``` nipkow@68023 ` 476` ```by(induction t arbitrary: t' rule: split_max.induct) ``` nipkow@68023 ` 477` ``` (auto simp: sorted_lems inorder_adjust pre_adj_if_postR post_split_max split: prod.splits) ``` nipkow@61793 ` 478` nipkow@61793 ` 479` ```lemma inorder_delete: ``` nipkow@62496 ` 480` ``` "invar t \ sorted(inorder t) \ inorder(delete x t) = del_list x (inorder t)" ``` nipkow@61793 ` 481` ```by(induction t) ``` nipkow@62496 ` 482` ``` (auto simp: del_list_simps inorder_adjust pre_adj_if_postL pre_adj_if_postR ``` nipkow@68023 ` 483` ``` post_split_max post_delete split_maxD split: prod.splits) ``` nipkow@61793 ` 484` nipkow@62496 ` 485` ```interpretation I: Set_by_Ordered ``` nipkow@61793 ` 486` ```where empty = Leaf and isin = isin and insert = insert and delete = delete ``` nipkow@62496 ` 487` ```and inorder = inorder and inv = invar ``` nipkow@61793 ` 488` ```proof (standard, goal_cases) ``` nipkow@61793 ` 489` ``` case 1 show ?case by simp ``` nipkow@61793 ` 490` ```next ``` nipkow@67967 ` 491` ``` case 2 thus ?case by(simp add: isin_set_inorder) ``` nipkow@61793 ` 492` ```next ``` nipkow@61793 ` 493` ``` case 3 thus ?case by(simp add: inorder_insert) ``` nipkow@61793 ` 494` ```next ``` nipkow@61793 ` 495` ``` case 4 thus ?case by(simp add: inorder_delete) ``` nipkow@62496 ` 496` ```next ``` nipkow@62496 ` 497` ``` case 5 thus ?case by(simp) ``` nipkow@62496 ` 498` ```next ``` nipkow@62496 ` 499` ``` case 6 thus ?case by(simp add: invar_insert) ``` nipkow@62496 ` 500` ```next ``` nipkow@62496 ` 501` ``` case 7 thus ?case using post_delete by(auto simp: post_del_def) ``` nipkow@62496 ` 502` ```qed ``` nipkow@61793 ` 503` nipkow@62390 ` 504` ```end ```