Theory Occurrences

section Occurrences

text ‹This theory contains all of the definitions and laws required for reasoning about
  identifiers that occur in the data structures of the concurrent revisions model.›

theory Occurrences
  imports Data
begin

subsection Definitions

subsubsection ‹Revision identifiers›

definition RIDS :: "('r,'l,'v) store  'r set" where
  "RIDS σ   (RIDV ` ran σ)"

definition RIDL :: "('r,'l,'v) local_state  'r set" where
  "RIDL s  case s of (σ, τ, e)  RIDS σ  RIDS τ  RIDE e"

definition RIDG :: "('r,'l,'v) global_state  'r set" where
  "RIDG s  dom s   (RIDL ` ran s)"

subsubsection ‹Location identifiers›

definition LIDS :: "('r,'l,'v) store  'l set" where
  "LIDS σ  dom σ   (LIDV ` ran σ)"

definition LIDL :: "('r,'l,'v) local_state  'l set" where
  "LIDL s  case s of (σ, τ, e)  LIDS σ  LIDS τ  LIDE e"

definition LIDG :: "('r,'l,'v) global_state  'l set" where
  "LIDG s   (LIDL ` ran s)"

subsection ‹Inference rules›

subsubsection ‹Introduction and elimination rules›

lemma RIDSI [intro]: "σ l = Some v  r  RIDV v  r  RIDS σ"
  by (auto simp add: RIDS_def ran_def)

lemma RIDSE [elim]: "r  RIDS σ  (l v. σ l = Some v  r  RIDV v  P)  P"
  by (auto simp add: RIDS_def ran_def)

lemma RIDLI [intro, consumes 1]:
  assumes "s = (σ, τ, e)"
  shows
    "r  RIDS σ  r  RIDL s"
    "r  RIDS τ  r  RIDL s"
    "r  RIDE e  r  RIDL s"
  by (auto simp add: RIDL_def assms)

lemma RIDLE [elim]:
  " r  RIDL s; (σ τ e. s = (σ, τ, e)  (r  RIDS σ  P)  (r  RIDS τ  P)  (r  RIDE e  P)  P)  P   P" 
  by (cases s) (auto simp add: RIDL_def)

lemma RIDGI [intro]:
  "s r = Some v  r  RIDG s"
  "s r' = Some ls  r  RIDL ls  r  RIDG s"
   apply (simp add: RIDG_def domI)
  by (metis (no_types, lifting) RIDG_def UN_I UnI2 ranI)

lemma RIDGE [elim]:
  assumes
    "r  RIDG s"
    "r  dom s  P"
    "r' ls. s r' = Some ls  r  RIDL ls  P"
  shows P
  using assms by (auto simp add: RIDG_def ran_def)

lemma LIDSI [intro]:
  "σ l = Some v  l  LIDS σ"
  "σ l' = Some v  l  LIDV v  l  LIDS σ"
  by (auto simp add: LIDS_def ran_def)

lemma LIDSE [elim]:
  assumes 
    "l  LIDS σ"
    "l  dom σ  P"
    "l' v. σ l' = Some v  l  LIDV v  P"
  shows P
  using assms by (auto simp add: LIDS_def ran_def)

lemma LIDLI [intro]:
  assumes "s = (σ, τ, e)"
  shows
    "r  LIDS σ  r  LIDL s" 
    "r  LIDS τ  r  LIDL s" 
    "r  LIDE e  r  LIDL s"
  by (auto simp add: LIDL_def assms)

lemma LIDLE [elim]:
  " l  LIDL s; (σ τ e. s = (σ, τ, e)  (l  LIDS σ  P)  (l  LIDS τ  P)  (l  LIDE e  P)  P)  P   P" 
  by (cases s) (auto simp add: LIDL_def)

lemma LIDGI [intro]: "s r = Some ls  l  LIDL ls  l  LIDG s"
  by (auto simp add: LIDG_def LIDL_def ran_def)

lemma LIDGE [elim]: "l  LIDG s  (r ls. s r = Some ls  l  LIDL ls  P)  P"
  by (auto simp add: LIDG_def ran_def)

subsubsection ‹Distributive laws›

lemma ID_distr_completion [simp]: 
  "RIDE ([e]) = RIDC   RIDE e"
  "LIDE ([e]) = LIDC   LIDE e"
  by (induct rule: plug.induct) auto

