Theory Update_Map_Entries

theory Update_Map_Entries
  imports
    "Finite-Map-Extras.Finite_Map_Extras"
begin

section ‹Updates to map entries›

text ‹This section defines functions used to update entries in a finite map. These are used to
      construct and update the index trees.›

fun update_entry :: ‹('a, 'b) fmap ⇒ 'a ⇒ ('b ⇒ 'b) ⇒ 'b ⇒ ('a, 'b) fmap› where
  ‹update_entry m k f v = m(k $$:= f (case m $$ k of None ⇒ v | Some v0 ⇒ v0))›

lemma update_entry_preservation: ‹∀x ∈ fmran' m. P x ⟹
                                  ∀v0. m $$ k = Some v0 ⟶ P (f v0) ⟹
                                  P (f v) ⟹
                                  ∀x ∈ fmran' (update_entry m k f v). P x›
  by (smt (verit, best) fmlookup_ran'_iff fmupd_lookup option.case_eq_if option.collapse option.sel update_entry.simps)

lemma update_entry_ran:
  assumes ‹m $$ k = None›
    shows ‹fmran' (m(k $$:= v)) = insert v (fmran' m)›
proof (rule set_eqI)
  show ‹⋀x. (x ∈ fmran' (m(k $$:= v))) = (x ∈ insert v (fmran' m))›
    by (metis assms fmlookup_ran'_iff fmupd_lookup insert_iff option.sel option.simps(3))
qed

fun update_entries :: ‹('a, 'b) fmap ⇒ 'a list ⇒ ('a ⇒ 'b ⇒ 'b) ⇒ 'b ⇒ ('a, 'b) fmap› where
  ‹update_entries m [] f v = m›
