| 
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
  | 
| 
73591
 | 
    97  | 
    proof cases (* For readability; is automated more (below) *)
  | 
| 
 | 
    98  | 
      assume "l = Leaf" thus ?thesis using \<open>x < a\<close> "2.prems" by(auto)
  | 
| 
71352
 | 
    99  | 
    next
  | 
| 
73591
 | 
   100  | 
      assume l: "l \<noteq> Leaf"
  | 
| 
 | 
   101  | 
      show ?thesis
  | 
| 
 | 
   102  | 
      proof (cases "color l")
  | 
| 
 | 
   103  | 
        assume *: "color l = Black"
  | 
| 
 | 
   104  | 
        hence "bheight l > 0" using l neq_LeafD[of l] by auto
  | 
| 
 | 
   105  | 
        thus ?thesis using \<open>x < a\<close> "2.IH"(1) "2.prems" inv_baldL[of "del x l"] * l by(auto)
  | 
| 
 | 
   106  | 
      next
  | 
| 
 | 
   107  | 
        assume "color l = Red"
  | 
| 
 | 
   108  | 
        thus ?thesis using \<open>x < a\<close> "2.prems" "2.IH"(1) by(auto)
  | 
| 
 | 
   109  | 
      qed
  | 
| 
71352
 | 
   110  | 
    qed
  | 
| 
71457
 | 
   111  | 
  next (* more automation: *)
  | 
| 
71352
 | 
   112  | 
    assume "\<not> x < a"
  | 
| 
 | 
   113  | 
    show ?thesis
  | 
| 
 | 
   114  | 
    proof cases
  | 
| 
 | 
   115  | 
      assume "x > a"
  | 
| 
71457
 | 
   116  | 
      show ?thesis using \<open>a < x\<close> "2.IH"(2) "2.prems" neq_LeafD[of r] inv_baldR[of _ "del x r"]
  | 
| 
71846
 | 
   117  | 
          by(auto split: if_split)
  | 
| 
71457
 | 
   118  | 
    
  | 
| 
71352
 | 
   119  | 
    next
  | 
| 
 | 
   120  | 
      assume "\<not> x > a"
  | 
| 
71457
 | 
   121  | 
      show ?thesis using "2.prems" \<open>\<not> x < a\<close> \<open>\<not> x > a\<close>
  | 
| 
 | 
   122  | 
          by(auto simp: inv_baldR invc2I dest!: inv_split_min dest: neq_LeafD split: prod.split if_split)
  | 
| 
71352
 | 
   123  | 
    qed
  | 
| 
 | 
   124  | 
  qed
  | 
| 
 | 
   125  | 
qed
  | 
| 
 | 
   126  | 
  | 
| 
 | 
   127  | 
theorem rbt_delete: "rbt t \<Longrightarrow> rbt (delete x t)"
  | 
| 
 | 
   128  | 
by (metis delete_def rbt_def color_paint_Black inv_del invh_paint)
  | 
| 
 | 
   129  | 
  | 
| 
 | 
   130  | 
text \<open>Overall correctness:\<close>
  | 
| 
 | 
   131  | 
  | 
| 
 | 
   132  | 
interpretation S: Set_by_Ordered
  | 
| 
 | 
   133  | 
where empty = empty and isin = isin and insert = insert and delete = delete
  | 
| 
 | 
   134  | 
and inorder = inorder and inv = rbt
  | 
| 
 | 
   135  | 
proof (standard, goal_cases)
  | 
| 
 | 
   136  | 
  case 1 show ?case by (simp add: empty_def)
  | 
| 
 | 
   137  | 
next
  | 
| 
 | 
   138  | 
  case 2 thus ?case by(simp add: isin_set_inorder)
  | 
| 
 | 
   139  | 
next
  | 
| 
 | 
   140  | 
  case 3 thus ?case by(simp add: inorder_insert)
  | 
| 
 | 
   141  | 
next
  | 
| 
 | 
   142  | 
  case 4 thus ?case by(simp add: inorder_delete)
  | 
| 
 | 
   143  | 
next
  | 
| 
 | 
   144  | 
  case 5 thus ?case by (simp add: rbt_def empty_def) 
  | 
| 
 | 
   145  | 
next
  | 
| 
 | 
   146  | 
  case 6 thus ?case by (simp add: rbt_insert) 
  | 
| 
 | 
   147  | 
next
  | 
| 
 | 
   148  | 
  case 7 thus ?case by (simp add: rbt_delete) 
  | 
| 
 | 
   149  | 
qed
  | 
| 
 | 
   150  | 
  | 
| 
 | 
   151  | 
  | 
| 
 | 
   152  | 
end
  |