lemma ID_restricted_store_subset_store: 
  "RIDS (σ(l := None))  RIDS σ"
  "LIDS (σ(l := None))  LIDS σ"
proof -
  show "RIDS (σ(l := None))  RIDS σ"
  proof (rule subsetI)
    fix r
    assume "r  RIDS (σ(l := None))"
    then obtain l' v where "(σ(l := None)) l' = Some v" and r_v: "r  RIDV v" by blast
    have "l'  l" using (σ(l := None)) l' = Some v by auto
    hence "σ l' = Some v" using (σ(l := None)) l' = Some v by auto
    thus "r  RIDS σ" using r_v by blast
  qed
next
  show "LIDS (σ(l := None))  LIDS σ"
  proof (rule subsetI)
    fix l'
    assume "l'  LIDS (σ(l := None))"
    hence "l'  dom (σ(l := None))  (l'' v. (σ(l := None)) l'' = Some v  l'  LIDV v)" by blast
    thus "l'  LIDS σ"
    proof (rule disjE)
      assume "l'  dom (σ(l := None))"
      thus "l'  LIDS σ" by auto
    next
      assume "l'' v. (σ(l := None)) l'' = Some v  l'  LIDV v"
      then obtain l'' v where "(σ(l := None)) l'' = Some v" and l'_in_v: "l'  LIDV v" by blast
      hence "σ l'' = Some v" by (cases "l = l''", auto)
      thus "l'  LIDS σ" using l'_in_v by blast
    qed
  qed
qed

lemma in_restricted_store_in_unrestricted_store [intro]: 
  "r  RIDS (σ(l := None))  r  RIDS σ"
  "l'  LIDS (σ(l := None))  l'  LIDS σ" 
  by (meson contra_subsetD ID_restricted_store_subset_store)+

lemma in_restricted_store_in_updated_store [intro]: 
  "r  RIDS (σ(l := None))  r  RIDS (σ(l  v))" 
  "l'  LIDS (σ(l := None))  l'  LIDS (σ(l  v))"
proof -
  assume "r  RIDS (σ(l := None))"
  hence "r  RIDS (σ |` (- {l}))" by (simp add: restrict_complement_singleton_eq)
  hence "r  RIDS (σ(l  v) |` (- {l}))" by simp
  hence "r  RIDS (σ(l := Some v, l := None))" by (simp add: restrict_complement_singleton_eq)
  thus "r  RIDS (σ(l  v))" by blast
next
  assume "l'  LIDS (σ(l := None))"
  hence "l'  LIDS (σ |` (- {l}))" by (simp add: restrict_complement_singleton_eq)
  hence "l'  LIDS (σ(l  v) |` (- {l}))" by simp
  hence "l'  LIDS (σ(l := Some v, l := None))" by (simp add: restrict_complement_singleton_eq)
  thus "l'  LIDS (σ(l  v))" by blast
qed

lemma ID_distr_store [simp]:
  "RIDS (τ(l  v)) = RIDS (τ(l := None))  RIDV v"
  "LIDS (τ(l  v)) = insert l (LIDS (τ(l := None))  LIDV v)"
proof -
  show "RIDS (τ(l  v)) = RIDS (τ(l := None))  RIDV v"
  proof (rule set_eqI, rule iffI)
    fix r
    assume "r  RIDS (τ(l  v))"
    then obtain l' v' where "(τ(l  v)) l' = Some v'" "r  RIDV v'" by blast
    thus "r  RIDS (τ(l := None))  RIDV v" by (cases "l' = l") auto
  qed auto
next
  show "LIDS (τ(l  v)) = insert l (LIDS (τ(l := None))  LIDV v)"
  proof (rule set_eqI, rule iffI)
    fix l'
    assume "l'  LIDS (τ(l  v))"
    hence "l'  dom (τ(l  v))  (l'' v'. (τ(l  v)) l'' = Some v'  l'  LIDV v')" by blast
    thus "l'  insert l (LIDS (τ(l := None))  LIDV v)"
    proof (rule disjE)
      assume "l'  dom (τ(l  v))"
      thus "l'  insert l (LIDS (τ(l := None))  LIDV v)" by (cases "l' = l") auto
    next
      assume "l'' v'. (τ(l  v)) l'' = Some v'  l'  LIDV v'"
      then obtain l'' v' where "(τ(l  v)) l'' = Some v'" "l'  LIDV v'" by blast
      thus "l'  insert l (LIDS (τ(l := None))  LIDV v)" by (cases "l = l''") auto
    qed
  qed auto
