Theory Tree23_Set

theory Tree23_Set
imports Tree23 Cmp Set_by_Ordered
(* Author: Tobias Nipkow *)

section ‹2-3 Tree Implementation of Sets›

theory Tree23_Set
imports
  Tree23
  Cmp
  Set_by_Ordered
begin

fun isin :: "'a::linorder tree23 ⇒ 'a ⇒ bool" where
"isin Leaf x = False" |
"isin (Node2 l a r) x =
  (case cmp x a of
     LT ⇒ isin l x |
     EQ ⇒ True |
     GT ⇒ isin r x)" |
"isin (Node3 l a m b r) x =
  (case cmp x a of
     LT ⇒ isin l x |
     EQ ⇒ True |
     GT ⇒
       (case cmp x b of
          LT ⇒ isin m x |
          EQ ⇒ True |
          GT ⇒ isin r x))"

datatype 'a upi = Ti "'a tree23" | Upi "'a tree23" 'a "'a tree23"

fun treei :: "'a upi ⇒ 'a tree23" where
"treei (Ti t) = t" |
"treei (Upi l a r) = Node2 l a r"

fun ins :: "'a::linorder ⇒ 'a tree23 ⇒ 'a upi" where
"ins x Leaf = Upi Leaf x Leaf" |
"ins x (Node2 l a r) =
   (case cmp x a of
      LT ⇒
        (case ins x l of
           Ti l' => Ti (Node2 l' a r) |
           Upi l1 b l2 => Ti (Node3 l1 b l2 a r)) |
      EQ ⇒ Ti (Node2 l x r) |
      GT ⇒
        (case ins x r of
           Ti r' => Ti (Node2 l a r') |
           Upi r1 b r2 => Ti (Node3 l a r1 b r2)))" |
"ins x (Node3 l a m b r) =
   (case cmp x a of
      LT ⇒
        (case ins x l of
           Ti l' => Ti (Node3 l' a m b r) |
           Upi l1 c l2 => Upi (Node2 l1 c l2) a (Node2 m b r)) |
      EQ ⇒ Ti (Node3 l a m b r) |
      GT ⇒
        (case cmp x b of
           GT ⇒
             (case ins x r of
                Ti r' => Ti (Node3 l a m b r') |
                Upi r1 c r2 => Upi (Node2 l a m) b (Node2 r1 c r2)) |
           EQ ⇒ Ti (Node3 l a m b r) |
           LT ⇒
             (case ins x m of
                Ti m' => Ti (Node3 l a m' b r) |
                Upi m1 c m2 => Upi (Node2 l a m1) c (Node2 m2 b r))))"

hide_const insert

definition insert :: "'a::linorder ⇒ 'a tree23 ⇒ 'a tree23" where
"insert x t = treei(ins x t)"

datatype 'a upd = Td "'a tree23" | Upd "'a tree23"

fun treed :: "'a upd ⇒ 'a tree23" where
"treed (Td t) = t" |
"treed (Upd t) = t"

(* Variation: return None to signal no-change *)

fun node21 :: "'a upd ⇒ 'a ⇒ 'a tree23 ⇒ 'a upd" where
"node21 (Td t1) a t2 = Td(Node2 t1 a t2)" |
"node21 (Upd t1) a (Node2 t2 b t3) = Upd(Node3 t1 a t2 b t3)" |
"node21 (Upd t1) a (Node3 t2 b t3 c t4) = Td(Node2 (Node2 t1 a t2) b (Node2 t3 c t4))"

fun node22 :: "'a tree23 ⇒ 'a ⇒ 'a upd ⇒ 'a upd" where
"node22 t1 a (Td t2) = Td(Node2 t1 a t2)" |
"node22 (Node2 t1 b t2) a (Upd t3) = Upd(Node3 t1 b t2 a t3)" |
"node22 (Node3 t1 b t2 c t3) a (Upd t4) = Td(Node2 (Node2 t1 b t2) c (Node2 t3 a t4))"

fun node31 :: "'a upd ⇒ 'a ⇒ 'a tree23 ⇒ 'a ⇒ 'a tree23 ⇒ 'a upd" where
"node31 (Td t1) a t2 b t3 = Td(Node3 t1 a t2 b t3)" |
"node31 (Upd t1) a (Node2 t2 b t3) c t4 = Td(Node2 (Node3 t1 a t2 b t3) c t4)" |
"node31 (Upd t1) a (Node3 t2 b t3 c t4) d t5 = Td(Node3 (Node2 t1 a t2) b (Node2 t3 c t4) d t5)"

