| 71352 |      1 | (* Author: Tobias Nipkow *)
 | 
|  |      2 | 
 | 
|  |      3 | section \<open>Alternative Deletion in Red-Black Trees\<close>
 | 
|  |      4 | 
 | 
|  |      5 | theory RBT_Set2
 | 
|  |      6 | imports RBT_Set
 | 
|  |      7 | begin
 | 
|  |      8 | 
 | 
| 71830 |      9 | text \<open>This is a conceptually simpler version of deletion. Instead of the tricky \<open>join\<close>
 | 
| 71352 |     10 | function this version follows the standard approach of replacing the deleted element
 | 
|  |     11 | (in function \<open>del\<close>) by the minimal element in its right subtree.\<close>
 | 
|  |     12 | 
 | 
|  |     13 | fun split_min :: "'a rbt \<Rightarrow> 'a \<times> 'a rbt" where
 | 
|  |     14 | "split_min (Node l (a, _) r) =
 | 
|  |     15 |   (if l = Leaf then (a,r)
 | 
|  |     16 |    else let (x,l') = split_min l
 | 
|  |     17 |         in (x, if color l = Black then baldL l' a r else R l' a r))"
 | 
|  |     18 | 
 | 
|  |     19 | fun del :: "'a::linorder \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt" where
 | 
|  |     20 | "del x Leaf = Leaf" |
 | 
|  |     21 | "del x (Node l (a, _) r) =
 | 
|  |     22 |   (case cmp x a of
 | 
|  |     23 |      LT \<Rightarrow> let l' = del x l in if l \<noteq> Leaf \<and> color l = Black
 | 
|  |     24 |            then baldL l' a r else R l' a r |
 | 
|  |     25 |      GT \<Rightarrow> let r' = del x r in if r \<noteq> Leaf \<and> color r = Black
 | 
|  |     26 |            then baldR l a r' else R l a r' |
 | 
|  |     27 |      EQ \<Rightarrow> if r = Leaf then l else let (a',r') = split_min r in
 | 
|  |     28 |            if color r = Black then baldR l a' r' else R l a' r')"
 | 
|  |     29 | 
 | 
|  |     30 | text \<open>The first two \<open>let\<close>s speed up the automatic proof of \<open>inv_del\<close> below.\<close>
 | 
|  |     31 | 
 | 
|  |     32 | definition delete :: "'a::linorder \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt" where
 | 
|  |     33 | "delete x t = paint Black (del x t)"
 | 
|  |     34 | 
 | 
|  |     35 | 
 | 
|  |     36 | subsection "Functional Correctness Proofs"
 | 
|  |     37 | 
 | 
| 71846 |     38 | declare Let_def[simp]
 | 
|  |     39 | 
 | 
| 71352 |     40 | lemma split_minD:
 | 
|  |     41 |   "split_min t = (x,t') \<Longrightarrow> t \<noteq> Leaf \<Longrightarrow> x # inorder t' = inorder t"
 | 
|  |     42 | by(induction t arbitrary: t' rule: split_min.induct)
 | 
|  |     43 |   (auto simp: inorder_baldL sorted_lems split: prod.splits if_splits)
 | 
|  |     44 | 
 | 
|  |     45 | lemma inorder_del:
 | 
|  |     46 |  "sorted(inorder t) \<Longrightarrow>  inorder(del x t) = del_list x (inorder t)"
 | 
|  |     47 | by(induction x t rule: del.induct)
 | 
| 71846 |     48 |   (auto simp: del_list_simps inorder_baldL inorder_baldR split_minD split: prod.splits)
 | 
| 71352 |     49 | 
 | 
|  |     50 | lemma inorder_delete:
 | 
|  |     51 |   "sorted(inorder t) \<Longrightarrow> inorder(delete x t) = del_list x (inorder t)"
 | 
|  |     52 | by (auto simp: delete_def inorder_del inorder_paint)
 | 
|  |     53 | 
 | 
|  |     54 | 
 | 
|  |     55 | subsection \<open>Structural invariants\<close>
 | 
|  |     56 | 
 | 
|  |     57 | lemma neq_Red[simp]: "(c \<noteq> Red) = (c = Black)"
 | 
|  |     58 | by (cases c) auto
 | 
|  |     59 | 
 | 
|  |     60 | 
 | 
|  |     61 | subsubsection \<open>Deletion\<close>
 | 
|  |     62 | 
 | 
|  |     63 | lemma inv_split_min: "\<lbrakk> split_min t = (x,t'); t \<noteq> Leaf; invh t; invc t \<rbrakk> \<Longrightarrow>
 | 
|  |     64 |    invh t' \<and>
 | 
|  |     65 |    (color t = Red \<longrightarrow> bheight t' = bheight t \<and> invc t') \<and>
 | 
|  |     66 |    (color t = Black \<longrightarrow> bheight t' = bheight t - 1 \<and> invc2 t')"
 | 
|  |     67 | apply(induction t arbitrary: x t' rule: split_min.induct)
 | 
|  |     68 | apply(auto simp: inv_baldR inv_baldL invc2I dest!: neq_LeafD
 | 
|  |     69 |            split: if_splits prod.splits)
 | 
|  |     70 | done
 | 
|  |     71 | 
 | 
|  |     72 | text \<open>An automatic proof. It is quite brittle, e.g. inlining the \<open>let\<close>s in @{const del} breaks it.\<close>
 | 
|  |     73 | lemma inv_del: "\<lbrakk> invh t; invc t \<rbrakk> \<Longrightarrow>
 | 
|  |     74 |    invh (del x t) \<and>
 | 
|  |     75 |    (color t = Red \<longrightarrow> bheight (del x t) = bheight t \<and> invc (del x t)) \<and>
 | 
|  |     76 |    (color t = Black \<longrightarrow> bheight (del x t) = bheight t - 1 \<and> invc2 (del x t))"
 | 
|  |     77 | apply(induction x t rule: del.induct)
 | 
| 71846 |     78 | apply(auto simp: inv_baldR inv_baldL invc2I dest!: inv_split_min dest: neq_LeafD
 | 
| 71352 |     79 |            split!: prod.splits if_splits)
 | 
|  |     80 | done
 | 
|  |     81 | 
 | 
|  |     82 | text\<open>A structured proof where one can see what is used in each case.\<close>
 | 
|  |     83 | lemma inv_del2: "\<lbrakk> invh t; invc t \<rbrakk> \<Longrightarrow>
 | 
|  |     84 |    invh (del x t) \<and>
 | 
|  |     85 |    (color t = Red \<longrightarrow> bheight (del x t) = bheight t \<and> invc (del x t)) \<and>
 | 
|  |     86 |    (color t = Black \<longrightarrow> bheight (del x t) = bheight t - 1 \<and> invc2 (del x t))"
 | 
|  |     87 | proof(induction x t rule: del.induct)
 | 
|  |     88 |   case (1 x)
 | 
|  |     89 |   then show ?case by simp
 | 
|  |     90 | next
 | 
|  |     91 |   case (2 x l a c r)
 | 
|  |     92 |   note if_split[split del]
 | 
|  |     93 |   show ?case
 | 
|  |     94 |   proof cases
 | 
|  |     95 |     assume "x < a"
 | 
|  |     96 |     show ?thesis
 | 
| 71457 |     97 |     proof cases (* For readability; could be automated more: *)
 | 
|  |     98 |       assume *: "l \<noteq> Leaf \<and> color l = Black"
 | 
|  |     99 |       hence "bheight l > 0" using neq_LeafD[of l] by auto
 | 
|  |    100 |       thus ?thesis using \<open>x < a\<close> "2.IH"(1) "2.prems" inv_baldL[of "del x l"] * by(auto)
 | 
| 71352 |    101 |     next
 | 
|  |    102 |       assume "\<not>(l \<noteq> Leaf \<and> color l = Black)"
 | 
| 71457 |    103 |       thus ?thesis using \<open>x < a\<close> "2.prems" "2.IH"(1) by(auto)
 | 
| 71352 |    104 |     qed
 | 
| 71457 |    105 |   next (* more automation: *)
 | 
| 71352 |    106 |     assume "\<not> x < a"
 | 
|  |    107 |     show ?thesis
 | 
|  |    108 |     proof cases
 | 
|  |    109 |       assume "x > a"
 | 
| 71457 |    110 |       show ?thesis using \<open>a < x\<close> "2.IH"(2) "2.prems" neq_LeafD[of r] inv_baldR[of _ "del x r"]
 | 
| 71846 |    111 |           by(auto split: if_split)
 | 
| 71457 |    112 |     
 | 
| 71352 |    113 |     next
 | 
|  |    114 |       assume "\<not> x > a"
 | 
| 71457 |    115 |       show ?thesis using "2.prems" \<open>\<not> x < a\<close> \<open>\<not> x > a\<close>
 | 
|  |    116 |           by(auto simp: inv_baldR invc2I dest!: inv_split_min dest: neq_LeafD split: prod.split if_split)
 | 
| 71352 |    117 |     qed
 | 
|  |    118 |   qed
 | 
|  |    119 | qed
 | 
|  |    120 | 
 | 
|  |    121 | theorem rbt_delete: "rbt t \<Longrightarrow> rbt (delete x t)"
 | 
|  |    122 | by (metis delete_def rbt_def color_paint_Black inv_del invh_paint)
 | 
|  |    123 | 
 | 
|  |    124 | text \<open>Overall correctness:\<close>
 | 
|  |    125 | 
 | 
|  |    126 | interpretation S: Set_by_Ordered
 | 
|  |    127 | where empty = empty and isin = isin and insert = insert and delete = delete
 | 
|  |    128 | and inorder = inorder and inv = rbt
 | 
|  |    129 | proof (standard, goal_cases)
 | 
|  |    130 |   case 1 show ?case by (simp add: empty_def)
 | 
|  |    131 | next
 | 
|  |    132 |   case 2 thus ?case by(simp add: isin_set_inorder)
 | 
|  |    133 | next
 | 
|  |    134 |   case 3 thus ?case by(simp add: inorder_insert)
 | 
|  |    135 | next
 | 
|  |    136 |   case 4 thus ?case by(simp add: inorder_delete)
 | 
|  |    137 | next
 | 
|  |    138 |   case 5 thus ?case by (simp add: rbt_def empty_def) 
 | 
|  |    139 | next
 | 
|  |    140 |   case 6 thus ?case by (simp add: rbt_insert) 
 | 
|  |    141 | next
 | 
|  |    142 |   case 7 thus ?case by (simp add: rbt_delete) 
 | 
|  |    143 | qed
 | 
|  |    144 | 
 | 
|  |    145 | 
 | 
|  |    146 | end
 |