tuned def. of del and proved preservation of rbt (finally)
authornipkow
Mon Jun 11 20:45:51 2018 +0200 (11 months ago)
changeset 68415d74ba11680d4
parent 68414 b001bef9aa39
child 68419 a1f43b4f984d
child 68422 0a3a36fa1d63
tuned def. of del and proved preservation of rbt (finally)
src/HOL/Data_Structures/RBT_Map.thy
     1.1 --- a/src/HOL/Data_Structures/RBT_Map.thy	Mon Jun 11 16:29:38 2018 +0200
     1.2 +++ b/src/HOL/Data_Structures/RBT_Map.thy	Mon Jun 11 20:45:51 2018 +0200
     1.3 @@ -22,19 +22,14 @@
     1.4  definition update :: "'a::linorder \<Rightarrow> 'b \<Rightarrow> ('a*'b) rbt \<Rightarrow> ('a*'b) rbt" where
     1.5  "update x y t = paint Black (upd x y t)"
     1.6  
     1.7 -fun del :: "'a::linorder \<Rightarrow> ('a*'b)rbt \<Rightarrow> ('a*'b)rbt"
     1.8 -and delL :: "'a::linorder \<Rightarrow> ('a*'b)rbt \<Rightarrow> 'a*'b \<Rightarrow> ('a*'b)rbt \<Rightarrow> ('a*'b)rbt"
     1.9 -and delR :: "'a::linorder \<Rightarrow> ('a*'b)rbt \<Rightarrow> 'a*'b \<Rightarrow> ('a*'b)rbt \<Rightarrow> ('a*'b)rbt"
    1.10 -where
    1.11 +fun del :: "'a::linorder \<Rightarrow> ('a*'b)rbt \<Rightarrow> ('a*'b)rbt" where
    1.12  "del x Leaf = Leaf" |
    1.13 -"del x (Node t1 (a,b) c t2) = (case cmp x a of
    1.14 -  LT \<Rightarrow> delL x t1 (a,b) t2 |
    1.15 -  GT \<Rightarrow> delR x t1 (a,b) t2 |
    1.16 -  EQ \<Rightarrow> combine t1 t2)" |
    1.17 -"delL x (B t1 a t2) b t3 = baldL (del x (B t1 a t2)) b t3" |
    1.18 -"delL x t1 a t2 = R (del x t1) a t2" |
    1.19 -"delR x t1 a (B t2 b t3) = baldR t1 a (del x (B t2 b t3))" | 
    1.20 -"delR x t1 a t2 = R t1 a (del x t2)"
    1.21 +"del x (Node l (a,b) c r) = (case cmp x a of
    1.22 +     LT \<Rightarrow> if l \<noteq> Leaf \<and> color l = Black
    1.23 +           then baldL (del x l) (a,b) r else R (del x l) (a,b) r |
    1.24 +     GT \<Rightarrow> if r \<noteq> Leaf\<and> color r = Black
    1.25 +           then baldR l (a,b) (del x r) else R l (a,b) (del x r) |
    1.26 +  EQ \<Rightarrow> combine l r)"
    1.27  
    1.28  definition delete :: "'a::linorder \<Rightarrow> ('a*'b) rbt \<Rightarrow> ('a*'b) rbt" where
    1.29  "delete x t = paint Black (del x t)"
    1.30 @@ -52,21 +47,66 @@
    1.31  by(simp add: update_def inorder_upd inorder_paint)
    1.32  
    1.33  lemma inorder_del:
    1.34 - "sorted1(inorder t1) \<Longrightarrow>  inorder(del x t1) = del_list x (inorder t1)" and
    1.35 - "sorted1(inorder t1) \<Longrightarrow>  inorder(delL x t1 a t2) =
    1.36 -    del_list x (inorder t1) @ a # inorder t2" and
    1.37 - "sorted1(inorder t2) \<Longrightarrow>  inorder(delR x t1 a t2) =
    1.38 -    inorder t1 @ a # del_list x (inorder t2)"
    1.39 -by(induction x t1 and x t1 a t2 and x t1 a t2 rule: del_delL_delR.induct)
    1.40 + "sorted1(inorder t) \<Longrightarrow>  inorder(del x t) = del_list x (inorder t)"
    1.41 +by(induction x t rule: del.induct)
    1.42    (auto simp: del_list_simps inorder_combine inorder_baldL inorder_baldR)
    1.43  
    1.44  lemma inorder_delete:
    1.45    "sorted1(inorder t) \<Longrightarrow> inorder(delete x t) = del_list x (inorder t)"
    1.46  by(simp add: delete_def inorder_del inorder_paint)
    1.47  
    1.48 +
    1.49 +subsection \<open>Structural invariants\<close>
    1.50 +
    1.51 +subsubsection \<open>Update\<close>
    1.52 +
    1.53 +lemma invc_upd: assumes "invc t"
    1.54 +  shows "color t = Black \<Longrightarrow> invc (upd x y t)" "invc2 (upd x y t)"
    1.55 +using assms
    1.56 +by (induct x y t rule: upd.induct) (auto simp: invc_baliL invc_baliR invc2I)
    1.57 +
    1.58 +lemma invh_upd: assumes "invh t"
    1.59 +  shows "invh (upd x y t)" "bheight (upd x y t) = bheight t"
    1.60 +using assms
    1.61 +by(induct x y t rule: upd.induct)
    1.62 +  (auto simp: invh_baliL invh_baliR bheight_baliL bheight_baliR)
    1.63 +
    1.64 +theorem rbt_update: "rbt t \<Longrightarrow> rbt (update x y t)"
    1.65 +by (simp add: invc_upd(2) invh_upd(1) color_paint_Black invc_paint_Black invh_paint
    1.66 +  rbt_def update_def)
    1.67 +
    1.68 +
    1.69 +subsubsection \<open>Deletion\<close>
    1.70 +
    1.71 +lemma del_invc_invh: "invh t \<Longrightarrow> invc t \<Longrightarrow> invh (del x t) \<and>
    1.72 +   (color t = Red \<and> bheight (del x t) = bheight t \<and> invc (del x t) \<or>
    1.73 +    color t = Black \<and> bheight (del x t) = bheight t - 1 \<and> invc2 (del x t))"
    1.74 +proof (induct x t rule: del.induct)
    1.75 +case (2 x _ y _ c)
    1.76 +  have "x = y \<or> x < y \<or> x > y" by auto
    1.77 +  thus ?case proof (elim disjE)
    1.78 +    assume "x = y"
    1.79 +    with 2 show ?thesis
    1.80 +    by (cases c) (simp_all add: invh_combine invc_combine)
    1.81 +  next
    1.82 +    assume "x < y"
    1.83 +    with 2 show ?thesis
    1.84 +      by(cases c)
    1.85 +        (auto simp: invh_baldL_invc invc_baldL invc2_baldL dest: neq_LeafD)
    1.86 +  next
    1.87 +    assume "y < x"
    1.88 +    with 2 show ?thesis
    1.89 +      by(cases c)
    1.90 +        (auto simp: invh_baldR_invc invc_baldR invc2_baldR dest: neq_LeafD)
    1.91 +  qed
    1.92 +qed auto
    1.93 +
    1.94 +theorem rbt_delete: "rbt t \<Longrightarrow> rbt (delete k t)"
    1.95 +by (metis delete_def rbt_def color_paint_Black del_invc_invh invc_paint_Black invc2I invh_paint)
    1.96 +
    1.97  interpretation Map_by_Ordered
    1.98  where empty = Leaf and lookup = lookup and update = update and delete = delete
    1.99 -and inorder = inorder and inv = "\<lambda>_. True"
   1.100 +and inorder = inorder and inv = rbt
   1.101  proof (standard, goal_cases)
   1.102    case 1 show ?case by simp
   1.103  next
   1.104 @@ -75,6 +115,12 @@
   1.105    case 3 thus ?case by(simp add: inorder_update)
   1.106  next
   1.107    case 4 thus ?case by(simp add: inorder_delete)
   1.108 -qed auto
   1.109 +next
   1.110 +  case 5 thus ?case by (simp add: rbt_Leaf) 
   1.111 +next
   1.112 +  case 6 thus ?case by (simp add: rbt_update) 
   1.113 +next
   1.114 +  case 7 thus ?case by (simp add: rbt_delete) 
   1.115 +qed
   1.116  
   1.117  end