fun node32 :: "'a tree23 ⇒ 'a ⇒ 'a upd ⇒ 'a ⇒ 'a tree23 ⇒ 'a upd" where
"node32 t1 a (Td t2) b t3 = Td(Node3 t1 a t2 b t3)" |
"node32 t1 a (Upd t2) b (Node2 t3 c t4) = Td(Node2 t1 a (Node3 t2 b t3 c t4))" |
"node32 t1 a (Upd t2) b (Node3 t3 c t4 d t5) = Td(Node3 t1 a (Node2 t2 b t3) c (Node2 t4 d t5))"

fun node33 :: "'a tree23 ⇒ 'a ⇒ 'a tree23 ⇒ 'a ⇒ 'a upd ⇒ 'a upd" where
"node33 l a m b (Td r) = Td(Node3 l a m b r)" |
"node33 t1 a (Node2 t2 b t3) c (Upd t4) = Td(Node2 t1 a (Node3 t2 b t3 c t4))" |
"node33 t1 a (Node3 t2 b t3 c t4) d (Upd t5) = Td(Node3 t1 a (Node2 t2 b t3) c (Node2 t4 d t5))"

fun del_min :: "'a tree23 ⇒ 'a * 'a upd" where
"del_min (Node2 Leaf a Leaf) = (a, Upd Leaf)" |
"del_min (Node3 Leaf a Leaf b Leaf) = (a, Td(Node2 Leaf b Leaf))" |
"del_min (Node2 l a r) = (let (x,l') = del_min l in (x, node21 l' a r))" |
"del_min (Node3 l a m b r) = (let (x,l') = del_min l in (x, node31 l' a m b r))"

fun del :: "'a::linorder ⇒ 'a tree23 ⇒ 'a upd" where
"del x Leaf = Td Leaf" |
"del x (Node2 Leaf a Leaf) =
  (if x = a then Upd Leaf else Td(Node2 Leaf a Leaf))" |
"del x (Node3 Leaf a Leaf b Leaf) =
  Td(if x = a then Node2 Leaf b Leaf else
     if x = b then Node2 Leaf a Leaf
     else Node3 Leaf a Leaf b Leaf)" |
