Theory List_Ins_Del

theory List_Ins_Del
imports Sorted_Less
(* Author: Tobias Nipkow *)

section {* List Insertion and Deletion *}

theory List_Ins_Del
imports Sorted_Less
begin

subsection ‹Elements in a list›

fun elems :: "'a list ⇒ 'a set" where
"elems [] = {}" |
"elems (x#xs) = Set.insert x (elems xs)"

lemma elems_app: "elems (xs @ ys) = (elems xs ∪ elems ys)"
by (induction xs) auto

lemma elems_eq_set: "elems xs = set xs"
by (induction xs) auto

lemma sorted_Cons_iff:
  "sorted(x # xs) = ((∀y ∈ elems xs. x < y) ∧ sorted xs)"
by(simp add: elems_eq_set sorted_wrt_Cons)

lemma sorted_snoc_iff:
  "sorted(xs @ [x]) = (sorted xs ∧ (∀y ∈ elems xs. y < x))"
by(simp add: elems_eq_set sorted_wrt_append)

text{* The above two rules introduce quantifiers. It turns out
that in practice this is not a problem because of the simplicity of
the "isin" functions that implement @{const elems}. Nevertheless
it is possible to avoid the quantifiers with the help of some rewrite rules: *}

lemma sorted_ConsD: "sorted (y # xs) ⟹ x ≤ y ⟹ x ∉ elems xs"
by (auto simp: sorted_Cons_iff)

lemma sorted_snocD: "sorted (xs @ [y]) ⟹ y ≤ x ⟹ x ∉ elems xs"
by (auto simp: sorted_snoc_iff)

lemmas elems_simps = sorted_lems elems_app
lemmas elems_simps1 = elems_simps sorted_Cons_iff sorted_snoc_iff
lemmas elems_simps2 = elems_simps sorted_ConsD sorted_snocD


subsection ‹Inserting into an ordered list without duplicates:›

fun ins_list :: "'a::linorder ⇒ 'a list ⇒ 'a list" where
"ins_list x [] = [x]" |
"ins_list x (a#xs) =
  (if x < a then x#a#xs else if x=a then a#xs else a # ins_list x xs)"

lemma set_ins_list: "elems (ins_list x xs) = insert x (elems xs)"
by(induction xs) auto

lemma distinct_if_sorted: "sorted xs ⟹ distinct xs"
apply(induction xs rule: sorted_wrt_induct)
apply auto
by (metis in_set_conv_decomp_first less_imp_not_less sorted_mid_iff2)

lemma sorted_ins_list: "sorted xs ⟹ sorted(ins_list x xs)"
by(induction xs rule: sorted_wrt_induct) auto

lemma ins_list_sorted: "sorted (xs @ [a]) ⟹
  ins_list x (xs @ a # ys) =
  (if x < a then ins_list x xs @ (a#ys) else xs @ ins_list x (a#ys))"
by(induction xs) (auto simp: sorted_lems)

text‹In principle, @{thm ins_list_sorted} suffices, but the following two
corollaries speed up proofs.›

corollary ins_list_sorted1: "sorted (xs @ [a]) ⟹ a ≤ x ⟹
  ins_list x (xs @ a # ys) = xs @ ins_list x (a#ys)"
by(auto simp add: ins_list_sorted)

corollary ins_list_sorted2: "sorted (xs @ [a]) ⟹ x < a ⟹
  ins_list x (xs @ a # ys) = ins_list x xs @ (a#ys)"
by(auto simp: ins_list_sorted)

lemmas ins_list_simps = sorted_lems ins_list_sorted1 ins_list_sorted2

text‹Splay trees need two additional @{const ins_list} lemmas:›

lemma ins_list_Cons: "sorted (x # xs) ⟹ ins_list x xs = x # xs"
by (induction xs) auto

lemma ins_list_snoc: "sorted (xs @ [x]) ⟹ ins_list x xs = xs @ [x]"
by(induction xs) (auto simp add: sorted_mid_iff2)


subsection ‹Delete one occurrence of an element from a list:›

fun del_list :: "'a ⇒ 'a list ⇒ 'a list" where
"del_list x [] = []" |
"del_list x (a#xs) = (if x=a then xs else a # del_list x xs)"

lemma del_list_idem: "x ∉ elems xs ⟹ del_list x xs = xs"
by (induct xs) simp_all

lemma elems_del_list_eq:
  "distinct xs ⟹ elems (del_list x xs) = elems xs - {x}"
apply(induct xs)
 apply simp
apply (simp add: elems_eq_set)
apply blast
done

lemma sorted_del_list: "sorted xs ⟹ sorted(del_list x xs)"
apply(induction xs rule: sorted_wrt_induct)
apply auto
by (meson order.strict_trans sorted_Cons_iff)

lemma del_list_sorted: "sorted (xs @ a # ys) ⟹
  del_list x (xs @ a # ys) = (if x < a then del_list x xs @ a # ys else xs @ del_list x (a # ys))"
by(induction xs)
  (fastforce simp: sorted_lems sorted_Cons_iff elems_eq_set intro!: del_list_idem)+

text‹In principle, @{thm del_list_sorted} suffices, but the following
corollaries speed up proofs.›

corollary del_list_sorted1: "sorted (xs @ a # ys) ⟹ a ≤ x ⟹
  del_list x (xs @ a # ys) = xs @ del_list x (a # ys)"
by (auto simp: del_list_sorted)

corollary del_list_sorted2: "sorted (xs @ a # ys) ⟹ x < a ⟹
  del_list x (xs @ a # ys) = del_list x xs @ a # ys"
by (auto simp: del_list_sorted)

corollary del_list_sorted3:
  "sorted (xs @ a # ys @ b # zs) ⟹ x < b ⟹
  del_list x (xs @ a # ys @ b # zs) = del_list x (xs @ a # ys) @ b # zs"
by (auto simp: del_list_sorted sorted_lems)

corollary del_list_sorted4:
  "sorted (xs @ a # ys @ b # zs @ c # us) ⟹ x < c ⟹
  del_list x (xs @ a # ys @ b # zs @ c # us) = del_list x (xs @ a # ys @ b # zs) @ c # us"
by (auto simp: del_list_sorted sorted_lems)

corollary del_list_sorted5:
  "sorted (xs @ a # ys @ b # zs @ c # us @ d # vs) ⟹ x < d ⟹
   del_list x (xs @ a # ys @ b # zs @ c # us @ d # vs) =
   del_list x (xs @ a # ys @ b # zs @ c # us) @ d # vs" 
by (auto simp: del_list_sorted sorted_lems)

lemmas del_list_simps = sorted_lems
  del_list_sorted1
  del_list_sorted2
  del_list_sorted3
  del_list_sorted4
  del_list_sorted5

text‹Splay trees need two additional @{const del_list} lemmas:›

lemma del_list_notin_Cons: "sorted (x # xs) ⟹ del_list x xs = xs"
by(induction xs)(fastforce simp: sorted_Cons_iff)+

lemma del_list_sorted_app:
  "sorted(xs @ [x]) ⟹ del_list x (xs @ ys) = xs @ del_list x ys"
by (induction xs) (auto simp: sorted_mid_iff2)

end