Theory Variants

(*<*)
theory Variants
imports Abbrevs 
begin


lemma restrict_map_inverse: "m |` (dom m - X) = m |`(-X)"
  apply (rule ext)
  apply (auto simp add: restrict_map_def)
  done

lemma conj_assoc: "((P  Q)  X) = (P  Q  X)"
  by simp

(* Constructor markup for some datatypes *)
(* instr *)
notation (latex output)
Read ("latex‹\\constructor{Read}›")
notation (latex output)
Write ("latex‹\\constructor{Write}›")
notation (latex output)
RMW ("latex‹\\constructor{RMW}›")
notation (latex output)
Fence ("latex‹\\constructor{Fence}›")
notation (latex output)
Ghost ("latex‹\\constructor{Ghost}›")

(* memref *)
notation (latex output)
Writesb ("latex‹\\constructor{Write}›sb")
notation (latex output)
Readsb ("latex‹\\constructor{Read}›sb")
notation (latex output)
Progsb ("latex‹\\constructor{Prog}›sb")
notation (latex output)
Ghostsb ("latex‹\\constructor{Ghost}›sb")



(* expr *)
notation (latex output)
Const ("latex‹\\constructor{Const}›")
notation (latex output)
Mem ("latex‹\\constructor{Mem}›")
notation (latex output)
Tmp ("latex‹\\constructor{Tmp}›")
notation (latex output)
Unop ("latex‹\\constructor{Unop}›")
notation (latex output)
Binop ("latex‹\\constructor{Binop}›")

(* stmt *)
notation (latex output)
Skip ("latex‹\\constructor{Skip}›")
notation (latex output)
Assign ("latex‹\\constructor{Assign}›")
notation (latex output)
CAS ("latex‹\\constructor{CAS}›")
notation (latex output)
Seq ("latex‹\\constructor{Seq}›")
notation (latex output)
Cond ("latex‹\\constructor{Cond}›")
notation (latex output)
While ("latex‹\\constructor{While}›")
notation (latex output)
SGhost ("latex‹\\constructor{SGhost}›")
notation (latex output)
SFence ("latex‹\\constructor{SFence}›")

lemma sim_direct_config_def': "tssb d ts 
(tssb = (map (λ(p,is, θ,sb::unit,𝒟, 𝒪,). (p,is,θ,[],(),(),())) ts))"
apply (rule HOL.eq_reflection)
apply rule
apply  (erule sim_direct_config.cases)
apply  (clarsimp)
apply  (rule nth_equalityI)
apply   simp
apply  clarsimp
apply  (case_tac "ts!i")
apply  fastforce
apply (rule sim_direct_config.intros)
apply auto
done

ML @{term "(λ(p,is, θ,sb::unit,𝒟, 𝒪,). (p,is,θ,[],(),(),()))"}

lemma DRead: "(Read volatile a t # is,θ, x, m,ghst) 
               (is, θ (tm a), x, m, ghst)"
apply (cases ghst) 
apply (simp add: direct_memop_step.Read)
done

