src/HOL/Data_Structures/RBT_Set.thy
author nipkow
Fri Jan 27 17:28:10 2017 +0100 (2017-01-27)
changeset 64952 f11e974b47e0
parent 64951 140addd19343
child 64953 f9cfb10761ff
permissions -rw-r--r--
removed unclear clause; slower but clearer
nipkow@64951
     1
(* Author: Tobias Nipkow *)
nipkow@61224
     2
nipkow@61224
     3
section \<open>Red-Black Tree Implementation of Sets\<close>
nipkow@61224
     4
nipkow@61224
     5
theory RBT_Set
nipkow@61224
     6
imports
nipkow@64950
     7
  Complex_Main
nipkow@61224
     8
  RBT
nipkow@61581
     9
  Cmp
nipkow@61224
    10
  Isin2
nipkow@61224
    11
begin
nipkow@61224
    12
nipkow@63411
    13
fun ins :: "'a::linorder \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt" where
nipkow@61749
    14
"ins x Leaf = R Leaf x Leaf" |
nipkow@61749
    15
"ins x (B l a r) =
nipkow@61678
    16
  (case cmp x a of
nipkow@61749
    17
     LT \<Rightarrow> bal (ins x l) a r |
nipkow@61749
    18
     GT \<Rightarrow> bal l a (ins x r) |
nipkow@61678
    19
     EQ \<Rightarrow> B l a r)" |
nipkow@61749
    20
"ins x (R l a r) =
nipkow@61678
    21
  (case cmp x a of
nipkow@61749
    22
    LT \<Rightarrow> R (ins x l) a r |
nipkow@61749
    23
    GT \<Rightarrow> R l a (ins x r) |
nipkow@61678
    24
    EQ \<Rightarrow> R l a r)"
nipkow@61224
    25
nipkow@63411
    26
definition insert :: "'a::linorder \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt" where
nipkow@61749
    27
"insert x t = paint Black (ins x t)"
nipkow@61749
    28
nipkow@63411
    29
fun del :: "'a::linorder \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt"
nipkow@63411
    30
and delL :: "'a::linorder \<Rightarrow> 'a rbt \<Rightarrow> 'a \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt"
nipkow@63411
    31
and delR :: "'a::linorder \<Rightarrow> 'a rbt \<Rightarrow> 'a \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt"
nipkow@61224
    32
where
nipkow@61749
    33
"del x Leaf = Leaf" |
nipkow@61749
    34
"del x (Node _ l a r) =
nipkow@61678
    35
  (case cmp x a of
nipkow@61749
    36
     LT \<Rightarrow> delL x l a r |
nipkow@61749
    37
     GT \<Rightarrow> delR x l a r |
nipkow@61678
    38
     EQ \<Rightarrow> combine l r)" |
nipkow@61749
    39
"delL x (B t1 a t2) b t3 = balL (del x (B t1 a t2)) b t3" |
nipkow@61749
    40
"delL x l a r = R (del x l) a r" |
nipkow@61749
    41
"delR x t1 a (B t2 b t3) = balR t1 a (del x (B t2 b t3))" | 
nipkow@61749
    42
"delR x l a r = R l a (del x r)"
nipkow@61749
    43
nipkow@63411
    44
definition delete :: "'a::linorder \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt" where
nipkow@61749
    45
"delete x t = paint Black (del x t)"
nipkow@61224
    46
nipkow@61224
    47
nipkow@61224
    48
subsection "Functional Correctness Proofs"
nipkow@61224
    49
nipkow@61749
    50
lemma inorder_paint: "inorder(paint c t) = inorder t"
nipkow@62526
    51
by(cases t) (auto)
nipkow@61749
    52
nipkow@61224
    53
lemma inorder_bal:
nipkow@61224
    54
  "inorder(bal l a r) = inorder l @ a # inorder r"
nipkow@62526
    55
by(cases "(l,a,r)" rule: bal.cases) (auto)
nipkow@61224
    56
nipkow@61749
    57
lemma inorder_ins:
nipkow@61749
    58
  "sorted(inorder t) \<Longrightarrow> inorder(ins x t) = ins_list x (inorder t)"