| ‹update_entries m (k # ks) f v = update_entries (update_entry m k (f k) v) ks f v›

lemma update_entries_dom: ‹fmdom' (update_entries m ls f v) = set ls ∪ fmdom' m›
  by (induction ls arbitrary: m) auto

lemma updt_entries_some:
  assumes ‹k ∉ set ks›
      and ‹m $$ k = Some v0›
    shows ‹update_entries m (k # ks) f v $$ k = Some (f k v0)›
  using assms
proof (induction ks arbitrary: m)
  case Nil
  then show ?case
    by simp 
next
  case (Cons _ _)
  then show ?case
    by (smt (z3) fmupd_lookup fmupd_reorder_neq list.sel(1) list.sel(3) list.set_intros(1) list.set_intros(2) update_entries.simps(2) update_entry.simps) 
qed

lemma upd_entries_some:
  assumes ‹distinct ks›
      and ‹k ∈ set ks›
      and ‹m $$ k = Some v0›
    shows ‹update_entries m ks f v $$ k = Some (f k v0)›
  using assms
proof (induction ks arbitrary: m)
  case Nil
  then show ?case
    by simp 
next
  case (Cons a ks)
  then show ?case
    by (metis distinct.simps(2) fmupd_lookup set_ConsD updt_entries_some update_entries.simps(2) update_entry.simps) 
qed

lemma rew_ran_upd_entries_none:
  assumes ‹distinct ls›
      and ‹∀k∈set ls. m $$ k = None›
    shows ‹fmran' (update_entries m ls f v) = (λy. f y v) ` set ls ∪ fmran' m›
  using assms
proof (induction ls arbitrary: m)
  case Nil
  then show ?case 
    by simp
next
  case (Cons a ls)
  assume distinct_ls: ‹distinct (a # ls)›
  assume none_m_ls: ‹∀k∈set (a # ls). m $$ k = None›
  have ‹m $$ a = None› and ‹∀k∈set ls. m $$ k = None›
    using none_m_ls by auto
  let ?m' = ‹m(a $$:= f a v)›
  have ‹fmran' (update_entries m (a # ls) f v) = fmran' (update_entries ?m' ls f v)›
    by (simp add: none_m_ls)
  have IH: ‹fmran' (update_entries ?m' ls f v) = (λy. f y v) ` set ls ∪ fmran' ?m'›
    by (metis Cons.IH distinct.simps(2) distinct_ls fmupd_lookup none_m_ls set_subset_Cons subsetD)
  have ‹fmran' ?m' = insert (f a v) (fmran' m)›
    by (simp add: ‹m $$ a = None› update_entry_ran)
  show ‹fmran' (update_entries m (a # ls) f v) = (λy. f y v) ` set (a # ls) ∪ fmran' m›
    by (simp add: IH ‹m $$ a = None› update_entry_ran)
qed

fun update_or_remove_entry :: ‹('a, 'b) fmap ⇒ 'a ⇒ ('b ⇒ 'b option) ⇒ ('a, 'b) fmap› where
  ‹update_or_remove_entry m k f = (case m $$ k of
                                    None ⇒ m
                                  | Some v ⇒ (case f v of
                                                None ⇒ fmdrop k m
                                              | Some v0 ⇒ m(k $$:= v0)))›

lemma update_or_remove_entry_preservation: ‹∀x ∈ fmran' m. P x ⟹
                                            ∀v0 y. m $$ k = Some v0 ∧ f v0 = Some y ⟶ P y ⟹
                                            ∀x ∈ fmran' (update_or_remove_entry m k f). P x›
  by (smt (verit, best) fmlookup_drop fmlookup_ran'_iff fmupd_lookup option.case_eq_if option.collapse update_or_remove_entry.simps)

lemma update_or_remove_entry_dom: ‹fmdom' (update_or_remove_entry m k f) ⊆ fmdom' m›
  by (simp add: fmlookup_dom'_iff option.case_eq_if)

fun update_or_remove_entries :: ‹('a, 'b) fmap ⇒ 'a list ⇒ ('a ⇒ 'b ⇒ 'b option) ⇒ ('a, 'b) fmap› where
  ‹update_or_remove_entries m [] f = m›
| ‹update_or_remove_entries m (k # ks) f = update_or_remove_entries (update_or_remove_entry m k (f k)) ks f›

lemma update_or_remove_entries_dom : ‹fmdom' (update_or_remove_entries m ks f) ⊆ fmdom' m›
proof (induction ks arbitrary: m)
  case Nil
  then show ?case by simp
next
  case (Cons _ _)
  then show ?case
    by (metis dual_order.trans update_or_remove_entries.simps(2) update_or_remove_entry_dom)
qed 

lemma update_or_remove_entries_hd:
  assumes ‹k ∉ set ks›
      and ‹m $$ k = Some v›
    shows ‹update_or_remove_entries m (k # ks) f $$ k = f k v›
  using assms proof (induction ks arbitrary: m)
  case Nil
  then show ?case 
    by (simp add: option.case_eq_if) 
next
  case (Cons a ks)
  then show ?case
  proof (cases ‹f k v›)
    case None
    then show ?thesis using Cons 
      by (metis (mono_tags, lifting) fmdom'_drop fmdom'_notD option.case_eq_if option.distinct(1) option.sel subset_Diff_insert update_or_remove_entries.simps(2) update_or_remove_entries_dom update_or_remove_entry.simps)  
  next
    case (Some v)
    then show ?thesis
    proof (cases ‹m $$ a›)
      case None
      then show ?thesis 
        using Cons.IH Cons.prems(1) Cons.prems(2) Some by force
    next
      case s1:(Some u)
      then show ?thesis
      proof (cases ‹f a u›)
        case None
        then show ?thesis using s1 Some Cons  by (clarsimp simp add: fmdrop_fmupd)
      next
        case s2: (Some x)
        then show ?thesis using s1 Some Cons
          by (simp, metis fmupd_lookup fmupd_reorder_neq)
      qed
    qed
  qed
qed

lemma update_or_remove_entries_some: 
  assumes ‹distinct ks›
      and ‹k ∈ set ks›
      and ‹m $$ k = Some v›
    shows ‹update_or_remove_entries m ks f $$ k = f k v›
  using assms
proof (induction ks arbitrary: m)
  case Nil
  then show ?case 
    by simp
next
  case (Cons a ks)
  then show ?case
  proof (cases ‹m $$ a›)
    case None
    then show ?thesis using Cons
      by auto
  next
    case (Some v0)
    then show ?thesis using Cons  
      by (smt (verit, del_insts) distinct.simps(2) fmlookup_drop fmupd_lookup option.case_eq_if set_ConsD update_or_remove_entries.simps(2) update_or_remove_entry.simps update_or_remove_entries_hd) 
  qed
qed

end