src/HOL/Library/RBT.thy
author haftmann
Wed Mar 03 17:21:45 2010 +0100 (2010-03-03)
changeset 35550 e2bc7f8d8d51
parent 35534 14d8d72f8b1f
child 35602 e814157560e8
permissions -rw-r--r--
restructured RBT theory
krauss@26192
     1
(*  Title:      RBT.thy
krauss@26192
     2
    Author:     Markus Reiter, TU Muenchen
krauss@26192
     3
    Author:     Alexander Krauss, TU Muenchen
krauss@26192
     4
*)
krauss@26192
     5
krauss@26192
     6
header {* Red-Black Trees *}
krauss@26192
     7
krauss@26192
     8
(*<*)
krauss@26192
     9
theory RBT
haftmann@30738
    10
imports Main AssocList
krauss@26192
    11
begin
krauss@26192
    12
haftmann@35550
    13
subsection {* Datatype of RB trees *}
haftmann@35550
    14
krauss@26192
    15
datatype color = R | B
haftmann@35534
    16
datatype ('a, 'b) rbt = Empty | Branch color "('a, 'b) rbt" 'a 'b "('a, 'b) rbt"
haftmann@35534
    17
haftmann@35534
    18
lemma rbt_cases:
haftmann@35534
    19
  obtains (Empty) "t = Empty" 
haftmann@35534
    20
  | (Red) l k v r where "t = Branch R l k v r" 
haftmann@35534
    21
  | (Black) l k v r where "t = Branch B l k v r"
haftmann@35534
    22
proof (cases t)
haftmann@35534
    23
  case Empty with that show thesis by blast
haftmann@35534
    24
next
haftmann@35534
    25
  case (Branch c) with that show thesis by (cases c) blast+
haftmann@35534
    26
qed
haftmann@35534
    27
haftmann@35550
    28
subsection {* Tree properties *}
haftmann@35534
    29
haftmann@35550
    30
subsubsection {* Content of a tree *}
haftmann@35550
    31
haftmann@35550
    32
primrec entries :: "('a, 'b) rbt \<Rightarrow> ('a \<times> 'b) list"
haftmann@35534
    33
where 
haftmann@35534
    34
  "entries Empty = []"
haftmann@35534
    35
| "entries (Branch _ l k v r) = entries l @ (k,v) # entries r"
krauss@26192
    36
haftmann@35550
    37
abbreviation (input) entry_in_tree :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) rbt \<Rightarrow> bool"
krauss@26192
    38
where
haftmann@35550
    39
  "entry_in_tree k v t \<equiv> (k, v) \<in> set (entries t)"
haftmann@35550
    40
haftmann@35550
    41
definition keys :: "('a, 'b) rbt \<Rightarrow> 'a list" where
haftmann@35550
    42
  "keys t = map fst (entries t)"
krauss@26192
    43
haftmann@35550
    44
lemma keys_simps [simp, code]:
haftmann@35550
    45
  "keys Empty = []"
haftmann@35550
    46
  "keys (Branch c l k v r) = keys l @ k # keys r"
haftmann@35550
    47
  by (simp_all add: keys_def)
krauss@26192
    48
haftmann@35534
    49
lemma entry_in_tree_keys:
haftmann@35550
    50
  assumes "(k, v) \<in> set (entries t)"
haftmann@35550
    51
  shows "k \<in> set (keys t)"
haftmann@35550
    52
proof -
haftmann@35550
    53
  from assms have "fst (k, v) \<in> fst ` set (entries t)" by (rule imageI)
haftmann@35550
    54
  then show ?thesis by (simp add: keys_def)
haftmann@35550
    55
qed
haftmann@35550
    56
haftmann@35550
    57
haftmann@35550
    58
subsubsection {* Search tree properties *}
krauss@26192
    59
haftmann@35534
    60
definition tree_less :: "'a\<Colon>order \<Rightarrow> ('a, 'b) rbt \<Rightarrow> bool"
krauss@26192
    61
where
haftmann@35550
    62
  tree_less_prop: "tree_less k t \<longleftrightarrow> (\<forall>x\<in>set (keys t). x < k)"
haftmann@35534
    63
haftmann@35534
    64
abbreviation tree_less_symbol (infix "|\<guillemotleft>" 50)
haftmann@35534
    65
where "t |\<guillemotleft> x \<equiv> tree_less x t"
krauss@26192
    66
haftmann@35534
    67
definition tree_greater :: "'a\<Colon>order \<Rightarrow> ('a, 'b) rbt \<Rightarrow> bool" (infix "\<guillemotleft>|" 50) 
haftmann@35534
    68
where
haftmann@35550
    69
  tree_greater_prop: "tree_greater k t = (\<forall>x\<in>set (keys t). k < x)"
krauss@26192
    70
haftmann@35534
    71
lemma tree_less_simps [simp]:
haftmann@35534
    72
  "tree_less k Empty = True"
haftmann@35534
    73
  "tree_less k (Branch c lt kt v rt) \<longleftrightarrow> kt < k \<and> tree_less k lt \<and> tree_less k rt"
haftmann@35534
    74
  by (auto simp add: tree_less_prop)
krauss@26192
    75
haftmann@35534
    76
lemma tree_greater_simps [simp]:
haftmann@35534
    77
  "tree_greater k Empty = True"
haftmann@35534
    78
  "tree_greater k (Branch c lt kt v rt) \<longleftrightarrow> k < kt \<and> tree_greater k lt \<and> tree_greater k rt"
haftmann@35534
    79
  by (auto simp add: tree_greater_prop)
krauss@26192
    80
haftmann@35534
    81
lemmas tree_ord_props = tree_less_prop tree_greater_prop
krauss@26192
    82
haftmann@35534
    83
lemmas tree_greater_nit = tree_greater_prop entry_in_tree_keys
haftmann@35534
    84
lemmas tree_less_nit = tree_less_prop entry_in_tree_keys
krauss@26192
    85
haftmann@35550
    86
lemma tree_less_eq_trans: "l |\<guillemotleft> u \<Longrightarrow> u \<le> v \<Longrightarrow> l |\<guillemotleft> v"
haftmann@35550
    87
  and tree_less_trans: "t |\<guillemotleft> x \<Longrightarrow> x < y \<Longrightarrow> t |\<guillemotleft> y"
haftmann@35550
    88
  and tree_greater_eq_trans: "u \<le> v \<Longrightarrow> v \<guillemotleft>| r \<Longrightarrow> u \<guillemotleft>| r"
haftmann@35534
    89
  and tree_greater_trans: "x < y \<Longrightarrow> y \<guillemotleft>| t \<Longrightarrow> x \<guillemotleft>| t"
haftmann@35550
    90
  by (auto simp: tree_ord_props)
krauss@26192
    91
haftmann@35534
    92
primrec sorted :: "('a::linorder, 'b) rbt \<Rightarrow> bool"
krauss@26192
    93
where
haftmann@35534
    94
  "sorted Empty = True"
haftmann@35534
    95
| "sorted (Branch c l k v r) = (l |\<guillemotleft> k \<and> k \<guillemotleft>| r \<and> sorted l \<and> sorted r)"
krauss@26192
    96
haftmann@35550
    97
lemma sorted_entries:
haftmann@35550
    98
  "sorted t \<Longrightarrow> List.sorted (List.map fst (entries t))"
haftmann@35550
    99
by (induct t) 
haftmann@35550
   100
  (force simp: sorted_append sorted_Cons tree_ord_props 
haftmann@35550
   101
      dest!: entry_in_tree_keys)+
haftmann@35550
   102
haftmann@35550
   103
lemma distinct_entries:
haftmann@35550
   104
  "sorted t \<Longrightarrow> distinct (List.map fst (entries t))"
haftmann@35550
   105
by (induct t) 
haftmann@35550
   106
  (force simp: sorted_append sorted_Cons tree_ord_props 
haftmann@35550
   107
      dest!: entry_in_tree_keys)+
haftmann@35550
   108
haftmann@35550
   109
haftmann@35550
   110
subsubsection {* Tree lookup *}
haftmann@35550
   111
haftmann@35534
   112
primrec lookup :: "('a\<Colon>linorder, 'b) rbt \<Rightarrow> 'a \<rightharpoonup> 'b"
haftmann@35534
   113
where
haftmann@35534
   114
  "lookup Empty k = None"
haftmann@35534
   115
| "lookup (Branch _ l x y r) k = (if k < x then lookup l k else if x < k then lookup r k else Some y)"
haftmann@35534
   116
haftmann@35550
   117
lemma lookup_keys: "sorted t \<Longrightarrow> dom (lookup t) = set (keys t)"
haftmann@35550
   118
  by (induct t) (auto simp: dom_def tree_greater_prop tree_less_prop)
haftmann@35550
   119
haftmann@35550
   120
lemma dom_lookup_Branch: 
haftmann@35550
   121
  "sorted (Branch c t1 k v t2) \<Longrightarrow> 
haftmann@35550
   122
    dom (lookup (Branch c t1 k v t2)) 
haftmann@35550
   123
    = Set.insert k (dom (lookup t1) \<union> dom (lookup t2))"
haftmann@35550
   124
proof -
haftmann@35550
   125
  assume "sorted (Branch c t1 k v t2)"
haftmann@35550
   126
  moreover from this have "sorted t1" "sorted t2" by simp_all
haftmann@35550
   127
  ultimately show ?thesis by (simp add: lookup_keys)
haftmann@35550
   128
qed
haftmann@35550
   129
haftmann@35550
   130
lemma finite_dom_lookup [simp, intro!]: "finite (dom (lookup t))"
haftmann@35550
   131
proof (induct t)
haftmann@35550
   132
  case Empty then show ?case by simp
haftmann@35550
   133
next
haftmann@35550
   134
  case (Branch color t1 a b t2)
haftmann@35550
   135
  let ?A = "Set.insert a (dom (lookup t1) \<union> dom (lookup t2))"
haftmann@35550
   136
  have "dom (lookup (Branch color t1 a b t2)) \<subseteq> ?A" by (auto split: split_if_asm)
haftmann@35550
   137
  moreover from Branch have "finite (insert a (dom (lookup t1) \<union> dom (lookup t2)))" by simp
haftmann@35550
   138
  ultimately show ?case by (rule finite_subset)
haftmann@35550
   139
qed 
haftmann@35550
   140
haftmann@35534
   141
lemma lookup_tree_less[simp]: "t |\<guillemotleft> k \<Longrightarrow> lookup t k = None" 
krauss@26192
   142
by (induct t) auto
krauss@26192
   143
haftmann@35534
   144
lemma lookup_tree_greater[simp]: "k \<guillemotleft>| t \<Longrightarrow> lookup t k = None"
krauss@26192
   145
by (induct t) auto
krauss@26192
   146
haftmann@35550
   147
lemma lookup_in_tree: "sorted t \<Longrightarrow> (lookup t k = Some v) = entry_in_tree k v t"
haftmann@35534
   148
by (induct t) (auto simp: tree_less_prop tree_greater_prop entry_in_tree_keys)
krauss@26192
   149
haftmann@35534
   150
lemma lookup_Empty: "lookup Empty = empty"
krauss@26192
   151