nipkow@61749
    59
by(induction x t rule: ins.induct) (auto simp: ins_list_simps inorder_bal)
nipkow@61749
    60
nipkow@61224
    61
lemma inorder_insert:
nipkow@61749
    62
  "sorted(inorder t) \<Longrightarrow> inorder(insert x t) = ins_list x (inorder t)"
nipkow@61749
    63
by (simp add: insert_def inorder_ins inorder_paint)
nipkow@61224
    64
nipkow@61224
    65
lemma inorder_balL:
nipkow@61224
    66
  "inorder(balL l a r) = inorder l @ a # inorder r"
nipkow@62526
    67
by(cases "(l,a,r)" rule: balL.cases)(auto simp: inorder_bal inorder_paint)
nipkow@61224
    68
nipkow@61224
    69
lemma inorder_balR:
nipkow@61224
    70
  "inorder(balR l a r) = inorder l @ a # inorder r"
nipkow@62526
    71
by(cases "(l,a,r)" rule: balR.cases) (auto simp: inorder_bal inorder_paint)
nipkow@61224
    72
nipkow@61224
    73
lemma inorder_combine:
nipkow@61224
    74
  "inorder(combine l r) = inorder l @ inorder r"
nipkow@61224
    75
by(induction l r rule: combine.induct)
nipkow@61231
    76
  (auto simp: inorder_balL inorder_balR split: tree.split color.split)
nipkow@61224
    77
nipkow@61749
    78
lemma inorder_del:
nipkow@61749
    79
 "sorted(inorder t) \<Longrightarrow>  inorder(del x t) = del_list x (inorder t)"
nipkow@61749
    80
 "sorted(inorder l) \<Longrightarrow>  inorder(delL x l a r) =
nipkow@61678
    81
    del_list x (inorder l) @ a # inorder r"
nipkow@61749
    82
 "sorted(inorder r) \<Longrightarrow>  inorder(delR x l a r) =
nipkow@61224
    83
    inorder l @ a # del_list x (inorder r)"
nipkow@61749
    84
by(induction x t and x l a r and x l a r rule: del_delL_delR.induct)
nipkow@61231
    85
  (auto simp: del_list_simps inorder_combine inorder_balL inorder_balR)
nipkow@61224
    86
nipkow@61749
    87
lemma inorder_delete:
nipkow@61749
    88
  "sorted(inorder t) \<Longrightarrow> inorder(delete x t) = del_list x (inorder t)"
nipkow@61749
    89
by (auto simp: delete_def inorder_del inorder_paint)
nipkow@61749
    90
nipkow@61581
    91
nipkow@63411
    92
subsection \<open>Structural invariants\<close>
nipkow@61224
    93
nipkow@64952
    94
text\<open>The proofs are due to Markus Reiter and Alexander Krauss.\<close>
nipkow@61754
    95
nipkow@61754
    96
fun color :: "'a rbt \<Rightarrow> color" where
nipkow@61754
    97
"color Leaf = Black" |
nipkow@61754
    98
"color (Node c _ _ _) = c"
nipkow@61754
    99
nipkow@61754
   100
fun bheight :: "'a rbt \<Rightarrow> nat" where
nipkow@61754
   101
"bheight Leaf = 0" |
nipkow@64951
   102
"bheight (Node c l x r) = (if c = Black then bheight l + 1 else bheight l)"
nipkow@61754
   103
nipkow@63411
   104
fun invc :: "'a rbt \<Rightarrow> bool" where
nipkow@63411
   105
"invc Leaf = True" |
nipkow@63411
   106
"invc (Node c l a r) =
nipkow@64947
   107
  (invc l \<and> invc r \<and> (c = Red \<longrightarrow> color l = Black \<and> color r = Black))"
nipkow@61754
   108
nipkow@63411
   109
fun invc_sons :: "'a rbt \<Rightarrow> bool" \<comment> \<open>Weaker version\<close> where
nipkow@63411
   110
"invc_sons Leaf = True" |
nipkow@63411
   111
