src/HOL/Data_Structures/RBT_Set.thy
changeset 61224 759b5299a9f2
child 61231 cc6969542f8d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Data_Structures/RBT_Set.thy	Tue Sep 22 08:38:25 2015 +0200
     1.3 @@ -0,0 +1,86 @@
     1.4 +(* Author: Tobias Nipkow *)
     1.5 +
     1.6 +section \<open>Red-Black Tree Implementation of Sets\<close>
     1.7 +
     1.8 +theory RBT_Set
     1.9 +imports
    1.10 +  RBT
    1.11 +  Isin2
    1.12 +begin
    1.13 +
    1.14 +fun insert :: "'a::linorder \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt" where
    1.15 +"insert x Leaf = R Leaf x Leaf" |
    1.16 +"insert x (B l a r) =
    1.17 +  (if x < a then bal (insert x l) a r else
    1.18 +   if x > a then bal l a (insert x r) else B l a r)" |
    1.19 +"insert x (R l a r) =
    1.20 +  (if x < a then R (insert x l) a r
    1.21 +   else if x > a then R l a (insert x r) else R l a r)"
    1.22 +
    1.23 +fun delete :: "'a::linorder \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt"
    1.24 +and deleteL :: "'a::linorder \<Rightarrow> 'a rbt \<Rightarrow> 'a \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt"
    1.25 +and deleteR :: "'a::linorder \<Rightarrow> 'a rbt \<Rightarrow> 'a \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt"
    1.26 +where
    1.27 +"delete x Leaf = Leaf" |
    1.28 +"delete x (Node _ l a r) = 
    1.29 +  (if x < a then deleteL x l a r 
    1.30 +   else if x > a then deleteR x l a r else combine l r)" |
    1.31 +"deleteL x (B t1 a t2) b t3 = balL (delete x (B t1 a t2)) b t3" |
    1.32 +"deleteL x l a r = R (delete x l) a r" |
    1.33 +"deleteR x t1 a (B t2 b t3) = balR t1 a (delete x (B t2 b t3))" | 
    1.34 +"deleteR x l a r = R l a (delete x r)"
    1.35 +
    1.36 +
    1.37 +subsection "Functional Correctness Proofs"
    1.38 +
    1.39 +lemma inorder_bal:
    1.40 +  "inorder(bal l a r) = inorder l @ a # inorder r"
    1.41 +by(induction l a r rule: bal.induct) (auto simp: sorted_lems)
    1.42 +
    1.43 +lemma inorder_insert:
    1.44 +  "sorted(inorder t) \<Longrightarrow> inorder(insert a t) = ins_list a (inorder t)"
    1.45 +by(induction a t rule: insert.induct) (auto simp: ins_simps inorder_bal)
    1.46 +
    1.47 +lemma inorder_red: "inorder(red t) = inorder t"
    1.48 +by(induction t) (auto simp: sorted_lems)
    1.49 +
    1.50 +lemma inorder_balL:
    1.51 +  "inorder(balL l a r) = inorder l @ a # inorder r"
    1.52 +by(induction l a r rule: balL.induct)
    1.53 +  (auto simp: sorted_lems inorder_bal inorder_red)
    1.54 +
    1.55 +lemma inorder_balR:
    1.56 +  "inorder(balR l a r) = inorder l @ a # inorder r"
    1.57 +by(induction l a r rule: balR.induct)
    1.58 +  (auto simp: sorted_lems inorder_bal inorder_red)
    1.59 +
    1.60 +lemma inorder_combine:
    1.61 +  "inorder(combine l r) = inorder l @ inorder r"
    1.62 +by(induction l r rule: combine.induct)
    1.63 +  (auto simp: sorted_lems inorder_balL inorder_balR split: tree.split color.split)
    1.64 +
    1.65 +lemma inorder_delete:
    1.66 + "sorted(inorder t) \<Longrightarrow>  inorder(delete x t) = del_list x (inorder t)" and
    1.67 + "sorted(inorder l) \<Longrightarrow>  inorder(deleteL x l a r) =
    1.68 +    del_list x (inorder l) @ a # inorder r" and
    1.69 + "sorted(inorder r) \<Longrightarrow>  inorder(deleteR x l a r) =
    1.70 +    inorder l @ a # del_list x (inorder r)"
    1.71 +by(induction x t and x l a r and x l a r rule: delete_deleteL_deleteR.induct)
    1.72 +  (auto simp: del_simps inorder_combine inorder_balL inorder_balR)
    1.73 +
    1.74 +interpretation Set_by_Ordered
    1.75 +where empty = Leaf and isin = isin and insert = insert and delete = delete
    1.76 +and inorder = inorder and wf = "\<lambda>_. True"
    1.77 +proof (standard, goal_cases)
    1.78 +  case 1 show ?case by simp
    1.79 +next
    1.80 +  case 2 thus ?case by(simp add: isin_set)
    1.81 +next
    1.82 +  case 3 thus ?case by(simp add: inorder_insert)
    1.83 +next
    1.84 +  case 4 thus ?case by(simp add: inorder_delete)
    1.85 +next
    1.86 +  case 5 thus ?case ..
    1.87 +qed
    1.88 +
    1.89 +end