src/HOL/Library/RBT_Impl.thy
author blanchet
Tue Sep 09 20:51:36 2014 +0200 (2014-09-09)
changeset 58257 0662f35534fe
parent 58249 180f1b3508ed
child 58310 91ea607a34d8
permissions -rw-r--r--
half-ported Imperative HOL to new datatypes
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
haftmann@36147
     6
header {* Implementation of Red-Black Trees *}
krauss@26192
     7
haftmann@36147
     8
theory RBT_Impl
haftmann@45990
     9
imports Main
krauss@26192
    10
begin
krauss@26192
    11
haftmann@36147
    12
text {*
haftmann@36147
    13
  For applications, you should use theory @{text RBT} which defines
haftmann@36147
    14
  an abstract type of red-black tree obeying the invariant.
haftmann@36147
    15
*}
haftmann@36147
    16
haftmann@35550
    17
subsection {* Datatype of RB trees *}
haftmann@35550
    18
blanchet@58249
    19
datatype_new color = R | B
blanchet@58249
    20
datatype_new ('a, 'b) rbt = Empty | Branch color "('a, 'b) rbt" 'a 'b "('a, 'b) rbt"
haftmann@35534
    21
haftmann@35534
    22
lemma rbt_cases:
haftmann@35534
    23
  obtains (Empty) "t = Empty" 
haftmann@35534
    24
  | (Red) l k v r where "t = Branch R l k v r" 
haftmann@35534
    25
  | (Black) l k v r where "t = Branch B l k v r"
haftmann@35534
    26
proof (cases t)
haftmann@35534
    27
  case Empty with that show thesis by blast
haftmann@35534
    28
next
haftmann@35534
    29
  case (Branch c) with that show thesis by (cases c) blast+
haftmann@35534
    30
qed
haftmann@35534
    31
haftmann@35550
    32
subsection {* Tree properties *}
haftmann@35534
    33
haftmann@35550
    34
subsubsection {* Content of a tree *}
haftmann@35550
    35
haftmann@35550
    36
primrec entries :: "('a, 'b) rbt \<Rightarrow> ('a \<times> 'b) list"
haftmann@35534
    37
where 
haftmann@35534
    38
  "entries Empty = []"
haftmann@35534
    39
| "entries (Branch _ l k v r) = entries l @ (k,v) # entries r"
krauss@26192
    40
haftmann@35550
    41
abbreviation (input) entry_in_tree :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) rbt \<Rightarrow> bool"
krauss@26192
    42
where
haftmann@35550
    43
  "entry_in_tree k v t \<equiv> (k, v) \<in> set (entries t)"
haftmann@35550
    44
haftmann@35550
    45
definition keys :: "('a, 'b) rbt \<Rightarrow> 'a list" where
haftmann@35550
    46
  "keys t = map fst (entries t)"
krauss@26192
    47
haftmann@35550
    48
lemma keys_simps [simp, code]:
haftmann@35550
    49
  "keys Empty = []"
haftmann@35550
    50
  "keys (Branch c l k v r) = keys l @ k # keys r"
haftmann@35550
    51
  by (simp_all add: keys_def)
krauss@26192
    52
haftmann@35534
    53
lemma entry_in_tree_keys:
haftmann@35550
    54
  assumes "(k, v) \<in> set (entries t)"
haftmann@35550
    55
  shows "k \<in> set (keys t)"
haftmann@35550
    56
proof -
haftmann@35550
    57
  from assms have "fst (k, v) \<in> fst ` set (entries t)" by (rule imageI)
haftmann@35550
    58
  then show ?thesis by (simp add: keys_def)
haftmann@35550
    59
qed
haftmann@35550
    60
haftmann@35602
    61
lemma keys_entries:
haftmann@35602
    62
  "k \<in> set (keys t) \<longleftrightarrow> (\<exists>v. (k, v) \<in> set (entries t))"
haftmann@35602
    63
  by (auto intro: entry_in_tree_keys) (auto simp add: keys_def)
haftmann@35602
    64
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
haftmann@35550
    69
subsubsection {* Search tree properties *}
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
haftmann@35550
   132
subsubsection {* Tree lookup *}
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))"
Andreas@47450
   158
  have "dom (rbt_lookup (Branch color t1 a b t2)) \<subseteq> ?A" by (auto split: split_if_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
haftmann@35550
   291
subsubsection {* Red-black properties *}
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
haftmann@35534
   308
primrec inv1l :: "('a, 'b) rbt \<Rightarrow> bool" -- {* Weaker version *}
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
krauss@26192
   332
subsection {* Insertion *}
krauss@26192
   333
krauss@26192
   334
fun (* slow, due to massive case splitting *)
krauss@26192
   335
  balance :: "('a,'b) rbt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"
krauss@26192
   336
where
haftmann@35534
   337
  "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
   338
  "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
   339
  "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
   340
  "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
   341
  "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
   342
  "balance a s t b = Branch B a s t b"
krauss@26192
   343
krauss@26192
   344
lemma balance_inv1: "\<lbrakk>inv1l l; inv1l r\<rbrakk> \<Longrightarrow> inv1 (balance l k v r)" 
krauss@26192
   345
  by (induct l k v r rule: balance.induct) auto
krauss@26192
   346
haftmann@35534
   347
lemma balance_bheight: "bheight l = bheight r \<Longrightarrow> bheight (balance l k v r) = Suc (bheight l)"
krauss@26192
   348
  by (induct l k v r rule: balance.induct) auto
krauss@26192
   349
krauss@26192
   350
lemma balance_inv2: 
haftmann@35534
   351
  assumes "inv2 l" "inv2 r" "bheight l = bheight r"
krauss@26192
   352
  shows "inv2 (balance l k v r)"
krauss@26192
   353
  using assms
krauss@26192
   354
  by (induct l k v r rule: balance.induct) auto
krauss@26192
   355
Andreas@47450
   356
context ord begin
Andreas@47450
   357
Andreas@47450
   358
lemma balance_rbt_greater[simp]: "(v \<guillemotleft>| balance a k x b) = (v \<guillemotleft>| a \<and> v \<guillemotleft>| b \<and> v < k)" 
krauss@26192
   359
  by (induct a k x b rule: balance.induct) auto
krauss@26192
   360
Andreas@47450
   361
lemma balance_rbt_less[simp]: "(balance a k x b |\<guillemotleft> v) = (a |\<guillemotleft> v \<and> b |\<guillemotleft> v \<and> k < v)"
krauss@26192
   362
  by (induct a k x b rule: balance.induct) auto
krauss@26192
   363
Andreas@47450
   364
end
Andreas@47450
   365
Andreas@47450
   366
lemma (in linorder) balance_rbt_sorted: 
Andreas@47450
   367
  fixes k :: "'a"
Andreas@47450
   368
  assumes "rbt_sorted l" "rbt_sorted r" "l |\<guillemotleft> k" "k \<guillemotleft>| r"
Andreas@47450
   369
  shows "rbt_sorted (balance l k v r)"
krauss@26192
   370
using assms proof (induct l k v r rule: balance.induct)
krauss@26192
   371
  case ("2_2" a x w b y t c z s va vb vd vc)
haftmann@35534
   372
  hence "y < z \<and> z \<guillemotleft>| Branch B va vb vd vc" 
Andreas@47450
   373
    by (auto simp add: rbt_ord_props)
Andreas@47450
   374
  hence "y \<guillemotleft>| (Branch B va vb vd vc)" by (blast dest: rbt_greater_trans)
krauss@26192
   375
  with "2_2" show ?case by simp
krauss@26192
   376
next
krauss@26192
   377
  case ("3_2" va vb vd vc x w b y s c z)
Andreas@47450
   378
  from "3_2" have "x < y \<and> Branch B va vb vd vc |\<guillemotleft> x" 
haftmann@35534
   379
    by simp
Andreas@47450
   380
  hence "Branch B va vb vd vc |\<guillemotleft> y" by (blast dest: rbt_less_trans)
krauss@26192
   381
  with "3_2" show ?case by simp
krauss@26192
   382
next
krauss@26192
   383
  case ("3_3" x w b y s c z t va vb vd vc)
Andreas@47450
   384
  from "3_3" have "y < z \<and> z \<guillemotleft>| Branch B va vb vd vc" by simp
Andreas@47450
   385
  hence "y \<guillemotleft>| Branch B va vb vd vc" by (blast dest: rbt_greater_trans)
krauss@26192
   386
  with "3_3" show ?case by simp
krauss@26192
   387
next
krauss@26192
   388
  case ("3_4" vd ve vg vf x w b y s c z t va vb vii vc)
Andreas@47450
   389
  hence "x < y \<and> Branch B vd ve vg vf |\<guillemotleft> x" by simp
Andreas@47450
   390
  hence 1: "Branch B vd ve vg vf |\<guillemotleft> y" by (blast dest: rbt_less_trans)
Andreas@47450
   391
  from "3_4" have "y < z \<and> z \<guillemotleft>| Branch B va vb vii vc" by simp
Andreas@47450
   392
  hence "y \<guillemotleft>| Branch B va vb vii vc" by (blast dest: rbt_greater_trans)
krauss@26192
   393
  with 1 "3_4" show ?case by simp
krauss@26192
   394
next
krauss@26192
   395
  case ("4_2" va vb vd vc x w b y s c z t dd)
Andreas@47450
   396
  hence "x < y \<and> Branch B va vb vd vc |\<guillemotleft> x" by simp
Andreas@47450
   397
  hence "Branch B va vb vd vc |\<guillemotleft> y" by (blast dest: rbt_less_trans)
krauss@26192
   398
  with "4_2" show ?case by simp
krauss@26192
   399
next
krauss@26192
   400
  case ("5_2" x w b y s c z t va vb vd vc)
Andreas@47450
   401
  hence "y < z \<and> z \<guillemotleft>| Branch B va vb vd vc" by simp
Andreas@47450
   402
  hence "y \<guillemotleft>| Branch B va vb vd vc" by (blast dest: rbt_greater_trans)
krauss@26192
   403
  with "5_2" show ?case by simp
krauss@26192
   404
next
krauss@26192
   405
  case ("5_3" va vb vd vc x w b y s c z t)
Andreas@47450
   406
  hence "x < y \<and> Branch B va vb vd vc |\<guillemotleft> x" by simp
Andreas@47450
   407
  hence "Branch B va vb vd vc |\<guillemotleft> y" by (blast dest: rbt_less_trans)
krauss@26192
   408
  with "5_3" show ?case by simp
krauss@26192
   409
next
krauss@26192
   410
  case ("5_4" va vb vg vc x w b y s c z t vd ve vii vf)
Andreas@47450
   411
  hence "x < y \<and> Branch B va vb vg vc |\<guillemotleft> x" by simp
Andreas@47450
   412
  hence 1: "Branch B va vb vg vc |\<guillemotleft> y" by (blast dest: rbt_less_trans)
Andreas@47450
   413
  from "5_4" have "y < z \<and> z \<guillemotleft>| Branch B vd ve vii vf" by simp
Andreas@47450
   414
  hence "y \<guillemotleft>| Branch B vd ve vii vf" by (blast dest: rbt_greater_trans)
krauss@26192
   415
  with 1 "5_4" show ?case by simp
krauss@26192
   416
qed simp+
krauss@26192
   417
haftmann@35550
   418
lemma entries_balance [simp]:
haftmann@35550
   419
  "entries (balance l k v r) = entries l @ (k, v) # entries r"
haftmann@35550
   420
  by (induct l k v r rule: balance.induct) auto
krauss@26192
   421
haftmann@35550
   422
lemma keys_balance [simp]: 
haftmann@35550
   423
  "keys (balance l k v r) = keys l @ k # keys r"
haftmann@35550
   424
  by (simp add: keys_def)
haftmann@35550
   425
haftmann@35550
   426
lemma balance_in_tree:  
haftmann@35550
   427
  "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
   428
  by (auto simp add: keys_def)
krauss@26192
   429
Andreas@47450
   430
lemma (in linorder) rbt_lookup_balance[simp]: 
Andreas@47450
   431
fixes k :: "'a"
Andreas@47450
   432
assumes "rbt_sorted l" "rbt_sorted r" "l |\<guillemotleft> k" "k \<guillemotleft>| r"
Andreas@47450
   433
shows "rbt_lookup (balance l k v r) x = rbt_lookup (Branch B l k v r) x"
Andreas@47450
   434
by (rule rbt_lookup_from_in_tree) (auto simp:assms balance_in_tree balance_rbt_sorted)
krauss@26192
   435
krauss@26192
   436
primrec paint :: "color \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"
krauss@26192
   437
where
krauss@26192
   438
  "paint c Empty = Empty"
haftmann@35534
   439
| "paint c (Branch _ l k v r) = Branch c l k v r"
krauss@26192
   440
krauss@26192
   441
lemma paint_inv1l[simp]: "inv1l t \<Longrightarrow> inv1l (paint c t)" by (cases t) auto
krauss@26192
   442
lemma paint_inv1[simp]: "inv1l t \<Longrightarrow> inv1 (paint B t)" by (cases t) auto
krauss@26192
   443
lemma paint_inv2[simp]: "inv2 t \<Longrightarrow> inv2 (paint c t)" by (cases t) auto
haftmann@35534
   444
lemma paint_color_of[simp]: "color_of (paint B t) = B" by (cases t) auto
haftmann@35550
   445
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
   446
Andreas@47450
   447
context ord begin
Andreas@47450
   448
Andreas@47450
   449
lemma paint_rbt_sorted[simp]: "rbt_sorted t \<Longrightarrow> rbt_sorted (paint c t)" by (cases t) auto
Andreas@47450
   450
lemma paint_rbt_lookup[simp]: "rbt_lookup (paint c t) = rbt_lookup t" by (rule ext) (cases t, auto)
Andreas@47450
   451
lemma paint_rbt_greater[simp]: "(v \<guillemotleft>| paint c t) = (v \<guillemotleft>| t)" by (cases t) auto
Andreas@47450
   452
lemma paint_rbt_less[simp]: "(paint c t |\<guillemotleft> v) = (t |\<guillemotleft> v)" by (cases t) auto
krauss@26192
   453
krauss@26192
   454
fun
Andreas@47450
   455
  rbt_ins :: "('a \<Rightarrow> 'b \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"
krauss@26192
   456
where
Andreas@47450
   457
  "rbt_ins f k v Empty = Branch R Empty k v Empty" |
Andreas@47450
   458
  "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
   459
                                       else if k > x then balance l x y (rbt_ins f k v r)
Andreas@47450
   460
                                       else Branch B l x (f k y v) r)" |
