61203
|
1 |
(* Author: Tobias Nipkow *)
|
|
2 |
|
|
3 |
section {* Unbalanced Tree as Map *}
|
|
4 |
|
|
5 |
theory Tree_Map
|
|
6 |
imports
|
61231
|
7 |
Tree_Set
|
61203
|
8 |
Map_by_Ordered
|
|
9 |
begin
|
|
10 |
|
|
11 |
fun lookup :: "('a::linorder*'b) tree \<Rightarrow> 'a \<Rightarrow> 'b option" where
|
|
12 |
"lookup Leaf x = None" |
|
|
13 |
"lookup (Node l (a,b) r) x = (if x < a then lookup l x else
|
|
14 |
if x > a then lookup r x else Some b)"
|
|
15 |
|
|
16 |
fun update :: "'a::linorder \<Rightarrow> 'b \<Rightarrow> ('a*'b) tree \<Rightarrow> ('a*'b) tree" where
|
|
17 |
"update a b Leaf = Node Leaf (a,b) Leaf" |
|
|
18 |
"update a b (Node l (x,y) r) =
|
|
19 |
(if a < x then Node (update a b l) (x,y) r
|
|
20 |
else if a=x then Node l (a,b) r
|
|
21 |
else Node l (x,y) (update a b r))"
|
|
22 |
|
|
23 |
fun delete :: "'a::linorder \<Rightarrow> ('a*'b) tree \<Rightarrow> ('a*'b) tree" where
|
|
24 |
"delete k Leaf = Leaf" |
|
|
25 |
"delete k (Node l (a,b) r) = (if k<a then Node (delete k l) (a,b) r else
|
|
26 |
if k > a then Node l (a,b) (delete k r) else
|
|
27 |
if r = Leaf then l else let (ab',r') = del_min r in Node l ab' r')"
|
|
28 |
|
|
29 |
|
|
30 |
subsection "Functional Correctness Proofs"
|
|
31 |
|
61224
|
32 |
lemma lookup_eq:
|
|
33 |
"sorted1(inorder t) \<Longrightarrow> lookup t x = map_of (inorder t) x"
|
61231
|
34 |
by (induction t) (auto simp: map_of_simps split: option.split)
|
61203
|
35 |
|
|
36 |
|
|
37 |
lemma inorder_update:
|
|
38 |
"sorted1(inorder t) \<Longrightarrow> inorder(update a b t) = upd_list a b (inorder t)"
|
61224
|
39 |
by(induction t) (auto simp: upd_list_simps)
|
61203
|
40 |
|
|
41 |
|
|
42 |
lemma del_minD:
|
|
43 |
"del_min t = (x,t') \<Longrightarrow> t \<noteq> Leaf \<Longrightarrow> sorted1(inorder t) \<Longrightarrow>
|
|
44 |
x # inorder t' = inorder t"
|
|
45 |
by(induction t arbitrary: t' rule: del_min.induct)
|
61231
|
46 |
(auto simp: del_list_simps split: prod.splits)
|
61203
|
47 |
|
|
48 |
lemma inorder_delete:
|
|
49 |
"sorted1(inorder t) \<Longrightarrow> inorder(delete x t) = del_list x (inorder t)"
|
61231
|
50 |
by(induction t) (auto simp: del_list_simps del_minD split: prod.splits)
|
61203
|
51 |
|
|
52 |
|
|
53 |
interpretation Map_by_Ordered
|
|
54 |
where empty = Leaf and lookup = lookup and update = update and delete = delete
|
|
55 |
and inorder = inorder and wf = "\<lambda>_. True"
|
|
56 |
proof (standard, goal_cases)
|
|
57 |
case 1 show ?case by simp
|
|
58 |
next
|
|
59 |
case 2 thus ?case by(simp add: lookup_eq)
|
|
60 |
next
|
|
61 |
case 3 thus ?case by(simp add: inorder_update)
|
|
62 |
next
|
|
63 |
case 4 thus ?case by(simp add: inorder_delete)
|
|
64 |
qed (rule TrueI)+
|
|
65 |
|
|
66 |
end
|