author | haftmann |
Mon, 06 Feb 2017 20:56:34 +0100 | |
changeset 64990 | c6a7de505796 |
parent 63411 | e051eea34990 |
child 67406 | 23307fd33906 |
permissions | -rw-r--r-- |
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 |
||
63411
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
61790
diff
changeset
|
11 |
fun update :: "'a::linorder \<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 |
|
63411
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
61790
diff
changeset
|
18 |
fun delete :: "'a::linorder \<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 |
|
61790 | 45 |
case 2 thus ?case by(simp add: lookup_map_of) |
61232 | 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 |