Andreas@47450
   461
  "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
   462
                                       else if k > x then Branch R l x y (rbt_ins f k v r)
Andreas@47450
   463
                                       else Branch R l x (f k y v) r)"
krauss@26192
   464
krauss@26192
   465
lemma ins_inv1_inv2: 
krauss@26192
   466
  assumes "inv1 t" "inv2 t"
Andreas@47450
   467
  shows "inv2 (rbt_ins f k x t)" "bheight (rbt_ins f k x t) = bheight t" 
Andreas@47450
   468
  "color_of t = B \<Longrightarrow> inv1 (rbt_ins f k x t)" "inv1l (rbt_ins f k x t)"
krauss@26192
   469
  using assms
Andreas@47450
   470
  by (induct f k x t rule: rbt_ins.induct) (auto simp: balance_inv1 balance_inv2 balance_bheight)
Andreas@47450
   471
Andreas@47450
   472
end
Andreas@47450
   473
Andreas@47450
   474
context linorder begin
krauss@26192
   475
Andreas@47450
   476
lemma ins_rbt_greater[simp]: "(v \<guillemotleft>| rbt_ins f (k :: 'a) x t) = (v \<guillemotleft>| t \<and> k > v)"
Andreas@47450
   477
  by (induct f k x t rule: rbt_ins.induct) auto
Andreas@47450
   478
lemma ins_rbt_less[simp]: "(rbt_ins f k x t |\<guillemotleft> v) = (t |\<guillemotleft> v \<and> k < v)"
Andreas@47450
   479
  by (induct f k x t rule: rbt_ins.induct) auto
Andreas@47450
   480
lemma ins_rbt_sorted[simp]: "rbt_sorted t \<Longrightarrow> rbt_sorted (rbt_ins f k x t)"
Andreas@47450
   481
  by (induct f k x t rule: rbt_ins.induct) (auto simp: balance_rbt_sorted)
krauss@26192
   482
Andreas@47450
   483
lemma keys_ins: "set (keys (rbt_ins f k v t)) = { k } \<union> set (keys t)"
Andreas@47450
   484
  by (induct f k v t rule: rbt_ins.induct) auto
krauss@26192
   485
Andreas@47450
   486
lemma rbt_lookup_ins: 
Andreas@47450
   487
  fixes k :: "'a"
Andreas@47450
   488
  assumes "rbt_sorted t"
Andreas@47450
   489
  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
   490
                                                                | Some w \<Rightarrow> f k w v)) x"
Andreas@47450
   491
using assms by (induct f k v t rule: rbt_ins.induct) auto
Andreas@47450
   492
Andreas@47450
   493
end
Andreas@47450
   494
Andreas@47450
   495
context ord begin
Andreas@47450
   496
Andreas@47450
   497
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
   498
where "rbt_insert_with_key f k v t = paint B (rbt_ins f k v t)"
Andreas@47450
   499
Andreas@47450
   500
definition rbt_insertw_def: "rbt_insert_with f = rbt_insert_with_key (\<lambda>_. f)"
krauss@26192
   501
Andreas@47450
   502
definition rbt_insert :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) rbt \<Rightarrow> ('a, 'b) rbt" where
Andreas@47450
   503
  "rbt_insert = rbt_insert_with_key (\<lambda>_ _ nv. nv)"
Andreas@47450
   504
Andreas@47450
   505
end
Andreas@47450
   506
Andreas@47450
   507
context linorder begin
krauss@26192
   508
Andreas@47450
   509
lemma rbt_insertwk_rbt_sorted: "rbt_sorted t \<Longrightarrow> rbt_sorted (rbt_insert_with_key f (k :: 'a) x t)"
Andreas@47450
   510
  by (auto simp: rbt_insert_with_key_def)
krauss@26192
   511
Andreas@47450
   512
theorem rbt_insertwk_is_rbt: 
haftmann@35534
   513
  assumes inv: "is_rbt t" 
Andreas@47450
   514
  shows "is_rbt (rbt_insert_with_key f k x t)"
krauss@26192
   515
using assms
Andreas@47450
   516
unfolding rbt_insert_with_key_def is_rbt_def
krauss@26192
   517
by (auto simp: ins_inv1_inv2)
krauss@26192
   518
Andreas@47450
   519
lemma rbt_lookup_rbt_insertwk: 
Andreas@47450
   520
  assumes "rbt_sorted t"
Andreas@47450
   521
  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
   522
                                                       | Some w \<Rightarrow> f k w v)) x"
Andreas@47450
   523
unfolding rbt_insert_with_key_def using assms
Andreas@47450
   524
by (simp add:rbt_lookup_ins)
krauss@26192
   525
Andreas@47450
   526
lemma rbt_insertw_rbt_sorted: "rbt_sorted t \<Longrightarrow> rbt_sorted (rbt_insert_with f k v t)" 
Andreas@47450
   527
  by (simp add: rbt_insertwk_rbt_sorted rbt_insertw_def)
Andreas@47450
   528
theorem rbt_insertw_is_rbt: "is_rbt t \<Longrightarrow> is_rbt (rbt_insert_with f k v t)"
Andreas@47450
   529
  by (simp add: rbt_insertwk_is_rbt rbt_insertw_def)
krauss@26192
   530
Andreas@47450
   531
lemma rbt_lookup_rbt_insertw:
haftmann@35534
   532
  assumes "is_rbt t"
Andreas@47450
   533
  shows "rbt_lookup (rbt_insert_with f k v t) = (rbt_lookup t)(k \<mapsto> (if k:dom (rbt_lookup t) then f (the (rbt_lookup t k)) v else v))"
krauss@26192
   534
using assms
Andreas@47450
   535
unfolding rbt_insertw_def
Andreas@47450
   536
by (rule_tac ext) (cases "rbt_lookup t k", auto simp:rbt_lookup_rbt_insertwk dom_def)
krauss@26192
   537
Andreas@47450
   538
lemma rbt_insert_rbt_sorted: "rbt_sorted t \<Longrightarrow> rbt_sorted (rbt_insert k v t)"
Andreas@47450
   539
  by (simp add: rbt_insertwk_rbt_sorted rbt_insert_def)
Andreas@47450
   540
theorem rbt_insert_is_rbt [simp]: "is_rbt t \<Longrightarrow> is_rbt (rbt_insert k v t)"
Andreas@47450
   541
  by (simp add: rbt_insertwk_is_rbt rbt_insert_def)
krauss@26192
   542
Andreas@47450
   543
lemma rbt_lookup_rbt_insert: 
haftmann@35534
   544
  assumes "is_rbt t"
Andreas@47450
   545
  shows "rbt_lookup (rbt_insert k v t) = (rbt_lookup t)(k\<mapsto>v)"
Andreas@47450
   546
unfolding rbt_insert_def
krauss@26192
   547
using assms
Andreas@47450
   548
by (rule_tac ext) (simp add: rbt_lookup_rbt_insertwk split:option.split)
krauss@26192
   549
Andreas@47450
   550
end
krauss@26192
   551
krauss@26192
   552
subsection {* Deletion *}
krauss@26192
   553
haftmann@35534
   554
lemma bheight_paintR'[simp]: "color_of t = B \<Longrightarrow> bheight (paint R t) = bheight t - 1"
krauss@26192
   555
by (cases t rule: rbt_cases) auto
krauss@26192
   556
krauss@26192
   557
fun
haftmann@35550
   558
  balance_left :: "('a,'b) rbt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"
krauss@26192
   559
where
haftmann@35550
   560
  "balance_left (Branch R a k x b) s y c = Branch R (Branch B a k x b) s y c" |
haftmann@35550
   561
  "balance_left bl k x (Branch B a s y b) = balance bl k x (Branch R a s y b)" |
haftmann@35550
   562
  "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
   563
  "balance_left t k x s = Empty"
krauss@26192
   564
haftmann@35550
   565
lemma balance_left_inv2_with_inv1:
haftmann@35534
   566
  assumes "inv2 lt" "inv2 rt" "bheight lt + 1 = bheight rt" "inv1 rt"
haftmann@35550
   567
  shows "bheight (balance_left lt k v rt) = bheight lt + 1"
haftmann@35550
   568
  and   "inv2 (balance_left lt k v rt)"
krauss@26192
   569
using assms 
haftmann@35550
   570
by (induct lt k v rt rule: balance_left.induct) (auto simp: balance_inv2 balance_bheight)
krauss@26192
   571
haftmann@35550
   572
lemma balance_left_inv2_app: 
haftmann@35534
   573
  assumes "inv2 lt" "inv2 rt" "bheight lt + 1 = bheight rt" "color_of rt = B"
haftmann@35550
   574
  shows "inv2 (balance_left lt k v rt)" 
haftmann@35550
   575
        "bheight (balance_left lt k v rt) = bheight rt"
krauss@26192
   576
using assms 
haftmann@35550
   577
by (induct lt k v rt rule: balance_left.induct) (auto simp add: balance_inv2 balance_bheight)+ 
krauss@26192
   578
haftmann@35550
   579
lemma balance_left_inv1: "\<lbrakk>inv1l a; inv1 b; color_of b = B\<rbrakk> \<Longrightarrow> inv1 (balance_left a k x b)"
haftmann@35550
   580
  by (induct a k x b rule: balance_left.induct) (simp add: balance_inv1)+
krauss@26192
   581
haftmann@35550
   582
lemma balance_left_inv1l: "\<lbrakk> inv1l lt; inv1 rt \<rbrakk> \<Longrightarrow> inv1l (balance_left lt k x rt)"
haftmann@35550
   583
by (induct lt k x rt rule: balance_left.induct) (auto simp: balance_inv1)
krauss@26192
   584
Andreas@47450
   585
lemma (in linorder) balance_left_rbt_sorted: 
Andreas@47450
   586
  "\<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
   587
apply (induct l k v r rule: balance_left.induct)
Andreas@47450
   588
apply (auto simp: balance_rbt_sorted)
Andreas@47450
   589
apply (unfold rbt_greater_prop rbt_less_prop)
krauss@26192
   590
by force+
krauss@26192
   591
Andreas@47450
   592
context order begin
Andreas@47450
   593
Andreas@47450
   594
lemma balance_left_rbt_greater: 
Andreas@47450
   595
  fixes k :: "'a"
krauss@26192
   596
  assumes "k \<guillemotleft>| a" "k \<guillemotleft>| b" "k < x" 
haftmann@35550
   597
  shows "k \<guillemotleft>| balance_left a x t b"
krauss@26192
   598
using assms 
haftmann@35550
   599
by (induct a x t b rule: balance_left.induct) auto
krauss@26192
   600
Andreas@47450
   601
lemma balance_left_rbt_less: 
Andreas@47450
   602
  fixes k :: "'a"
krauss@26192
   603
  assumes "a |\<guillemotleft> k" "b |\<guillemotleft> k" "x < k" 
haftmann@35550
   604
  shows "balance_left a x t b |\<guillemotleft> k"
krauss@26192
   605
using assms
haftmann@35550
   606
by (induct a x t b rule: balance_left.induct) auto
krauss@26192
   607
Andreas@47450
   608
end
Andreas@47450
   609
haftmann@35550
   610
lemma balance_left_in_tree: 
haftmann@35534
   611
  assumes "inv1l l" "inv1 r" "bheight l + 1 = bheight r"
haftmann@35550
   612
  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
   613
using assms 
haftmann@35550
   614
by (induct l k v r rule: balance_left.induct) (auto simp: balance_in_tree)
krauss@26192
   615
krauss@26192
   616
fun
haftmann@35550
   617
  balance_right :: "('a,'b) rbt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"
krauss@26192
   618
where
haftmann@35550
   619
  "balance_right a k x (Branch R b s y c) = Branch R a k x (Branch B b s y c)" |
haftmann@35550
   620
  "balance_right (Branch B a k x b) s y bl = balance (Branch R a k x b) s y bl" |
haftmann@35550
   621
  "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
   622
  "balance_right t k x s = Empty"
krauss@26192
   623
haftmann@35550
   624
lemma balance_right_inv2_with_inv1:
haftmann@35534
   625
  assumes "inv2 lt" "inv2 rt" "bheight lt = bheight rt + 1" "inv1 lt"
haftmann@35550
   626
  shows "inv2 (balance_right lt k v rt) \<and> bheight (balance_right lt k v rt) = bheight lt"
krauss@26192
   627
using assms
haftmann@35550
   628
by (induct lt k v rt rule: balance_right.induct) (auto simp: balance_inv2 balance_bheight)
krauss@26192
   629
haftmann@35550
   630
lemma balance_right_inv1: "\<lbrakk>inv1 a; inv1l b; color_of a = B\<rbrakk> \<Longrightarrow> inv1 (balance_right a k x b)"
haftmann@35550
   631
by (induct a k x b rule: balance_right.induct) (simp add: balance_inv1)+
krauss@26192
   632
haftmann@35550
   633
lemma balance_right_inv1l: "\<lbrakk> inv1 lt; inv1l rt \<rbrakk> \<Longrightarrow>inv1l (balance_right lt k x rt)"
haftmann@35550
   634
by (induct lt k x rt rule: balance_right.induct) (auto simp: balance_inv1)
krauss@26192
   635
Andreas@47450
   636
lemma (in linorder) balance_right_rbt_sorted:
Andreas@47450
   637
  "\<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
   638
apply (induct l k v r rule: balance_right.induct)
Andreas@47450
   639
apply (auto simp:balance_rbt_sorted)
Andreas@47450
   640
apply (unfold rbt_less_prop rbt_greater_prop)
krauss@26192
   641
by force+
krauss@26192
   642
Andreas@47450
   643
context order begin
Andreas@47450
   644
Andreas@47450
   645
lemma balance_right_rbt_greater: 
Andreas@47450
   646
  fixes k :: "'a"