by (rule ext) simp
krauss@26192
   152
haftmann@35550
   153
lemma lookup_map_of_entries:
haftmann@35550
   154
  "sorted t \<Longrightarrow> lookup t = map_of (entries t)"
haftmann@35550
   155
proof (induct t)
haftmann@35550
   156
  case Empty thus ?case by (simp add: lookup_Empty)
haftmann@35550
   157
next
haftmann@35550
   158
  case (Branch c t1 k v t2)
haftmann@35550
   159
  have "lookup (Branch c t1 k v t2) = lookup t2 ++ [k\<mapsto>v] ++ lookup t1"
haftmann@35550
   160
  proof (rule ext)
haftmann@35550
   161
    fix x
haftmann@35550
   162
    from Branch have SORTED: "sorted (Branch c t1 k v t2)" by simp
haftmann@35550
   163
    let ?thesis = "lookup (Branch c t1 k v t2) x = (lookup t2 ++ [k \<mapsto> v] ++ lookup t1) x"
haftmann@35550
   164
haftmann@35550
   165
    have DOM_T1: "!!k'. k'\<in>dom (lookup t1) \<Longrightarrow> k>k'"
haftmann@35550
   166
    proof -
haftmann@35550
   167
      fix k'
haftmann@35550
   168
      from SORTED have "t1 |\<guillemotleft> k" by simp
haftmann@35550
   169
      with tree_less_prop have "\<forall>k'\<in>set (keys t1). k>k'" by auto
haftmann@35550
   170
      moreover assume "k'\<in>dom (lookup t1)"
haftmann@35550
   171
      ultimately show "k>k'" using lookup_keys SORTED by auto
haftmann@35550
   172
    qed
haftmann@35550
   173
    
haftmann@35550
   174
    have DOM_T2: "!!k'. k'\<in>dom (lookup t2) \<Longrightarrow> k<k'"
haftmann@35550
   175
    proof -
haftmann@35550
   176
      fix k'
haftmann@35550
   177
      from SORTED have "k \<guillemotleft>| t2" by simp
haftmann@35550
   178
      with tree_greater_prop have "\<forall>k'\<in>set (keys t2). k<k'" by auto
haftmann@35550
   179
      moreover assume "k'\<in>dom (lookup t2)"
haftmann@35550
   180
      ultimately show "k<k'" using lookup_keys SORTED by auto
haftmann@35550
   181
    qed
haftmann@35550
   182
    
haftmann@35550
   183
    {
haftmann@35550
   184
      assume C: "x<k"
haftmann@35550
   185
      hence "lookup (Branch c t1 k v t2) x = lookup t1 x" by simp
haftmann@35550
   186
      moreover from C have "x\<notin>dom [k\<mapsto>v]" by simp
haftmann@35550
   187
      moreover have "x\<notin>dom (lookup t2)" proof
haftmann@35550
   188
        assume "x\<in>dom (lookup t2)"
haftmann@35550
   189
        with DOM_T2 have "k<x" by blast
haftmann@35550
   190
        with C show False by simp
haftmann@35550
   191
      qed
haftmann@35550
   192
      ultimately have ?thesis by (simp add: map_add_upd_left map_add_dom_app_simps)
haftmann@35550
   193
    } moreover {
haftmann@35550
   194
      assume [simp]: "x=k"
haftmann@35550
   195
      hence "lookup (Branch c t1 k v t2) x = [k \<mapsto> v] x" by simp
haftmann@35550
   196
      moreover have "x\<notin>dom (lookup t1)" proof
haftmann@35550
   197
        assume "x\<in>dom (lookup t1)"
haftmann@35550
   198
        with DOM_T1 have "k>x" by blast
haftmann@35550
   199
        thus False by simp
haftmann@35550
   200
      qed
haftmann@35550
   201
      ultimately have ?thesis by (simp add: map_add_upd_left map_add_dom_app_simps)
haftmann@35550
   202
    } moreover {
haftmann@35550
   203
      assume C: "x>k"
haftmann@35550
   204
      hence "lookup (Branch c t1 k v t2) x = lookup t2 x" by (simp add: less_not_sym[of k x])
haftmann@35550
   205
      moreover from C have "x\<notin>dom [k\<mapsto>v]" by simp
haftmann@35550
   206
      moreover have "x\<notin>dom (lookup t1)" proof
haftmann@35550
   207
        assume "x\<in>dom (lookup t1)"
haftmann@35550
   208
        with DOM_T1 have "k>x" by simp
haftmann@35550
   209
        with C show False by simp
haftmann@35550
   210
      qed
haftmann@35550
   211
      ultimately have ?thesis by (simp add: map_add_upd_left map_add_dom_app_simps)
haftmann@35550
   212
    } ultimately show ?thesis using less_linear by blast
haftmann@35550
   213
  qed
haftmann@35550
   214
  also from Branch have "lookup t2 ++ [k \<mapsto> v] ++ lookup t1 = map_of (entries (Branch c t1 k v t2))" by simp
haftmann@35550
   215
  finally show ?case .
haftmann@35550
   216
qed
haftmann@35550
   217
haftmann@35550
   218
(*lemma map_of_inject:
haftmann@35550
   219
  assumes distinct: "distinct (map fst xs)" "distinct (map fst ys)"
haftmann@35550
   220
  shows "map_of xs = map_of ys \<longleftrightarrow> set xs = set ys"
haftmann@35550
   221
haftmann@35550
   222
lemma entries_eqI:
haftmann@35550
   223
  assumes sorted: "sorted t1" "sorted t2" 
haftmann@35550
   224
  assumes lookup: "lookup t1 = lookup t2"
haftmann@35550
   225
  shows entries: "entries t1 = entries t2"
haftmann@35550
   226
proof -
haftmann@35550
   227
  from sorted lookup have "map_of (entries t1) = map_of (entries t2)"
haftmann@35550
   228
    by (simp_all add: lookup_map_of_entries)
haftmann@35550
   229
qed*)
haftmann@35550
   230
krauss@26192
   231
(* a kind of extensionality *)
haftmann@35550
   232
lemma lookup_from_in_tree: 
haftmann@35534
   233
  assumes sorted: "sorted t1" "sorted t2" 
haftmann@35534
   234
  and eq: "\<And>v. entry_in_tree (k\<Colon>'a\<Colon>linorder) v t1 = entry_in_tree k v t2" 
haftmann@35534
   235
  shows "lookup t1 k = lookup t2 k"
haftmann@35534
   236
proof (cases "lookup t1 k")
krauss@26192
   237
  case None
haftmann@35534
   238
  then have "\<And>v. \<not> entry_in_tree k v t1"
haftmann@35550
   239
    by (simp add: lookup_in_tree[symmetric] sorted)
krauss@26192
   240
  with None show ?thesis
haftmann@35550
   241
    by (cases "lookup t2 k") (auto simp: lookup_in_tree sorted eq)
krauss@26192
   242
next
krauss@26192
   243
  case (Some a)
krauss@26192
   244
  then show ?thesis
haftmann@35534
   245
    apply (cases "lookup t2 k")
haftmann@35550
   246
    apply (auto simp: lookup_in_tree sorted eq)
haftmann@35550
   247
    by (auto simp add: lookup_in_tree[symmetric] sorted Some)
krauss@26192
   248
qed
krauss@26192
   249
haftmann@35550
   250
haftmann@35550
   251
subsubsection {* Red-black properties *}
krauss@26192
   252
haftmann@35534
   253
primrec color_of :: "('a, 'b) rbt \<Rightarrow> color"
krauss@26192
   254
where
haftmann@35534
   255
  "color_of Empty = B"
haftmann@35534
   256
| "color_of (Branch c _ _ _ _) = c"
krauss@26192
   257
haftmann@35534
   258
primrec bheight :: "('a,'b) rbt \<Rightarrow> nat"
haftmann@35534
   259
where
haftmann@35534
   260
  "bheight Empty = 0"
haftmann@35534
   261
| "bheight (Branch c lt k v rt) = (if c = B then Suc (bheight lt) else bheight lt)"
haftmann@35534
   262
haftmann@35534
   263
primrec inv1 :: "('a, 'b) rbt \<Rightarrow> bool"
krauss@26192
   264
where
krauss@26192
   265
  "inv1 Empty = True"
haftmann@35534
   266
| "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)"
krauss@26192
   267
haftmann@35534
   268
primrec inv1l :: "('a, 'b) rbt \<Rightarrow> bool" -- {* Weaker version *}
krauss@26192
   269
where
krauss@26192
   270
  "inv1l Empty = True"
haftmann@35534
   271
| "inv1l (Branch c l k v r) = (inv1 l \<and> inv1 r)"
krauss@26192
   272
lemma [simp]: "inv1 t \<Longrightarrow> inv1l t" by (cases t) simp+
krauss@26192
   273
haftmann@35534
   274
primrec inv2 :: "('a, 'b) rbt \<Rightarrow> bool"
krauss@26192
   275
where
krauss@26192
   276
  "inv2 Empty = True"
haftmann@35534
   277
| "inv2 (Branch c lt k v rt) = (inv2 lt \<and> inv2 rt \<and> bheight lt = bheight rt)"
krauss@26192
   278
haftmann@35534
   279
definition is_rbt :: "('a\<Colon>linorder, 'b) rbt \<Rightarrow> bool" where
haftmann@35534
   280
  "is_rbt t \<longleftrightarrow> inv1 t \<and> inv2 t \<and> color_of t = B \<and> sorted t"
krauss@26192
   281
haftmann@35534
   282
lemma is_rbt_sorted [simp]:
haftmann@35534
   283
  "is_rbt t \<Longrightarrow> sorted t" by (simp add: is_rbt_def)
krauss@26192
   284
haftmann@35534
   285
theorem Empty_is_rbt [simp]:
haftmann@35534
   286
  "is_rbt Empty" by (simp add: is_rbt_def)
krauss@26192
   287
krauss@26192
   288
krauss@26192
   289
subsection {* Insertion *}
krauss@26192
   290
krauss@26192
   291
fun (* slow, due to massive case splitting *)
krauss@26192
   292
  balance :: "('a,'b) rbt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"
krauss@26192
   293
where
haftmann@35534
   294
  "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)" |
haftmann@35534
   295
  "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)" |
haftmann@35534
   296
  "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)" |
haftmann@35534
   297
  "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)" |
haftmann@35534
   298
  "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)" |
haftmann@35534
   299
  "balance a s t b = Branch B a s t b"
krauss@26192
   300
krauss@26192
   301
lemma balance_inv1: "\<lbrakk>inv1l l; inv1l r\<rbrakk> \<Longrightarrow> inv1 (balance l k v r)" 
krauss@26192
   302
  by (induct l k v r rule: balance.induct) auto
krauss@26192
   303
haftmann@35534
   304
lemma balance_bheight: "bheight l = bheight r \<Longrightarrow> bheight (balance l k v r) = Suc (bheight l)"
krauss@26192
   305
  by (induct l k v r rule: balance.induct) auto
krauss@26192
   306
krauss@26192
   307
lemma balance_inv2: 
haftmann@35534
   308
  assumes "inv2 l" "inv2 r" "bheight l = bheight r"
