Theory AA_Map

theory AA_Map
imports AA_Set Lookup2
(* Author: Tobias Nipkow *)

section "AA Implementation of Maps"

theory AA_Map
imports
  AA_Set
  Lookup2
begin

fun update :: "'a::cmp ⇒ 'b ⇒ ('a*'b) aa_tree ⇒ ('a*'b) aa_tree" where
"update x y Leaf = Node 1 Leaf (x,y) Leaf" |
"update x y (Node lv t1 (a,b) t2) =
  (case cmp x a of
     LT ⇒ split (skew (Node lv (update x y t1) (a,b) t2)) |
     GT ⇒ split (skew (Node lv t1 (a,b) (update x y t2))) |
     EQ ⇒ Node lv t1 (x,y) t2)"

fun delete :: "'a::cmp ⇒ ('a*'b) aa_tree ⇒ ('a*'b) aa_tree" where
"delete _ Leaf = Leaf" |
"delete x (Node lv l (a,b) r) =
  (case cmp x a of
     LT ⇒ adjust (Node lv (delete x l) (a,b) r) |
     GT ⇒ adjust (Node lv l (a,b) (delete x r)) |
     EQ ⇒ (if l = Leaf then r
            else let (l',ab') = del_max l in adjust (Node lv l' ab' 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_split inorder_skew)


theorem inorder_delete:
  "sorted1(inorder t) ⟹ inorder (delete x t) = del_list x (inorder t)"
by(induction t)
  (auto simp: del_list_simps inorder_adjust 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