"invc_sons (Node c l a r) = (invc l \<and> invc r)"
nipkow@61754
   112
nipkow@63411
   113
fun invh :: "'a rbt \<Rightarrow> bool" where
nipkow@63411
   114
"invh Leaf = True" |
nipkow@63411
   115
"invh (Node c l x r) = (invh l \<and> invh r \<and> bheight l = bheight r)"
nipkow@61754
   116
nipkow@63411
   117
lemma invc_sonsI: "invc t \<Longrightarrow> invc_sons t"
nipkow@61754
   118
by (cases t) simp+
nipkow@61754
   119
nipkow@61754
   120
definition rbt :: "'a rbt \<Rightarrow> bool" where
nipkow@63411
   121
"rbt t = (invc t \<and> invh t \<and> color t = Black)"
nipkow@61754
   122
nipkow@61754
   123
lemma color_paint_Black: "color (paint Black t) = Black"
nipkow@61754
   124
by (cases t) auto
nipkow@61754
   125
nipkow@61754
   126
theorem rbt_Leaf: "rbt Leaf"
nipkow@61754
   127
by (simp add: rbt_def)
nipkow@61754
   128
nipkow@63411
   129
lemma paint_invc_sons: "invc_sons t \<Longrightarrow> invc_sons (paint c t)"
nipkow@61754
   130
by (cases t) auto
nipkow@61754
   131
nipkow@63411
   132
lemma invc_paint_Black: "invc_sons t \<Longrightarrow> invc (paint Black t)"
nipkow@61754
   133
by (cases t) auto
nipkow@61754
   134
nipkow@63411
   135
lemma invh_paint: "invh t \<Longrightarrow> invh (paint c t)"
nipkow@61754
   136
by (cases t) auto
nipkow@61754
   137
nipkow@64952
   138
lemma invc_bal:
nipkow@64952
   139
  "\<lbrakk>invc l \<and> invc_sons r \<or> invc_sons l \<and> invc r\<rbrakk> \<Longrightarrow> invc (bal l a r)" 
nipkow@61754
   140
by (induct l a r rule: bal.induct) auto
nipkow@61754
   141
nipkow@61754
   142
lemma bheight_bal:
nipkow@61754
   143
  "bheight l = bheight r \<Longrightarrow> bheight (bal l a r) = Suc (bheight l)"
nipkow@61754
   144
by (induct l a r rule: bal.induct) auto
nipkow@61754
   145
nipkow@63411
   146
lemma invh_bal: 
nipkow@63411
   147
  "\<lbrakk> invh l; invh r; bheight l = bheight r \<rbrakk> \<Longrightarrow> invh (bal l a r)"
nipkow@61754
   148
by (induct l a r rule: bal.induct) auto
nipkow@61754
   149
nipkow@61754
   150
nipkow@61754
   151
subsubsection \<open>Insertion\<close>
nipkow@61754
   152
nipkow@63411
   153
lemma invc_ins: assumes "invc t"
nipkow@63411
   154
  shows "color t = Black \<Longrightarrow> invc (ins x t)" "invc_sons (ins x t)"
nipkow@61754
   155
using assms
nipkow@63411
   156
by (induct x t rule: ins.induct) (auto simp: invc_bal invc_sonsI)
nipkow@61754
   157
nipkow@63411
   158
lemma invh_ins: assumes "invh t"
nipkow@63411
   159
  shows "invh (ins x t)" "bheight (ins x t) = bheight t"
nipkow@61754
   160
using assms
nipkow@63411
   161
by (induct x t rule: ins.induct) (auto simp: invh_bal bheight_bal)
nipkow@61754
   162
nipkow@63411
   163
theorem rbt_insert: "rbt t \<Longrightarrow> rbt (insert x t)"
nipkow@63411
   164
by (simp add: invc_ins invh_ins color_paint_Black invc_paint_Black invh_paint
nipkow@61754
   165
  rbt_def insert_def)
nipkow@61754
   166
nipkow@63411
   167
nipkow@63411
   168