krauss@26192
   309
  shows "inv2 (balance l k v r)"
krauss@26192
   310
  using assms
krauss@26192
   311
  by (induct l k v r rule: balance.induct) auto
krauss@26192
   312
haftmann@35534
   313
lemma balance_tree_greater[simp]: "(v \<guillemotleft>| balance a k x b) = (v \<guillemotleft>| a \<and> v \<guillemotleft>| b \<and> v < k)" 
krauss@26192
   314
  by (induct a k x b rule: balance.induct) auto
krauss@26192
   315
haftmann@35534
   316
lemma balance_tree_less[simp]: "(balance a k x b |\<guillemotleft> v) = (a |\<guillemotleft> v \<and> b |\<guillemotleft> v \<and> k < v)"
krauss@26192
   317
  by (induct a k x b rule: balance.induct) auto
krauss@26192
   318
haftmann@35534
   319
lemma balance_sorted: 
krauss@26192
   320
  fixes k :: "'a::linorder"
haftmann@35534
   321
  assumes "sorted l" "sorted r" "l |\<guillemotleft> k" "k \<guillemotleft>| r"
haftmann@35534
   322
  shows "sorted (balance l k v r)"
krauss@26192
   323
using assms proof (induct l k v r rule: balance.induct)
krauss@26192
   324
  case ("2_2" a x w b y t c z s va vb vd vc)
haftmann@35534
   325
  hence "y < z \<and> z \<guillemotleft>| Branch B va vb vd vc" 
haftmann@35534
   326
    by (auto simp add: tree_ord_props)
haftmann@35534
   327
  hence "tree_greater y (Branch B va vb vd vc)" by (blast dest: tree_greater_trans)
krauss@26192
   328
  with "2_2" show ?case by simp
krauss@26192
   329
next
krauss@26192
   330
  case ("3_2" va vb vd vc x w b y s c z)
haftmann@35534
   331
  from "3_2" have "x < y \<and> tree_less x (Branch B va vb vd vc)" 
haftmann@35534
   332
    by simp
haftmann@35534
   333
  hence "tree_less y (Branch B va vb vd vc)" by (blast dest: tree_less_trans)
krauss@26192
   334
  with "3_2" show ?case by simp
krauss@26192
   335
next
krauss@26192
   336
  case ("3_3" x w b y s c z t va vb vd vc)
haftmann@35534
   337
  from "3_3" have "y < z \<and> tree_greater z (Branch B va vb vd vc)" by simp
haftmann@35534
   338
  hence "tree_greater y (Branch B va vb vd vc)" by (blast dest: tree_greater_trans)
krauss@26192
   339
  with "3_3" show ?case by simp
krauss@26192
   340
next
krauss@26192
   341
  case ("3_4" vd ve vg vf x w b y s c z t va vb vii vc)
haftmann@35534
   342
  hence "x < y \<and> tree_less x (Branch B vd ve vg vf)" by simp
haftmann@35534
   343
  hence 1: "tree_less y (Branch B vd ve vg vf)" by (blast dest: tree_less_trans)
haftmann@35534
   344
  from "3_4" have "y < z \<and> tree_greater z (Branch B va vb vii vc)" by simp
haftmann@35534
   345
  hence "tree_greater y (Branch B va vb vii vc)" by (blast dest: tree_greater_trans)
krauss@26192
   346
  with 1 "3_4" show ?case by simp
krauss@26192
   347
next
krauss@26192
   348
  case ("4_2" va vb vd vc x w b y s c z t dd)
haftmann@35534
   349
  hence "x < y \<and> tree_less x (Branch B va vb vd vc)" by simp
haftmann@35534
   350
  hence "tree_less y (Branch B va vb vd vc)" by (blast dest: tree_less_trans)
krauss@26192
   351
  with "4_2" show ?case by simp
krauss@26192
   352
next
krauss@26192
   353
  case ("5_2" x w b y s c z t va vb vd vc)
haftmann@35534
   354
  hence "y < z \<and> tree_greater z (Branch B va vb vd vc)" by simp
haftmann@35534
   355
  hence "tree_greater y (Branch B va vb vd vc)" by (blast dest: tree_greater_trans)
krauss@26192
   356
  with "5_2" show ?case by simp
krauss@26192
   357
next
krauss@26192
   358
  case ("5_3" va vb vd vc x w b y s c z t)
haftmann@35534
   359
  hence "x < y \<and> tree_less x (Branch B va vb vd vc)" by simp
haftmann@35534
   360
  hence "tree_less y (Branch B va vb vd vc)" by (blast dest: tree_less_trans)
krauss@26192
   361
  with "5_3" show ?case by simp
krauss@26192
   362
next
krauss@26192
   363
  case ("5_4" va vb vg vc x w b y s c z t vd ve vii vf)
haftmann@35534
   364
  hence "x < y \<and> tree_less x (Branch B va vb vg vc)" by simp
haftmann@35534
   365
  hence 1: "tree_less y (Branch B va vb vg vc)" by (blast dest: tree_less_trans)
haftmann@35534
   366
  from "5_4" have "y < z \<and> tree_greater z (Branch B vd ve vii vf)" by simp
haftmann@35534
   367
  hence "tree_greater y (Branch B vd ve vii vf)" by (blast dest: tree_greater_trans)
krauss@26192
   368
  with 1 "5_4" show ?case by simp
krauss@26192
   369
qed simp+
krauss@26192
   370
haftmann@35550
   371
lemma entries_balance [simp]:
haftmann@35550
   372
  "entries (balance l k v r) = entries l @ (k, v) # entries r"
haftmann@35550
   373
  by (induct l k v r rule: balance.induct) auto
krauss@26192
   374
haftmann@35550
   375
lemma keys_balance [simp]: 
haftmann@35550
   376
  "keys (balance l k v r) = keys l @ k # keys r"
haftmann@35550
   377
  by (simp add: keys_def)
haftmann@35550
   378
haftmann@35550
   379
lemma balance_in_tree:  
haftmann@35550
   380
  "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"
haftmann@35550
   381
  by (auto simp add: keys_def)
krauss@26192
   382
haftmann@35534
   383
lemma lookup_balance[simp]: 
krauss@26192
   384
fixes k :: "'a::linorder"
haftmann@35534
   385
assumes "sorted l" "sorted r" "l |\<guillemotleft> k" "k \<guillemotleft>| r"
haftmann@35534
   386
shows "lookup (balance l k v r) x = lookup (Branch B l k v r) x"
haftmann@35550
   387
by (rule lookup_from_in_tree) (auto simp:assms balance_in_tree balance_sorted)
krauss@26192
   388
krauss@26192
   389
primrec paint :: "color \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"
krauss@26192
   390
where
krauss@26192
   391
  "paint c Empty = Empty"
haftmann@35534
   392
| "paint c (Branch _ l k v r) = Branch c l k v r"
krauss@26192
   393
krauss@26192
   394
lemma paint_inv1l[simp]: "inv1l t \<Longrightarrow> inv1l (paint c t)" by (cases t) auto
krauss@26192
   395
lemma paint_inv1[simp]: "inv1l t \<Longrightarrow> inv1 (paint B t)" by (cases t) auto
krauss@26192
   396
lemma paint_inv2[simp]: "inv2 t \<Longrightarrow> inv2 (paint c t)" by (cases t) auto
haftmann@35534
   397
lemma paint_color_of[simp]: "color_of (paint B t) = B" by (cases t) auto
haftmann@35534
   398
lemma paint_sorted[simp]: "sorted t \<Longrightarrow> sorted (paint c t)" by (cases t) auto
haftmann@35550
   399
lemma paint_in_tree[simp]: "entry_in_tree k x (paint c t) = entry_in_tree k x t" by (cases t) auto
haftmann@35534
   400
lemma paint_lookup[simp]: "lookup (paint c t) = lookup t" by (rule ext) (cases t, auto)
haftmann@35534
   401
lemma paint_tree_greater[simp]: "(v \<guillemotleft>| paint c t) = (v \<guillemotleft>| t)" by (cases t) auto
haftmann@35534
   402
lemma paint_tree_less[simp]: "(paint c t |\<guillemotleft> v) = (t |\<guillemotleft> v)" by (cases t) auto
krauss@26192
   403
krauss@26192
   404
fun
krauss@26192
   405
  ins :: "('a\<Colon>linorder \<Rightarrow> 'b \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"
krauss@26192
   406
where
haftmann@35534
   407
  "ins f k v Empty = Branch R Empty k v Empty" |
haftmann@35534
   408
  "ins f k v (Branch B l x y r) = (if k < x then balance (ins f k v l) x y r
krauss@26192
   409
                               else if k > x then balance l x y (ins f k v r)
haftmann@35534
   410
                               else Branch B l x (f k y v) r)" |
haftmann@35534
   411
  "ins f k v (Branch R l x y r) = (if k < x then Branch R (ins f k v l) x y r
haftmann@35534
   412
                               else if k > x then Branch R l x y (ins f k v r)
haftmann@35534
   413
                               else Branch R l x (f k y v) r)"
krauss@26192
   414
krauss@26192
   415
lemma ins_inv1_inv2: 
krauss@26192
   416
  assumes "inv1 t" "inv2 t"
haftmann@35534
   417
  shows "inv2 (ins f k x t)" "bheight (ins f k x t) = bheight t" 
haftmann@35534
   418
  "color_of t = B \<Longrightarrow> inv1 (ins f k x t)" "inv1l (ins f k x t)"
krauss@26192
   419
  using assms
haftmann@35534
   420
  by (induct f k x t rule: ins.induct) (auto simp: balance_inv1 balance_inv2 balance_bheight)
krauss@26192
   421
haftmann@35534
   422
lemma ins_tree_greater[simp]: "(v \<guillemotleft>| ins f k x t) = (v \<guillemotleft>| t \<and> k > v)"
krauss@26192
   423
  by (induct f k x t rule: ins.induct) auto
haftmann@35534
   424
lemma ins_tree_less[simp]: "(ins f k x t |\<guillemotleft> v) = (t |\<guillemotleft> v \<and> k < v)"
krauss@26192
   425
  by (induct f k x t rule: ins.induct) auto
haftmann@35534
   426
lemma ins_sorted[simp]: "sorted t \<Longrightarrow> sorted (ins f k x t)"
haftmann@35534
   427
  by (induct f k x t rule: ins.induct) (auto simp: balance_sorted)
krauss@26192
   428
haftmann@35550
   429
lemma keys_ins: "set (keys (ins f k v t)) = { k } \<union> set (keys t)"
haftmann@35550
   430
  by (induct f k v t rule: ins.induct) auto
krauss@26192
   431
haftmann@35534
   432
lemma lookup_ins: 
krauss@26192
   433
  fixes k :: "'a::linorder"
haftmann@35534
   434
  assumes "sorted t"
haftmann@35534
   435
  shows "lookup (ins f k v t) x = ((lookup t)(k |-> case lookup t k of None \<Rightarrow> v 
krauss@26192
   436
                                                       | Some w \<Rightarrow> f k w v)) x"
krauss@26192
   437
using assms by (induct f k v t rule: ins.induct) auto
krauss@26192
   438
krauss@26192
   439
