src/HOL/Data_Structures/RBT_Set.thy
author nipkow
Sun Nov 15 12:45:28 2015 +0100 (2015-11-15)
changeset 61678 b594e9277be3
parent 61588 1d2907d0ed73
child 61749 7f530d7e552d
permissions -rw-r--r--
tuned white space
     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 insert :: "'a::cmp \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt" where
    13 "insert x Leaf = R Leaf x Leaf" |
    14 "insert x (B l a r) =
    15   (case cmp x a of
    16      LT \<Rightarrow> bal (insert x l) a r |
    17      GT \<Rightarrow> bal l a (insert x r) |
    18      EQ \<Rightarrow> B l a r)" |
    19 "insert x (R l a r) =
    20   (case cmp x a of
    21     LT \<Rightarrow> R (insert x l) a r |
    22     GT \<Rightarrow> R l a (insert x r) |
    23     EQ \<Rightarrow> R l a r)"
    24 
    25 fun delete :: "'a::cmp \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt"
    26 and deleteL :: "'a::cmp \<Rightarrow> 'a rbt \<Rightarrow> 'a \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt"
    27 and deleteR :: "'a::cmp \<Rightarrow> 'a rbt \<Rightarrow> 'a \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt"
    28 where
    29 "delete x Leaf = Leaf" |
    30 "delete x (Node _ l a r) =
    31   (case cmp x a of
    32      LT \<Rightarrow> deleteL x l a r |
    33      GT \<Rightarrow> deleteR x l a r |
    34      EQ \<Rightarrow> combine l r)" |
    35 "deleteL x (B t1 a t2) b t3 = balL (delete x (B t1 a t2)) b t3" |
    36 "deleteL x l a r = R (delete x l) a r" |
    37 "deleteR x t1 a (B t2 b t3) = balR t1 a (delete x (B t2 b t3))" | 
    38 "deleteR x l a r = R l a (delete x r)"
    39 
    40 
    41 subsection "Functional Correctness Proofs"
    42 
    43 lemma inorder_bal:
    44   "inorder(bal l a r) = inorder l @ a # inorder r"
    45 by(induction l a r rule: bal.induct) (auto)
    46 
    47 lemma inorder_insert:
    48   "sorted(inorder t) \<Longrightarrow> inorder(insert a t) = ins_list a (inorder t)"
    49 by(induction a t rule: insert.induct) (auto simp: ins_list_simps inorder_bal)
    50 
    51 lemma inorder_red: "inorder(red t) = inorder t"
    52 by(induction t) (auto)
    53 
    54 lemma inorder_balL:
    55   "inorder(balL l a r) = inorder l @ a # inorder r"
    56 by(induction l a r rule: balL.induct)(auto simp: inorder_bal inorder_red)
    57 
    58 lemma inorder_balR:
    59   "inorder(balR l a r) = inorder l @ a # inorder r"
    60 by(induction l a r rule: balR.induct) (auto simp: inorder_bal inorder_red)
    61 
    62 lemma inorder_combine:
    63   "inorder(combine l r) = inorder l @ inorder r"
    64 by(induction l r rule: combine.induct)
    65   (auto simp: inorder_balL inorder_balR split: tree.split color.split)
    66 
    67 lemma inorder_delete:
    68  "sorted(inorder t) \<Longrightarrow>  inorder(delete x t) = del_list x (inorder t)"
    69  "sorted(inorder l) \<Longrightarrow>  inorder(deleteL x l a r) =
    70     del_list x (inorder l) @ a # inorder r"
    71  "sorted(inorder r) \<Longrightarrow>  inorder(deleteR x l a r) =
    72     inorder l @ a # del_list x (inorder r)"
    73 by(induction x t and x l a r and x l a r rule: delete_deleteL_deleteR.induct)
    74   (auto simp: del_list_simps inorder_combine inorder_balL inorder_balR)
    75 
    76 
    77 interpretation Set_by_Ordered
    78 where empty = Leaf and isin = isin and insert = insert and delete = delete
    79 and inorder = inorder and inv = "\<lambda>_. True"
    80 proof (standard, goal_cases)
    81   case 1 show ?case by simp
    82 next
    83   case 2 thus ?case by(simp add: isin_set)
    84 next
    85   case 3 thus ?case by(simp add: inorder_insert)
    86 next
    87   case 4 thus ?case by(simp add: inorder_delete(1))
    88 qed (rule TrueI)+
    89 
    90 end