subsubsection \<open>Deletion\<close>
nipkow@63411
   169
nipkow@63411
   170
lemma bheight_paint_Red:
nipkow@63411
   171
  "color t = Black \<Longrightarrow> bheight (paint Red t) = bheight t - 1"
nipkow@61754
   172
by (cases t) auto
nipkow@61754
   173
nipkow@63411
   174
lemma balL_invh_with_invc:
nipkow@63411
   175
  assumes "invh lt" "invh rt" "bheight lt + 1 = bheight rt" "invc rt"
nipkow@63411
   176
  shows "bheight (balL lt a rt) = bheight lt + 1"  "invh (balL lt a rt)"
nipkow@61754
   177
using assms 
nipkow@63411
   178
by (induct lt a rt rule: balL.induct)
nipkow@63411
   179
   (auto simp: invh_bal invh_paint bheight_bal bheight_paint_Red)
nipkow@61754
   180
nipkow@63411
   181
lemma balL_invh_app: 
nipkow@63411
   182
  assumes "invh lt" "invh rt" "bheight lt + 1 = bheight rt" "color rt = Black"
nipkow@63411
   183
  shows "invh (balL lt a rt)" 
nipkow@61754
   184
        "bheight (balL lt a rt) = bheight rt"
nipkow@61754
   185
using assms 
nipkow@63411
   186
by (induct lt a rt rule: balL.induct) (auto simp add: invh_bal bheight_bal) 
nipkow@61754
   187
nipkow@63411
   188
lemma balL_invc: "\<lbrakk>invc_sons l; invc r; color r = Black\<rbrakk> \<Longrightarrow> invc (balL l a r)"
nipkow@63411
   189
by (induct l a r rule: balL.induct) (simp_all add: invc_bal)
nipkow@61754
   190
nipkow@63411
   191
lemma balL_invc_sons: "\<lbrakk> invc_sons lt; invc rt \<rbrakk> \<Longrightarrow> invc_sons (balL lt a rt)"
nipkow@63411
   192
by (induct lt a rt rule: balL.induct) (auto simp: invc_bal paint_invc_sons invc_sonsI)
nipkow@61754
   193
nipkow@63411
   194
lemma balR_invh_with_invc:
nipkow@63411
   195
  assumes "invh lt" "invh rt" "bheight lt = bheight rt + 1" "invc lt"
nipkow@63411
   196
  shows "invh (balR lt a rt) \<and> bheight (balR lt a rt) = bheight lt"
nipkow@61754
   197
using assms
nipkow@63411
   198
by(induct lt a rt rule: balR.induct)
nipkow@63411
   199
  (auto simp: invh_bal bheight_bal invh_paint bheight_paint_Red)
nipkow@61754
   200
nipkow@63411
   201
lemma invc_balR: "\<lbrakk>invc a; invc_sons b; color a = Black\<rbrakk> \<Longrightarrow> invc (balR a x b)"
nipkow@63411
   202
by (induct a x b rule: balR.induct) (simp_all add: invc_bal)
nipkow@61754
   203
nipkow@63411
   204
lemma invc_sons_balR: "\<lbrakk> invc lt; invc_sons rt \<rbrakk> \<Longrightarrow>invc_sons (balR lt x rt)"
nipkow@63411
   205
by (induct lt x rt rule: balR.induct) (auto simp: invc_bal paint_invc_sons invc_sonsI)
nipkow@61754
   206
nipkow@63411
   207
lemma invh_combine:
nipkow@63411
   208
  assumes "invh lt" "invh rt" "bheight lt = bheight rt"
nipkow@63411
   209
  shows "bheight (combine lt rt) = bheight lt" "invh (combine lt rt)"
nipkow@61754
   210
using assms 
nipkow@61754
   211
by (induct lt rt rule: combine.induct) 
nipkow@63411
   212
   (auto simp: balL_invh_app split: tree.splits color.splits)
nipkow@61754
   213
nipkow@63411
   214
lemma invc_combine: 
nipkow@63411
   215
  assumes "invc lt" "invc rt"
