src/HOL/Library/RBT.thy
author nipkow
Wed Mar 04 10:47:20 2009 +0100 (2009-03-04)
changeset 30235 58d147683393
parent 27368 9f90ac19e32b
child 30738 0842e906300c
permissions -rw-r--r--
Made Option a separate theory and renamed option_map to Option.map
krauss@26192
     1
(*  Title:      RBT.thy
krauss@26192
     2
    ID:         $Id$
krauss@26192
     3
    Author:     Markus Reiter, TU Muenchen
krauss@26192
     4
    Author:     Alexander Krauss, TU Muenchen
krauss@26192
     5
*)
krauss@26192
     6
krauss@26192
     7
header {* Red-Black Trees *}
krauss@26192
     8
krauss@26192
     9
(*<*)
krauss@26192
    10
theory RBT
haftmann@27368
    11
imports Plain AssocList
krauss@26192
    12
begin
krauss@26192
    13
krauss@26192
    14
datatype color = R | B
krauss@26192
    15
datatype ('a,'b)"rbt" = Empty | Tr color "('a,'b)rbt" 'a 'b "('a,'b)rbt"
krauss@26192
    16
krauss@26192
    17
(* Suchbaum-Eigenschaften *)
krauss@26192
    18
krauss@26192
    19
primrec
krauss@26192
    20
  pin_tree :: "'a \<Rightarrow> 'b \<Rightarrow> ('a,'b) rbt \<Rightarrow> bool"
krauss@26192
    21
where
krauss@26192
    22
  "pin_tree k v Empty = False"
krauss@26192
    23
| "pin_tree k v (Tr c l x y r) = (k = x \<and> v = y \<or> pin_tree k v l \<or> pin_tree k v r)"
krauss@26192
    24
krauss@26192
    25
primrec
krauss@26192
    26
  keys :: "('k,'v) rbt \<Rightarrow> 'k set"
krauss@26192
    27
where
krauss@26192
    28
  "keys Empty = {}"
krauss@26192
    29
| "keys (Tr _ l k _ r) = { k } \<union> keys l \<union> keys r"
krauss@26192
    30
krauss@26192
    31
lemma pint_keys: "pin_tree k v t \<Longrightarrow> k \<in> keys t" by (induct t) auto
krauss@26192
    32
krauss@26192
    33
primrec tlt :: "'a\<Colon>order \<Rightarrow> ('a,'b) rbt \<Rightarrow> bool"
krauss@26192
    34
where
krauss@26192
    35
  "tlt k Empty = True"
krauss@26192
    36
| "tlt k (Tr c lt kt v rt) = (kt < k \<and> tlt k lt \<and> tlt k rt)"
krauss@26192
    37
krauss@26192
    38
abbreviation tllt (infix "|\<guillemotleft>" 50)
krauss@26192
    39
where "t |\<guillemotleft> x == tlt x t"
krauss@26192
    40
krauss@26192
    41
primrec tgt :: "'a\<Colon>order \<Rightarrow> ('a,'b) rbt \<Rightarrow> bool" (infix "\<guillemotleft>|" 50) 
krauss@26192
    42
where
krauss@26192
    43
  "tgt k Empty = True"
krauss@26192
    44
| "tgt k (Tr c lt kt v rt) = (k < kt \<and> tgt k lt \<and> tgt k rt)"
krauss@26192
    45
krauss@26192
    46
lemma tlt_prop: "(t |\<guillemotleft> k) = (\<forall>x\<in>keys t. x < k)" by (induct t) auto
krauss@26192
    47
lemma tgt_prop: "(k \<guillemotleft>| t) = (\<forall>x\<in>keys t. k < x)" by (induct t) auto
krauss@26192
    48
lemmas tlgt_props = tlt_prop tgt_prop
krauss@26192
    49
krauss@26192
    50
lemmas tgt_nit = tgt_prop pint_keys
krauss@26192
    51
lemmas tlt_nit = tlt_prop pint_keys
krauss@26192
    52
krauss@26192
    53
lemma tlt_trans: "\<lbrakk> t |\<guillemotleft> x; x < y \<rbrakk> \<Longrightarrow> t |\<guillemotleft> y"
krauss@26192
    54
  and tgt_trans: "\<lbrakk> x < y; y \<guillemotleft>| t\<rbrakk> \<Longrightarrow> x \<guillemotleft>| t"
krauss@26192
    55
by (auto simp: tlgt_props)
krauss@26192
    56
krauss@26192
    57
krauss@26192
    58
primrec st :: "('a::linorder, 'b) rbt \<Rightarrow> bool"
krauss@26192
    59
where
krauss@26192
    60
  "st Empty = True"
krauss@26192
    61
| "st (Tr c l k v r) = (l |\<guillemotleft> k \<and> k \<guillemotleft>| r \<and> st l \<and> st r)"
krauss@26192
    62
krauss@26192
    63
primrec map_of :: "('a\<Colon>linorder, 'b) rbt \<Rightarrow> 'a \<rightharpoonup> 'b"
krauss@26192
    64
where
krauss@26192
    65
  "map_of Empty k = None"
krauss@26192
    66
| "map_of (Tr _ l x y r) k = (if k < x then map_of l k else if x < k then map_of r k else Some y)"
krauss@26192
    67
krauss@26192
    68
lemma map_of_tlt[simp]: "t |\<guillemotleft> k \<Longrightarrow> map_of t k = None" 
krauss@26192
    69
by (induct t) auto
krauss@26192
    70
krauss@26192
    71
lemma map_of_tgt[simp]: "k \<guillemotleft>| t \<Longrightarrow> map_of t k = None"
krauss@26192
    72
by (induct t) auto
krauss@26192
    73
krauss@26192
    74
lemma mapof_keys: "st t \<Longrightarrow> dom (map_of t) = keys t"
krauss@26192
    75
by (induct t) (auto simp: dom_def tgt_prop tlt_prop)
krauss@26192
    76
krauss@26192
    77
lemma mapof_pit: "st t \<Longrightarrow> (map_of t k = Some v) = pin_tree k v t"
krauss@26192
    78
by (induct t) (auto simp: tlt_prop tgt_prop pint_keys)
krauss@26192
    79
krauss@26192
    80
lemma map_of_Empty: "map_of Empty = empty"
krauss@26192
    81
by (rule ext) simp
krauss@26192
    82
krauss@26192
    83
(* a kind of extensionality *)
krauss@26192
    84
lemma mapof_from_pit: 
krauss@26192
    85
  assumes st: "st t1" "st t2" 
krauss@26192
    86
  and eq: "\<And>v. pin_tree (k\<Colon>'a\<Colon>linorder) v t1 = pin_tree k v t2" 
krauss@26192
    87
  shows "map_of t1 k = map_of t2 k"
krauss@26192
    88
proof (cases "map_of t1 k")
krauss@26192
    89
  case None
krauss@26192
    90
  then have "\<And>v. \<not> pin_tree k v t1"
krauss@26192
    91
    by (simp add: mapof_pit[symmetric] st)
krauss@26192
    92
  with None show ?thesis
krauss@26192
    93
    by (cases "map_of t2 k") (auto simp: mapof_pit st eq)
krauss@26192
    94
next
krauss@26192
    95
  case (Some a)
krauss@26192
    96
  then show ?thesis
krauss@26192
    97
    apply (cases "map_of t2 k")
krauss@26192
    98
    apply (auto simp: mapof_pit st eq)
krauss@26192
    99
    by (auto simp add: mapof_pit[symmetric] st Some)
krauss@26192
   100
qed
krauss@26192
   101
krauss@26192
   102
subsection {* Red-black properties *}
krauss@26192
   103
krauss@26192
   104
primrec treec :: "('a,'b) rbt \<Rightarrow> color"
krauss@26192
   105
where
krauss@26192
   106
  "treec Empty = B"
krauss@26192
   107
| "treec (Tr c _ _ _ _) = c"
krauss@26192
   108
krauss@26192
   109
primrec inv1 :: "('a,'b) rbt \<Rightarrow> bool"
krauss@26192
   110
where
krauss@26192
   111
  "inv1 Empty = True"
krauss@26192
   112
| "inv1 (Tr c lt k v rt) = (inv1 lt \<and> inv1 rt \<and> (c = B \<or> treec lt = B \<and> treec rt = B))"
krauss@26192
   113
krauss@26192
   114
(* Weaker version *)
krauss@26192
   115
primrec inv1l :: "('a,'b) rbt \<Rightarrow> bool"
krauss@26192
   116
where
krauss@26192
   117
  "inv1l Empty = True"
krauss@26192
   118
| "inv1l (Tr c l k v r) = (inv1 l \<and> inv1 r)"
krauss@26192
   119
lemma [simp]: "inv1 t \<Longrightarrow> inv1l t" by (cases t) simp+
krauss@26192
   120
krauss@26192
   121
primrec bh :: "('a,'b) rbt \<Rightarrow> nat"
krauss@26192
   122
where
krauss@26192
   123
  "bh Empty = 0"
krauss@26192
   124
| "bh (Tr c lt k v rt) = (if c = B then Suc (bh lt) else bh lt)"
krauss@26192
   125
krauss@26192
   126
primrec inv2 :: "('a,'b) rbt \<Rightarrow> bool"
krauss@26192
   127
where
krauss@26192
   128
  "inv2 Empty = True"
krauss@26192
   129
| "inv2 (Tr c lt k v rt) = (inv2 lt \<and> inv2 rt \<and> bh lt = bh rt)"
krauss@26192
   130
krauss@26192
   131
definition
krauss@26192
   132
  "isrbt t = (inv1 t \<and> inv2 t \<and> treec t = B \<and> st t)"
krauss@26192
   133
krauss@26192
   134
lemma isrbt_st[simp]: "isrbt t \<Longrightarrow> st t" by (simp add: isrbt_def)
krauss@26192
   135
krauss@26192
   136
lemma rbt_cases:
krauss@26192
   137
  obtains (Empty) "t = Empty" 
krauss@26192
   138
  | (Red) l k v r where "t = Tr R l k v r" 
