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