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