definition
haftmann@35550
   440
  insert_with_key :: "('a\<Colon>linorder \<Rightarrow> 'b \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"
krauss@26192
   441
where
haftmann@35550
   442
  "insert_with_key f k v t = paint B (ins f k v t)"
krauss@26192
   443
haftmann@35550
   444
lemma insertwk_sorted: "sorted t \<Longrightarrow> sorted (insert_with_key f k x t)"
haftmann@35550
   445
  by (auto simp: insert_with_key_def)
krauss@26192
   446
haftmann@35534
   447
theorem insertwk_is_rbt: 
haftmann@35534
   448
  assumes inv: "is_rbt t" 
haftmann@35550
   449
  shows "is_rbt (insert_with_key f k x t)"
krauss@26192
   450
using assms
haftmann@35550
   451
unfolding insert_with_key_def is_rbt_def
krauss@26192
   452
by (auto simp: ins_inv1_inv2)
krauss@26192
   453
haftmann@35534
   454
lemma lookup_insertwk: 
haftmann@35534
   455
  assumes "sorted t"
haftmann@35550
   456
  shows "lookup (insert_with_key f k v t) x = ((lookup t)(k |-> case lookup t k of None \<Rightarrow> v 
krauss@26192
   457
                                                       | Some w \<Rightarrow> f k w v)) x"
haftmann@35550
   458
unfolding insert_with_key_def using assms
haftmann@35534
   459
by (simp add:lookup_ins)
krauss@26192
   460
krauss@26192
   461
definition
haftmann@35550
   462
  insertw_def: "insert_with f = insert_with_key (\<lambda>_. f)"
krauss@26192
   463
haftmann@35550
   464
lemma insertw_sorted: "sorted t \<Longrightarrow> sorted (insert_with f k v t)" by (simp add: insertwk_sorted insertw_def)
haftmann@35550
   465
theorem insertw_is_rbt: "is_rbt t \<Longrightarrow> is_rbt (insert_with f k v t)" by (simp add: insertwk_is_rbt insertw_def)
krauss@26192
   466
haftmann@35534
   467
lemma lookup_insertw:
haftmann@35534
   468
  assumes "is_rbt t"
haftmann@35550
   469
  shows "lookup (insert_with f k v t) = (lookup t)(k \<mapsto> (if k:dom (lookup t) then f (the (lookup t k)) v else v))"
krauss@26192
   470
using assms
krauss@26192
   471
unfolding insertw_def
haftmann@35534
   472
by (rule_tac ext) (cases "lookup t k", auto simp:lookup_insertwk dom_def)
krauss@26192
   473
haftmann@35534
   474
definition insert :: "'a\<Colon>linorder \<Rightarrow> 'b \<Rightarrow> ('a, 'b) rbt \<Rightarrow> ('a, 'b) rbt" where
haftmann@35550
   475
  "insert = insert_with_key (\<lambda>_ _ nv. nv)"
krauss@26192
   476
haftmann@35534
   477
lemma insert_sorted: "sorted t \<Longrightarrow> sorted (insert k v t)" by (simp add: insertwk_sorted insert_def)
haftmann@35550
   478
theorem insert_is_rbt [simp]: "is_rbt t \<Longrightarrow> is_rbt (insert k v t)" by (simp add: insertwk_is_rbt insert_def)
krauss@26192
   479
haftmann@35534
   480
lemma lookup_insert: 
haftmann@35534
   481
  assumes "is_rbt t"
haftmann@35534
   482
  shows "lookup (insert k v t) = (lookup t)(k\<mapsto>v)"
haftmann@35534
   483
unfolding insert_def
krauss@26192
   484
using assms
haftmann@35534
   485
by (rule_tac ext) (simp add: lookup_insertwk split:option.split)
krauss@26192
   486
krauss@26192
   487
krauss@26192
   488
subsection {* Deletion *}
krauss@26192
   489
haftmann@35534
   490
lemma bheight_paintR'[simp]: "color_of t = B \<Longrightarrow> bheight (paint R t) = bheight t - 1"
krauss@26192
   491
by (cases t rule: rbt_cases) auto
krauss@26192
   492
krauss@26192
   493
fun
haftmann@35550
   494
  balance_left :: "('a,'b) rbt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"
krauss@26192
   495
where
haftmann@35550
   496
  "balance_left (Branch R a k x b) s y c = Branch R (Branch B a k x b) s y c" |
haftmann@35550
   497
  "balance_left bl k x (Branch B a s y b) = balance bl k x (Branch R a s y b)" |
haftmann@35550
   498
  "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))" |
haftmann@35550
   499
  "balance_left t k x s = Empty"
krauss@26192
   500
haftmann@35550
   501
lemma balance_left_inv2_with_inv1:
haftmann@35534
   502
  assumes "inv2 lt" "inv2 rt" "bheight lt + 1 = bheight rt" "inv1 rt"
haftmann@35550
   503
  shows "bheight (balance_left lt k v rt) = bheight lt + 1"
haftmann@35550
   504
  and   "inv2 (balance_left lt k v rt)"
krauss@26192
   505
using assms 
haftmann@35550
   506
by (induct lt k v rt rule: balance_left.induct) (auto simp: balance_inv2 balance_bheight)
krauss@26192
   507
haftmann@35550
   508
lemma balance_left_inv2_app: 
haftmann@35534
   509
  assumes "inv2 lt" "inv2 rt" "bheight lt + 1 = bheight rt" "color_of rt = B"
haftmann@35550
   510
  shows "inv2 (balance_left lt k v rt)" 
haftmann@35550
   511
        "bheight (balance_left lt k v rt) = bheight rt"
krauss@26192
   512
using assms 
haftmann@35550
   513
by (induct lt k v rt rule: balance_left.induct) (auto simp add: balance_inv2 balance_bheight)+ 
krauss@26192
   514
haftmann@35550
   515
lemma balance_left_inv1: "\<lbrakk>inv1l a; inv1 b; color_of b = B\<rbrakk> \<Longrightarrow> inv1 (balance_left a k x b)"
haftmann@35550
   516
  by (induct a k x b rule: balance_left.induct) (simp add: balance_inv1)+
krauss@26192
   517
haftmann@35550
   518
lemma balance_left_inv1l: "\<lbrakk> inv1l lt; inv1 rt \<rbrakk> \<Longrightarrow> inv1l (balance_left lt k x rt)"
haftmann@35550
   519
by (induct lt k x rt rule: balance_left.induct) (auto simp: balance_inv1)
krauss@26192
   520
haftmann@35550
   521
lemma balance_left_sorted: "\<lbrakk> sorted l; sorted r; tree_less k l; tree_greater k r \<rbrakk> \<Longrightarrow> sorted (balance_left l k v r)"
haftmann@35550
   522
apply (induct l k v r rule: balance_left.induct)
haftmann@35534
   523
apply (auto simp: balance_sorted)
haftmann@35534
   524
apply (unfold tree_greater_prop tree_less_prop)
krauss@26192
   525
by force+
krauss@26192
   526
haftmann@35550
   527
lemma balance_left_tree_greater: 
krauss@26192
   528
  fixes k :: "'a::order"
krauss@26192
   529
  assumes "k \<guillemotleft>| a" "k \<guillemotleft>| b" "k < x" 
haftmann@35550
   530
  shows "k \<guillemotleft>| balance_left a x t b"
krauss@26192
   531
using assms 
haftmann@35550
   532
by (induct a x t b rule: balance_left.induct) auto
krauss@26192
   533
haftmann@35550
   534
lemma balance_left_tree_less: 
krauss@26192
   535
  fixes k :: "'a::order"
krauss@26192
   536
  assumes "a |\<guillemotleft> k" "b |\<guillemotleft> k" "x < k" 
haftmann@35550
   537
  shows "balance_left a x t b |\<guillemotleft> k"
krauss@26192
   538
using assms
haftmann@35550
   539
by (induct a x t b rule: balance_left.induct) auto
krauss@26192
   540
haftmann@35550
   541
lemma balance_left_in_tree: 
haftmann@35534
   542
  assumes "inv1l l" "inv1 r" "bheight l + 1 = bheight r"
haftmann@35550
   543
  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)"
krauss@26192
   544
using assms 
haftmann@35550
   545
by (induct l k v r rule: balance_left.induct) (auto simp: balance_in_tree)
krauss@26192
   546
krauss@26192
   547
fun
haftmann@35550
   548
  balance_right :: "('a,'b) rbt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"
krauss@26192
   549
where
haftmann@35550
   550
  "balance_right a k x (Branch R b s y c) = Branch R a k x (Branch B b s y c)" |
haftmann@35550
   551
  "balance_right (Branch B a k x b) s y bl = balance (Branch R a k x b) s y bl" |
haftmann@35550
   552
  "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)" |
haftmann@35550
   553
  "balance_right t k x s = Empty"
krauss@26192
   554
haftmann@35550
   555
lemma balance_right_inv2_with_inv1:
haftmann@35534
   556
  assumes "inv2 lt" "inv2 rt" "bheight lt = bheight rt + 1" "inv1 lt"
haftmann@35550
   557
  shows "inv2 (balance_right lt k v rt) \<and> bheight (balance_right lt k v rt) = bheight lt"
krauss@26192
   558
using assms
haftmann@35550
   559
by (induct lt k v rt rule: balance_right.induct) (auto simp: balance_inv2 balance_bheight)
krauss@26192
   560
haftmann@35550
   561
lemma balance_right_inv1: "\<lbrakk>inv1 a; inv1l b; color_of a = B\<rbrakk> \<Longrightarrow> inv1 (balance_right a k x b)"
haftmann@35550
   562
by (induct a k x b rule: balance_right.induct) (simp add: balance_inv1)+
krauss@26192
   563
haftmann@35550
   564
lemma balance_right_inv1l: "\<lbrakk> inv1 lt; inv1l rt \<rbrakk> \<Longrightarrow>inv1l (balance_right lt k x rt)"
haftmann@35550
   565
by (induct lt k x rt rule: balance_right.induct) (auto simp: balance_inv1)
krauss@26192
   566
haftmann@35550
   567
lemma balance_right_sorted: "\<lbrakk> sorted l; sorted r; tree_less k l; tree_greater k r \<rbrakk> \<Longrightarrow> sorted (balance_right l k v r)"
haftmann@35550
   568
apply (induct l k v r rule: balance_right.induct)
haftmann@35534
   569
apply (auto simp:balance_sorted)
haftmann@35534
   570
apply (unfold tree_less_prop tree_greater_prop)
krauss@26192
   571
by force+
krauss@26192
   572
haftmann@35550
   573
lemma balance_right_tree_greater: 
krauss@26192
   574
  fixes k :: "'a::order"
krauss@26192
   575
  assumes "k \<guillemotleft>| a" "k \<guillemotleft>| b" "k < x" 
haftmann@35550
   576
  shows "k \<guillemotleft>| balance_right a x t b"
haftmann@35550
   577
using assms by (induct a x t b rule: balance_right.induct) auto
krauss@26192
   578
haftmann@35550
   579
lemma balance_right_tree_less: 
krauss@26192
   580
  fixes k :: "'a::order"