nipkow@63411
   216
  shows "color lt = Black \<Longrightarrow> color rt = Black \<Longrightarrow> invc (combine lt rt)"
nipkow@63411
   217
         "invc_sons (combine lt rt)"
nipkow@61754
   218
using assms 
nipkow@61754
   219
by (induct lt rt rule: combine.induct)
nipkow@63411
   220
   (auto simp: balL_invc invc_sonsI split: tree.splits color.splits)
nipkow@61754
   221
nipkow@61754
   222
nipkow@63411
   223
lemma assumes "invh lt" "invc lt"
nipkow@61754
   224
  shows
nipkow@63411
   225
  del_invc_invh: "invh (del x lt) \<and> (color lt = Red \<and> bheight (del x lt) = bheight lt \<and> invc (del x lt) 
nipkow@63411
   226
  \<or> color lt = Black \<and> bheight (del x lt) = bheight lt - 1 \<and> invc_sons (del x lt))"
nipkow@63411
   227
and  "\<lbrakk>invh rt; bheight lt = bheight rt; invc rt\<rbrakk> \<Longrightarrow>
nipkow@63411
   228
   invh (delL x lt k rt) \<and> 
nipkow@63411
   229
   bheight (delL x lt k rt) = bheight lt \<and> 
nipkow@63411
   230
   (color lt = Black \<and> color rt = Black \<and> invc (delL x lt k rt) \<or> 
nipkow@63411
   231
    (color lt \<noteq> Black \<or> color rt \<noteq> Black) \<and> invc_sons (delL x lt k rt))"
nipkow@63411
   232
  and "\<lbrakk>invh rt; bheight lt = bheight rt; invc rt\<rbrakk> \<Longrightarrow>
nipkow@63411
   233
  invh (delR x lt k rt) \<and> 
nipkow@63411
   234
  bheight (delR x lt k rt) = bheight lt \<and> 
nipkow@63411
   235
  (color lt = Black \<and> color rt = Black \<and> invc (delR x lt k rt) \<or> 
nipkow@63411
   236
   (color lt \<noteq> Black \<or> color rt \<noteq> Black) \<and> invc_sons (delR x lt k rt))"
nipkow@61754
   237
using assms
nipkow@63411
   238
proof (induct x lt and x lt k rt and x lt k rt rule: del_delL_delR.induct)
nipkow@61754
   239