lemma DWriteNonVolatile:"
  (Write False a (D,f) A L R W#is, θ, x, m, ghst)  (is, θ, x, m(a := f θ), ghst)"
apply (cases ghst) 
apply (simp add: direct_memop_step.WriteNonVolatile)
done

lemma DWriteVolatile:
  "ghst = (𝒟, 𝒪, , 𝒮)  ghst' = (True, 𝒪  A - R, Map.empty, 𝒮 ⊕⇘WR ⊖⇘AL) 
    (Write True a (D,f) A L R W# is, θ, x, m, ghst)  (is, θ,  x, m(a:=f θ), ghst')"
by (simp add: direct_memop_step.WriteVolatile)

lemma DGhost:
  "ghst = (𝒟, 𝒪, , 𝒮)  ghst' = (𝒟, 𝒪  A - R, augment_rels (dom 𝒮) R , 𝒮 ⊕⇘WR ⊖⇘AL) 
    (Ghost A L R W# is, θ, x, m, ghst)  (is, θ,  x, m, ghst')"
by (simp add: direct_memop_step.Ghost)

lemma DRMWReadOnly:
  "¬ cond (θ(tm a)); ghst = (𝒟, 𝒪, , 𝒮); ghst'=(False, 𝒪, Map.empty,𝒮)  
   (RMW a t (D,f) cond ret A L R W # is, θ, x, m, ghst)  (is, θ(tm a),x,m, ghst')"
apply (simp add: direct_memop_step.RMWReadOnly)
done

lemma DRMWWrite:
  "cond (θ(tm a)); 
    θ' = θ(tret (m a) (f(θ(tm a))));
    m' = m(a:= f(θ(tm a)));
    ghst = (𝒟, 𝒪, , 𝒮); 
   ghst' = (False,𝒪  A - R, Map.empty, 𝒮 ⊕⇘WR ⊖⇘AL) 
    
   (RMW a t (D,f) cond ret A L R W# is, θ, x, m, ghst)  (is, θ',x, m' , ghst')"
apply (simp add: direct_memop_step.RMWWrite)
done

lemma VRead: "(Read volatile a t # is,θ, x, m,ghst) v
               (is, θ (tm a), x, m, ghst)"
apply (cases ghst) 
apply (simp add: virtual_memop_step.Read)
done

lemma VWriteNonVolatile:"
  (Write False a (D,f) A L R W#is, θ, x, m, ghst) v (is, θ, x, m(a := f θ), ghst)"
apply (cases ghst) 
apply (simp add: virtual_memop_step.WriteNonVolatile)
done

lemma VWriteVolatile:
  "ghst = (𝒟, 𝒪, , 𝒮)  ghst' = (True, 𝒪  A - R, , 𝒮 ⊕⇘WR ⊖⇘AL) 
    (Write True a (D,f) A L R W# is, θ, x, m, ghst) v (is, θ,  x, m(a:=f θ), ghst')"
by (simp add: virtual_memop_step.WriteVolatile)

lemma VRMWReadOnly:
  "¬ cond (θ(tm a)); ghst = (𝒟, 𝒪, , 𝒮); ghst'=(False, 𝒪,,𝒮)  
   (RMW a t (D,f) cond ret A L R W # is, θ, x, m, ghst) v (is, θ(tm a),x,m, ghst')"
apply (simp add: virtual_memop_step.RMWReadOnly)
done

lemma VFence:
  "ghst = (𝒟, 𝒪, , 𝒮)  ghst' = (False, 𝒪, , 𝒮) 
    (Fence# is, θ, x, m, ghst) v (is, θ,  x, m, ghst')"
by (simp add: virtual_memop_step.Fence)

lemma VGhost:
  "ghst = (𝒟, 𝒪, , 𝒮)  ghst' = (𝒟, 𝒪  A - R, , 𝒮 ⊕⇘WR ⊖⇘AL)  
    (Ghost A L R W# is, θ, x, m, ghst) v (is, θ,  x, m, ghst')"
by (simp add: virtual_memop_step.Ghost)

lemma VRMWWrite:
  "cond (θ(tm a)); 
    θ' = θ(tret (m a) (f(θ(tm a))));
    m' = m(a:= f(θ(tm a)));
    ghst = (𝒟, 𝒪, , 𝒮); 
   ghst' = (False,𝒪  A - R, , 𝒮 ⊕⇘WR ⊖⇘AL) 
    
   (RMW a t (D,f) cond ret A L R W# is, θ, x, m, ghst) v (is, θ',x, m' , ghst')"
apply (simp add: virtual_memop_step.RMWWrite)
done


lemma SafeWriteVolatile:
  "j < length 𝒪s. ij  a  𝒪s!j; a  read_only 𝒮;    
    j < length 𝒪s. ij   A   𝒪s!j = {};
    A  𝒪  dom 𝒮; L  A; R  𝒪; A  R = {}
   
    
   𝒪s,i(Write True a (D,f) A L R W# is, θ, m, 𝒟, 𝒪, 𝒮)"
apply (rule safe_direct_memop_state.WriteVolatile)
apply auto
done

lemma SafeDelayedWriteVolatile:
  "j < length 𝒪s. ij  a  (𝒪s!j  dom (ℛs!j)); a  read_only 𝒮;
  j < length 𝒪s. ij   A   (𝒪s!j  dom (ℛs!j)) = {};
    A  dom 𝒮  𝒪; L  A; R  𝒪; A  R = {}
   
    
   𝒪s,ℛs,i(Write True a (D,f) A L R W# is, θ, m, 𝒟, 𝒪, 𝒮)"
apply (rule safe_delayed_direct_memop_state.WriteVolatile)
apply auto
done


lemma SafeRMWReadOnly:
  "¬ cond (θ(tm a)); a  dom 𝒮  𝒪  
   𝒪s,i (RMW a t (D,f) cond ret A L R W# is, θ, m, 𝒟, 𝒪, 𝒮)"
apply (rule safe_direct_memop_state.RMWReadOnly)
apply auto
done

lemma SafeDelayedRMWReadOnly:
  "¬ cond (θ(tm a)); a  dom 𝒮  𝒪; 
   j < length 𝒪s. ij  (ℛs!j) a  Some False ― ‹no release of unshared address›
    
   𝒪s,ℛs,i(RMW a t (D,f) cond ret A L R W# is, θ, m, 𝒟, 𝒪, 𝒮)"
apply (rule safe_delayed_direct_memop_state.RMWReadOnly)
apply auto
done

lemma SafeRMWWrite:
  "cond (θ(tm a));  
    j < length 𝒪s. ij  a  𝒪s!j; a  read_only 𝒮;
    j < length 𝒪s. ij  A  𝒪s!j  = {};    
    A   𝒪  dom 𝒮; L  A; R  𝒪; A  R = {}
     
    
   𝒪s,i (RMW a t (D,f) cond ret A L R W# is, θ, m, 𝒟, 𝒪, 𝒮)"
apply (rule safe_direct_memop_state.RMWWrite)
apply auto
done

lemma SafeDelayedRMWWrite:
  "cond (θ(tm a)); a  dom 𝒮  𝒪;  
    j < length 𝒪s. ij  a  (𝒪s!j  dom (ℛs!j));a  read_only 𝒮;
    j < length 𝒪s. ij  A  (𝒪s!j  dom (ℛs!j))  = {};
    A  dom 𝒮  𝒪; L  A; R  𝒪; A  R = {}
     
    
   𝒪s,ℛs,i(RMW a t (D,f) cond ret A L R W# is, θ, m, 𝒟, 𝒪, 𝒮)"
apply (rule safe_delayed_direct_memop_state.RMWWrite)
apply auto
done

lemma  WritesbNonVolatile: 
  "(m, Writesb False a sop v A L R W# rs,𝒪,,𝒮) f (m(a := v), rs,𝒪,,𝒮)"
  apply (rule flush_step.Writesb)
  apply auto
  done

lemma WritesbVolatile: 
"𝒪'= 𝒪  A - R;  𝒮'=(𝒮 ⊕⇘WR ⊖⇘AL) 
  (m, Writesb True a sop v A L R W# rs,𝒪,,𝒮) f (m(a := v), rs,𝒪',Map.empty,𝒮')"
  apply (rule flush_step.Writesb)
  apply auto
  done

lemma Ghostsb: "𝒪'= 𝒪  A - R; ℛ'= augment_rels (dom 𝒮) R ; 𝒮'=𝒮 ⊕⇘WR ⊖⇘AL  
             (m, Ghostsb A L R W# rs,𝒪,,𝒮) f (m, rs,𝒪',ℛ',𝒮')"
by (simp add: flush_step.Ghost)

lemma  SBHRead: 
  "v = (case (buffered_val sb a) of Some v'  v' | None  m a);
   sb' = sb@[Readsb volatile a t v] 
   
   (Read volatile a t # is, θ, sb, m,ghst) sbh
          (is, θ (tv), sb', m,ghst)"
  apply (cases ghst)
  apply (cases "buffered_val sb a")
  apply (auto simp add: SBHReadBuffered SBHReadUnbuffered)
  done

lemma  SBRead: 
  "v = (case (buffered_val sb a) of Some v'  v' | None  m a)
   
   (Read volatile a t # is, θ, sb, m,ghst) sb
          (is, θ (tv), sb, m,ghst)"
  apply (cases ghst)
  apply (cases "buffered_val sb a")
  apply (auto simp add: SBReadBuffered SBReadUnbuffered)
  done

lemma  SBHReadBuffered': 
  "buffered_val sb a = Some v;
   sb' = sb@[Readsb volatile a t v] 
   
   (Read volatile a t # is, θ, sb, m, 𝒟, 𝒪,, 𝒮) sbh
          (is, θ (tv), sb', m, 𝒟, 𝒪,, 𝒮)"
  by (simp add: SBHReadBuffered)

lemma SBHReadUnbuffered': 
  "buffered_val sb a = None;
    sb' = sb@[Readsb volatile a t (m a)] 
   
   (Read volatile a t # is,θ, sb, m, 𝒟, 𝒪,, 𝒮) sbh
          (is,θ (tm a), sb', m, 𝒟, 𝒪,, 𝒮)"
by (simp add: SBHReadUnbuffered)

lemma SBHWriteNonVolatile':
  " sb'= sb@ [Writesb False a (D,f) (f θ) A L R W] 
   
   (Write False a (D,f) A L R W#is,θ, sb, m, ghst) sbh
          (is, θ, sb', m, ghst)"
by (cases ghst) (simp add: SBHWriteNonVolatile)

lemma SBWriteNonVolatile':
  " sb'= sb@ [Writesb False a (D,f) (f θ) A L R W] 
   
   (Write False a (D,f) A L R W#is,θ, sb, m, ghst) sb
          (is, θ, sb', m, ghst)"
by (cases ghst) (simp add: SBWriteNonVolatile)

lemma SBHWriteVolatile':
  "sb'= sb@[Writesb True a (D,f) (f θ) A L R W]; ghst = (𝒟, 𝒪, , 𝒮); ghst' = (True, 𝒪,, 𝒮)
    
   (Write True a (D,f) A L R W# is,θ, sb, m,ghst) sbh
         (is,θ, sb', m,ghst')"
by (simp add: SBHWriteVolatile)

lemma SBHGhost':
  "(Ghost A L R W# is, θ, sb, m, G) sbh
         (is, θ, sb@[Ghostsb A L R W], m, G)"
  by (cases G) (simp add: SBHGhost)


lemma SBWriteVolatile':
  "sb'= sb@[Writesb True a (D,f) (f θ) A L R W]
    
   (Write True a (D,f) A L R W# is,θ, sb, m,ghst) sb
         (is,θ, sb', m,ghst)"
by (cases ghst) (simp add: SBWriteVolatile)

lemma SBWrite':
  "sb'= sb@[Writesb volatile a (D,f) (f θ) A L R W]
    
   (Write volatile a (D,f) A L R W# is,θ, sb, m,ghst) sb
         (is,θ, sb', m,ghst)"
apply (cases volatile)
apply (auto intro: SBWriteVolatile' SBWriteNonVolatile')
done


lemma SBHRMWReadOnly':
  "¬ cond (θ(tm a)); ghst = (𝒟, 𝒪, , 𝒮); ghst' = (False, 𝒪, Map.empty,𝒮)  
   (RMW a t (D,f) cond ret A L R W# is, θ, [], m, ghst) sbh (is, θ(tm a),[], m, ghst')"
by (simp add: SBHRMWReadOnly)

lemma SBHRMWWrite':
  "cond (θ(tm a)); θ'=θ(tret (m a) (f(θ(tm a))));m'=m(a:= f(θ(tm a)));
   ghst = (𝒟, 𝒪,, 𝒮); ghst'=(False, 𝒪  A - R, Map.empty,𝒮 ⊕⇘WR ⊖⇘AL)  
   (RMW a t (D,f) cond ret A L R W# is, θ, [], m, ghst) sbh
         (is, θ',[], m', ghst')"
  by (simp add: SBHRMWWrite)

lemma SBRMWReadOnly':
  "¬ cond (θ(tm a)); θ'=θ(tm a)  
   (RMW a t (D,f) cond ret A L R W# is, θ, [], m, ghst) sb (is, θ',[], m, ghst)"
by (cases ghst) (simp add: SBRMWReadOnly)

lemma SBRMWWrite':
  "cond (θ(tm a)); θ'=θ(tret (m a) (f(θ(tm a))));m'=m(a:= f(θ(tm a))) 
    
   (RMW a t (D,f) cond ret A L R W# is, θ, [], m, ghst) sb
         (is, θ',[], m', ghst)"
  by (cases ghst) (simp add: SBRMWWrite)

lemma sim_config':
  "m = flush_all_until_volatile_write tssbh msbh;
    𝒮 = share_all_until_volatile_write tssbh 𝒮sbh;
    length tssbh = length ts; 
    i < length tssbh. 
           let (psbh, issbh, θsbh, sb, 𝒟sbh, 𝒪sbh,sbh) = tssbh!i;
               execs = takeWhile (Not  is_volatile_Writesb) sb;
               suspends = dropWhile (Not  is_volatile_Writesb) sb
            in  is 𝒟. instrs suspends @ issbh = is @ prog_instrs suspends 
                    𝒟sbh = (𝒟  outstanding_refs is_volatile_Writesb sb  {}) 
                ts!i = (hd_prog psbh suspends, 
                        is,
                        θsbh |` (dom θsbh - read_tmps suspends),(),
                        𝒟,  
                        acquired True execs 𝒪sbh,
                        release execs (dom 𝒮sbh) sbh)
    
     
     (tssbh,msbh,𝒮sbh)  (ts,m,𝒮)"
apply (rule sim_config.intros)
apply (simp_all add: Let_def)
done

lemma  AssignAddr':
  "sop. a  Tmp sop; a'=Tmp (eval_expr t a); t'= t + used_tmps a; is=issue_expr t a  
   θ (Assign volatile a e A L R W, t) s 
         ((Assign volatile a' e A L R W, t'),is)"
  by (simp add: AssignAddr)


lemma  Assign':
  "D  dom θ; is= issue_expr t e@[Write volatile (a θ) (eval_expr t e) (A θ) (L θ) (R θ) (W θ)]  
   θ (Assign volatile (Tmp (D,a)) e A L R W, t) s 
         ((Skip, t + used_tmps e), is)"
  by (simp add: Assign)


lemma CASAddr':
  "sop. a  Tmp sop; a'=(Tmp (eval_expr t a));t'=t + used_tmps a; is=issue_expr t a  
   θ (CAS a ce se A L R W, t) s 
         ((CAS a' ce se A L R W, t'), is)"
  by (simp add: CASAddr)

lemma CASComp':
  "sop. ce  Tmp sop;ce'=(Tmp (eval_expr t ce));t'=t + used_tmps ce; is= issue_expr t ce  
   θ (CAS (Tmp a) ce se A L R W, t) s 
         ((CAS (Tmp a) ce' se A L R W, t'), is)"
  by (cases a) (simp add: CASComp)
  
lemma CAS':
  "Da  dom θ; Dc  dom θ; eval_expr t se  = (D,f);t'=(t + used_tmps se); 
   cond = (λθ. the (θ t') = c θ);
   ret = (λv1 v2. v1);
   is = issue_expr t se@
           [RMW (a θ) t' (D,f) cond ret 
            (A θ) (L θ) (R θ) (W θ) ]  
   
   θ (CAS (Tmp (Da,a)) (Tmp (Dc,c)) se A L R W, t) s 
         ((Skip, Suc t'),is )"
  by (simp add: CAS)


lemma SCond':
  "sop. e  Tmp sop  e'= (Tmp (eval_expr t e))  t'=t + used_tmps e  is=issue_expr t e
   
   θ (Cond e s1 s2, t) s 
    ((Cond e' s1 s2, t'), is)"
  by (simp add: Cond)

lemma SWhile':
  "s'= (Cond e (Seq s (While e s)) Skip) 
   θ (While e s, t) s ((s', t),[])"
  by (simp add: stmt_step.While)


theorem (in xvalid_program) simulation_hol:
  "(tssbh,msbh,𝒮sbh) sbh (tssbh',msbh',𝒮sbh') 
   (tssbh,msbh,𝒮sbh)  (ts,m,𝒮)  safe_reach_direct safe_delayed (ts, m, 𝒮) 
   invariant tssbh 𝒮sbh msbh 
  invariant tssbh' 𝒮sbh' msbh' 
           (ts' 𝒮' m'. (ts,m,𝒮) d* (ts',m',𝒮')  (tssbh',msbh',𝒮sbh')  (ts',m',𝒮'))"
  apply clarify
  apply (drule simulation')
  by auto

theorem (in xvalid_program_progress) store_buffer_execution_result_sequential_consistent'_hol:
"(tssb,m,x) sb* (tssb',m',x') 
empty_store_buffers tssb' 
tssb d ts 
initialv ts 𝒮 valid 
safe_reach_virtual safe_free_flowing (ts,m,𝒮) 

(ts' 𝒮'. 
          (ts,m,𝒮) v* (ts',m',𝒮')  tssb' d ts')"
  apply clarify
  apply (drule store_buffer_execution_result_sequential_consistent')
  apply auto
  done

end
(*>*)