src/HOL/Data_Structures/Tree23_Set.thy
changeset 72805 976d656ed31e
parent 72566 831f17da1aab
child 78199 d6e6618db929
equal deleted inserted replaced
72804:d7393c35aa5d 72805:976d656ed31e
    43    (case cmp x a of
    43    (case cmp x a of
    44       LT \<Rightarrow>
    44       LT \<Rightarrow>
    45         (case ins x l of
    45         (case ins x l of
    46            TI l' => TI (Node2 l' a r) |
    46            TI l' => TI (Node2 l' a r) |
    47            OF l1 b l2 => TI (Node3 l1 b l2 a r)) |
    47            OF l1 b l2 => TI (Node3 l1 b l2 a r)) |
    48       EQ \<Rightarrow> TI (Node2 l x r) |
    48       EQ \<Rightarrow> TI (Node2 l a r) |
    49       GT \<Rightarrow>
    49       GT \<Rightarrow>
    50         (case ins x r of
    50         (case ins x r of
    51            TI r' => TI (Node2 l a r') |
    51            TI r' => TI (Node2 l a r') |
    52            OF r1 b r2 => TI (Node3 l a r1 b r2)))" |
    52            OF r1 b r2 => TI (Node3 l a r1 b r2)))" |
    53 "ins x (Node3 l a m b r) =
    53 "ins x (Node3 l a m b r) =
   208 
   208 
   209 subsubsection "Proofs for insert"
   209 subsubsection "Proofs for insert"
   210 
   210 
   211 text\<open>First a standard proof that \<^const>\<open>ins\<close> preserves \<^const>\<open>complete\<close>.\<close>
   211 text\<open>First a standard proof that \<^const>\<open>ins\<close> preserves \<^const>\<open>complete\<close>.\<close>
   212 
   212 
   213 instantiation upI :: (type)height
   213 fun hI :: "'a upI \<Rightarrow> nat" where
   214 begin
   214 "hI (TI t) = height t" |
   215 
   215 "hI (OF l a r) = height l"
   216 fun height_upI :: "'a upI \<Rightarrow> nat" where
   216 
   217 "height (TI t) = height t" |
   217 lemma complete_ins: "complete t \<Longrightarrow> complete (treeI(ins a t)) \<and> hI(ins a t) = height t"
   218 "height (OF l a r) = height l"
       
   219 
       
   220 instance ..
       
   221 
       
   222 end
       
   223 
       
   224 lemma complete_ins: "complete t \<Longrightarrow> complete (treeI(ins a t)) \<and> height(ins a t) = height t"
       
   225 by (induct t) (auto split!: if_split upI.split) (* 15 secs in 2015 *)
   218 by (induct t) (auto split!: if_split upI.split) (* 15 secs in 2015 *)
   226 
   219 
   227 text\<open>Now an alternative proof (by Brian Huffman) that runs faster because
   220 text\<open>Now an alternative proof (by Brian Huffman) that runs faster because
   228 two properties (completeness and height) are combined in one predicate.\<close>
   221 two properties (completeness and height) are combined in one predicate.\<close>
   229 
   222 
   288 done
   281 done
   289 
   282 
   290 
   283 
   291 subsection "Proofs for delete"
   284 subsection "Proofs for delete"
   292 
   285 
   293 instantiation upD :: (type)height
   286 fun hD :: "'a upD \<Rightarrow> nat" where
   294 begin
   287 "hD (TD t) = height t" |
   295 
   288 "hD (UF t) = height t + 1"
   296 fun height_upD :: "'a upD \<Rightarrow> nat" where
       
   297 "height (TD t) = height t" |
       
   298 "height (UF t) = height t + 1"
       
   299 
       
   300 instance ..
       
   301 
       
   302 end
       
   303 
   289 
   304 lemma complete_treeD_node21:
   290 lemma complete_treeD_node21:
   305   "\<lbrakk>complete r; complete (treeD l'); height r = height l' \<rbrakk> \<Longrightarrow> complete (treeD (node21 l' a r))"
   291   "\<lbrakk>complete r; complete (treeD l'); height r = hD l' \<rbrakk> \<Longrightarrow> complete (treeD (node21 l' a r))"
   306 by(induct l' a r rule: node21.induct) auto
   292 by(induct l' a r rule: node21.induct) auto
   307 
   293 
   308 lemma complete_treeD_node22:
   294 lemma complete_treeD_node22:
   309   "\<lbrakk>complete(treeD r'); complete l; height r' = height l \<rbrakk> \<Longrightarrow> complete (treeD (node22 l a r'))"
   295   "\<lbrakk>complete(treeD r'); complete l; hD r' = height l \<rbrakk> \<Longrightarrow> complete (treeD (node22 l a r'))"
   310 by(induct l a r' rule: node22.induct) auto
   296 by(induct l a r' rule: node22.induct) auto
   311 
   297 
   312 lemma complete_treeD_node31:
   298 lemma complete_treeD_node31:
   313   "\<lbrakk> complete (treeD l'); complete m; complete r; height l' = height r; height m = height r \<rbrakk>
   299   "\<lbrakk> complete (treeD l'); complete m; complete r; hD l' = height r; height m = height r \<rbrakk>
   314   \<Longrightarrow> complete (treeD (node31 l' a m b r))"
   300   \<Longrightarrow> complete (treeD (node31 l' a m b r))"
   315 by(induct l' a m b r rule: node31.induct) auto
   301 by(induct l' a m b r rule: node31.induct) auto
   316 
   302 
   317 lemma complete_treeD_node32:
   303 lemma complete_treeD_node32:
   318   "\<lbrakk> complete l; complete (treeD m'); complete r; height l = height r; height m' = height r \<rbrakk>
   304   "\<lbrakk> complete l; complete (treeD m'); complete r; height l = height r; hD m' = height r \<rbrakk>
   319   \<Longrightarrow> complete (treeD (node32 l a m' b r))"
   305   \<Longrightarrow> complete (treeD (node32 l a m' b r))"
   320 by(induct l a m' b r rule: node32.induct) auto
   306 by(induct l a m' b r rule: node32.induct) auto
   321 
   307 
   322 lemma complete_treeD_node33:
   308 lemma complete_treeD_node33:
   323   "\<lbrakk> complete l; complete m; complete(treeD r'); height l = height r'; height m = height r' \<rbrakk>
   309   "\<lbrakk> complete l; complete m; complete(treeD r'); height l = hD r'; height m = hD r' \<rbrakk>
   324   \<Longrightarrow> complete (treeD (node33 l a m b r'))"
   310   \<Longrightarrow> complete (treeD (node33 l a m b r'))"
   325 by(induct l a m b r' rule: node33.induct) auto
   311 by(induct l a m b r' rule: node33.induct) auto
   326 
   312 
   327 lemmas completes = complete_treeD_node21 complete_treeD_node22
   313 lemmas completes = complete_treeD_node21 complete_treeD_node22
   328   complete_treeD_node31 complete_treeD_node32 complete_treeD_node33
   314   complete_treeD_node31 complete_treeD_node32 complete_treeD_node33
   329 
   315 
   330 lemma height'_node21:
   316 lemma height'_node21:
   331    "height r > 0 \<Longrightarrow> height(node21 l' a r) = max (height l') (height r) + 1"
   317    "height r > 0 \<Longrightarrow> hD(node21 l' a r) = max (hD l') (height r) + 1"
   332 by(induct l' a r rule: node21.induct)(simp_all)
   318 by(induct l' a r rule: node21.induct)(simp_all)
   333 
   319 
   334 lemma height'_node22:
   320 lemma height'_node22:
   335    "height l > 0 \<Longrightarrow> height(node22 l a r') = max (height l) (height r') + 1"
   321    "height l > 0 \<Longrightarrow> hD(node22 l a r') = max (height l) (hD r') + 1"
   336 by(induct l a r' rule: node22.induct)(simp_all)
   322 by(induct l a r' rule: node22.induct)(simp_all)
   337 
   323 
   338 lemma height'_node31:
   324 lemma height'_node31:
   339   "height m > 0 \<Longrightarrow> height(node31 l a m b r) =
   325   "height m > 0 \<Longrightarrow> hD(node31 l a m b r) =
   340    max (height l) (max (height m) (height r)) + 1"
   326    max (hD l) (max (height m) (height r)) + 1"
   341 by(induct l a m b r rule: node31.induct)(simp_all add: max_def)
   327 by(induct l a m b r rule: node31.induct)(simp_all add: max_def)
   342 
   328 
   343 lemma height'_node32:
   329 lemma height'_node32:
   344   "height r > 0 \<Longrightarrow> height(node32 l a m b r) =
   330   "height r > 0 \<Longrightarrow> hD(node32 l a m b r) =
   345    max (height l) (max (height m) (height r)) + 1"
   331    max (height l) (max (hD m) (height r)) + 1"
   346 by(induct l a m b r rule: node32.induct)(simp_all add: max_def)
   332 by(induct l a m b r rule: node32.induct)(simp_all add: max_def)
   347 
   333 
   348 lemma height'_node33:
   334 lemma height'_node33:
   349   "height m > 0 \<Longrightarrow> height(node33 l a m b r) =
   335   "height m > 0 \<Longrightarrow> hD(node33 l a m b r) =
   350    max (height l) (max (height m) (height r)) + 1"
   336    max (height l) (max (height m) (hD r)) + 1"
   351 by(induct l a m b r rule: node33.induct)(simp_all add: max_def)
   337 by(induct l a m b r rule: node33.induct)(simp_all add: max_def)
   352 
   338 
   353 lemmas heights = height'_node21 height'_node22
   339 lemmas heights = height'_node21 height'_node22
   354   height'_node31 height'_node32 height'_node33
   340   height'_node31 height'_node32 height'_node33
   355 
   341 
   356 lemma height_split_min:
   342 lemma height_split_min:
   357   "split_min t = (x, t') \<Longrightarrow> height t > 0 \<Longrightarrow> complete t \<Longrightarrow> height t' = height t"
   343   "split_min t = (x, t') \<Longrightarrow> height t > 0 \<Longrightarrow> complete t \<Longrightarrow> hD t' = height t"
   358 by(induct t arbitrary: x t' rule: split_min.induct)
   344 by(induct t arbitrary: x t' rule: split_min.induct)
   359   (auto simp: heights split: prod.splits)
   345   (auto simp: heights split: prod.splits)
   360 
   346 
   361 lemma height_del: "complete t \<Longrightarrow> height(del x t) = height t"
   347 lemma height_del: "complete t \<Longrightarrow> hD(del x t) = height t"
   362 by(induction x t rule: del.induct)
   348 by(induction x t rule: del.induct)
   363   (auto simp: heights max_def height_split_min split: prod.splits)
   349   (auto simp: heights max_def height_split_min split: prod.splits)
   364 
   350 
   365 lemma complete_split_min:
   351 lemma complete_split_min:
   366   "\<lbrakk> split_min t = (x, t'); complete t; height t > 0 \<rbrakk> \<Longrightarrow> complete (treeD t')"
   352   "\<lbrakk> split_min t = (x, t'); complete t; height t > 0 \<rbrakk> \<Longrightarrow> complete (treeD t')"