src/HOL/Data_Structures/RBT_Set.thy
author nipkow
Fri Nov 27 18:01:13 2015 +0100 (2015-11-27)
changeset 61749 7f530d7e552d
parent 61678 b594e9277be3
child 61754 862daa8144f3
permissions -rw-r--r--
paint root black after insert and delete
nipkow@61224
     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@61224
     7
  RBT
nipkow@61581
     8
  Cmp
nipkow@61224
     9
  Isin2
nipkow@61224
    10
begin
nipkow@61224
    11
nipkow@61749
    12
fun ins :: "'a::cmp \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt" where
nipkow@61749
    13
"ins x Leaf = R Leaf x Leaf" |
nipkow@61749
    14
"ins x (B l a r) =
nipkow@61678
    15
  (case cmp x a of
nipkow@61749
    16
     LT \<Rightarrow> bal (ins x l) a r |
nipkow@61749
    17
     GT \<Rightarrow> bal l a (ins x r) |
nipkow@61678
    18
     EQ \<Rightarrow> B l a r)" |
nipkow@61749
    19
"ins x (R l a r) =
nipkow@61678
    20
  (case cmp x a of
nipkow@61749
    21
    LT \<Rightarrow> R (ins x l) a r |
nipkow@61749
    22
    GT \<Rightarrow> R l a (ins x r) |
nipkow@61678
    23
    EQ \<Rightarrow> R l a r)"
nipkow@61224
    24
nipkow@61749
    25
definition insert :: "'a::cmp \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt" where
nipkow@61749
    26
"insert x t = paint Black (ins x t)"
nipkow@61749
    27
nipkow@61749
    28
fun del :: "'a::cmp \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt"
nipkow@61749
    29
and delL :: "'a::cmp \<Rightarrow> 'a rbt \<Rightarrow> 'a \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt"
nipkow@61749
    30
and delR :: "'a::cmp \<Rightarrow> 'a rbt \<Rightarrow> 'a \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt"
nipkow@61224
    31
where
nipkow@61749
    32
"del x Leaf = Leaf" |
nipkow@61749
    33
"del x (Node _ l a r) =
nipkow@61678
    34
  (case cmp x a of
nipkow@61749
    35
     LT \<Rightarrow> delL x l a r |
nipkow@61749
    36
     GT \<Rightarrow> delR x l a r |
nipkow@61678
    37
     EQ \<Rightarrow> combine l r)" |
nipkow@61749
    38
"delL x (B t1 a t2) b t3 = balL (del x (B t1 a t2)) b t3" |
nipkow@61749
    39
"delL x l a r = R (del x l) a r" |
nipkow@61749
    40
"delR x t1 a (B t2 b t3) = balR t1 a (del x (B t2 b t3))" | 
nipkow@61749
    41
"delR x l a r = R l a (del x r)"
nipkow@61749
    42
nipkow@61749
    43
definition delete :: "'a::cmp \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt" where
nipkow@61749
    44
"delete x t = paint Black (del x t)"
nipkow@61224
    45
nipkow@61224
    46
nipkow@61224
    47
subsection "Functional Correctness Proofs"
nipkow@61224
    48
nipkow@61749
    49
lemma inorder_paint: "inorder(paint c t) = inorder t"
nipkow@61749
    50
by(induction t) (auto)
nipkow@61749
    51
nipkow@61224
    52
lemma inorder_bal:
nipkow@61224
    53
  "inorder(bal l a r) = inorder l @ a # inorder r"
nipkow@61231
    54
by(induction l a r rule: bal.induct) (auto)
nipkow@61224
    55
nipkow@61749
    56
lemma inorder_ins:
nipkow@61749
    57
  "sorted(inorder t) \<Longrightarrow> inorder(ins x t) = ins_list x (inorder t)"
nipkow@61749
    58
by(induction x t rule: ins.induct) (auto simp: ins_list_simps inorder_bal)
nipkow@61749
    59
nipkow@61224
    60
lemma inorder_insert:
nipkow@61749
    61
  "sorted(inorder t) \<Longrightarrow> inorder(insert x t) = ins_list x (inorder t)"
nipkow@61749
    62
by (simp add: insert_def inorder_ins inorder_paint)
nipkow@61224
    63
nipkow@61224
    64
lemma inorder_balL:
nipkow@61224
    65
  "inorder(balL l a r) = inorder l @ a # inorder r"
nipkow@61749
    66
by(induction l a r rule: balL.induct)(auto simp: inorder_bal inorder_paint)
nipkow@61224
    67
nipkow@61224
    68
lemma inorder_balR:
nipkow@61224
    69
  "inorder(balR l a r) = inorder l @ a # inorder r"
nipkow@61749
    70
by(induction l a r rule: balR.induct) (auto simp: inorder_bal inorder_paint)
nipkow@61224
    71
nipkow@61224
    72
lemma inorder_combine:
nipkow@61224
    73
  "inorder(combine l r) = inorder l @ inorder r"
nipkow@61224
    74
by(induction l r rule: combine.induct)
nipkow@61231
    75
  (auto simp: inorder_balL inorder_balR split: tree.split color.split)
nipkow@61224
    76
nipkow@61749
    77
lemma inorder_del:
nipkow@61749
    78
 "sorted(inorder t) \<Longrightarrow>  inorder(del x t) = del_list x (inorder t)"
nipkow@61749
    79
 "sorted(inorder l) \<Longrightarrow>  inorder(delL x l a r) =
nipkow@61678
    80
    del_list x (inorder l) @ a # inorder r"
nipkow@61749
    81
 "sorted(inorder r) \<Longrightarrow>  inorder(delR x l a r) =
nipkow@61224
    82
    inorder l @ a # del_list x (inorder r)"
nipkow@61749
    83
by(induction x t and x l a r and x l a r rule: del_delL_delR.induct)
nipkow@61231
    84
  (auto simp: del_list_simps inorder_combine inorder_balL inorder_balR)
nipkow@61224
    85
nipkow@61749
    86
lemma inorder_delete:
nipkow@61749
    87
  "sorted(inorder t) \<Longrightarrow> inorder(delete x t) = del_list x (inorder t)"
nipkow@61749
    88
by (auto simp: delete_def inorder_del inorder_paint)
nipkow@61749
    89
nipkow@61581
    90
nipkow@61224
    91
interpretation Set_by_Ordered
nipkow@61224
    92
where empty = Leaf and isin = isin and insert = insert and delete = delete
nipkow@61588
    93
and inorder = inorder and inv = "\<lambda>_. True"
nipkow@61224
    94
proof (standard, goal_cases)
nipkow@61224
    95
  case 1 show ?case by simp
nipkow@61224
    96
next
nipkow@61224
    97
  case 2 thus ?case by(simp add: isin_set)
nipkow@61224
    98
next
nipkow@61224
    99
  case 3 thus ?case by(simp add: inorder_insert)
nipkow@61224
   100
next
nipkow@61749
   101
  case 4 thus ?case by(simp add: inorder_delete)
nipkow@61749
   102
qed auto
nipkow@61224
   103
nipkow@61224
   104
end