krauss@26192
   581
  assumes "a |\<guillemotleft> k" "b |\<guillemotleft> k" "x < k" 
haftmann@35550
   582
  shows "balance_right a x t b |\<guillemotleft> k"
haftmann@35550
   583
using assms by (induct a x t b rule: balance_right.induct) auto
krauss@26192
   584
haftmann@35550
   585
lemma balance_right_in_tree:
haftmann@35534
   586
  assumes "inv1 l" "inv1l r" "bheight l = bheight r + 1" "inv2 l" "inv2 r"
haftmann@35550
   587
  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)"
haftmann@35550
   588
using assms by (induct l k v r rule: balance_right.induct) (auto simp: balance_in_tree)
krauss@26192
   589
krauss@26192
   590
fun
haftmann@35550
   591
  combine :: "('a,'b) rbt \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"
krauss@26192
   592
where
haftmann@35550
   593
  "combine Empty x = x" 
haftmann@35550
   594
| "combine x Empty = x" 
haftmann@35550
   595
| "combine (Branch R a k x b) (Branch R c s y d) = (case (combine b c) of
haftmann@35534
   596
                                      Branch R b2 t z c2 \<Rightarrow> (Branch R (Branch R a k x b2) t z (Branch R c2 s y d)) |
haftmann@35534
   597
                                      bc \<Rightarrow> Branch R a k x (Branch R bc s y d))" 
haftmann@35550
   598
| "combine (Branch B a k x b) (Branch B c s y d) = (case (combine b c) of
haftmann@35534
   599
                                      Branch R b2 t z c2 \<Rightarrow> Branch R (Branch B a k x b2) t z (Branch B c2 s y d) |
haftmann@35550
   600
                                      bc \<Rightarrow> balance_left a k x (Branch B bc s y d))" 
haftmann@35550
   601
| "combine a (Branch R b k x c) = Branch R (combine a b) k x c" 
haftmann@35550
   602
| "combine (Branch R a k x b) c = Branch R a k x (combine b c)" 
krauss@26192
   603
haftmann@35550
   604
lemma combine_inv2:
haftmann@35534
   605
  assumes "inv2 lt" "inv2 rt" "bheight lt = bheight rt"
haftmann@35550
   606
  shows "bheight (combine lt rt) = bheight lt" "inv2 (combine lt rt)"
krauss@26192
   607
using assms 
haftmann@35550
   608
by (induct lt rt rule: combine.induct) 
haftmann@35550
   609
   (auto simp: balance_left_inv2_app split: rbt.splits color.splits)
krauss@26192
   610
haftmann@35550
   611
lemma combine_inv1: 
krauss@26192
   612
  assumes "inv1 lt" "inv1 rt"
haftmann@35550
   613
  shows "color_of lt = B \<Longrightarrow> color_of rt = B \<Longrightarrow> inv1 (combine lt rt)"
haftmann@35550
   614
         "inv1l (combine lt rt)"
krauss@26192
   615
using assms 
haftmann@35550
   616
by (induct lt rt rule: combine.induct)
haftmann@35550
   617
   (auto simp: balance_left_inv1 split: rbt.splits color.splits)
krauss@26192
   618
haftmann@35550
   619
lemma combine_tree_greater[simp]: 
krauss@26192
   620
  fixes k :: "'a::linorder"
krauss@26192
   621
  assumes "k \<guillemotleft>| l" "k \<guillemotleft>| r" 
haftmann@35550
   622
  shows "k \<guillemotleft>| combine l r"
krauss@26192
   623
using assms 
haftmann@35550
   624
by (induct l r rule: combine.induct)
haftmann@35550
   625
   (auto simp: balance_left_tree_greater split:rbt.splits color.splits)
krauss@26192
   626
haftmann@35550
   627
lemma combine_tree_less[simp]: 
krauss@26192
   628
  fixes k :: "'a::linorder"
krauss@26192
   629
  assumes "l |\<guillemotleft> k" "r |\<guillemotleft> k" 
haftmann@35550
   630
  shows "combine l r |\<guillemotleft> k"
krauss@26192
   631
using assms 
haftmann@35550
   632
by (induct l r rule: combine.induct)
haftmann@35550
   633
   (auto simp: balance_left_tree_less split:rbt.splits color.splits)
krauss@26192
   634
haftmann@35550
   635
lemma combine_sorted: 
krauss@26192
   636
  fixes k :: "'a::linorder"
haftmann@35534
   637
  assumes "sorted l" "sorted r" "l |\<guillemotleft> k" "k \<guillemotleft>| r"
haftmann@35550
   638
  shows "sorted (combine l r)"
haftmann@35550
   639
using assms proof (induct l r rule: combine.induct)
krauss@26192
   640
  case (3 a x v b c y w d)
krauss@26192
   641
  hence ineqs: "a |\<guillemotleft> x" "x \<guillemotleft>| b" "b |\<guillemotleft> k" "k \<guillemotleft>| c" "c |\<guillemotleft> y" "y \<guillemotleft>| d"
krauss@26192
   642
    by auto
krauss@26192
   643
  with 3
krauss@26192
   644
  show ?case
haftmann@35550
   645
    by (cases "combine b c" rule: rbt_cases)
haftmann@35550
   646
      (auto, (metis combine_tree_greater combine_tree_less ineqs ineqs tree_less_simps(2) tree_greater_simps(2) tree_greater_trans tree_less_trans)+)
krauss@26192
   647
next
krauss@26192
   648
  case (4 a x v b c y w d)
haftmann@35534
   649
  hence "x < k \<and> tree_greater k c" by simp
haftmann@35534
   650
  hence "tree_greater x c" by (blast dest: tree_greater_trans)
haftmann@35550
   651
  with 4 have 2: "tree_greater x (combine b c)" by (simp add: combine_tree_greater)
haftmann@35534
   652
  from 4 have "k < y \<and> tree_less k b" by simp
haftmann@35534
   653
  hence "tree_less y b" by (blast dest: tree_less_trans)
haftmann@35550
   654
  with 4 have 3: "tree_less y (combine b c)" by (simp add: combine_tree_less)
krauss@26192
   655
  show ?case
haftmann@35550
   656
  proof (cases "combine b c" rule: rbt_cases)
krauss@26192
   657
    case Empty
haftmann@35534
   658
    from 4 have "x < y \<and> tree_greater y d" by auto
haftmann@35534
   659
    hence "tree_greater x d" by (blast dest: tree_greater_trans)
haftmann@35534
   660
    with 4 Empty have "sorted a" and "sorted (Branch B Empty y w d)" and "tree_less x a" and "tree_greater x (Branch B Empty y w d)" by auto
haftmann@35550
   661
    with Empty show ?thesis by (simp add: balance_left_sorted)
krauss@26192
   662
  next
krauss@26192
   663
    case (Red lta va ka rta)
haftmann@35534
   664
    with 2 4 have "x < va \<and> tree_less x a" by simp
haftmann@35534
   665
    hence 5: "tree_less va a" by (blast dest: tree_less_trans)
haftmann@35534
   666
    from Red 3 4 have "va < y \<and> tree_greater y d" by simp
haftmann@35534
   667
    hence "tree_greater va d" by (blast dest: tree_greater_trans)
krauss@26192
   668
    with Red 2 3 4 5 show ?thesis by simp
krauss@26192
   669
  next
krauss@26192
   670
    case (Black lta va ka rta)
haftmann@35534
   671
    from 4 have "x < y \<and> tree_greater y d" by auto
haftmann@35534
   672
    hence "tree_greater x d" by (blast dest: tree_greater_trans)
haftmann@35550
   673
    with Black 2 3 4 have "sorted a" and "sorted (Branch B (combine b c) y w d)" and "tree_less x a" and "tree_greater x (Branch B (combine b c) y w d)" by auto
haftmann@35550
   674
    with Black show ?thesis by (simp add: balance_left_sorted)
krauss@26192
   675
  qed
krauss@26192
   676
next
krauss@26192
   677
  case (5 va vb vd vc b x w c)
haftmann@35534
   678
  hence "k < x \<and> tree_less k (Branch B va vb vd vc)" by simp
haftmann@35534
   679
  hence "tree_less x (Branch B va vb vd vc)" by (blast dest: tree_less_trans)
haftmann@35550
   680
  with 5 show ?case by (simp add: combine_tree_less)
krauss@26192
   681
next
krauss@26192
   682
  case (6 a x v b va vb vd vc)
haftmann@35534
   683
  hence "x < k \<and> tree_greater k (Branch B va vb vd vc)" by simp
haftmann@35534
   684
  hence "tree_greater x (Branch B va vb vd vc)" by (blast dest: tree_greater_trans)
haftmann@35550
   685
  with 6 show ?case by (simp add: combine_tree_greater)
krauss@26192
   686
qed simp+
krauss@26192
   687
haftmann@35550
   688
lemma combine_in_tree: 
haftmann@35534
   689
  assumes "inv2 l" "inv2 r" "bheight l = bheight r" "inv1 l" "inv1 r"
haftmann@35550
   690
  shows "entry_in_tree k v (combine l r) = (entry_in_tree k v l \<or> entry_in_tree k v r)"
krauss@26192
   691
using assms 
haftmann@35550
   692
proof (induct l r rule: combine.induct)
krauss@26192
   693
  case (4 _ _ _ b c)
haftmann@35550
   694
  hence a: "bheight (combine b c) = bheight b" by (simp add: combine_inv2)
haftmann@35550
   695
  from 4 have b: "inv1l (combine b c)" by (simp add: combine_inv1)
krauss@26192
   696
krauss@26192
   697
  show ?case
haftmann@35550
   698
  proof (cases "combine b c" rule: rbt_cases)
krauss@26192
   699
    case Empty
haftmann@35550
   700
    with 4 a show ?thesis by (auto simp: balance_left_in_tree)
krauss@26192
   701
  next
krauss@26192
   702
    case (Red lta ka va rta)
krauss@26192
   703
    with 4 show ?thesis by auto
krauss@26192
   704
  next
krauss@26192
   705
    case (Black lta ka va rta)
haftmann@35550
   706
    with a b 4  show ?thesis by (auto simp: balance_left_in_tree)
krauss@26192
   707
  qed 
krauss@26192
   708
qed (auto split: rbt.splits color.splits)
krauss@26192
   709
krauss@26192
   710
fun
haftmann@35550
   711
  del_from_left :: "('a\<Colon>linorder) \<Rightarrow> ('a,'b) rbt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt" and
haftmann@35550
   712
  del_from_right :: "('a\<Colon>linorder) \<Rightarrow> ('a,'b) rbt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt" and
krauss@26192
   713
  del :: "('a\<Colon>linorder) \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"
krauss@26192
   714
where
krauss@26192
   715
  "del x Empty = Empty" |
haftmann@35550
   716
  "del x (Branch c a y s b) = (if x < y then del_from_left x a y s b else (if x > y then del_from_right x a y s b else combine a b))" |
haftmann@35550
   717
  "del_from_left x (Branch B lt z v rt) y s b = balance_left (del x (Branch B lt z v rt)) y s b" |
haftmann@35550
   718
  "del_from_left x a y s b = Branch R (del x a) y s b" |