qed

lemma ID_distr_local [simp]: 
  "LIDL (σ,τ,e) = LIDS σ  LIDS τ  LIDE e"
  "RIDL (σ,τ,e) = RIDS σ  RIDS τ  RIDE e"
  by (simp add: LIDL_def RIDL_def)+

lemma ID_restricted_global_subset_unrestricted:
  "LIDG (s(r := None))  LIDG s"
  "RIDG (s(r := None))  RIDG s"
proof -
  show "LIDG (s(r := None))  LIDG s"
  proof (rule subsetI)
    fix l
    assume "l  LIDG (s(r := None))"
    then obtain r'' ls where "(s(r := None)) r'' = Some ls" and l_in_ls: "l  LIDL ls" by blast
    have "r''  r " using (s(r := None)) r'' = Some ls by auto
    hence "s r'' = Some ls" using (s(r := None)) r'' = Some ls by auto
    thus "l  LIDG s" using l_in_ls by blast
  qed
next
  show "RIDG (s(r := None))  RIDG s"
  proof (rule subsetI)
    fix r'
    assume "r'  RIDG (s(r := None))"
    hence "r'  dom (s(r := None))  (r'' ls. (s(r := None)) r'' = Some ls  r'  RIDL ls)" by blast
    thus "r'  RIDG s"
    proof (rule disjE)
      assume "r'' ls. (s(r := None)) r'' = Some ls  r'  RIDL ls"
      then obtain r'' ls where "(s(r := None)) r'' = Some ls" and r'_in_ls: "r'  RIDL ls" by blast
      have neq: "r''  r" using (s(r := None)) r'' = Some ls by auto
      hence "s r'' = Some ls" using (s(r := None)) r'' = Some ls by auto
      thus "r'  RIDG s" using r'_in_ls by blast
    qed auto
  qed
qed

lemma in_restricted_global_in_unrestricted_global [intro]: 
  "r'  RIDG (s(r := None))  r'  RIDG s"
  "l  LIDG (s(r := None))  l  LIDG s"
  by (simp add: ID_restricted_global_subset_unrestricted rev_subsetD)+

lemma in_restricted_global_in_updated_global [intro]: 
  "r'  RIDG (s(r := None))  r'  RIDG (s(r  ls))" 
  "l  LIDG (s(r := None))  l  LIDG (s(r  ls))"
proof -
  assume "r'  RIDG (s(r := None))"
  hence "r'  RIDG (s |` (- {r}))" by (simp add: restrict_complement_singleton_eq)
  hence "r'  RIDG (s(r  ls) |` (- {r}))" by simp
  hence "r'  RIDG (s(r := Some ls, r := None))" by (simp add: restrict_complement_singleton_eq)
  thus "r'  RIDG (s(r  ls))" by blast
next
  assume "l  LIDG (s(r := None))"
  hence "l  LIDG (s |` (- {r}))" by (simp add: restrict_complement_singleton_eq)
  hence "l  LIDG (s(r  ls) |` (- {r}))" by simp
  hence "l  LIDG (s(r := Some ls, r := None))" by (simp add: restrict_complement_singleton_eq)
  thus "l  LIDG (s(r  ls))" by blast
qed

lemma ID_distr_global [simp]:
  "RIDG (s(r  ls)) = insert r (RIDG (s(r := None))  RIDL ls)"
  "LIDG (s(r  ls)) = LIDG (s(r := None))  LIDL ls"
