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