krauss@26192
   139
  | (Black) l k v r where "t = Tr B l k v r" 
krauss@26192
   140
by (cases t, simp) (case_tac "color", auto)
krauss@26192
   141
krauss@26192
   142
theorem Empty_isrbt[simp]: "isrbt Empty"
krauss@26192
   143
unfolding isrbt_def by simp
krauss@26192
   144
krauss@26192
   145
krauss@26192
   146
subsection {* Insertion *}
krauss@26192
   147
krauss@26192
   148
fun (* slow, due to massive case splitting *)
krauss@26192
   149
  balance :: "('a,'b) rbt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"
krauss@26192
   150
where
krauss@26192
   151
  "balance (Tr R a w x b) s t (Tr R c y z d) = Tr R (Tr B a w x b) s t (Tr B c y z d)" |
krauss@26192
   152
  "balance (Tr R (Tr R a w x b) s t c) y z d = Tr R (Tr B a w x b) s t (Tr B c y z d)" |
krauss@26192
   153
  "balance (Tr R a w x (Tr R b s t c)) y z d = Tr R (Tr B a w x b) s t (Tr B c y z d)" |
krauss@26192
   154
  "balance a w x (Tr R b s t (Tr R c y z d)) = Tr R (Tr B a w x b) s t (Tr B c y z d)" |
krauss@26192
   155
  "balance a w x (Tr R (Tr R b s t c) y z d) = Tr R (Tr B a w x b) s t (Tr B c y z d)" |
krauss@26192
   156
  "balance a s t b = Tr B a s t b"
krauss@26192
   157
krauss@26192
   158
lemma balance_inv1: "\<lbrakk>inv1l l; inv1l r\<rbrakk> \<Longrightarrow> inv1 (balance l k v r)" 
krauss@26192
   159
  by (induct l k v r rule: balance.induct) auto
krauss@26192
   160
krauss@26192
   161
lemma balance_bh: "bh l = bh r \<Longrightarrow> bh (balance l k v r) = Suc (bh l)"
krauss@26192
   162
  by (induct l k v r rule: balance.induct) auto
krauss@26192
   163
krauss@26192
   164
lemma balance_inv2: 
krauss@26192
   165
  assumes "inv2 l" "inv2 r" "bh l = bh r"
krauss@26192
   166
  shows "inv2 (balance l k v r)"
krauss@26192
   167
  using assms
krauss@26192
   168
  by (induct l k v r rule: balance.induct) auto
krauss@26192
   169
krauss@26192
   170
lemma balance_tgt[simp]: "(v \<guillemotleft>| balance a k x b) = (v \<guillemotleft>| a \<and> v \<guillemotleft>| b \<and> v < k)" 
krauss@26192
   171
  by (induct a k x b rule: balance.induct) auto
krauss@26192
   172
krauss@26192
   173
lemma balance_tlt[simp]: "(balance a k x b |\<guillemotleft> v) = (a |\<guillemotleft> v \<and> b |\<guillemotleft> v \<and> k < v)"
krauss@26192
   174
  by (induct a k x b rule: balance.induct) auto
krauss@26192
   175
krauss@26192
   176
lemma balance_st: 
krauss@26192
   177
  fixes k :: "'a::linorder"
krauss@26192
   178
  assumes "st l" "st r" "l |\<guillemotleft> k" "k \<guillemotleft>| r"
krauss@26192
   179
  shows "st (balance l k v r)"
krauss@26192
   180
using assms proof (induct l k v r rule: balance.induct)
krauss@26192
   181
  case ("2_2" a x w b y t c z s va vb vd vc)
krauss@26192
   182
  hence "y < z \<and> z \<guillemotleft>| Tr B va vb vd vc" 
krauss@26192
   183
    by (auto simp add: tlgt_props)
krauss@26192
   184
  hence "tgt y (Tr B va vb vd vc)" by (blast dest: tgt_trans)
krauss@26192
   185
  with "2_2" show ?case by simp
krauss@26192
   186
next
krauss@26192
   187
  case ("3_2" va vb vd vc x w b y s c z)
krauss@26192
   188
  from "3_2" have "x < y \<and> tlt x (Tr B va vb vd vc)" 
krauss@26192
   189
    by (simp add: tlt.simps tgt.simps)
krauss@26192
   190
  hence "tlt y (Tr B va vb vd vc)" by (blast dest: tlt_trans)
krauss@26192
   191
  with "3_2" show ?case by simp
krauss@26192
   192
next
krauss@26192
   193
  case ("3_3" x w b y s c z t va vb vd vc)
krauss@26192
   194
  from "3_3" have "y < z \<and> tgt z (Tr B va vb vd vc)" by simp
krauss@26192
   195
  hence "tgt y (Tr B va vb vd vc)" by (blast dest: tgt_trans)
krauss@26192
   196
  with "3_3" show ?case by simp
krauss@26192
   197
next
krauss@26192
   198
  case ("3_4" vd ve vg vf x w b y s c z t va vb vii vc)
krauss@26192
   199
  hence "x < y \<and> tlt x (Tr B vd ve vg vf)" by simp
krauss@26192
   200
  hence 1: "tlt y (Tr B vd ve vg vf)" by (blast dest: tlt_trans)
krauss@26192
   201
  from "3_4" have "y < z \<and> tgt z (Tr B va vb vii vc)" by simp
krauss@26192
   202
  hence "tgt y (Tr B va vb vii vc)" by (blast dest: tgt_trans)
krauss@26192
   203
  with 1 "3_4" show ?case by simp
krauss@26192
   204
next
krauss@26192
   205
  case ("4_2" va vb vd vc x w b y s c z t dd)
krauss@26192
   206
  hence "x < y \<and> tlt x (Tr B va vb vd vc)" by simp
krauss@26192
   207
  hence "tlt y (Tr B va vb vd vc)" by (blast dest: tlt_trans)
krauss@26192
   208
  with "4_2" show ?case by simp
krauss@26192
   209
next
krauss@26192
   210
  case ("5_2" x w b y s c z t va vb vd vc)
krauss@26192
   211
  hence "y < z \<and> tgt z (Tr B va vb vd vc)" by simp
krauss@26192
   212
  hence "tgt y (Tr B va vb vd vc)" by (blast dest: tgt_trans)
krauss@26192
   213
  with "5_2" show ?case by simp
krauss@26192
   214
next
krauss@26192
   215
  case ("5_3" va vb vd vc x w b y s c z t)
krauss@26192
   216
  hence "x < y \<and> tlt x (Tr B va vb vd vc)" by simp
krauss@26192
   217
  hence "tlt y (Tr B va vb vd vc)" by (blast dest: tlt_trans)
krauss@26192
   218
  with "5_3" show ?case by simp
krauss@26192
   219
next
krauss@26192
   220
  case ("5_4" va vb vg vc x w b y s c z t vd ve vii vf)
krauss@26192
   221
  hence "x < y \<and> tlt x (Tr B va vb vg vc)" by simp
krauss@26192
   222
  hence 1: "tlt y (Tr B va vb vg vc)" by (blast dest: tlt_trans)
krauss@26192
   223
  from "5_4" have "y < z \<and> tgt z (Tr B vd ve vii vf)" by simp
krauss@26192
   224
  hence "tgt y (Tr B vd ve vii vf)" by (blast dest: tgt_trans)
krauss@26192
   225
  with 1 "5_4" show ?case by simp
krauss@26192
   226
qed simp+
krauss@26192
   227
krauss@26192
   228
lemma keys_balance[simp]: 
krauss@26192
   229
  "keys (balance l k v r) = { k } \<union> keys l \<union> keys r"
krauss@26192
   230
by (induct l k v r rule: balance.induct) auto
krauss@26192
   231
krauss@26192
   232
lemma balance_pit:  
krauss@26192
   233
  "pin_tree k x (balance l v y r) = (pin_tree k x l \<or> k = v \<and> x = y \<or> pin_tree k x r)" 
krauss@26192
   234
by (induct l v y r rule: balance.induct) auto
krauss@26192
   235
krauss@26192
   236
lemma map_of_balance[simp]: 
krauss@26192
   237
fixes k :: "'a::linorder"
krauss@26192
   238
assumes "st l" "st r" "l |\<guillemotleft> k" "k \<guillemotleft>| r"
krauss@26192
   239
shows "map_of (balance l k v r) x = map_of (Tr B l k v r) x"
krauss@26192
   240
by (rule mapof_from_pit) (auto simp:assms balance_pit balance_st)
krauss@26192
   241
krauss@26192
   242
primrec paint :: "color \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"
krauss@26192
   243
where
krauss@26192
   244
  "paint c Empty = Empty"
krauss@26192
   245
| "paint c (Tr _ l k v r) = Tr c l k v r"
krauss@26192
   246
krauss@26192
   247
lemma paint_inv1l[simp]: "inv1l t \<Longrightarrow> inv1l (paint c t)" by (cases t) auto
krauss@26192
   248
lemma paint_inv1[simp]: "inv1l t \<Longrightarrow> inv1 (paint B t)" by (cases t) auto
krauss@26192
   249
lemma paint_inv2[simp]: "inv2 t \<Longrightarrow> inv2 (paint c t)" by (cases t) auto
krauss@26192
   250
lemma paint_treec[simp]: "treec (paint B t) = B" by (cases t) auto
krauss@26192
   251
lemma paint_st[simp]: "st t \<Longrightarrow> st (paint c t)" by (cases t) auto
krauss@26192
   252
lemma paint_pit[simp]: "pin_tree k x (paint c t) = pin_tree k x t" by (cases t) auto
krauss@26192
   253
lemma paint_mapof[simp]: "map_of (paint c t) = map_of t" by (rule ext) (cases t, auto)
krauss@26192
   254
lemma paint_tgt[simp]: "(v \<guillemotleft>| paint c t) = (v \<guillemotleft>| t)" by (cases t) auto
krauss@26192
   255
lemma paint_tlt[simp]: "(paint c t |\<guillemotleft> v) = (t |\<guillemotleft> v)" by (cases t) auto
krauss@26192
   256