case (2 y c _ y')
nipkow@61754
   240
  have "y = y' \<or> y < y' \<or> y > y'" by auto
nipkow@61754
   241
  thus ?case proof (elim disjE)
nipkow@61754
   242
    assume "y = y'"
nipkow@63411
   243
    with 2 show ?thesis
nipkow@63411
   244
    by (cases c) (simp_all add: invh_combine invc_combine)
nipkow@61754
   245
  next
nipkow@61754
   246
    assume "y < y'"
nipkow@63411
   247
    with 2 show ?thesis by (cases c) (auto simp: invc_sonsI)
nipkow@61754
   248
  next
nipkow@61754
   249
    assume "y' < y"
nipkow@63411
   250
    with 2 show ?thesis by (cases c) (auto simp: invc_sonsI)
nipkow@61754
   251
  qed
nipkow@61754
   252
next
nipkow@63411
   253
  case (3 y lt z rta y' bb)
nipkow@63411
   254
  thus ?case by (cases "color (Node Black lt z rta) = Black \<and> color bb = Black") (simp add: balL_invh_with_invc balL_invc balL_invc_sons)+
nipkow@61754
   255
next
nipkow@63411
   256
  case (5 y a y' lt z rta)
nipkow@63411
   257
  thus ?case by (cases "color a = Black \<and> color (Node Black lt z rta) = Black") (simp add: balR_invh_with_invc invc_balR invc_sons_balR)+
nipkow@61754
   258
next
nipkow@63411
   259
  case ("6_1" y a y') thus ?case by (cases "color a = Black \<and> color Leaf = Black") simp+
nipkow@61754
   260
qed auto
nipkow@61754
   261
nipkow@63411
   262
theorem rbt_delete: "rbt t \<Longrightarrow> rbt (delete k t)"
nipkow@63411
   263
by (metis delete_def rbt_def color_paint_Black del_invc_invh invc_paint_Black invc_sonsI invh_paint)
nipkow@63411
   264
nipkow@63411
   265
text \<open>Overall correctness:\<close>
nipkow@63411
   266
nipkow@63411
   267
interpretation Set_by_Ordered
nipkow@63411
   268
where empty = Leaf and isin = isin and insert = insert and delete = delete
nipkow@63411
   269
and inorder = inorder and inv = rbt
nipkow@63411
   270
proof (standard, goal_cases)
nipkow@63411
   271
  case 1 show ?case by simp
nipkow@63411
   272
next
nipkow@63411
   273
  case 2 thus ?case by(simp add: isin_set)
nipkow@63411
   274
next
nipkow@63411
   275
  case 3 thus ?case by(simp add: inorder_insert)
nipkow@63411
   276
next
nipkow@63411
   277
  case 4 thus ?case by(simp add: inorder_delete)
nipkow@63411
   278
next
nipkow@63411
   279
  case 5 thus ?case by (simp add: rbt_Leaf) 
nipkow@63411
   280
next
nipkow@63411
   281
  case 6 thus ?case by (simp add: rbt_insert) 
nipkow@63411
   282
next
nipkow@63411
   283
  case 7 thus ?case by (simp add: rbt_delete) 
nipkow@63411
   284
qed
nipkow@63411
   285
nipkow@63411
   286
nipkow@63411
   287
subsection \<open>Height-Size Relation\<close>
nipkow@63411
   288
nipkow@64950
   289
lemma neq_Black[simp]: "(c \<noteq> Black) = (c = Red)"
nipkow@64950
   290
by (cases c) auto
nipkow@64950
   291
nipkow@64950
   292
lemma rbt_height_bheight_if_nat: "invc t \<Longrightarrow> invh t \<Longrightarrow>
nipkow@64950
   293
  height t \<le> (if color t = Black then 2 * bheight t else 2 * bheight t + 1)"
nipkow@64950
   294
by(induction t) (auto split: if_split_asm)
nipkow@64950
   295
nipkow@64950
   296
lemma rbt_height_bheight_if: "invc t \<Longrightarrow> invh t \<Longrightarrow>
nipkow@64950
   297
  (if color t = Black then height t / 2 else (height t - 1) / 2) \<le> bheight t"
nipkow@64950
   298
by(induction t) (auto split: if_split_asm)
nipkow@64950
   299
nipkow@64950
   300
lemma rbt_height_bheight: "rbt t \<Longrightarrow> height t / 2 \<le> bheight t "
nipkow@64950
   301
by(auto simp: rbt_def dest: rbt_height_bheight_if)
nipkow@64950
   302
nipkow@64950
   303
lemma bheight_size_bound:  "invc t \<Longrightarrow> invh t \<Longrightarrow> size1 t \<ge>  2 ^ (bheight t)"
nipkow@64950
   304
by (induction t) auto
nipkow@64950
   305
nipkow@64950
   306
lemma rbt_height_le: assumes "rbt t" shows "height t \<le> 2 * log 2 (size1 t)"
nipkow@64950
   307
proof -
nipkow@64950
   308
  have "2 powr (height t / 2) \<le> 2 powr bheight t"
nipkow@64950
   309
    using rbt_height_bheight[OF assms] by (simp)
nipkow@64950
   310
  also have "\<dots> \<le> size1 t" using assms
nipkow@64950
   311
    by (simp add: powr_realpow bheight_size_bound rbt_def)
nipkow@64950
   312
  finally have "2 powr (height t / 2) \<le> size1 t" .
nipkow@64950
   313
  hence "height t / 2 \<le> log 2 (size1 t)"
nipkow@64950
   314
    by(simp add: le_log_iff size1_def del: Int.divide_le_eq_numeral1(1))
nipkow@64950
   315
  thus ?thesis by simp
nipkow@64950
   316
qed
nipkow@64950
   317
nipkow@61224
   318
end