haftmann@35550
   719
  "del_from_right x a y s (Branch B lt z v rt) = balance_right a y s (del x (Branch B lt z v rt))" | 
haftmann@35550
   720
  "del_from_right x a y s b = Branch R a y s (del x b)"
krauss@26192
   721
krauss@26192
   722
lemma 
krauss@26192
   723
  assumes "inv2 lt" "inv1 lt"
krauss@26192
   724
  shows
haftmann@35534
   725
  "\<lbrakk>inv2 rt; bheight lt = bheight rt; inv1 rt\<rbrakk> \<Longrightarrow>
haftmann@35550
   726
  inv2 (del_from_left x lt k v rt) \<and> bheight (del_from_left x lt k v rt) = bheight lt \<and> (color_of lt = B \<and> color_of rt = B \<and> inv1 (del_from_left x lt k v rt) \<or> (color_of lt \<noteq> B \<or> color_of rt \<noteq> B) \<and> inv1l (del_from_left x lt k v rt))"
haftmann@35534
   727
  and "\<lbrakk>inv2 rt; bheight lt = bheight rt; inv1 rt\<rbrakk> \<Longrightarrow>
haftmann@35550
   728
  inv2 (del_from_right x lt k v rt) \<and> bheight (del_from_right x lt k v rt) = bheight lt \<and> (color_of lt = B \<and> color_of rt = B \<and> inv1 (del_from_right x lt k v rt) \<or> (color_of lt \<noteq> B \<or> color_of rt \<noteq> B) \<and> inv1l (del_from_right x lt k v rt))"
haftmann@35534
   729
  and del_inv1_inv2: "inv2 (del x lt) \<and> (color_of lt = R \<and> bheight (del x lt) = bheight lt \<and> inv1 (del x lt) 
haftmann@35534
   730
  \<or> color_of lt = B \<and> bheight (del x lt) = bheight lt - 1 \<and> inv1l (del x lt))"
krauss@26192
   731
using assms
haftmann@35550
   732
proof (induct x lt k v rt and x lt k v rt and x lt rule: del_from_left_del_from_right_del.induct)
krauss@26192
   733