"del x (Node2 l a r) =
  (case cmp x a of
     LT ⇒ node21 (del x l) a r |
     GT ⇒ node22 l a (del x r) |
     EQ ⇒ let (a',t) = del_min r in node22 l a' t)" |
"del x (Node3 l a m b r) =
  (case cmp x a of
     LT ⇒ node31 (del x l) a m b r |
     EQ ⇒ let (a',m') = del_min m in node32 l a' m' b r |
     GT ⇒
       (case cmp x b of
          LT ⇒ node32 l a (del x m) b r |
          EQ ⇒ let (b',r') = del_min r in node33 l a m b' r' |
          GT ⇒ node33 l a m b (del x r)))"

definition delete :: "'a::linorder ⇒ 'a tree23 ⇒ 'a tree23" where
"delete x t = treed(del x t)"


subsection "Functional Correctness"

subsubsection "Proofs for isin"

lemma "sorted(inorder t) ⟹ isin t x = (x ∈ elems (inorder t))"
by (induction t) (auto simp: elems_simps1 ball_Un)

lemma isin_set: "sorted(inorder t) ⟹ isin t x = (x ∈ elems (inorder t))"
by (induction t) (auto simp: elems_simps2)


subsubsection "Proofs for insert"

lemma inorder_ins:
  "sorted(inorder t) ⟹ inorder(treei(ins x t)) = ins_list x (inorder t)"
by(induction t) (auto simp: ins_list_simps split: upi.splits)

lemma inorder_insert:
  "sorted(inorder t) ⟹ inorder(insert a t) = ins_list a (inorder t)"
by(simp add: insert_def inorder_ins)


subsubsection "Proofs for delete"

lemma inorder_node21: "height r > 0 ⟹
  inorder (treed (node21 l' a r)) = inorder (treed l') @ a # inorder r"
by(induct l' a r rule: node21.induct) auto

lemma inorder_node22: "height l > 0 ⟹
  inorder (treed (node22 l a r')) = inorder l @ a # inorder (treed r')"
by(induct l a r' rule: node22.induct) auto

lemma inorder_node31: "height m > 0 ⟹
  inorder (treed (node31 l' a m b r)) = inorder (treed l') @ a # inorder m @ b # inorder r"
by(induct l' a m b r rule: node31.induct) auto

lemma inorder_node32: "height r > 0 ⟹
  inorder (treed (node32 l a m' b r)) = inorder l @ a # inorder (treed m') @ b # inorder r"
by(induct l a m' b r rule: node32.induct) auto

lemma inorder_node33: "height m > 0 ⟹
  inorder (treed (node33 l a m b r')) = inorder l @ a # inorder m @ b # inorder (treed r')"
by(induct l a m b r' rule: node33.induct) auto

lemmas inorder_nodes = inorder_node21 inorder_node22
  inorder_node31 inorder_node32 inorder_node33

lemma del_minD:
  "del_min t = (x,t') ⟹ bal t ⟹ height t > 0 ⟹
  x # inorder(treed t') = inorder t"
by(induction t arbitrary: t' rule: del_min.induct)
  (auto simp: inorder_nodes split: prod.splits)

lemma inorder_del: "⟦ bal t ; sorted(inorder t) ⟧ ⟹
  inorder(treed (del x t)) = del_list x (inorder t)"
by(induction t rule: del.induct)
  (auto simp: del_list_simps inorder_nodes del_minD split!: if_split prod.splits)

lemma inorder_delete: "⟦ bal t ; sorted(inorder t) ⟧ ⟹
  inorder(delete x t) = del_list x (inorder t)"
by(simp add: delete_def inorder_del)


subsection ‹Balancedness›


subsubsection "Proofs for insert"

text{* First a standard proof that @{const ins} preserves @{const bal}. *}

instantiation upi :: (type)height
begin

fun height_upi :: "'a upi ⇒ nat" where
"height (Ti t) = height t" |
"height (Upi l a r) = height l"

instance ..

end

lemma bal_ins: "bal t ⟹ bal (treei(ins a t)) ∧ height(ins a t) = height t"
by (induct t) (auto split!: if_split upi.split) (* 15 secs in 2015 *)

text{* Now an alternative proof (by Brian Huffman) that runs faster because
two properties (balance and height) are combined in one predicate. *}

inductive full :: "nat ⇒ 'a tree23 ⇒ bool" where
"full 0 Leaf" |
"⟦full n l; full n r⟧ ⟹ full (Suc n) (Node2 l p r)" |
"⟦full n l; full n m; full n r⟧ ⟹ full (Suc n) (Node3 l p m q r)"

inductive_cases full_elims:
  "full n Leaf"
  "full n (Node2 l p r)"
  "full n (Node3 l p m q r)"

inductive_cases full_0_elim: "full 0 t"
inductive_cases full_Suc_elim: "full (Suc n) t"

lemma full_0_iff [simp]: "full 0 t ⟷ t = Leaf"
  by (auto elim: full_0_elim intro: full.intros)

lemma full_Leaf_iff [simp]: "full n Leaf ⟷ n = 0"
  by (auto elim: full_elims intro: full.intros)

lemma full_Suc_Node2_iff [simp]:
  "full (Suc n) (Node2 l p r) ⟷ full n l ∧ full n r"
  by (auto elim: full_elims intro: full.intros)

lemma full_Suc_Node3_iff [simp]:
  "full (Suc n) (Node3 l p m q r) ⟷ full n l ∧ full n m ∧ full n r"
  by (auto elim: full_elims intro: full.intros)

lemma full_imp_height: "full n t ⟹ height t = n"
  by (induct set: full, simp_all)

lemma full_imp_bal: "full n t ⟹ bal t"
  by (induct set: full, auto dest: full_imp_height)

lemma bal_imp_full: "bal t ⟹ full (height t) t"
  by (induct t, simp_all)

lemma bal_iff_full: "bal t ⟷ (∃n. full n t)"
  by (auto elim!: bal_imp_full full_imp_bal)

text {* The @{const "insert"} function either preserves the height of the
tree, or increases it by one. The constructor returned by the @{term
"insert"} function determines which: A return value of the form @{term
"Ti t"} indicates that the height will be the same. A value of the
form @{term "Upi l p r"} indicates an increase in height. *}

fun fulli :: "nat ⇒ 'a upi ⇒ bool" where
"fulli n (Ti t) ⟷ full n t" |
"fulli n (Upi l p r) ⟷ full n l ∧ full n r"

lemma fulli_ins: "full n t ⟹ fulli n (ins a t)"
by (induct rule: full.induct) (auto split: upi.split)

text {* The @{const insert} operation preserves balance. *}

lemma bal_insert: "bal t ⟹ bal (insert a t)"
unfolding bal_iff_full insert_def
apply (erule exE)
apply (drule fulli_ins [of _ _ a])
apply (cases "ins a t")
apply (auto intro: full.intros)
done


subsection "Proofs for delete"

instantiation upd :: (type)height
begin

fun height_upd :: "'a upd ⇒ nat" where
"height (Td t) = height t" |
"height (Upd t) = height t + 1"

instance ..

end

lemma bal_treed_node21:
  "⟦bal r; bal (treed l'); height r = height l' ⟧ ⟹ bal (treed (node21 l' a r))"
by(induct l' a r rule: node21.induct) auto

lemma bal_treed_node22:
  "⟦bal(treed r'); bal l; height r' = height l ⟧ ⟹ bal (treed (node22 l a r'))"
by(induct l a r' rule: node22.induct) auto

lemma bal_treed_node31:
  "⟦ bal (treed l'); bal m; bal r; height l' = height r; height m = height r ⟧
  ⟹ bal (treed (node31 l' a m b r))"
by(induct l' a m b r rule: node31.induct) auto

lemma bal_treed_node32:
  "⟦ bal l; bal (treed m'); bal r; height l = height r; height m' = height r ⟧
  ⟹ bal (treed (node32 l a m' b r))"
by(induct l a m' b r rule: node32.induct) auto

lemma bal_treed_node33:
  "⟦ bal l; bal m; bal(treed r'); height l = height r'; height m = height r' ⟧
  ⟹ bal (treed (node33 l a m b r'))"
by(induct l a m b r' rule: node33.induct) auto

lemmas bals = bal_treed_node21 bal_treed_node22
  bal_treed_node31 bal_treed_node32 bal_treed_node33

lemma height'_node21:
   "height r > 0 ⟹ height(node21 l' a r) = max (height l') (height r) + 1"
by(induct l' a r rule: node21.induct)(simp_all)

lemma height'_node22:
   "height l > 0 ⟹ height(node22 l a r') = max (height l) (height r') + 1"
by(induct l a r' rule: node22.induct)(simp_all)

lemma height'_node31:
  "height m > 0 ⟹ height(node31 l a m b r) =
   max (height l) (max (height m) (height r)) + 1"
by(induct l a m b r rule: node31.induct)(simp_all add: max_def)

lemma height'_node32:
  "height r > 0 ⟹ height(node32 l a m b r) =
   max (height l) (max (height m) (height r)) + 1"
by(induct l a m b r rule: node32.induct)(simp_all add: max_def)

lemma height'_node33:
  "height m > 0 ⟹ height(node33 l a m b r) =
   max (height l) (max (height m) (height r)) + 1"
by(induct l a m b r rule: node33.induct)(simp_all add: max_def)

lemmas heights = height'_node21 height'_node22
  height'_node31 height'_node32 height'_node33

lemma height_del_min:
  "del_min t = (x, t') ⟹ height t > 0 ⟹ bal t ⟹ height t' = height t"
by(induct t arbitrary: x t' rule: del_min.induct)
  (auto simp: heights split: prod.splits)

lemma height_del: "bal t ⟹ height(del x t) = height t"
by(induction x t rule: del.induct)
  (auto simp: heights max_def height_del_min split: prod.splits)

lemma bal_del_min:
  "⟦ del_min t = (x, t'); bal t; height t > 0 ⟧ ⟹ bal (treed t')"
by(induct t arbitrary: x t' rule: del_min.induct)
  (auto simp: heights height_del_min bals split: prod.splits)

lemma bal_treed_del: "bal t ⟹ bal(treed(del x t))"
by(induction x t rule: del.induct)
  (auto simp: bals bal_del_min height_del height_del_min split: prod.splits)

corollary bal_delete: "bal t ⟹ bal(delete x t)"
by(simp add: delete_def bal_treed_del)


subsection ‹Overall Correctness›

interpretation Set_by_Ordered
where empty = Leaf and isin = isin and insert = insert and delete = delete
and inorder = inorder and inv = bal
proof (standard, goal_cases)
  case 2 thus ?case by(simp add: isin_set)
next
  case 3 thus ?case by(simp add: inorder_insert)
next
  case 4 thus ?case by(simp add: inorder_delete)
next
  case 6 thus ?case by(simp add: bal_insert)
next
  case 7 thus ?case by(simp add: bal_delete)
qed simp+

end