krauss@26192
   257
fun
krauss@26192
   258
  ins :: "('a\<Colon>linorder \<Rightarrow> 'b \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"
krauss@26192
   259
where
krauss@26192
   260
  "ins f k v Empty = Tr R Empty k v Empty" |
krauss@26192
   261
  "ins f k v (Tr B l x y r) = (if k < x then balance (ins f k v l) x y r
krauss@26192
   262
                               else if k > x then balance l x y (ins f k v r)
krauss@26192
   263
                               else Tr B l x (f k y v) r)" |
krauss@26192
   264
  "ins f k v (Tr R l x y r) = (if k < x then Tr R (ins f k v l) x y r
krauss@26192
   265
                               else if k > x then Tr R l x y (ins f k v r)
krauss@26192
   266
                               else Tr R l x (f k y v) r)"
krauss@26192
   267
krauss@26192
   268
lemma ins_inv1_inv2: 
krauss@26192
   269
  assumes "inv1 t" "inv2 t"
krauss@26192
   270
  shows "inv2 (ins f k x t)" "bh (ins f k x t) = bh t" 
krauss@26192
   271
  "treec t = B \<Longrightarrow> inv1 (ins f k x t)" "inv1l (ins f k x t)"
krauss@26192
   272
  using assms
krauss@26192
   273
  by (induct f k x t rule: ins.induct) (auto simp: balance_inv1 balance_inv2 balance_bh)
krauss@26192
   274
krauss@26192
   275
lemma ins_tgt[simp]: "(v \<guillemotleft>| ins f k x t) = (v \<guillemotleft>| t \<and> k > v)"
krauss@26192
   276
  by (induct f k x t rule: ins.induct) auto
krauss@26192
   277
lemma ins_tlt[simp]: "(ins f k x t |\<guillemotleft> v) = (t |\<guillemotleft> v \<and> k < v)"
krauss@26192
   278
  by (induct f k x t rule: ins.induct) auto
krauss@26192
   279
lemma ins_st[simp]: "st t \<Longrightarrow> st (ins f k x t)"
krauss@26192
   280
  by (induct f k x t rule: ins.induct) (auto simp: balance_st)
krauss@26192
   281
krauss@26192
   282
lemma keys_ins: "keys (ins f k v t) = { k } \<union> keys t"
krauss@26192
   283
by (induct f k v t rule: ins.induct) auto
krauss@26192
   284
krauss@26192
   285
lemma map_of_ins: 
krauss@26192
   286
  fixes k :: "'a::linorder"
krauss@26192
   287
  assumes "st t"
krauss@26192
   288
  shows "map_of (ins f k v t) x = ((map_of t)(k |-> case map_of t k of None \<Rightarrow> v 
krauss@26192
   289
                                                       | Some w \<Rightarrow> f k w v)) x"
krauss@26192
   290
using assms by (induct f k v t rule: ins.induct) auto
krauss@26192
   291
krauss@26192
   292
definition
krauss@26192
   293
  insertwithkey :: "('a\<Colon>linorder \<Rightarrow> 'b \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"
krauss@26192
   294
where
krauss@26192
   295
  "insertwithkey f k v t = paint B (ins f k v t)"
krauss@26192
   296
krauss@26192
   297
lemma insertwk_st: "st t \<Longrightarrow> st (insertwithkey f k x t)"
krauss@26192
   298
  by (auto simp: insertwithkey_def)
krauss@26192
   299
krauss@26192
   300
theorem insertwk_isrbt: 
krauss@26192
   301
  assumes inv: "isrbt t" 
krauss@26192
   302
  shows "isrbt (insertwithkey f k x t)"
krauss@26192
   303
using assms
krauss@26192
   304
unfolding insertwithkey_def isrbt_def
krauss@26192
   305
by (auto simp: ins_inv1_inv2)
krauss@26192
   306
krauss@26192
   307
lemma map_of_insertwk: 
krauss@26192
   308
  assumes "st t"
krauss@26192
   309
  shows "map_of (insertwithkey f k v t) x = ((map_of t)(k |-> case map_of t k of None \<Rightarrow> v 
krauss@26192
   310
                                                       | Some w \<Rightarrow> f k w v)) x"
krauss@26192
   311
unfolding insertwithkey_def using assms
krauss@26192
   312
by (simp add:map_of_ins)
krauss@26192
   313
krauss@26192
   314
definition
krauss@26192
   315
  insertw_def: "insertwith f = insertwithkey (\<lambda>_. f)"
krauss@26192
   316
krauss@26192
   317
lemma insertw_st: "st t \<Longrightarrow> st (insertwith f k v t)" by (simp add: insertwk_st insertw_def)
krauss@26192
   318
theorem insertw_isrbt: "isrbt t \<Longrightarrow> isrbt (insertwith f k v t)" by (simp add: insertwk_isrbt insertw_def)
krauss@26192
   319
krauss@26192
   320
lemma map_of_insertw:
krauss@26192
   321
  assumes "isrbt t"
krauss@26192
   322
  shows "map_of (insertwith f k v t) = (map_of t)(k \<mapsto> (if k:dom (map_of t) then f (the (map_of t k)) v else v))"
krauss@26192
   323
using assms
krauss@26192
   324
unfolding insertw_def
krauss@26192
   325
by (rule_tac ext) (cases "map_of t k", auto simp:map_of_insertwk dom_def)
krauss@26192
   326
krauss@26192
   327
krauss@26192
   328
definition
krauss@26192
   329
  "insrt k v t = insertwithkey (\<lambda>_ _ nv. nv) k v t"
krauss@26192
   330
krauss@26192
   331
lemma insrt_st: "st t \<Longrightarrow> st (insrt k v t)" by (simp add: insertwk_st insrt_def)
krauss@26192
   332
theorem insrt_isrbt: "isrbt t \<Longrightarrow> isrbt (insrt k v t)" by (simp add: insertwk_isrbt insrt_def)
krauss@26192
   333
krauss@26192
   334
lemma map_of_insert: 
krauss@26192
   335
  assumes "isrbt t"
krauss@26192
   336
  shows "map_of (insrt k v t) = (map_of t)(k\<mapsto>v)"
krauss@26192
   337
unfolding insrt_def
krauss@26192
   338
using assms
krauss@26192
   339
by (rule_tac ext) (simp add: map_of_insertwk split:option.split)
krauss@26192
   340
krauss@26192
   341
krauss@26192
   342
subsection {* Deletion *}
krauss@26192
   343
krauss@26192
   344
(*definition
krauss@26192
   345
  [simp]: "ibn t = (bh t > 0 \<and> treec t = B)"
krauss@26192
   346
*)
krauss@26192
   347
lemma bh_paintR'[simp]: "treec t = B \<Longrightarrow> bh (paint R t) = bh t - 1"
krauss@26192
   348
by (cases t rule: rbt_cases) auto
krauss@26192
   349
krauss@26192
   350
fun
krauss@26192
   351
  balleft :: "('a,'b) rbt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"
krauss@26192
   352
where
krauss@26192
   353
  "balleft (Tr R a k x b) s y c = Tr R (Tr B a k x b) s y c" |
krauss@26192
   354
  "balleft bl k x (Tr B a s y b) = balance bl k x (Tr R a s y b)" |
krauss@26192
   355
  "balleft bl k x (Tr R (Tr B a s y b) t z c) = Tr R (Tr B bl k x a) s y (balance b t z (paint R c))" |
krauss@26192
   356
  "balleft t k x s = Empty"
krauss@26192
   357
krauss@26192
   358
lemma balleft_inv2_with_inv1:
krauss@26192
   359
  assumes "inv2 lt" "inv2 rt" "bh lt + 1 = bh rt" "inv1 rt"
krauss@26192
   360
  shows "bh (balleft lt k v rt) = bh lt + 1"
krauss@26192
   361
  and   "inv2 (balleft lt k v rt)"
krauss@26192
   362
using assms 
krauss@26192
   363
by (induct lt k v rt rule: balleft.induct) (auto simp: balance_inv2 balance_bh)
krauss@26192
   364
krauss@26192
   365
lemma balleft_inv2_app: 
krauss@26192
   366
  assumes "inv2 lt" "inv2 rt" "bh lt + 1 = bh rt" "treec rt = B"
krauss@26192
   367
  shows "inv2 (balleft lt k v rt)" 
krauss@26192
   368
        "bh (balleft lt k v rt) = bh rt"
krauss@26192
   369
using assms 
krauss@26192
   370
by (induct lt k v rt rule: balleft.induct) (auto simp add: balance_inv2 balance_bh)+ 
krauss@26192
   371
krauss@26192
   372
lemma balleft_inv1: "\<lbrakk>inv1l a; inv1 b; treec b = B\<rbrakk> \<Longrightarrow> inv1 (balleft a k x b)"
krauss@26192
   373
  by (induct a k x b rule: balleft.induct) (simp add: balance_inv1)+
krauss@26192
   374
krauss@26192
   375
lemma balleft_inv1l: "\<lbrakk> inv1l lt; inv1 rt \<rbrakk> \<Longrightarrow> inv1l (balleft lt k x rt)"
krauss@26192
   376
by (induct lt k x rt rule: balleft.induct) (auto simp: balance_inv1)
krauss@26192
   377
krauss@26192
   378
lemma balleft_st: "\<lbrakk> st l; st r; tlt k l; tgt k r \<rbrakk> \<Longrightarrow> st (balleft l k v r)"
krauss@26192
   379
apply (induct l k v r rule: balleft.induct)
krauss@26192
   380
apply (auto simp: balance_st)
krauss@26192
   381
apply (unfold tgt_prop tlt_prop)
krauss@26192
   382
by force+
krauss@26192
   383
krauss@26192
   384
lemma balleft_tgt: 
krauss@26192
   385
  fixes k :: "'a::order"
krauss@26192
   386
  assumes "k \<guillemotleft>| a" "k \<guillemotleft>| b" "k < x" 
krauss@26192
   387
  shows "k \<guillemotleft>| balleft a x t b"
