| 61224 |      1 | (* Author: Tobias Nipkow *)
 | 
|  |      2 | 
 | 
|  |      3 | section \<open>Red-Black Tree Implementation of Maps\<close>
 | 
|  |      4 | 
 | 
|  |      5 | theory RBT_Map
 | 
|  |      6 | imports
 | 
|  |      7 |   RBT_Set
 | 
| 61231 |      8 |   Lookup2
 | 
| 61224 |      9 | begin
 | 
|  |     10 | 
 | 
|  |     11 | fun update :: "'a::linorder \<Rightarrow> 'b \<Rightarrow> ('a*'b) rbt \<Rightarrow> ('a*'b) rbt" where
 | 
|  |     12 | "update x y Leaf = R Leaf (x,y) Leaf" |
 | 
|  |     13 | "update x y (B l (a,b) r) =
 | 
|  |     14 |   (if x < a then bal (update x y l) (a,b) r else
 | 
|  |     15 |    if x > a then bal l (a,b) (update x y r)
 | 
|  |     16 |    else B l (x,y) r)" |
 | 
|  |     17 | "update x y (R l (a,b) r) =
 | 
|  |     18 |   (if x < a then R (update x y l) (a,b) r else
 | 
|  |     19 |    if x > a then R l (a,b) (update x y r)
 | 
|  |     20 |    else R l (x,y) r)"
 | 
|  |     21 | 
 | 
|  |     22 | fun delete :: "'a::linorder \<Rightarrow> ('a*'b)rbt \<Rightarrow> ('a*'b)rbt"
 | 
|  |     23 | and deleteL :: "'a::linorder \<Rightarrow> ('a*'b)rbt \<Rightarrow> 'a*'b \<Rightarrow> ('a*'b)rbt \<Rightarrow> ('a*'b)rbt"
 | 
|  |     24 | and deleteR :: "'a::linorder \<Rightarrow> ('a*'b)rbt \<Rightarrow> 'a*'b \<Rightarrow> ('a*'b)rbt \<Rightarrow> ('a*'b)rbt"
 | 
|  |     25 | where
 | 
|  |     26 | "delete x Leaf = Leaf" |
 | 
|  |     27 | "delete x (Node c t1 (a,b) t2) = 
 | 
|  |     28 |   (if x < a then deleteL x t1 (a,b) t2 else
 | 
|  |     29 |    if x > a then deleteR x t1 (a,b) t2 else combine t1 t2)" |
 | 
|  |     30 | "deleteL x (B t1 a t2) b t3 = balL (delete x (B t1 a t2)) b t3" |
 | 
|  |     31 | "deleteL x t1 a t2 = R (delete x t1) a t2" |
 | 
|  |     32 | "deleteR x t1 a (B t2 b t3) = balR t1 a (delete x (B t2 b t3))" | 
 | 
|  |     33 | "deleteR x t1 a t2 = R t1 a (delete x t2)"
 | 
|  |     34 | 
 | 
|  |     35 | 
 | 
|  |     36 | subsection "Functional Correctness Proofs"
 | 
|  |     37 | 
 | 
|  |     38 | lemma inorder_update:
 | 
|  |     39 |   "sorted1(inorder t) \<Longrightarrow> inorder(update x y t) = upd_list x y (inorder t)"
 | 
|  |     40 | by(induction x y t rule: update.induct)
 | 
|  |     41 |   (auto simp: upd_list_simps inorder_bal)
 | 
|  |     42 | 
 | 
|  |     43 | 
 | 
|  |     44 | lemma inorder_delete:
 | 
|  |     45 |  "sorted1(inorder t1) \<Longrightarrow>  inorder(delete x t1) = del_list x (inorder t1)" and
 | 
|  |     46 |  "sorted1(inorder t1) \<Longrightarrow>  inorder(deleteL x t1 a t2) =
 | 
|  |     47 |     del_list x (inorder t1) @ a # inorder t2" and
 | 
|  |     48 |  "sorted1(inorder t2) \<Longrightarrow>  inorder(deleteR x t1 a t2) =
 | 
|  |     49 |     inorder t1 @ a # del_list x (inorder t2)"
 | 
|  |     50 | by(induction x t1 and x t1 a t2 and x t1 a t2 rule: delete_deleteL_deleteR.induct)
 | 
| 61231 |     51 |   (auto simp: del_list_simps inorder_combine inorder_balL inorder_balR)
 | 
| 61224 |     52 | 
 | 
|  |     53 | 
 | 
|  |     54 | interpretation Map_by_Ordered
 | 
|  |     55 | where empty = Leaf and lookup = lookup and update = update and delete = delete
 | 
|  |     56 | and inorder = inorder and wf = "\<lambda>_. True"
 | 
|  |     57 | proof (standard, goal_cases)
 | 
|  |     58 |   case 1 show ?case by simp
 | 
|  |     59 | next
 | 
|  |     60 |   case 2 thus ?case by(simp add: lookup_eq)
 | 
|  |     61 | next
 | 
|  |     62 |   case 3 thus ?case by(simp add: inorder_update)
 | 
|  |     63 | next
 | 
|  |     64 |   case 4 thus ?case by(simp add: inorder_delete)
 | 
|  |     65 | qed (rule TrueI)+
 | 
|  |     66 | 
 | 
|  |     67 | end
 |