paint root black after insert and delete
authornipkow
Fri Nov 27 18:01:13 2015 +0100 (2015-11-27)
changeset 617497f530d7e552d
parent 61748 fc53fbf9fe01
child 61752 814bbe5d9204
paint root black after insert and delete
src/HOL/Data_Structures/RBT.thy
src/HOL/Data_Structures/RBT_Map.thy
src/HOL/Data_Structures/RBT_Set.thy
     1.1 --- a/src/HOL/Data_Structures/RBT.thy	Wed Nov 25 15:58:22 2015 +0100
     1.2 +++ b/src/HOL/Data_Structures/RBT.thy	Fri Nov 27 18:01:13 2015 +0100
     1.3 @@ -22,20 +22,20 @@
     1.4  "bal t1 a1 (R (R t2 a2 t3) a3 t4) = R (B t1 a1 t2) a2 (B t3 a3 t4)" |
     1.5  "bal t1 a t2 = B t1 a t2"
     1.6  
     1.7 -fun red :: "'a rbt \<Rightarrow> 'a rbt" where
     1.8 -"red Leaf = Leaf" |
     1.9 -"red (Node _ l a r) = R l a r"
    1.10 +fun paint :: "color \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt" where
    1.11 +"paint c Leaf = Leaf" |
    1.12 +"paint c (Node _ l a r) = Node c l a r"
    1.13  
    1.14  fun balL :: "'a rbt \<Rightarrow> 'a \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt" where
    1.15  "balL (R t1 x t2) y t3 = R (B t1 x t2) y t3" |
    1.16  "balL bl x (B t1 y t2) = bal bl x (R t1 y t2)" |
    1.17 -"balL bl x (R (B t1 y t2) z t3) = R (B bl x t1) y (bal t2 z (red t3))" |
    1.18 +"balL bl x (R (B t1 y t2) z t3) = R (B bl x t1) y (bal t2 z (paint Red t3))" |
    1.19  "balL t1 x t2 = R t1 x t2"
    1.20  
    1.21  fun balR :: "'a rbt \<Rightarrow> 'a \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt" where
    1.22  "balR t1 x (R t2 y t3) = R t1 x (B t2 y t3)" |
    1.23  "balR (B t1 x t2) y t3 = bal (R t1 x t2) y t3" |
    1.24 -"balR (R t1 x (B t2 y t3)) z t4 = R (bal (red t1) x t2) y (B t3 z t4)" |
    1.25 +"balR (R t1 x (B t2 y t3)) z t4 = R (bal (paint Red t1) x t2) y (B t3 z t4)" |
    1.26  "balR t1 x t2 = R t1 x t2"
    1.27  
    1.28  fun combine :: "'a rbt \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt" where
     2.1 --- a/src/HOL/Data_Structures/RBT_Map.thy	Wed Nov 25 15:58:22 2015 +0100
     2.2 +++ b/src/HOL/Data_Structures/RBT_Map.thy	Fri Nov 27 18:01:13 2015 +0100
     2.3 @@ -8,48 +8,61 @@
     2.4    Lookup2
     2.5  begin
     2.6  
     2.7 -fun update :: "'a::cmp \<Rightarrow> 'b \<Rightarrow> ('a*'b) rbt \<Rightarrow> ('a*'b) rbt" where
     2.8 -"update x y Leaf = R Leaf (x,y) Leaf" |
     2.9 -"update x y (B l (a,b) r) = (case cmp x a of
    2.10 -  LT \<Rightarrow> bal (update x y l) (a,b) r |
    2.11 -  GT \<Rightarrow> bal l (a,b) (update x y r) |
    2.12 +fun upd :: "'a::cmp \<Rightarrow> 'b \<Rightarrow> ('a*'b) rbt \<Rightarrow> ('a*'b) rbt" where
    2.13 +"upd x y Leaf = R Leaf (x,y) Leaf" |
    2.14 +"upd x y (B l (a,b) r) = (case cmp x a of
    2.15 +  LT \<Rightarrow> bal (upd x y l) (a,b) r |
    2.16 +  GT \<Rightarrow> bal l (a,b) (upd x y r) |
    2.17    EQ \<Rightarrow> B l (x,y) r)" |
    2.18 -"update x y (R l (a,b) r) = (case cmp x a of
    2.19 -  LT \<Rightarrow> R (update x y l) (a,b) r |
    2.20 -  GT \<Rightarrow> R l (a,b) (update x y r) |
    2.21 +"upd x y (R l (a,b) r) = (case cmp x a of
    2.22 +  LT \<Rightarrow> R (upd x y l) (a,b) r |
    2.23 +  GT \<Rightarrow> R l (a,b) (upd x y r) |
    2.24    EQ \<Rightarrow> R l (x,y) r)"
    2.25  
    2.26 -fun delete :: "'a::cmp \<Rightarrow> ('a*'b)rbt \<Rightarrow> ('a*'b)rbt"
    2.27 -and deleteL :: "'a::cmp \<Rightarrow> ('a*'b)rbt \<Rightarrow> 'a*'b \<Rightarrow> ('a*'b)rbt \<Rightarrow> ('a*'b)rbt"
    2.28 -and deleteR :: "'a::cmp \<Rightarrow> ('a*'b)rbt \<Rightarrow> 'a*'b \<Rightarrow> ('a*'b)rbt \<Rightarrow> ('a*'b)rbt"
    2.29 +definition update :: "'a::cmp \<Rightarrow> 'b \<Rightarrow> ('a*'b) rbt \<Rightarrow> ('a*'b) rbt" where
    2.30 +"update x y t = paint Black (upd x y t)"
    2.31 +
    2.32 +fun del :: "'a::cmp \<Rightarrow> ('a*'b)rbt \<Rightarrow> ('a*'b)rbt"
    2.33 +and delL :: "'a::cmp \<Rightarrow> ('a*'b)rbt \<Rightarrow> 'a*'b \<Rightarrow> ('a*'b)rbt \<Rightarrow> ('a*'b)rbt"
    2.34 +and delR :: "'a::cmp \<Rightarrow> ('a*'b)rbt \<Rightarrow> 'a*'b \<Rightarrow> ('a*'b)rbt \<Rightarrow> ('a*'b)rbt"
    2.35  where
    2.36 -"delete x Leaf = Leaf" |
    2.37 -"delete x (Node c t1 (a,b) t2) = (case cmp x a of
    2.38 -  LT \<Rightarrow> deleteL x t1 (a,b) t2 |
    2.39 -  GT \<Rightarrow> deleteR x t1 (a,b) t2 |
    2.40 +"del x Leaf = Leaf" |
    2.41 +"del x (Node c t1 (a,b) t2) = (case cmp x a of
    2.42 +  LT \<Rightarrow> delL x t1 (a,b) t2 |
    2.43 +  GT \<Rightarrow> delR x t1 (a,b) t2 |
    2.44    EQ \<Rightarrow> combine t1 t2)" |
    2.45 -"deleteL x (B t1 a t2) b t3 = balL (delete x (B t1 a t2)) b t3" |
    2.46 -"deleteL x t1 a t2 = R (delete x t1) a t2" |
    2.47 -"deleteR x t1 a (B t2 b t3) = balR t1 a (delete x (B t2 b t3))" | 
    2.48 -"deleteR x t1 a t2 = R t1 a (delete x t2)"
    2.49 +"delL x (B t1 a t2) b t3 = balL (del x (B t1 a t2)) b t3" |
    2.50 +"delL x t1 a t2 = R (del x t1) a t2" |
    2.51 +"delR x t1 a (B t2 b t3) = balR t1 a (del x (B t2 b t3))" | 
    2.52 +"delR x t1 a t2 = R t1 a (del x t2)"
    2.53 +
    2.54 +definition delete :: "'a::cmp \<Rightarrow> ('a*'b) rbt \<Rightarrow> ('a*'b) rbt" where
    2.55 +"delete x t = paint Black (del x t)"
    2.56  
    2.57  
    2.58  subsection "Functional Correctness Proofs"
    2.59  
    2.60 +lemma inorder_upd:
    2.61 +  "sorted1(inorder t) \<Longrightarrow> inorder(upd x y t) = upd_list x y (inorder t)"
    2.62 +by(induction x y t rule: upd.induct)
    2.63 +  (auto simp: upd_list_simps inorder_bal)
    2.64 +
    2.65  lemma inorder_update:
    2.66    "sorted1(inorder t) \<Longrightarrow> inorder(update x y t) = upd_list x y (inorder t)"
    2.67 -by(induction x y t rule: update.induct)
    2.68 -  (auto simp: upd_list_simps inorder_bal)
    2.69 +by(simp add: update_def inorder_upd inorder_paint)
    2.70  
    2.71 +lemma inorder_del:
    2.72 + "sorted1(inorder t1) \<Longrightarrow>  inorder(del x t1) = del_list x (inorder t1)" and
    2.73 + "sorted1(inorder t1) \<Longrightarrow>  inorder(delL x t1 a t2) =
    2.74 +    del_list x (inorder t1) @ a # inorder t2" and
    2.75 + "sorted1(inorder t2) \<Longrightarrow>  inorder(delR x t1 a t2) =
    2.76 +    inorder t1 @ a # del_list x (inorder t2)"
    2.77 +by(induction x t1 and x t1 a t2 and x t1 a t2 rule: del_delL_delR.induct)
    2.78 +  (auto simp: del_list_simps inorder_combine inorder_balL inorder_balR)
    2.79  
    2.80  lemma inorder_delete:
    2.81 - "sorted1(inorder t1) \<Longrightarrow>  inorder(delete x t1) = del_list x (inorder t1)" and
    2.82 - "sorted1(inorder t1) \<Longrightarrow>  inorder(deleteL x t1 a t2) =
    2.83 -    del_list x (inorder t1) @ a # inorder t2" and
    2.84 - "sorted1(inorder t2) \<Longrightarrow>  inorder(deleteR x t1 a t2) =
    2.85 -    inorder t1 @ a # del_list x (inorder t2)"
    2.86 -by(induction x t1 and x t1 a t2 and x t1 a t2 rule: delete_deleteL_deleteR.induct)
    2.87 -  (auto simp: del_list_simps inorder_combine inorder_balL inorder_balR)
    2.88 +  "sorted1(inorder t) \<Longrightarrow> inorder(delete x t) = del_list x (inorder t)"
    2.89 +by(simp add: delete_def inorder_del inorder_paint)
    2.90  
    2.91  interpretation Map_by_Ordered
    2.92  where empty = Leaf and lookup = lookup and update = update and delete = delete
     3.1 --- a/src/HOL/Data_Structures/RBT_Set.thy	Wed Nov 25 15:58:22 2015 +0100
     3.2 +++ b/src/HOL/Data_Structures/RBT_Set.thy	Fri Nov 27 18:01:13 2015 +0100
     3.3 @@ -9,70 +9,84 @@
     3.4    Isin2
     3.5  begin
     3.6  
     3.7 -fun insert :: "'a::cmp \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt" where
     3.8 -"insert x Leaf = R Leaf x Leaf" |
     3.9 -"insert x (B l a r) =
    3.10 +fun ins :: "'a::cmp \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt" where
    3.11 +"ins x Leaf = R Leaf x Leaf" |
    3.12 +"ins x (B l a r) =
    3.13    (case cmp x a of
    3.14 -     LT \<Rightarrow> bal (insert x l) a r |
    3.15 -     GT \<Rightarrow> bal l a (insert x r) |
    3.16 +     LT \<Rightarrow> bal (ins x l) a r |
    3.17 +     GT \<Rightarrow> bal l a (ins x r) |
    3.18       EQ \<Rightarrow> B l a r)" |
    3.19 -"insert x (R l a r) =
    3.20 +"ins x (R l a r) =
    3.21    (case cmp x a of
    3.22 -    LT \<Rightarrow> R (insert x l) a r |
    3.23 -    GT \<Rightarrow> R l a (insert x r) |
    3.24 +    LT \<Rightarrow> R (ins x l) a r |
    3.25 +    GT \<Rightarrow> R l a (ins x r) |
    3.26      EQ \<Rightarrow> R l a r)"
    3.27  
    3.28 -fun delete :: "'a::cmp \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt"
    3.29 -and deleteL :: "'a::cmp \<Rightarrow> 'a rbt \<Rightarrow> 'a \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt"
    3.30 -and deleteR :: "'a::cmp \<Rightarrow> 'a rbt \<Rightarrow> 'a \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt"
    3.31 +definition insert :: "'a::cmp \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt" where
    3.32 +"insert x t = paint Black (ins x t)"
    3.33 +
    3.34 +fun del :: "'a::cmp \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt"
    3.35 +and delL :: "'a::cmp \<Rightarrow> 'a rbt \<Rightarrow> 'a \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt"
    3.36 +and delR :: "'a::cmp \<Rightarrow> 'a rbt \<Rightarrow> 'a \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt"
    3.37  where
    3.38 -"delete x Leaf = Leaf" |
    3.39 -"delete x (Node _ l a r) =
    3.40 +"del x Leaf = Leaf" |
    3.41 +"del x (Node _ l a r) =
    3.42    (case cmp x a of
    3.43 -     LT \<Rightarrow> deleteL x l a r |
    3.44 -     GT \<Rightarrow> deleteR x l a r |
    3.45 +     LT \<Rightarrow> delL x l a r |
    3.46 +     GT \<Rightarrow> delR x l a r |
    3.47       EQ \<Rightarrow> combine l r)" |
    3.48 -"deleteL x (B t1 a t2) b t3 = balL (delete x (B t1 a t2)) b t3" |
    3.49 -"deleteL x l a r = R (delete x l) a r" |
    3.50 -"deleteR x t1 a (B t2 b t3) = balR t1 a (delete x (B t2 b t3))" | 
    3.51 -"deleteR x l a r = R l a (delete x r)"
    3.52 +"delL x (B t1 a t2) b t3 = balL (del x (B t1 a t2)) b t3" |
    3.53 +"delL x l a r = R (del x l) a r" |
    3.54 +"delR x t1 a (B t2 b t3) = balR t1 a (del x (B t2 b t3))" | 
    3.55 +"delR x l a r = R l a (del x r)"
    3.56 +
    3.57 +definition delete :: "'a::cmp \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt" where
    3.58 +"delete x t = paint Black (del x t)"
    3.59  
    3.60  
    3.61  subsection "Functional Correctness Proofs"
    3.62  
    3.63 +lemma inorder_paint: "inorder(paint c t) = inorder t"
    3.64 +by(induction t) (auto)
    3.65 +
    3.66  lemma inorder_bal:
    3.67    "inorder(bal l a r) = inorder l @ a # inorder r"
    3.68  by(induction l a r rule: bal.induct) (auto)
    3.69  
    3.70 +lemma inorder_ins:
    3.71 +  "sorted(inorder t) \<Longrightarrow> inorder(ins x t) = ins_list x (inorder t)"
    3.72 +by(induction x t rule: ins.induct) (auto simp: ins_list_simps inorder_bal)
    3.73 +
    3.74  lemma inorder_insert:
    3.75 -  "sorted(inorder t) \<Longrightarrow> inorder(insert a t) = ins_list a (inorder t)"
    3.76 -by(induction a t rule: insert.induct) (auto simp: ins_list_simps inorder_bal)
    3.77 -
    3.78 -lemma inorder_red: "inorder(red t) = inorder t"
    3.79 -by(induction t) (auto)
    3.80 +  "sorted(inorder t) \<Longrightarrow> inorder(insert x t) = ins_list x (inorder t)"
    3.81 +by (simp add: insert_def inorder_ins inorder_paint)
    3.82  
    3.83  lemma inorder_balL:
    3.84    "inorder(balL l a r) = inorder l @ a # inorder r"
    3.85 -by(induction l a r rule: balL.induct)(auto simp: inorder_bal inorder_red)
    3.86 +by(induction l a r rule: balL.induct)(auto simp: inorder_bal inorder_paint)
    3.87  
    3.88  lemma inorder_balR:
    3.89    "inorder(balR l a r) = inorder l @ a # inorder r"
    3.90 -by(induction l a r rule: balR.induct) (auto simp: inorder_bal inorder_red)
    3.91 +by(induction l a r rule: balR.induct) (auto simp: inorder_bal inorder_paint)
    3.92  
    3.93  lemma inorder_combine:
    3.94    "inorder(combine l r) = inorder l @ inorder r"
    3.95  by(induction l r rule: combine.induct)
    3.96    (auto simp: inorder_balL inorder_balR split: tree.split color.split)
    3.97  
    3.98 -lemma inorder_delete:
    3.99 - "sorted(inorder t) \<Longrightarrow>  inorder(delete x t) = del_list x (inorder t)"
   3.100 - "sorted(inorder l) \<Longrightarrow>  inorder(deleteL x l a r) =
   3.101 +lemma inorder_del:
   3.102 + "sorted(inorder t) \<Longrightarrow>  inorder(del x t) = del_list x (inorder t)"
   3.103 + "sorted(inorder l) \<Longrightarrow>  inorder(delL x l a r) =
   3.104      del_list x (inorder l) @ a # inorder r"
   3.105 - "sorted(inorder r) \<Longrightarrow>  inorder(deleteR x l a r) =
   3.106 + "sorted(inorder r) \<Longrightarrow>  inorder(delR x l a r) =
   3.107      inorder l @ a # del_list x (inorder r)"
   3.108 -by(induction x t and x l a r and x l a r rule: delete_deleteL_deleteR.induct)
   3.109 +by(induction x t and x l a r and x l a r rule: del_delL_delR.induct)
   3.110    (auto simp: del_list_simps inorder_combine inorder_balL inorder_balR)
   3.111  
   3.112 +lemma inorder_delete:
   3.113 +  "sorted(inorder t) \<Longrightarrow> inorder(delete x t) = del_list x (inorder t)"
   3.114 +by (auto simp: delete_def inorder_del inorder_paint)
   3.115 +
   3.116  
   3.117  interpretation Set_by_Ordered
   3.118  where empty = Leaf and isin = isin and insert = insert and delete = delete
   3.119 @@ -84,7 +98,7 @@
   3.120  next
   3.121    case 3 thus ?case by(simp add: inorder_insert)
   3.122  next
   3.123 -  case 4 thus ?case by(simp add: inorder_delete(1))
   3.124 -qed (rule TrueI)+
   3.125 +  case 4 thus ?case by(simp add: inorder_delete)
   3.126 +qed auto
   3.127  
   3.128  end