krauss@26192
   388
using assms 
krauss@26192
   389
by (induct a x t b rule: balleft.induct) auto
krauss@26192
   390
krauss@26192
   391
lemma balleft_tlt: 
krauss@26192
   392
  fixes k :: "'a::order"
krauss@26192
   393
  assumes "a |\<guillemotleft> k" "b |\<guillemotleft> k" "x < k" 
krauss@26192
   394
  shows "balleft a x t b |\<guillemotleft> k"
krauss@26192
   395
using assms
krauss@26192
   396
by (induct a x t b rule: balleft.induct) auto
krauss@26192
   397
krauss@26192
   398
lemma balleft_pit: 
krauss@26192
   399
  assumes "inv1l l" "inv1 r" "bh l + 1 = bh r"
krauss@26192
   400
  shows "pin_tree k v (balleft l a b r) = (pin_tree k v l \<or> k = a \<and> v = b \<or> pin_tree k v r)"
krauss@26192
   401
using assms 
krauss@26192
   402
by (induct l k v r rule: balleft.induct) (auto simp: balance_pit)
krauss@26192
   403
krauss@26192
   404
fun
krauss@26192
   405
  balright :: "('a,'b) rbt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"
krauss@26192
   406
where
krauss@26192
   407
  "balright a k x (Tr R b s y c) = Tr R a k x (Tr B b s y c)" |
krauss@26192
   408
  "balright (Tr B a k x b) s y bl = balance (Tr R a k x b) s y bl" |
krauss@26192
   409
  "balright (Tr R a k x (Tr B b s y c)) t z bl = Tr R (balance (paint R a) k x b) s y (Tr B c t z bl)" |
krauss@26192
   410
  "balright t k x s = Empty"
krauss@26192
   411
krauss@26192
   412
lemma balright_inv2_with_inv1:
krauss@26192
   413
  assumes "inv2 lt" "inv2 rt" "bh lt = bh rt + 1" "inv1 lt"
krauss@26192
   414
  shows "inv2 (balright lt k v rt) \<and> bh (balright lt k v rt) = bh lt"
krauss@26192
   415
using assms
krauss@26192
   416
by (induct lt k v rt rule: balright.induct) (auto simp: balance_inv2 balance_bh)
krauss@26192
   417
krauss@26192
   418
lemma balright_inv1: "\<lbrakk>inv1 a; inv1l b; treec a = B\<rbrakk> \<Longrightarrow> inv1 (balright a k x b)"
krauss@26192
   419
by (induct a k x b rule: balright.induct) (simp add: balance_inv1)+
krauss@26192
   420
krauss@26192
   421
lemma balright_inv1l: "\<lbrakk> inv1 lt; inv1l rt \<rbrakk> \<Longrightarrow>inv1l (balright lt k x rt)"
krauss@26192
   422
by (induct lt k x rt rule: balright.induct) (auto simp: balance_inv1)
krauss@26192
   423
krauss@26192
   424
lemma balright_st: "\<lbrakk> st l; st r; tlt k l; tgt k r \<rbrakk> \<Longrightarrow> st (balright l k v r)"
krauss@26192
   425
apply (induct l k v r rule: balright.induct)
krauss@26192
   426
apply (auto simp:balance_st)
krauss@26192
   427
apply (unfold tlt_prop tgt_prop)
krauss@26192
   428
by force+
krauss@26192
   429
krauss@26192
   430
lemma balright_tgt: 
krauss@26192
   431
  fixes k :: "'a::order"
krauss@26192
   432
  assumes "k \<guillemotleft>| a" "k \<guillemotleft>| b" "k < x" 
krauss@26192
   433
  shows "k \<guillemotleft>| balright a x t b"
krauss@26192
   434
using assms by (induct a x t b rule: balright.induct) auto
krauss@26192
   435
krauss@26192
   436
lemma balright_tlt: 
krauss@26192
   437
  fixes k :: "'a::order"
krauss@26192
   438
  assumes "a |\<guillemotleft> k" "b |\<guillemotleft> k" "x < k" 
krauss@26192
   439
  shows "balright a x t b |\<guillemotleft> k"
krauss@26192
   440
using assms by (induct a x t b rule: balright.induct) auto
krauss@26192
   441
krauss@26192
   442
lemma balright_pit:
krauss@26192
   443
  assumes "inv1 l" "inv1l r" "bh l = bh r + 1" "inv2 l" "inv2 r"
krauss@26192
   444
  shows "pin_tree x y (balright l k v r) = (pin_tree x y l \<or> x = k \<and> y = v \<or> pin_tree x y r)"
krauss@26192
   445
using assms by (induct l k v r rule: balright.induct) (auto simp: balance_pit)
krauss@26192
   446
krauss@26192
   447
krauss@26192
   448
text {* app *}
krauss@26192
   449
krauss@26192
   450
fun
krauss@26192
   451
  app :: "('a,'b) rbt \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"
krauss@26192
   452
where
krauss@26192
   453
  "app Empty x = x" 
krauss@26192
   454
| "app x Empty = x" 
krauss@26192
   455
| "app (Tr R a k x b) (Tr R c s y d) = (case (app b c) of
krauss@26192
   456
                                      Tr R b2 t z c2 \<Rightarrow> (Tr R (Tr R a k x b2) t z (Tr R c2 s y d)) |
krauss@26192
   457
                                      bc \<Rightarrow> Tr R a k x (Tr R bc s y d))" 
krauss@26192
   458
| "app (Tr B a k x b) (Tr B c s y d) = (case (app b c) of
krauss@26192
   459
                                      Tr R b2 t z c2 \<Rightarrow> Tr R (Tr B a k x b2) t z (Tr B c2 s y d) |
krauss@26192
   460
                                      bc \<Rightarrow> balleft a k x (Tr B bc s y d))" 
krauss@26192
   461
| "app a (Tr R b k x c) = Tr R (app a b) k x c" 
krauss@26192
   462
| "app (Tr R a k x b) c = Tr R a k x (app b c)" 
krauss@26192
   463
krauss@26192
   464
lemma app_inv2:
krauss@26192
   465
  assumes "inv2 lt" "inv2 rt" "bh lt = bh rt"
krauss@26192
   466
  shows "bh (app lt rt) = bh lt" "inv2 (app lt rt)"
krauss@26192
   467
using assms 
krauss@26192
   468
by (induct lt rt rule: app.induct) 
krauss@26192
   469
   (auto simp: balleft_inv2_app split: rbt.splits color.splits)
krauss@26192
   470
krauss@26192
   471
lemma app_inv1: 
krauss@26192
   472
  assumes "inv1 lt" "inv1 rt"
krauss@26192
   473
  shows "treec lt = B \<Longrightarrow> treec rt = B \<Longrightarrow> inv1 (app lt rt)"
krauss@26192
   474
         "inv1l (app lt rt)"
krauss@26192
   475
using assms 
krauss@26192
   476
by (induct lt rt rule: app.induct)
krauss@26192
   477
   (auto simp: balleft_inv1 split: rbt.splits color.splits)
krauss@26192
   478
krauss@26192
   479
lemma app_tgt[simp]: 
krauss@26192
   480
  fixes k :: "'a::linorder"
krauss@26192
   481
  assumes "k \<guillemotleft>| l" "k \<guillemotleft>| r" 
krauss@26192
   482
  shows "k \<guillemotleft>| app l r"
krauss@26192
   483
using assms 
krauss@26192
   484
by (induct l r rule: app.induct)
krauss@26192
   485
   (auto simp: balleft_tgt split:rbt.splits color.splits)
krauss@26192
   486
krauss@26192
   487
lemma app_tlt[simp]: 
krauss@26192
   488
  fixes k :: "'a::linorder"
krauss@26192
   489
  assumes "l |\<guillemotleft> k" "r |\<guillemotleft> k" 
krauss@26192
   490
  shows "app l r |\<guillemotleft> k"
krauss@26192
   491
using assms 
krauss@26192
   492
by (induct l r rule: app.induct)
krauss@26192
   493
   (auto simp: balleft_tlt split:rbt.splits color.splits)
krauss@26192
   494
krauss@26192
   495
lemma app_st: 
krauss@26192
   496
  fixes k :: "'a::linorder"
krauss@26192
   497
  assumes "st l" "st r" "l |\<guillemotleft> k" "k \<guillemotleft>| r"
krauss@26192
   498
  shows "st (app l r)"
krauss@26192
   499
using assms proof (induct l r rule: app.induct)
krauss@26192
   500
  case (3 a x v b c y w d)
krauss@26192
   501
  hence ineqs: "a |\<guillemotleft> x" "x \<guillemotleft>| b" "b |\<guillemotleft> k" "k \<guillemotleft>| c" "c |\<guillemotleft> y" "y \<guillemotleft>| d"
krauss@26192
   502
    by auto
krauss@26192
   503
  with 3
krauss@26192
   504
  show ?case
krauss@26192
   505
    apply (cases "app b c" rule: rbt_cases)
krauss@26192
   506
    apply auto
krauss@26192
   507
    by (metis app_tgt app_tlt ineqs ineqs tlt.simps(2) tgt.simps(2) tgt_trans tlt_trans)+
krauss@26192
   508
next
krauss@26192
   509
  case (4 a x v b c y w d)
krauss@26192
   510
  hence "x < k \<and> tgt k c" by simp
krauss@26192
   511
  hence "tgt x c" by (blast dest: tgt_trans)
krauss@26192
   512
  with 4 have 2: "tgt x (app b c)" by (simp add: app_tgt)
krauss@26192
   513
  from 4 have "k < y \<and> tlt k b" by simp
krauss@26192
   514
  hence "tlt y b" by (blast dest: tlt_trans)
krauss@26192
   515
  with 4 have 3: "tlt y (app b c)" by (simp add: app_tlt)
krauss@26192
   516
  show ?case
krauss@26192
   517
  proof (cases "app b c" rule: rbt_cases)
krauss@26192
   518
    case Empty
krauss@26192
   519
    from 4 have "x < y \<and> tgt y d" by auto