krauss@26192
   647
  assumes "k \<guillemotleft>| a" "k \<guillemotleft>| b" "k < x" 
haftmann@35550
   648
  shows "k \<guillemotleft>| balance_right a x t b"
haftmann@35550
   649
using assms by (induct a x t b rule: balance_right.induct) auto
krauss@26192
   650
Andreas@47450
   651
lemma balance_right_rbt_less: 
Andreas@47450
   652
  fixes k :: "'a"
krauss@26192
   653
  assumes "a |\<guillemotleft> k" "b |\<guillemotleft> k" "x < k" 
haftmann@35550
   654
  shows "balance_right a x t b |\<guillemotleft> k"
haftmann@35550
   655
using assms by (induct a x t b rule: balance_right.induct) auto
krauss@26192
   656
Andreas@47450
   657
end
Andreas@47450
   658
haftmann@35550
   659
lemma balance_right_in_tree:
haftmann@35534
   660
  assumes "inv1 l" "inv1l r" "bheight l = bheight r + 1" "inv2 l" "inv2 r"
haftmann@35550
   661
  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
   662
using assms by (induct l k v r rule: balance_right.induct) (auto simp: balance_in_tree)
krauss@26192
   663
krauss@26192
   664
fun
haftmann@35550
   665
  combine :: "('a,'b) rbt \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"
krauss@26192
   666
where
haftmann@35550
   667
  "combine Empty x = x" 
haftmann@35550
   668
| "combine x Empty = x" 
haftmann@35550
   669
| "combine (Branch R a k x b) (Branch R c s y d) = (case (combine b c) of
Andreas@47450
   670
                                    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
   671
                                    bc \<Rightarrow> Branch R a k x (Branch R bc s y d))" 
haftmann@35550
   672
| "combine (Branch B a k x b) (Branch B c s y d) = (case (combine b c) of
Andreas@47450
   673
                                    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
   674
                                    bc \<Rightarrow> balance_left a k x (Branch B bc s y d))" 
haftmann@35550
   675
| "combine a (Branch R b k x c) = Branch R (combine a b) k x c" 
haftmann@35550
   676
| "combine (Branch R a k x b) c = Branch R a k x (combine b c)" 
krauss@26192
   677
haftmann@35550
   678
lemma combine_inv2:
haftmann@35534
   679
  assumes "inv2 lt" "inv2 rt" "bheight lt = bheight rt"
haftmann@35550
   680
  shows "bheight (combine lt rt) = bheight lt" "inv2 (combine lt rt)"
krauss@26192
   681
using assms 
haftmann@35550
   682
by (induct lt rt rule: combine.induct) 
haftmann@35550
   683
   (auto simp: balance_left_inv2_app split: rbt.splits color.splits)
krauss@26192
   684
haftmann@35550
   685
lemma combine_inv1: 
krauss@26192
   686
  assumes "inv1 lt" "inv1 rt"
haftmann@35550
   687
  shows "color_of lt = B \<Longrightarrow> color_of rt = B \<Longrightarrow> inv1 (combine lt rt)"
haftmann@35550
   688
         "inv1l (combine lt rt)"
krauss@26192
   689
using assms 
haftmann@35550
   690
by (induct lt rt rule: combine.induct)
haftmann@35550
   691
   (auto simp: balance_left_inv1 split: rbt.splits color.splits)
krauss@26192
   692
Andreas@47450
   693
context linorder begin
Andreas@47450
   694
Andreas@47450
   695
lemma combine_rbt_greater[simp]: 
Andreas@47450
   696
  fixes k :: "'a"
krauss@26192
   697
  assumes "k \<guillemotleft>| l" "k \<guillemotleft>| r" 
haftmann@35550
   698
  shows "k \<guillemotleft>| combine l r"
krauss@26192
   699
using assms 
haftmann@35550
   700
by (induct l r rule: combine.induct)
Andreas@47450
   701
   (auto simp: balance_left_rbt_greater split:rbt.splits color.splits)
krauss@26192
   702
Andreas@47450
   703
lemma combine_rbt_less[simp]: 
Andreas@47450
   704
  fixes k :: "'a"
krauss@26192
   705
  assumes "l |\<guillemotleft> k" "r |\<guillemotleft> k" 
haftmann@35550
   706
  shows "combine l r |\<guillemotleft> k"
krauss@26192
   707
using assms 
haftmann@35550
   708
by (induct l r rule: combine.induct)
Andreas@47450
   709
   (auto simp: balance_left_rbt_less split:rbt.splits color.splits)
krauss@26192
   710
Andreas@47450
   711
lemma combine_rbt_sorted: 
Andreas@47450
   712
  fixes k :: "'a"
Andreas@47450
   713
  assumes "rbt_sorted l" "rbt_sorted r" "l |\<guillemotleft> k" "k \<guillemotleft>| r"
Andreas@47450
   714
  shows "rbt_sorted (combine l r)"
haftmann@35550
   715
using assms proof (induct l r rule: combine.induct)
krauss@26192
   716
  case (3 a x v b c y w d)
krauss@26192
   717
  hence ineqs: "a |\<guillemotleft> x" "x \<guillemotleft>| b" "b |\<guillemotleft> k" "k \<guillemotleft>| c" "c |\<guillemotleft> y" "y \<guillemotleft>| d"
krauss@26192
   718
    by auto
krauss@26192
   719
  with 3
krauss@26192
   720
  show ?case
haftmann@35550
   721
    by (cases "combine b c" rule: rbt_cases)
Andreas@47450
   722
      (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
   723
next
krauss@26192
   724
  case (4 a x v b c y w d)
Andreas@47450
   725
  hence "x < k \<and> rbt_greater k c" by simp
Andreas@47450
   726
  hence "rbt_greater x c" by (blast dest: rbt_greater_trans)
Andreas@47450
   727
  with 4 have 2: "rbt_greater x (combine b c)" by (simp add: combine_rbt_greater)
Andreas@47450
   728
  from 4 have "k < y \<and> rbt_less k b" by simp
Andreas@47450
   729
  hence "rbt_less y b" by (blast dest: rbt_less_trans)
Andreas@47450
   730
  with 4 have 3: "rbt_less y (combine b c)" by (simp add: combine_rbt_less)
krauss@26192
   731
  show ?case
haftmann@35550
   732
  proof (cases "combine b c" rule: rbt_cases)
krauss@26192
   733
    case Empty
Andreas@47450
   734
    from 4 have "x < y \<and> rbt_greater y d" by auto
Andreas@47450
   735
    hence "rbt_greater x d" by (blast dest: rbt_greater_trans)
Andreas@47450
   736
    with 4 Empty have "rbt_sorted a" and "rbt_sorted (Branch B Empty y w d)"
Andreas@47450
   737
      and "rbt_less x a" and "rbt_greater x (Branch B Empty y w d)" by auto
Andreas@47450
   738
    with Empty show ?thesis by (simp add: balance_left_rbt_sorted)
krauss@26192
   739
  next
krauss@26192
   740
    case (Red lta va ka rta)
Andreas@47450
   741
    with 2 4 have "x < va \<and> rbt_less x a" by simp
Andreas@47450
   742
    hence 5: "rbt_less va a" by (blast dest: rbt_less_trans)
Andreas@47450
   743
    from Red 3 4 have "va < y \<and> rbt_greater y d" by simp
Andreas@47450
   744
    hence "rbt_greater va d" by (blast dest: rbt_greater_trans)
krauss@26192
   745
    with Red 2 3 4 5 show ?thesis by simp
krauss@26192
   746
  next
krauss@26192
   747
    case (Black lta va ka rta)
Andreas@47450
   748
    from 4 have "x < y \<and> rbt_greater y d" by auto
Andreas@47450
   749
    hence "rbt_greater x d" by (blast dest: rbt_greater_trans)
Andreas@47450
   750
    with Black 2 3 4 have "rbt_sorted a" and "rbt_sorted (Branch B (combine b c) y w d)" 
Andreas@47450
   751
      and "rbt_less x a" and "rbt_greater x (Branch B (combine b c) y w d)" by auto
Andreas@47450
   752
    with Black show ?thesis by (simp add: balance_left_rbt_sorted)
krauss@26192
   753
  qed
krauss@26192
   754
next
krauss@26192
   755
  case (5 va vb vd vc b x w c)
Andreas@47450
   756
  hence "k < x \<and> rbt_less k (Branch B va vb vd vc)" by simp
Andreas@47450
   757
  hence "rbt_less x (Branch B va vb vd vc)" by (blast dest: rbt_less_trans)
Andreas@47450
   758
  with 5 show ?case by (simp add: combine_rbt_less)
krauss@26192
   759
next
krauss@26192
   760
  case (6 a x v b va vb vd vc)
Andreas@47450
   761
  hence "x < k \<and> rbt_greater k (Branch B va vb vd vc)" by simp
Andreas@47450
   762
  hence "rbt_greater x (Branch B va vb vd vc)" by (blast dest: rbt_greater_trans)
Andreas@47450
   763
  with 6 show ?case by (simp add: combine_rbt_greater)
krauss@26192
   764
qed simp+
krauss@26192
   765
Andreas@47450
   766
end
Andreas@47450
   767
haftmann@35550
   768
lemma combine_in_tree: 
haftmann@35534
   769
  assumes "inv2 l" "inv2 r" "bheight l = bheight r" "inv1 l" "inv1 r"
haftmann@35550
   770
  shows "entry_in_tree k v (combine l r) = (entry_in_tree k v l \<or> entry_in_tree k v r)"
krauss@26192
   771
using assms 
haftmann@35550
   772
proof (induct l r rule: combine.induct)
krauss@26192
   773
  case (4 _ _ _ b c)
haftmann@35550
   774
  hence a: "bheight (combine b c) = bheight b" by (simp add: combine_inv2)
haftmann@35550
   775
  from 4 have b: "inv1l (combine b c)" by (simp add: combine_inv1)
krauss@26192
   776
krauss@26192
   777
  show ?case
haftmann@35550
   778
  proof (cases "combine b c" rule: rbt_cases)
krauss@26192
   779
    case Empty
haftmann@35550
   780
    with 4 a show ?thesis by (auto simp: balance_left_in_tree)
krauss@26192
   781
  next
krauss@26192
   782
    case (Red lta ka va rta)
krauss@26192
   783
    with 4 show ?thesis by auto
krauss@26192
   784
  next
krauss@26192
   785
    case (Black lta ka va rta)
haftmann@35550
   786
    with a b 4  show ?thesis by (auto simp: balance_left_in_tree)
krauss@26192
   787
  qed 
krauss@26192
   788
qed (auto split: rbt.splits color.splits)
krauss@26192
   789
Andreas@47450
   790
context ord begin
Andreas@47450
   791
krauss@26192
   792
fun
Andreas@47450
   793
  rbt_del_from_left :: "'a \<Rightarrow> ('a,'b) rbt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt" and
Andreas@47450
   794
  rbt_del_from_right :: "'a \<Rightarrow> ('a,'b) rbt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt" and
Andreas@47450
   795
  rbt_del :: "'a\<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"
krauss@26192
   796
where
Andreas@47450
   797
  "rbt_del x Empty = Empty" |
Andreas@47450
   798
  "rbt_del x (Branch c a y s b) = 
Andreas@47450
   799
   (if x < y then rbt_del_from_left x a y s b 
Andreas@47450
   800
    else (if x > y then rbt_del_from_right x a y s b else combine a b))" |
Andreas@47450
   801
  "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
   802
  "rbt_del_from_left x a y s b = Branch R (rbt_del x a) y s b" |
Andreas@47450
   803
  "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
   804
  "rbt_del_from_right x a y s b = Branch R a y s (rbt_del x b)"
Andreas@47450
   805
Andreas@47450
   806
end
Andreas@47450
   807
Andreas@47450
   808
context linorder begin
krauss@26192
   809
krauss@26192
   810
lemma 
krauss@26192
   811
  assumes "inv2 lt" "inv1 lt"
krauss@26192
   812
  shows
haftmann@35534
   813
  "\<lbrakk>inv2 rt; bheight lt = bheight rt; inv1 rt\<rbrakk> \<Longrightarrow>
Andreas@47450
   814
   inv2 (rbt_del_from_left x lt k v rt) \<and> 
Andreas@47450
   815
   bheight (rbt_del_from_left x lt k v rt) = bheight lt \<and> 
Andreas@47450
   816
   (color_of lt = B \<and> color_of rt = B \<and> inv1 (rbt_del_from_left x lt k v rt) \<or> 
Andreas@47450
   817
    (color_of lt \<noteq> B \<or> color_of rt \<noteq> B) \<and> inv1l (rbt_del_from_left x lt k v rt))"
haftmann@35534
   818
  and "\<lbrakk>inv2 rt; bheight lt = bheight rt; inv1 rt\<rbrakk> \<Longrightarrow>
Andreas@47450
   819
  inv2 (rbt_del_from_right x lt k v rt) \<and> 
Andreas@47450
   820
  bheight (rbt_del_from_right x lt k v rt) = bheight lt \<and> 
Andreas@47450
   821
  (color_of lt = B \<and> color_of rt = B \<and> inv1 (rbt_del_from_right x lt k v rt) \<or> 
Andreas@47450
   822
   (color_of lt \<noteq> B \<or> color_of rt \<noteq> B) \<and> inv1l (rbt_del_from_right x lt k v rt))"
Andreas@47450
   823
  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
   824
  \<or> color_of lt = B \<and> bheight (rbt_del x lt) = bheight lt - 1 \<and> inv1l (rbt_del x lt))"
krauss@26192
   825
using assms
Andreas@47450
   826
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
   827
