61232
|
1 |
(* Author: Tobias Nipkow *)
|
|
2 |
|
|
3 |
section "AVL Tree Implementation of Maps"
|
|
4 |
|
|
5 |
theory AVL_Map
|
|
6 |
imports
|
|
7 |
AVL_Set
|
|
8 |
Lookup2
|
|
9 |
begin
|
|
10 |
|
61581
|
11 |
fun update :: "'a::cmp \<Rightarrow> 'b \<Rightarrow> ('a*'b) avl_tree \<Rightarrow> ('a*'b) avl_tree" where
|
61232
|
12 |
"update x y Leaf = Node 1 Leaf (x,y) Leaf" |
|
61581
|
13 |
"update x y (Node h l (a,b) r) = (case cmp x a of
|
|
14 |
EQ \<Rightarrow> Node h l (x,y) r |
|
|
15 |
LT \<Rightarrow> balL (update x y l) (a,b) r |
|
|
16 |
GT \<Rightarrow> balR l (a,b) (update x y r))"
|
61232
|
17 |
|
61581
|
18 |
fun delete :: "'a::cmp \<Rightarrow> ('a*'b) avl_tree \<Rightarrow> ('a*'b) avl_tree" where
|
61232
|
19 |
"delete _ Leaf = Leaf" |
|
61581
|
20 |
"delete x (Node h l (a,b) r) = (case cmp x a of
|
61648
|
21 |
EQ \<Rightarrow> del_root (Node h l (a,b) r) |
|
61581
|
22 |
LT \<Rightarrow> balR (delete x l) (a,b) r |
|
|
23 |
GT \<Rightarrow> balL l (a,b) (delete x r))"
|
61232
|
24 |
|
|
25 |
|
|
26 |
subsection {* Functional Correctness Proofs *}
|
|
27 |
|
|
28 |
theorem inorder_update:
|
|
29 |
"sorted1(inorder t) \<Longrightarrow> inorder(update x y t) = upd_list x y (inorder t)"
|
61581
|
30 |
by (induct t) (auto simp: upd_list_simps inorder_balL inorder_balR)
|
61232
|
31 |
|
|
32 |
|
|
33 |
theorem inorder_delete:
|
|
34 |
"sorted1(inorder t) \<Longrightarrow> inorder (delete x t) = del_list x (inorder t)"
|
|
35 |
by(induction t)
|
61581
|
36 |
(auto simp: del_list_simps inorder_balL inorder_balR
|
61648
|
37 |
inorder_del_root inorder_del_maxD split: prod.splits)
|
61232
|
38 |
|
|
39 |
interpretation Map_by_Ordered
|
|
40 |
where empty = Leaf and lookup = lookup and update = update and delete = delete
|
61686
|
41 |
and inorder = inorder and inv = "\<lambda>_. True"
|
61232
|
42 |
proof (standard, goal_cases)
|
|
43 |
case 1 show ?case by simp
|
|
44 |
next
|
|
45 |
case 2 thus ?case by(simp add: lookup_eq)
|
|
46 |
next
|
|
47 |
case 3 thus ?case by(simp add: inorder_update)
|
|
48 |
next
|
|
49 |
case 4 thus ?case by(simp add: inorder_delete)
|
61686
|
50 |
qed auto
|
61232
|
51 |
|
|
52 |
end
|