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
|
|
8 |
Map_by_Ordered
|
|
9 |
begin
|
|
10 |
|
|
11 |
fun lookup :: "('a::linorder * 'b) rbt \<Rightarrow> 'a \<Rightarrow> 'b option" where
|
|
12 |
"lookup Leaf x = None" |
|
|
13 |
"lookup (Node _ l (a,b) r) x =
|
|
14 |
(if x < a then lookup l x else
|
|
15 |
if x > a then lookup r x else Some b)"
|
|
16 |
|
|
17 |
fun update :: "'a::linorder \<Rightarrow> 'b \<Rightarrow> ('a*'b) rbt \<Rightarrow> ('a*'b) rbt" where
|
|
18 |
"update x y Leaf = R Leaf (x,y) Leaf" |
|
|
19 |
"update x y (B l (a,b) r) =
|
|
20 |
(if x < a then bal (update x y l) (a,b) r else
|
|
21 |
if x > a then bal l (a,b) (update x y r)
|
|
22 |
else B l (x,y) r)" |
|
|
23 |
"update x y (R l (a,b) r) =
|
|
24 |
(if x < a then R (update x y l) (a,b) r else
|
|
25 |
if x > a then R l (a,b) (update x y r)
|
|
26 |
else R l (x,y) r)"
|
|
27 |
|
|
28 |
fun delete :: "'a::linorder \<Rightarrow> ('a*'b)rbt \<Rightarrow> ('a*'b)rbt"
|
|
29 |
and deleteL :: "'a::linorder \<Rightarrow> ('a*'b)rbt \<Rightarrow> 'a*'b \<Rightarrow> ('a*'b)rbt \<Rightarrow> ('a*'b)rbt"
|
|
30 |
and deleteR :: "'a::linorder \<Rightarrow> ('a*'b)rbt \<Rightarrow> 'a*'b \<Rightarrow> ('a*'b)rbt \<Rightarrow> ('a*'b)rbt"
|
|
31 |
where
|
|
32 |
"delete x Leaf = Leaf" |
|
|
33 |
"delete x (Node c t1 (a,b) t2) =
|
|
34 |
(if x < a then deleteL x t1 (a,b) t2 else
|
|
35 |
if x > a then deleteR x t1 (a,b) t2 else combine t1 t2)" |
|
|
36 |
"deleteL x (B t1 a t2) b t3 = balL (delete x (B t1 a t2)) b t3" |
|
|
37 |
"deleteL x t1 a t2 = R (delete x t1) a t2" |
|
|
38 |
"deleteR x t1 a (B t2 b t3) = balR t1 a (delete x (B t2 b t3))" |
|
|
39 |
"deleteR x t1 a t2 = R t1 a (delete x t2)"
|
|
40 |
|
|
41 |
|
|
42 |
subsection "Functional Correctness Proofs"
|
|
43 |
|
|
44 |
lemma lookup_eq:
|
|
45 |
"sorted1(inorder t) \<Longrightarrow> lookup t x = map_of (inorder t) x"
|
|
46 |
by(induction t)
|
|
47 |
(auto simp: sorted_lems map_of_append map_of_sorteds split: option.split)
|
|
48 |
|
|
49 |
|
|
50 |
lemma inorder_update:
|
|
51 |
"sorted1(inorder t) \<Longrightarrow> inorder(update x y t) = upd_list x y (inorder t)"
|
|
52 |
by(induction x y t rule: update.induct)
|
|
53 |
(auto simp: upd_list_simps inorder_bal)
|
|
54 |
|
|
55 |
|
|
56 |
lemma inorder_delete:
|
|
57 |
"sorted1(inorder t1) \<Longrightarrow> inorder(delete x t1) = del_list x (inorder t1)" and
|
|
58 |
"sorted1(inorder t1) \<Longrightarrow> inorder(deleteL x t1 a t2) =
|
|
59 |
del_list x (inorder t1) @ a # inorder t2" and
|
|
60 |
"sorted1(inorder t2) \<Longrightarrow> inorder(deleteR x t1 a t2) =
|
|
61 |
inorder t1 @ a # del_list x (inorder t2)"
|
|
62 |
by(induction x t1 and x t1 a t2 and x t1 a t2 rule: delete_deleteL_deleteR.induct)
|
|
63 |
(auto simp: del_list_sorted sorted_lems inorder_combine inorder_balL inorder_balR)
|
|
64 |
|
|
65 |
|
|
66 |
interpretation Map_by_Ordered
|
|
67 |
where empty = Leaf and lookup = lookup and update = update and delete = delete
|
|
68 |
and inorder = inorder and wf = "\<lambda>_. True"
|
|
69 |
proof (standard, goal_cases)
|
|
70 |
case 1 show ?case by simp
|
|
71 |
next
|
|
72 |
case 2 thus ?case by(simp add: lookup_eq)
|
|
73 |
next
|
|
74 |
case 3 thus ?case by(simp add: inorder_update)
|
|
75 |
next
|
|
76 |
case 4 thus ?case by(simp add: inorder_delete)
|
|
77 |
qed (rule TrueI)+
|
|
78 |
|
|
79 |
end
|