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