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