Theory AVL_Map

theory AVL_Map
imports AVL_Set Lookup2
(* Author: Tobias Nipkow *)

section "AVL Tree Implementation of Maps"

theory AVL_Map
imports
  AVL_Set
  Lookup2
begin

fun update :: "'a::linorder ⇒ 'b ⇒ ('a*'b) avl_tree ⇒ ('a*'b) avl_tree" where
"update x y Leaf = Node 1 Leaf (x,y) Leaf" |
"update x y (Node h l (a,b) r) = (case cmp x a of
   EQ ⇒ Node h l (x,y) r |
   LT ⇒ balL (update x y l) (a,b) r |
   GT ⇒ balR l (a,b) (update x y r))"

fun delete :: "'a::linorder ⇒ ('a*'b) avl_tree ⇒ ('a*'b) avl_tree" where
"delete _ Leaf = Leaf" |
"delete x (Node h l (a,b) r) = (case cmp x a of
   EQ ⇒ del_root (Node h l (a,b) r) |
   LT ⇒ balR (delete x l) (a,b) r |
   GT ⇒ balL l (a,b) (delete x r))"


subsection {* Functional Correctness Proofs *}

theorem inorder_update:
  "sorted1(inorder t) ⟹ inorder(update x y t) = upd_list x y (inorder t)"
by (induct t) (auto simp: upd_list_simps inorder_balL inorder_balR)


theorem inorder_delete:
  "sorted1(inorder t) ⟹ inorder (delete x t) = del_list x (inorder t)"
by(induction t)
  (auto simp: del_list_simps inorder_balL inorder_balR
     inorder_del_root inorder_del_maxD split: prod.splits)

interpretation Map_by_Ordered
where empty = Leaf and lookup = lookup and update = update and delete = delete
and inorder = inorder and inv = "λ_. True"
proof (standard, goal_cases)
  case 1 show ?case by simp
next
  case 2 thus ?case by(simp add: lookup_map_of)
next
  case 3 thus ?case by(simp add: inorder_update)
next
  case 4 thus ?case by(simp add: inorder_delete)
qed auto

end