case (2 y c _ y')
krauss@26192
   828
  have "y = y' \<or> y < y' \<or> y > y'" by auto
krauss@26192
   829
  thus ?case proof (elim disjE)
krauss@26192
   830
    assume "y = y'"
haftmann@35550
   831
    with 2 show ?thesis by (cases c) (simp add: combine_inv2 combine_inv1)+
krauss@26192
   832
  next
krauss@26192
   833
    assume "y < y'"
krauss@26192
   834
    with 2 show ?thesis by (cases c) auto
krauss@26192
   835
  next
krauss@26192
   836
    assume "y' < y"
krauss@26192
   837
    with 2 show ?thesis by (cases c) auto
krauss@26192
   838
  qed
krauss@26192
   839
next
krauss@26192
   840
  case (3 y lt z v rta y' ss bb) 
haftmann@35550
   841
  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
   842
next
krauss@26192
   843
  case (5 y a y' ss lt z v rta)
haftmann@35550
   844
  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
   845
next
haftmann@35534
   846
  case ("6_1" y a y' ss) thus ?case by (cases "color_of a = B \<and> color_of Empty = B") simp+
krauss@26192
   847
qed auto
krauss@26192
   848
krauss@26192
   849
lemma 
Andreas@47450
   850
  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
   851
  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
   852
  and rbt_del_rbt_less: "lt |\<guillemotleft> v \<Longrightarrow> rbt_del x lt |\<guillemotleft> v"
Andreas@47450
   853
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
   854
   (auto simp: balance_left_rbt_less balance_right_rbt_less)
krauss@26192
   855
Andreas@47450
   856
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
   857
  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
   858
  and rbt_del_rbt_greater: "v \<guillemotleft>| lt \<Longrightarrow> v \<guillemotleft>| rbt_del x lt"
Andreas@47450
   859
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
   860
   (auto simp: balance_left_rbt_greater balance_right_rbt_greater)
krauss@26192
   861
Andreas@47450
   862
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
   863
  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
   864
  and rbt_del_rbt_sorted: "rbt_sorted lt \<Longrightarrow> rbt_sorted (rbt_del x lt)"
Andreas@47450
   865
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
   866
  case (3 x lta zz v rta yy ss bb)
Andreas@47450
   867
  from 3 have "Branch B lta zz v rta |\<guillemotleft> yy" by simp
Andreas@47450
   868
  hence "rbt_del x (Branch B lta zz v rta) |\<guillemotleft> yy" by (rule rbt_del_rbt_less)
Andreas@47450
   869
  with 3 show ?case by (simp add: balance_left_rbt_sorted)
krauss@26192
   870
next
krauss@26192
   871
  case ("4_2" x vaa vbb vdd vc yy ss bb)
Andreas@47450
   872
  hence "Branch R vaa vbb vdd vc |\<guillemotleft> yy" by simp
Andreas@47450
   873
  hence "rbt_del x (Branch R vaa vbb vdd vc) |\<guillemotleft> yy" by (rule rbt_del_rbt_less)
krauss@26192
   874
  with "4_2" show ?case by simp
krauss@26192
   875
next
krauss@26192
   876
  case (5 x aa yy ss lta zz v rta) 
Andreas@47450
   877
  hence "yy \<guillemotleft>| Branch B lta zz v rta" by simp
Andreas@47450
   878
  hence "yy \<guillemotleft>| rbt_del x (Branch B lta zz v rta)" by (rule rbt_del_rbt_greater)
Andreas@47450
   879
  with 5 show ?case by (simp add: balance_right_rbt_sorted)
krauss@26192
   880
next
krauss@26192
   881
  case ("6_2" x aa yy ss vaa vbb vdd vc)
Andreas@47450
   882
  hence "yy \<guillemotleft>| Branch R vaa vbb vdd vc" by simp
Andreas@47450
   883
  hence "yy \<guillemotleft>| rbt_del x (Branch R vaa vbb vdd vc)" by (rule rbt_del_rbt_greater)
krauss@26192
   884
  with "6_2" show ?case by simp
Andreas@47450
   885
qed (auto simp: combine_rbt_sorted)
krauss@26192
   886
Andreas@47450
   887
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
   888
  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
   889
  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
   890
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
   891
  case (2 xx c aa yy ss bb)
krauss@26192
   892
  have "xx = yy \<or> xx < yy \<or> xx > yy" by auto
krauss@26192
   893
  from this 2 show ?case proof (elim disjE)
krauss@26192
   894
    assume "xx = yy"
krauss@26192
   895
    with 2 show ?thesis proof (cases "xx = k")
krauss@26192
   896
      case True
Andreas@47450
   897
      from 2 `xx = yy` `xx = k` have "rbt_sorted (Branch c aa yy ss bb) \<and> k = yy" by simp
Andreas@47450
   898
      hence "\<not> entry_in_tree k v aa" "\<not> entry_in_tree k v bb" by (auto simp: rbt_less_nit rbt_greater_prop)
haftmann@35550
   899
      with `xx = yy` 2 `xx = k` show ?thesis by (simp add: combine_in_tree)
haftmann@35550
   900
    qed (simp add: combine_in_tree)
krauss@26192
   901
  qed simp+
krauss@26192
   902
next    
krauss@26192
   903
  case (3 xx lta zz vv rta yy ss bb)
haftmann@35534
   904
  def mt[simp]: mt == "Branch B lta zz vv rta"
krauss@26192
   905
  from 3 have "inv2 mt \<and> inv1 mt" by simp
Andreas@47450
   906
  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
   907
  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
   908
  thus ?case proof (cases "xx = k")
krauss@26192
   909
    case True
Andreas@47450
   910
    from 3 True have "yy \<guillemotleft>| bb \<and> yy > k" by simp
Andreas@47450
   911
    hence "k \<guillemotleft>| bb" by (blast dest: rbt_greater_trans)
Andreas@47450
   912
    with 3 4 True show ?thesis by (auto simp: rbt_greater_nit)
krauss@26192
   913
  qed auto
krauss@26192
   914
next
krauss@26192
   915
  case ("4_1" xx yy ss bb)
krauss@26192
   916
  show ?case proof (cases "xx = k")
krauss@26192
   917
    case True
Andreas@47450
   918
    with "4_1" have "yy \<guillemotleft>| bb \<and> k < yy" by simp
Andreas@47450
   919
    hence "k \<guillemotleft>| bb" by (blast dest: rbt_greater_trans)
krauss@26192
   920
    with "4_1" `xx = k` 
Andreas@47450
   921
   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
   922
    thus ?thesis by auto
krauss@26192
   923
  qed simp+
krauss@26192
   924
next
krauss@26192
   925
  case ("4_2" xx vaa vbb vdd vc yy ss bb)
krauss@26192
   926
  thus ?case proof (cases "xx = k")
krauss@26192
   927
    case True
Andreas@47450
   928
    with "4_2" have "k < yy \<and> yy \<guillemotleft>| bb" by simp
Andreas@47450
   929
    hence "k \<guillemotleft>| bb" by (blast dest: rbt_greater_trans)
Andreas@47450
   930
    with True "4_2" show ?thesis by (auto simp: rbt_greater_nit)
haftmann@35550
   931
  qed auto
krauss@26192
   932
next
krauss@26192
   933
  case (5 xx aa yy ss lta zz vv rta)
haftmann@35534
   934
  def mt[simp]: mt == "Branch B lta zz vv rta"
krauss@26192
   935
  from 5 have "inv2 mt \<and> inv1 mt" by simp
Andreas@47450
   936
  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
   937
  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
   938
  thus ?case proof (cases "xx = k")
krauss@26192
   939
    case True
Andreas@47450
   940
    from 5 True have "aa |\<guillemotleft> yy \<and> yy < k" by simp
Andreas@47450
   941
    hence "aa |\<guillemotleft> k" by (blast dest: rbt_less_trans)
Andreas@47450
   942
    with 3 5 True show ?thesis by (auto simp: rbt_less_nit)
krauss@26192
   943
  qed auto
krauss@26192
   944
next
krauss@26192
   945
  case ("6_1" xx aa yy ss)
krauss@26192
   946
  show ?case proof (cases "xx = k")
krauss@26192
   947
    case True
Andreas@47450
   948
    with "6_1" have "aa |\<guillemotleft> yy \<and> k > yy" by simp
Andreas@47450
   949
    hence "aa |\<guillemotleft> k" by (blast dest: rbt_less_trans)
Andreas@47450
   950
    with "6_1" `xx = k` show ?thesis by (auto simp: rbt_less_nit)
krauss@26192
   951
  qed simp
krauss@26192
   952
next
krauss@26192
   953
  case ("6_2" xx aa yy ss vaa vbb vdd vc)
krauss@26192
   954
  thus ?case proof (cases "xx = k")
krauss@26192
   955
    case True
Andreas@47450
   956
    with "6_2" have "k > yy \<and> aa |\<guillemotleft> yy" by simp
Andreas@47450
   957
    hence "aa |\<guillemotleft> k" by (blast dest: rbt_less_trans)
Andreas@47450
   958
    with True "6_2" show ?thesis by (auto simp: rbt_less_nit)
haftmann@35550
   959
  qed auto
krauss@26192
   960
qed simp
krauss@26192
   961
Andreas@47450
   962
definition (in ord) rbt_delete where
Andreas@47450
   963
  "rbt_delete k t = paint B (rbt_del k t)"
krauss@26192
   964
Andreas@47450
   965
theorem rbt_delete_is_rbt [simp]: assumes "is_rbt t" shows "is_rbt (rbt_delete k t)"
krauss@26192
   966
proof -
haftmann@35534
   967
  from assms have "inv2 t" and "inv1 t" unfolding is_rbt_def by auto 
Andreas@47450
   968
  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
   969
  hence "inv2 (rbt_del k t) \<and> inv1l (rbt_del k t)" by (cases "color_of t") auto
krauss@26192
   970
  with assms show ?thesis
Andreas@47450
   971
    unfolding is_rbt_def rbt_delete_def
Andreas@47450
   972
    by (auto intro: paint_rbt_sorted rbt_del_rbt_sorted)
krauss@26192
   973
qed
krauss@26192
   974
Andreas@47450
   975
lemma rbt_delete_in_tree: 
haftmann@35534
   976
  assumes "is_rbt t" 
Andreas@47450
   977
  shows "entry_in_tree k v (rbt_delete x t) = (x \<noteq> k \<and> entry_in_tree k v t)"
Andreas@47450
   978
  using assms unfolding is_rbt_def rbt_delete_def
Andreas@47450
   979
  by (auto simp: rbt_del_in_tree)
krauss@26192
   980
Andreas@47450
   981
lemma rbt_lookup_rbt_delete:
haftmann@35534
   982
  assumes is_rbt: "is_rbt t"
Andreas@47450
   983
  shows "rbt_lookup (rbt_delete k t) = (rbt_lookup t)|`(-{k})"
krauss@26192
   984
proof
krauss@26192
   985
  fix x
Andreas@47450
   986
  show "rbt_lookup (rbt_delete k t) x = (rbt_lookup t |` (-{k})) x" 
krauss@26192
   987
  proof (cases "x = k")
krauss@26192
   988
    assume "x = k" 
haftmann@35534
   989
    with is_rbt show ?thesis
Andreas@47450
   990
      by (cases "rbt_lookup (rbt_delete k t) k") (auto simp: rbt_lookup_in_tree rbt_delete_in_tree)
krauss@26192
   991
  next
krauss@26192
   992
    assume "x \<noteq> k"
krauss@26192
   993
    thus ?thesis
Andreas@47450
   994
      by auto (metis is_rbt rbt_delete_is_rbt rbt_delete_in_tree is_rbt_rbt_sorted rbt_lookup_from_in_tree)
krauss@26192
   995
  qed
krauss@26192
   996
qed
krauss@26192
   997
Andreas@47450
   998
end
haftmann@35550
   999
haftmann@35550
  1000
subsection {* Modifying existing entries *}
krauss@26192
  1001
Andreas@47450
  1002
context ord begin
Andreas@47450
  1003
krauss@26192
  1004
primrec
Andreas@47450
  1005
  rbt_map_entry :: "'a \<Rightarrow> ('b \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) rbt \<Rightarrow> ('a, 'b) rbt"
krauss@26192
  1006
where
Andreas@47450
  1007
  "rbt_map_entry k f Empty = Empty"
Andreas@47450
  1008
| "rbt_map_entry k f (Branch c lt x v rt) =
Andreas@47450
  1009
    (if k < x then Branch c (rbt_map_entry k f lt) x v rt
Andreas@47450
  1010
    else if k > x then (Branch c lt x v (rbt_map_entry k f rt))
haftmann@35602
  1011
    else Branch c lt x (f v) rt)"
krauss@26192
  1012
Andreas@47450
  1013
Andreas@47450
  1014
lemma rbt_map_entry_color_of: "color_of (rbt_map_entry k f t) = color_of t" by (induct t) simp+
Andreas@47450
  1015
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
  1016
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
  1017
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
  1018
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
  1019
lemma rbt_map_entry_rbt_sorted: "rbt_sorted (rbt_map_entry k f t) = rbt_sorted t"
Andreas@47450
  1020
  by (induct t) (simp_all add: rbt_map_entry_rbt_less rbt_map_entry_rbt_greater)
krauss@26192
  1021
Andreas@47450
  1022
theorem rbt_map_entry_is_rbt [simp]: "is_rbt (rbt_map_entry k f t) = is_rbt t" 
Andreas@47450
  1023
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
  1024
Andreas@47450
  1025
end
Andreas@47450
  1026
Andreas@47450
  1027
theorem (in linorder) rbt_lookup_rbt_map_entry:
blanchet@55466
  1028
  "rbt_lookup (rbt_map_entry k f t) = (rbt_lookup t)(k := map_option f (rbt_lookup t k))"
nipkow@39302
  1029
  by (induct t) (auto split: option.splits simp add: fun_eq_iff)
krauss@26192
  1030
haftmann@35550
  1031
subsection {* Mapping all entries *}
krauss@26192
  1032
krauss@26192
  1033
primrec
haftmann@35602
  1034
  map :: "('a \<Rightarrow> 'b \<Rightarrow> 'c) \<Rightarrow> ('a, 'b) rbt \<Rightarrow> ('a, 'c) rbt"
krauss@26192
  1035
where
haftmann@35550
  1036
  "map f Empty = Empty"
haftmann@35550
  1037
| "map f (Branch c lt k v rt) = Branch c (map f lt) k (f k v) (map f rt)"
krauss@32237
  1038
haftmann@35550
  1039
lemma map_entries [simp]: "entries (map f t) = List.map (\<lambda>(k, v). (k, f k v)) (entries t)"
haftmann@35550
  1040
  by (induct t) auto
haftmann@35550
  1041
lemma map_keys [simp]: "keys (map f t) = keys t" by (simp add: keys_def split_def)
haftmann@35550
  1042
lemma map_color_of: "color_of (map f t) = color_of t" by (induct t) simp+
haftmann@35550
  1043
lemma map_inv1: "inv1 (map f t) = inv1 t" by (induct t) (simp add: map_color_of)+
haftmann@35550
  1044
lemma map_inv2: "inv2 (map f t) = inv2 t" "bheight (map f t) = bheight t" by (induct t) simp+
Andreas@47450
  1045
Andreas@47450
  1046
context ord begin
Andreas@47450
  1047
Andreas@47450
  1048
lemma map_rbt_greater: "rbt_greater k (map f t) = rbt_greater k t" by (induct t) simp+
Andreas@47450
  1049
lemma map_rbt_less: "rbt_less k (map f t) = rbt_less k t" by (induct t) simp+
Andreas@47450
  1050
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
  1051
theorem map_is_rbt [simp]: "is_rbt (map f t) = is_rbt t" 
Andreas@47450
  1052
unfolding is_rbt_def by (simp add: map_inv1 map_inv2 map_rbt_sorted map_color_of)
krauss@32237
  1053
Andreas@47450
  1054
end
krauss@26192
  1055
blanchet@55466
  1056
theorem (in linorder) rbt_lookup_map: "rbt_lookup (map f t) x = map_option (f x) (rbt_lookup t x)"
Andreas@47450
  1057
  apply(induct t)
Andreas@47450
  1058
  apply auto
blanchet@58257
  1059
  apply(rename_tac a b c, subgoal_tac "x = a")
Andreas@47450
  1060
  apply auto
Andreas@47450
  1061
  done
Andreas@47450
  1062
 (* FIXME: simproc "antisym less" does not work for linorder context, only for linorder type class
Andreas@47450
  1063
    by (induct t) auto *)
haftmann@35550
  1064
Andreas@49770
  1065
hide_const (open) map
Andreas@49770
  1066
haftmann@35550
  1067
subsection {* Folding over entries *}
haftmann@35550
  1068
haftmann@35550
  1069
definition fold :: "('a \<Rightarrow> 'b \<Rightarrow> 'c \<Rightarrow> 'c) \<Rightarrow> ('a, 'b) rbt \<Rightarrow> 'c \<Rightarrow> 'c" where
blanchet@55414
  1070
  "fold f t = List.fold (case_prod f) (entries t)"
krauss@26192
  1071
Andreas@49770
  1072
lemma fold_simps [simp]:
haftmann@35550
  1073
  "fold f Empty = id"
haftmann@35550
  1074
  "fold f (Branch c lt k v rt) = fold f rt \<circ> f k v \<circ> fold f lt"
nipkow@39302
  1075
  by (simp_all add: fold_def fun_eq_iff)
haftmann@35534
  1076
Andreas@49770
  1077
lemma fold_code [code]:
Andreas@49810
  1078
  "fold f Empty x = x"
Andreas@49810
  1079
  "fold f (Branch c lt k v rt) x = fold f rt (f k v (fold f lt x))"
Andreas@49770
  1080
by(simp_all)
Andreas@49770
  1081
kuncar@48621
  1082
(* fold with continuation predicate *)
kuncar@48621
  1083
kuncar@48621
  1084
fun foldi :: "('c \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> 'b \<Rightarrow> 'c \<Rightarrow> 'c) \<Rightarrow> ('a :: linorder, 'b) rbt \<Rightarrow> 'c \<Rightarrow> 'c" 
kuncar@48621
  1085
  where
kuncar@48621
  1086
  "foldi c f Empty s = s" |
kuncar@48621
  1087
  "foldi c f (Branch col l k v r) s = (
kuncar@48621
  1088
    if (c s) then
kuncar@48621
  1089
      let s' = foldi c f l s in
kuncar@48621
  1090
        if (c s') then
kuncar@48621
  1091
          foldi c f r (f k v s')
kuncar@48621
  1092
        else s'
kuncar@48621
  1093
    else 
kuncar@48621
  1094
      s
kuncar@48621
  1095
  )"
haftmann@35606
  1096
haftmann@35606
  1097
subsection {* Bulkloading a tree *}
haftmann@35606
  1098
Andreas@47450
  1099
definition (in ord) rbt_bulkload :: "('a \<times> 'b) list \<Rightarrow> ('a, 'b) rbt" where
Andreas@47450
  1100
  "rbt_bulkload xs = foldr (\<lambda>(k, v). rbt_insert k v) xs Empty"
Andreas@47450
  1101
Andreas@47450
  1102
context linorder begin
haftmann@35606
  1103
Andreas@47450
  1104
lemma rbt_bulkload_is_rbt [simp, intro]:
Andreas@47450
  1105
  "is_rbt (rbt_bulkload xs)"
Andreas@47450
  1106
  unfolding rbt_bulkload_def by (induct xs) auto
haftmann@35606
  1107
Andreas@47450
  1108
lemma rbt_lookup_rbt_bulkload:
Andreas@47450
  1109
  "rbt_lookup (rbt_bulkload xs) = map_of xs"
haftmann@35606
  1110
proof -
haftmann@35606
  1111
  obtain ys where "ys = rev xs" by simp
haftmann@35606
  1112
  have "\<And>t. is_rbt t \<Longrightarrow>
blanchet@55414
  1113
    rbt_lookup (List.fold (case_prod rbt_insert) ys t) = rbt_lookup t ++ map_of (rev ys)"
blanchet@55414
  1114
      by (induct ys) (simp_all add: rbt_bulkload_def rbt_lookup_rbt_insert case_prod_beta)
haftmann@35606
  1115
  from this Empty_is_rbt have
blanchet@55414
  1116
    "rbt_lookup (List.fold (case_prod rbt_insert) (rev xs) Empty) = rbt_lookup Empty ++ map_of xs"
haftmann@35606
  1117
     by (simp add: `ys = rev xs`)
Andreas@47450
  1118
  then show ?thesis by (simp add: rbt_bulkload_def rbt_lookup_Empty foldr_conv_fold)
haftmann@35606
  1119
qed
haftmann@35606
  1120
Andreas@47450
  1121
end
Andreas@47450
  1122
Andreas@49770
  1123
Andreas@49770
  1124
Andreas@49770
  1125
subsection {* Building a RBT from a sorted list *}
Andreas@49770
  1126
Andreas@49770
  1127
text {* 
Andreas@49770
  1128
  These functions have been adapted from 
Andreas@49770
  1129
  Andrew W. Appel, Efficient Verified Red-Black Trees (September 2011) 
Andreas@49770
  1130
*}
Andreas@49770
  1131
Andreas@49770
  1132
fun rbtreeify_f :: "nat \<Rightarrow> ('a \<times> 'b) list \<Rightarrow> ('a, 'b) rbt \<times> ('a \<times> 'b) list"
Andreas@49770
  1133
  and rbtreeify_g :: "nat \<Rightarrow> ('a \<times> 'b) list \<Rightarrow> ('a, 'b) rbt \<times> ('a \<times> 'b) list"
Andreas@49770
  1134
where
Andreas@49770
  1135
  "rbtreeify_f n kvs =
Andreas@49770
  1136
   (if n = 0 then (Empty, kvs)
Andreas@49770
  1137
    else if n = 1 then
Andreas@49770
  1138
      case kvs of (k, v) # kvs' \<Rightarrow> (Branch R Empty k v Empty, kvs')
Andreas@49770
  1139
    else if (n mod 2 = 0) then
Andreas@49770
  1140
      case rbtreeify_f (n div 2) kvs of (t1, (k, v) # kvs') \<Rightarrow>
Andreas@49770
  1141
        apfst (Branch B t1 k v) (rbtreeify_g (n div 2) kvs')
Andreas@49770
  1142
    else case rbtreeify_f (n div 2) kvs of (t1, (k, v) # kvs') \<Rightarrow>
Andreas@49770
  1143
        apfst (Branch B t1 k v) (rbtreeify_f (n div 2) kvs'))"
Andreas@49770
  1144
Andreas@49770
  1145
| "rbtreeify_g n kvs =
Andreas@49770
  1146
   (if n = 0 \<or> n = 1 then (Empty, kvs)
Andreas@49770
  1147
    else if n mod 2 = 0 then
Andreas@49770
  1148
      case rbtreeify_g (n div 2) kvs of (t1, (k, v) # kvs') \<Rightarrow>
Andreas@49770
  1149
        apfst (Branch B t1 k v) (rbtreeify_g (n div 2) kvs')
Andreas@49770
  1150
    else case rbtreeify_f (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
Andreas@49770
  1153
definition rbtreeify :: "('a \<times> 'b) list \<Rightarrow> ('a, 'b) rbt"
Andreas@49770
  1154
where "rbtreeify kvs = fst (rbtreeify_g (Suc (length kvs)) kvs)"
Andreas@49770
  1155
Andreas@49770
  1156
declare rbtreeify_f.simps [simp del] rbtreeify_g.simps [simp del]
Andreas@49770
  1157
Andreas@49770
  1158
lemma rbtreeify_f_code [code]:
Andreas@49770
  1159
  "rbtreeify_f n kvs =
Andreas@49770
  1160
   (if n = 0 then (Empty, kvs)
Andreas@49770
  1161
    else if n = 1 then
Andreas@49770
  1162
      case kvs of (k, v) # kvs' \<Rightarrow> 
Andreas@49770
  1163
        (Branch R Empty k v Empty, kvs')
Andreas@49770
  1164
    else let (n', r) = divmod_nat n 2 in
Andreas@49770
  1165
      if r = 0 then
Andreas@49770
  1166
        case rbtreeify_f n' kvs of (t1, (k, v) # kvs') \<Rightarrow>
Andreas@49770
  1167
          apfst (Branch B t1 k v) (rbtreeify_g n' kvs')
Andreas@49770
  1168
      else case rbtreeify_f n' kvs of (t1, (k, v) # kvs') \<Rightarrow>
Andreas@49770
  1169
          apfst (Branch B t1 k v) (rbtreeify_f n' kvs'))"
blanchet@55412
  1170
by (subst rbtreeify_f.simps) (simp only: Let_def divmod_nat_div_mod prod.case)
Andreas@49770
  1171
Andreas@49770
  1172
lemma rbtreeify_g_code [code]:
Andreas@49770
  1173
  "rbtreeify_g n kvs =
Andreas@49770
  1174
   (if n = 0 \<or> n = 1 then (Empty, kvs)
Andreas@49770
  1175
    else let (n', r) = divmod_nat n 2 in
Andreas@49770
  1176
      if r = 0 then
Andreas@49770
  1177
        case rbtreeify_g n' kvs of (t1, (k, v) # kvs') \<Rightarrow>
Andreas@49770
  1178
          apfst (Branch B t1 k v) (rbtreeify_g n' kvs')
Andreas@49770
  1179
      else case rbtreeify_f n' kvs of (t1, (k, v) # kvs') \<Rightarrow>
Andreas@49770
  1180
          apfst (Branch B t1 k v) (rbtreeify_g n' kvs'))"
blanchet@55412
  1181
by(subst rbtreeify_g.simps)(simp only: Let_def divmod_nat_div_mod prod.case)
Andreas@49770
  1182
Andreas@49770
  1183
lemma Suc_double_half: "Suc (2 * n) div 2 = n"
Andreas@49770
  1184
by simp
Andreas@49770
  1185
Andreas@49770
  1186
lemma div2_plus_div2: "n div 2 + n div 2 = (n :: nat) - n mod 2"
Andreas@49770
  1187
by arith
Andreas@49770
  1188
Andreas@49770
  1189
lemma rbtreeify_f_rec_aux_lemma:
Andreas@49770
  1190
  "\<lbrakk>k - n div 2 = Suc k'; n \<le> k; n mod 2 = Suc 0\<rbrakk>
Andreas@49770
  1191
  \<Longrightarrow> k' - n div 2 = k - n"
Andreas@49770
  1192
apply(rule add_right_imp_eq[where a = "n - n div 2"])
Andreas@49770
  1193
apply(subst add_diff_assoc2, arith)
Andreas@49770
  1194
apply(simp add: div2_plus_div2)
Andreas@49770
  1195
done
Andreas@49770
  1196
Andreas@49770
  1197
lemma rbtreeify_f_simps:
Andreas@49770
  1198
  "rbtreeify_f 0 kvs = (RBT_Impl.Empty, kvs)"
Andreas@49770
  1199
  "rbtreeify_f (Suc 0) ((k, v) # kvs) = 
Andreas@49770
  1200
  (Branch R Empty k v Empty, kvs)"
Andreas@49770
  1201
  "0 < n \<Longrightarrow> rbtreeify_f (2 * n) kvs =
Andreas@49770
  1202
   (case rbtreeify_f n kvs of (t1, (k, v) # kvs') \<Rightarrow>
Andreas@49770
  1203
     apfst (Branch B t1 k v) (rbtreeify_g n kvs'))"
Andreas@49770
  1204
  "0 < n \<Longrightarrow> rbtreeify_f (Suc (2 * n)) kvs =
Andreas@49770
  1205
   (case rbtreeify_f n kvs of (t1, (k, v) # kvs') \<Rightarrow> 
Andreas@49770
  1206
     apfst (Branch B t1 k v) (rbtreeify_f n kvs'))"
Andreas@49770
  1207
by(subst (1) rbtreeify_f.simps, simp add: Suc_double_half)+
Andreas@49770
  1208
Andreas@49770
  1209
lemma rbtreeify_g_simps:
Andreas@49770
  1210
  "rbtreeify_g 0 kvs = (Empty, kvs)"
Andreas@49770
  1211
  "rbtreeify_g (Suc 0) kvs = (Empty, kvs)"
Andreas@49770
  1212
  "0 < n \<Longrightarrow> rbtreeify_g (2 * n) kvs =
Andreas@49770
  1213
   (case rbtreeify_g n kvs of (t1, (k, v) # kvs') \<Rightarrow> 
Andreas@49770
  1214
     apfst (Branch B t1 k v) (rbtreeify_g n kvs'))"
Andreas@49770
  1215
  "0 < n \<Longrightarrow> rbtreeify_g (Suc (2 * n)) kvs =
Andreas@49770
  1216
   (case rbtreeify_f n kvs of (t1, (k, v) # kvs') \<Rightarrow> 
Andreas@49770
  1217
     apfst (Branch B t1 k v) (rbtreeify_g n kvs'))"
Andreas@49770
  1218
by(subst (1) rbtreeify_g.simps, simp add: Suc_double_half)+
Andreas@49770
  1219
Andreas@49770
  1220
declare rbtreeify_f_simps[simp] rbtreeify_g_simps[simp]
Andreas@49770
  1221
Andreas@49770
  1222
lemma length_rbtreeify_f: "n \<le> length kvs
Andreas@49770
  1223
  \<Longrightarrow> length (snd (rbtreeify_f n kvs)) = length kvs - n"
Andreas@49770
  1224
  and length_rbtreeify_g:"\<lbrakk> 0 < n; n \<le> Suc (length kvs) \<rbrakk>
Andreas@49770
  1225
  \<Longrightarrow> length (snd (rbtreeify_g n kvs)) = Suc (length kvs) - n"
Andreas@49770
  1226
proof(induction n kvs and n kvs rule: rbtreeify_f_rbtreeify_g.induct)
Andreas@49770
  1227
  case (1 n kvs)
Andreas@49770
  1228
  show ?case
Andreas@49770
  1229
  proof(cases "n \<le> 1")
Andreas@49770
  1230
    case True thus ?thesis using "1.prems"
Andreas@49770
  1231
      by(cases n kvs rule: nat.exhaust[case_product list.exhaust]) auto
Andreas@49770
  1232
  next
Andreas@49770
  1233
    case False
Andreas@49770
  1234
    hence "n \<noteq> 0" "n \<noteq> 1" by simp_all
Andreas@49770
  1235
    note IH = "1.IH"[OF this]
Andreas@49770
  1236
    show ?thesis
Andreas@49770
  1237
    proof(cases "n mod 2 = 0")
Andreas@49770
  1238
      case True
Andreas@49770
  1239
      hence "length (snd (rbtreeify_f n kvs)) = 
Andreas@49770
  1240
        length (snd (rbtreeify_f (2 * (n div 2)) kvs))"
Andreas@49770
  1241
        by(metis minus_nat.diff_0 mult_div_cancel)
Andreas@49770
  1242
      also from "1.prems" False obtain k v kvs' 
Andreas@49770
  1243
        where kvs: "kvs = (k, v) # kvs'" by(cases kvs) auto
Andreas@49770
  1244
      also have "0 < n div 2" using False by(simp) 
Andreas@49770
  1245
      note rbtreeify_f_simps(3)[OF this]
Andreas@49770
  1246
      also note kvs[symmetric] 
Andreas@49770
  1247
      also let ?rest1 = "snd (rbtreeify_f (n div 2) kvs)"
Andreas@49770
  1248
      from "1.prems" have "n div 2 \<le> length kvs" by simp
Andreas@49770
  1249
      with True have len: "length ?rest1 = length kvs - n div 2" by(rule IH)
Andreas@49770
  1250
      with "1.prems" False obtain t1 k' v' kvs''
Andreas@49770
  1251
        where kvs'': "rbtreeify_f (n div 2) kvs = (t1, (k', v') # kvs'')"
Andreas@49770
  1252
         by(cases ?rest1)(auto simp add: snd_def split: prod.split_asm)
blanchet@55412
  1253
      note this also note prod.case also note list.simps(5) 
blanchet@55412
  1254
      also note prod.case also note snd_apfst
Andreas@49770
  1255
      also have "0 < n div 2" "n div 2 \<le> Suc (length kvs'')" 
Andreas@49770
  1256
        using len "1.prems" False unfolding kvs'' by simp_all
Andreas@49770
  1257
      with True kvs''[symmetric] refl refl
Andreas@49770
  1258
      have "length (snd (rbtreeify_g (n div 2) kvs'')) = 
Andreas@49770
  1259
        Suc (length kvs'') - n div 2" by(rule IH)
Andreas@49770
  1260
      finally show ?thesis using len[unfolded kvs''] "1.prems" True
Andreas@49770
  1261
        by(simp add: Suc_diff_le[symmetric] mult_2[symmetric] mult_div_cancel)
Andreas@49770
  1262
    next
Andreas@49770
  1263
      case False
Andreas@49770
  1264
      hence "length (snd (rbtreeify_f n kvs)) = 
Andreas@49770
  1265
        length (snd (rbtreeify_f (Suc (2 * (n div 2))) kvs))"
Andreas@49770
  1266
        by(metis Suc_eq_plus1_left comm_semiring_1_class.normalizing_semiring_rules(7)
Andreas@49770
  1267
             mod_2_not_eq_zero_eq_one_nat semiring_div_class.mod_div_equality')
Andreas@49770
  1268
      also from "1.prems" `\<not> n \<le> 1` obtain k v kvs' 
Andreas@49770
  1269
        where kvs: "kvs = (k, v) # kvs'" by(cases kvs) auto
Andreas@49770
  1270
      also have "0 < n div 2" using `\<not> n \<le> 1` by(simp) 
Andreas@49770
  1271
      note rbtreeify_f_simps(4)[OF this]
Andreas@49770
  1272
      also note kvs[symmetric] 
Andreas@49770
  1273
      also let ?rest1 = "snd (rbtreeify_f (n div 2) kvs)"
Andreas@49770
  1274
      from "1.prems" have "n div 2 \<le> length kvs" by simp
Andreas@49770
  1275
      with False have len: "length ?rest1 = length kvs - n div 2" by(rule IH)
Andreas@49770
  1276
      with "1.prems" `\<not> n \<le> 1` obtain t1 k' v' kvs''
Andreas@49770
  1277
        where kvs'': "rbtreeify_f (n div 2) kvs = (t1, (k', v') # kvs'')"
Andreas@49770
  1278
        by(cases ?rest1)(auto simp add: snd_def split: prod.split_asm)
blanchet@55412
  1279
      note this also note prod.case also note list.simps(5)
blanchet@55412
  1280
      also note prod.case also note snd_apfst
Andreas@49770
  1281
      also have "n div 2 \<le> length kvs''" 
Andreas@49770
  1282
        using len "1.prems" False unfolding kvs'' by simp arith
Andreas@49770
  1283
      with False kvs''[symmetric] refl refl
Andreas@49770
  1284
      have "length (snd (rbtreeify_f (n div 2) kvs'')) = length kvs'' - n div 2"
Andreas@49770
  1285
        by(rule IH)
Andreas@49770
  1286
      finally show ?thesis using len[unfolded kvs''] "1.prems" False
Andreas@49770
  1287
        by simp(rule rbtreeify_f_rec_aux_lemma[OF sym])
Andreas@49770
  1288
    qed
Andreas@49770
  1289
  qed
Andreas@49770
  1290
next
Andreas@49770
  1291
  case (2 n kvs)
Andreas@49770
  1292
  show ?case
Andreas@49770
  1293
  proof(cases "n > 1")
Andreas@49770
  1294
    case False with `0 < n` show ?thesis
Andreas@49770
  1295
      by(cases n kvs rule: nat.exhaust[case_product list.exhaust]) simp_all
Andreas@49770
  1296
  next
Andreas@49770
  1297
    case True
Andreas@49770
  1298
    hence "\<not> (n = 0 \<or> n = 1)" by simp
Andreas@49770
  1299
    note IH = "2.IH"[OF this]
Andreas@49770
  1300
    show ?thesis
Andreas@49770
  1301
    proof(cases "n mod 2 = 0")
Andreas@49770
  1302
      case True
Andreas@49770
  1303
      hence "length (snd (rbtreeify_g n kvs)) =
Andreas@49770
  1304
        length (snd (rbtreeify_g (2 * (n div 2)) kvs))"
Andreas@49770
  1305
        by(metis minus_nat.diff_0 mult_div_cancel)
Andreas@49770
  1306
      also from "2.prems" True obtain k v kvs' 
Andreas@49770
  1307
        where kvs: "kvs = (k, v) # kvs'" by(cases kvs) auto
Andreas@49770
  1308
      also have "0 < n div 2" using `1 < n` by(simp) 
Andreas@49770
  1309
      note rbtreeify_g_simps(3)[OF this]
Andreas@49770
  1310
      also note kvs[symmetric] 
Andreas@49770
  1311
      also let ?rest1 = "snd (rbtreeify_g (n div 2) kvs)"
Andreas@49770
  1312
      from "2.prems" `1 < n`
Andreas@49770
  1313
      have "0 < n div 2" "n div 2 \<le> Suc (length kvs)" by simp_all
Andreas@49770
  1314
      with True have len: "length ?rest1 = Suc (length kvs) - n div 2" by(rule IH)
Andreas@49770
  1315
      with "2.prems" obtain t1 k' v' kvs''
Andreas@49770
  1316
        where kvs'': "rbtreeify_g (n div 2) kvs = (t1, (k', v') # kvs'')"
Andreas@49770
  1317
        by(cases ?rest1)(auto simp add: snd_def split: prod.split_asm)
blanchet@55412
  1318
      note this also note prod.case also note list.simps(5) 
blanchet@55412
  1319
      also note prod.case also note snd_apfst
Andreas@49770
  1320
      also have "n div 2 \<le> Suc (length kvs'')" 
Andreas@49770
  1321
        using len "2.prems" unfolding kvs'' by simp
Andreas@49770
  1322
      with True kvs''[symmetric] refl refl `0 < n div 2`
Andreas@49770
  1323
      have "length (snd (rbtreeify_g (n div 2) kvs'')) = Suc (length kvs'') - n div 2"
Andreas@49770
  1324
        by(rule IH)
Andreas@49770
  1325
      finally show ?thesis using len[unfolded kvs''] "2.prems" True
Andreas@49770
  1326
        by(simp add: Suc_diff_le[symmetric] mult_2[symmetric] mult_div_cancel)
Andreas@49770
  1327
    next
Andreas@49770
  1328
      case False
Andreas@49770
  1329
      hence "length (snd (rbtreeify_g n kvs)) = 
Andreas@49770
  1330
        length (snd (rbtreeify_g (Suc (2 * (n div 2))) kvs))"
Andreas@49770
  1331
        by(metis Suc_eq_plus1_left comm_semiring_1_class.normalizing_semiring_rules(7) 
Andreas@49770
  1332
            mod_2_not_eq_zero_eq_one_nat semiring_div_class.mod_div_equality')
Andreas@49770
  1333
      also from "2.prems" `1 < n` obtain k v kvs'
Andreas@49770
  1334
        where kvs: "kvs = (k, v) # kvs'" by(cases kvs) auto
Andreas@49770
  1335
      also have "0 < n div 2" using `1 < n` 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)
Andreas@49770
  1341
      with "2.prems" `1 < n` 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
Andreas@49770
  1348
      with False kvs''[symmetric] refl refl `0 < n div 2`
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@57512
  1419
        thus ?thesis using True by (metis div_mod_equality' 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 
Andreas@49770
  1434
          by simp (metis One_nat_def Suc_eq_plus1_left le_add_diff_inverse mod_less_eq_dividend mult_div_cancel)
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@57512
  1465
          by(metis div_mod_equality' 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 
Andreas@49770
  1481
          by simp (metis One_nat_def Suc_eq_plus1_left le_add_diff_inverse mod_less_eq_dividend mult_div_cancel)
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')
Andreas@49770
  1602
  from rbtreeify_fD[OF `rbtreeify_f n kvs = (t, (k, v) # kvs')` `n \<le> length kvs`]
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)
Andreas@49770
  1606
  from `sorted (map fst kvs)` 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)
Andreas@49770
  1609
  moreover from `distinct (map fst kvs)` 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')"
Andreas@49770
  1615
    using `n \<le> Suc (length kvs')` `n \<le> length kvs` 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)
Andreas@49770
  1617
  moreover from `sorted (map fst kvs)` `distinct (map fst kvs)`
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')"
Andreas@49770
  1620
    using `sorted (map fst kvs)` `distinct (map fst kvs)`
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
Andreas@49770
  1624
    using `0 < n` `rbtreeify_f n kvs = (t, (k, v) # kvs')` by simp
Andreas@49770
  1625
next
Andreas@49770
  1626
  case (f_odd n kvs t k v kvs')
Andreas@49770
  1627
  from rbtreeify_fD[OF `rbtreeify_f n kvs = (t, (k, v) # kvs')` `n \<le> length kvs`]
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)
Andreas@49770
  1631
  from `sorted (map fst kvs)` 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)
Andreas@49770
  1634
  moreover from `distinct (map fst kvs)` 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')"
Andreas@49770
  1640
    using `n \<le> length kvs'` `n \<le> length kvs` set_take_subset[of n kvs']
Andreas@49770
  1641
    by(auto simp add: rbt_greater_prop rbt_less_prop take_map split_def)
Andreas@49770
  1642
  moreover from `sorted (map fst kvs)` `distinct (map fst kvs)`
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')"
Andreas@49770
  1645
    using `sorted (map fst kvs)` `distinct (map fst kvs)`
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 
Andreas@49770
  1649
    using `0 < n` `rbtreeify_f n kvs = (t, (k, v) # kvs')` by simp
Andreas@49770
  1650
next
Andreas@49770
  1651
  case (g_even n kvs t k v kvs')
Andreas@49770
  1652
  from rbtreeify_gD[OF `rbtreeify_g n kvs = (t, (k, v) # kvs')` `n \<le> Suc (length kvs)`]
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)
Andreas@49770
  1656
  from `sorted (map fst kvs)` 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)
Andreas@49770
  1659
  moreover from `distinct (map fst kvs)` 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')"
Andreas@49770
  1665
    using `n \<le> Suc (length kvs')` `n \<le> Suc (length kvs)` set_take_subset[of "n - 1" kvs']
Andreas@49770
  1666
    by(auto simp add: rbt_greater_prop rbt_less_prop take_map split_def)
Andreas@49770
  1667
  moreover from `sorted (map fst kvs)` `distinct (map fst kvs)`
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')"
Andreas@49770
  1670
    using `sorted (map fst kvs)` `distinct (map fst kvs)`
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)
Andreas@49770
  1673
  ultimately show ?case using `0 < n` `rbtreeify_g n kvs = (t, (k, v) # kvs')` by simp
Andreas@49770
  1674
next
Andreas@49770
  1675
  case (g_odd n kvs t k v kvs')
Andreas@49770
  1676
  from rbtreeify_fD[OF `rbtreeify_f n kvs = (t, (k, v) # kvs')` `n \<le> length kvs`]
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)
Andreas@49770
  1680
  from `sorted (map fst kvs)` 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)
Andreas@49770
  1683
  moreover from `distinct (map fst kvs)` 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')"
Andreas@49770
  1689
    using `n \<le> Suc (length kvs')` `n \<le> length kvs` set_take_subset[of "n - 1" kvs']
Andreas@49770
  1690
    by(auto simp add: rbt_greater_prop rbt_less_prop take_map split_def)
Andreas@49770
  1691
  moreover from `sorted (map fst kvs)` `distinct (map fst kvs)`
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')"
Andreas@49770
  1694
    using `sorted (map fst kvs)` `distinct (map fst kvs)`
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
Andreas@49770
  1698
    using `0 < n` `rbtreeify_f n kvs = (t, (k, v) # kvs')` 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
Andreas@49770
  1717
text {* 
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)
Andreas@49770
  1720
*}
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@58249
  1731
datatype_new 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
blanchet@55417
  1758
  compare.nchotomy compare.split compare.split_asm rec_compare_def
Andreas@49770
  1759
  compare.eq.refl compare.eq.simps
Andreas@49770
  1760
  compare.EQ_def compare.GT_def compare.LT_def
Andreas@49770
  1761
  equal_compare_def
Andreas@49807
  1762
  skip_red_def skip_red.simps skip_red.cases skip_red.induct 
Andreas@49807
  1763
  skip_black_def
Andreas@49807
  1764
  compare_height_def compare_height.simps
Andreas@49770
  1765
Andreas@49770
  1766
subsection {* union and intersection of sorted associative lists *}
Andreas@49770
  1767
Andreas@49770
  1768
context ord begin
Andreas@49770
  1769
Andreas@49770
  1770
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
  1771
where
Andreas@49770
  1772
  "sunion_with f ((k, v) # as) ((k', v') # bs) =
Andreas@49770
  1773
   (if k > k' then (k', v') # sunion_with f ((k, v) # as) bs
Andreas@49770
  1774
    else if k < k' then (k, v) # sunion_with f as ((k', v') # bs)
Andreas@49770
  1775
    else (k, f k v v') # sunion_with f as bs)"
Andreas@49770
  1776
| "sunion_with f [] bs = bs"
Andreas@49770
  1777
| "sunion_with f as [] = as"
Andreas@49770
  1778
by pat_completeness auto
Andreas@49770
  1779
termination by lexicographic_order
Andreas@49770
  1780
Andreas@49770
  1781
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
  1782
where
Andreas@49770
  1783
  "sinter_with f ((k, v) # as) ((k', v') # bs) =
Andreas@49770
  1784
  (if k > k' then sinter_with f ((k, v) # as) bs
Andreas@49770
  1785
   else if k < k' then sinter_with f as ((k', v') # bs)
Andreas@49770
  1786
   else (k, f k v v') # sinter_with f as bs)"
Andreas@49770
  1787
| "sinter_with f [] _ = []"
Andreas@49770
  1788
| "sinter_with f _ [] = []"
Andreas@49770
  1789
by pat_completeness auto
Andreas@49770
  1790
termination by lexicographic_order
Andreas@49770
  1791
Andreas@49770
  1792
end
Andreas@49770
  1793
Andreas@49770
  1794
declare ord.sunion_with.simps [code] ord.sinter_with.simps[code]
Andreas@49770
  1795
Andreas@49770
  1796
context linorder begin
Andreas@49770
  1797
Andreas@49770
  1798
lemma set_fst_sunion_with: 
Andreas@49770
  1799
  "set (map fst (sunion_with f xs ys)) = set (map fst xs) \<union> set (map fst ys)"
Andreas@49770
  1800
by(induct f xs ys rule: sunion_with.induct) auto
Andreas@49770
  1801
Andreas@49770
  1802
lemma sorted_sunion_with [simp]:
Andreas@49770
  1803
  "\<lbrakk> sorted (map fst xs); sorted (map fst ys) \<rbrakk> 
Andreas@49770
  1804
  \<Longrightarrow> sorted (map fst (sunion_with f xs ys))"
Andreas@49770
  1805
by(induct f xs ys rule: sunion_with.induct)
Andreas@49770
  1806
  (auto simp add: sorted_Cons set_fst_sunion_with simp del: set_map)
Andreas@49770
  1807
Andreas@49770
  1808
lemma distinct_sunion_with [simp]:
Andreas@49770
  1809
  "\<lbrakk> distinct (map fst xs); distinct (map fst ys); sorted (map fst xs); sorted (map fst ys) \<rbrakk>
Andreas@49770
  1810
  \<Longrightarrow> distinct (map fst (sunion_with f xs ys))"
Andreas@49770
  1811
proof(induct f xs ys rule: sunion_with.induct)
Andreas@49770
  1812
  case (1 f k v xs k' v' ys)
Andreas@49770
  1813
  have "\<lbrakk> \<not> k < k'; \<not> k' < k \<rbrakk> \<Longrightarrow> k = k'" by simp
Andreas@49770
  1814
  thus ?case using "1"
Andreas@49770
  1815
    by(auto simp add: set_fst_sunion_with sorted_Cons simp del: set_map)
Andreas@49770
  1816
qed simp_all
Andreas@49770
  1817
Andreas@49770
  1818
lemma map_of_sunion_with: 
Andreas@49770
  1819
  "\<lbrakk> sorted (map fst xs); sorted (map fst ys) \<rbrakk>
Andreas@49770
  1820
  \<Longrightarrow> map_of (sunion_with f xs ys) k = 
Andreas@49770
  1821
  (case map_of xs k of None \<Rightarrow> map_of ys k 
Andreas@49770
  1822
  | Some v \<Rightarrow> case map_of ys k of None \<Rightarrow> Some v 
Andreas@49770
  1823
              | Some w \<Rightarrow> Some (f k v w))"
Andreas@49770
  1824
by(induct f xs ys rule: sunion_with.induct)(auto simp add: sorted_Cons split: option.split dest: map_of_SomeD bspec)
Andreas@49770
  1825
Andreas@49770
  1826
lemma set_fst_sinter_with [simp]:
Andreas@49770
  1827
  "\<lbrakk> sorted (map fst xs); sorted (map fst ys) \<rbrakk>
Andreas@49770
  1828
  \<Longrightarrow> set (map fst (sinter_with f xs ys)) = set (map fst xs) \<inter> set (map fst ys)"
Andreas@49770
  1829
by(induct f xs ys rule: sinter_with.induct)(auto simp add: sorted_Cons simp del: set_map)
Andreas@49770
  1830
Andreas@49770
  1831
lemma set_fst_sinter_with_subset1:
Andreas@49770
  1832
  "set (map fst (sinter_with f xs ys)) \<subseteq> set (map fst xs)"
Andreas@49770
  1833
by(induct f xs ys rule: sinter_with.induct) auto
Andreas@49770
  1834
Andreas@49770
  1835
lemma set_fst_sinter_with_subset2:
Andreas@49770
  1836
  "set (map fst (sinter_with f xs ys)) \<subseteq> set (map fst ys)"
Andreas@49770
  1837
by(induct f xs ys rule: sinter_with.induct)(auto simp del: set_map)
Andreas@49770
  1838
Andreas@49770
  1839
lemma sorted_sinter_with [simp]:
Andreas@49770
  1840
  "\<lbrakk> sorted (map fst xs); sorted (map fst ys) \<rbrakk>
Andreas@49770
  1841
  \<Longrightarrow> sorted (map fst (sinter_with f xs ys))"
Andreas@49770
  1842
by(induct f xs ys rule: sinter_with.induct)(auto simp add: sorted_Cons simp del: set_map)
Andreas@49770
  1843
Andreas@49770
  1844
lemma distinct_sinter_with [simp]:
Andreas@49770
  1845
  "\<lbrakk> distinct (map fst xs); distinct (map fst ys) \<rbrakk>
Andreas@49770
  1846
  \<Longrightarrow> distinct (map fst (sinter_with f xs ys))"
Andreas@49770
  1847
proof(induct f xs ys rule: sinter_with.induct)
Andreas@49770
  1848
  case (1 f k v as k' v' bs)
Andreas@49770
  1849
  have "\<lbrakk> \<not> k < k'; \<not> k' < k \<rbrakk> \<Longrightarrow> k = k'" by simp
Andreas@49770
  1850
  thus ?case using "1" set_fst_sinter_with_subset1[of f as bs]
Andreas@49770
  1851
    set_fst_sinter_with_subset2[of f as bs]
Andreas@49770
  1852
    by(auto simp del: set_map)
Andreas@49770
  1853
qed simp_all
Andreas@49770
  1854
Andreas@49770
  1855
lemma map_of_sinter_with:
Andreas@49770
  1856
  "\<lbrakk> sorted (map fst xs); sorted (map fst ys) \<rbrakk>
Andreas@49770
  1857
  \<Longrightarrow> map_of (sinter_with f xs ys) k = 
blanchet@55466
  1858
  (case map_of xs k of None \<Rightarrow> None | Some v \<Rightarrow> map_option (f k v) (map_of ys k))"
Andreas@49770
  1859
apply(induct f xs ys rule: sinter_with.induct)
blanchet@55466
  1860
apply(auto simp add: sorted_Cons map_option_case split: option.splits dest: map_of_SomeD bspec)
Andreas@49770
  1861
done
Andreas@49770
  1862
Andreas@49770
  1863
end
Andreas@49770
  1864
Andreas@49770
  1865
lemma distinct_map_of_rev: "distinct (map fst xs) \<Longrightarrow> map_of (rev xs) = map_of xs"
Andreas@49770
  1866
by(induct xs)(auto 4 3 simp add: map_add_def intro!: ext split: option.split intro: rev_image_eqI)
Andreas@49770
  1867
Andreas@49770
  1868
lemma map_map_filter: 
blanchet@55466
  1869
  "map f (List.map_filter g xs) = List.map_filter (map_option f \<circ> g) xs"
Andreas@49770
  1870
by(auto simp add: List.map_filter_def)
Andreas@49770
  1871
blanchet@55466
  1872
lemma map_filter_map_option_const: 
blanchet@55466
  1873
  "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
  1874
by(auto simp add: map_filter_def filter_map o_def)
Andreas@49770
  1875
Andreas@49770
  1876
lemma set_map_filter: "set (List.map_filter P xs) = the ` (P ` set xs - {None})"
Andreas@49770
  1877
by(auto simp add: List.map_filter_def intro: rev_image_eqI)
Andreas@49770
  1878
Andreas@49770
  1879
context ord begin
Andreas@49770
  1880
Andreas@49770
  1881
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
  1882
where
Andreas@49770
  1883
  "rbt_union_with_key f t1 t2 =
Andreas@49770
  1884
  (case RBT_Impl.compare_height t1 t1 t2 t2
Andreas@49770
  1885
   of compare.EQ \<Rightarrow> rbtreeify (sunion_with f (entries t1) (entries t2))
Andreas@49770
  1886
    | compare.LT \<Rightarrow> fold (rbt_insert_with_key (\<lambda>k v w. f k w v)) t1 t2
Andreas@49770
  1887
    | compare.GT \<Rightarrow> fold (rbt_insert_with_key f) t2 t1)"
Andreas@49770
  1888
Andreas@49770
  1889
definition rbt_union_with where
Andreas@49770
  1890
  "rbt_union_with f = rbt_union_with_key (\<lambda>_. f)"
Andreas@49770
  1891
Andreas@49770
  1892
definition rbt_union where
Andreas@49770
  1893
  "rbt_union = rbt_union_with_key (%_ _ rv. rv)"
Andreas@49770
  1894
Andreas@49770
  1895
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
  1896
where
Andreas@49770
  1897
  "rbt_inter_with_key f t1 t2 =
Andreas@49770
  1898
  (case RBT_Impl.compare_height t1 t1 t2 t2 
Andreas@49770
  1899
   of compare.EQ \<Rightarrow> rbtreeify (sinter_with f (entries t1) (entries t2))
blanchet@55466
  1900
    | 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
  1901
    | 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
  1902
Andreas@49770
  1903
definition rbt_inter_with where
Andreas@49770
  1904
  "rbt_inter_with f = rbt_inter_with_key (\<lambda>_. f)"
Andreas@49770
  1905
Andreas@49770
  1906
definition rbt_inter where
Andreas@49770
  1907
  "rbt_inter = rbt_inter_with_key (\<lambda>_ _ rv. rv)"
Andreas@49770
  1908
Andreas@49770
  1909
end
Andreas@49770
  1910
Andreas@49770
  1911
context linorder begin
Andreas@49770
  1912
Andreas@49770
  1913
lemma rbt_sorted_entries_right_unique:
Andreas@49770
  1914
  "\<lbrakk> (k, v) \<in> set (entries t); (k, v') \<in> set (entries t); 
Andreas@49770
  1915
     rbt_sorted t \<rbrakk> \<Longrightarrow> v = v'"
Andreas@49770
  1916
by(auto dest!: distinct_entries inj_onD[where x="(k, v)" and y="(k, v')"] simp add: distinct_map)
Andreas@49770
  1917
Andreas@49770
  1918
lemma rbt_sorted_fold_rbt_insertwk:
Andreas@49770
  1919
  "rbt_sorted t \<Longrightarrow> rbt_sorted (List.fold (\<lambda>(k, v). rbt_insert_with_key f k v) xs t)"
Andreas@49770
  1920
by(induct xs rule: rev_induct)(auto simp add: rbt_insertwk_rbt_sorted)
Andreas@49770
  1921
Andreas@49770
  1922
lemma is_rbt_fold_rbt_insertwk:
Andreas@49770
  1923
  assumes "is_rbt t1"
Andreas@49770
  1924
  shows "is_rbt (fold (rbt_insert_with_key f) t2 t1)"
Andreas@49770
  1925
proof -
Andreas@49770
  1926
  def xs \<equiv> "entries t2"
Andreas@49770
  1927
  from assms show ?thesis unfolding fold_def xs_def[symmetric]
Andreas@49770
  1928
    by(induct xs rule: rev_induct)(auto simp add: rbt_insertwk_is_rbt)
Andreas@49770
  1929
qed
Andreas@49770
  1930
Andreas@49770
  1931
lemma rbt_lookup_fold_rbt_insertwk:
Andreas@49770
  1932
  assumes t1: "rbt_sorted t1" and t2: "rbt_sorted t2"
Andreas@49770
  1933
  shows "rbt_lookup (fold (rbt_insert_with_key f) t1 t2) k =
Andreas@49770
  1934
  (case rbt_lookup t1 k of None \<Rightarrow> rbt_lookup t2 k
Andreas@49770
  1935
   | Some v \<Rightarrow> case rbt_lookup t2 k of None \<Rightarrow> Some v
Andreas@49770
  1936
               | Some w \<Rightarrow> Some (f k w v))"
Andreas@49770
  1937
proof -
Andreas@49770
  1938
  def xs \<equiv> "entries t1"
Andreas@49770
  1939
  hence dt1: "distinct (map fst xs)" using t1 by(simp add: distinct_entries)
Andreas@49770
  1940
  with t2 show ?thesis
Andreas@49770
  1941
    unfolding fold_def map_of_entries[OF t1, symmetric]
Andreas@49770
  1942
      xs_def[symmetric] distinct_map_of_rev[OF dt1, symmetric]
Andreas@49770
  1943
    apply(induct xs rule: rev_induct)
Andreas@49770
  1944
    apply(auto simp add: rbt_lookup_rbt_insertwk rbt_sorted_fold_rbt_insertwk split: option.splits)
Andreas@49770
  1945
    apply(auto simp add: distinct_map_of_rev intro: rev_image_eqI)
Andreas@49770
  1946
    done
Andreas@49770
  1947
qed
Andreas@49770
  1948
Andreas@49770
  1949
lemma is_rbt_rbt_unionwk [simp]:
Andreas@49770
  1950
  "\<lbrakk> is_rbt t1; is_rbt t2 \<rbrakk> \<Longrightarrow> is_rbt (rbt_union_with_key f t1 t2)"
Andreas@49770
  1951
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
  1952
Andreas@49770
  1953
lemma rbt_lookup_rbt_unionwk:
Andreas@49770
  1954
  "\<lbrakk> rbt_sorted t1; rbt_sorted t2 \<rbrakk> 
Andreas@49770
  1955
  \<Longrightarrow> rbt_lookup (rbt_union_with_key f t1 t2) k = 
Andreas@49770
  1956
  (case rbt_lookup t1 k of None \<Rightarrow> rbt_lookup t2 k 
Andreas@49770
  1957
   | Some v \<Rightarrow> case rbt_lookup t2 k of None \<Rightarrow> Some v 
Andreas@49770
  1958
              | Some w \<Rightarrow> Some (f k v w))"
Andreas@49770
  1959
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
  1960
Andreas@49770
  1961
lemma rbt_unionw_is_rbt: "\<lbrakk> is_rbt lt; is_rbt rt \<rbrakk> \<Longrightarrow> is_rbt (rbt_union_with f lt rt)"
Andreas@49770
  1962
by(simp add: rbt_union_with_def)
Andreas@49770
  1963
Andreas@49770
  1964
lemma rbt_union_is_rbt: "\<lbrakk> is_rbt lt; is_rbt rt \<rbrakk> \<Longrightarrow> is_rbt (rbt_union lt rt)"
Andreas@49770
  1965
by(simp add: rbt_union_def)
Andreas@49770
  1966
Andreas@49770
  1967
lemma rbt_lookup_rbt_union:
Andreas@49770
  1968
  "\<lbrakk> rbt_sorted s; rbt_sorted t \<rbrakk> \<Longrightarrow>
Andreas@49770
  1969
  rbt_lookup (rbt_union s t) = rbt_lookup s ++ rbt_lookup t"
Andreas@49770
  1970
by(rule ext)(simp add: rbt_lookup_rbt_unionwk rbt_union_def map_add_def split: option.split)
Andreas@49770
  1971
Andreas@49770
  1972
lemma rbt_interwk_is_rbt [simp]:
Andreas@49770
  1973
  "\<lbrakk> rbt_sorted t1; rbt_sorted t2 \<rbrakk> \<Longrightarrow> is_rbt (rbt_inter_with_key f t1 t2)"
blanchet@55466
  1974
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
  1975
Andreas@49770
  1976
lemma rbt_interw_is_rbt:
Andreas@49770
  1977
  "\<lbrakk> rbt_sorted t1; rbt_sorted t2 \<rbrakk> \<Longrightarrow> is_rbt (rbt_inter_with f t1 t2)"
Andreas@49770
  1978
by(simp add: rbt_inter_with_def)
Andreas@49770
  1979
Andreas@49770
  1980
lemma rbt_inter_is_rbt:
Andreas@49770
  1981
  "\<lbrakk> rbt_sorted t1; rbt_sorted t2 \<rbrakk> \<Longrightarrow> is_rbt (rbt_inter t1 t2)"
Andreas@49770
  1982
by(simp add: rbt_inter_def)
Andreas@49770
  1983
Andreas@49770
  1984
lemma rbt_lookup_rbt_interwk:
Andreas@49770
  1985
  "\<lbrakk> rbt_sorted t1; rbt_sorted t2 \<rbrakk>
Andreas@49770
  1986
  \<Longrightarrow> rbt_lookup (rbt_inter_with_key f t1 t2) k =
Andreas@49770
  1987
  (case rbt_lookup t1 k of None \<Rightarrow> None 
Andreas@49770
  1988
   | Some v \<Rightarrow> case rbt_lookup t2 k of None \<Rightarrow> None
Andreas@49770
  1989
               | Some w \<Rightarrow> Some (f k v w))"
blanchet@55466
  1990
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
  1991
Andreas@49770
  1992
lemma rbt_lookup_rbt_inter:
Andreas@49770
  1993
  "\<lbrakk> rbt_sorted t1; rbt_sorted t2 \<rbrakk>
Andreas@49770
  1994
  \<Longrightarrow> rbt_lookup (rbt_inter t1 t2) = rbt_lookup t2 |` dom (rbt_lookup t1)"
Andreas@49770
  1995
by(auto simp add: rbt_inter_def rbt_lookup_rbt_interwk restrict_map_def split: option.split)
Andreas@49770
  1996
Andreas@49770
  1997
end
Andreas@49770
  1998
Andreas@49770
  1999
Andreas@49480
  2000
subsection {* Code generator setup *}
Andreas@49480
  2001
Andreas@47450
  2002
lemmas [code] =
Andreas@47450
  2003
  ord.rbt_less_prop
Andreas@47450
  2004
  ord.rbt_greater_prop
Andreas@47450
  2005
  ord.rbt_sorted.simps
Andreas@47450
  2006
  ord.rbt_lookup.simps
Andreas@47450
  2007
  ord.is_rbt_def
Andreas@47450
  2008
  ord.rbt_ins.simps
Andreas@47450
  2009
  ord.rbt_insert_with_key_def
Andreas@47450
  2010
  ord.rbt_insertw_def
Andreas@47450
  2011
  ord.rbt_insert_def
Andreas@47450
  2012
  ord.rbt_del_from_left.simps
Andreas@47450
  2013
  ord.rbt_del_from_right.simps
Andreas@47450
  2014
  ord.rbt_del.simps
Andreas@47450
  2015
  ord.rbt_delete_def
Andreas@49770
  2016
  ord.sunion_with.simps
Andreas@49770
  2017
  ord.sinter_with.simps
Andreas@49770
  2018
  ord.rbt_union_with_key_def
Andreas@47450
  2019
  ord.rbt_union_with_def
Andreas@47450
  2020
  ord.rbt_union_def
Andreas@49770
  2021
  ord.rbt_inter_with_key_def
Andreas@49770
  2022
  ord.rbt_inter_with_def
Andreas@49770
  2023
  ord.rbt_inter_def
Andreas@47450
  2024
  ord.rbt_map_entry.simps
Andreas@47450
  2025
  ord.rbt_bulkload_def
Andreas@47450
  2026
Andreas@49480
  2027
text {* More efficient implementations for @{term entries} and @{term keys} *}
Andreas@49480
  2028
Andreas@49480
  2029
definition gen_entries :: 
Andreas@49480
  2030
  "(('a \<times> 'b) \<times> ('a, 'b) rbt) list \<Rightarrow> ('a, 'b) rbt \<Rightarrow> ('a \<times> 'b) list"
Andreas@49480
  2031
where
Andreas@49770
  2032
  "gen_entries kvts t = entries t @ concat (map (\<lambda>(kv, t). kv # entries t) kvts)"
Andreas@49480
  2033
Andreas@49480
  2034
lemma gen_entries_simps [simp, code]:
Andreas@49480
  2035
  "gen_entries [] Empty = []"
Andreas@49480
  2036
  "gen_entries ((kv, t) # kvts) Empty = kv # gen_entries kvts t"
Andreas@49480
  2037
  "gen_entries kvts (Branch c l k v r) = gen_entries (((k, v), r) # kvts) l"
Andreas@49480
  2038
by(simp_all add: gen_entries_def)
Andreas@49480
  2039
Andreas@49480
  2040
lemma entries_code [code]:
Andreas@49480
  2041
  "entries = gen_entries []"
Andreas@49480
  2042
by(simp add: gen_entries_def fun_eq_iff)
Andreas@49480
  2043
Andreas@49480
  2044
definition gen_keys :: "('a \<times> ('a, 'b) rbt) list \<Rightarrow> ('a, 'b) rbt \<Rightarrow> 'a list"
Andreas@49480
  2045
where "gen_keys kts t = RBT_Impl.keys t @ concat (List.map (\<lambda>(k, t). k # keys t) kts)"
Andreas@49480
  2046
Andreas@49480
  2047
lemma gen_keys_simps [simp, code]:
Andreas@49480
  2048
  "gen_keys [] Empty = []"
Andreas@49480
  2049
  "gen_keys ((k, t) # kts) Empty = k # gen_keys kts t"
Andreas@49480
  2050
  "gen_keys kts (Branch c l k v r) = gen_keys ((k, r) # kts) l"
Andreas@49480
  2051
by(simp_all add: gen_keys_def)
Andreas@49480
  2052
Andreas@49480
  2053
lemma keys_code [code]:
Andreas@49480
  2054
  "keys = gen_keys []"
Andreas@49480
  2055
by(simp add: gen_keys_def fun_eq_iff)
Andreas@49480
  2056
Andreas@47450
  2057
text {* Restore original type constraints for constants *}
Andreas@47450
  2058
setup {*
Andreas@47450
  2059
  fold Sign.add_const_constraint
Andreas@47450
  2060
    [(@{const_name rbt_less}, SOME @{typ "('a :: order) \<Rightarrow> ('a, 'b) rbt \<Rightarrow> bool"}),
Andreas@47450
  2061
     (@{const_name rbt_greater}, SOME @{typ "('a :: order) \<Rightarrow> ('a, 'b) rbt \<Rightarrow> bool"}),
Andreas@47450
  2062
     (@{const_name rbt_sorted}, SOME @{typ "('a :: linorder, 'b) rbt \<Rightarrow> bool"}),
Andreas@47450
  2063
     (@{const_name rbt_lookup}, SOME @{typ "('a :: linorder, 'b) rbt \<Rightarrow> 'a \<rightharpoonup> 'b"}),
Andreas@47450
  2064
     (@{const_name is_rbt}, SOME @{typ "('a :: linorder, 'b) rbt \<Rightarrow> bool"}),
Andreas@47450
  2065
     (@{const_name rbt_ins}, SOME @{typ "('a\<Colon>linorder \<Rightarrow> 'b \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"}),
Andreas@47450
  2066
     (@{const_name rbt_insert_with_key}, SOME @{typ "('a\<Colon>linorder \<Rightarrow> 'b \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"}),
Andreas@47450
  2067
     (@{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
  2068
     (@{const_name rbt_insert}, SOME @{typ "('a :: linorder) \<Rightarrow> 'b \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"}),
Andreas@47450
  2069
     (@{const_name rbt_del_from_left}, SOME @{typ "('a\<Colon>linorder) \<Rightarrow> ('a,'b) rbt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"}),
Andreas@47450
  2070
     (@{const_name rbt_del_from_right}, SOME @{typ "('a\<Colon>linorder) \<Rightarrow> ('a,'b) rbt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"}),
Andreas@47450
  2071
     (@{const_name rbt_del}, SOME @{typ "('a\<Colon>linorder) \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"}),
Andreas@47450
  2072
     (@{const_name rbt_delete}, SOME @{typ "('a\<Colon>linorder) \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"}),
Andreas@47450
  2073
     (@{const_name rbt_union_with_key}, SOME @{typ "('a\<Colon>linorder \<Rightarrow> 'b \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"}),
Andreas@47450
  2074
     (@{const_name rbt_union_with}, SOME @{typ "('b \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> ('a\<Colon>linorder,'b) rbt \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"}),
Andreas@47450
  2075
     (@{const_name rbt_union}, SOME @{typ "('a\<Colon>linorder,'b) rbt \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"}),
Andreas@47450
  2076
     (@{const_name rbt_map_entry}, SOME @{typ "'a\<Colon>linorder \<Rightarrow> ('b \<Rightarrow> 'b) \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"}),
Andreas@47450
  2077
     (@{const_name rbt_bulkload}, SOME @{typ "('a \<times> 'b) list \<Rightarrow> ('a\<Colon>linorder,'b) rbt"})]
Andreas@47450
  2078
*}
Andreas@47450
  2079
Andreas@49770
  2080
hide_const (open) R B Empty entries keys fold gen_keys gen_entries
krauss@26192
  2081
krauss@26192
  2082
end