src/HOL/Data_Structures/RBT_Set.thy
changeset 61749 7f530d7e552d
parent 61678 b594e9277be3
child 61754 862daa8144f3
     1.1 --- a/src/HOL/Data_Structures/RBT_Set.thy	Wed Nov 25 15:58:22 2015 +0100
     1.2 +++ b/src/HOL/Data_Structures/RBT_Set.thy	Fri Nov 27 18:01:13 2015 +0100
     1.3 @@ -9,70 +9,84 @@
     1.4    Isin2
     1.5  begin
     1.6  
     1.7 -fun insert :: "'a::cmp \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt" where
     1.8 -"insert x Leaf = R Leaf x Leaf" |
     1.9 -"insert x (B l a r) =
    1.10 +fun ins :: "'a::cmp \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt" where
    1.11 +"ins x Leaf = R Leaf x Leaf" |
    1.12 +"ins x (B l a r) =
    1.13    (case cmp x a of
    1.14 -     LT \<Rightarrow> bal (insert x l) a r |
    1.15 -     GT \<Rightarrow> bal l a (insert x r) |
    1.16 +     LT \<Rightarrow> bal (ins x l) a r |
    1.17 +     GT \<Rightarrow> bal l a (ins x r) |
    1.18       EQ \<Rightarrow> B l a r)" |
    1.19 -"insert x (R l a r) =
    1.20 +"ins x (R l a r) =
    1.21    (case cmp x a of
    1.22 -    LT \<Rightarrow> R (insert x l) a r |
    1.23 -    GT \<Rightarrow> R l a (insert x r) |
    1.24 +    LT \<Rightarrow> R (ins x l) a r |
    1.25 +    GT \<Rightarrow> R l a (ins x r) |
    1.26      EQ \<Rightarrow> R l a r)"
    1.27  
    1.28 -fun delete :: "'a::cmp \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt"
    1.29 -and deleteL :: "'a::cmp \<Rightarrow> 'a rbt \<Rightarrow> 'a \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt"
    1.30 -and deleteR :: "'a::cmp \<Rightarrow> 'a rbt \<Rightarrow> 'a \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt"
    1.31 +definition insert :: "'a::cmp \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt" where
    1.32 +"insert x t = paint Black (ins x t)"
    1.33 +
    1.34 +fun del :: "'a::cmp \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt"
    1.35 +and delL :: "'a::cmp \<Rightarrow> 'a rbt \<Rightarrow> 'a \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt"
    1.36 +and delR :: "'a::cmp \<Rightarrow> 'a rbt \<Rightarrow> 'a \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt"
    1.37  where
    1.38 -"delete x Leaf = Leaf" |
    1.39 -"delete x (Node _ l a r) =
    1.40 +"del x Leaf = Leaf" |
    1.41 +"del x (Node _ l a r) =
    1.42    (case cmp x a of
    1.43 -     LT \<Rightarrow> deleteL x l a r |
    1.44 -     GT \<Rightarrow> deleteR x l a r |
    1.45 +     LT \<Rightarrow> delL x l a r |
    1.46 +     GT \<Rightarrow> delR x l a r |
    1.47       EQ \<Rightarrow> combine l r)" |
    1.48 -"deleteL x (B t1 a t2) b t3 = balL (delete x (B t1 a t2)) b t3" |
    1.49 -"deleteL x l a r = R (delete x l) a r" |
    1.50 -"deleteR x t1 a (B t2 b t3) = balR t1 a (delete x (B t2 b t3))" | 
    1.51 -"deleteR x l a r = R l a (delete x r)"
    1.52 +"delL x (B t1 a t2) b t3 = balL (del x (B t1 a t2)) b t3" |
    1.53 +"delL x l a r = R (del x l) a r" |
    1.54 +"delR x t1 a (B t2 b t3) = balR t1 a (del x (B t2 b t3))" | 
    1.55 +"delR x l a r = R l a (del x r)"
    1.56 +
    1.57 +definition delete :: "'a::cmp \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt" where
    1.58 +"delete x t = paint Black (del x t)"
    1.59  
    1.60  
    1.61  subsection "Functional Correctness Proofs"
    1.62  
    1.63 +lemma inorder_paint: "inorder(paint c t) = inorder t"
    1.64 +by(induction t) (auto)
    1.65 +
    1.66  lemma inorder_bal:
    1.67    "inorder(bal l a r) = inorder l @ a # inorder r"
    1.68  by(induction l a r rule: bal.induct) (auto)
    1.69  
    1.70 +lemma inorder_ins:
    1.71 +  "sorted(inorder t) \<Longrightarrow> inorder(ins x t) = ins_list x (inorder t)"
    1.72 +by(induction x t rule: ins.induct) (auto simp: ins_list_simps inorder_bal)
    1.73 +
    1.74  lemma inorder_insert:
    1.75 -  "sorted(inorder t) \<Longrightarrow> inorder(insert a t) = ins_list a (inorder t)"
    1.76 -by(induction a t rule: insert.induct) (auto simp: ins_list_simps inorder_bal)
    1.77 -
    1.78 -lemma inorder_red: "inorder(red t) = inorder t"
    1.79 -by(induction t) (auto)
    1.80 +  "sorted(inorder t) \<Longrightarrow> inorder(insert x t) = ins_list x (inorder t)"
    1.81 +by (simp add: insert_def inorder_ins inorder_paint)
    1.82  
    1.83  lemma inorder_balL:
    1.84    "inorder(balL l a r) = inorder l @ a # inorder r"
    1.85 -by(induction l a r rule: balL.induct)(auto simp: inorder_bal inorder_red)
    1.86 +by(induction l a r rule: balL.induct)(auto simp: inorder_bal inorder_paint)
    1.87  
    1.88  lemma inorder_balR:
    1.89    "inorder(balR l a r) = inorder l @ a # inorder r"
    1.90 -by(induction l a r rule: balR.induct) (auto simp: inorder_bal inorder_red)
    1.91 +by(induction l a r rule: balR.induct) (auto simp: inorder_bal inorder_paint)
    1.92  
    1.93  lemma inorder_combine:
    1.94    "inorder(combine l r) = inorder l @ inorder r"
    1.95  by(induction l r rule: combine.induct)
    1.96    (auto simp: inorder_balL inorder_balR split: tree.split color.split)
    1.97  
    1.98 -lemma inorder_delete:
    1.99 - "sorted(inorder t) \<Longrightarrow>  inorder(delete x t) = del_list x (inorder t)"
   1.100 - "sorted(inorder l) \<Longrightarrow>  inorder(deleteL x l a r) =
   1.101 +lemma inorder_del:
   1.102 + "sorted(inorder t) \<Longrightarrow>  inorder(del x t) = del_list x (inorder t)"
   1.103 + "sorted(inorder l) \<Longrightarrow>  inorder(delL x l a r) =
   1.104      del_list x (inorder l) @ a # inorder r"
   1.105 - "sorted(inorder r) \<Longrightarrow>  inorder(deleteR x l a r) =
   1.106 + "sorted(inorder r) \<Longrightarrow>  inorder(delR x l a r) =
   1.107      inorder l @ a # del_list x (inorder r)"
   1.108 -by(induction x t and x l a r and x l a r rule: delete_deleteL_deleteR.induct)
   1.109 +by(induction x t and x l a r and x l a r rule: del_delL_delR.induct)
   1.110    (auto simp: del_list_simps inorder_combine inorder_balL inorder_balR)
   1.111  
   1.112 +lemma inorder_delete:
   1.113 +  "sorted(inorder t) \<Longrightarrow> inorder(delete x t) = del_list x (inorder t)"
   1.114 +by (auto simp: delete_def inorder_del inorder_paint)
   1.115 +
   1.116  
   1.117  interpretation Set_by_Ordered
   1.118  where empty = Leaf and isin = isin and insert = insert and delete = delete
   1.119 @@ -84,7 +98,7 @@
   1.120  next
   1.121    case 3 thus ?case by(simp add: inorder_insert)
   1.122  next
   1.123 -  case 4 thus ?case by(simp add: inorder_delete(1))
   1.124 -qed (rule TrueI)+
   1.125 +  case 4 thus ?case by(simp add: inorder_delete)
   1.126 +qed auto
   1.127  
   1.128  end