krauss@26192
   520
    hence "tgt x d" by (blast dest: tgt_trans)
krauss@26192
   521
    with 4 Empty have "st a" and "st (Tr B Empty y w d)" and "tlt x a" and "tgt x (Tr B Empty y w d)" by auto
krauss@26192
   522
    with Empty show ?thesis by (simp add: balleft_st)
krauss@26192
   523
  next
krauss@26192
   524
    case (Red lta va ka rta)
krauss@26192
   525
    with 2 4 have "x < va \<and> tlt x a" by simp
krauss@26192
   526
    hence 5: "tlt va a" by (blast dest: tlt_trans)
krauss@26192
   527
    from Red 3 4 have "va < y \<and> tgt y d" by simp
krauss@26192
   528
    hence "tgt va d" by (blast dest: tgt_trans)
krauss@26192
   529
    with Red 2 3 4 5 show ?thesis by simp
krauss@26192
   530
  next
krauss@26192
   531
    case (Black lta va ka rta)
krauss@26192
   532
    from 4 have "x < y \<and> tgt y d" by auto
krauss@26192
   533
    hence "tgt x d" by (blast dest: tgt_trans)
krauss@26192
   534
    with Black 2 3 4 have "st a" and "st (Tr B (app b c) y w d)" and "tlt x a" and "tgt x (Tr B (app b c) y w d)" by auto
krauss@26192
   535
    with Black show ?thesis by (simp add: balleft_st)
krauss@26192
   536
  qed
krauss@26192
   537
next
krauss@26192
   538
  case (5 va vb vd vc b x w c)
krauss@26192
   539
  hence "k < x \<and> tlt k (Tr B va vb vd vc)" by simp
krauss@26192
   540
  hence "tlt x (Tr B va vb vd vc)" by (blast dest: tlt_trans)
krauss@26192
   541
  with 5 show ?case by (simp add: app_tlt)
krauss@26192
   542
next
krauss@26192
   543
  case (6 a x v b va vb vd vc)
krauss@26192
   544
  hence "x < k \<and> tgt k (Tr B va vb vd vc)" by simp
krauss@26192
   545
  hence "tgt x (Tr B va vb vd vc)" by (blast dest: tgt_trans)
krauss@26192
   546
  with 6 show ?case by (simp add: app_tgt)
krauss@26192
   547
qed simp+
krauss@26192
   548
krauss@26192
   549
lemma app_pit: 
krauss@26192
   550
  assumes "inv2 l" "inv2 r" "bh l = bh r" "inv1 l" "inv1 r"
krauss@26192
   551
  shows "pin_tree k v (app l r) = (pin_tree k v l \<or> pin_tree k v r)"
krauss@26192
   552
using assms 
krauss@26192
   553
proof (induct l r rule: app.induct)
krauss@26192
   554
  case (4 _ _ _ b c)
krauss@26192
   555
  hence a: "bh (app b c) = bh b" by (simp add: app_inv2)
krauss@26192
   556
  from 4 have b: "inv1l (app b c)" by (simp add: app_inv1)
krauss@26192
   557
krauss@26192
   558
  show ?case
krauss@26192
   559
  proof (cases "app b c" rule: rbt_cases)
krauss@26192
   560
    case Empty
krauss@26192
   561
    with 4 a show ?thesis by (auto simp: balleft_pit)
krauss@26192
   562
  next
krauss@26192
   563
    case (Red lta ka va rta)
krauss@26192
   564
    with 4 show ?thesis by auto
krauss@26192
   565
  next
krauss@26192
   566
    case (Black lta ka va rta)
krauss@26192
   567
    with a b 4  show ?thesis by (auto simp: balleft_pit)
krauss@26192
   568
  qed 
krauss@26192
   569
qed (auto split: rbt.splits color.splits)
krauss@26192
   570
krauss@26192
   571
fun
krauss@26192
   572
  delformLeft :: "('a\<Colon>linorder) \<Rightarrow> ('a,'b) rbt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt" and
krauss@26192
   573
  delformRight :: "('a\<Colon>linorder) \<Rightarrow> ('a,'b) rbt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt" and
krauss@26192
   574
  del :: "('a\<Colon>linorder) \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"
krauss@26192
   575
where
krauss@26192
   576
  "del x Empty = Empty" |
krauss@26192
   577
  "del x (Tr c a y s b) = (if x < y then delformLeft x a y s b else (if x > y then delformRight x a y s b else app a b))" |
krauss@26192
   578
  "delformLeft x (Tr B lt z v rt) y s b = balleft (del x (Tr B lt z v rt)) y s b" |
krauss@26192
   579
  "delformLeft x a y s b = Tr R (del x a) y s b" |
krauss@26192
   580
  "delformRight x a y s (Tr B lt z v rt) = balright a y s (del x (Tr B lt z v rt))" | 
krauss@26192
   581
  "delformRight x a y s b = Tr R a y s (del x b)"
krauss@26192
   582
krauss@26192
   583
lemma 
krauss@26192
   584
  assumes "inv2 lt" "inv1 lt"
krauss@26192
   585
  shows
krauss@26192
   586
  "\<lbrakk>inv2 rt; bh lt = bh rt; inv1 rt\<rbrakk> \<Longrightarrow>
krauss@26192
   587
  inv2 (delformLeft x lt k v rt) \<and> bh (delformLeft x lt k v rt) = bh lt \<and> (treec lt = B \<and> treec rt = B \<and> inv1 (delformLeft x lt k v rt) \<or> (treec lt \<noteq> B \<or> treec rt \<noteq> B) \<and> inv1l (delformLeft x lt k v rt))"
krauss@26192
   588
  and "\<lbrakk>inv2 rt; bh lt = bh rt; inv1 rt\<rbrakk> \<Longrightarrow>
krauss@26192
   589
  inv2 (delformRight x lt k v rt) \<and> bh (delformRight x lt k v rt) = bh lt \<and> (treec lt = B \<and> treec rt = B \<and> inv1 (delformRight x lt k v rt) \<or> (treec lt \<noteq> B \<or> treec rt \<noteq> B) \<and> inv1l (delformRight x lt k v rt))"
krauss@26192
   590
  and del_inv1_inv2: "inv2 (del x lt) \<and> (treec lt = R \<and> bh (del x lt) = bh lt \<and> inv1 (del x lt) 
krauss@26192
   591
  \<or> treec lt = B \<and> bh (del x lt) = bh lt - 1 \<and> inv1l (del x lt))"
krauss@26192
   592
using assms
krauss@26192
   593
proof (induct x lt k v rt and x lt k v rt and x lt rule: delformLeft_delformRight_del.induct)
krauss@26192
   594
