src/HOL/Library/RBT_Impl.thy
author Andreas Lochbihler
Fri Apr 13 11:45:30 2012 +0200 (2012-04-13)
changeset 47450 2ada2be850cb
parent 47397 d654c73e4b12
child 47455 26315a545e26
permissions -rw-r--r--
move RBT implementation into type class contexts
     1 (*  Title:      RBT_Impl.thy
     2     Author:     Markus Reiter, TU Muenchen
     3     Author:     Alexander Krauss, TU Muenchen
     4 *)
     5 
     6 header {* Implementation of Red-Black Trees *}
     7 
     8 theory RBT_Impl
     9 imports Main
    10 begin
    11 
    12 text {*
    13   For applications, you should use theory @{text RBT} which defines
    14   an abstract type of red-black tree obeying the invariant.
    15 *}
    16 
    17 subsection {* Datatype of RB trees *}
    18 
    19 datatype color = R | B
    20 datatype ('a, 'b) rbt = Empty | Branch color "('a, 'b) rbt" 'a 'b "('a, 'b) rbt"
    21 
    22 lemma rbt_cases:
    23   obtains (Empty) "t = Empty" 
    24   | (Red) l k v r where "t = Branch R l k v r" 
    25   | (Black) l k v r where "t = Branch B l k v r"
    26 proof (cases t)
    27   case Empty with that show thesis by blast
    28 next
    29   case (Branch c) with that show thesis by (cases c) blast+
    30 qed
    31 
    32 subsection {* Tree properties *}
    33 
    34 subsubsection {* Content of a tree *}
    35 
    36 primrec entries :: "('a, 'b) rbt \<Rightarrow> ('a \<times> 'b) list"
    37 where 
    38   "entries Empty = []"
    39 | "entries (Branch _ l k v r) = entries l @ (k,v) # entries r"
    40 
    41 abbreviation (input) entry_in_tree :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) rbt \<Rightarrow> bool"
    42 where
    43   "entry_in_tree k v t \<equiv> (k, v) \<in> set (entries t)"
    44 
    45 definition keys :: "('a, 'b) rbt \<Rightarrow> 'a list" where
    46   "keys t = map fst (entries t)"
    47 
    48 lemma keys_simps [simp, code]:
    49   "keys Empty = []"
    50   "keys (Branch c l k v r) = keys l @ k # keys r"
    51   by (simp_all add: keys_def)
    52 
    53 lemma entry_in_tree_keys:
    54   assumes "(k, v) \<in> set (entries t)"
    55   shows "k \<in> set (keys t)"
    56 proof -
    57   from assms have "fst (k, v) \<in> fst ` set (entries t)" by (rule imageI)
    58   then show ?thesis by (simp add: keys_def)
    59 qed
    60 
    61 lemma keys_entries:
    62   "k \<in> set (keys t) \<longleftrightarrow> (\<exists>v. (k, v) \<in> set (entries t))"
    63   by (auto intro: entry_in_tree_keys) (auto simp add: keys_def)
    64 
    65 
    66 subsubsection {* Search tree properties *}
    67 
    68 context ord begin
    69 
    70 definition rbt_less :: "'a \<Rightarrow> ('a, 'b) rbt \<Rightarrow> bool"
    71 where
    72   rbt_less_prop: "rbt_less k t \<longleftrightarrow> (\<forall>x\<in>set (keys t). x < k)"
    73 
    74 abbreviation rbt_less_symbol (infix "|\<guillemotleft>" 50)
    75 where "t |\<guillemotleft> x \<equiv> rbt_less x t"
    76 
    77 definition rbt_greater :: "'a \<Rightarrow> ('a, 'b) rbt \<Rightarrow> bool" (infix "\<guillemotleft>|" 50) 
    78 where
    79   rbt_greater_prop: "rbt_greater k t = (\<forall>x\<in>set (keys t). k < x)"
    80 
    81 lemma rbt_less_simps [simp]:
    82   "Empty |\<guillemotleft> k = True"
    83   "Branch c lt kt v rt |\<guillemotleft> k \<longleftrightarrow> kt < k \<and> lt |\<guillemotleft> k \<and> rt |\<guillemotleft> k"
    84   by (auto simp add: rbt_less_prop)
    85 
    86 lemma rbt_greater_simps [simp]:
    87   "k \<guillemotleft>| Empty = True"
    88   "k \<guillemotleft>| (Branch c lt kt v rt) \<longleftrightarrow> k < kt \<and> k \<guillemotleft>| lt \<and> k \<guillemotleft>| rt"
    89   by (auto simp add: rbt_greater_prop)
    90 
    91 lemmas rbt_ord_props = rbt_less_prop rbt_greater_prop
    92 
    93 lemmas rbt_greater_nit = rbt_greater_prop entry_in_tree_keys
    94 lemmas rbt_less_nit = rbt_less_prop entry_in_tree_keys
    95 
    96 lemma (in order)
    97   shows rbt_less_eq_trans: "l |\<guillemotleft> u \<Longrightarrow> u \<le> v \<Longrightarrow> l |\<guillemotleft> v"
    98   and rbt_less_trans: "t |\<guillemotleft> x \<Longrightarrow> x < y \<Longrightarrow> t |\<guillemotleft> y"
    99   and rbt_greater_eq_trans: "u \<le> v \<Longrightarrow> v \<guillemotleft>| r \<Longrightarrow> u \<guillemotleft>| r"
   100   and rbt_greater_trans: "x < y \<Longrightarrow> y \<guillemotleft>| t \<Longrightarrow> x \<guillemotleft>| t"
   101   by (auto simp: rbt_ord_props)
   102 
   103 primrec rbt_sorted :: "('a, 'b) rbt \<Rightarrow> bool"
   104 where
   105   "rbt_sorted Empty = True"
   106 | "rbt_sorted (Branch c l k v r) = (l |\<guillemotleft> k \<and> k \<guillemotleft>| r \<and> rbt_sorted l \<and> rbt_sorted r)"
   107 
   108 end
   109 
   110 context linorder begin
   111 
   112 lemma rbt_sorted_entries:
   113   "rbt_sorted t \<Longrightarrow> List.sorted (List.map fst (entries t))"
   114 by (induct t) 
   115   (force simp: sorted_append sorted_Cons rbt_ord_props 
   116       dest!: entry_in_tree_keys)+
   117 
   118 lemma distinct_entries:
   119   "rbt_sorted t \<Longrightarrow> distinct (List.map fst (entries t))"
   120 by (induct t) 
   121   (force simp: sorted_append sorted_Cons rbt_ord_props 
   122       dest!: entry_in_tree_keys)+
   123 
   124 subsubsection {* Tree lookup *}
   125 
   126 primrec (in ord) rbt_lookup :: "('a, 'b) rbt \<Rightarrow> 'a \<rightharpoonup> 'b"
   127 where
   128   "rbt_lookup Empty k = None"
   129 | "rbt_lookup (Branch _ l x y r) k = 
   130    (if k < x then rbt_lookup l k else if x < k then rbt_lookup r k else Some y)"
   131 
   132 lemma rbt_lookup_keys: "rbt_sorted t \<Longrightarrow> dom (rbt_lookup t) = set (keys t)"
   133   by (induct t) (auto simp: dom_def rbt_greater_prop rbt_less_prop)
   134 
   135 lemma dom_rbt_lookup_Branch: 
   136   "rbt_sorted (Branch c t1 k v t2) \<Longrightarrow> 
   137     dom (rbt_lookup (Branch c t1 k v t2)) 
   138     = Set.insert k (dom (rbt_lookup t1) \<union> dom (rbt_lookup t2))"
   139 proof -
   140   assume "rbt_sorted (Branch c t1 k v t2)"
   141   moreover from this have "rbt_sorted t1" "rbt_sorted t2" by simp_all
   142   ultimately show ?thesis by (simp add: rbt_lookup_keys)
   143 qed
   144 
   145 lemma finite_dom_rbt_lookup [simp, intro!]: "finite (dom (rbt_lookup t))"
   146 proof (induct t)
   147   case Empty then show ?case by simp
   148 next
   149   case (Branch color t1 a b t2)
   150   let ?A = "Set.insert a (dom (rbt_lookup t1) \<union> dom (rbt_lookup t2))"
   151   have "dom (rbt_lookup (Branch color t1 a b t2)) \<subseteq> ?A" by (auto split: split_if_asm)
   152   moreover from Branch have "finite (insert a (dom (rbt_lookup t1) \<union> dom (rbt_lookup t2)))" by simp
   153   ultimately show ?case by (rule finite_subset)
   154 qed 
   155 
   156 end
   157 
   158 context ord begin
   159 
   160 lemma rbt_lookup_rbt_less[simp]: "t |\<guillemotleft> k \<Longrightarrow> rbt_lookup t k = None" 
   161 by (induct t) auto
   162 
   163 lemma rbt_lookup_rbt_greater[simp]: "k \<guillemotleft>| t \<Longrightarrow> rbt_lookup t k = None"
   164 by (induct t) auto
   165 
   166 lemma rbt_lookup_Empty: "rbt_lookup Empty = empty"
   167 by (rule ext) simp
   168 
   169 end
   170 
   171 context linorder begin
   172 
   173 lemma map_of_entries:
   174   "rbt_sorted t \<Longrightarrow> map_of (entries t) = rbt_lookup t"
   175 proof (induct t)
   176   case Empty thus ?case by (simp add: rbt_lookup_Empty)
   177 next
   178   case (Branch c t1 k v t2)
   179   have "rbt_lookup (Branch c t1 k v t2) = rbt_lookup t2 ++ [k\<mapsto>v] ++ rbt_lookup t1"
   180   proof (rule ext)
   181     fix x
   182     from Branch have RBT_SORTED: "rbt_sorted (Branch c t1 k v t2)" by simp
   183     let ?thesis = "rbt_lookup (Branch c t1 k v t2) x = (rbt_lookup t2 ++ [k \<mapsto> v] ++ rbt_lookup t1) x"
   184 
   185     have DOM_T1: "!!k'. k'\<in>dom (rbt_lookup t1) \<Longrightarrow> k>k'"
   186     proof -
   187       fix k'
   188       from RBT_SORTED have "t1 |\<guillemotleft> k" by simp
   189       with rbt_less_prop have "\<forall>k'\<in>set (keys t1). k>k'" by auto
   190       moreover assume "k'\<in>dom (rbt_lookup t1)"
   191       ultimately show "k>k'" using rbt_lookup_keys RBT_SORTED by auto
   192     qed
   193     
   194     have DOM_T2: "!!k'. k'\<in>dom (rbt_lookup t2) \<Longrightarrow> k<k'"
   195     proof -
   196       fix k'
   197       from RBT_SORTED have "k \<guillemotleft>| t2" by simp
   198       with rbt_greater_prop have "\<forall>k'\<in>set (keys t2). k<k'" by auto
   199       moreover assume "k'\<in>dom (rbt_lookup t2)"
   200       ultimately show "k<k'" using rbt_lookup_keys RBT_SORTED by auto
   201     qed
   202     
   203     {
   204       assume C: "x<k"
   205       hence "rbt_lookup (Branch c t1 k v t2) x = rbt_lookup t1 x" by simp
   206       moreover from C have "x\<notin>dom [k\<mapsto>v]" by simp
   207       moreover have "x \<notin> dom (rbt_lookup t2)"
   208       proof
   209         assume "x \<in> dom (rbt_lookup t2)"
   210         with DOM_T2 have "k<x" by blast
   211         with C show False by simp
   212       qed
   213       ultimately have ?thesis by (simp add: map_add_upd_left map_add_dom_app_simps)
   214     } moreover {
   215       assume [simp]: "x=k"
   216       hence "rbt_lookup (Branch c t1 k v t2) x = [k \<mapsto> v] x" by simp
   217       moreover have "x \<notin> dom (rbt_lookup t1)" 
   218       proof
   219         assume "x \<in> dom (rbt_lookup t1)"
   220         with DOM_T1 have "k>x" by blast
   221         thus False by simp
   222       qed
   223       ultimately have ?thesis by (simp add: map_add_upd_left map_add_dom_app_simps)
   224     } moreover {
   225       assume C: "x>k"
   226       hence "rbt_lookup (Branch c t1 k v t2) x = rbt_lookup t2 x" by (simp add: less_not_sym[of k x])
   227       moreover from C have "x\<notin>dom [k\<mapsto>v]" by simp
   228       moreover have "x\<notin>dom (rbt_lookup t1)" proof
   229         assume "x\<in>dom (rbt_lookup t1)"
   230         with DOM_T1 have "k>x" by simp
   231         with C show False by simp
   232       qed
   233       ultimately have ?thesis by (simp add: map_add_upd_left map_add_dom_app_simps)
   234     } ultimately show ?thesis using less_linear by blast
   235   qed
   236   also from Branch 
   237   have "rbt_lookup t2 ++ [k \<mapsto> v] ++ rbt_lookup t1 = map_of (entries (Branch c t1 k v t2))" by simp
   238   finally show ?case by simp
   239 qed
   240 
   241 lemma rbt_lookup_in_tree: "rbt_sorted t \<Longrightarrow> rbt_lookup t k = Some v \<longleftrightarrow> (k, v) \<in> set (entries t)"
   242   by (simp add: map_of_entries [symmetric] distinct_entries)
   243 
   244 lemma set_entries_inject:
   245   assumes rbt_sorted: "rbt_sorted t1" "rbt_sorted t2" 
   246   shows "set (entries t1) = set (entries t2) \<longleftrightarrow> entries t1 = entries t2"
   247 proof -
   248   from rbt_sorted have "distinct (map fst (entries t1))"
   249     "distinct (map fst (entries t2))"
   250     by (auto intro: distinct_entries)
   251   with rbt_sorted show ?thesis
   252     by (auto intro: map_sorted_distinct_set_unique rbt_sorted_entries simp add: distinct_map)
   253 qed
   254 
   255 lemma entries_eqI:
   256   assumes rbt_sorted: "rbt_sorted t1" "rbt_sorted t2" 
   257   assumes rbt_lookup: "rbt_lookup t1 = rbt_lookup t2"
   258   shows "entries t1 = entries t2"
   259 proof -
   260   from rbt_sorted rbt_lookup have "map_of (entries t1) = map_of (entries t2)"
   261     by (simp add: map_of_entries)
   262   with rbt_sorted have "set (entries t1) = set (entries t2)"
   263     by (simp add: map_of_inject_set distinct_entries)
   264   with rbt_sorted show ?thesis by (simp add: set_entries_inject)
   265 qed
   266 
   267 lemma entries_rbt_lookup:
   268   assumes "rbt_sorted t1" "rbt_sorted t2" 
   269   shows "entries t1 = entries t2 \<longleftrightarrow> rbt_lookup t1 = rbt_lookup t2"
   270   using assms by (auto intro: entries_eqI simp add: map_of_entries [symmetric])
   271 
   272 lemma rbt_lookup_from_in_tree: 
   273   assumes "rbt_sorted t1" "rbt_sorted t2" 
   274   and "\<And>v. (k, v) \<in> set (entries t1) \<longleftrightarrow> (k, v) \<in> set (entries t2)" 
   275   shows "rbt_lookup t1 k = rbt_lookup t2 k"
   276 proof -
   277   from assms have "k \<in> dom (rbt_lookup t1) \<longleftrightarrow> k \<in> dom (rbt_lookup t2)"
   278     by (simp add: keys_entries rbt_lookup_keys)
   279   with assms show ?thesis by (auto simp add: rbt_lookup_in_tree [symmetric])
   280 qed
   281 
   282 end
   283 
   284 subsubsection {* Red-black properties *}
   285 
   286 primrec color_of :: "('a, 'b) rbt \<Rightarrow> color"
   287 where
   288   "color_of Empty = B"
   289 | "color_of (Branch c _ _ _ _) = c"
   290 
   291 primrec bheight :: "('a,'b) rbt \<Rightarrow> nat"
   292 where
   293   "bheight Empty = 0"
   294 | "bheight (Branch c lt k v rt) = (if c = B then Suc (bheight lt) else bheight lt)"
   295 
   296 primrec inv1 :: "('a, 'b) rbt \<Rightarrow> bool"
   297 where
   298   "inv1 Empty = True"
   299 | "inv1 (Branch c lt k v rt) \<longleftrightarrow> inv1 lt \<and> inv1 rt \<and> (c = B \<or> color_of lt = B \<and> color_of rt = B)"
   300 
   301 primrec inv1l :: "('a, 'b) rbt \<Rightarrow> bool" -- {* Weaker version *}
   302 where
   303   "inv1l Empty = True"
   304 | "inv1l (Branch c l k v r) = (inv1 l \<and> inv1 r)"
   305 lemma [simp]: "inv1 t \<Longrightarrow> inv1l t" by (cases t) simp+
   306 
   307 primrec inv2 :: "('a, 'b) rbt \<Rightarrow> bool"
   308 where
   309   "inv2 Empty = True"
   310 | "inv2 (Branch c lt k v rt) = (inv2 lt \<and> inv2 rt \<and> bheight lt = bheight rt)"
   311 
   312 context ord begin
   313 
   314 definition is_rbt :: "('a, 'b) rbt \<Rightarrow> bool" where
   315   "is_rbt t \<longleftrightarrow> inv1 t \<and> inv2 t \<and> color_of t = B \<and> rbt_sorted t"
   316 
   317 lemma is_rbt_rbt_sorted [simp]:
   318   "is_rbt t \<Longrightarrow> rbt_sorted t" by (simp add: is_rbt_def)
   319 
   320 theorem Empty_is_rbt [simp]:
   321   "is_rbt Empty" by (simp add: is_rbt_def)
   322 
   323 end
   324 
   325 subsection {* Insertion *}
   326 
   327 fun (* slow, due to massive case splitting *)
   328   balance :: "('a,'b) rbt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"
   329 where
   330   "balance (Branch R a w x b) s t (Branch R c y z d) = Branch R (Branch B a w x b) s t (Branch B c y z d)" |
   331   "balance (Branch R (Branch R a w x b) s t c) y z d = Branch R (Branch B a w x b) s t (Branch B c y z d)" |
   332   "balance (Branch R a w x (Branch R b s t c)) y z d = Branch R (Branch B a w x b) s t (Branch B c y z d)" |
   333   "balance a w x (Branch R b s t (Branch R c y z d)) = Branch R (Branch B a w x b) s t (Branch B c y z d)" |
   334   "balance a w x (Branch R (Branch R b s t c) y z d) = Branch R (Branch B a w x b) s t (Branch B c y z d)" |
   335   "balance a s t b = Branch B a s t b"
   336 
   337 lemma balance_inv1: "\<lbrakk>inv1l l; inv1l r\<rbrakk> \<Longrightarrow> inv1 (balance l k v r)" 
   338   by (induct l k v r rule: balance.induct) auto
   339 
   340 lemma balance_bheight: "bheight l = bheight r \<Longrightarrow> bheight (balance l k v r) = Suc (bheight l)"
   341   by (induct l k v r rule: balance.induct) auto
   342 
   343 lemma balance_inv2: 
   344   assumes "inv2 l" "inv2 r" "bheight l = bheight r"
   345   shows "inv2 (balance l k v r)"
   346   using assms
   347   by (induct l k v r rule: balance.induct) auto
   348 
   349 context ord begin
   350 
   351 lemma balance_rbt_greater[simp]: "(v \<guillemotleft>| balance a k x b) = (v \<guillemotleft>| a \<and> v \<guillemotleft>| b \<and> v < k)" 
   352   by (induct a k x b rule: balance.induct) auto
   353 
   354 lemma balance_rbt_less[simp]: "(balance a k x b |\<guillemotleft> v) = (a |\<guillemotleft> v \<and> b |\<guillemotleft> v \<and> k < v)"
   355   by (induct a k x b rule: balance.induct) auto
   356 
   357 end
   358 
   359 lemma (in linorder) balance_rbt_sorted: 
   360   fixes k :: "'a"
   361   assumes "rbt_sorted l" "rbt_sorted r" "l |\<guillemotleft> k" "k \<guillemotleft>| r"
   362   shows "rbt_sorted (balance l k v r)"
   363 using assms proof (induct l k v r rule: balance.induct)
   364   case ("2_2" a x w b y t c z s va vb vd vc)
   365   hence "y < z \<and> z \<guillemotleft>| Branch B va vb vd vc" 
   366     by (auto simp add: rbt_ord_props)
   367   hence "y \<guillemotleft>| (Branch B va vb vd vc)" by (blast dest: rbt_greater_trans)
   368   with "2_2" show ?case by simp
   369 next
   370   case ("3_2" va vb vd vc x w b y s c z)
   371   from "3_2" have "x < y \<and> Branch B va vb vd vc |\<guillemotleft> x" 
   372     by simp
   373   hence "Branch B va vb vd vc |\<guillemotleft> y" by (blast dest: rbt_less_trans)
   374   with "3_2" show ?case by simp
   375 next
   376   case ("3_3" x w b y s c z t va vb vd vc)
   377   from "3_3" have "y < z \<and> z \<guillemotleft>| Branch B va vb vd vc" by simp
   378   hence "y \<guillemotleft>| Branch B va vb vd vc" by (blast dest: rbt_greater_trans)
   379   with "3_3" show ?case by simp
   380 next
   381   case ("3_4" vd ve vg vf x w b y s c z t va vb vii vc)
   382   hence "x < y \<and> Branch B vd ve vg vf |\<guillemotleft> x" by simp
   383   hence 1: "Branch B vd ve vg vf |\<guillemotleft> y" by (blast dest: rbt_less_trans)
   384   from "3_4" have "y < z \<and> z \<guillemotleft>| Branch B va vb vii vc" by simp
   385   hence "y \<guillemotleft>| Branch B va vb vii vc" by (blast dest: rbt_greater_trans)
   386   with 1 "3_4" show ?case by simp
   387 next
   388   case ("4_2" va vb vd vc x w b y s c z t dd)
   389   hence "x < y \<and> Branch B va vb vd vc |\<guillemotleft> x" by simp
   390   hence "Branch B va vb vd vc |\<guillemotleft> y" by (blast dest: rbt_less_trans)
   391   with "4_2" show ?case by simp
   392 next
   393   case ("5_2" x w b y s c z t va vb vd vc)
   394   hence "y < z \<and> z \<guillemotleft>| Branch B va vb vd vc" by simp
   395   hence "y \<guillemotleft>| Branch B va vb vd vc" by (blast dest: rbt_greater_trans)
   396   with "5_2" show ?case by simp
   397 next
   398   case ("5_3" va vb vd vc x w b y s c z t)
   399   hence "x < y \<and> Branch B va vb vd vc |\<guillemotleft> x" by simp
   400   hence "Branch B va vb vd vc |\<guillemotleft> y" by (blast dest: rbt_less_trans)
   401   with "5_3" show ?case by simp
   402 next
   403   case ("5_4" va vb vg vc x w b y s c z t vd ve vii vf)
   404   hence "x < y \<and> Branch B va vb vg vc |\<guillemotleft> x" by simp
   405   hence 1: "Branch B va vb vg vc |\<guillemotleft> y" by (blast dest: rbt_less_trans)
   406   from "5_4" have "y < z \<and> z \<guillemotleft>| Branch B vd ve vii vf" by simp
   407   hence "y \<guillemotleft>| Branch B vd ve vii vf" by (blast dest: rbt_greater_trans)
   408   with 1 "5_4" show ?case by simp
   409 qed simp+
   410 
   411 lemma entries_balance [simp]:
   412   "entries (balance l k v r) = entries l @ (k, v) # entries r"
   413   by (induct l k v r rule: balance.induct) auto
   414 
   415 lemma keys_balance [simp]: 
   416   "keys (balance l k v r) = keys l @ k # keys r"
   417   by (simp add: keys_def)
   418 
   419 lemma balance_in_tree:  
   420   "entry_in_tree k x (balance l v y r) \<longleftrightarrow> entry_in_tree k x l \<or> k = v \<and> x = y \<or> entry_in_tree k x r"
   421   by (auto simp add: keys_def)
   422 
   423 lemma (in linorder) rbt_lookup_balance[simp]: 
   424 fixes k :: "'a"
   425 assumes "rbt_sorted l" "rbt_sorted r" "l |\<guillemotleft> k" "k \<guillemotleft>| r"
   426 shows "rbt_lookup (balance l k v r) x = rbt_lookup (Branch B l k v r) x"
   427 by (rule rbt_lookup_from_in_tree) (auto simp:assms balance_in_tree balance_rbt_sorted)
   428 
   429 primrec paint :: "color \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"
   430 where
   431   "paint c Empty = Empty"
   432 | "paint c (Branch _ l k v r) = Branch c l k v r"
   433 
   434 lemma paint_inv1l[simp]: "inv1l t \<Longrightarrow> inv1l (paint c t)" by (cases t) auto
   435 lemma paint_inv1[simp]: "inv1l t \<Longrightarrow> inv1 (paint B t)" by (cases t) auto
   436 lemma paint_inv2[simp]: "inv2 t \<Longrightarrow> inv2 (paint c t)" by (cases t) auto
   437 lemma paint_color_of[simp]: "color_of (paint B t) = B" by (cases t) auto
   438 lemma paint_in_tree[simp]: "entry_in_tree k x (paint c t) = entry_in_tree k x t" by (cases t) auto
   439 
   440 context ord begin
   441 
   442 lemma paint_rbt_sorted[simp]: "rbt_sorted t \<Longrightarrow> rbt_sorted (paint c t)" by (cases t) auto
   443 lemma paint_rbt_lookup[simp]: "rbt_lookup (paint c t) = rbt_lookup t" by (rule ext) (cases t, auto)
   444 lemma paint_rbt_greater[simp]: "(v \<guillemotleft>| paint c t) = (v \<guillemotleft>| t)" by (cases t) auto
   445 lemma paint_rbt_less[simp]: "(paint c t |\<guillemotleft> v) = (t |\<guillemotleft> v)" by (cases t) auto
   446 
   447 fun
   448   rbt_ins :: "('a \<Rightarrow> 'b \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"
   449 where
   450   "rbt_ins f k v Empty = Branch R Empty k v Empty" |
   451   "rbt_ins f k v (Branch B l x y r) = (if k < x then balance (rbt_ins f k v l) x y r
   452                                        else if k > x then balance l x y (rbt_ins f k v r)
   453                                        else Branch B l x (f k y v) r)" |
   454   "rbt_ins f k v (Branch R l x y r) = (if k < x then Branch R (rbt_ins f k v l) x y r
   455                                        else if k > x then Branch R l x y (rbt_ins f k v r)
   456                                        else Branch R l x (f k y v) r)"
   457 
   458 lemma ins_inv1_inv2: 
   459   assumes "inv1 t" "inv2 t"
   460   shows "inv2 (rbt_ins f k x t)" "bheight (rbt_ins f k x t) = bheight t" 
   461   "color_of t = B \<Longrightarrow> inv1 (rbt_ins f k x t)" "inv1l (rbt_ins f k x t)"
   462   using assms
   463   by (induct f k x t rule: rbt_ins.induct) (auto simp: balance_inv1 balance_inv2 balance_bheight)
   464 
   465 end
   466 
   467 context linorder begin
   468 
   469 lemma ins_rbt_greater[simp]: "(v \<guillemotleft>| rbt_ins f (k :: 'a) x t) = (v \<guillemotleft>| t \<and> k > v)"
   470   by (induct f k x t rule: rbt_ins.induct) auto
   471 lemma ins_rbt_less[simp]: "(rbt_ins f k x t |\<guillemotleft> v) = (t |\<guillemotleft> v \<and> k < v)"
   472   by (induct f k x t rule: rbt_ins.induct) auto
   473 lemma ins_rbt_sorted[simp]: "rbt_sorted t \<Longrightarrow> rbt_sorted (rbt_ins f k x t)"
   474   by (induct f k x t rule: rbt_ins.induct) (auto simp: balance_rbt_sorted)
   475 
   476 lemma keys_ins: "set (keys (rbt_ins f k v t)) = { k } \<union> set (keys t)"
   477   by (induct f k v t rule: rbt_ins.induct) auto
   478 
   479 lemma rbt_lookup_ins: 
   480   fixes k :: "'a"
   481   assumes "rbt_sorted t"
   482   shows "rbt_lookup (rbt_ins f k v t) x = ((rbt_lookup t)(k |-> case rbt_lookup t k of None \<Rightarrow> v 
   483                                                                 | Some w \<Rightarrow> f k w v)) x"
   484 using assms by (induct f k v t rule: rbt_ins.induct) auto
   485 
   486 end
   487 
   488 context ord begin
   489 
   490 definition rbt_insert_with_key :: "('a \<Rightarrow> 'b \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"
   491 where "rbt_insert_with_key f k v t = paint B (rbt_ins f k v t)"
   492 
   493 definition rbt_insertw_def: "rbt_insert_with f = rbt_insert_with_key (\<lambda>_. f)"
   494 
   495 definition rbt_insert :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) rbt \<Rightarrow> ('a, 'b) rbt" where
   496   "rbt_insert = rbt_insert_with_key (\<lambda>_ _ nv. nv)"
   497 
   498 end
   499 
   500 context linorder begin
   501 
   502 lemma rbt_insertwk_rbt_sorted: "rbt_sorted t \<Longrightarrow> rbt_sorted (rbt_insert_with_key f (k :: 'a) x t)"
   503   by (auto simp: rbt_insert_with_key_def)
   504 
   505 theorem rbt_insertwk_is_rbt: 
   506   assumes inv: "is_rbt t" 
   507   shows "is_rbt (rbt_insert_with_key f k x t)"
   508 using assms
   509 unfolding rbt_insert_with_key_def is_rbt_def
   510 by (auto simp: ins_inv1_inv2)
   511 
   512 lemma rbt_lookup_rbt_insertwk: 
   513   assumes "rbt_sorted t"
   514   shows "rbt_lookup (rbt_insert_with_key f k v t) x = ((rbt_lookup t)(k |-> case rbt_lookup t k of None \<Rightarrow> v 
   515                                                        | Some w \<Rightarrow> f k w v)) x"
   516 unfolding rbt_insert_with_key_def using assms
   517 by (simp add:rbt_lookup_ins)
   518 
   519 lemma rbt_insertw_rbt_sorted: "rbt_sorted t \<Longrightarrow> rbt_sorted (rbt_insert_with f k v t)" 
   520   by (simp add: rbt_insertwk_rbt_sorted rbt_insertw_def)
   521 theorem rbt_insertw_is_rbt: "is_rbt t \<Longrightarrow> is_rbt (rbt_insert_with f k v t)"
   522   by (simp add: rbt_insertwk_is_rbt rbt_insertw_def)
   523 
   524 lemma rbt_lookup_rbt_insertw:
   525   assumes "is_rbt t"
   526   shows "rbt_lookup (rbt_insert_with f k v t) = (rbt_lookup t)(k \<mapsto> (if k:dom (rbt_lookup t) then f (the (rbt_lookup t k)) v else v))"
   527 using assms
   528 unfolding rbt_insertw_def
   529 by (rule_tac ext) (cases "rbt_lookup t k", auto simp:rbt_lookup_rbt_insertwk dom_def)
   530 
   531 lemma rbt_insert_rbt_sorted: "rbt_sorted t \<Longrightarrow> rbt_sorted (rbt_insert k v t)"
   532   by (simp add: rbt_insertwk_rbt_sorted rbt_insert_def)
   533 theorem rbt_insert_is_rbt [simp]: "is_rbt t \<Longrightarrow> is_rbt (rbt_insert k v t)"
   534   by (simp add: rbt_insertwk_is_rbt rbt_insert_def)
   535 
   536 lemma rbt_lookup_rbt_insert: 
   537   assumes "is_rbt t"
   538   shows "rbt_lookup (rbt_insert k v t) = (rbt_lookup t)(k\<mapsto>v)"
   539 unfolding rbt_insert_def
   540 using assms
   541 by (rule_tac ext) (simp add: rbt_lookup_rbt_insertwk split:option.split)
   542 
   543 end
   544 
   545 subsection {* Deletion *}
   546 
   547 lemma bheight_paintR'[simp]: "color_of t = B \<Longrightarrow> bheight (paint R t) = bheight t - 1"
   548 by (cases t rule: rbt_cases) auto
   549 
   550 fun
   551   balance_left :: "('a,'b) rbt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"
   552 where
   553   "balance_left (Branch R a k x b) s y c = Branch R (Branch B a k x b) s y c" |
   554   "balance_left bl k x (Branch B a s y b) = balance bl k x (Branch R a s y b)" |
   555   "balance_left bl k x (Branch R (Branch B a s y b) t z c) = Branch R (Branch B bl k x a) s y (balance b t z (paint R c))" |
   556   "balance_left t k x s = Empty"
   557 
   558 lemma balance_left_inv2_with_inv1:
   559   assumes "inv2 lt" "inv2 rt" "bheight lt + 1 = bheight rt" "inv1 rt"
   560   shows "bheight (balance_left lt k v rt) = bheight lt + 1"
   561   and   "inv2 (balance_left lt k v rt)"
   562 using assms 
   563 by (induct lt k v rt rule: balance_left.induct) (auto simp: balance_inv2 balance_bheight)
   564 
   565 lemma balance_left_inv2_app: 
   566   assumes "inv2 lt" "inv2 rt" "bheight lt + 1 = bheight rt" "color_of rt = B"
   567   shows "inv2 (balance_left lt k v rt)" 
   568         "bheight (balance_left lt k v rt) = bheight rt"
   569 using assms 
   570 by (induct lt k v rt rule: balance_left.induct) (auto simp add: balance_inv2 balance_bheight)+ 
   571 
   572 lemma balance_left_inv1: "\<lbrakk>inv1l a; inv1 b; color_of b = B\<rbrakk> \<Longrightarrow> inv1 (balance_left a k x b)"
   573   by (induct a k x b rule: balance_left.induct) (simp add: balance_inv1)+
   574 
   575 lemma balance_left_inv1l: "\<lbrakk> inv1l lt; inv1 rt \<rbrakk> \<Longrightarrow> inv1l (balance_left lt k x rt)"
   576 by (induct lt k x rt rule: balance_left.induct) (auto simp: balance_inv1)
   577 
   578 lemma (in linorder) balance_left_rbt_sorted: 
   579   "\<lbrakk> rbt_sorted l; rbt_sorted r; rbt_less k l; k \<guillemotleft>| r \<rbrakk> \<Longrightarrow> rbt_sorted (balance_left l k v r)"
   580 apply (induct l k v r rule: balance_left.induct)
   581 apply (auto simp: balance_rbt_sorted)
   582 apply (unfold rbt_greater_prop rbt_less_prop)
   583 by force+
   584 
   585 context order begin
   586 
   587 lemma balance_left_rbt_greater: 
   588   fixes k :: "'a"
   589   assumes "k \<guillemotleft>| a" "k \<guillemotleft>| b" "k < x" 
   590   shows "k \<guillemotleft>| balance_left a x t b"
   591 using assms 
   592 by (induct a x t b rule: balance_left.induct) auto
   593 
   594 lemma balance_left_rbt_less: 
   595   fixes k :: "'a"
   596   assumes "a |\<guillemotleft> k" "b |\<guillemotleft> k" "x < k" 
   597   shows "balance_left a x t b |\<guillemotleft> k"
   598 using assms
   599 by (induct a x t b rule: balance_left.induct) auto
   600 
   601 end
   602 
   603 lemma balance_left_in_tree: 
   604   assumes "inv1l l" "inv1 r" "bheight l + 1 = bheight r"
   605   shows "entry_in_tree k v (balance_left l a b r) = (entry_in_tree k v l \<or> k = a \<and> v = b \<or> entry_in_tree k v r)"
   606 using assms 
   607 by (induct l k v r rule: balance_left.induct) (auto simp: balance_in_tree)
   608 
   609 fun
   610   balance_right :: "('a,'b) rbt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"
   611 where
   612   "balance_right a k x (Branch R b s y c) = Branch R a k x (Branch B b s y c)" |
   613   "balance_right (Branch B a k x b) s y bl = balance (Branch R a k x b) s y bl" |
   614   "balance_right (Branch R a k x (Branch B b s y c)) t z bl = Branch R (balance (paint R a) k x b) s y (Branch B c t z bl)" |
   615   "balance_right t k x s = Empty"
   616 
   617 lemma balance_right_inv2_with_inv1:
   618   assumes "inv2 lt" "inv2 rt" "bheight lt = bheight rt + 1" "inv1 lt"
   619   shows "inv2 (balance_right lt k v rt) \<and> bheight (balance_right lt k v rt) = bheight lt"
   620 using assms
   621 by (induct lt k v rt rule: balance_right.induct) (auto simp: balance_inv2 balance_bheight)
   622 
   623 lemma balance_right_inv1: "\<lbrakk>inv1 a; inv1l b; color_of a = B\<rbrakk> \<Longrightarrow> inv1 (balance_right a k x b)"
   624 by (induct a k x b rule: balance_right.induct) (simp add: balance_inv1)+
   625 
   626 lemma balance_right_inv1l: "\<lbrakk> inv1 lt; inv1l rt \<rbrakk> \<Longrightarrow>inv1l (balance_right lt k x rt)"
   627 by (induct lt k x rt rule: balance_right.induct) (auto simp: balance_inv1)
   628 
   629 lemma (in linorder) balance_right_rbt_sorted:
   630   "\<lbrakk> rbt_sorted l; rbt_sorted r; rbt_less k l; k \<guillemotleft>| r \<rbrakk> \<Longrightarrow> rbt_sorted (balance_right l k v r)"
   631 apply (induct l k v r rule: balance_right.induct)
   632 apply (auto simp:balance_rbt_sorted)
   633 apply (unfold rbt_less_prop rbt_greater_prop)
   634 by force+
   635 
   636 context order begin
   637 
   638 lemma balance_right_rbt_greater: 
   639   fixes k :: "'a"
   640   assumes "k \<guillemotleft>| a" "k \<guillemotleft>| b" "k < x" 
   641   shows "k \<guillemotleft>| balance_right a x t b"
   642 using assms by (induct a x t b rule: balance_right.induct) auto
   643 
   644 lemma balance_right_rbt_less: 
   645   fixes k :: "'a"
   646   assumes "a |\<guillemotleft> k" "b |\<guillemotleft> k" "x < k" 
   647   shows "balance_right a x t b |\<guillemotleft> k"
   648 using assms by (induct a x t b rule: balance_right.induct) auto
   649 
   650 end
   651 
   652 lemma balance_right_in_tree:
   653   assumes "inv1 l" "inv1l r" "bheight l = bheight r + 1" "inv2 l" "inv2 r"
   654   shows "entry_in_tree x y (balance_right l k v r) = (entry_in_tree x y l \<or> x = k \<and> y = v \<or> entry_in_tree x y r)"
   655 using assms by (induct l k v r rule: balance_right.induct) (auto simp: balance_in_tree)
   656 
   657 fun
   658   combine :: "('a,'b) rbt \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"
   659 where
   660   "combine Empty x = x" 
   661 | "combine x Empty = x" 
   662 | "combine (Branch R a k x b) (Branch R c s y d) = (case (combine b c) of
   663                                     Branch R b2 t z c2 \<Rightarrow> (Branch R (Branch R a k x b2) t z (Branch R c2 s y d)) |
   664                                     bc \<Rightarrow> Branch R a k x (Branch R bc s y d))" 
   665 | "combine (Branch B a k x b) (Branch B c s y d) = (case (combine b c) of
   666                                     Branch R b2 t z c2 \<Rightarrow> Branch R (Branch B a k x b2) t z (Branch B c2 s y d) |
   667                                     bc \<Rightarrow> balance_left a k x (Branch B bc s y d))" 
   668 | "combine a (Branch R b k x c) = Branch R (combine a b) k x c" 
   669 | "combine (Branch R a k x b) c = Branch R a k x (combine b c)" 
   670 
   671 lemma combine_inv2:
   672   assumes "inv2 lt" "inv2 rt" "bheight lt = bheight rt"
   673   shows "bheight (combine lt rt) = bheight lt" "inv2 (combine lt rt)"
   674 using assms 
   675 by (induct lt rt rule: combine.induct) 
   676    (auto simp: balance_left_inv2_app split: rbt.splits color.splits)
   677 
   678 lemma combine_inv1: 
   679   assumes "inv1 lt" "inv1 rt"
   680   shows "color_of lt = B \<Longrightarrow> color_of rt = B \<Longrightarrow> inv1 (combine lt rt)"
   681          "inv1l (combine lt rt)"
   682 using assms 
   683 by (induct lt rt rule: combine.induct)
   684    (auto simp: balance_left_inv1 split: rbt.splits color.splits)
   685 
   686 context linorder begin
   687 
   688 lemma combine_rbt_greater[simp]: 
   689   fixes k :: "'a"
   690   assumes "k \<guillemotleft>| l" "k \<guillemotleft>| r" 
   691   shows "k \<guillemotleft>| combine l r"
   692 using assms 
   693 by (induct l r rule: combine.induct)
   694    (auto simp: balance_left_rbt_greater split:rbt.splits color.splits)
   695 
   696 lemma combine_rbt_less[simp]: 
   697   fixes k :: "'a"
   698   assumes "l |\<guillemotleft> k" "r |\<guillemotleft> k" 
   699   shows "combine l r |\<guillemotleft> k"
   700 using assms 
   701 by (induct l r rule: combine.induct)
   702    (auto simp: balance_left_rbt_less split:rbt.splits color.splits)
   703 
   704 lemma combine_rbt_sorted: 
   705   fixes k :: "'a"
   706   assumes "rbt_sorted l" "rbt_sorted r" "l |\<guillemotleft> k" "k \<guillemotleft>| r"
   707   shows "rbt_sorted (combine l r)"
   708 using assms proof (induct l r rule: combine.induct)
   709   case (3 a x v b c y w d)
   710   hence ineqs: "a |\<guillemotleft> x" "x \<guillemotleft>| b" "b |\<guillemotleft> k" "k \<guillemotleft>| c" "c |\<guillemotleft> y" "y \<guillemotleft>| d"
   711     by auto
   712   with 3
   713   show ?case
   714     by (cases "combine b c" rule: rbt_cases)
   715       (auto, (metis combine_rbt_greater combine_rbt_less ineqs ineqs rbt_less_simps(2) rbt_greater_simps(2) rbt_greater_trans rbt_less_trans)+)
   716 next
   717   case (4 a x v b c y w d)
   718   hence "x < k \<and> rbt_greater k c" by simp
   719   hence "rbt_greater x c" by (blast dest: rbt_greater_trans)
   720   with 4 have 2: "rbt_greater x (combine b c)" by (simp add: combine_rbt_greater)
   721   from 4 have "k < y \<and> rbt_less k b" by simp
   722   hence "rbt_less y b" by (blast dest: rbt_less_trans)
   723   with 4 have 3: "rbt_less y (combine b c)" by (simp add: combine_rbt_less)
   724   show ?case
   725   proof (cases "combine b c" rule: rbt_cases)
   726     case Empty
   727     from 4 have "x < y \<and> rbt_greater y d" by auto
   728     hence "rbt_greater x d" by (blast dest: rbt_greater_trans)
   729     with 4 Empty have "rbt_sorted a" and "rbt_sorted (Branch B Empty y w d)"
   730       and "rbt_less x a" and "rbt_greater x (Branch B Empty y w d)" by auto
   731     with Empty show ?thesis by (simp add: balance_left_rbt_sorted)
   732   next
   733     case (Red lta va ka rta)
   734     with 2 4 have "x < va \<and> rbt_less x a" by simp
   735     hence 5: "rbt_less va a" by (blast dest: rbt_less_trans)
   736     from Red 3 4 have "va < y \<and> rbt_greater y d" by simp
   737     hence "rbt_greater va d" by (blast dest: rbt_greater_trans)
   738     with Red 2 3 4 5 show ?thesis by simp
   739   next
   740     case (Black lta va ka rta)
   741     from 4 have "x < y \<and> rbt_greater y d" by auto
   742     hence "rbt_greater x d" by (blast dest: rbt_greater_trans)
   743     with Black 2 3 4 have "rbt_sorted a" and "rbt_sorted (Branch B (combine b c) y w d)" 
   744       and "rbt_less x a" and "rbt_greater x (Branch B (combine b c) y w d)" by auto
   745     with Black show ?thesis by (simp add: balance_left_rbt_sorted)
   746   qed
   747 next
   748   case (5 va vb vd vc b x w c)
   749   hence "k < x \<and> rbt_less k (Branch B va vb vd vc)" by simp
   750   hence "rbt_less x (Branch B va vb vd vc)" by (blast dest: rbt_less_trans)
   751   with 5 show ?case by (simp add: combine_rbt_less)
   752 next
   753   case (6 a x v b va vb vd vc)
   754   hence "x < k \<and> rbt_greater k (Branch B va vb vd vc)" by simp
   755   hence "rbt_greater x (Branch B va vb vd vc)" by (blast dest: rbt_greater_trans)
   756   with 6 show ?case by (simp add: combine_rbt_greater)
   757 qed simp+
   758 
   759 end
   760 
   761 lemma combine_in_tree: 
   762   assumes "inv2 l" "inv2 r" "bheight l = bheight r" "inv1 l" "inv1 r"
   763   shows "entry_in_tree k v (combine l r) = (entry_in_tree k v l \<or> entry_in_tree k v r)"
   764 using assms 
   765 proof (induct l r rule: combine.induct)
   766   case (4 _ _ _ b c)
   767   hence a: "bheight (combine b c) = bheight b" by (simp add: combine_inv2)
   768   from 4 have b: "inv1l (combine b c)" by (simp add: combine_inv1)
   769 
   770   show ?case
   771   proof (cases "combine b c" rule: rbt_cases)
   772     case Empty
   773     with 4 a show ?thesis by (auto simp: balance_left_in_tree)
   774   next
   775     case (Red lta ka va rta)
   776     with 4 show ?thesis by auto
   777   next
   778     case (Black lta ka va rta)
   779     with a b 4  show ?thesis by (auto simp: balance_left_in_tree)
   780   qed 
   781 qed (auto split: rbt.splits color.splits)
   782 
   783 context ord begin
   784 
   785 fun
   786   rbt_del_from_left :: "'a \<Rightarrow> ('a,'b) rbt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt" and
   787   rbt_del_from_right :: "'a \<Rightarrow> ('a,'b) rbt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt" and
   788   rbt_del :: "'a\<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"
   789 where
   790   "rbt_del x Empty = Empty" |
   791   "rbt_del x (Branch c a y s b) = 
   792    (if x < y then rbt_del_from_left x a y s b 
   793     else (if x > y then rbt_del_from_right x a y s b else combine a b))" |
   794   "rbt_del_from_left x (Branch B lt z v rt) y s b = balance_left (rbt_del x (Branch B lt z v rt)) y s b" |
   795   "rbt_del_from_left x a y s b = Branch R (rbt_del x a) y s b" |
   796   "rbt_del_from_right x a y s (Branch B lt z v rt) = balance_right a y s (rbt_del x (Branch B lt z v rt))" | 
   797   "rbt_del_from_right x a y s b = Branch R a y s (rbt_del x b)"
   798 
   799 end
   800 
   801 context linorder begin
   802 
   803 lemma 
   804   assumes "inv2 lt" "inv1 lt"
   805   shows
   806   "\<lbrakk>inv2 rt; bheight lt = bheight rt; inv1 rt\<rbrakk> \<Longrightarrow>
   807    inv2 (rbt_del_from_left x lt k v rt) \<and> 
   808    bheight (rbt_del_from_left x lt k v rt) = bheight lt \<and> 
   809    (color_of lt = B \<and> color_of rt = B \<and> inv1 (rbt_del_from_left x lt k v rt) \<or> 
   810     (color_of lt \<noteq> B \<or> color_of rt \<noteq> B) \<and> inv1l (rbt_del_from_left x lt k v rt))"
   811   and "\<lbrakk>inv2 rt; bheight lt = bheight rt; inv1 rt\<rbrakk> \<Longrightarrow>
   812   inv2 (rbt_del_from_right x lt k v rt) \<and> 
   813   bheight (rbt_del_from_right x lt k v rt) = bheight lt \<and> 
   814   (color_of lt = B \<and> color_of rt = B \<and> inv1 (rbt_del_from_right x lt k v rt) \<or> 
   815    (color_of lt \<noteq> B \<or> color_of rt \<noteq> B) \<and> inv1l (rbt_del_from_right x lt k v rt))"
   816   and rbt_del_inv1_inv2: "inv2 (rbt_del x lt) \<and> (color_of lt = R \<and> bheight (rbt_del x lt) = bheight lt \<and> inv1 (rbt_del x lt) 
   817   \<or> color_of lt = B \<and> bheight (rbt_del x lt) = bheight lt - 1 \<and> inv1l (rbt_del x lt))"
   818 using assms
   819 proof (induct x lt k v rt and x lt k v rt and x lt rule: rbt_del_from_left_rbt_del_from_right_rbt_del.induct)
   820 case (2 y c _ y')
   821   have "y = y' \<or> y < y' \<or> y > y'" by auto
   822   thus ?case proof (elim disjE)
   823     assume "y = y'"
   824     with 2 show ?thesis by (cases c) (simp add: combine_inv2 combine_inv1)+
   825   next
   826     assume "y < y'"
   827     with 2 show ?thesis by (cases c) auto
   828   next
   829     assume "y' < y"
   830     with 2 show ?thesis by (cases c) auto
   831   qed
   832 next
   833   case (3 y lt z v rta y' ss bb) 
   834   thus ?case by (cases "color_of (Branch B lt z v rta) = B \<and> color_of bb = B") (simp add: balance_left_inv2_with_inv1 balance_left_inv1 balance_left_inv1l)+
   835 next
   836   case (5 y a y' ss lt z v rta)
   837   thus ?case by (cases "color_of a = B \<and> color_of (Branch B lt z v rta) = B") (simp add: balance_right_inv2_with_inv1 balance_right_inv1 balance_right_inv1l)+
   838 next
   839   case ("6_1" y a y' ss) thus ?case by (cases "color_of a = B \<and> color_of Empty = B") simp+
   840 qed auto
   841 
   842 lemma 
   843   rbt_del_from_left_rbt_less: "\<lbrakk> lt |\<guillemotleft> v; rt |\<guillemotleft> v; k < v\<rbrakk> \<Longrightarrow> rbt_del_from_left x lt k y rt |\<guillemotleft> v"
   844   and rbt_del_from_right_rbt_less: "\<lbrakk>lt |\<guillemotleft> v; rt |\<guillemotleft> v; k < v\<rbrakk> \<Longrightarrow> rbt_del_from_right x lt k y rt |\<guillemotleft> v"
   845   and rbt_del_rbt_less: "lt |\<guillemotleft> v \<Longrightarrow> rbt_del x lt |\<guillemotleft> v"
   846 by (induct x lt k y rt and x lt k y rt and x lt rule: rbt_del_from_left_rbt_del_from_right_rbt_del.induct) 
   847    (auto simp: balance_left_rbt_less balance_right_rbt_less)
   848 
   849 lemma rbt_del_from_left_rbt_greater: "\<lbrakk>v \<guillemotleft>| lt; v \<guillemotleft>| rt; k > v\<rbrakk> \<Longrightarrow> v \<guillemotleft>| rbt_del_from_left x lt k y rt"
   850   and rbt_del_from_right_rbt_greater: "\<lbrakk>v \<guillemotleft>| lt; v \<guillemotleft>| rt; k > v\<rbrakk> \<Longrightarrow> v \<guillemotleft>| rbt_del_from_right x lt k y rt"
   851   and rbt_del_rbt_greater: "v \<guillemotleft>| lt \<Longrightarrow> v \<guillemotleft>| rbt_del x lt"
   852 by (induct x lt k y rt and x lt k y rt and x lt rule: rbt_del_from_left_rbt_del_from_right_rbt_del.induct)
   853    (auto simp: balance_left_rbt_greater balance_right_rbt_greater)
   854 
   855 lemma "\<lbrakk>rbt_sorted lt; rbt_sorted rt; lt |\<guillemotleft> k; k \<guillemotleft>| rt\<rbrakk> \<Longrightarrow> rbt_sorted (rbt_del_from_left x lt k y rt)"
   856   and "\<lbrakk>rbt_sorted lt; rbt_sorted rt; lt |\<guillemotleft> k; k \<guillemotleft>| rt\<rbrakk> \<Longrightarrow> rbt_sorted (rbt_del_from_right x lt k y rt)"
   857   and rbt_del_rbt_sorted: "rbt_sorted lt \<Longrightarrow> rbt_sorted (rbt_del x lt)"
   858 proof (induct x lt k y rt and x lt k y rt and x lt rule: rbt_del_from_left_rbt_del_from_right_rbt_del.induct)
   859   case (3 x lta zz v rta yy ss bb)
   860   from 3 have "Branch B lta zz v rta |\<guillemotleft> yy" by simp
   861   hence "rbt_del x (Branch B lta zz v rta) |\<guillemotleft> yy" by (rule rbt_del_rbt_less)
   862   with 3 show ?case by (simp add: balance_left_rbt_sorted)
   863 next
   864   case ("4_2" x vaa vbb vdd vc yy ss bb)
   865   hence "Branch R vaa vbb vdd vc |\<guillemotleft> yy" by simp
   866   hence "rbt_del x (Branch R vaa vbb vdd vc) |\<guillemotleft> yy" by (rule rbt_del_rbt_less)
   867   with "4_2" show ?case by simp
   868 next
   869   case (5 x aa yy ss lta zz v rta) 
   870   hence "yy \<guillemotleft>| Branch B lta zz v rta" by simp
   871   hence "yy \<guillemotleft>| rbt_del x (Branch B lta zz v rta)" by (rule rbt_del_rbt_greater)
   872   with 5 show ?case by (simp add: balance_right_rbt_sorted)
   873 next
   874   case ("6_2" x aa yy ss vaa vbb vdd vc)
   875   hence "yy \<guillemotleft>| Branch R vaa vbb vdd vc" by simp
   876   hence "yy \<guillemotleft>| rbt_del x (Branch R vaa vbb vdd vc)" by (rule rbt_del_rbt_greater)
   877   with "6_2" show ?case by simp
   878 qed (auto simp: combine_rbt_sorted)
   879 
   880 lemma "\<lbrakk>rbt_sorted lt; rbt_sorted rt; lt |\<guillemotleft> kt; kt \<guillemotleft>| rt; inv1 lt; inv1 rt; inv2 lt; inv2 rt; bheight lt = bheight rt; x < kt\<rbrakk> \<Longrightarrow> entry_in_tree k v (rbt_del_from_left x lt kt y rt) = (False \<or> (x \<noteq> k \<and> entry_in_tree k v (Branch c lt kt y rt)))"
   881   and "\<lbrakk>rbt_sorted lt; rbt_sorted rt; lt |\<guillemotleft> kt; kt \<guillemotleft>| rt; inv1 lt; inv1 rt; inv2 lt; inv2 rt; bheight lt = bheight rt; x > kt\<rbrakk> \<Longrightarrow> entry_in_tree k v (rbt_del_from_right x lt kt y rt) = (False \<or> (x \<noteq> k \<and> entry_in_tree k v (Branch c lt kt y rt)))"
   882   and rbt_del_in_tree: "\<lbrakk>rbt_sorted t; inv1 t; inv2 t\<rbrakk> \<Longrightarrow> entry_in_tree k v (rbt_del x t) = (False \<or> (x \<noteq> k \<and> entry_in_tree k v t))"
   883 proof (induct x lt kt y rt and x lt kt y rt and x t rule: rbt_del_from_left_rbt_del_from_right_rbt_del.induct)
   884   case (2 xx c aa yy ss bb)
   885   have "xx = yy \<or> xx < yy \<or> xx > yy" by auto
   886   from this 2 show ?case proof (elim disjE)
   887     assume "xx = yy"
   888     with 2 show ?thesis proof (cases "xx = k")
   889       case True
   890       from 2 `xx = yy` `xx = k` have "rbt_sorted (Branch c aa yy ss bb) \<and> k = yy" by simp
   891       hence "\<not> entry_in_tree k v aa" "\<not> entry_in_tree k v bb" by (auto simp: rbt_less_nit rbt_greater_prop)
   892       with `xx = yy` 2 `xx = k` show ?thesis by (simp add: combine_in_tree)
   893     qed (simp add: combine_in_tree)
   894   qed simp+
   895 next    
   896   case (3 xx lta zz vv rta yy ss bb)
   897   def mt[simp]: mt == "Branch B lta zz vv rta"
   898   from 3 have "inv2 mt \<and> inv1 mt" by simp
   899   hence "inv2 (rbt_del xx mt) \<and> (color_of mt = R \<and> bheight (rbt_del xx mt) = bheight mt \<and> inv1 (rbt_del xx mt) \<or> color_of mt = B \<and> bheight (rbt_del xx mt) = bheight mt - 1 \<and> inv1l (rbt_del xx mt))" by (blast dest: rbt_del_inv1_inv2)
   900   with 3 have 4: "entry_in_tree k v (rbt_del_from_left xx mt yy ss bb) = (False \<or> xx \<noteq> k \<and> entry_in_tree k v mt \<or> (k = yy \<and> v = ss) \<or> entry_in_tree k v bb)" by (simp add: balance_left_in_tree)
   901   thus ?case proof (cases "xx = k")
   902     case True
   903     from 3 True have "yy \<guillemotleft>| bb \<and> yy > k" by simp
   904     hence "k \<guillemotleft>| bb" by (blast dest: rbt_greater_trans)
   905     with 3 4 True show ?thesis by (auto simp: rbt_greater_nit)
   906   qed auto
   907 next
   908   case ("4_1" xx yy ss bb)
   909   show ?case proof (cases "xx = k")
   910     case True
   911     with "4_1" have "yy \<guillemotleft>| bb \<and> k < yy" by simp
   912     hence "k \<guillemotleft>| bb" by (blast dest: rbt_greater_trans)
   913     with "4_1" `xx = k` 
   914    have "entry_in_tree k v (Branch R Empty yy ss bb) = entry_in_tree k v Empty" by (auto simp: rbt_greater_nit)
   915     thus ?thesis by auto
   916   qed simp+
   917 next
   918   case ("4_2" xx vaa vbb vdd vc yy ss bb)
   919   thus ?case proof (cases "xx = k")
   920     case True
   921     with "4_2" have "k < yy \<and> yy \<guillemotleft>| bb" by simp
   922     hence "k \<guillemotleft>| bb" by (blast dest: rbt_greater_trans)
   923     with True "4_2" show ?thesis by (auto simp: rbt_greater_nit)
   924   qed auto
   925 next
   926   case (5 xx aa yy ss lta zz vv rta)
   927   def mt[simp]: mt == "Branch B lta zz vv rta"
   928   from 5 have "inv2 mt \<and> inv1 mt" by simp
   929   hence "inv2 (rbt_del xx mt) \<and> (color_of mt = R \<and> bheight (rbt_del xx mt) = bheight mt \<and> inv1 (rbt_del xx mt) \<or> color_of mt = B \<and> bheight (rbt_del xx mt) = bheight mt - 1 \<and> inv1l (rbt_del xx mt))" by (blast dest: rbt_del_inv1_inv2)
   930   with 5 have 3: "entry_in_tree k v (rbt_del_from_right xx aa yy ss mt) = (entry_in_tree k v aa \<or> (k = yy \<and> v = ss) \<or> False \<or> xx \<noteq> k \<and> entry_in_tree k v mt)" by (simp add: balance_right_in_tree)
   931   thus ?case proof (cases "xx = k")
   932     case True
   933     from 5 True have "aa |\<guillemotleft> yy \<and> yy < k" by simp
   934     hence "aa |\<guillemotleft> k" by (blast dest: rbt_less_trans)
   935     with 3 5 True show ?thesis by (auto simp: rbt_less_nit)
   936   qed auto
   937 next
   938   case ("6_1" xx aa yy ss)
   939   show ?case proof (cases "xx = k")
   940     case True
   941     with "6_1" have "aa |\<guillemotleft> yy \<and> k > yy" by simp
   942     hence "aa |\<guillemotleft> k" by (blast dest: rbt_less_trans)
   943     with "6_1" `xx = k` show ?thesis by (auto simp: rbt_less_nit)
   944   qed simp
   945 next
   946   case ("6_2" xx aa yy ss vaa vbb vdd vc)
   947   thus ?case proof (cases "xx = k")
   948     case True
   949     with "6_2" have "k > yy \<and> aa |\<guillemotleft> yy" by simp
   950     hence "aa |\<guillemotleft> k" by (blast dest: rbt_less_trans)
   951     with True "6_2" show ?thesis by (auto simp: rbt_less_nit)
   952   qed auto
   953 qed simp
   954 
   955 definition (in ord) rbt_delete where
   956   "rbt_delete k t = paint B (rbt_del k t)"
   957 
   958 theorem rbt_delete_is_rbt [simp]: assumes "is_rbt t" shows "is_rbt (rbt_delete k t)"
   959 proof -
   960   from assms have "inv2 t" and "inv1 t" unfolding is_rbt_def by auto 
   961   hence "inv2 (rbt_del k t) \<and> (color_of t = R \<and> bheight (rbt_del k t) = bheight t \<and> inv1 (rbt_del k t) \<or> color_of t = B \<and> bheight (rbt_del k t) = bheight t - 1 \<and> inv1l (rbt_del k t))" by (rule rbt_del_inv1_inv2)
   962   hence "inv2 (rbt_del k t) \<and> inv1l (rbt_del k t)" by (cases "color_of t") auto
   963   with assms show ?thesis
   964     unfolding is_rbt_def rbt_delete_def
   965     by (auto intro: paint_rbt_sorted rbt_del_rbt_sorted)
   966 qed
   967 
   968 lemma rbt_delete_in_tree: 
   969   assumes "is_rbt t" 
   970   shows "entry_in_tree k v (rbt_delete x t) = (x \<noteq> k \<and> entry_in_tree k v t)"
   971   using assms unfolding is_rbt_def rbt_delete_def
   972   by (auto simp: rbt_del_in_tree)
   973 
   974 lemma rbt_lookup_rbt_delete:
   975   assumes is_rbt: "is_rbt t"
   976   shows "rbt_lookup (rbt_delete k t) = (rbt_lookup t)|`(-{k})"
   977 proof
   978   fix x
   979   show "rbt_lookup (rbt_delete k t) x = (rbt_lookup t |` (-{k})) x" 
   980   proof (cases "x = k")
   981     assume "x = k" 
   982     with is_rbt show ?thesis
   983       by (cases "rbt_lookup (rbt_delete k t) k") (auto simp: rbt_lookup_in_tree rbt_delete_in_tree)
   984   next
   985     assume "x \<noteq> k"
   986     thus ?thesis
   987       by auto (metis is_rbt rbt_delete_is_rbt rbt_delete_in_tree is_rbt_rbt_sorted rbt_lookup_from_in_tree)
   988   qed
   989 qed
   990 
   991 end
   992 
   993 subsection {* Union *}
   994 
   995 context ord begin
   996 
   997 primrec rbt_union_with_key :: "('a \<Rightarrow> 'b \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"
   998 where
   999   "rbt_union_with_key f t Empty = t"
  1000 | "rbt_union_with_key f t (Branch c lt k v rt) = rbt_union_with_key f (rbt_union_with_key f (rbt_insert_with_key f k v t) lt) rt"
  1001 
  1002 definition rbt_union_with where
  1003   "rbt_union_with f = rbt_union_with_key (\<lambda>_. f)"
  1004 
  1005 definition rbt_union where
  1006   "rbt_union = rbt_union_with_key (%_ _ rv. rv)"
  1007 
  1008 end
  1009 
  1010 context linorder begin
  1011 
  1012 lemma rbt_unionwk_rbt_sorted: "rbt_sorted lt \<Longrightarrow> rbt_sorted (rbt_union_with_key f lt rt)" 
  1013   by (induct rt arbitrary: lt) (auto simp: rbt_insertwk_rbt_sorted)
  1014 theorem rbt_unionwk_is_rbt[simp]: "is_rbt lt \<Longrightarrow> is_rbt (rbt_union_with_key f lt rt)" 
  1015   by (induct rt arbitrary: lt) (simp add: rbt_insertwk_is_rbt)+
  1016 
  1017 theorem rbt_unionw_is_rbt: "is_rbt lt \<Longrightarrow> is_rbt (rbt_union_with f lt rt)" unfolding rbt_union_with_def by simp
  1018 
  1019 theorem rbt_union_is_rbt: "is_rbt lt \<Longrightarrow> is_rbt (rbt_union lt rt)" unfolding rbt_union_def by simp
  1020 
  1021 lemma (in ord) rbt_union_Branch[simp]:
  1022   "rbt_union t (Branch c lt k v rt) = rbt_union (rbt_union (rbt_insert k v t) lt) rt"
  1023   unfolding rbt_union_def rbt_insert_def
  1024   by simp
  1025 
  1026 lemma rbt_lookup_rbt_union:
  1027   assumes "is_rbt s" "rbt_sorted t"
  1028   shows "rbt_lookup (rbt_union s t) = rbt_lookup s ++ rbt_lookup t"
  1029 using assms
  1030 proof (induct t arbitrary: s)
  1031   case Empty thus ?case by (auto simp: rbt_union_def)
  1032 next
  1033   case (Branch c l k v r s)
  1034   then have "rbt_sorted r" "rbt_sorted l" "l |\<guillemotleft> k" "k \<guillemotleft>| r" by auto
  1035 
  1036   have meq: "rbt_lookup s(k \<mapsto> v) ++ rbt_lookup l ++ rbt_lookup r =
  1037     rbt_lookup s ++
  1038     (\<lambda>a. if a < k then rbt_lookup l a
  1039     else if k < a then rbt_lookup r a else Some v)" (is "?m1 = ?m2")
  1040   proof (rule ext)
  1041     fix a
  1042 
  1043    have "k < a \<or> k = a \<or> k > a" by auto
  1044     thus "?m1 a = ?m2 a"
  1045     proof (elim disjE)
  1046       assume "k < a"
  1047       with `l |\<guillemotleft> k` have "l |\<guillemotleft> a" by (rule rbt_less_trans)
  1048       with `k < a` show ?thesis
  1049         by (auto simp: map_add_def split: option.splits)
  1050     next
  1051       assume "k = a"
  1052       with `l |\<guillemotleft> k` `k \<guillemotleft>| r` 
  1053       show ?thesis by (auto simp: map_add_def)
  1054     next
  1055       assume "a < k"
  1056       from this `k \<guillemotleft>| r` have "a \<guillemotleft>| r" by (rule rbt_greater_trans)
  1057       with `a < k` show ?thesis
  1058         by (auto simp: map_add_def split: option.splits)
  1059     qed
  1060   qed
  1061 
  1062   from Branch have is_rbt: "is_rbt (RBT_Impl.rbt_union (RBT_Impl.rbt_insert k v s) l)"
  1063     by (auto intro: rbt_union_is_rbt rbt_insert_is_rbt)
  1064   with Branch have IHs:
  1065     "rbt_lookup (rbt_union (rbt_union (rbt_insert k v s) l) r) = rbt_lookup (rbt_union (rbt_insert k v s) l) ++ rbt_lookup r"
  1066     "rbt_lookup (rbt_union (rbt_insert k v s) l) = rbt_lookup (rbt_insert k v s) ++ rbt_lookup l"
  1067     by auto
  1068   
  1069   with meq show ?case
  1070     by (auto simp: rbt_lookup_rbt_insert[OF Branch(3)])
  1071 
  1072 qed
  1073 
  1074 end
  1075 
  1076 subsection {* Modifying existing entries *}
  1077 
  1078 context ord begin
  1079 
  1080 primrec
  1081   rbt_map_entry :: "'a \<Rightarrow> ('b \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) rbt \<Rightarrow> ('a, 'b) rbt"
  1082 where
  1083   "rbt_map_entry k f Empty = Empty"
  1084 | "rbt_map_entry k f (Branch c lt x v rt) =
  1085     (if k < x then Branch c (rbt_map_entry k f lt) x v rt
  1086     else if k > x then (Branch c lt x v (rbt_map_entry k f rt))
  1087     else Branch c lt x (f v) rt)"
  1088 
  1089 
  1090 lemma rbt_map_entry_color_of: "color_of (rbt_map_entry k f t) = color_of t" by (induct t) simp+
  1091 lemma rbt_map_entry_inv1: "inv1 (rbt_map_entry k f t) = inv1 t" by (induct t) (simp add: rbt_map_entry_color_of)+
  1092 lemma rbt_map_entry_inv2: "inv2 (rbt_map_entry k f t) = inv2 t" "bheight (rbt_map_entry k f t) = bheight t" by (induct t) simp+
  1093 lemma rbt_map_entry_rbt_greater: "rbt_greater a (rbt_map_entry k f t) = rbt_greater a t" by (induct t) simp+
  1094 lemma rbt_map_entry_rbt_less: "rbt_less a (rbt_map_entry k f t) = rbt_less a t" by (induct t) simp+
  1095 lemma rbt_map_entry_rbt_sorted: "rbt_sorted (rbt_map_entry k f t) = rbt_sorted t"
  1096   by (induct t) (simp_all add: rbt_map_entry_rbt_less rbt_map_entry_rbt_greater)
  1097 
  1098 theorem rbt_map_entry_is_rbt [simp]: "is_rbt (rbt_map_entry k f t) = is_rbt t" 
  1099 unfolding is_rbt_def by (simp add: rbt_map_entry_inv2 rbt_map_entry_color_of rbt_map_entry_rbt_sorted rbt_map_entry_inv1 )
  1100 
  1101 end
  1102 
  1103 theorem (in linorder) rbt_lookup_rbt_map_entry:
  1104   "rbt_lookup (rbt_map_entry k f t) = (rbt_lookup t)(k := Option.map f (rbt_lookup t k))"
  1105   by (induct t) (auto split: option.splits simp add: fun_eq_iff)
  1106 
  1107 subsection {* Mapping all entries *}
  1108 
  1109 primrec
  1110   map :: "('a \<Rightarrow> 'b \<Rightarrow> 'c) \<Rightarrow> ('a, 'b) rbt \<Rightarrow> ('a, 'c) rbt"
  1111 where
  1112   "map f Empty = Empty"
  1113 | "map f (Branch c lt k v rt) = Branch c (map f lt) k (f k v) (map f rt)"
  1114 
  1115 lemma map_entries [simp]: "entries (map f t) = List.map (\<lambda>(k, v). (k, f k v)) (entries t)"
  1116   by (induct t) auto
  1117 lemma map_keys [simp]: "keys (map f t) = keys t" by (simp add: keys_def split_def)
  1118 lemma map_color_of: "color_of (map f t) = color_of t" by (induct t) simp+
  1119 lemma map_inv1: "inv1 (map f t) = inv1 t" by (induct t) (simp add: map_color_of)+
  1120 lemma map_inv2: "inv2 (map f t) = inv2 t" "bheight (map f t) = bheight t" by (induct t) simp+
  1121 
  1122 context ord begin
  1123 
  1124 lemma map_rbt_greater: "rbt_greater k (map f t) = rbt_greater k t" by (induct t) simp+
  1125 lemma map_rbt_less: "rbt_less k (map f t) = rbt_less k t" by (induct t) simp+
  1126 lemma map_rbt_sorted: "rbt_sorted (map f t) = rbt_sorted t"  by (induct t) (simp add: map_rbt_less map_rbt_greater)+
  1127 theorem map_is_rbt [simp]: "is_rbt (map f t) = is_rbt t" 
  1128 unfolding is_rbt_def by (simp add: map_inv1 map_inv2 map_rbt_sorted map_color_of)
  1129 
  1130 end
  1131 
  1132 theorem (in linorder) rbt_lookup_map: "rbt_lookup (map f t) x = Option.map (f x) (rbt_lookup t x)"
  1133   apply(induct t)
  1134   apply auto
  1135   apply(subgoal_tac "x = a")
  1136   apply auto
  1137   done
  1138  (* FIXME: simproc "antisym less" does not work for linorder context, only for linorder type class
  1139     by (induct t) auto *)
  1140 
  1141 subsection {* Folding over entries *}
  1142 
  1143 definition fold :: "('a \<Rightarrow> 'b \<Rightarrow> 'c \<Rightarrow> 'c) \<Rightarrow> ('a, 'b) rbt \<Rightarrow> 'c \<Rightarrow> 'c" where
  1144   "fold f t = List.fold (prod_case f) (entries t)"
  1145 
  1146 lemma fold_simps [simp, code]:
  1147   "fold f Empty = id"
  1148   "fold f (Branch c lt k v rt) = fold f rt \<circ> f k v \<circ> fold f lt"
  1149   by (simp_all add: fold_def fun_eq_iff)
  1150 
  1151 
  1152 subsection {* Bulkloading a tree *}
  1153 
  1154 definition (in ord) rbt_bulkload :: "('a \<times> 'b) list \<Rightarrow> ('a, 'b) rbt" where
  1155   "rbt_bulkload xs = foldr (\<lambda>(k, v). rbt_insert k v) xs Empty"
  1156 
  1157 context linorder begin
  1158 
  1159 lemma rbt_bulkload_is_rbt [simp, intro]:
  1160   "is_rbt (rbt_bulkload xs)"
  1161   unfolding rbt_bulkload_def by (induct xs) auto
  1162 
  1163 lemma rbt_lookup_rbt_bulkload:
  1164   "rbt_lookup (rbt_bulkload xs) = map_of xs"
  1165 proof -
  1166   obtain ys where "ys = rev xs" by simp
  1167   have "\<And>t. is_rbt t \<Longrightarrow>
  1168     rbt_lookup (List.fold (prod_case rbt_insert) ys t) = rbt_lookup t ++ map_of (rev ys)"
  1169       by (induct ys) (simp_all add: rbt_bulkload_def rbt_lookup_rbt_insert prod_case_beta)
  1170   from this Empty_is_rbt have
  1171     "rbt_lookup (List.fold (prod_case rbt_insert) (rev xs) Empty) = rbt_lookup Empty ++ map_of xs"
  1172      by (simp add: `ys = rev xs`)
  1173   then show ?thesis by (simp add: rbt_bulkload_def rbt_lookup_Empty foldr_conv_fold)
  1174 qed
  1175 
  1176 end
  1177 
  1178 lemmas [code] =
  1179   ord.rbt_less_prop
  1180   ord.rbt_greater_prop
  1181   ord.rbt_sorted.simps
  1182   ord.rbt_lookup.simps
  1183   ord.is_rbt_def
  1184   ord.rbt_ins.simps
  1185   ord.rbt_insert_with_key_def
  1186   ord.rbt_insertw_def
  1187   ord.rbt_insert_def
  1188   ord.rbt_del_from_left.simps
  1189   ord.rbt_del_from_right.simps
  1190   ord.rbt_del.simps
  1191   ord.rbt_delete_def
  1192   ord.rbt_union_with_key.simps
  1193   ord.rbt_union_with_def
  1194   ord.rbt_union_def
  1195   ord.rbt_map_entry.simps
  1196   ord.rbt_bulkload_def
  1197 
  1198 text {* Restore original type constraints for constants *}
  1199 setup {*
  1200   fold Sign.add_const_constraint
  1201     [(@{const_name rbt_less}, SOME @{typ "('a :: order) \<Rightarrow> ('a, 'b) rbt \<Rightarrow> bool"}),
  1202      (@{const_name rbt_greater}, SOME @{typ "('a :: order) \<Rightarrow> ('a, 'b) rbt \<Rightarrow> bool"}),
  1203      (@{const_name rbt_sorted}, SOME @{typ "('a :: linorder, 'b) rbt \<Rightarrow> bool"}),
  1204      (@{const_name rbt_lookup}, SOME @{typ "('a :: linorder, 'b) rbt \<Rightarrow> 'a \<rightharpoonup> 'b"}),
  1205      (@{const_name is_rbt}, SOME @{typ "('a :: linorder, 'b) rbt \<Rightarrow> bool"}),
  1206      (@{const_name rbt_ins}, SOME @{typ "('a\<Colon>linorder \<Rightarrow> 'b \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"}),
  1207      (@{const_name rbt_insert_with_key}, SOME @{typ "('a\<Colon>linorder \<Rightarrow> 'b \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"}),
  1208      (@{const_name rbt_insert_with}, SOME @{typ "('b \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> ('a :: linorder) \<Rightarrow> 'b \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"}),
  1209      (@{const_name rbt_insert}, SOME @{typ "('a :: linorder) \<Rightarrow> 'b \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"}),
  1210      (@{const_name rbt_del_from_left}, SOME @{typ "('a\<Colon>linorder) \<Rightarrow> ('a,'b) rbt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"}),
  1211      (@{const_name rbt_del_from_right}, SOME @{typ "('a\<Colon>linorder) \<Rightarrow> ('a,'b) rbt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"}),
  1212      (@{const_name rbt_del}, SOME @{typ "('a\<Colon>linorder) \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"}),
  1213      (@{const_name rbt_delete}, SOME @{typ "('a\<Colon>linorder) \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"}),
  1214      (@{const_name rbt_union_with_key}, SOME @{typ "('a\<Colon>linorder \<Rightarrow> 'b \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"}),
  1215      (@{const_name rbt_union_with}, SOME @{typ "('b \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> ('a\<Colon>linorder,'b) rbt \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"}),
  1216      (@{const_name rbt_union}, SOME @{typ "('a\<Colon>linorder,'b) rbt \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"}),
  1217      (@{const_name rbt_map_entry}, SOME @{typ "'a\<Colon>linorder \<Rightarrow> ('b \<Rightarrow> 'b) \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"}),
  1218      (@{const_name rbt_bulkload}, SOME @{typ "('a \<times> 'b) list \<Rightarrow> ('a\<Colon>linorder,'b) rbt"})]
  1219 *}
  1220 
  1221 hide_const (open) R B Empty entries keys map fold
  1222 
  1223 end