src/HOL/Library/RBT_Impl.thy
author nipkow
Tue Sep 22 14:31:22 2015 +0200 (2015-09-22)
changeset 61225 1a690dce8cfc
parent 61121 efe8b18306b7
child 61433 a4c0de1df3d8
permissions -rw-r--r--
tuned references
     1 (*  Title:      HOL/Library/RBT_Impl.thy
     2     Author:     Markus Reiter, TU Muenchen
     3     Author:     Alexander Krauss, TU Muenchen
     4 *)
     5 
     6 section \<open>Implementation of Red-Black Trees\<close>
     7 
     8 theory RBT_Impl
     9 imports Main
    10 begin
    11 
    12 text \<open>
    13   For applications, you should use theory @{text RBT} which defines
    14   an abstract type of red-black tree obeying the invariant.
    15 \<close>
    16 
    17 subsection \<open>Datatype of RB trees\<close>
    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 \<open>Tree properties\<close>
    33 
    34 subsubsection \<open>Content of a tree\<close>
    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 lemma non_empty_rbt_keys: 
    66   "t \<noteq> rbt.Empty \<Longrightarrow> keys t \<noteq> []"
    67   by (cases t) simp_all
    68 
    69 subsubsection \<open>Search tree properties\<close>
    70 
    71 context ord begin
    72 
    73 definition rbt_less :: "'a \<Rightarrow> ('a, 'b) rbt \<Rightarrow> bool"
    74 where
    75   rbt_less_prop: "rbt_less k t \<longleftrightarrow> (\<forall>x\<in>set (keys t). x < k)"
    76 
    77 abbreviation rbt_less_symbol (infix "|\<guillemotleft>" 50)
    78 where "t |\<guillemotleft> x \<equiv> rbt_less x t"
    79 
    80 definition rbt_greater :: "'a \<Rightarrow> ('a, 'b) rbt \<Rightarrow> bool" (infix "\<guillemotleft>|" 50) 
    81 where
    82   rbt_greater_prop: "rbt_greater k t = (\<forall>x\<in>set (keys t). k < x)"
    83 
    84 lemma rbt_less_simps [simp]:
    85   "Empty |\<guillemotleft> k = True"
    86   "Branch c lt kt v rt |\<guillemotleft> k \<longleftrightarrow> kt < k \<and> lt |\<guillemotleft> k \<and> rt |\<guillemotleft> k"
    87   by (auto simp add: rbt_less_prop)
    88 
    89 lemma rbt_greater_simps [simp]:
    90   "k \<guillemotleft>| Empty = True"
    91   "k \<guillemotleft>| (Branch c lt kt v rt) \<longleftrightarrow> k < kt \<and> k \<guillemotleft>| lt \<and> k \<guillemotleft>| rt"
    92   by (auto simp add: rbt_greater_prop)
    93 
    94 lemmas rbt_ord_props = rbt_less_prop rbt_greater_prop
    95 
    96 lemmas rbt_greater_nit = rbt_greater_prop entry_in_tree_keys
    97 lemmas rbt_less_nit = rbt_less_prop entry_in_tree_keys
    98 
    99 lemma (in order)
   100   shows rbt_less_eq_trans: "l |\<guillemotleft> u \<Longrightarrow> u \<le> v \<Longrightarrow> l |\<guillemotleft> v"
   101   and rbt_less_trans: "t |\<guillemotleft> x \<Longrightarrow> x < y \<Longrightarrow> t |\<guillemotleft> y"
   102   and rbt_greater_eq_trans: "u \<le> v \<Longrightarrow> v \<guillemotleft>| r \<Longrightarrow> u \<guillemotleft>| r"
   103   and rbt_greater_trans: "x < y \<Longrightarrow> y \<guillemotleft>| t \<Longrightarrow> x \<guillemotleft>| t"
   104   by (auto simp: rbt_ord_props)
   105 
   106 primrec rbt_sorted :: "('a, 'b) rbt \<Rightarrow> bool"
   107 where
   108   "rbt_sorted Empty = True"
   109 | "rbt_sorted (Branch c l k v r) = (l |\<guillemotleft> k \<and> k \<guillemotleft>| r \<and> rbt_sorted l \<and> rbt_sorted r)"
   110 
   111 end
   112 
   113 context linorder begin
   114 
   115 lemma rbt_sorted_entries:
   116   "rbt_sorted t \<Longrightarrow> List.sorted (map fst (entries t))"
   117 by (induct t) 
   118   (force simp: sorted_append sorted_Cons rbt_ord_props 
   119       dest!: entry_in_tree_keys)+
   120 
   121 lemma distinct_entries:
   122   "rbt_sorted t \<Longrightarrow> distinct (map fst (entries t))"
   123 by (induct t) 
   124   (force simp: sorted_append sorted_Cons rbt_ord_props 
   125       dest!: entry_in_tree_keys)+
   126 
   127 lemma distinct_keys:
   128   "rbt_sorted t \<Longrightarrow> distinct (keys t)"
   129   by (simp add: distinct_entries keys_def)
   130 
   131 
   132 subsubsection \<open>Tree lookup\<close>
   133 
   134 primrec (in ord) rbt_lookup :: "('a, 'b) rbt \<Rightarrow> 'a \<rightharpoonup> 'b"
   135 where
   136   "rbt_lookup Empty k = None"
   137 | "rbt_lookup (Branch _ l x y r) k = 
   138    (if k < x then rbt_lookup l k else if x < k then rbt_lookup r k else Some y)"
   139 
   140 lemma rbt_lookup_keys: "rbt_sorted t \<Longrightarrow> dom (rbt_lookup t) = set (keys t)"
   141   by (induct t) (auto simp: dom_def rbt_greater_prop rbt_less_prop)
   142 
   143 lemma dom_rbt_lookup_Branch: 
   144   "rbt_sorted (Branch c t1 k v t2) \<Longrightarrow> 
   145     dom (rbt_lookup (Branch c t1 k v t2)) 
   146     = Set.insert k (dom (rbt_lookup t1) \<union> dom (rbt_lookup t2))"
   147 proof -
   148   assume "rbt_sorted (Branch c t1 k v t2)"
   149   then show ?thesis by (simp add: rbt_lookup_keys)
   150 qed
   151 
   152 lemma finite_dom_rbt_lookup [simp, intro!]: "finite (dom (rbt_lookup t))"
   153 proof (induct t)
   154   case Empty then show ?case by simp
   155 next
   156   case (Branch color t1 a b t2)
   157   let ?A = "Set.insert a (dom (rbt_lookup t1) \<union> dom (rbt_lookup t2))"
   158   have "dom (rbt_lookup (Branch color t1 a b t2)) \<subseteq> ?A" by (auto split: split_if_asm)
   159   moreover from Branch have "finite (insert a (dom (rbt_lookup t1) \<union> dom (rbt_lookup t2)))" by simp
   160   ultimately show ?case by (rule finite_subset)
   161 qed 
   162 
   163 end
   164 
   165 context ord begin
   166 
   167 lemma rbt_lookup_rbt_less[simp]: "t |\<guillemotleft> k \<Longrightarrow> rbt_lookup t k = None" 
   168 by (induct t) auto
   169 
   170 lemma rbt_lookup_rbt_greater[simp]: "k \<guillemotleft>| t \<Longrightarrow> rbt_lookup t k = None"
   171 by (induct t) auto
   172 
   173 lemma rbt_lookup_Empty: "rbt_lookup Empty = empty"
   174 by (rule ext) simp
   175 
   176 end
   177 
   178 context linorder begin
   179 
   180 lemma map_of_entries:
   181   "rbt_sorted t \<Longrightarrow> map_of (entries t) = rbt_lookup t"
   182 proof (induct t)
   183   case Empty thus ?case by (simp add: rbt_lookup_Empty)
   184 next
   185   case (Branch c t1 k v t2)
   186   have "rbt_lookup (Branch c t1 k v t2) = rbt_lookup t2 ++ [k\<mapsto>v] ++ rbt_lookup t1"
   187   proof (rule ext)
   188     fix x
   189     from Branch have RBT_SORTED: "rbt_sorted (Branch c t1 k v t2)" by simp
   190     let ?thesis = "rbt_lookup (Branch c t1 k v t2) x = (rbt_lookup t2 ++ [k \<mapsto> v] ++ rbt_lookup t1) x"
   191 
   192     have DOM_T1: "!!k'. k'\<in>dom (rbt_lookup t1) \<Longrightarrow> k>k'"
   193     proof -
   194       fix k'
   195       from RBT_SORTED have "t1 |\<guillemotleft> k" by simp
   196       with rbt_less_prop have "\<forall>k'\<in>set (keys t1). k>k'" by auto
   197       moreover assume "k'\<in>dom (rbt_lookup t1)"
   198       ultimately show "k>k'" using rbt_lookup_keys RBT_SORTED by auto
   199     qed
   200     
   201     have DOM_T2: "!!k'. k'\<in>dom (rbt_lookup t2) \<Longrightarrow> k<k'"
   202     proof -
   203       fix k'
   204       from RBT_SORTED have "k \<guillemotleft>| t2" by simp
   205       with rbt_greater_prop have "\<forall>k'\<in>set (keys t2). k<k'" by auto
   206       moreover assume "k'\<in>dom (rbt_lookup t2)"
   207       ultimately show "k<k'" using rbt_lookup_keys RBT_SORTED by auto
   208     qed
   209     
   210     {
   211       assume C: "x<k"
   212       hence "rbt_lookup (Branch c t1 k v t2) x = rbt_lookup t1 x" by simp
   213       moreover from C have "x\<notin>dom [k\<mapsto>v]" by simp
   214       moreover have "x \<notin> dom (rbt_lookup t2)"
   215       proof
   216         assume "x \<in> dom (rbt_lookup t2)"
   217         with DOM_T2 have "k<x" by blast
   218         with C show False by simp
   219       qed
   220       ultimately have ?thesis by (simp add: map_add_upd_left map_add_dom_app_simps)
   221     } moreover {
   222       assume [simp]: "x=k"
   223       hence "rbt_lookup (Branch c t1 k v t2) x = [k \<mapsto> v] x" by simp
   224       moreover have "x \<notin> dom (rbt_lookup t1)" 
   225       proof
   226         assume "x \<in> dom (rbt_lookup t1)"
   227         with DOM_T1 have "k>x" by blast
   228         thus False by simp
   229       qed
   230       ultimately have ?thesis by (simp add: map_add_upd_left map_add_dom_app_simps)
   231     } moreover {
   232       assume C: "x>k"
   233       hence "rbt_lookup (Branch c t1 k v t2) x = rbt_lookup t2 x" by (simp add: less_not_sym[of k x])
   234       moreover from C have "x\<notin>dom [k\<mapsto>v]" by simp
   235       moreover have "x\<notin>dom (rbt_lookup t1)" proof
   236         assume "x\<in>dom (rbt_lookup t1)"
   237         with DOM_T1 have "k>x" by simp
   238         with C show False by simp
   239       qed
   240       ultimately have ?thesis by (simp add: map_add_upd_left map_add_dom_app_simps)
   241     } ultimately show ?thesis using less_linear by blast
   242   qed
   243   also from Branch 
   244   have "rbt_lookup t2 ++ [k \<mapsto> v] ++ rbt_lookup t1 = map_of (entries (Branch c t1 k v t2))" by simp
   245   finally show ?case by simp
   246 qed
   247 
   248 lemma rbt_lookup_in_tree: "rbt_sorted t \<Longrightarrow> rbt_lookup t k = Some v \<longleftrightarrow> (k, v) \<in> set (entries t)"
   249   by (simp add: map_of_entries [symmetric] distinct_entries)
   250 
   251 lemma set_entries_inject:
   252   assumes rbt_sorted: "rbt_sorted t1" "rbt_sorted t2" 
   253   shows "set (entries t1) = set (entries t2) \<longleftrightarrow> entries t1 = entries t2"
   254 proof -
   255   from rbt_sorted have "distinct (map fst (entries t1))"
   256     "distinct (map fst (entries t2))"
   257     by (auto intro: distinct_entries)
   258   with rbt_sorted show ?thesis
   259     by (auto intro: map_sorted_distinct_set_unique rbt_sorted_entries simp add: distinct_map)
   260 qed
   261 
   262 lemma entries_eqI:
   263   assumes rbt_sorted: "rbt_sorted t1" "rbt_sorted t2" 
   264   assumes rbt_lookup: "rbt_lookup t1 = rbt_lookup t2"
   265   shows "entries t1 = entries t2"
   266 proof -
   267   from rbt_sorted rbt_lookup have "map_of (entries t1) = map_of (entries t2)"
   268     by (simp add: map_of_entries)
   269   with rbt_sorted have "set (entries t1) = set (entries t2)"
   270     by (simp add: map_of_inject_set distinct_entries)
   271   with rbt_sorted show ?thesis by (simp add: set_entries_inject)
   272 qed
   273 
   274 lemma entries_rbt_lookup:
   275   assumes "rbt_sorted t1" "rbt_sorted t2" 
   276   shows "entries t1 = entries t2 \<longleftrightarrow> rbt_lookup t1 = rbt_lookup t2"
   277   using assms by (auto intro: entries_eqI simp add: map_of_entries [symmetric])
   278 
   279 lemma rbt_lookup_from_in_tree: 
   280   assumes "rbt_sorted t1" "rbt_sorted t2" 
   281   and "\<And>v. (k, v) \<in> set (entries t1) \<longleftrightarrow> (k, v) \<in> set (entries t2)" 
   282   shows "rbt_lookup t1 k = rbt_lookup t2 k"
   283 proof -
   284   from assms have "k \<in> dom (rbt_lookup t1) \<longleftrightarrow> k \<in> dom (rbt_lookup t2)"
   285     by (simp add: keys_entries rbt_lookup_keys)
   286   with assms show ?thesis by (auto simp add: rbt_lookup_in_tree [symmetric])
   287 qed
   288 
   289 end
   290 
   291 subsubsection \<open>Red-black properties\<close>
   292 
   293 primrec color_of :: "('a, 'b) rbt \<Rightarrow> color"
   294 where
   295   "color_of Empty = B"
   296 | "color_of (Branch c _ _ _ _) = c"
   297 
   298 primrec bheight :: "('a,'b) rbt \<Rightarrow> nat"
   299 where
   300   "bheight Empty = 0"
   301 | "bheight (Branch c lt k v rt) = (if c = B then Suc (bheight lt) else bheight lt)"
   302 
   303 primrec inv1 :: "('a, 'b) rbt \<Rightarrow> bool"
   304 where
   305   "inv1 Empty = True"
   306 | "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)"
   307 
   308 primrec inv1l :: "('a, 'b) rbt \<Rightarrow> bool" -- \<open>Weaker version\<close>
   309 where
   310   "inv1l Empty = True"
   311 | "inv1l (Branch c l k v r) = (inv1 l \<and> inv1 r)"
   312 lemma [simp]: "inv1 t \<Longrightarrow> inv1l t" by (cases t) simp+
   313 
   314 primrec inv2 :: "('a, 'b) rbt \<Rightarrow> bool"
   315 where
   316   "inv2 Empty = True"
   317 | "inv2 (Branch c lt k v rt) = (inv2 lt \<and> inv2 rt \<and> bheight lt = bheight rt)"
   318 
   319 context ord begin
   320 
   321 definition is_rbt :: "('a, 'b) rbt \<Rightarrow> bool" where
   322   "is_rbt t \<longleftrightarrow> inv1 t \<and> inv2 t \<and> color_of t = B \<and> rbt_sorted t"
   323 
   324 lemma is_rbt_rbt_sorted [simp]:
   325   "is_rbt t \<Longrightarrow> rbt_sorted t" by (simp add: is_rbt_def)
   326 
   327 theorem Empty_is_rbt [simp]:
   328   "is_rbt Empty" by (simp add: is_rbt_def)
   329 
   330 end
   331 
   332 subsection \<open>Insertion\<close>
   333 
   334 text \<open>The function definitions are based on the book by Okasaki.\<close>
   335 
   336 fun (* slow, due to massive case splitting *)
   337   balance :: "('a,'b) rbt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"
   338 where
   339   "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)" |
   340   "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)" |
   341   "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)" |
   342   "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)" |
   343   "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)" |
   344   "balance a s t b = Branch B a s t b"
   345 
   346 lemma balance_inv1: "\<lbrakk>inv1l l; inv1l r\<rbrakk> \<Longrightarrow> inv1 (balance l k v r)" 
   347   by (induct l k v r rule: balance.induct) auto
   348 
   349 lemma balance_bheight: "bheight l = bheight r \<Longrightarrow> bheight (balance l k v r) = Suc (bheight l)"
   350   by (induct l k v r rule: balance.induct) auto
   351 
   352 lemma balance_inv2: 
   353   assumes "inv2 l" "inv2 r" "bheight l = bheight r"
   354   shows "inv2 (balance l k v r)"
   355   using assms
   356   by (induct l k v r rule: balance.induct) auto
   357 
   358 context ord begin
   359 
   360 lemma balance_rbt_greater[simp]: "(v \<guillemotleft>| balance a k x b) = (v \<guillemotleft>| a \<and> v \<guillemotleft>| b \<and> v < k)" 
   361   by (induct a k x b rule: balance.induct) auto
   362 
   363 lemma balance_rbt_less[simp]: "(balance a k x b |\<guillemotleft> v) = (a |\<guillemotleft> v \<and> b |\<guillemotleft> v \<and> k < v)"
   364   by (induct a k x b rule: balance.induct) auto
   365 
   366 end
   367 
   368 lemma (in linorder) balance_rbt_sorted: 
   369   fixes k :: "'a"
   370   assumes "rbt_sorted l" "rbt_sorted r" "l |\<guillemotleft> k" "k \<guillemotleft>| r"
   371   shows "rbt_sorted (balance l k v r)"
   372 using assms proof (induct l k v r rule: balance.induct)
   373   case ("2_2" a x w b y t c z s va vb vd vc)
   374   hence "y < z \<and> z \<guillemotleft>| Branch B va vb vd vc" 
   375     by (auto simp add: rbt_ord_props)
   376   hence "y \<guillemotleft>| (Branch B va vb vd vc)" by (blast dest: rbt_greater_trans)
   377   with "2_2" show ?case by simp
   378 next
   379   case ("3_2" va vb vd vc x w b y s c z)
   380   from "3_2" have "x < y \<and> Branch B va vb vd vc |\<guillemotleft> x" 
   381     by simp
   382   hence "Branch B va vb vd vc |\<guillemotleft> y" by (blast dest: rbt_less_trans)
   383   with "3_2" show ?case by simp
   384 next
   385   case ("3_3" x w b y s c z t va vb vd vc)
   386   from "3_3" have "y < z \<and> z \<guillemotleft>| Branch B va vb vd vc" by simp
   387   hence "y \<guillemotleft>| Branch B va vb vd vc" by (blast dest: rbt_greater_trans)
   388   with "3_3" show ?case by simp
   389 next
   390   case ("3_4" vd ve vg vf x w b y s c z t va vb vii vc)
   391   hence "x < y \<and> Branch B vd ve vg vf |\<guillemotleft> x" by simp
   392   hence 1: "Branch B vd ve vg vf |\<guillemotleft> y" by (blast dest: rbt_less_trans)
   393   from "3_4" have "y < z \<and> z \<guillemotleft>| Branch B va vb vii vc" by simp
   394   hence "y \<guillemotleft>| Branch B va vb vii vc" by (blast dest: rbt_greater_trans)
   395   with 1 "3_4" show ?case by simp
   396 next
   397   case ("4_2" va vb vd vc x w b y s c z t dd)
   398   hence "x < y \<and> Branch B va vb vd vc |\<guillemotleft> x" by simp
   399   hence "Branch B va vb vd vc |\<guillemotleft> y" by (blast dest: rbt_less_trans)
   400   with "4_2" show ?case by simp
   401 next
   402   case ("5_2" x w b y s c z t va vb vd vc)
   403   hence "y < z \<and> z \<guillemotleft>| Branch B va vb vd vc" by simp
   404   hence "y \<guillemotleft>| Branch B va vb vd vc" by (blast dest: rbt_greater_trans)
   405   with "5_2" show ?case by simp
   406 next
   407   case ("5_3" va vb vd vc x w b y s c z t)
   408   hence "x < y \<and> Branch B va vb vd vc |\<guillemotleft> x" by simp
   409   hence "Branch B va vb vd vc |\<guillemotleft> y" by (blast dest: rbt_less_trans)
   410   with "5_3" show ?case by simp
   411 next
   412   case ("5_4" va vb vg vc x w b y s c z t vd ve vii vf)
   413   hence "x < y \<and> Branch B va vb vg vc |\<guillemotleft> x" by simp
   414   hence 1: "Branch B va vb vg vc |\<guillemotleft> y" by (blast dest: rbt_less_trans)
   415   from "5_4" have "y < z \<and> z \<guillemotleft>| Branch B vd ve vii vf" by simp
   416   hence "y \<guillemotleft>| Branch B vd ve vii vf" by (blast dest: rbt_greater_trans)
   417   with 1 "5_4" show ?case by simp
   418 qed simp+
   419 
   420 lemma entries_balance [simp]:
   421   "entries (balance l k v r) = entries l @ (k, v) # entries r"
   422   by (induct l k v r rule: balance.induct) auto
   423 
   424 lemma keys_balance [simp]: 
   425   "keys (balance l k v r) = keys l @ k # keys r"
   426   by (simp add: keys_def)
   427 
   428 lemma balance_in_tree:  
   429   "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"
   430   by (auto simp add: keys_def)
   431 
   432 lemma (in linorder) rbt_lookup_balance[simp]: 
   433 fixes k :: "'a"
   434 assumes "rbt_sorted l" "rbt_sorted r" "l |\<guillemotleft> k" "k \<guillemotleft>| r"
   435 shows "rbt_lookup (balance l k v r) x = rbt_lookup (Branch B l k v r) x"
   436 by (rule rbt_lookup_from_in_tree) (auto simp:assms balance_in_tree balance_rbt_sorted)
   437 
   438 primrec paint :: "color \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"
   439 where
   440   "paint c Empty = Empty"
   441 | "paint c (Branch _ l k v r) = Branch c l k v r"
   442 
   443 lemma paint_inv1l[simp]: "inv1l t \<Longrightarrow> inv1l (paint c t)" by (cases t) auto
   444 lemma paint_inv1[simp]: "inv1l t \<Longrightarrow> inv1 (paint B t)" by (cases t) auto
   445 lemma paint_inv2[simp]: "inv2 t \<Longrightarrow> inv2 (paint c t)" by (cases t) auto
   446 lemma paint_color_of[simp]: "color_of (paint B t) = B" by (cases t) auto
   447 lemma paint_in_tree[simp]: "entry_in_tree k x (paint c t) = entry_in_tree k x t" by (cases t) auto
   448 
   449 context ord begin
   450 
   451 lemma paint_rbt_sorted[simp]: "rbt_sorted t \<Longrightarrow> rbt_sorted (paint c t)" by (cases t) auto
   452 lemma paint_rbt_lookup[simp]: "rbt_lookup (paint c t) = rbt_lookup t" by (rule ext) (cases t, auto)
   453 lemma paint_rbt_greater[simp]: "(v \<guillemotleft>| paint c t) = (v \<guillemotleft>| t)" by (cases t) auto
   454 lemma paint_rbt_less[simp]: "(paint c t |\<guillemotleft> v) = (t |\<guillemotleft> v)" by (cases t) auto
   455 
   456 fun
   457   rbt_ins :: "('a \<Rightarrow> 'b \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"
   458 where
   459   "rbt_ins f k v Empty = Branch R Empty k v Empty" |
   460   "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
   461                                        else if k > x then balance l x y (rbt_ins f k v r)
   462                                        else Branch B l x (f k y v) r)" |
   463   "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
   464                                        else if k > x then Branch R l x y (rbt_ins f k v r)
   465                                        else Branch R l x (f k y v) r)"
   466 
   467 lemma ins_inv1_inv2: 
   468   assumes "inv1 t" "inv2 t"
   469   shows "inv2 (rbt_ins f k x t)" "bheight (rbt_ins f k x t) = bheight t" 
   470   "color_of t = B \<Longrightarrow> inv1 (rbt_ins f k x t)" "inv1l (rbt_ins f k x t)"
   471   using assms
   472   by (induct f k x t rule: rbt_ins.induct) (auto simp: balance_inv1 balance_inv2 balance_bheight)
   473 
   474 end
   475 
   476 context linorder begin
   477 
   478 lemma ins_rbt_greater[simp]: "(v \<guillemotleft>| rbt_ins f (k :: 'a) x t) = (v \<guillemotleft>| t \<and> k > v)"
   479   by (induct f k x t rule: rbt_ins.induct) auto
   480 lemma ins_rbt_less[simp]: "(rbt_ins f k x t |\<guillemotleft> v) = (t |\<guillemotleft> v \<and> k < v)"
   481   by (induct f k x t rule: rbt_ins.induct) auto
   482 lemma ins_rbt_sorted[simp]: "rbt_sorted t \<Longrightarrow> rbt_sorted (rbt_ins f k x t)"
   483   by (induct f k x t rule: rbt_ins.induct) (auto simp: balance_rbt_sorted)
   484 
   485 lemma keys_ins: "set (keys (rbt_ins f k v t)) = { k } \<union> set (keys t)"
   486   by (induct f k v t rule: rbt_ins.induct) auto
   487 
   488 lemma rbt_lookup_ins: 
   489   fixes k :: "'a"
   490   assumes "rbt_sorted t"
   491   shows "rbt_lookup (rbt_ins f k v t) x = ((rbt_lookup t)(k |-> case rbt_lookup t k of None \<Rightarrow> v 
   492                                                                 | Some w \<Rightarrow> f k w v)) x"
   493 using assms by (induct f k v t rule: rbt_ins.induct) auto
   494 
   495 end
   496 
   497 context ord begin
   498 
   499 definition rbt_insert_with_key :: "('a \<Rightarrow> 'b \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"
   500 where "rbt_insert_with_key f k v t = paint B (rbt_ins f k v t)"
   501 
   502 definition rbt_insertw_def: "rbt_insert_with f = rbt_insert_with_key (\<lambda>_. f)"
   503 
   504 definition rbt_insert :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) rbt \<Rightarrow> ('a, 'b) rbt" where
   505   "rbt_insert = rbt_insert_with_key (\<lambda>_ _ nv. nv)"
   506 
   507 end
   508 
   509 context linorder begin
   510 
   511 lemma rbt_insertwk_rbt_sorted: "rbt_sorted t \<Longrightarrow> rbt_sorted (rbt_insert_with_key f (k :: 'a) x t)"
   512   by (auto simp: rbt_insert_with_key_def)
   513 
   514 theorem rbt_insertwk_is_rbt: 
   515   assumes inv: "is_rbt t" 
   516   shows "is_rbt (rbt_insert_with_key f k x t)"
   517 using assms
   518 unfolding rbt_insert_with_key_def is_rbt_def
   519 by (auto simp: ins_inv1_inv2)
   520 
   521 lemma rbt_lookup_rbt_insertwk: 
   522   assumes "rbt_sorted t"
   523   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 
   524                                                        | Some w \<Rightarrow> f k w v)) x"
   525 unfolding rbt_insert_with_key_def using assms
   526 by (simp add:rbt_lookup_ins)
   527 
   528 lemma rbt_insertw_rbt_sorted: "rbt_sorted t \<Longrightarrow> rbt_sorted (rbt_insert_with f k v t)" 
   529   by (simp add: rbt_insertwk_rbt_sorted rbt_insertw_def)
   530 theorem rbt_insertw_is_rbt: "is_rbt t \<Longrightarrow> is_rbt (rbt_insert_with f k v t)"
   531   by (simp add: rbt_insertwk_is_rbt rbt_insertw_def)
   532 
   533 lemma rbt_lookup_rbt_insertw:
   534   assumes "is_rbt t"
   535   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))"
   536 using assms
   537 unfolding rbt_insertw_def
   538 by (rule_tac ext) (cases "rbt_lookup t k", auto simp:rbt_lookup_rbt_insertwk dom_def)
   539 
   540 lemma rbt_insert_rbt_sorted: "rbt_sorted t \<Longrightarrow> rbt_sorted (rbt_insert k v t)"
   541   by (simp add: rbt_insertwk_rbt_sorted rbt_insert_def)
   542 theorem rbt_insert_is_rbt [simp]: "is_rbt t \<Longrightarrow> is_rbt (rbt_insert k v t)"
   543   by (simp add: rbt_insertwk_is_rbt rbt_insert_def)
   544 
   545 lemma rbt_lookup_rbt_insert: 
   546   assumes "is_rbt t"
   547   shows "rbt_lookup (rbt_insert k v t) = (rbt_lookup t)(k\<mapsto>v)"
   548 unfolding rbt_insert_def
   549 using assms
   550 by (rule_tac ext) (simp add: rbt_lookup_rbt_insertwk split:option.split)
   551 
   552 end
   553 
   554 subsection \<open>Deletion\<close>
   555 
   556 lemma bheight_paintR'[simp]: "color_of t = B \<Longrightarrow> bheight (paint R t) = bheight t - 1"
   557 by (cases t rule: rbt_cases) auto
   558 
   559 text \<open>The function definitions are based on the Haskell code by Stefan Kahrs
   560 at @{url "http://www.cs.ukc.ac.uk/people/staff/smk/redblack/rb.html"} .\<close>
   561 
   562 fun
   563   balance_left :: "('a,'b) rbt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"
   564 where
   565   "balance_left (Branch R a k x b) s y c = Branch R (Branch B a k x b) s y c" |
   566   "balance_left bl k x (Branch B a s y b) = balance bl k x (Branch R a s y b)" |
   567   "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))" |
   568   "balance_left t k x s = Empty"
   569 
   570 lemma balance_left_inv2_with_inv1:
   571   assumes "inv2 lt" "inv2 rt" "bheight lt + 1 = bheight rt" "inv1 rt"
   572   shows "bheight (balance_left lt k v rt) = bheight lt + 1"
   573   and   "inv2 (balance_left lt k v rt)"
   574 using assms 
   575 by (induct lt k v rt rule: balance_left.induct) (auto simp: balance_inv2 balance_bheight)
   576 
   577 lemma balance_left_inv2_app: 
   578   assumes "inv2 lt" "inv2 rt" "bheight lt + 1 = bheight rt" "color_of rt = B"
   579   shows "inv2 (balance_left lt k v rt)" 
   580         "bheight (balance_left lt k v rt) = bheight rt"
   581 using assms 
   582 by (induct lt k v rt rule: balance_left.induct) (auto simp add: balance_inv2 balance_bheight)+ 
   583 
   584 lemma balance_left_inv1: "\<lbrakk>inv1l a; inv1 b; color_of b = B\<rbrakk> \<Longrightarrow> inv1 (balance_left a k x b)"
   585   by (induct a k x b rule: balance_left.induct) (simp add: balance_inv1)+
   586 
   587 lemma balance_left_inv1l: "\<lbrakk> inv1l lt; inv1 rt \<rbrakk> \<Longrightarrow> inv1l (balance_left lt k x rt)"
   588 by (induct lt k x rt rule: balance_left.induct) (auto simp: balance_inv1)
   589 
   590 lemma (in linorder) balance_left_rbt_sorted: 
   591   "\<lbrakk> rbt_sorted l; rbt_sorted r; rbt_less k l; k \<guillemotleft>| r \<rbrakk> \<Longrightarrow> rbt_sorted (balance_left l k v r)"
   592 apply (induct l k v r rule: balance_left.induct)
   593 apply (auto simp: balance_rbt_sorted)
   594 apply (unfold rbt_greater_prop rbt_less_prop)
   595 by force+
   596 
   597 context order begin
   598 
   599 lemma balance_left_rbt_greater: 
   600   fixes k :: "'a"
   601   assumes "k \<guillemotleft>| a" "k \<guillemotleft>| b" "k < x" 
   602   shows "k \<guillemotleft>| balance_left a x t b"
   603 using assms 
   604 by (induct a x t b rule: balance_left.induct) auto
   605 
   606 lemma balance_left_rbt_less: 
   607   fixes k :: "'a"
   608   assumes "a |\<guillemotleft> k" "b |\<guillemotleft> k" "x < k" 
   609   shows "balance_left a x t b |\<guillemotleft> k"
   610 using assms
   611 by (induct a x t b rule: balance_left.induct) auto
   612 
   613 end
   614 
   615 lemma balance_left_in_tree: 
   616   assumes "inv1l l" "inv1 r" "bheight l + 1 = bheight r"
   617   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)"
   618 using assms 
   619 by (induct l k v r rule: balance_left.induct) (auto simp: balance_in_tree)
   620 
   621 fun
   622   balance_right :: "('a,'b) rbt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"
   623 where
   624   "balance_right a k x (Branch R b s y c) = Branch R a k x (Branch B b s y c)" |
   625   "balance_right (Branch B a k x b) s y bl = balance (Branch R a k x b) s y bl" |
   626   "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)" |
   627   "balance_right t k x s = Empty"
   628 
   629 lemma balance_right_inv2_with_inv1:
   630   assumes "inv2 lt" "inv2 rt" "bheight lt = bheight rt + 1" "inv1 lt"
   631   shows "inv2 (balance_right lt k v rt) \<and> bheight (balance_right lt k v rt) = bheight lt"
   632 using assms
   633 by (induct lt k v rt rule: balance_right.induct) (auto simp: balance_inv2 balance_bheight)
   634 
   635 lemma balance_right_inv1: "\<lbrakk>inv1 a; inv1l b; color_of a = B\<rbrakk> \<Longrightarrow> inv1 (balance_right a k x b)"
   636 by (induct a k x b rule: balance_right.induct) (simp add: balance_inv1)+
   637 
   638 lemma balance_right_inv1l: "\<lbrakk> inv1 lt; inv1l rt \<rbrakk> \<Longrightarrow>inv1l (balance_right lt k x rt)"
   639 by (induct lt k x rt rule: balance_right.induct) (auto simp: balance_inv1)
   640 
   641 lemma (in linorder) balance_right_rbt_sorted:
   642   "\<lbrakk> rbt_sorted l; rbt_sorted r; rbt_less k l; k \<guillemotleft>| r \<rbrakk> \<Longrightarrow> rbt_sorted (balance_right l k v r)"
   643 apply (induct l k v r rule: balance_right.induct)
   644 apply (auto simp:balance_rbt_sorted)
   645 apply (unfold rbt_less_prop rbt_greater_prop)
   646 by force+
   647 
   648 context order begin
   649 
   650 lemma balance_right_rbt_greater: 
   651   fixes k :: "'a"
   652   assumes "k \<guillemotleft>| a" "k \<guillemotleft>| b" "k < x" 
   653   shows "k \<guillemotleft>| balance_right a x t b"
   654 using assms by (induct a x t b rule: balance_right.induct) auto
   655 
   656 lemma balance_right_rbt_less: 
   657   fixes k :: "'a"
   658   assumes "a |\<guillemotleft> k" "b |\<guillemotleft> k" "x < k" 
   659   shows "balance_right a x t b |\<guillemotleft> k"
   660 using assms by (induct a x t b rule: balance_right.induct) auto
   661 
   662 end
   663 
   664 lemma balance_right_in_tree:
   665   assumes "inv1 l" "inv1l r" "bheight l = bheight r + 1" "inv2 l" "inv2 r"
   666   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)"
   667 using assms by (induct l k v r rule: balance_right.induct) (auto simp: balance_in_tree)
   668 
   669 fun
   670   combine :: "('a,'b) rbt \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"
   671 where
   672   "combine Empty x = x" 
   673 | "combine x Empty = x" 
   674 | "combine (Branch R a k x b) (Branch R c s y d) = (case (combine b c) of
   675                                     Branch R b2 t z c2 \<Rightarrow> (Branch R (Branch R a k x b2) t z (Branch R c2 s y d)) |
   676                                     bc \<Rightarrow> Branch R a k x (Branch R bc s y d))" 
   677 | "combine (Branch B a k x b) (Branch B c s y d) = (case (combine b c) of
   678                                     Branch R b2 t z c2 \<Rightarrow> Branch R (Branch B a k x b2) t z (Branch B c2 s y d) |
   679                                     bc \<Rightarrow> balance_left a k x (Branch B bc s y d))" 
   680 | "combine a (Branch R b k x c) = Branch R (combine a b) k x c" 
   681 | "combine (Branch R a k x b) c = Branch R a k x (combine b c)" 
   682 
   683 lemma combine_inv2:
   684   assumes "inv2 lt" "inv2 rt" "bheight lt = bheight rt"
   685   shows "bheight (combine lt rt) = bheight lt" "inv2 (combine lt rt)"
   686 using assms 
   687 by (induct lt rt rule: combine.induct) 
   688    (auto simp: balance_left_inv2_app split: rbt.splits color.splits)
   689 
   690 lemma combine_inv1: 
   691   assumes "inv1 lt" "inv1 rt"
   692   shows "color_of lt = B \<Longrightarrow> color_of rt = B \<Longrightarrow> inv1 (combine lt rt)"
   693          "inv1l (combine lt rt)"
   694 using assms 
   695 by (induct lt rt rule: combine.induct)
   696    (auto simp: balance_left_inv1 split: rbt.splits color.splits)
   697 
   698 context linorder begin
   699 
   700 lemma combine_rbt_greater[simp]: 
   701   fixes k :: "'a"
   702   assumes "k \<guillemotleft>| l" "k \<guillemotleft>| r" 
   703   shows "k \<guillemotleft>| combine l r"
   704 using assms 
   705 by (induct l r rule: combine.induct)
   706    (auto simp: balance_left_rbt_greater split:rbt.splits color.splits)
   707 
   708 lemma combine_rbt_less[simp]: 
   709   fixes k :: "'a"
   710   assumes "l |\<guillemotleft> k" "r |\<guillemotleft> k" 
   711   shows "combine l r |\<guillemotleft> k"
   712 using assms 
   713 by (induct l r rule: combine.induct)
   714    (auto simp: balance_left_rbt_less split:rbt.splits color.splits)
   715 
   716 lemma combine_rbt_sorted: 
   717   fixes k :: "'a"
   718   assumes "rbt_sorted l" "rbt_sorted r" "l |\<guillemotleft> k" "k \<guillemotleft>| r"
   719   shows "rbt_sorted (combine l r)"
   720 using assms proof (induct l r rule: combine.induct)
   721   case (3 a x v b c y w d)
   722   hence ineqs: "a |\<guillemotleft> x" "x \<guillemotleft>| b" "b |\<guillemotleft> k" "k \<guillemotleft>| c" "c |\<guillemotleft> y" "y \<guillemotleft>| d"
   723     by auto
   724   with 3
   725   show ?case
   726     by (cases "combine b c" rule: rbt_cases)
   727       (auto, (metis combine_rbt_greater combine_rbt_less ineqs ineqs rbt_less_simps(2) rbt_greater_simps(2) rbt_greater_trans rbt_less_trans)+)
   728 next
   729   case (4 a x v b c y w d)
   730   hence "x < k \<and> rbt_greater k c" by simp
   731   hence "rbt_greater x c" by (blast dest: rbt_greater_trans)
   732   with 4 have 2: "rbt_greater x (combine b c)" by (simp add: combine_rbt_greater)
   733   from 4 have "k < y \<and> rbt_less k b" by simp
   734   hence "rbt_less y b" by (blast dest: rbt_less_trans)
   735   with 4 have 3: "rbt_less y (combine b c)" by (simp add: combine_rbt_less)
   736   show ?case
   737   proof (cases "combine b c" rule: rbt_cases)
   738     case Empty
   739     from 4 have "x < y \<and> rbt_greater y d" by auto
   740     hence "rbt_greater x d" by (blast dest: rbt_greater_trans)
   741     with 4 Empty have "rbt_sorted a" and "rbt_sorted (Branch B Empty y w d)"
   742       and "rbt_less x a" and "rbt_greater x (Branch B Empty y w d)" by auto
   743     with Empty show ?thesis by (simp add: balance_left_rbt_sorted)
   744   next
   745     case (Red lta va ka rta)
   746     with 2 4 have "x < va \<and> rbt_less x a" by simp
   747     hence 5: "rbt_less va a" by (blast dest: rbt_less_trans)
   748     from Red 3 4 have "va < y \<and> rbt_greater y d" by simp
   749     hence "rbt_greater va d" by (blast dest: rbt_greater_trans)
   750     with Red 2 3 4 5 show ?thesis by simp
   751   next
   752     case (Black lta va ka rta)
   753     from 4 have "x < y \<and> rbt_greater y d" by auto
   754     hence "rbt_greater x d" by (blast dest: rbt_greater_trans)
   755     with Black 2 3 4 have "rbt_sorted a" and "rbt_sorted (Branch B (combine b c) y w d)" 
   756       and "rbt_less x a" and "rbt_greater x (Branch B (combine b c) y w d)" by auto
   757     with Black show ?thesis by (simp add: balance_left_rbt_sorted)
   758   qed
   759 next
   760   case (5 va vb vd vc b x w c)
   761   hence "k < x \<and> rbt_less k (Branch B va vb vd vc)" by simp
   762   hence "rbt_less x (Branch B va vb vd vc)" by (blast dest: rbt_less_trans)
   763   with 5 show ?case by (simp add: combine_rbt_less)
   764 next
   765   case (6 a x v b va vb vd vc)
   766   hence "x < k \<and> rbt_greater k (Branch B va vb vd vc)" by simp
   767   hence "rbt_greater x (Branch B va vb vd vc)" by (blast dest: rbt_greater_trans)
   768   with 6 show ?case by (simp add: combine_rbt_greater)
   769 qed simp+
   770 
   771 end
   772 
   773 lemma combine_in_tree: 
   774   assumes "inv2 l" "inv2 r" "bheight l = bheight r" "inv1 l" "inv1 r"
   775   shows "entry_in_tree k v (combine l r) = (entry_in_tree k v l \<or> entry_in_tree k v r)"
   776 using assms 
   777 proof (induct l r rule: combine.induct)
   778   case (4 _ _ _ b c)
   779   hence a: "bheight (combine b c) = bheight b" by (simp add: combine_inv2)
   780   from 4 have b: "inv1l (combine b c)" by (simp add: combine_inv1)
   781 
   782   show ?case
   783   proof (cases "combine b c" rule: rbt_cases)
   784     case Empty
   785     with 4 a show ?thesis by (auto simp: balance_left_in_tree)
   786   next
   787     case (Red lta ka va rta)
   788     with 4 show ?thesis by auto
   789   next
   790     case (Black lta ka va rta)
   791     with a b 4  show ?thesis by (auto simp: balance_left_in_tree)
   792   qed 
   793 qed (auto split: rbt.splits color.splits)
   794 
   795 context ord begin
   796 
   797 fun
   798   rbt_del_from_left :: "'a \<Rightarrow> ('a,'b) rbt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt" and
   799   rbt_del_from_right :: "'a \<Rightarrow> ('a,'b) rbt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt" and
   800   rbt_del :: "'a\<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"
   801 where
   802   "rbt_del x Empty = Empty" |
   803   "rbt_del x (Branch c a y s b) = 
   804    (if x < y then rbt_del_from_left x a y s b 
   805     else (if x > y then rbt_del_from_right x a y s b else combine a b))" |
   806   "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" |
   807   "rbt_del_from_left x a y s b = Branch R (rbt_del x a) y s b" |
   808   "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))" | 
   809   "rbt_del_from_right x a y s b = Branch R a y s (rbt_del x b)"
   810 
   811 end
   812 
   813 context linorder begin
   814 
   815 lemma 
   816   assumes "inv2 lt" "inv1 lt"
   817   shows
   818   "\<lbrakk>inv2 rt; bheight lt = bheight rt; inv1 rt\<rbrakk> \<Longrightarrow>
   819    inv2 (rbt_del_from_left x lt k v rt) \<and> 
   820    bheight (rbt_del_from_left x lt k v rt) = bheight lt \<and> 
   821    (color_of lt = B \<and> color_of rt = B \<and> inv1 (rbt_del_from_left x lt k v rt) \<or> 
   822     (color_of lt \<noteq> B \<or> color_of rt \<noteq> B) \<and> inv1l (rbt_del_from_left x lt k v rt))"
   823   and "\<lbrakk>inv2 rt; bheight lt = bheight rt; inv1 rt\<rbrakk> \<Longrightarrow>
   824   inv2 (rbt_del_from_right x lt k v rt) \<and> 
   825   bheight (rbt_del_from_right x lt k v rt) = bheight lt \<and> 
   826   (color_of lt = B \<and> color_of rt = B \<and> inv1 (rbt_del_from_right x lt k v rt) \<or> 
   827    (color_of lt \<noteq> B \<or> color_of rt \<noteq> B) \<and> inv1l (rbt_del_from_right x lt k v rt))"
   828   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) 
   829   \<or> color_of lt = B \<and> bheight (rbt_del x lt) = bheight lt - 1 \<and> inv1l (rbt_del x lt))"
   830 using assms
   831 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)
   832 case (2 y c _ y')
   833   have "y = y' \<or> y < y' \<or> y > y'" by auto
   834   thus ?case proof (elim disjE)
   835     assume "y = y'"
   836     with 2 show ?thesis by (cases c) (simp add: combine_inv2 combine_inv1)+
   837   next
   838     assume "y < y'"
   839     with 2 show ?thesis by (cases c) auto
   840   next
   841     assume "y' < y"
   842     with 2 show ?thesis by (cases c) auto
   843   qed
   844 next
   845   case (3 y lt z v rta y' ss bb) 
   846   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)+
   847 next
   848   case (5 y a y' ss lt z v rta)
   849   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)+
   850 next
   851   case ("6_1" y a y' ss) thus ?case by (cases "color_of a = B \<and> color_of Empty = B") simp+
   852 qed auto
   853 
   854 lemma 
   855   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"
   856   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"
   857   and rbt_del_rbt_less: "lt |\<guillemotleft> v \<Longrightarrow> rbt_del x lt |\<guillemotleft> v"
   858 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) 
   859    (auto simp: balance_left_rbt_less balance_right_rbt_less)
   860 
   861 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"
   862   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"
   863   and rbt_del_rbt_greater: "v \<guillemotleft>| lt \<Longrightarrow> v \<guillemotleft>| rbt_del x lt"
   864 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)
   865    (auto simp: balance_left_rbt_greater balance_right_rbt_greater)
   866 
   867 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)"
   868   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)"
   869   and rbt_del_rbt_sorted: "rbt_sorted lt \<Longrightarrow> rbt_sorted (rbt_del x lt)"
   870 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)
   871   case (3 x lta zz v rta yy ss bb)
   872   from 3 have "Branch B lta zz v rta |\<guillemotleft> yy" by simp
   873   hence "rbt_del x (Branch B lta zz v rta) |\<guillemotleft> yy" by (rule rbt_del_rbt_less)
   874   with 3 show ?case by (simp add: balance_left_rbt_sorted)
   875 next
   876   case ("4_2" x vaa vbb vdd vc yy ss bb)
   877   hence "Branch R vaa vbb vdd vc |\<guillemotleft> yy" by simp
   878   hence "rbt_del x (Branch R vaa vbb vdd vc) |\<guillemotleft> yy" by (rule rbt_del_rbt_less)
   879   with "4_2" show ?case by simp
   880 next
   881   case (5 x aa yy ss lta zz v rta) 
   882   hence "yy \<guillemotleft>| Branch B lta zz v rta" by simp
   883   hence "yy \<guillemotleft>| rbt_del x (Branch B lta zz v rta)" by (rule rbt_del_rbt_greater)
   884   with 5 show ?case by (simp add: balance_right_rbt_sorted)
   885 next
   886   case ("6_2" x aa yy ss vaa vbb vdd vc)
   887   hence "yy \<guillemotleft>| Branch R vaa vbb vdd vc" by simp
   888   hence "yy \<guillemotleft>| rbt_del x (Branch R vaa vbb vdd vc)" by (rule rbt_del_rbt_greater)
   889   with "6_2" show ?case by simp
   890 qed (auto simp: combine_rbt_sorted)
   891 
   892 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)))"
   893   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)))"
   894   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))"
   895 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)
   896   case (2 xx c aa yy ss bb)
   897   have "xx = yy \<or> xx < yy \<or> xx > yy" by auto
   898   from this 2 show ?case proof (elim disjE)
   899     assume "xx = yy"
   900     with 2 show ?thesis proof (cases "xx = k")
   901       case True
   902       from 2 \<open>xx = yy\<close> \<open>xx = k\<close> have "rbt_sorted (Branch c aa yy ss bb) \<and> k = yy" by simp
   903       hence "\<not> entry_in_tree k v aa" "\<not> entry_in_tree k v bb" by (auto simp: rbt_less_nit rbt_greater_prop)
   904       with \<open>xx = yy\<close> 2 \<open>xx = k\<close> show ?thesis by (simp add: combine_in_tree)
   905     qed (simp add: combine_in_tree)
   906   qed simp+
   907 next    
   908   case (3 xx lta zz vv rta yy ss bb)
   909   def mt[simp]: mt == "Branch B lta zz vv rta"
   910   from 3 have "inv2 mt \<and> inv1 mt" by simp
   911   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)
   912   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)
   913   thus ?case proof (cases "xx = k")
   914     case True
   915     from 3 True have "yy \<guillemotleft>| bb \<and> yy > k" by simp
   916     hence "k \<guillemotleft>| bb" by (blast dest: rbt_greater_trans)
   917     with 3 4 True show ?thesis by (auto simp: rbt_greater_nit)
   918   qed auto
   919 next
   920   case ("4_1" xx yy ss bb)
   921   show ?case proof (cases "xx = k")
   922     case True
   923     with "4_1" have "yy \<guillemotleft>| bb \<and> k < yy" by simp
   924     hence "k \<guillemotleft>| bb" by (blast dest: rbt_greater_trans)
   925     with "4_1" \<open>xx = k\<close> 
   926    have "entry_in_tree k v (Branch R Empty yy ss bb) = entry_in_tree k v Empty" by (auto simp: rbt_greater_nit)
   927     thus ?thesis by auto
   928   qed simp+
   929 next
   930   case ("4_2" xx vaa vbb vdd vc yy ss bb)
   931   thus ?case proof (cases "xx = k")
   932     case True
   933     with "4_2" have "k < yy \<and> yy \<guillemotleft>| bb" by simp
   934     hence "k \<guillemotleft>| bb" by (blast dest: rbt_greater_trans)
   935     with True "4_2" show ?thesis by (auto simp: rbt_greater_nit)
   936   qed auto
   937 next
   938   case (5 xx aa yy ss lta zz vv rta)
   939   def mt[simp]: mt == "Branch B lta zz vv rta"
   940   from 5 have "inv2 mt \<and> inv1 mt" by simp
   941   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)
   942   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)
   943   thus ?case proof (cases "xx = k")
   944     case True
   945     from 5 True have "aa |\<guillemotleft> yy \<and> yy < k" by simp
   946     hence "aa |\<guillemotleft> k" by (blast dest: rbt_less_trans)
   947     with 3 5 True show ?thesis by (auto simp: rbt_less_nit)
   948   qed auto
   949 next
   950   case ("6_1" xx aa yy ss)
   951   show ?case proof (cases "xx = k")
   952     case True
   953     with "6_1" have "aa |\<guillemotleft> yy \<and> k > yy" by simp
   954     hence "aa |\<guillemotleft> k" by (blast dest: rbt_less_trans)
   955     with "6_1" \<open>xx = k\<close> show ?thesis by (auto simp: rbt_less_nit)
   956   qed simp
   957 next
   958   case ("6_2" xx aa yy ss vaa vbb vdd vc)
   959   thus ?case proof (cases "xx = k")
   960     case True
   961     with "6_2" have "k > yy \<and> aa |\<guillemotleft> yy" by simp
   962     hence "aa |\<guillemotleft> k" by (blast dest: rbt_less_trans)
   963     with True "6_2" show ?thesis by (auto simp: rbt_less_nit)
   964   qed auto
   965 qed simp
   966 
   967 definition (in ord) rbt_delete where
   968   "rbt_delete k t = paint B (rbt_del k t)"
   969 
   970 theorem rbt_delete_is_rbt [simp]: assumes "is_rbt t" shows "is_rbt (rbt_delete k t)"
   971 proof -
   972   from assms have "inv2 t" and "inv1 t" unfolding is_rbt_def by auto 
   973   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)
   974   hence "inv2 (rbt_del k t) \<and> inv1l (rbt_del k t)" by (cases "color_of t") auto
   975   with assms show ?thesis
   976     unfolding is_rbt_def rbt_delete_def
   977     by (auto intro: paint_rbt_sorted rbt_del_rbt_sorted)
   978 qed
   979 
   980 lemma rbt_delete_in_tree: 
   981   assumes "is_rbt t" 
   982   shows "entry_in_tree k v (rbt_delete x t) = (x \<noteq> k \<and> entry_in_tree k v t)"
   983   using assms unfolding is_rbt_def rbt_delete_def
   984   by (auto simp: rbt_del_in_tree)
   985 
   986 lemma rbt_lookup_rbt_delete:
   987   assumes is_rbt: "is_rbt t"
   988   shows "rbt_lookup (rbt_delete k t) = (rbt_lookup t)|`(-{k})"
   989 proof
   990   fix x
   991   show "rbt_lookup (rbt_delete k t) x = (rbt_lookup t |` (-{k})) x" 
   992   proof (cases "x = k")
   993     assume "x = k" 
   994     with is_rbt show ?thesis
   995       by (cases "rbt_lookup (rbt_delete k t) k") (auto simp: rbt_lookup_in_tree rbt_delete_in_tree)
   996   next
   997     assume "x \<noteq> k"
   998     thus ?thesis
   999       by auto (metis is_rbt rbt_delete_is_rbt rbt_delete_in_tree is_rbt_rbt_sorted rbt_lookup_from_in_tree)
  1000   qed
  1001 qed
  1002 
  1003 end
  1004 
  1005 subsection \<open>Modifying existing entries\<close>
  1006 
  1007 context ord begin
  1008 
  1009 primrec
  1010   rbt_map_entry :: "'a \<Rightarrow> ('b \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) rbt \<Rightarrow> ('a, 'b) rbt"
  1011 where
  1012   "rbt_map_entry k f Empty = Empty"
  1013 | "rbt_map_entry k f (Branch c lt x v rt) =
  1014     (if k < x then Branch c (rbt_map_entry k f lt) x v rt
  1015     else if k > x then (Branch c lt x v (rbt_map_entry k f rt))
  1016     else Branch c lt x (f v) rt)"
  1017 
  1018 
  1019 lemma rbt_map_entry_color_of: "color_of (rbt_map_entry k f t) = color_of t" by (induct t) simp+
  1020 lemma rbt_map_entry_inv1: "inv1 (rbt_map_entry k f t) = inv1 t" by (induct t) (simp add: rbt_map_entry_color_of)+
  1021 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+
  1022 lemma rbt_map_entry_rbt_greater: "rbt_greater a (rbt_map_entry k f t) = rbt_greater a t" by (induct t) simp+
  1023 lemma rbt_map_entry_rbt_less: "rbt_less a (rbt_map_entry k f t) = rbt_less a t" by (induct t) simp+
  1024 lemma rbt_map_entry_rbt_sorted: "rbt_sorted (rbt_map_entry k f t) = rbt_sorted t"
  1025   by (induct t) (simp_all add: rbt_map_entry_rbt_less rbt_map_entry_rbt_greater)
  1026 
  1027 theorem rbt_map_entry_is_rbt [simp]: "is_rbt (rbt_map_entry k f t) = is_rbt t" 
  1028 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 )
  1029 
  1030 end
  1031 
  1032 theorem (in linorder) rbt_lookup_rbt_map_entry:
  1033   "rbt_lookup (rbt_map_entry k f t) = (rbt_lookup t)(k := map_option f (rbt_lookup t k))"
  1034   by (induct t) (auto split: option.splits simp add: fun_eq_iff)
  1035 
  1036 subsection \<open>Mapping all entries\<close>
  1037 
  1038 primrec
  1039   map :: "('a \<Rightarrow> 'b \<Rightarrow> 'c) \<Rightarrow> ('a, 'b) rbt \<Rightarrow> ('a, 'c) rbt"
  1040 where
  1041   "map f Empty = Empty"
  1042 | "map f (Branch c lt k v rt) = Branch c (map f lt) k (f k v) (map f rt)"
  1043 
  1044 lemma map_entries [simp]: "entries (map f t) = List.map (\<lambda>(k, v). (k, f k v)) (entries t)"
  1045   by (induct t) auto
  1046 lemma map_keys [simp]: "keys (map f t) = keys t" by (simp add: keys_def split_def)
  1047 lemma map_color_of: "color_of (map f t) = color_of t" by (induct t) simp+
  1048 lemma map_inv1: "inv1 (map f t) = inv1 t" by (induct t) (simp add: map_color_of)+
  1049 lemma map_inv2: "inv2 (map f t) = inv2 t" "bheight (map f t) = bheight t" by (induct t) simp+
  1050 
  1051 context ord begin
  1052 
  1053 lemma map_rbt_greater: "rbt_greater k (map f t) = rbt_greater k t" by (induct t) simp+
  1054 lemma map_rbt_less: "rbt_less k (map f t) = rbt_less k t" by (induct t) simp+
  1055 lemma map_rbt_sorted: "rbt_sorted (map f t) = rbt_sorted t"  by (induct t) (simp add: map_rbt_less map_rbt_greater)+
  1056 theorem map_is_rbt [simp]: "is_rbt (map f t) = is_rbt t" 
  1057 unfolding is_rbt_def by (simp add: map_inv1 map_inv2 map_rbt_sorted map_color_of)
  1058 
  1059 end
  1060 
  1061 theorem (in linorder) rbt_lookup_map: "rbt_lookup (map f t) x = map_option (f x) (rbt_lookup t x)"
  1062   apply(induct t)
  1063   apply auto
  1064   apply(rename_tac a b c, subgoal_tac "x = a")
  1065   apply auto
  1066   done
  1067  (* FIXME: simproc "antisym less" does not work for linorder context, only for linorder type class
  1068     by (induct t) auto *)
  1069 
  1070 hide_const (open) map
  1071 
  1072 subsection \<open>Folding over entries\<close>
  1073 
  1074 definition fold :: "('a \<Rightarrow> 'b \<Rightarrow> 'c \<Rightarrow> 'c) \<Rightarrow> ('a, 'b) rbt \<Rightarrow> 'c \<Rightarrow> 'c" where
  1075   "fold f t = List.fold (case_prod f) (entries t)"
  1076 
  1077 lemma fold_simps [simp]:
  1078   "fold f Empty = id"
  1079   "fold f (Branch c lt k v rt) = fold f rt \<circ> f k v \<circ> fold f lt"
  1080   by (simp_all add: fold_def fun_eq_iff)
  1081 
  1082 lemma fold_code [code]:
  1083   "fold f Empty x = x"
  1084   "fold f (Branch c lt k v rt) x = fold f rt (f k v (fold f lt x))"
  1085 by(simp_all)
  1086 
  1087 (* fold with continuation predicate *)
  1088 
  1089 fun foldi :: "('c \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> 'b \<Rightarrow> 'c \<Rightarrow> 'c) \<Rightarrow> ('a :: linorder, 'b) rbt \<Rightarrow> 'c \<Rightarrow> 'c" 
  1090   where
  1091   "foldi c f Empty s = s" |
  1092   "foldi c f (Branch col l k v r) s = (
  1093     if (c s) then
  1094       let s' = foldi c f l s in
  1095         if (c s') then
  1096           foldi c f r (f k v s')
  1097         else s'
  1098     else 
  1099       s
  1100   )"
  1101 
  1102 subsection \<open>Bulkloading a tree\<close>
  1103 
  1104 definition (in ord) rbt_bulkload :: "('a \<times> 'b) list \<Rightarrow> ('a, 'b) rbt" where
  1105   "rbt_bulkload xs = foldr (\<lambda>(k, v). rbt_insert k v) xs Empty"
  1106 
  1107 context linorder begin
  1108 
  1109 lemma rbt_bulkload_is_rbt [simp, intro]:
  1110   "is_rbt (rbt_bulkload xs)"
  1111   unfolding rbt_bulkload_def by (induct xs) auto
  1112 
  1113 lemma rbt_lookup_rbt_bulkload:
  1114   "rbt_lookup (rbt_bulkload xs) = map_of xs"
  1115 proof -
  1116   obtain ys where "ys = rev xs" by simp
  1117   have "\<And>t. is_rbt t \<Longrightarrow>
  1118     rbt_lookup (List.fold (case_prod rbt_insert) ys t) = rbt_lookup t ++ map_of (rev ys)"
  1119       by (induct ys) (simp_all add: rbt_bulkload_def rbt_lookup_rbt_insert case_prod_beta)
  1120   from this Empty_is_rbt have
  1121     "rbt_lookup (List.fold (case_prod rbt_insert) (rev xs) Empty) = rbt_lookup Empty ++ map_of xs"
  1122      by (simp add: \<open>ys = rev xs\<close>)
  1123   then show ?thesis by (simp add: rbt_bulkload_def rbt_lookup_Empty foldr_conv_fold)
  1124 qed
  1125 
  1126 end
  1127 
  1128 
  1129 
  1130 subsection \<open>Building a RBT from a sorted list\<close>
  1131 
  1132 text \<open>
  1133   These functions have been adapted from 
  1134   Andrew W. Appel, Efficient Verified Red-Black Trees (September 2011) 
  1135 \<close>
  1136 
  1137 fun rbtreeify_f :: "nat \<Rightarrow> ('a \<times> 'b) list \<Rightarrow> ('a, 'b) rbt \<times> ('a \<times> 'b) list"
  1138   and rbtreeify_g :: "nat \<Rightarrow> ('a \<times> 'b) list \<Rightarrow> ('a, 'b) rbt \<times> ('a \<times> 'b) list"
  1139 where
  1140   "rbtreeify_f n kvs =
  1141    (if n = 0 then (Empty, kvs)
  1142     else if n = 1 then
  1143       case kvs of (k, v) # kvs' \<Rightarrow> (Branch R Empty k v Empty, kvs')
  1144     else if (n mod 2 = 0) then
  1145       case rbtreeify_f (n div 2) kvs of (t1, (k, v) # kvs') \<Rightarrow>
  1146         apfst (Branch B t1 k v) (rbtreeify_g (n div 2) kvs')
  1147     else case rbtreeify_f (n div 2) kvs of (t1, (k, v) # kvs') \<Rightarrow>
  1148         apfst (Branch B t1 k v) (rbtreeify_f (n div 2) kvs'))"
  1149 
  1150 | "rbtreeify_g n kvs =
  1151    (if n = 0 \<or> n = 1 then (Empty, kvs)
  1152     else if n mod 2 = 0 then
  1153       case rbtreeify_g (n div 2) kvs of (t1, (k, v) # kvs') \<Rightarrow>
  1154         apfst (Branch B t1 k v) (rbtreeify_g (n div 2) kvs')
  1155     else case rbtreeify_f (n div 2) kvs of (t1, (k, v) # kvs') \<Rightarrow>
  1156         apfst (Branch B t1 k v) (rbtreeify_g (n div 2) kvs'))"
  1157 
  1158 definition rbtreeify :: "('a \<times> 'b) list \<Rightarrow> ('a, 'b) rbt"
  1159 where "rbtreeify kvs = fst (rbtreeify_g (Suc (length kvs)) kvs)"
  1160 
  1161 declare rbtreeify_f.simps [simp del] rbtreeify_g.simps [simp del]
  1162 
  1163 lemma rbtreeify_f_code [code]:
  1164   "rbtreeify_f n kvs =
  1165    (if n = 0 then (Empty, kvs)
  1166     else if n = 1 then
  1167       case kvs of (k, v) # kvs' \<Rightarrow> 
  1168         (Branch R Empty k v Empty, kvs')
  1169     else let (n', r) = divmod_nat n 2 in
  1170       if r = 0 then
  1171         case rbtreeify_f n' kvs of (t1, (k, v) # kvs') \<Rightarrow>
  1172           apfst (Branch B t1 k v) (rbtreeify_g n' kvs')
  1173       else case rbtreeify_f n' kvs of (t1, (k, v) # kvs') \<Rightarrow>
  1174           apfst (Branch B t1 k v) (rbtreeify_f n' kvs'))"
  1175 by (subst rbtreeify_f.simps) (simp only: Let_def divmod_nat_div_mod prod.case)
  1176 
  1177 lemma rbtreeify_g_code [code]:
  1178   "rbtreeify_g n kvs =
  1179    (if n = 0 \<or> n = 1 then (Empty, kvs)
  1180     else let (n', r) = divmod_nat n 2 in
  1181       if r = 0 then
  1182         case rbtreeify_g n' kvs of (t1, (k, v) # kvs') \<Rightarrow>
  1183           apfst (Branch B t1 k v) (rbtreeify_g n' kvs')
  1184       else case rbtreeify_f n' kvs of (t1, (k, v) # kvs') \<Rightarrow>
  1185           apfst (Branch B t1 k v) (rbtreeify_g n' kvs'))"
  1186 by(subst rbtreeify_g.simps)(simp only: Let_def divmod_nat_div_mod prod.case)
  1187 
  1188 lemma Suc_double_half: "Suc (2 * n) div 2 = n"
  1189 by simp
  1190 
  1191 lemma div2_plus_div2: "n div 2 + n div 2 = (n :: nat) - n mod 2"
  1192 by arith
  1193 
  1194 lemma rbtreeify_f_rec_aux_lemma:
  1195   "\<lbrakk>k - n div 2 = Suc k'; n \<le> k; n mod 2 = Suc 0\<rbrakk>
  1196   \<Longrightarrow> k' - n div 2 = k - n"
  1197 apply(rule add_right_imp_eq[where a = "n - n div 2"])
  1198 apply(subst add_diff_assoc2, arith)
  1199 apply(simp add: div2_plus_div2)
  1200 done
  1201 
  1202 lemma rbtreeify_f_simps:
  1203   "rbtreeify_f 0 kvs = (Empty, kvs)"
  1204   "rbtreeify_f (Suc 0) ((k, v) # kvs) = 
  1205   (Branch R Empty k v Empty, kvs)"
  1206   "0 < n \<Longrightarrow> rbtreeify_f (2 * n) kvs =
  1207    (case rbtreeify_f n kvs of (t1, (k, v) # kvs') \<Rightarrow>
  1208      apfst (Branch B t1 k v) (rbtreeify_g n kvs'))"
  1209   "0 < n \<Longrightarrow> rbtreeify_f (Suc (2 * n)) kvs =
  1210    (case rbtreeify_f n kvs of (t1, (k, v) # kvs') \<Rightarrow> 
  1211      apfst (Branch B t1 k v) (rbtreeify_f n kvs'))"
  1212 by(subst (1) rbtreeify_f.simps, simp add: Suc_double_half)+
  1213 
  1214 lemma rbtreeify_g_simps:
  1215   "rbtreeify_g 0 kvs = (Empty, kvs)"
  1216   "rbtreeify_g (Suc 0) kvs = (Empty, kvs)"
  1217   "0 < n \<Longrightarrow> rbtreeify_g (2 * n) kvs =
  1218    (case rbtreeify_g n kvs of (t1, (k, v) # kvs') \<Rightarrow> 
  1219      apfst (Branch B t1 k v) (rbtreeify_g n kvs'))"
  1220   "0 < n \<Longrightarrow> rbtreeify_g (Suc (2 * n)) kvs =
  1221    (case rbtreeify_f n kvs of (t1, (k, v) # kvs') \<Rightarrow> 
  1222      apfst (Branch B t1 k v) (rbtreeify_g n kvs'))"
  1223 by(subst (1) rbtreeify_g.simps, simp add: Suc_double_half)+
  1224 
  1225 declare rbtreeify_f_simps[simp] rbtreeify_g_simps[simp]
  1226 
  1227 lemma length_rbtreeify_f: "n \<le> length kvs
  1228   \<Longrightarrow> length (snd (rbtreeify_f n kvs)) = length kvs - n"
  1229   and length_rbtreeify_g:"\<lbrakk> 0 < n; n \<le> Suc (length kvs) \<rbrakk>
  1230   \<Longrightarrow> length (snd (rbtreeify_g n kvs)) = Suc (length kvs) - n"
  1231 proof(induction n kvs and n kvs rule: rbtreeify_f_rbtreeify_g.induct)
  1232   case (1 n kvs)
  1233   show ?case
  1234   proof(cases "n \<le> 1")
  1235     case True thus ?thesis using "1.prems"
  1236       by(cases n kvs rule: nat.exhaust[case_product list.exhaust]) auto
  1237   next
  1238     case False
  1239     hence "n \<noteq> 0" "n \<noteq> 1" by simp_all
  1240     note IH = "1.IH"[OF this]
  1241     show ?thesis
  1242     proof(cases "n mod 2 = 0")
  1243       case True
  1244       hence "length (snd (rbtreeify_f n kvs)) = 
  1245         length (snd (rbtreeify_f (2 * (n div 2)) kvs))"
  1246         by(metis minus_nat.diff_0 mult_div_cancel)
  1247       also from "1.prems" False obtain k v kvs' 
  1248         where kvs: "kvs = (k, v) # kvs'" by(cases kvs) auto
  1249       also have "0 < n div 2" using False by(simp) 
  1250       note rbtreeify_f_simps(3)[OF this]
  1251       also note kvs[symmetric] 
  1252       also let ?rest1 = "snd (rbtreeify_f (n div 2) kvs)"
  1253       from "1.prems" have "n div 2 \<le> length kvs" by simp
  1254       with True have len: "length ?rest1 = length kvs - n div 2" by(rule IH)
  1255       with "1.prems" False obtain t1 k' v' kvs''
  1256         where kvs'': "rbtreeify_f (n div 2) kvs = (t1, (k', v') # kvs'')"
  1257          by(cases ?rest1)(auto simp add: snd_def split: prod.split_asm)
  1258       note this also note prod.case also note list.simps(5) 
  1259       also note prod.case also note snd_apfst
  1260       also have "0 < n div 2" "n div 2 \<le> Suc (length kvs'')" 
  1261         using len "1.prems" False unfolding kvs'' by simp_all
  1262       with True kvs''[symmetric] refl refl
  1263       have "length (snd (rbtreeify_g (n div 2) kvs'')) = 
  1264         Suc (length kvs'') - n div 2" by(rule IH)
  1265       finally show ?thesis using len[unfolded kvs''] "1.prems" True
  1266         by(simp add: Suc_diff_le[symmetric] mult_2[symmetric] mult_div_cancel)
  1267     next
  1268       case False
  1269       hence "length (snd (rbtreeify_f n kvs)) = 
  1270         length (snd (rbtreeify_f (Suc (2 * (n div 2))) kvs))"
  1271         by (simp add: mod_eq_0_iff_dvd)
  1272       also from "1.prems" \<open>\<not> n \<le> 1\<close> obtain k v kvs' 
  1273         where kvs: "kvs = (k, v) # kvs'" by(cases kvs) auto
  1274       also have "0 < n div 2" using \<open>\<not> n \<le> 1\<close> by(simp) 
  1275       note rbtreeify_f_simps(4)[OF this]
  1276       also note kvs[symmetric] 
  1277       also let ?rest1 = "snd (rbtreeify_f (n div 2) kvs)"
  1278       from "1.prems" have "n div 2 \<le> length kvs" by simp
  1279       with False have len: "length ?rest1 = length kvs - n div 2" by(rule IH)
  1280       with "1.prems" \<open>\<not> n \<le> 1\<close> obtain t1 k' v' kvs''
  1281         where kvs'': "rbtreeify_f (n div 2) kvs = (t1, (k', v') # kvs'')"
  1282         by(cases ?rest1)(auto simp add: snd_def split: prod.split_asm)
  1283       note this also note prod.case also note list.simps(5)
  1284       also note prod.case also note snd_apfst
  1285       also have "n div 2 \<le> length kvs''" 
  1286         using len "1.prems" False unfolding kvs'' by simp arith
  1287       with False kvs''[symmetric] refl refl
  1288       have "length (snd (rbtreeify_f (n div 2) kvs'')) = length kvs'' - n div 2"
  1289         by(rule IH)
  1290       finally show ?thesis using len[unfolded kvs''] "1.prems" False
  1291         by simp(rule rbtreeify_f_rec_aux_lemma[OF sym])
  1292     qed
  1293   qed
  1294 next
  1295   case (2 n kvs)
  1296   show ?case
  1297   proof(cases "n > 1")
  1298     case False with \<open>0 < n\<close> show ?thesis
  1299       by(cases n kvs rule: nat.exhaust[case_product list.exhaust]) simp_all
  1300   next
  1301     case True
  1302     hence "\<not> (n = 0 \<or> n = 1)" by simp
  1303     note IH = "2.IH"[OF this]
  1304     show ?thesis
  1305     proof(cases "n mod 2 = 0")
  1306       case True
  1307       hence "length (snd (rbtreeify_g n kvs)) =
  1308         length (snd (rbtreeify_g (2 * (n div 2)) kvs))"
  1309         by(metis minus_nat.diff_0 mult_div_cancel)
  1310       also from "2.prems" True obtain k v kvs' 
  1311         where kvs: "kvs = (k, v) # kvs'" by(cases kvs) auto
  1312       also have "0 < n div 2" using \<open>1 < n\<close> by(simp) 
  1313       note rbtreeify_g_simps(3)[OF this]
  1314       also note kvs[symmetric] 
  1315       also let ?rest1 = "snd (rbtreeify_g (n div 2) kvs)"
  1316       from "2.prems" \<open>1 < n\<close>
  1317       have "0 < n div 2" "n div 2 \<le> Suc (length kvs)" by simp_all
  1318       with True have len: "length ?rest1 = Suc (length kvs) - n div 2" by(rule IH)
  1319       with "2.prems" obtain t1 k' v' kvs''
  1320         where kvs'': "rbtreeify_g (n div 2) kvs = (t1, (k', v') # kvs'')"
  1321         by(cases ?rest1)(auto simp add: snd_def split: prod.split_asm)
  1322       note this also note prod.case also note list.simps(5) 
  1323       also note prod.case also note snd_apfst
  1324       also have "n div 2 \<le> Suc (length kvs'')" 
  1325         using len "2.prems" unfolding kvs'' by simp
  1326       with True kvs''[symmetric] refl refl \<open>0 < n div 2\<close>
  1327       have "length (snd (rbtreeify_g (n div 2) kvs'')) = Suc (length kvs'') - n div 2"
  1328         by(rule IH)
  1329       finally show ?thesis using len[unfolded kvs''] "2.prems" True
  1330         by(simp add: Suc_diff_le[symmetric] mult_2[symmetric] mult_div_cancel)
  1331     next
  1332       case False
  1333       hence "length (snd (rbtreeify_g n kvs)) = 
  1334         length (snd (rbtreeify_g (Suc (2 * (n div 2))) kvs))"
  1335         by (simp add: mod_eq_0_iff_dvd)
  1336       also from "2.prems" \<open>1 < n\<close> obtain k v kvs'
  1337         where kvs: "kvs = (k, v) # kvs'" by(cases kvs) auto
  1338       also have "0 < n div 2" using \<open>1 < n\<close> by(simp)
  1339       note rbtreeify_g_simps(4)[OF this]
  1340       also note kvs[symmetric] 
  1341       also let ?rest1 = "snd (rbtreeify_f (n div 2) kvs)"
  1342       from "2.prems" have "n div 2 \<le> length kvs" by simp
  1343       with False have len: "length ?rest1 = length kvs - n div 2" by(rule IH)
  1344       with "2.prems" \<open>1 < n\<close> False obtain t1 k' v' kvs'' 
  1345         where kvs'': "rbtreeify_f (n div 2) kvs = (t1, (k', v') # kvs'')"
  1346         by(cases ?rest1)(auto simp add: snd_def split: prod.split_asm, arith)
  1347       note this also note prod.case also note list.simps(5) 
  1348       also note prod.case also note snd_apfst
  1349       also have "n div 2 \<le> Suc (length kvs'')" 
  1350         using len "2.prems" False unfolding kvs'' by simp arith
  1351       with False kvs''[symmetric] refl refl \<open>0 < n div 2\<close>
  1352       have "length (snd (rbtreeify_g (n div 2) kvs'')) = Suc (length kvs'') - n div 2"
  1353         by(rule IH)
  1354       finally show ?thesis using len[unfolded kvs''] "2.prems" False
  1355         by(simp add: div2_plus_div2)
  1356     qed
  1357   qed
  1358 qed
  1359 
  1360 lemma rbtreeify_induct [consumes 1, case_names f_0 f_1 f_even f_odd g_0 g_1 g_even g_odd]:
  1361   fixes P Q
  1362   defines "f0 == (\<And>kvs. P 0 kvs)"
  1363   and "f1 == (\<And>k v kvs. P (Suc 0) ((k, v) # kvs))"
  1364   and "feven ==
  1365     (\<And>n kvs t k v kvs'. \<lbrakk> n > 0; n \<le> length kvs; P n kvs; 
  1366        rbtreeify_f n kvs = (t, (k, v) # kvs'); n \<le> Suc (length kvs'); Q n kvs' \<rbrakk> 
  1367      \<Longrightarrow> P (2 * n) kvs)"
  1368   and "fodd == 
  1369     (\<And>n kvs t k v kvs'. \<lbrakk> n > 0; n \<le> length kvs; P n kvs;
  1370        rbtreeify_f n kvs = (t, (k, v) # kvs'); n \<le> length kvs'; P n kvs' \<rbrakk> 
  1371     \<Longrightarrow> P (Suc (2 * n)) kvs)"
  1372   and "g0 == (\<And>kvs. Q 0 kvs)"
  1373   and "g1 == (\<And>kvs. Q (Suc 0) kvs)"
  1374   and "geven == 
  1375     (\<And>n kvs t k v kvs'. \<lbrakk> n > 0; n \<le> Suc (length kvs); Q n kvs; 
  1376        rbtreeify_g n kvs = (t, (k, v) # kvs'); n \<le> Suc (length kvs'); Q n kvs' \<rbrakk>
  1377     \<Longrightarrow> Q (2 * n) kvs)"
  1378   and "godd == 
  1379     (\<And>n kvs t k v kvs'. \<lbrakk> n > 0; n \<le> length kvs; P n kvs;
  1380        rbtreeify_f n kvs = (t, (k, v) # kvs'); n \<le> Suc (length kvs'); Q n kvs' \<rbrakk>
  1381     \<Longrightarrow> Q (Suc (2 * n)) kvs)"
  1382   shows "\<lbrakk> n \<le> length kvs; 
  1383            PROP f0; PROP f1; PROP feven; PROP fodd; 
  1384            PROP g0; PROP g1; PROP geven; PROP godd \<rbrakk>
  1385          \<Longrightarrow> P n kvs"
  1386   and "\<lbrakk> n \<le> Suc (length kvs);
  1387           PROP f0; PROP f1; PROP feven; PROP fodd; 
  1388           PROP g0; PROP g1; PROP geven; PROP godd \<rbrakk>
  1389        \<Longrightarrow> Q n kvs"
  1390 proof -
  1391   assume f0: "PROP f0" and f1: "PROP f1" and feven: "PROP feven" and fodd: "PROP fodd"
  1392     and g0: "PROP g0" and g1: "PROP g1" and geven: "PROP geven" and godd: "PROP godd"
  1393   show "n \<le> length kvs \<Longrightarrow> P n kvs" and "n \<le> Suc (length kvs) \<Longrightarrow> Q n kvs"
  1394   proof(induction rule: rbtreeify_f_rbtreeify_g.induct)
  1395     case (1 n kvs)
  1396     show ?case
  1397     proof(cases "n \<le> 1")
  1398       case True thus ?thesis using "1.prems"
  1399         by(cases n kvs rule: nat.exhaust[case_product list.exhaust])
  1400           (auto simp add: f0[unfolded f0_def] f1[unfolded f1_def])
  1401     next
  1402       case False 
  1403       hence ns: "n \<noteq> 0" "n \<noteq> 1" by simp_all
  1404       hence ge0: "n div 2 > 0" by simp
  1405       note IH = "1.IH"[OF ns]
  1406       show ?thesis
  1407       proof(cases "n mod 2 = 0")
  1408         case True note ge0 
  1409         moreover from "1.prems" have n2: "n div 2 \<le> length kvs" by simp
  1410         moreover from True n2 have "P (n div 2) kvs" by(rule IH)
  1411         moreover from length_rbtreeify_f[OF n2] ge0 "1.prems" obtain t k v kvs' 
  1412           where kvs': "rbtreeify_f (n div 2) kvs = (t, (k, v) # kvs')"
  1413           by(cases "snd (rbtreeify_f (n div 2) kvs)")
  1414             (auto simp add: snd_def split: prod.split_asm)
  1415         moreover from "1.prems" length_rbtreeify_f[OF n2] ge0
  1416         have n2': "n div 2 \<le> Suc (length kvs')" by(simp add: kvs')
  1417         moreover from True kvs'[symmetric] refl refl n2'
  1418         have "Q (n div 2) kvs'" by(rule IH)
  1419         moreover note feven[unfolded feven_def]
  1420           (* FIXME: why does by(rule feven[unfolded feven_def]) not work? *)
  1421         ultimately have "P (2 * (n div 2)) kvs" by -
  1422         thus ?thesis using True by (metis div_mod_equality' minus_nat.diff_0 mult.commute)
  1423       next
  1424         case False note ge0
  1425         moreover from "1.prems" have n2: "n div 2 \<le> length kvs" by simp
  1426         moreover from False n2 have "P (n div 2) kvs" by(rule IH)
  1427         moreover from length_rbtreeify_f[OF n2] ge0 "1.prems" obtain t k v kvs' 
  1428           where kvs': "rbtreeify_f (n div 2) kvs = (t, (k, v) # kvs')"
  1429           by(cases "snd (rbtreeify_f (n div 2) kvs)")
  1430             (auto simp add: snd_def split: prod.split_asm)
  1431         moreover from "1.prems" length_rbtreeify_f[OF n2] ge0 False
  1432         have n2': "n div 2 \<le> length kvs'" by(simp add: kvs') arith
  1433         moreover from False kvs'[symmetric] refl refl n2' have "P (n div 2) kvs'" by(rule IH)
  1434         moreover note fodd[unfolded fodd_def]
  1435         ultimately have "P (Suc (2 * (n div 2))) kvs" by -
  1436         thus ?thesis using False 
  1437           by simp (metis One_nat_def Suc_eq_plus1_left le_add_diff_inverse mod_less_eq_dividend mult_div_cancel)
  1438       qed
  1439     qed
  1440   next
  1441     case (2 n kvs)
  1442     show ?case
  1443     proof(cases "n \<le> 1")
  1444       case True thus ?thesis using "2.prems"
  1445         by(cases n kvs rule: nat.exhaust[case_product list.exhaust])
  1446           (auto simp add: g0[unfolded g0_def] g1[unfolded g1_def])
  1447     next
  1448       case False 
  1449       hence ns: "\<not> (n = 0 \<or> n = 1)" by simp
  1450       hence ge0: "n div 2 > 0" by simp
  1451       note IH = "2.IH"[OF ns]
  1452       show ?thesis
  1453       proof(cases "n mod 2 = 0")
  1454         case True note ge0
  1455         moreover from "2.prems" have n2: "n div 2 \<le> Suc (length kvs)" by simp
  1456         moreover from True n2 have "Q (n div 2) kvs" by(rule IH)
  1457         moreover from length_rbtreeify_g[OF ge0 n2] ge0 "2.prems" obtain t k v kvs' 
  1458           where kvs': "rbtreeify_g (n div 2) kvs = (t, (k, v) # kvs')"
  1459           by(cases "snd (rbtreeify_g (n div 2) kvs)")
  1460             (auto simp add: snd_def split: prod.split_asm)
  1461         moreover from "2.prems" length_rbtreeify_g[OF ge0 n2] ge0
  1462         have n2': "n div 2 \<le> Suc (length kvs')" by(simp add: kvs')
  1463         moreover from True kvs'[symmetric] refl refl  n2'
  1464         have "Q (n div 2) kvs'" by(rule IH)
  1465         moreover note geven[unfolded geven_def]
  1466         ultimately have "Q (2 * (n div 2)) kvs" by -
  1467         thus ?thesis using True 
  1468           by(metis div_mod_equality' minus_nat.diff_0 mult.commute)
  1469       next
  1470         case False note ge0
  1471         moreover from "2.prems" have n2: "n div 2 \<le> length kvs" by simp
  1472         moreover from False n2 have "P (n div 2) kvs" by(rule IH)
  1473         moreover from length_rbtreeify_f[OF n2] ge0 "2.prems" False obtain t k v kvs' 
  1474           where kvs': "rbtreeify_f (n div 2) kvs = (t, (k, v) # kvs')"
  1475           by(cases "snd (rbtreeify_f (n div 2) kvs)")
  1476             (auto simp add: snd_def split: prod.split_asm, arith)
  1477         moreover from "2.prems" length_rbtreeify_f[OF n2] ge0 False
  1478         have n2': "n div 2 \<le> Suc (length kvs')" by(simp add: kvs') arith
  1479         moreover from False kvs'[symmetric] refl refl n2'
  1480         have "Q (n div 2) kvs'" by(rule IH)
  1481         moreover note godd[unfolded godd_def]
  1482         ultimately have "Q (Suc (2 * (n div 2))) kvs" by -
  1483         thus ?thesis using False 
  1484           by simp (metis One_nat_def Suc_eq_plus1_left le_add_diff_inverse mod_less_eq_dividend mult_div_cancel)
  1485       qed
  1486     qed
  1487   qed
  1488 qed
  1489 
  1490 lemma inv1_rbtreeify_f: "n \<le> length kvs 
  1491   \<Longrightarrow> inv1 (fst (rbtreeify_f n kvs))"
  1492   and inv1_rbtreeify_g: "n \<le> Suc (length kvs)
  1493   \<Longrightarrow> inv1 (fst (rbtreeify_g n kvs))"
  1494 by(induct n kvs and n kvs rule: rbtreeify_induct) simp_all
  1495 
  1496 fun plog2 :: "nat \<Rightarrow> nat" 
  1497 where "plog2 n = (if n \<le> 1 then 0 else plog2 (n div 2) + 1)"
  1498 
  1499 declare plog2.simps [simp del]
  1500 
  1501 lemma plog2_simps [simp]:
  1502   "plog2 0 = 0" "plog2 (Suc 0) = 0"
  1503   "0 < n \<Longrightarrow> plog2 (2 * n) = 1 + plog2 n"
  1504   "0 < n \<Longrightarrow> plog2 (Suc (2 * n)) = 1 + plog2 n"
  1505 by(subst plog2.simps, simp add: Suc_double_half)+
  1506 
  1507 lemma bheight_rbtreeify_f: "n \<le> length kvs
  1508   \<Longrightarrow> bheight (fst (rbtreeify_f n kvs)) = plog2 n"
  1509   and bheight_rbtreeify_g: "n \<le> Suc (length kvs)
  1510   \<Longrightarrow> bheight (fst (rbtreeify_g n kvs)) = plog2 n"
  1511 by(induct n kvs and n kvs rule: rbtreeify_induct) simp_all
  1512 
  1513 lemma bheight_rbtreeify_f_eq_plog2I:
  1514   "\<lbrakk> rbtreeify_f n kvs = (t, kvs'); n \<le> length kvs \<rbrakk> 
  1515   \<Longrightarrow> bheight t = plog2 n"
  1516 using bheight_rbtreeify_f[of n kvs] by simp
  1517 
  1518 lemma bheight_rbtreeify_g_eq_plog2I: 
  1519   "\<lbrakk> rbtreeify_g n kvs = (t, kvs'); n \<le> Suc (length kvs) \<rbrakk>
  1520   \<Longrightarrow> bheight t = plog2 n"
  1521 using bheight_rbtreeify_g[of n kvs] by simp
  1522 
  1523 hide_const (open) plog2
  1524 
  1525 lemma inv2_rbtreeify_f: "n \<le> length kvs
  1526   \<Longrightarrow> inv2 (fst (rbtreeify_f n kvs))"
  1527   and inv2_rbtreeify_g: "n \<le> Suc (length kvs)
  1528   \<Longrightarrow> inv2 (fst (rbtreeify_g n kvs))"
  1529 by(induct n kvs and n kvs rule: rbtreeify_induct)
  1530   (auto simp add: bheight_rbtreeify_f bheight_rbtreeify_g 
  1531         intro: bheight_rbtreeify_f_eq_plog2I bheight_rbtreeify_g_eq_plog2I)
  1532 
  1533 lemma "n \<le> length kvs \<Longrightarrow> True"
  1534   and color_of_rbtreeify_g:
  1535   "\<lbrakk> n \<le> Suc (length kvs); 0 < n \<rbrakk> 
  1536   \<Longrightarrow> color_of (fst (rbtreeify_g n kvs)) = B"
  1537 by(induct n kvs and n kvs rule: rbtreeify_induct) simp_all
  1538 
  1539 lemma entries_rbtreeify_f_append:
  1540   "n \<le> length kvs 
  1541   \<Longrightarrow> entries (fst (rbtreeify_f n kvs)) @ snd (rbtreeify_f n kvs) = kvs"
  1542   and entries_rbtreeify_g_append: 
  1543   "n \<le> Suc (length kvs) 
  1544   \<Longrightarrow> entries (fst (rbtreeify_g n kvs)) @ snd (rbtreeify_g n kvs) = kvs"
  1545 by(induction rule: rbtreeify_induct) simp_all
  1546 
  1547 lemma length_entries_rbtreeify_f:
  1548   "n \<le> length kvs \<Longrightarrow> length (entries (fst (rbtreeify_f n kvs))) = n"
  1549   and length_entries_rbtreeify_g: 
  1550   "n \<le> Suc (length kvs) \<Longrightarrow> length (entries (fst (rbtreeify_g n kvs))) = n - 1"
  1551 by(induct rule: rbtreeify_induct) simp_all
  1552 
  1553 lemma rbtreeify_f_conv_drop: 
  1554   "n \<le> length kvs \<Longrightarrow> snd (rbtreeify_f n kvs) = drop n kvs"
  1555 using entries_rbtreeify_f_append[of n kvs]
  1556 by(simp add: append_eq_conv_conj length_entries_rbtreeify_f)
  1557 
  1558 lemma rbtreeify_g_conv_drop: 
  1559   "n \<le> Suc (length kvs) \<Longrightarrow> snd (rbtreeify_g n kvs) = drop (n - 1) kvs"
  1560 using entries_rbtreeify_g_append[of n kvs]
  1561 by(simp add: append_eq_conv_conj length_entries_rbtreeify_g)
  1562 
  1563 lemma entries_rbtreeify_f [simp]:
  1564   "n \<le> length kvs \<Longrightarrow> entries (fst (rbtreeify_f n kvs)) = take n kvs"
  1565 using entries_rbtreeify_f_append[of n kvs]
  1566 by(simp add: append_eq_conv_conj length_entries_rbtreeify_f)
  1567 
  1568 lemma entries_rbtreeify_g [simp]:
  1569   "n \<le> Suc (length kvs) \<Longrightarrow> 
  1570   entries (fst (rbtreeify_g n kvs)) = take (n - 1) kvs"
  1571 using entries_rbtreeify_g_append[of n kvs]
  1572 by(simp add: append_eq_conv_conj length_entries_rbtreeify_g)
  1573 
  1574 lemma keys_rbtreeify_f [simp]: "n \<le> length kvs
  1575   \<Longrightarrow> keys (fst (rbtreeify_f n kvs)) = take n (map fst kvs)"
  1576 by(simp add: keys_def take_map)
  1577 
  1578 lemma keys_rbtreeify_g [simp]: "n \<le> Suc (length kvs)
  1579   \<Longrightarrow> keys (fst (rbtreeify_g n kvs)) = take (n - 1) (map fst kvs)"
  1580 by(simp add: keys_def take_map)
  1581 
  1582 lemma rbtreeify_fD: 
  1583   "\<lbrakk> rbtreeify_f n kvs = (t, kvs'); n \<le> length kvs \<rbrakk> 
  1584   \<Longrightarrow> entries t = take n kvs \<and> kvs' = drop n kvs"
  1585 using rbtreeify_f_conv_drop[of n kvs] entries_rbtreeify_f[of n kvs] by simp
  1586 
  1587 lemma rbtreeify_gD: 
  1588   "\<lbrakk> rbtreeify_g n kvs = (t, kvs'); n \<le> Suc (length kvs) \<rbrakk>
  1589   \<Longrightarrow> entries t = take (n - 1) kvs \<and> kvs' = drop (n - 1) kvs"
  1590 using rbtreeify_g_conv_drop[of n kvs] entries_rbtreeify_g[of n kvs] by simp
  1591 
  1592 lemma entries_rbtreeify [simp]: "entries (rbtreeify kvs) = kvs"
  1593 by(simp add: rbtreeify_def entries_rbtreeify_g)
  1594 
  1595 context linorder begin
  1596 
  1597 lemma rbt_sorted_rbtreeify_f: 
  1598   "\<lbrakk> n \<le> length kvs; sorted (map fst kvs); distinct (map fst kvs) \<rbrakk> 
  1599   \<Longrightarrow> rbt_sorted (fst (rbtreeify_f n kvs))"
  1600   and rbt_sorted_rbtreeify_g: 
  1601   "\<lbrakk> n \<le> Suc (length kvs); sorted (map fst kvs); distinct (map fst kvs) \<rbrakk>
  1602   \<Longrightarrow> rbt_sorted (fst (rbtreeify_g n kvs))"
  1603 proof(induction n kvs and n kvs rule: rbtreeify_induct)
  1604   case (f_even n kvs t k v kvs')
  1605   from rbtreeify_fD[OF \<open>rbtreeify_f n kvs = (t, (k, v) # kvs')\<close> \<open>n \<le> length kvs\<close>]
  1606   have "entries t = take n kvs"
  1607     and kvs': "drop n kvs = (k, v) # kvs'" by simp_all
  1608   hence unfold: "kvs = take n kvs @ (k, v) # kvs'" by(metis append_take_drop_id)
  1609   from \<open>sorted (map fst kvs)\<close> kvs'
  1610   have "(\<forall>(x, y) \<in> set (take n kvs). x \<le> k) \<and> (\<forall>(x, y) \<in> set kvs'. k \<le> x)"
  1611     by(subst (asm) unfold)(auto simp add: sorted_append sorted_Cons)
  1612   moreover from \<open>distinct (map fst kvs)\<close> kvs'
  1613   have "(\<forall>(x, y) \<in> set (take n kvs). x \<noteq> k) \<and> (\<forall>(x, y) \<in> set kvs'. x \<noteq> k)"
  1614     by(subst (asm) unfold)(auto intro: rev_image_eqI)
  1615   ultimately have "(\<forall>(x, y) \<in> set (take n kvs). x < k) \<and> (\<forall>(x, y) \<in> set kvs'. k < x)"
  1616     by fastforce
  1617   hence "fst (rbtreeify_f n kvs) |\<guillemotleft> k" "k \<guillemotleft>| fst (rbtreeify_g n kvs')"
  1618     using \<open>n \<le> Suc (length kvs')\<close> \<open>n \<le> length kvs\<close> set_take_subset[of "n - 1" kvs']
  1619     by(auto simp add: ord.rbt_greater_prop ord.rbt_less_prop take_map split_def)
  1620   moreover from \<open>sorted (map fst kvs)\<close> \<open>distinct (map fst kvs)\<close>
  1621   have "rbt_sorted (fst (rbtreeify_f n kvs))" by(rule f_even.IH)
  1622   moreover have "sorted (map fst kvs')" "distinct (map fst kvs')"
  1623     using \<open>sorted (map fst kvs)\<close> \<open>distinct (map fst kvs)\<close>
  1624     by(subst (asm) (1 2) unfold, simp add: sorted_append sorted_Cons)+
  1625   hence "rbt_sorted (fst (rbtreeify_g n kvs'))" by(rule f_even.IH)
  1626   ultimately show ?case
  1627     using \<open>0 < n\<close> \<open>rbtreeify_f n kvs = (t, (k, v) # kvs')\<close> by simp
  1628 next
  1629   case (f_odd n kvs t k v kvs')
  1630   from rbtreeify_fD[OF \<open>rbtreeify_f n kvs = (t, (k, v) # kvs')\<close> \<open>n \<le> length kvs\<close>]
  1631   have "entries t = take n kvs" 
  1632     and kvs': "drop n kvs = (k, v) # kvs'" by simp_all
  1633   hence unfold: "kvs = take n kvs @ (k, v) # kvs'" by(metis append_take_drop_id)
  1634   from \<open>sorted (map fst kvs)\<close> kvs'
  1635   have "(\<forall>(x, y) \<in> set (take n kvs). x \<le> k) \<and> (\<forall>(x, y) \<in> set kvs'. k \<le> x)"
  1636     by(subst (asm) unfold)(auto simp add: sorted_append sorted_Cons)
  1637   moreover from \<open>distinct (map fst kvs)\<close> kvs'
  1638   have "(\<forall>(x, y) \<in> set (take n kvs). x \<noteq> k) \<and> (\<forall>(x, y) \<in> set kvs'. x \<noteq> k)"
  1639     by(subst (asm) unfold)(auto intro: rev_image_eqI)
  1640   ultimately have "(\<forall>(x, y) \<in> set (take n kvs). x < k) \<and> (\<forall>(x, y) \<in> set kvs'. k < x)"
  1641     by fastforce
  1642   hence "fst (rbtreeify_f n kvs) |\<guillemotleft> k" "k \<guillemotleft>| fst (rbtreeify_f n kvs')"
  1643     using \<open>n \<le> length kvs'\<close> \<open>n \<le> length kvs\<close> set_take_subset[of n kvs']
  1644     by(auto simp add: rbt_greater_prop rbt_less_prop take_map split_def)
  1645   moreover from \<open>sorted (map fst kvs)\<close> \<open>distinct (map fst kvs)\<close>
  1646   have "rbt_sorted (fst (rbtreeify_f n kvs))" by(rule f_odd.IH)
  1647   moreover have "sorted (map fst kvs')" "distinct (map fst kvs')"
  1648     using \<open>sorted (map fst kvs)\<close> \<open>distinct (map fst kvs)\<close>
  1649     by(subst (asm) (1 2) unfold, simp add: sorted_append sorted_Cons)+
  1650   hence "rbt_sorted (fst (rbtreeify_f n kvs'))" by(rule f_odd.IH)
  1651   ultimately show ?case 
  1652     using \<open>0 < n\<close> \<open>rbtreeify_f n kvs = (t, (k, v) # kvs')\<close> by simp
  1653 next
  1654   case (g_even n kvs t k v kvs')
  1655   from rbtreeify_gD[OF \<open>rbtreeify_g n kvs = (t, (k, v) # kvs')\<close> \<open>n \<le> Suc (length kvs)\<close>]
  1656   have t: "entries t = take (n - 1) kvs" 
  1657     and kvs': "drop (n - 1) kvs = (k, v) # kvs'" by simp_all
  1658   hence unfold: "kvs = take (n - 1) kvs @ (k, v) # kvs'" by(metis append_take_drop_id)
  1659   from \<open>sorted (map fst kvs)\<close> kvs'
  1660   have "(\<forall>(x, y) \<in> set (take (n - 1) kvs). x \<le> k) \<and> (\<forall>(x, y) \<in> set kvs'. k \<le> x)"
  1661     by(subst (asm) unfold)(auto simp add: sorted_append sorted_Cons)
  1662   moreover from \<open>distinct (map fst kvs)\<close> kvs'
  1663   have "(\<forall>(x, y) \<in> set (take (n - 1) kvs). x \<noteq> k) \<and> (\<forall>(x, y) \<in> set kvs'. x \<noteq> k)"
  1664     by(subst (asm) unfold)(auto intro: rev_image_eqI)
  1665   ultimately have "(\<forall>(x, y) \<in> set (take (n - 1) kvs). x < k) \<and> (\<forall>(x, y) \<in> set kvs'. k < x)"
  1666     by fastforce
  1667   hence "fst (rbtreeify_g n kvs) |\<guillemotleft> k" "k \<guillemotleft>| fst (rbtreeify_g n kvs')"
  1668     using \<open>n \<le> Suc (length kvs')\<close> \<open>n \<le> Suc (length kvs)\<close> set_take_subset[of "n - 1" kvs']
  1669     by(auto simp add: rbt_greater_prop rbt_less_prop take_map split_def)
  1670   moreover from \<open>sorted (map fst kvs)\<close> \<open>distinct (map fst kvs)\<close>
  1671   have "rbt_sorted (fst (rbtreeify_g n kvs))" by(rule g_even.IH)
  1672   moreover have "sorted (map fst kvs')" "distinct (map fst kvs')"
  1673     using \<open>sorted (map fst kvs)\<close> \<open>distinct (map fst kvs)\<close>
  1674     by(subst (asm) (1 2) unfold, simp add: sorted_append sorted_Cons)+
  1675   hence "rbt_sorted (fst (rbtreeify_g n kvs'))" by(rule g_even.IH)
  1676   ultimately show ?case using \<open>0 < n\<close> \<open>rbtreeify_g n kvs = (t, (k, v) # kvs')\<close> by simp
  1677 next
  1678   case (g_odd n kvs t k v kvs')
  1679   from rbtreeify_fD[OF \<open>rbtreeify_f n kvs = (t, (k, v) # kvs')\<close> \<open>n \<le> length kvs\<close>]
  1680   have "entries t = take n kvs"
  1681     and kvs': "drop n kvs = (k, v) # kvs'" by simp_all
  1682   hence unfold: "kvs = take n kvs @ (k, v) # kvs'" by(metis append_take_drop_id)
  1683   from \<open>sorted (map fst kvs)\<close> kvs'
  1684   have "(\<forall>(x, y) \<in> set (take n kvs). x \<le> k) \<and> (\<forall>(x, y) \<in> set kvs'. k \<le> x)"
  1685     by(subst (asm) unfold)(auto simp add: sorted_append sorted_Cons)
  1686   moreover from \<open>distinct (map fst kvs)\<close> kvs'
  1687   have "(\<forall>(x, y) \<in> set (take n kvs). x \<noteq> k) \<and> (\<forall>(x, y) \<in> set kvs'. x \<noteq> k)"
  1688     by(subst (asm) unfold)(auto intro: rev_image_eqI)
  1689   ultimately have "(\<forall>(x, y) \<in> set (take n kvs). x < k) \<and> (\<forall>(x, y) \<in> set kvs'. k < x)"
  1690     by fastforce
  1691   hence "fst (rbtreeify_f n kvs) |\<guillemotleft> k" "k \<guillemotleft>| fst (rbtreeify_g n kvs')"
  1692     using \<open>n \<le> Suc (length kvs')\<close> \<open>n \<le> length kvs\<close> set_take_subset[of "n - 1" kvs']
  1693     by(auto simp add: rbt_greater_prop rbt_less_prop take_map split_def)
  1694   moreover from \<open>sorted (map fst kvs)\<close> \<open>distinct (map fst kvs)\<close>
  1695   have "rbt_sorted (fst (rbtreeify_f n kvs))" by(rule g_odd.IH)
  1696   moreover have "sorted (map fst kvs')" "distinct (map fst kvs')"
  1697     using \<open>sorted (map fst kvs)\<close> \<open>distinct (map fst kvs)\<close>
  1698     by(subst (asm) (1 2) unfold, simp add: sorted_append sorted_Cons)+
  1699   hence "rbt_sorted (fst (rbtreeify_g n kvs'))" by(rule g_odd.IH)
  1700   ultimately show ?case
  1701     using \<open>0 < n\<close> \<open>rbtreeify_f n kvs = (t, (k, v) # kvs')\<close> by simp
  1702 qed simp_all
  1703 
  1704 lemma rbt_sorted_rbtreeify: 
  1705   "\<lbrakk> sorted (map fst kvs); distinct (map fst kvs) \<rbrakk> \<Longrightarrow> rbt_sorted (rbtreeify kvs)"
  1706 by(simp add: rbtreeify_def rbt_sorted_rbtreeify_g)
  1707 
  1708 lemma is_rbt_rbtreeify: 
  1709   "\<lbrakk> sorted (map fst kvs); distinct (map fst kvs) \<rbrakk>
  1710   \<Longrightarrow> is_rbt (rbtreeify kvs)"
  1711 by(simp add: is_rbt_def rbtreeify_def inv1_rbtreeify_g inv2_rbtreeify_g rbt_sorted_rbtreeify_g color_of_rbtreeify_g)
  1712 
  1713 lemma rbt_lookup_rbtreeify:
  1714   "\<lbrakk> sorted (map fst kvs); distinct (map fst kvs) \<rbrakk> \<Longrightarrow> 
  1715   rbt_lookup (rbtreeify kvs) = map_of kvs"
  1716 by(simp add: map_of_entries[symmetric] rbt_sorted_rbtreeify)
  1717 
  1718 end
  1719 
  1720 text \<open>
  1721   Functions to compare the height of two rbt trees, taken from 
  1722   Andrew W. Appel, Efficient Verified Red-Black Trees (September 2011)
  1723 \<close>
  1724 
  1725 fun skip_red :: "('a, 'b) rbt \<Rightarrow> ('a, 'b) rbt"
  1726 where
  1727   "skip_red (Branch color.R l k v r) = l"
  1728 | "skip_red t = t"
  1729 
  1730 definition skip_black :: "('a, 'b) rbt \<Rightarrow> ('a, 'b) rbt"
  1731 where
  1732   "skip_black t = (let t' = skip_red t in case t' of Branch color.B l k v r \<Rightarrow> l | _ \<Rightarrow> t')"
  1733 
  1734 datatype compare = LT | GT | EQ
  1735 
  1736 partial_function (tailrec) compare_height :: "('a, 'b) rbt \<Rightarrow> ('a, 'b) rbt \<Rightarrow> ('a, 'b) rbt \<Rightarrow> ('a, 'b) rbt \<Rightarrow> compare"
  1737 where
  1738   "compare_height sx s t tx =
  1739   (case (skip_red sx, skip_red s, skip_red t, skip_red tx) of
  1740      (Branch _ sx' _ _ _, Branch _ s' _ _ _, Branch _ t' _ _ _, Branch _ tx' _ _ _) \<Rightarrow> 
  1741        compare_height (skip_black sx') s' t' (skip_black tx')
  1742    | (_, rbt.Empty, _, Branch _ _ _ _ _) \<Rightarrow> LT
  1743    | (Branch _ _ _ _ _, _, rbt.Empty, _) \<Rightarrow> GT
  1744    | (Branch _ sx' _ _ _, Branch _ s' _ _ _, Branch _ t' _ _ _, rbt.Empty) \<Rightarrow>
  1745        compare_height (skip_black sx') s' t' rbt.Empty
  1746    | (rbt.Empty, Branch _ s' _ _ _, Branch _ t' _ _ _, Branch _ tx' _ _ _) \<Rightarrow>
  1747        compare_height rbt.Empty s' t' (skip_black tx')
  1748    | _ \<Rightarrow> EQ)"
  1749 
  1750 declare compare_height.simps [code]
  1751 
  1752 hide_type (open) compare
  1753 hide_const (open)
  1754   compare_height skip_black skip_red LT GT EQ case_compare rec_compare
  1755   Abs_compare Rep_compare
  1756 hide_fact (open)
  1757   Abs_compare_cases Abs_compare_induct Abs_compare_inject Abs_compare_inverse
  1758   Rep_compare Rep_compare_cases Rep_compare_induct Rep_compare_inject Rep_compare_inverse
  1759   compare.simps compare.exhaust compare.induct compare.rec compare.simps
  1760   compare.size compare.case_cong compare.case_cong_weak compare.case
  1761   compare.nchotomy compare.split compare.split_asm rec_compare_def
  1762   compare.eq.refl compare.eq.simps
  1763   compare.EQ_def compare.GT_def compare.LT_def
  1764   equal_compare_def
  1765   skip_red.simps skip_red.cases skip_red.induct 
  1766   skip_black_def
  1767   compare_height.simps
  1768 
  1769 subsection \<open>union and intersection of sorted associative lists\<close>
  1770 
  1771 context ord begin
  1772 
  1773 function sunion_with :: "('a \<Rightarrow> 'b \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> ('a \<times> 'b) list \<Rightarrow> ('a \<times> 'b) list \<Rightarrow> ('a \<times> 'b) list" 
  1774 where
  1775   "sunion_with f ((k, v) # as) ((k', v') # bs) =
  1776    (if k > k' then (k', v') # sunion_with f ((k, v) # as) bs
  1777     else if k < k' then (k, v) # sunion_with f as ((k', v') # bs)
  1778     else (k, f k v v') # sunion_with f as bs)"
  1779 | "sunion_with f [] bs = bs"
  1780 | "sunion_with f as [] = as"
  1781 by pat_completeness auto
  1782 termination by lexicographic_order
  1783 
  1784 function sinter_with :: "('a \<Rightarrow> 'b \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> ('a \<times> 'b) list \<Rightarrow> ('a \<times> 'b) list \<Rightarrow> ('a \<times> 'b) list"
  1785 where
  1786   "sinter_with f ((k, v) # as) ((k', v') # bs) =
  1787   (if k > k' then sinter_with f ((k, v) # as) bs
  1788    else if k < k' then sinter_with f as ((k', v') # bs)
  1789    else (k, f k v v') # sinter_with f as bs)"
  1790 | "sinter_with f [] _ = []"
  1791 | "sinter_with f _ [] = []"
  1792 by pat_completeness auto
  1793 termination by lexicographic_order
  1794 
  1795 end
  1796 
  1797 declare ord.sunion_with.simps [code] ord.sinter_with.simps[code]
  1798 
  1799 context linorder begin
  1800 
  1801 lemma set_fst_sunion_with: 
  1802   "set (map fst (sunion_with f xs ys)) = set (map fst xs) \<union> set (map fst ys)"
  1803 by(induct f xs ys rule: sunion_with.induct) auto
  1804 
  1805 lemma sorted_sunion_with [simp]:
  1806   "\<lbrakk> sorted (map fst xs); sorted (map fst ys) \<rbrakk> 
  1807   \<Longrightarrow> sorted (map fst (sunion_with f xs ys))"
  1808 by(induct f xs ys rule: sunion_with.induct)
  1809   (auto simp add: sorted_Cons set_fst_sunion_with simp del: set_map)
  1810 
  1811 lemma distinct_sunion_with [simp]:
  1812   "\<lbrakk> distinct (map fst xs); distinct (map fst ys); sorted (map fst xs); sorted (map fst ys) \<rbrakk>
  1813   \<Longrightarrow> distinct (map fst (sunion_with f xs ys))"
  1814 proof(induct f xs ys rule: sunion_with.induct)
  1815   case (1 f k v xs k' v' ys)
  1816   have "\<lbrakk> \<not> k < k'; \<not> k' < k \<rbrakk> \<Longrightarrow> k = k'" by simp
  1817   thus ?case using "1"
  1818     by(auto simp add: set_fst_sunion_with sorted_Cons simp del: set_map)
  1819 qed simp_all
  1820 
  1821 lemma map_of_sunion_with: 
  1822   "\<lbrakk> sorted (map fst xs); sorted (map fst ys) \<rbrakk>
  1823   \<Longrightarrow> map_of (sunion_with f xs ys) k = 
  1824   (case map_of xs k of None \<Rightarrow> map_of ys k 
  1825   | Some v \<Rightarrow> case map_of ys k of None \<Rightarrow> Some v 
  1826               | Some w \<Rightarrow> Some (f k v w))"
  1827 by(induct f xs ys rule: sunion_with.induct)(auto simp add: sorted_Cons split: option.split dest: map_of_SomeD bspec)
  1828 
  1829 lemma set_fst_sinter_with [simp]:
  1830   "\<lbrakk> sorted (map fst xs); sorted (map fst ys) \<rbrakk>
  1831   \<Longrightarrow> set (map fst (sinter_with f xs ys)) = set (map fst xs) \<inter> set (map fst ys)"
  1832 by(induct f xs ys rule: sinter_with.induct)(auto simp add: sorted_Cons simp del: set_map)
  1833 
  1834 lemma set_fst_sinter_with_subset1:
  1835   "set (map fst (sinter_with f xs ys)) \<subseteq> set (map fst xs)"
  1836 by(induct f xs ys rule: sinter_with.induct) auto
  1837 
  1838 lemma set_fst_sinter_with_subset2:
  1839   "set (map fst (sinter_with f xs ys)) \<subseteq> set (map fst ys)"
  1840 by(induct f xs ys rule: sinter_with.induct)(auto simp del: set_map)
  1841 
  1842 lemma sorted_sinter_with [simp]:
  1843   "\<lbrakk> sorted (map fst xs); sorted (map fst ys) \<rbrakk>
  1844   \<Longrightarrow> sorted (map fst (sinter_with f xs ys))"
  1845 by(induct f xs ys rule: sinter_with.induct)(auto simp add: sorted_Cons simp del: set_map)
  1846 
  1847 lemma distinct_sinter_with [simp]:
  1848   "\<lbrakk> distinct (map fst xs); distinct (map fst ys) \<rbrakk>
  1849   \<Longrightarrow> distinct (map fst (sinter_with f xs ys))"
  1850 proof(induct f xs ys rule: sinter_with.induct)
  1851   case (1 f k v as k' v' bs)
  1852   have "\<lbrakk> \<not> k < k'; \<not> k' < k \<rbrakk> \<Longrightarrow> k = k'" by simp
  1853   thus ?case using "1" set_fst_sinter_with_subset1[of f as bs]
  1854     set_fst_sinter_with_subset2[of f as bs]
  1855     by(auto simp del: set_map)
  1856 qed simp_all
  1857 
  1858 lemma map_of_sinter_with:
  1859   "\<lbrakk> sorted (map fst xs); sorted (map fst ys) \<rbrakk>
  1860   \<Longrightarrow> map_of (sinter_with f xs ys) k = 
  1861   (case map_of xs k of None \<Rightarrow> None | Some v \<Rightarrow> map_option (f k v) (map_of ys k))"
  1862 apply(induct f xs ys rule: sinter_with.induct)
  1863 apply(auto simp add: sorted_Cons map_option_case split: option.splits dest: map_of_SomeD bspec)
  1864 done
  1865 
  1866 end
  1867 
  1868 lemma distinct_map_of_rev: "distinct (map fst xs) \<Longrightarrow> map_of (rev xs) = map_of xs"
  1869 by(induct xs)(auto 4 3 simp add: map_add_def intro!: ext split: option.split intro: rev_image_eqI)
  1870 
  1871 lemma map_map_filter: 
  1872   "map f (List.map_filter g xs) = List.map_filter (map_option f \<circ> g) xs"
  1873 by(auto simp add: List.map_filter_def)
  1874 
  1875 lemma map_filter_map_option_const: 
  1876   "List.map_filter (\<lambda>x. map_option (\<lambda>y. f x) (g (f x))) xs = filter (\<lambda>x. g x \<noteq> None) (map f xs)"
  1877 by(auto simp add: map_filter_def filter_map o_def)
  1878 
  1879 lemma set_map_filter: "set (List.map_filter P xs) = the ` (P ` set xs - {None})"
  1880 by(auto simp add: List.map_filter_def intro: rev_image_eqI)
  1881 
  1882 context ord begin
  1883 
  1884 definition rbt_union_with_key :: "('a \<Rightarrow> 'b \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) rbt \<Rightarrow> ('a, 'b) rbt \<Rightarrow> ('a, 'b) rbt"
  1885 where
  1886   "rbt_union_with_key f t1 t2 =
  1887   (case RBT_Impl.compare_height t1 t1 t2 t2
  1888    of compare.EQ \<Rightarrow> rbtreeify (sunion_with f (entries t1) (entries t2))
  1889     | compare.LT \<Rightarrow> fold (rbt_insert_with_key (\<lambda>k v w. f k w v)) t1 t2
  1890     | compare.GT \<Rightarrow> fold (rbt_insert_with_key f) t2 t1)"
  1891 
  1892 definition rbt_union_with where
  1893   "rbt_union_with f = rbt_union_with_key (\<lambda>_. f)"
  1894 
  1895 definition rbt_union where
  1896   "rbt_union = rbt_union_with_key (%_ _ rv. rv)"
  1897 
  1898 definition rbt_inter_with_key :: "('a \<Rightarrow> 'b \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) rbt \<Rightarrow> ('a, 'b) rbt \<Rightarrow> ('a, 'b) rbt"
  1899 where
  1900   "rbt_inter_with_key f t1 t2 =
  1901   (case RBT_Impl.compare_height t1 t1 t2 t2 
  1902    of compare.EQ \<Rightarrow> rbtreeify (sinter_with f (entries t1) (entries t2))
  1903     | compare.LT \<Rightarrow> rbtreeify (List.map_filter (\<lambda>(k, v). map_option (\<lambda>w. (k, f k v w)) (rbt_lookup t2 k)) (entries t1))
  1904     | compare.GT \<Rightarrow> rbtreeify (List.map_filter (\<lambda>(k, v). map_option (\<lambda>w. (k, f k w v)) (rbt_lookup t1 k)) (entries t2)))"
  1905 
  1906 definition rbt_inter_with where
  1907   "rbt_inter_with f = rbt_inter_with_key (\<lambda>_. f)"
  1908 
  1909 definition rbt_inter where
  1910   "rbt_inter = rbt_inter_with_key (\<lambda>_ _ rv. rv)"
  1911 
  1912 end
  1913 
  1914 context linorder begin
  1915 
  1916 lemma rbt_sorted_entries_right_unique:
  1917   "\<lbrakk> (k, v) \<in> set (entries t); (k, v') \<in> set (entries t); 
  1918      rbt_sorted t \<rbrakk> \<Longrightarrow> v = v'"
  1919 by(auto dest!: distinct_entries inj_onD[where x="(k, v)" and y="(k, v')"] simp add: distinct_map)
  1920 
  1921 lemma rbt_sorted_fold_rbt_insertwk:
  1922   "rbt_sorted t \<Longrightarrow> rbt_sorted (List.fold (\<lambda>(k, v). rbt_insert_with_key f k v) xs t)"
  1923 by(induct xs rule: rev_induct)(auto simp add: rbt_insertwk_rbt_sorted)
  1924 
  1925 lemma is_rbt_fold_rbt_insertwk:
  1926   assumes "is_rbt t1"
  1927   shows "is_rbt (fold (rbt_insert_with_key f) t2 t1)"
  1928 proof -
  1929   def xs \<equiv> "entries t2"
  1930   from assms show ?thesis unfolding fold_def xs_def[symmetric]
  1931     by(induct xs rule: rev_induct)(auto simp add: rbt_insertwk_is_rbt)
  1932 qed
  1933 
  1934 lemma rbt_lookup_fold_rbt_insertwk:
  1935   assumes t1: "rbt_sorted t1" and t2: "rbt_sorted t2"
  1936   shows "rbt_lookup (fold (rbt_insert_with_key f) t1 t2) k =
  1937   (case rbt_lookup t1 k of None \<Rightarrow> rbt_lookup t2 k
  1938    | Some v \<Rightarrow> case rbt_lookup t2 k of None \<Rightarrow> Some v
  1939                | Some w \<Rightarrow> Some (f k w v))"
  1940 proof -
  1941   def xs \<equiv> "entries t1"
  1942   hence dt1: "distinct (map fst xs)" using t1 by(simp add: distinct_entries)
  1943   with t2 show ?thesis
  1944     unfolding fold_def map_of_entries[OF t1, symmetric]
  1945       xs_def[symmetric] distinct_map_of_rev[OF dt1, symmetric]
  1946     apply(induct xs rule: rev_induct)
  1947     apply(auto simp add: rbt_lookup_rbt_insertwk rbt_sorted_fold_rbt_insertwk split: option.splits)
  1948     apply(auto simp add: distinct_map_of_rev intro: rev_image_eqI)
  1949     done
  1950 qed
  1951 
  1952 lemma is_rbt_rbt_unionwk [simp]:
  1953   "\<lbrakk> is_rbt t1; is_rbt t2 \<rbrakk> \<Longrightarrow> is_rbt (rbt_union_with_key f t1 t2)"
  1954 by(simp add: rbt_union_with_key_def Let_def is_rbt_fold_rbt_insertwk is_rbt_rbtreeify rbt_sorted_entries distinct_entries split: compare.split)
  1955 
  1956 lemma rbt_lookup_rbt_unionwk:
  1957   "\<lbrakk> rbt_sorted t1; rbt_sorted t2 \<rbrakk> 
  1958   \<Longrightarrow> rbt_lookup (rbt_union_with_key f t1 t2) k = 
  1959   (case rbt_lookup t1 k of None \<Rightarrow> rbt_lookup t2 k 
  1960    | Some v \<Rightarrow> case rbt_lookup t2 k of None \<Rightarrow> Some v 
  1961               | Some w \<Rightarrow> Some (f k v w))"
  1962 by(auto simp add: rbt_union_with_key_def Let_def rbt_lookup_fold_rbt_insertwk rbt_sorted_entries distinct_entries map_of_sunion_with map_of_entries rbt_lookup_rbtreeify split: option.split compare.split)
  1963 
  1964 lemma rbt_unionw_is_rbt: "\<lbrakk> is_rbt lt; is_rbt rt \<rbrakk> \<Longrightarrow> is_rbt (rbt_union_with f lt rt)"
  1965 by(simp add: rbt_union_with_def)
  1966 
  1967 lemma rbt_union_is_rbt: "\<lbrakk> is_rbt lt; is_rbt rt \<rbrakk> \<Longrightarrow> is_rbt (rbt_union lt rt)"
  1968 by(simp add: rbt_union_def)
  1969 
  1970 lemma rbt_lookup_rbt_union:
  1971   "\<lbrakk> rbt_sorted s; rbt_sorted t \<rbrakk> \<Longrightarrow>
  1972   rbt_lookup (rbt_union s t) = rbt_lookup s ++ rbt_lookup t"
  1973 by(rule ext)(simp add: rbt_lookup_rbt_unionwk rbt_union_def map_add_def split: option.split)
  1974 
  1975 lemma rbt_interwk_is_rbt [simp]:
  1976   "\<lbrakk> rbt_sorted t1; rbt_sorted t2 \<rbrakk> \<Longrightarrow> is_rbt (rbt_inter_with_key f t1 t2)"
  1977 by(auto simp add: rbt_inter_with_key_def Let_def map_map_filter split_def o_def option.map_comp map_filter_map_option_const sorted_filter[where f=id, simplified] rbt_sorted_entries distinct_entries intro: is_rbt_rbtreeify split: compare.split)
  1978 
  1979 lemma rbt_interw_is_rbt:
  1980   "\<lbrakk> rbt_sorted t1; rbt_sorted t2 \<rbrakk> \<Longrightarrow> is_rbt (rbt_inter_with f t1 t2)"
  1981 by(simp add: rbt_inter_with_def)
  1982 
  1983 lemma rbt_inter_is_rbt:
  1984   "\<lbrakk> rbt_sorted t1; rbt_sorted t2 \<rbrakk> \<Longrightarrow> is_rbt (rbt_inter t1 t2)"
  1985 by(simp add: rbt_inter_def)
  1986 
  1987 lemma rbt_lookup_rbt_interwk:
  1988   "\<lbrakk> rbt_sorted t1; rbt_sorted t2 \<rbrakk>
  1989   \<Longrightarrow> rbt_lookup (rbt_inter_with_key f t1 t2) k =
  1990   (case rbt_lookup t1 k of None \<Rightarrow> None 
  1991    | Some v \<Rightarrow> case rbt_lookup t2 k of None \<Rightarrow> None
  1992                | Some w \<Rightarrow> Some (f k v w))"
  1993 by(auto 4 3 simp add: rbt_inter_with_key_def Let_def map_of_entries[symmetric] rbt_lookup_rbtreeify map_map_filter split_def o_def option.map_comp map_filter_map_option_const sorted_filter[where f=id, simplified] rbt_sorted_entries distinct_entries map_of_sinter_with map_of_eq_None_iff set_map_filter split: option.split compare.split intro: rev_image_eqI dest: rbt_sorted_entries_right_unique)
  1994 
  1995 lemma rbt_lookup_rbt_inter:
  1996   "\<lbrakk> rbt_sorted t1; rbt_sorted t2 \<rbrakk>
  1997   \<Longrightarrow> rbt_lookup (rbt_inter t1 t2) = rbt_lookup t2 |` dom (rbt_lookup t1)"
  1998 by(auto simp add: rbt_inter_def rbt_lookup_rbt_interwk restrict_map_def split: option.split)
  1999 
  2000 end
  2001 
  2002 
  2003 subsection \<open>Code generator setup\<close>
  2004 
  2005 lemmas [code] =
  2006   ord.rbt_less_prop
  2007   ord.rbt_greater_prop
  2008   ord.rbt_sorted.simps
  2009   ord.rbt_lookup.simps
  2010   ord.is_rbt_def
  2011   ord.rbt_ins.simps
  2012   ord.rbt_insert_with_key_def
  2013   ord.rbt_insertw_def
  2014   ord.rbt_insert_def
  2015   ord.rbt_del_from_left.simps
  2016   ord.rbt_del_from_right.simps
  2017   ord.rbt_del.simps
  2018   ord.rbt_delete_def
  2019   ord.sunion_with.simps
  2020   ord.sinter_with.simps
  2021   ord.rbt_union_with_key_def
  2022   ord.rbt_union_with_def
  2023   ord.rbt_union_def
  2024   ord.rbt_inter_with_key_def
  2025   ord.rbt_inter_with_def
  2026   ord.rbt_inter_def
  2027   ord.rbt_map_entry.simps
  2028   ord.rbt_bulkload_def
  2029 
  2030 text \<open>More efficient implementations for @{term entries} and @{term keys}\<close>
  2031 
  2032 definition gen_entries :: 
  2033   "(('a \<times> 'b) \<times> ('a, 'b) rbt) list \<Rightarrow> ('a, 'b) rbt \<Rightarrow> ('a \<times> 'b) list"
  2034 where
  2035   "gen_entries kvts t = entries t @ concat (map (\<lambda>(kv, t). kv # entries t) kvts)"
  2036 
  2037 lemma gen_entries_simps [simp, code]:
  2038   "gen_entries [] Empty = []"
  2039   "gen_entries ((kv, t) # kvts) Empty = kv # gen_entries kvts t"
  2040   "gen_entries kvts (Branch c l k v r) = gen_entries (((k, v), r) # kvts) l"
  2041 by(simp_all add: gen_entries_def)
  2042 
  2043 lemma entries_code [code]:
  2044   "entries = gen_entries []"
  2045 by(simp add: gen_entries_def fun_eq_iff)
  2046 
  2047 definition gen_keys :: "('a \<times> ('a, 'b) rbt) list \<Rightarrow> ('a, 'b) rbt \<Rightarrow> 'a list"
  2048 where "gen_keys kts t = RBT_Impl.keys t @ concat (List.map (\<lambda>(k, t). k # keys t) kts)"
  2049 
  2050 lemma gen_keys_simps [simp, code]:
  2051   "gen_keys [] Empty = []"
  2052   "gen_keys ((k, t) # kts) Empty = k # gen_keys kts t"
  2053   "gen_keys kts (Branch c l k v r) = gen_keys ((k, r) # kts) l"
  2054 by(simp_all add: gen_keys_def)
  2055 
  2056 lemma keys_code [code]:
  2057   "keys = gen_keys []"
  2058 by(simp add: gen_keys_def fun_eq_iff)
  2059 
  2060 text \<open>Restore original type constraints for constants\<close>
  2061 setup \<open>
  2062   fold Sign.add_const_constraint
  2063     [(@{const_name rbt_less}, SOME @{typ "('a :: order) \<Rightarrow> ('a, 'b) rbt \<Rightarrow> bool"}),
  2064      (@{const_name rbt_greater}, SOME @{typ "('a :: order) \<Rightarrow> ('a, 'b) rbt \<Rightarrow> bool"}),
  2065      (@{const_name rbt_sorted}, SOME @{typ "('a :: linorder, 'b) rbt \<Rightarrow> bool"}),
  2066      (@{const_name rbt_lookup}, SOME @{typ "('a :: linorder, 'b) rbt \<Rightarrow> 'a \<rightharpoonup> 'b"}),
  2067      (@{const_name is_rbt}, SOME @{typ "('a :: linorder, 'b) rbt \<Rightarrow> bool"}),
  2068      (@{const_name rbt_ins}, SOME @{typ "('a::linorder \<Rightarrow> 'b \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"}),
  2069      (@{const_name rbt_insert_with_key}, SOME @{typ "('a::linorder \<Rightarrow> 'b \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"}),
  2070      (@{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"}),
  2071      (@{const_name rbt_insert}, SOME @{typ "('a :: linorder) \<Rightarrow> 'b \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"}),
  2072      (@{const_name rbt_del_from_left}, SOME @{typ "('a::linorder) \<Rightarrow> ('a,'b) rbt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"}),
  2073      (@{const_name rbt_del_from_right}, SOME @{typ "('a::linorder) \<Rightarrow> ('a,'b) rbt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"}),
  2074      (@{const_name rbt_del}, SOME @{typ "('a::linorder) \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"}),
  2075      (@{const_name rbt_delete}, SOME @{typ "('a::linorder) \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"}),
  2076      (@{const_name rbt_union_with_key}, SOME @{typ "('a::linorder \<Rightarrow> 'b \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"}),
  2077      (@{const_name rbt_union_with}, SOME @{typ "('b \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> ('a::linorder,'b) rbt \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"}),
  2078      (@{const_name rbt_union}, SOME @{typ "('a::linorder,'b) rbt \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"}),
  2079      (@{const_name rbt_map_entry}, SOME @{typ "'a::linorder \<Rightarrow> ('b \<Rightarrow> 'b) \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"}),
  2080      (@{const_name rbt_bulkload}, SOME @{typ "('a \<times> 'b) list \<Rightarrow> ('a::linorder,'b) rbt"})]
  2081 \<close>
  2082 
  2083 hide_const (open) R B Empty entries keys fold gen_keys gen_entries
  2084 
  2085 end