case (2 y c _ y')
krauss@26192
   734
  have "y = y' \<or> y < y' \<or> y > y'" by auto
krauss@26192
   735
  thus ?case proof (elim disjE)
krauss@26192
   736
    assume "y = y'"
haftmann@35550
   737
    with 2 show ?thesis by (cases c) (simp add: combine_inv2 combine_inv1)+
krauss@26192
   738
  next
krauss@26192
   739
    assume "y < y'"
krauss@26192
   740
    with 2 show ?thesis by (cases c) auto
krauss@26192
   741
  next
krauss@26192
   742
    assume "y' < y"
krauss@26192
   743
    with 2 show ?thesis by (cases c) auto
krauss@26192
   744
  qed
krauss@26192
   745
next
krauss@26192
   746
  case (3 y lt z v rta y' ss bb) 
haftmann@35550
   747
  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)+
krauss@26192
   748
next
krauss@26192
   749
  case (5 y a y' ss lt z v rta)
haftmann@35550
   750
  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)+
krauss@26192
   751
next
haftmann@35534
   752
  case ("6_1" y a y' ss) thus ?case by (cases "color_of a = B \<and> color_of Empty = B") simp+
krauss@26192
   753
qed auto
krauss@26192
   754
krauss@26192
   755
lemma 
haftmann@35550
   756
  del_from_left_tree_less: "\<lbrakk>tree_less v lt; tree_less v rt; k < v\<rbrakk> \<Longrightarrow> tree_less v (del_from_left x lt k y rt)"
haftmann@35550
   757
  and del_from_right_tree_less: "\<lbrakk>tree_less v lt; tree_less v rt; k < v\<rbrakk> \<Longrightarrow> tree_less v (del_from_right x lt k y rt)"
haftmann@35534
   758
  and del_tree_less: "tree_less v lt \<Longrightarrow> tree_less v (del x lt)"
haftmann@35550
   759
by (induct x lt k y rt and x lt k y rt and x lt rule: del_from_left_del_from_right_del.induct) 
haftmann@35550
   760
   (auto simp: balance_left_tree_less balance_right_tree_less)
krauss@26192
   761
haftmann@35550
   762
lemma del_from_left_tree_greater: "\<lbrakk>tree_greater v lt; tree_greater v rt; k > v\<rbrakk> \<Longrightarrow> tree_greater v (del_from_left x lt k y rt)"
haftmann@35550
   763
  and del_from_right_tree_greater: "\<lbrakk>tree_greater v lt; tree_greater v rt; k > v\<rbrakk> \<Longrightarrow> tree_greater v (del_from_right x lt k y rt)"
haftmann@35534
   764
  and del_tree_greater: "tree_greater v lt \<Longrightarrow> tree_greater v (del x lt)"
haftmann@35550
   765
by (induct x lt k y rt and x lt k y rt and x lt rule: del_from_left_del_from_right_del.induct)
haftmann@35550
   766
   (auto simp: balance_left_tree_greater balance_right_tree_greater)
krauss@26192
   767
haftmann@35550
   768
lemma "\<lbrakk>sorted lt; sorted rt; tree_less k lt; tree_greater k rt\<rbrakk> \<Longrightarrow> sorted (del_from_left x lt k y rt)"
haftmann@35550
   769
  and "\<lbrakk>sorted lt; sorted rt; tree_less k lt; tree_greater k rt\<rbrakk> \<Longrightarrow> sorted (del_from_right x lt k y rt)"
haftmann@35534
   770
  and del_sorted: "sorted lt \<Longrightarrow> sorted (del x lt)"
haftmann@35550
   771
proof (induct x lt k y rt and x lt k y rt and x lt rule: del_from_left_del_from_right_del.induct)
krauss@26192
   772
  case (3 x lta zz v rta yy ss bb)
haftmann@35534
   773
  from 3 have "tree_less yy (Branch B lta zz v rta)" by simp
haftmann@35534
   774
  hence "tree_less yy (del x (Branch B lta zz v rta))" by (rule del_tree_less)
haftmann@35550
   775
  with 3 show ?case by (simp add: balance_left_sorted)
krauss@26192
   776
next
krauss@26192
   777
  case ("4_2" x vaa vbb vdd vc yy ss bb)
haftmann@35534
   778
  hence "tree_less yy (Branch R vaa vbb vdd vc)" by simp
haftmann@35534
   779
  hence "tree_less yy (del x (Branch R vaa vbb vdd vc))" by (rule del_tree_less)
krauss@26192
   780
  with "4_2" show ?case by simp
krauss@26192
   781
next
krauss@26192
   782
  case (5 x aa yy ss lta zz v rta) 
haftmann@35534
   783
  hence "tree_greater yy (Branch B lta zz v rta)" by simp
haftmann@35534
   784
  hence "tree_greater yy (del x (Branch B lta zz v rta))" by (rule del_tree_greater)
haftmann@35550
   785
  with 5 show ?case by (simp add: balance_right_sorted)
krauss@26192
   786
next
krauss@26192
   787
  case ("6_2" x aa yy ss vaa vbb vdd vc)
haftmann@35534
   788
  hence "tree_greater yy (Branch R vaa vbb vdd vc)" by simp
haftmann@35534
   789
  hence "tree_greater yy (del x (Branch R vaa vbb vdd vc))" by (rule del_tree_greater)
krauss@26192
   790
  with "6_2" show ?case by simp
haftmann@35550
   791
qed (auto simp: combine_sorted)
krauss@26192
   792
haftmann@35550
   793
lemma "\<lbrakk>sorted lt; sorted rt; tree_less kt lt; tree_greater kt rt; inv1 lt; inv1 rt; inv2 lt; inv2 rt; bheight lt = bheight rt; x < kt\<rbrakk> \<Longrightarrow> entry_in_tree k v (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)))"
haftmann@35550
   794
  and "\<lbrakk>sorted lt; sorted rt; tree_less kt lt; tree_greater kt rt; inv1 lt; inv1 rt; inv2 lt; inv2 rt; bheight lt = bheight rt; x > kt\<rbrakk> \<Longrightarrow> entry_in_tree k v (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)))"
haftmann@35550
   795
  and del_in_tree: "\<lbrakk>sorted t; inv1 t; inv2 t\<rbrakk> \<Longrightarrow> entry_in_tree k v (del x t) = (False \<or> (x \<noteq> k \<and> entry_in_tree k v t))"
haftmann@35550
   796
proof (induct x lt kt y rt and x lt kt y rt and x t rule: del_from_left_del_from_right_del.induct)
krauss@26192
   797
  case (2 xx c aa yy ss bb)
krauss@26192
   798
  have "xx = yy \<or> xx < yy \<or> xx > yy" by auto
krauss@26192
   799
  from this 2 show ?case proof (elim disjE)
krauss@26192
   800
    assume "xx = yy"
krauss@26192
   801
    with 2 show ?thesis proof (cases "xx = k")
krauss@26192
   802
      case True
haftmann@35534
   803
      from 2 `xx = yy` `xx = k` have "sorted (Branch c aa yy ss bb) \<and> k = yy" by simp
haftmann@35534
   804
      hence "\<not> entry_in_tree k v aa" "\<not> entry_in_tree k v bb" by (auto simp: tree_less_nit tree_greater_prop)
haftmann@35550
   805
      with `xx = yy` 2 `xx = k` show ?thesis by (simp add: combine_in_tree)
haftmann@35550
   806
    qed (simp add: combine_in_tree)
krauss@26192
   807
  qed simp+
krauss@26192
   808
next    
krauss@26192
   809
  case (3 xx lta zz vv rta yy ss bb)
haftmann@35534
   810
  def mt[simp]: mt == "Branch B lta zz vv rta"
krauss@26192
   811
  from 3 have "inv2 mt \<and> inv1 mt" by simp
haftmann@35534
   812
  hence "inv2 (del xx mt) \<and> (color_of mt = R \<and> bheight (del xx mt) = bheight mt \<and> inv1 (del xx mt) \<or> color_of mt = B \<and> bheight (del xx mt) = bheight mt - 1 \<and> inv1l (del xx mt))" by (blast dest: del_inv1_inv2)
haftmann@35550
   813
  with 3 have 4: "entry_in_tree k v (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)
krauss@26192
   814
  thus ?case proof (cases "xx = k")
krauss@26192
   815
    case True
haftmann@35534
   816
    from 3 True have "tree_greater yy bb \<and> yy > k" by simp
haftmann@35534
   817
    hence "tree_greater k bb" by (blast dest: tree_greater_trans)
haftmann@35534
   818
    with 3 4 True show ?thesis by (auto simp: tree_greater_nit)
krauss@26192
   819
  qed auto
krauss@26192
   820
next
krauss@26192
   821
  case ("4_1" xx yy ss bb)
krauss@26192
   822
  show ?case proof (cases "xx = k")
krauss@26192
   823
    case True
haftmann@35534
   824
    with "4_1" have "tree_greater yy bb \<and> k < yy" by simp
haftmann@35534
   825
    hence "tree_greater k bb" by (blast dest: tree_greater_trans)
krauss@26192
   826
    with "4_1" `xx = k` 
haftmann@35534
   827
   have "entry_in_tree k v (Branch R Empty yy ss bb) = entry_in_tree k v Empty" by (auto simp: tree_greater_nit)
krauss@26192
   828
    thus ?thesis by auto
krauss@26192
   829
  qed simp+
krauss@26192
   830
next
krauss@26192
   831
  case ("4_2" xx vaa vbb vdd vc yy ss bb)
krauss@26192
   832
  thus ?case proof (cases "xx = k")
krauss@26192
   833
    case True
haftmann@35534
   834
    with "4_2" have "k < yy \<and> tree_greater yy bb" by simp
haftmann@35534
   835
    hence "tree_greater k bb" by (blast dest: tree_greater_trans)
haftmann@35534
   836
    with True "4_2" show ?thesis by (auto simp: tree_greater_nit)
haftmann@35550
   837
  qed auto
krauss@26192
   838
next
krauss@26192
   839
  case (5 xx aa yy ss lta zz vv rta)
haftmann@35534
   840
  def mt[simp]: mt == "Branch B lta zz vv rta"
krauss@26192
   841
  from 5 have "inv2 mt \<and> inv1 mt" by simp
haftmann@35534
   842
  hence "inv2 (del xx mt) \<and> (color_of mt = R \<and> bheight (del xx mt) = bheight mt \<and> inv1 (del xx mt) \<or> color_of mt = B \<and> bheight (del xx mt) = bheight mt - 1 \<and> inv1l (del xx mt))" by (blast dest: del_inv1_inv2)
haftmann@35550
   843
  with 5 have 3: "entry_in_tree k v (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)
krauss@26192
   844
  thus ?case proof (cases "xx = k")
krauss@26192
   845
    case True
haftmann@35534
   846
    from 5 True have "tree_less yy aa \<and> yy < k" by simp
haftmann@35534
   847
    hence "tree_less k aa" by (blast dest: tree_less_trans)
haftmann@35534
   848
    with 3 5 True show ?thesis by (auto simp: tree_less_nit)
krauss@26192
   849
  qed auto
krauss@26192
   850
next
krauss@26192
   851
  case ("6_1" xx aa yy ss)
krauss@26192
   852
  show ?case proof (cases "xx = k")
krauss@26192
   853
    case True
haftmann@35534
   854
    with "6_1" have "tree_less yy aa \<and> k > yy" by simp
haftmann@35534
   855
    hence "tree_less k aa" by (blast dest: tree_less_trans)
haftmann@35534
   856
    with "6_1" `xx = k` show ?thesis by (auto simp: tree_less_nit)
krauss@26192
   857
  qed simp
krauss@26192
   858
next
krauss@26192
   859
  case ("6_2" xx aa yy ss vaa vbb vdd vc)
krauss@26192
   860
  thus ?case proof (cases "xx = k")
krauss@26192
   861
    case True
haftmann@35534
   862
    with "6_2" have "k > yy \<and> tree_less yy aa" by simp
haftmann@35534
   863
    hence "tree_less k aa" by (blast dest: tree_less_trans)
haftmann@35534
   864
    with True "6_2" show ?thesis by (auto simp: tree_less_nit)
haftmann@35550
   865
  qed auto
krauss@26192
   866
qed simp
krauss@26192
   867
krauss@26192
   868
krauss@26192
   869
definition delete where
krauss@26192
   870
  delete_def: "delete k t = paint B (del k t)"
krauss@26192
   871
haftmann@35550
   872
theorem delete_is_rbt [simp]: assumes "is_rbt t" shows "is_rbt (delete k t)"
krauss@26192
   873
proof -
haftmann@35534
   874
  from assms have "inv2 t" and "inv1 t" unfolding is_rbt_def by auto 
haftmann@35534
   875
  hence "inv2 (del k t) \<and> (color_of t = R \<and> bheight (del k t) = bheight t \<and> inv1 (del k t) \<or> color_of t = B \<and> bheight (del k t) = bheight t - 1 \<and> inv1l (del k t))" by (rule del_inv1_inv2)
haftmann@35534
   876
  hence "inv2 (del k t) \<and> inv1l (del k t)" by (cases "color_of t") auto
krauss@26192
   877
  with assms show ?thesis
haftmann@35534
   878
    unfolding is_rbt_def delete_def
haftmann@35534
   879
    by (auto intro: paint_sorted del_sorted)
krauss@26192
   880
qed
krauss@26192
   881
haftmann@35550
   882
lemma delete_in_tree: 
haftmann@35534
   883
  assumes "is_rbt t" 
haftmann@35534
   884
  shows "entry_in_tree k v (delete x t) = (x \<noteq> k \<and> entry_in_tree k v t)"
haftmann@35534
   885
  using assms unfolding is_rbt_def delete_def
haftmann@35550
   886
  by (auto simp: del_in_tree)
krauss@26192
   887
haftmann@35534
   888
lemma lookup_delete:
haftmann@35534
   889
  assumes is_rbt: "is_rbt t"
haftmann@35534
   890
  shows "lookup (delete k t) = (lookup t)|`(-{k})"
krauss@26192
   891
proof
krauss@26192
   892
  fix x
haftmann@35534
   893
  show "lookup (delete k t) x = (lookup t |` (-{k})) x" 
krauss@26192
   894
  proof (cases "x = k")
krauss@26192
   895
    assume "x = k" 
haftmann@35534
   896
    with is_rbt show ?thesis
haftmann@35550
   897
      by (cases "lookup (delete k t) k") (auto simp: lookup_in_tree delete_in_tree)
krauss@26192
   898
  next
krauss@26192
   899
    assume "x \<noteq> k"
krauss@26192
   900
    thus ?thesis
haftmann@35550
   901
      by auto (metis is_rbt delete_is_rbt delete_in_tree is_rbt_sorted lookup_from_in_tree)
krauss@26192
   902
  qed
krauss@26192
   903
qed
krauss@26192
   904
haftmann@35550
   905
krauss@26192
   906
subsection {* Union *}
krauss@26192
   907
krauss@26192
   908
primrec
haftmann@35550
   909
  union_with_key :: "('a\<Colon>linorder \<Rightarrow> 'b \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"
krauss@26192
   910
where
haftmann@35550
   911
  "union_with_key f t Empty = t"
haftmann@35550
   912
| "union_with_key f t (Branch c lt k v rt) = union_with_key f (union_with_key f (insert_with_key f k v t) lt) rt"
krauss@26192
   913
haftmann@35550
   914
lemma unionwk_sorted: "sorted lt \<Longrightarrow> sorted (union_with_key f lt rt)" 
haftmann@35534
   915
  by (induct rt arbitrary: lt) (auto simp: insertwk_sorted)
haftmann@35550
   916
theorem unionwk_is_rbt[simp]: "is_rbt lt \<Longrightarrow> is_rbt (union_with_key f lt rt)" 
haftmann@35534
   917
  by (induct rt arbitrary: lt) (simp add: insertwk_is_rbt)+
krauss@26192
   918
krauss@26192
   919
definition
haftmann@35550
   920
  union_with where
haftmann@35550
   921
  "union_with f = union_with_key (\<lambda>_. f)"
krauss@26192
   922
haftmann@35550
   923
theorem unionw_is_rbt: "is_rbt lt \<Longrightarrow> is_rbt (union_with f lt rt)" unfolding union_with_def by simp
krauss@26192
   924
krauss@26192
   925
definition union where
haftmann@35550
   926
  "union = union_with_key (%_ _ rv. rv)"
krauss@26192
   927
haftmann@35534
   928
theorem union_is_rbt: "is_rbt lt \<Longrightarrow> is_rbt (union lt rt)" unfolding union_def by simp
krauss@26192
   929
haftmann@35534
   930
lemma union_Branch[simp]:
haftmann@35534
   931
  "union t (Branch c lt k v rt) = union (union (insert k v t) lt) rt"
haftmann@35534
   932
  unfolding union_def insert_def
krauss@26192
   933
  by simp
krauss@26192
   934
haftmann@35534
   935
lemma lookup_union:
haftmann@35534
   936
  assumes "is_rbt s" "sorted t"
haftmann@35534
   937
  shows "lookup (union s t) = lookup s ++ lookup t"
krauss@26192
   938
using assms
krauss@26192
   939
proof (induct t arbitrary: s)
krauss@26192
   940
  case Empty thus ?case by (auto simp: union_def)
krauss@26192
   941
next
haftmann@35534
   942
  case (Branch c l k v r s)
haftmann@35550
   943
  then have "sorted r" "sorted l" "l |\<guillemotleft> k" "k \<guillemotleft>| r" by auto
krauss@26192
   944
haftmann@35534
   945
  have meq: "lookup s(k \<mapsto> v) ++ lookup l ++ lookup r =
haftmann@35534
   946
    lookup s ++
haftmann@35534
   947
    (\<lambda>a. if a < k then lookup l a
haftmann@35534
   948
    else if k < a then lookup r a else Some v)" (is "?m1 = ?m2")
krauss@26192
   949
  proof (rule ext)
krauss@26192
   950
    fix a
krauss@26192
   951
krauss@26192
   952
   have "k < a \<or> k = a \<or> k > a" by auto
krauss@26192
   953
    thus "?m1 a = ?m2 a"
krauss@26192
   954
    proof (elim disjE)
krauss@26192
   955
      assume "k < a"
haftmann@35534
   956
      with `l |\<guillemotleft> k` have "l |\<guillemotleft> a" by (rule tree_less_trans)
krauss@26192
   957
      with `k < a` show ?thesis
krauss@26192
   958
        by (auto simp: map_add_def split: option.splits)
krauss@26192
   959
    next
krauss@26192
   960
      assume "k = a"
krauss@26192
   961
      with `l |\<guillemotleft> k` `k \<guillemotleft>| r` 
krauss@26192
   962
      show ?thesis by (auto simp: map_add_def)
krauss@26192
   963
    next
krauss@26192
   964
      assume "a < k"
haftmann@35534
   965
      from this `k \<guillemotleft>| r` have "a \<guillemotleft>| r" by (rule tree_greater_trans)
krauss@26192
   966
      with `a < k` show ?thesis
krauss@26192
   967
        by (auto simp: map_add_def split: option.splits)
krauss@26192
   968
    qed
krauss@26192
   969
  qed
krauss@26192
   970
haftmann@35550
   971
  from Branch have is_rbt: "is_rbt (RBT.union (RBT.insert k v s) l)"
haftmann@35550
   972
    by (auto intro: union_is_rbt insert_is_rbt)
haftmann@35550
   973
  with Branch have IHs:
haftmann@35534
   974
    "lookup (union (union (insert k v s) l) r) = lookup (union (insert k v s) l) ++ lookup r"
haftmann@35534
   975
    "lookup (union (insert k v s) l) = lookup (insert k v s) ++ lookup l"
haftmann@35550
   976
    by auto
krauss@26192
   977
  
krauss@26192
   978
  with meq show ?case
haftmann@35534
   979
    by (auto simp: lookup_insert[OF Branch(3)])
haftmann@35550
   980
krauss@26192
   981
qed
krauss@26192
   982
haftmann@35550
   983
haftmann@35550
   984
subsection {* Modifying existing entries *}
krauss@26192
   985
krauss@26192
   986
primrec
haftmann@35550
   987
  map_entry :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> ('a\<Colon>linorder) \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"
krauss@26192
   988
where
haftmann@35550
   989
  "map_entry f k Empty = Empty"
haftmann@35550
   990
| "map_entry f k (Branch c lt x v rt) = (if k < x then (Branch c (map_entry f k lt) x v rt) else if k > x then (Branch c lt x v (map_entry f k rt)) else (Branch c lt x (f x v) rt))"
krauss@26192
   991
haftmann@35550
   992
lemma map_entrywk_color_of: "color_of (map_entry f k t) = color_of t" by (induct t) simp+
haftmann@35550
   993
lemma map_entrywk_inv1: "inv1 (map_entry f k t) = inv1 t" by (induct t) (simp add: map_entrywk_color_of)+
haftmann@35550
   994
lemma map_entrywk_inv2: "inv2 (map_entry f k t) = inv2 t" "bheight (map_entry f k t) = bheight t" by (induct t) simp+
haftmann@35550
   995
lemma map_entrywk_tree_greater: "tree_greater k (map_entry f kk t) = tree_greater k t" by (induct t) simp+
haftmann@35550
   996
lemma map_entrywk_tree_less: "tree_less k (map_entry f kk t) = tree_less k t" by (induct t) simp+
haftmann@35550
   997
lemma map_entrywk_sorted: "sorted (map_entry f k t) = sorted t" by (induct t) (simp add: map_entrywk_tree_less map_entrywk_tree_greater)+
krauss@26192
   998
haftmann@35550
   999
theorem map_entrywk_is_rbt [simp]: "is_rbt (map_entry f k t) = is_rbt t" 
haftmann@35550
  1000
unfolding is_rbt_def by (simp add: map_entrywk_inv2 map_entrywk_color_of map_entrywk_sorted map_entrywk_inv1 )
krauss@26192
  1001
haftmann@35550
  1002
theorem map_entry_map [simp]:
haftmann@35550
  1003
  "lookup (map_entry f k t) x = 
haftmann@35534
  1004
  (if x = k then case lookup t x of None \<Rightarrow> None | Some y \<Rightarrow> Some (f k y)
haftmann@35534
  1005
            else lookup t x)"
krauss@26192
  1006
by (induct t arbitrary: x) (auto split:option.splits)
krauss@26192
  1007
krauss@26192
  1008
haftmann@35550
  1009
subsection {* Mapping all entries *}
krauss@26192
  1010
krauss@26192
  1011
primrec
haftmann@35550
  1012
  map :: "('a::linorder \<Rightarrow> 'b \<Rightarrow> 'c) \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'c) rbt"
krauss@26192
  1013
where
haftmann@35550
  1014
  "map f Empty = Empty"
haftmann@35550
  1015
| "map f (Branch c lt k v rt) = Branch c (map f lt) k (f k v) (map f rt)"
krauss@32237
  1016
haftmann@35550
  1017
lemma map_entries [simp]: "entries (map f t) = List.map (\<lambda>(k, v). (k, f k v)) (entries t)"
haftmann@35550
  1018
  by (induct t) auto
haftmann@35550
  1019
lemma map_keys [simp]: "keys (map f t) = keys t" by (simp add: keys_def split_def)
haftmann@35550
  1020
lemma map_tree_greater: "tree_greater k (map f t) = tree_greater k t" by (induct t) simp+
haftmann@35550
  1021
lemma map_tree_less: "tree_less k (map f t) = tree_less k t" by (induct t) simp+
haftmann@35550
  1022
lemma map_sorted: "sorted (map f t) = sorted t"  by (induct t) (simp add: map_tree_less map_tree_greater)+
haftmann@35550
  1023
lemma map_color_of: "color_of (map f t) = color_of t" by (induct t) simp+
haftmann@35550
  1024
lemma map_inv1: "inv1 (map f t) = inv1 t" by (induct t) (simp add: map_color_of)+
haftmann@35550
  1025
lemma map_inv2: "inv2 (map f t) = inv2 t" "bheight (map f t) = bheight t" by (induct t) simp+
haftmann@35550
  1026
theorem map_is_rbt [simp]: "is_rbt (map f t) = is_rbt t" 
haftmann@35550
  1027
unfolding is_rbt_def by (simp add: map_inv1 map_inv2 map_sorted map_color_of)
krauss@32237
  1028
haftmann@35550
  1029
theorem lookup_map [simp]: "lookup (map f t) x = Option.map (f x) (lookup t x)"
krauss@26192
  1030
by (induct t) auto
krauss@26192
  1031
haftmann@35550
  1032
haftmann@35550
  1033
subsection {* Folding over entries *}
haftmann@35550
  1034
haftmann@35550
  1035
definition fold :: "('a \<Rightarrow> 'b \<Rightarrow> 'c \<Rightarrow> 'c) \<Rightarrow> ('a, 'b) rbt \<Rightarrow> 'c \<Rightarrow> 'c" where
haftmann@35550
  1036
  "fold f t s = foldl (\<lambda>s (k, v). f k v s) s (entries t)"
krauss@26192
  1037
haftmann@35550
  1038
lemma fold_simps [simp, code]:
haftmann@35550
  1039
  "fold f Empty = id"
haftmann@35550
  1040
  "fold f (Branch c lt k v rt) = fold f rt \<circ> f k v \<circ> fold f lt"
haftmann@35550
  1041
  by (simp_all add: fold_def expand_fun_eq)
haftmann@35534
  1042
haftmann@35550
  1043
hide (open) const Empty insert delete entries lookup map_entry map fold union sorted
krauss@26192
  1044
(*>*)
krauss@26192
  1045
krauss@26192
  1046
text {* 
krauss@26192
  1047
  This theory defines purely functional red-black trees which can be
krauss@26192
  1048
  used as an efficient representation of finite maps.
krauss@26192
  1049
*}
krauss@26192
  1050
haftmann@35550
  1051
krauss@26192
  1052
subsection {* Data type and invariant *}
krauss@26192
  1053
krauss@26192
  1054
text {*
krauss@26192
  1055
  The type @{typ "('k, 'v) rbt"} denotes red-black trees with keys of
krauss@26192
  1056
  type @{typ "'k"} and values of type @{typ "'v"}. To function
haftmann@35534
  1057
  properly, the key type musorted belong to the @{text "linorder"} class.
krauss@26192
  1058
krauss@26192
  1059
  A value @{term t} of this type is a valid red-black tree if it
haftmann@35534
  1060
  satisfies the invariant @{text "is_rbt t"}.
krauss@26192
  1061
  This theory provides lemmas to prove that the invariant is
krauss@26192
  1062
  satisfied throughout the computation.
krauss@26192
  1063
haftmann@35534
  1064
  The interpretation function @{const "RBT.lookup"} returns the partial
krauss@26192
  1065
  map represented by a red-black tree:
haftmann@35534
  1066
  @{term_type[display] "RBT.lookup"}
krauss@26192
  1067
krauss@26192
  1068
  This function should be used for reasoning about the semantics of the RBT
krauss@26192
  1069
  operations. Furthermore, it implements the lookup functionality for
haftmann@35534
  1070
  the data sortedructure: It is executable and the lookup is performed in
krauss@26192
  1071
  $O(\log n)$.  
krauss@26192
  1072
*}
krauss@26192
  1073
haftmann@35550
  1074
krauss@26192
  1075
subsection {* Operations *}
krauss@26192
  1076
krauss@26192
  1077
text {*
krauss@26192
  1078
  Currently, the following operations are supported:
krauss@26192
  1079
haftmann@35534
  1080
  @{term_type[display] "RBT.Empty"}
krauss@26192
  1081
  Returns the empty tree. $O(1)$
krauss@26192
  1082
haftmann@35534
  1083
  @{term_type[display] "RBT.insert"}
krauss@26192
  1084
  Updates the map at a given position. $O(\log n)$
krauss@26192
  1085
haftmann@35534
  1086
  @{term_type[display] "RBT.delete"}
krauss@26192
  1087
  Deletes a map entry at a given position. $O(\log n)$
krauss@26192
  1088
haftmann@35534
  1089
  @{term_type[display] "RBT.union"}
krauss@26192
  1090
  Forms the union of two trees, preferring entries from the first one.
krauss@26192
  1091
haftmann@35534
  1092
  @{term_type[display] "RBT.map"}
krauss@26192
  1093
  Maps a function over the values of a map. $O(n)$
krauss@26192
  1094
*}
krauss@26192
  1095
krauss@26192
  1096
krauss@26192
  1097
subsection {* Invariant preservation *}
krauss@26192
  1098
krauss@26192
  1099
text {*
krauss@26192
  1100
  \noindent
haftmann@35534
  1101
  @{thm Empty_is_rbt}\hfill(@{text "Empty_is_rbt"})
krauss@26192
  1102
krauss@26192
  1103
  \noindent
haftmann@35534
  1104
  @{thm insert_is_rbt}\hfill(@{text "insert_is_rbt"})
krauss@26192
  1105
krauss@26192
  1106
  \noindent
haftmann@35534
  1107
  @{thm delete_is_rbt}\hfill(@{text "delete_is_rbt"})
krauss@26192
  1108
krauss@26192
  1109
  \noindent
haftmann@35534
  1110
  @{thm union_is_rbt}\hfill(@{text "union_is_rbt"})
krauss@26192
  1111
krauss@26192
  1112
  \noindent
haftmann@35534
  1113
  @{thm map_is_rbt}\hfill(@{text "map_is_rbt"})
krauss@26192
  1114
*}
krauss@26192
  1115
haftmann@35550
  1116
krauss@26192
  1117
subsection {* Map Semantics *}
krauss@26192
  1118
krauss@26192
  1119
text {*
krauss@26192
  1120
  \noindent
haftmann@35534
  1121
  \underline{@{text "lookup_Empty"}}
haftmann@35534
  1122
  @{thm[display] lookup_Empty}
krauss@26192
  1123
  \vspace{1ex}
krauss@26192
  1124
krauss@26192
  1125
  \noindent
haftmann@35534
  1126
  \underline{@{text "lookup_insert"}}
haftmann@35534
  1127
  @{thm[display] lookup_insert}
krauss@26192
  1128
  \vspace{1ex}
krauss@26192
  1129
krauss@26192
  1130
  \noindent
haftmann@35534
  1131
  \underline{@{text "lookup_delete"}}
haftmann@35534
  1132
  @{thm[display] lookup_delete}
krauss@26192
  1133
  \vspace{1ex}
krauss@26192
  1134
krauss@26192
  1135
  \noindent
haftmann@35534
  1136
  \underline{@{text "lookup_union"}}
haftmann@35534
  1137
  @{thm[display] lookup_union}
krauss@26192
  1138
  \vspace{1ex}
krauss@26192
  1139
krauss@26192
  1140
  \noindent
haftmann@35534
  1141
  \underline{@{text "lookup_map"}}
haftmann@35534
  1142
  @{thm[display] lookup_map}
krauss@26192
  1143
  \vspace{1ex}
krauss@26192
  1144
*}
krauss@26192
  1145
krauss@26192
  1146
end