case (2 y c _ y')
krauss@26192
   595
  have "y = y' \<or> y < y' \<or> y > y'" by auto
krauss@26192
   596
  thus ?case proof (elim disjE)
krauss@26192
   597
    assume "y = y'"
krauss@26192
   598
    with 2 show ?thesis by (cases c) (simp add: app_inv2 app_inv1)+
krauss@26192
   599
  next
krauss@26192
   600
    assume "y < y'"
krauss@26192
   601
    with 2 show ?thesis by (cases c) auto
krauss@26192
   602
  next
krauss@26192
   603
    assume "y' < y"
krauss@26192
   604
    with 2 show ?thesis by (cases c) auto
krauss@26192
   605
  qed
krauss@26192
   606
next
krauss@26192
   607
  case (3 y lt z v rta y' ss bb) 
krauss@26192
   608
  thus ?case by (cases "treec (Tr B lt z v rta) = B \<and> treec bb = B") (simp add: balleft_inv2_with_inv1 balleft_inv1 balleft_inv1l)+
krauss@26192
   609
next
krauss@26192
   610
  case (5 y a y' ss lt z v rta)
krauss@26192
   611
  thus ?case by (cases "treec a = B \<and> treec (Tr B lt z v rta) = B") (simp add: balright_inv2_with_inv1 balright_inv1 balright_inv1l)+
krauss@26192
   612
next
krauss@26192
   613
  case ("6_1" y a y' ss) thus ?case by (cases "treec a = B \<and> treec Empty = B") simp+
krauss@26192
   614
qed auto
krauss@26192
   615
krauss@26192
   616
lemma 
krauss@26192
   617
  delformLeft_tlt: "\<lbrakk>tlt v lt; tlt v rt; k < v\<rbrakk> \<Longrightarrow> tlt v (delformLeft x lt k y rt)"
krauss@26192
   618
  and delformRight_tlt: "\<lbrakk>tlt v lt; tlt v rt; k < v\<rbrakk> \<Longrightarrow> tlt v (delformRight x lt k y rt)"
krauss@26192
   619
  and del_tlt: "tlt v lt \<Longrightarrow> tlt v (del x lt)"
krauss@26192
   620
by (induct x lt k y rt and x lt k y rt and x lt rule: delformLeft_delformRight_del.induct) 
krauss@26192
   621
   (auto simp: balleft_tlt balright_tlt)
krauss@26192
   622
krauss@26192
   623
lemma delformLeft_tgt: "\<lbrakk>tgt v lt; tgt v rt; k > v\<rbrakk> \<Longrightarrow> tgt v (delformLeft x lt k y rt)"
krauss@26192
   624
  and delformRight_tgt: "\<lbrakk>tgt v lt; tgt v rt; k > v\<rbrakk> \<Longrightarrow> tgt v (delformRight x lt k y rt)"
krauss@26192
   625
  and del_tgt: "tgt v lt \<Longrightarrow> tgt v (del x lt)"
krauss@26192
   626
by (induct x lt k y rt and x lt k y rt and x lt rule: delformLeft_delformRight_del.induct)
krauss@26192
   627
   (auto simp: balleft_tgt balright_tgt)
krauss@26192
   628
krauss@26192
   629
lemma "\<lbrakk>st lt; st rt; tlt k lt; tgt k rt\<rbrakk> \<Longrightarrow> st (delformLeft x lt k y rt)"
krauss@26192
   630
  and "\<lbrakk>st lt; st rt; tlt k lt; tgt k rt\<rbrakk> \<Longrightarrow> st (delformRight x lt k y rt)"
krauss@26192
   631
  and del_st: "st lt \<Longrightarrow> st (del x lt)"
krauss@26192
   632
proof (induct x lt k y rt and x lt k y rt and x lt rule: delformLeft_delformRight_del.induct)
krauss@26192
   633
  case (3 x lta zz v rta yy ss bb)
krauss@26192
   634
  from 3 have "tlt yy (Tr B lta zz v rta)" by simp
krauss@26192
   635
  hence "tlt yy (del x (Tr B lta zz v rta))" by (rule del_tlt)
krauss@26192
   636
  with 3 show ?case by (simp add: balleft_st)
krauss@26192
   637
next
krauss@26192
   638
  case ("4_2" x vaa vbb vdd vc yy ss bb)
krauss@26192
   639
  hence "tlt yy (Tr R vaa vbb vdd vc)" by simp
krauss@26192
   640
  hence "tlt yy (del x (Tr R vaa vbb vdd vc))" by (rule del_tlt)
krauss@26192
   641
  with "4_2" show ?case by simp
krauss@26192
   642
next
krauss@26192
   643
  case (5 x aa yy ss lta zz v rta) 
krauss@26192
   644
  hence "tgt yy (Tr B lta zz v rta)" by simp
krauss@26192
   645
  hence "tgt yy (del x (Tr B lta zz v rta))" by (rule del_tgt)
krauss@26192
   646
  with 5 show ?case by (simp add: balright_st)
krauss@26192
   647
next
krauss@26192
   648
  case ("6_2" x aa yy ss vaa vbb vdd vc)
krauss@26192
   649
  hence "tgt yy (Tr R vaa vbb vdd vc)" by simp
krauss@26192
   650
  hence "tgt yy (del x (Tr R vaa vbb vdd vc))" by (rule del_tgt)
krauss@26192
   651
  with "6_2" show ?case by simp
krauss@26192
   652
qed (auto simp: app_st)
krauss@26192
   653
krauss@26192
   654
lemma "\<lbrakk>st lt; st rt; tlt kt lt; tgt kt rt; inv1 lt; inv1 rt; inv2 lt; inv2 rt; bh lt = bh rt; x < kt\<rbrakk> \<Longrightarrow> pin_tree k v (delformLeft x lt kt y rt) = (False \<or> (x \<noteq> k \<and> pin_tree k v (Tr c lt kt y rt)))"
krauss@26192
   655
  and "\<lbrakk>st lt; st rt; tlt kt lt; tgt kt rt; inv1 lt; inv1 rt; inv2 lt; inv2 rt; bh lt = bh rt; x > kt\<rbrakk> \<Longrightarrow> pin_tree k v (delformRight x lt kt y rt) = (False \<or> (x \<noteq> k \<and> pin_tree k v (Tr c lt kt y rt)))"
krauss@26192
   656
  and del_pit: "\<lbrakk>st t; inv1 t; inv2 t\<rbrakk> \<Longrightarrow> pin_tree k v (del x t) = (False \<or> (x \<noteq> k \<and> pin_tree k v t))"
krauss@26192
   657
proof (induct x lt kt y rt and x lt kt y rt and x t rule: delformLeft_delformRight_del.induct)
krauss@26192
   658
  case (2 xx c aa yy ss bb)
krauss@26192
   659
  have "xx = yy \<or> xx < yy \<or> xx > yy" by auto
krauss@26192
   660
  from this 2 show ?case proof (elim disjE)
krauss@26192
   661
    assume "xx = yy"
krauss@26192
   662
    with 2 show ?thesis proof (cases "xx = k")
krauss@26192
   663
      case True
krauss@26192
   664
      from 2 `xx = yy` `xx = k` have "st (Tr c aa yy ss bb) \<and> k = yy" by simp
krauss@26192
   665
      hence "\<not> pin_tree k v aa" "\<not> pin_tree k v bb" by (auto simp: tlt_nit tgt_prop)
krauss@26192
   666
      with `xx = yy` 2 `xx = k` show ?thesis by (simp add: app_pit)
krauss@26192
   667
    qed (simp add: app_pit)
krauss@26192
   668
  qed simp+
krauss@26192
   669
next    
krauss@26192
   670
  case (3 xx lta zz vv rta yy ss bb)
krauss@26192
   671
  def mt[simp]: mt == "Tr B lta zz vv rta"
krauss@26192
   672
  from 3 have "inv2 mt \<and> inv1 mt" by simp
krauss@26192
   673
  hence "inv2 (del xx mt) \<and> (treec mt = R \<and> bh (del xx mt) = bh mt \<and> inv1 (del xx mt) \<or> treec mt = B \<and> bh (del xx mt) = bh mt - 1 \<and> inv1l (del xx mt))" by (blast dest: del_inv1_inv2)
krauss@26192
   674
  with 3 have 4: "pin_tree k v (delformLeft xx mt yy ss bb) = (False \<or> xx \<noteq> k \<and> pin_tree k v mt \<or> (k = yy \<and> v = ss) \<or> pin_tree k v bb)" by (simp add: balleft_pit)
krauss@26192
   675
  thus ?case proof (cases "xx = k")
krauss@26192
   676
    case True
krauss@26192
   677
    from 3 True have "tgt yy bb \<and> yy > k" by simp
krauss@26192
   678
    hence "tgt k bb" by (blast dest: tgt_trans)
krauss@26192
   679
    with 3 4 True show ?thesis by (auto simp: tgt_nit)
krauss@26192
   680
  qed auto
krauss@26192
   681
next
krauss@26192
   682
  case ("4_1" xx yy ss bb)
krauss@26192
   683
  show ?case proof (cases "xx = k")
krauss@26192
   684
    case True
krauss@26192
   685
    with "4_1" have "tgt yy bb \<and> k < yy" by simp
krauss@26192
   686
    hence "tgt k bb" by (blast dest: tgt_trans)
krauss@26192
   687
    with "4_1" `xx = k` 
krauss@26192
   688
   have "pin_tree k v (Tr R Empty yy ss bb) = pin_tree k v Empty" by (auto simp: tgt_nit)
krauss@26192
   689
    thus ?thesis by auto
krauss@26192
   690
  qed simp+
krauss@26192
   691
next
krauss@26192
   692
  case ("4_2" xx vaa vbb vdd vc yy ss bb)
krauss@26192
   693
  thus ?case proof (cases "xx = k")
krauss@26192
   694
    case True
krauss@26192
   695
    with "4_2" have "k < yy \<and> tgt yy bb" by simp
krauss@26192
   696
    hence "tgt k bb" by (blast dest: tgt_trans)
krauss@26192
   697
    with True "4_2" show ?thesis by (auto simp: tgt_nit)
krauss@26192
   698
  qed simp
krauss@26192
   699
next
krauss@26192
   700
  case (5 xx aa yy ss lta zz vv rta)
krauss@26192
   701
  def mt[simp]: mt == "Tr B lta zz vv rta"
krauss@26192
   702
  from 5 have "inv2 mt \<and> inv1 mt" by simp
krauss@26192
   703
  hence "inv2 (del xx mt) \<and> (treec mt = R \<and> bh (del xx mt) = bh mt \<and> inv1 (del xx mt) \<or> treec mt = B \<and> bh (del xx mt) = bh mt - 1 \<and> inv1l (del xx mt))" by (blast dest: del_inv1_inv2)
krauss@26192
   704
  with 5 have 3: "pin_tree k v (delformRight xx aa yy ss mt) = (pin_tree k v aa \<or> (k = yy \<and> v = ss) \<or> False \<or> xx \<noteq> k \<and> pin_tree k v mt)" by (simp add: balright_pit)
krauss@26192
   705
  thus ?case proof (cases "xx = k")
krauss@26192
   706
    case True
krauss@26192
   707
    from 5 True have "tlt yy aa \<and> yy < k" by simp
krauss@26192
   708
    hence "tlt k aa" by (blast dest: tlt_trans)
krauss@26192
   709
    with 3 5 True show ?thesis by (auto simp: tlt_nit)
krauss@26192
   710
  qed auto
krauss@26192
   711
next
krauss@26192
   712
  case ("6_1" xx aa yy ss)
krauss@26192
   713
  show ?case proof (cases "xx = k")
krauss@26192
   714
    case True
krauss@26192
   715
    with "6_1" have "tlt yy aa \<and> k > yy" by simp
krauss@26192
   716
    hence "tlt k aa" by (blast dest: tlt_trans)
krauss@26192
   717
    with "6_1" `xx = k` show ?thesis by (auto simp: tlt_nit)
krauss@26192
   718
  qed simp
krauss@26192
   719
next
krauss@26192
   720
  case ("6_2" xx aa yy ss vaa vbb vdd vc)
krauss@26192
   721
  thus ?case proof (cases "xx = k")
krauss@26192
   722
    case True
krauss@26192
   723
    with "6_2" have "k > yy \<and> tlt yy aa" by simp
krauss@26192
   724
    hence "tlt k aa" by (blast dest: tlt_trans)
krauss@26192
   725
    with True "6_2" show ?thesis by (auto simp: tlt_nit)
krauss@26192
   726
  qed simp
krauss@26192
   727
qed simp
krauss@26192
   728
krauss@26192
   729
krauss@26192
   730
definition delete where
krauss@26192
   731
  delete_def: "delete k t = paint B (del k t)"
krauss@26192
   732
krauss@26192
   733
theorem delete_isrbt[simp]: assumes "isrbt t" shows "isrbt (delete k t)"
krauss@26192
   734
proof -
krauss@26192
   735
  from assms have "inv2 t" and "inv1 t" unfolding isrbt_def by auto 
krauss@26192
   736
  hence "inv2 (del k t) \<and> (treec t = R \<and> bh (del k t) = bh t \<and> inv1 (del k t) \<or> treec t = B \<and> bh (del k t) = bh t - 1 \<and> inv1l (del k t))" by (rule del_inv1_inv2)
krauss@26192
   737
  hence "inv2 (del k t) \<and> inv1l (del k t)" by (cases "treec t") auto
krauss@26192
   738
  with assms show ?thesis
krauss@26192
   739
    unfolding isrbt_def delete_def
krauss@26192
   740
    by (auto intro: paint_st del_st)
krauss@26192
   741
qed
krauss@26192
   742
krauss@26192
   743
lemma delete_pit: 
krauss@26192
   744
  assumes "isrbt t" 
krauss@26192
   745
  shows "pin_tree k v (delete x t) = (x \<noteq> k \<and> pin_tree k v t)"
krauss@26192
   746
  using assms unfolding isrbt_def delete_def
krauss@26192
   747
  by (auto simp: del_pit)
krauss@26192
   748
krauss@26192
   749
lemma map_of_delete:
krauss@26192
   750
  assumes isrbt: "isrbt t"
krauss@26192
   751
  shows "map_of (delete k t) = (map_of t)|`(-{k})"
krauss@26192
   752
proof
krauss@26192
   753
  fix x
krauss@26192
   754
  show "map_of (delete k t) x = (map_of t |` (-{k})) x" 
krauss@26192
   755
  proof (cases "x = k")
krauss@26192
   756
    assume "x = k" 
krauss@26192
   757
    with isrbt show ?thesis
krauss@26192
   758
      by (cases "map_of (delete k t) k") (auto simp: mapof_pit delete_pit)
krauss@26192
   759
  next
krauss@26192
   760
    assume "x \<noteq> k"
krauss@26192
   761
    thus ?thesis
krauss@26192
   762
      by auto (metis isrbt delete_isrbt delete_pit isrbt_st mapof_from_pit)
krauss@26192
   763
  qed
krauss@26192
   764
qed
krauss@26192
   765
krauss@26192
   766
subsection {* Union *}
krauss@26192
   767
krauss@26192
   768
primrec
krauss@26192
   769
  unionwithkey :: "('a\<Colon>linorder \<Rightarrow> 'b \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"
krauss@26192
   770
where
krauss@26192
   771
  "unionwithkey f t Empty = t"
krauss@26192
   772
| "unionwithkey f t (Tr c lt k v rt) = unionwithkey f (unionwithkey f (insertwithkey f k v t) lt) rt"
krauss@26192
   773
krauss@26192
   774
lemma unionwk_st: "st lt \<Longrightarrow> st (unionwithkey f lt rt)" 
krauss@26192
   775
  by (induct rt arbitrary: lt) (auto simp: insertwk_st)
krauss@26192
   776
theorem unionwk_isrbt[simp]: "isrbt lt \<Longrightarrow> isrbt (unionwithkey f lt rt)" 
krauss@26192
   777
  by (induct rt arbitrary: lt) (simp add: insertwk_isrbt)+
krauss@26192
   778
krauss@26192
   779
definition
krauss@26192
   780
  unionwith where
krauss@26192
   781
  "unionwith f = unionwithkey (\<lambda>_. f)"
krauss@26192
   782
krauss@26192
   783
theorem unionw_isrbt: "isrbt lt \<Longrightarrow> isrbt (unionwith f lt rt)" unfolding unionwith_def by simp
krauss@26192
   784
krauss@26192
   785
definition union where
krauss@26192
   786
  "union = unionwithkey (%_ _ rv. rv)"
krauss@26192
   787
krauss@26192
   788
theorem union_isrbt: "isrbt lt \<Longrightarrow> isrbt (union lt rt)" unfolding union_def by simp
krauss@26192
   789
krauss@26192
   790
lemma union_Tr[simp]:
krauss@26192
   791
  "union t (Tr c lt k v rt) = union (union (insrt k v t) lt) rt"
krauss@26192
   792
  unfolding union_def insrt_def
krauss@26192
   793
  by simp
krauss@26192
   794
krauss@26192
   795
lemma map_of_union:
krauss@26192
   796
  assumes "isrbt s" "st t"
krauss@26192
   797
  shows "map_of (union s t) = map_of s ++ map_of t"
krauss@26192
   798
using assms
krauss@26192
   799
proof (induct t arbitrary: s)
krauss@26192
   800
  case Empty thus ?case by (auto simp: union_def)
krauss@26192
   801
next
krauss@26192
   802
  case (Tr c l k v r s)
krauss@26192
   803
  hence strl: "st r" "st l" "l |\<guillemotleft> k" "k \<guillemotleft>| r" by auto
krauss@26192
   804
krauss@26192
   805
  have meq: "map_of s(k \<mapsto> v) ++ map_of l ++ map_of r =
krauss@26192
   806
    map_of s ++
krauss@26192
   807
    (\<lambda>a. if a < k then map_of l a
krauss@26192
   808
    else if k < a then map_of r a else Some v)" (is "?m1 = ?m2")
krauss@26192
   809
  proof (rule ext)
krauss@26192
   810
    fix a
krauss@26192
   811
krauss@26192
   812
   have "k < a \<or> k = a \<or> k > a" by auto
krauss@26192
   813
    thus "?m1 a = ?m2 a"
krauss@26192
   814
    proof (elim disjE)
krauss@26192
   815
      assume "k < a"
krauss@26192
   816
      with `l |\<guillemotleft> k` have "l |\<guillemotleft> a" by (rule tlt_trans)
krauss@26192
   817
      with `k < a` show ?thesis
krauss@26192
   818
        by (auto simp: map_add_def split: option.splits)
krauss@26192
   819
    next
krauss@26192
   820
      assume "k = a"
krauss@26192
   821
      with `l |\<guillemotleft> k` `k \<guillemotleft>| r` 
krauss@26192
   822
      show ?thesis by (auto simp: map_add_def)
krauss@26192
   823
    next
krauss@26192
   824
      assume "a < k"
krauss@26192
   825
      from this `k \<guillemotleft>| r` have "a \<guillemotleft>| r" by (rule tgt_trans)
krauss@26192
   826
      with `a < k` show ?thesis
krauss@26192
   827
        by (auto simp: map_add_def split: option.splits)
krauss@26192
   828
    qed
krauss@26192
   829
  qed
krauss@26192
   830
krauss@26192
   831
  from Tr
krauss@26192
   832
  have IHs:
krauss@26192
   833
    "map_of (union (union (insrt k v s) l) r) = map_of (union (insrt k v s) l) ++ map_of r"
krauss@26192
   834
    "map_of (union (insrt k v s) l) = map_of (insrt k v s) ++ map_of l"
krauss@26192
   835
    by (auto intro: union_isrbt insrt_isrbt)
krauss@26192
   836
  
krauss@26192
   837
  with meq show ?case
krauss@26192
   838
    by (auto simp: map_of_insert[OF Tr(3)])
krauss@26192
   839
qed
krauss@26192
   840
krauss@26192
   841
subsection {* Adjust *}
krauss@26192
   842
krauss@26192
   843
primrec
krauss@26192
   844
  adjustwithkey :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> ('a\<Colon>linorder) \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt"
krauss@26192
   845
where
krauss@26192
   846
  "adjustwithkey f k Empty = Empty"
krauss@26192
   847
| "adjustwithkey f k (Tr c lt x v rt) = (if k < x then (Tr c (adjustwithkey f k lt) x v rt) else if k > x then (Tr c lt x v (adjustwithkey f k rt)) else (Tr c lt x (f x v) rt))"
krauss@26192
   848
krauss@26192
   849
lemma adjustwk_treec: "treec (adjustwithkey f k t) = treec t" by (induct t) simp+
krauss@26192
   850
lemma adjustwk_inv1: "inv1 (adjustwithkey f k t) = inv1 t" by (induct t) (simp add: adjustwk_treec)+
krauss@26192
   851
lemma adjustwk_inv2: "inv2 (adjustwithkey f k t) = inv2 t" "bh (adjustwithkey f k t) = bh t" by (induct t) simp+
krauss@26192
   852
lemma adjustwk_tgt: "tgt k (adjustwithkey f kk t) = tgt k t" by (induct t) simp+
krauss@26192
   853
lemma adjustwk_tlt: "tlt k (adjustwithkey f kk t) = tlt k t" by (induct t) simp+
krauss@26192
   854
lemma adjustwk_st: "st (adjustwithkey f k t) = st t" by (induct t) (simp add: adjustwk_tlt adjustwk_tgt)+
krauss@26192
   855
krauss@26192
   856
theorem adjustwk_isrbt[simp]: "isrbt (adjustwithkey f k t) = isrbt t" 
krauss@26192
   857
unfolding isrbt_def by (simp add: adjustwk_inv2 adjustwk_treec adjustwk_st adjustwk_inv1 )
krauss@26192
   858
krauss@26192
   859
theorem adjustwithkey_map[simp]:
krauss@26192
   860
  "map_of (adjustwithkey f k t) x = 
krauss@26192
   861
  (if x = k then case map_of t x of None \<Rightarrow> None | Some y \<Rightarrow> Some (f k y)
krauss@26192
   862
            else map_of t x)"
krauss@26192
   863
by (induct t arbitrary: x) (auto split:option.splits)
krauss@26192
   864
krauss@26192
   865
definition adjust where
krauss@26192
   866
  "adjust f = adjustwithkey (\<lambda>_. f)"
krauss@26192
   867
krauss@26192
   868
theorem adjust_isrbt[simp]: "isrbt (adjust f k t) = isrbt t" unfolding adjust_def by simp
krauss@26192
   869
krauss@26192
   870
theorem adjust_map[simp]:
krauss@26192
   871
  "map_of (adjust f k t) x = 
krauss@26192
   872
  (if x = k then case map_of t x of None \<Rightarrow> None | Some y \<Rightarrow> Some (f y)
krauss@26192
   873
            else map_of t x)"
krauss@26192
   874
unfolding adjust_def by simp
krauss@26192
   875
krauss@26192
   876
subsection {* Map *}
krauss@26192
   877
krauss@26192
   878
primrec
krauss@26192
   879
  mapwithkey :: "('a::linorder \<Rightarrow> 'b \<Rightarrow> 'c) \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'c) rbt"
krauss@26192
   880
where
krauss@26192
   881
  "mapwithkey f Empty = Empty"
krauss@26192
   882
| "mapwithkey f (Tr c lt k v rt) = Tr c (mapwithkey f lt) k (f k v) (mapwithkey f rt)"
krauss@26192
   883
krauss@26192
   884
theorem mapwk_keys[simp]: "keys (mapwithkey f t) = keys t" by (induct t) auto
krauss@26192
   885
lemma mapwk_tgt: "tgt k (mapwithkey f t) = tgt k t" by (induct t) simp+
krauss@26192
   886
lemma mapwk_tlt: "tlt k (mapwithkey f t) = tlt k t" by (induct t) simp+
krauss@26192
   887
lemma mapwk_st: "st (mapwithkey f t) = st t"  by (induct t) (simp add: mapwk_tlt mapwk_tgt)+
krauss@26192
   888
lemma mapwk_treec: "treec (mapwithkey f t) = treec t" by (induct t) simp+
krauss@26192
   889
lemma mapwk_inv1: "inv1 (mapwithkey f t) = inv1 t" by (induct t) (simp add: mapwk_treec)+
krauss@26192
   890
lemma mapwk_inv2: "inv2 (mapwithkey f t) = inv2 t" "bh (mapwithkey f t) = bh t" by (induct t) simp+
krauss@26192
   891
theorem mapwk_isrbt[simp]: "isrbt (mapwithkey f t) = isrbt t" 
krauss@26192
   892
unfolding isrbt_def by (simp add: mapwk_inv1 mapwk_inv2 mapwk_st mapwk_treec)
krauss@26192
   893
nipkow@30235
   894
theorem map_of_mapwk[simp]: "map_of (mapwithkey f t) x = Option.map (f x) (map_of t x)"
krauss@26192
   895
by (induct t) auto
krauss@26192
   896
krauss@26192
   897
definition map
krauss@26192
   898
where map_def: "map f == mapwithkey (\<lambda>_. f)"
krauss@26192
   899
krauss@26192
   900
theorem map_keys[simp]: "keys (map f t) = keys t" unfolding map_def by simp
krauss@26192
   901
theorem map_isrbt[simp]: "isrbt (map f t) = isrbt t" unfolding map_def by simp
nipkow@30235
   902
theorem map_of_map[simp]: "map_of (map f t) = Option.map f o map_of t"
krauss@26192
   903
  by (rule ext) (simp add:map_def)
krauss@26192
   904
krauss@26192
   905
subsection {* Fold *}
krauss@26192
   906
krauss@26192
   907
text {* The following is still incomplete... *}
krauss@26192
   908
krauss@26192
   909
primrec
krauss@26192
   910
  foldwithkey :: "('a::linorder \<Rightarrow> 'b \<Rightarrow> 'c \<Rightarrow> 'c) \<Rightarrow> ('a,'b) rbt \<Rightarrow> 'c \<Rightarrow> 'c"
krauss@26192
   911
where
krauss@26192
   912
  "foldwithkey f Empty v = v"
krauss@26192
   913
| "foldwithkey f (Tr c lt k x rt) v = foldwithkey f rt (f k x (foldwithkey f lt v))"
krauss@26192
   914
krauss@26192
   915
primrec alist_of
krauss@26192
   916
where 
krauss@26192
   917
  "alist_of Empty = []"
krauss@26192
   918
| "alist_of (Tr _ l k v r) = alist_of l @ (k,v) # alist_of r"
krauss@26192
   919
krauss@26192
   920
lemma map_of_alist_of:
krauss@26192
   921
  shows "st t \<Longrightarrow> Map.map_of (alist_of t) = map_of t"
krauss@26192
   922
  oops
krauss@26192
   923
krauss@26192
   924
lemma fold_alist_fold:
krauss@26192
   925
  "foldwithkey f t x = foldl (\<lambda>x (k,v). f k v x) x (alist_of t)"
krauss@26192
   926
by (induct t arbitrary: x) auto
krauss@26192
   927
krauss@26192
   928
lemma alist_pit[simp]: "(k, v) \<in> set (alist_of t) = pin_tree k v t"
krauss@26192
   929
by (induct t) auto
krauss@26192
   930
krauss@26192
   931
lemma sorted_alist:
krauss@26192
   932
  "st t \<Longrightarrow> sorted (List.map fst (alist_of t))"
krauss@26192
   933
by (induct t) 
krauss@26192
   934
  (force simp: sorted_append sorted_Cons tlgt_props 
krauss@26192
   935
      dest!:pint_keys)+
krauss@26192
   936
krauss@26192
   937
lemma distinct_alist:
krauss@26192
   938
  "st t \<Longrightarrow> distinct (List.map fst (alist_of t))"
krauss@26192
   939
by (induct t) 
krauss@26192
   940
  (force simp: sorted_append sorted_Cons tlgt_props 
krauss@26192
   941
      dest!:pint_keys)+
krauss@26192
   942
(*>*)
krauss@26192
   943
krauss@26192
   944
text {* 
krauss@26192
   945
  This theory defines purely functional red-black trees which can be
krauss@26192
   946
  used as an efficient representation of finite maps.
krauss@26192
   947
*}
krauss@26192
   948
krauss@26192
   949
subsection {* Data type and invariant *}
krauss@26192
   950
krauss@26192
   951
text {*
krauss@26192
   952
  The type @{typ "('k, 'v) rbt"} denotes red-black trees with keys of
krauss@26192
   953
  type @{typ "'k"} and values of type @{typ "'v"}. To function
krauss@26192
   954
  properly, the key type must belong to the @{text "linorder"} class.
krauss@26192
   955
krauss@26192
   956
  A value @{term t} of this type is a valid red-black tree if it
krauss@26192
   957
  satisfies the invariant @{text "isrbt t"}.
krauss@26192
   958
  This theory provides lemmas to prove that the invariant is
krauss@26192
   959
  satisfied throughout the computation.
krauss@26192
   960
krauss@26192
   961
  The interpretation function @{const "map_of"} returns the partial
krauss@26192
   962
  map represented by a red-black tree:
krauss@26192
   963
  @{term_type[display] "map_of"}
krauss@26192
   964
krauss@26192
   965
  This function should be used for reasoning about the semantics of the RBT
krauss@26192
   966
  operations. Furthermore, it implements the lookup functionality for
krauss@26192
   967
  the data structure: It is executable and the lookup is performed in
krauss@26192
   968
  $O(\log n)$.  
krauss@26192
   969
*}
krauss@26192
   970
krauss@26192
   971
subsection {* Operations *}
krauss@26192
   972
krauss@26192
   973
text {*
krauss@26192
   974
  Currently, the following operations are supported:
krauss@26192
   975
krauss@26192
   976
  @{term_type[display] "Empty"}
krauss@26192
   977
  Returns the empty tree. $O(1)$
krauss@26192
   978
krauss@26192
   979
  @{term_type[display] "insrt"}
krauss@26192
   980
  Updates the map at a given position. $O(\log n)$
krauss@26192
   981
krauss@26192
   982
  @{term_type[display] "delete"}
krauss@26192
   983
  Deletes a map entry at a given position. $O(\log n)$
krauss@26192
   984
krauss@26192
   985
  @{term_type[display] "union"}
krauss@26192
   986
  Forms the union of two trees, preferring entries from the first one.
krauss@26192
   987
krauss@26192
   988
  @{term_type[display] "map"}
krauss@26192
   989
  Maps a function over the values of a map. $O(n)$
krauss@26192
   990
*}
krauss@26192
   991
krauss@26192
   992
krauss@26192
   993
subsection {* Invariant preservation *}
krauss@26192
   994
krauss@26192
   995
text {*
krauss@26192
   996
  \noindent
krauss@26192
   997
  @{thm Empty_isrbt}\hfill(@{text "Empty_isrbt"})
krauss@26192
   998
krauss@26192
   999
  \noindent
krauss@26192
  1000
  @{thm insrt_isrbt}\hfill(@{text "insrt_isrbt"})
krauss@26192
  1001
krauss@26192
  1002
  \noindent
krauss@26192
  1003
  @{thm delete_isrbt}\hfill(@{text "delete_isrbt"})
krauss@26192
  1004
krauss@26192
  1005
  \noindent
krauss@26192
  1006
  @{thm union_isrbt}\hfill(@{text "union_isrbt"})
krauss@26192
  1007
krauss@26192
  1008
  \noindent
krauss@26192
  1009
  @{thm map_isrbt}\hfill(@{text "map_isrbt"})
krauss@26192
  1010
*}
krauss@26192
  1011
krauss@26192
  1012
subsection {* Map Semantics *}
krauss@26192
  1013
krauss@26192
  1014
text {*
krauss@26192
  1015
  \noindent
krauss@26192
  1016
  \underline{@{text "map_of_Empty"}}
krauss@26192
  1017
  @{thm[display] map_of_Empty}
krauss@26192
  1018
  \vspace{1ex}
krauss@26192
  1019
krauss@26192
  1020
  \noindent
krauss@26192
  1021
  \underline{@{text "map_of_insert"}}
krauss@26192
  1022
  @{thm[display] map_of_insert}
krauss@26192
  1023
  \vspace{1ex}
krauss@26192
  1024
krauss@26192
  1025
  \noindent
krauss@26192
  1026
  \underline{@{text "map_of_delete"}}
krauss@26192
  1027
  @{thm[display] map_of_delete}
krauss@26192
  1028
  \vspace{1ex}
krauss@26192
  1029
krauss@26192
  1030
  \noindent
krauss@26192
  1031
  \underline{@{text "map_of_union"}}
krauss@26192
  1032
  @{thm[display] map_of_union}
krauss@26192
  1033
  \vspace{1ex}
krauss@26192
  1034
krauss@26192
  1035
  \noindent
krauss@26192
  1036
  \underline{@{text "map_of_map"}}
krauss@26192
  1037
  @{thm[display] map_of_map}
krauss@26192
  1038
  \vspace{1ex}
krauss@26192
  1039
*}
krauss@26192
  1040
krauss@26192
  1041
end