proof -
  show "LIDG (s(r  ls)) = LIDG (s(r := None))  LIDL ls"
  proof (rule set_eqI)
    fix l
    show "(l  LIDG (s(r  ls))) = (l  LIDG (s(r := None))  LIDL ls)"
    proof (rule iffI)
      assume "l  LIDG (s(r  ls))"
      from this obtain r' ls' where "(s(r  ls)) r' = Some ls'" "l  LIDL ls'" by auto
      thus "l  LIDG (s(r := None))  LIDL ls" by (cases "r = r'") auto
    qed auto
  qed
  show "RIDG (s(r  ls)) = insert r (RIDG (s(r := None))  RIDL ls)"
  proof (rule set_eqI)
    fix r'
    show "(r'  RIDG (s(r  ls))) = (r'  insert r (RIDG (s(r := None))  RIDL ls))"
    proof (rule iffI, clarsimp)
      assume r'_g: "r'  RIDG (s(r  ls))" and neq: "r'  r" and r'_l: "r'  RIDL ls"
      show "r'  RIDG (s(r := None))"
      proof (rule RIDGE[OF r'_g])
        assume "r'  dom (s(r  ls))"
        thus ?thesis by (simp add: RIDG_def neq)
      next
        fix ls' r''
        assume r'_in_range:"(s(r  ls)) r'' = Some ls'" "r'  RIDL ls'"
        thus ?thesis by (cases "r'' = r") (use neq r'_l in auto)
      qed
    qed auto
  qed
qed

lemma restrictions_inwards [simp]:
  "x  x'  f(x := Some y, x' := None) = (f(x':= None, x := Some y))"
  by (rule fun_upd_twist)

subsubsection ‹Miscellaneous laws›

lemma ID_combination_subset_union [intro]:
  "RIDS (σ;;τ)  RIDS σ  RIDS τ"
  "LIDS (σ;;τ)  LIDS σ  LIDS τ"
proof -
  show "RIDS (σ;;τ)  RIDS σ  RIDS τ"
  proof (rule subsetI)
    fix r
    assume "r  RIDS (σ;;τ)"
    from this obtain l v where "(σ;;τ) l = Some v" and "r  RIDV v" by blast
    thus "r  RIDS σ  RIDS τ" by (cases "l  dom τ") auto
  qed
  show "LIDS (σ;;τ)  LIDS σ  LIDS τ"
  proof (rule subsetI)
    fix l
    assume "l  LIDS (σ;;τ)"
    hence "l  dom (σ;;τ)  (l' v. (σ;;τ) l' = Some v  l  LIDV v)" by blast
    thus "l  LIDS σ  LIDS τ"
    proof (rule disjE)
      assume "l  dom (σ;;τ)"
      thus "l  LIDS σ  LIDS τ" by (cases "l  dom τ") auto
    next 
      assume "l' v. (σ;;τ) l' = Some v  l  LIDV v"
      from this obtain l' v where "(σ;;τ) l' = Some v" "l  LIDV v" by blast
      thus "l  LIDS σ  LIDS τ" by (cases "l'  dom τ") auto
    qed
  qed
qed

lemma in_combination_in_component [intro]: 
  "r  RIDS (σ;;τ)  r  RIDS σ  r  RIDS τ"
  "r  RIDS (σ;;τ)  r  RIDS τ  r  RIDS σ"
  "l  LIDS (σ;;τ)  l  LIDS σ  l  LIDS τ"
  "l  LIDS (σ;;τ)  l  LIDS τ  l  LIDS σ"
  by (meson Un_iff ID_combination_subset_union subsetCE)+

lemma in_mapped_in_component_of_combination [intro]:
  assumes 
    maps_to_v: "(σ;;τ) l = Some v" and
    l'_in_v: "l'  LIDV v"
  shows
    "l'  LIDS τ  l'  LIDS σ"
    "l'  LIDS σ  l'  LIDS τ"
  by (metis LIDSI(2) combine.simps l'_in_v maps_to_v)+

lemma elim_trivial_restriction [simp]: "l  LIDS τ  (τ(l := None)) = τ" 
  by (simp add: LIDS_def domIff fun_upd_idem)

lemma ID_distr_global_conditional:
  "s r = Some ls  RIDG s = insert r (RIDG (s(r := None))  RIDL ls)"
  "s r = Some ls  LIDG s = LIDG (s(r := None))  LIDL ls"
proof -
  assume "s r = Some ls"
  hence s_as_upd: "s = (s(r  ls))" by (simp add: fun_upd_idem)
  show "RIDG s = insert r (RIDG (s(r := None))  RIDL ls)" by (subst s_as_upd, simp)
  show "LIDG s = LIDG (s(r := None))  LIDL ls" by (subst s_as_upd, simp)
qed

end