# Theory Tree234_Map

theory Tree234_Map
imports Tree234_Set Map_Specs
```(* Author: Tobias Nipkow *)

section ‹2-3-4 Tree Implementation of Maps›

theory Tree234_Map
imports
Tree234_Set
Map_Specs
begin

subsection ‹Map operations on 2-3-4 trees›

fun lookup :: "('a::linorder * 'b) tree234 ⇒ 'a ⇒ 'b option" where
"lookup Leaf x = None" |
"lookup (Node2 l (a,b) r) x = (case cmp x a of
LT ⇒ lookup l x |
GT ⇒ lookup r x |
EQ ⇒ Some b)" |
"lookup (Node3 l (a1,b1) m (a2,b2) r) x = (case cmp x a1 of
LT ⇒ lookup l x |
EQ ⇒ Some b1 |
GT ⇒ (case cmp x a2 of
LT ⇒ lookup m x |
EQ ⇒ Some b2 |
GT ⇒ lookup r x))" |
"lookup (Node4 t1 (a1,b1) t2 (a2,b2) t3 (a3,b3) t4) x = (case cmp x a2 of
LT ⇒ (case cmp x a1 of
LT ⇒ lookup t1 x | EQ ⇒ Some b1 | GT ⇒ lookup t2 x) |
EQ ⇒ Some b2 |
GT ⇒ (case cmp x a3 of
LT ⇒ lookup t3 x | EQ ⇒ Some b3 | GT ⇒ lookup t4 x))"

fun upd :: "'a::linorder ⇒ 'b ⇒ ('a*'b) tree234 ⇒ ('a*'b) up⇩i" where
"upd x y Leaf = Up⇩i Leaf (x,y) Leaf" |
"upd x y (Node2 l ab r) = (case cmp x (fst ab) of
LT ⇒ (case upd x y l of
T⇩i l' => T⇩i (Node2 l' ab r)
| Up⇩i l1 ab' l2 => T⇩i (Node3 l1 ab' l2 ab r)) |
EQ ⇒ T⇩i (Node2 l (x,y) r) |
GT ⇒ (case upd x y r of
T⇩i r' => T⇩i (Node2 l ab r')
| Up⇩i r1 ab' r2 => T⇩i (Node3 l ab r1 ab' r2)))" |
"upd x y (Node3 l ab1 m ab2 r) = (case cmp x (fst ab1) of
LT ⇒ (case upd x y l of
T⇩i l' => T⇩i (Node3 l' ab1 m ab2 r)
| Up⇩i l1 ab' l2 => Up⇩i (Node2 l1 ab' l2) ab1 (Node2 m ab2 r)) |
EQ ⇒ T⇩i (Node3 l (x,y) m ab2 r) |
GT ⇒ (case cmp x (fst ab2) of
LT ⇒ (case upd x y m of
T⇩i m' => T⇩i (Node3 l ab1 m' ab2 r)
| Up⇩i m1 ab' m2 => Up⇩i (Node2 l ab1 m1) ab' (Node2 m2 ab2 r)) |
EQ ⇒ T⇩i (Node3 l ab1 m (x,y) r) |
GT ⇒ (case upd x y r of
T⇩i r' => T⇩i (Node3 l ab1 m ab2 r')
| Up⇩i r1 ab' r2 => Up⇩i (Node2 l ab1 m) ab2 (Node2 r1 ab' r2))))" |
"upd x y (Node4 t1 ab1 t2 ab2 t3 ab3 t4) = (case cmp x (fst ab2) of
LT ⇒ (case cmp x (fst ab1) of
LT ⇒ (case upd x y t1 of
T⇩i t1' => T⇩i (Node4 t1' ab1 t2 ab2 t3 ab3 t4)
| Up⇩i t11 q t12 => Up⇩i (Node2 t11 q t12) ab1 (Node3 t2 ab2 t3 ab3 t4)) |
EQ ⇒ T⇩i (Node4 t1 (x,y) t2 ab2 t3 ab3 t4) |
GT ⇒ (case upd x y t2 of
T⇩i t2' => T⇩i (Node4 t1 ab1 t2' ab2 t3 ab3 t4)
| Up⇩i t21 q t22 => Up⇩i (Node2 t1 ab1 t21) q (Node3 t22 ab2 t3 ab3 t4))) |
EQ ⇒ T⇩i (Node4 t1 ab1 t2 (x,y) t3 ab3 t4) |
GT ⇒ (case cmp x (fst ab3) of
LT ⇒ (case upd x y t3 of
T⇩i t3' ⇒ T⇩i (Node4 t1 ab1 t2 ab2 t3' ab3 t4)
| Up⇩i t31 q t32 => Up⇩i (Node2 t1 ab1 t2) ab2(*q*) (Node3 t31 q t32 ab3 t4)) |
EQ ⇒ T⇩i (Node4 t1 ab1 t2 ab2 t3 (x,y) t4) |
GT ⇒ (case upd x y t4 of
T⇩i t4' => T⇩i (Node4 t1 ab1 t2 ab2 t3 ab3 t4')
| Up⇩i t41 q t42 => Up⇩i (Node2 t1 ab1 t2) ab2 (Node3 t3 ab3 t41 q t42))))"

definition update :: "'a::linorder ⇒ 'b ⇒ ('a*'b) tree234 ⇒ ('a*'b) tree234" where
"update x y t = tree⇩i(upd x y t)"

fun del :: "'a::linorder ⇒ ('a*'b) tree234 ⇒ ('a*'b) up⇩d" where
"del x Leaf = T⇩d Leaf" |
"del x (Node2 Leaf ab1 Leaf) = (if x=fst ab1 then Up⇩d Leaf else T⇩d(Node2 Leaf ab1 Leaf))" |
"del x (Node3 Leaf ab1 Leaf ab2 Leaf) = T⇩d(if x=fst ab1 then Node2 Leaf ab2 Leaf
else if x=fst ab2 then Node2 Leaf ab1 Leaf else Node3 Leaf ab1 Leaf ab2 Leaf)" |
"del x (Node4 Leaf ab1 Leaf ab2 Leaf ab3 Leaf) =
T⇩d(if x = fst ab1 then Node3 Leaf ab2 Leaf ab3 Leaf else
if x = fst ab2 then Node3 Leaf ab1 Leaf ab3 Leaf else
if x = fst ab3 then Node3 Leaf ab1 Leaf ab2 Leaf
else Node4 Leaf ab1 Leaf ab2 Leaf ab3 Leaf)" |
"del x (Node2 l ab1 r) = (case cmp x (fst ab1) of
LT ⇒ node21 (del x l) ab1 r |
GT ⇒ node22 l ab1 (del x r) |
EQ ⇒ let (ab1',t) = split_min r in node22 l ab1' t)" |
"del x (Node3 l ab1 m ab2 r) = (case cmp x (fst ab1) of
LT ⇒ node31 (del x l) ab1 m ab2 r |
EQ ⇒ let (ab1',m') = split_min m in node32 l ab1' m' ab2 r |
GT ⇒ (case cmp x (fst ab2) of
LT ⇒ node32 l ab1 (del x m) ab2 r |
EQ ⇒ let (ab2',r') = split_min r in node33 l ab1 m ab2' r' |
GT ⇒ node33 l ab1 m ab2 (del x r)))" |
"del x (Node4 t1 ab1 t2 ab2 t3 ab3 t4) = (case cmp x (fst ab2) of
LT ⇒ (case cmp x (fst ab1) of
LT ⇒ node41 (del x t1) ab1 t2 ab2 t3 ab3 t4 |
EQ ⇒ let (ab',t2') = split_min t2 in node42 t1 ab' t2' ab2 t3 ab3 t4 |
GT ⇒ node42 t1 ab1 (del x t2) ab2 t3 ab3 t4) |
EQ ⇒ let (ab',t3') = split_min t3 in node43 t1 ab1 t2 ab' t3' ab3 t4 |
GT ⇒ (case cmp x (fst ab3) of
LT ⇒ node43 t1 ab1 t2 ab2 (del x t3) ab3 t4 |
EQ ⇒ let (ab',t4') = split_min t4 in node44 t1 ab1 t2 ab2 t3 ab' t4' |
GT ⇒ node44 t1 ab1 t2 ab2 t3 ab3 (del x t4)))"

definition delete :: "'a::linorder ⇒ ('a*'b) tree234 ⇒ ('a*'b) tree234" where
"delete x t = tree⇩d(del x t)"

subsection "Functional correctness"

lemma lookup_map_of:
"sorted1(inorder t) ⟹ lookup t x = map_of (inorder t) x"
by (induction t) (auto simp: map_of_simps split: option.split)

lemma inorder_upd:
"sorted1(inorder t) ⟹ inorder(tree⇩i(upd a b t)) = upd_list a b (inorder t)"
by(induction t)
(auto simp: upd_list_simps, auto simp: upd_list_simps split: up⇩i.splits)

lemma inorder_update:
"sorted1(inorder t) ⟹ inorder(update a b t) = upd_list a b (inorder t)"

lemma inorder_del: "⟦ bal t ; sorted1(inorder t) ⟧ ⟹
inorder(tree⇩d (del x t)) = del_list x (inorder t)"
by(induction t rule: del.induct)
(auto simp: del_list_simps inorder_nodes split_minD split!: if_splits prod.splits)
(* 30 secs (2016) *)

lemma inorder_delete: "⟦ bal t ; sorted1(inorder t) ⟧ ⟹
inorder(delete x t) = del_list x (inorder t)"

subsection ‹Balancedness›

lemma bal_upd: "bal t ⟹ bal (tree⇩i(upd x y t)) ∧ height(upd x y t) = height t"
by (induct t) (auto, auto split!: if_split up⇩i.split) (* 20 secs (2015) *)

lemma bal_update: "bal t ⟹ bal (update x y t)"

lemma height_del: "bal t ⟹ height(del x t) = height t"
by(induction x t rule: del.induct)
(auto simp add: heights height_split_min split!: if_split prod.split)

lemma bal_tree⇩d_del: "bal t ⟹ bal(tree⇩d(del x t))"
by(induction x t rule: del.induct)
(auto simp: bals bal_split_min height_del height_split_min split!: if_split prod.split)

corollary bal_delete: "bal t ⟹ bal(delete x t)"

subsection ‹Overall Correctness›

interpretation M: Map_by_Ordered
where empty = empty and lookup = lookup and update = update and delete = delete
and inorder = inorder and inv = bal
proof (standard, goal_cases)
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)
next
case 6 thus ?case by(simp add: bal_update)
next
case 7 thus ?case by(simp add: bal_delete)