Section SimProofs.
Context {asmSem : Semantics}
{semAxioms : SemAxioms}
{semDet : SemDet}
{CI: CoreInj}
{initU : seq nat}.
Existing Instance OrdinalPool.OrdinalThreadPool.
Existing Instance DryHybridMachine.DryHybridMachineSig.
Existing Instance dryFineMach.
Existing Instance FineDilMem.
Existing Instance dryCoarseMach.
Existing Instance HybridFineMachine.scheduler.
Existing Instance HybridCoarseMachine.scheduler.
Existing Instance FineDilMem.
Existing Instance HybridCoarseMachine.DilMem.
Notation csafe := (HybridCoarseMachine.csafe).
Notation sim_fail_def := (@sim_fail_def _ _ initU).
Notation threadStep := (HybridMachineSig.threadStep).
Notation cmachine_step := ((corestep (AsmContext.coarse_semantics initU))).
Notation fmachine_step := ((corestep (AsmContext.fine_semantics initU))).
Notation "cnt '$' m '@'  'I'" := (getStepType cnt m Internal) (at level 80).
Notation "cnt '$' m '@'  'E'" := (getStepType cnt m Concurrent) (at level 80).
Notation "cnt '$' m '@'  'S'" := (getStepType cnt m Suspend) (at level 80).
Notation internal_step := (internal_step).
Notation internal_execution := (internal_execution).
Notation fyield := (@yield HybridFineMachine.scheduler).
Notation fdiluteMem := (@diluteMem FineDilMem).
Notation sim_internal_def := (@sim_internal_def _ _ initU).
Notation sim_suspend_def := (@sim_suspend_def _ _ initU).
Notation sim_external_def := (@sim_external_def _ _ initU).
(** NOTE: this is only needed to find out if a block is in the codomain of f, which is decidable *)
Hypothesis em : excluded_middle.
(** Function that projects the angel through a memory injection to compute a new angel *)
Definition projectAngel (f : memren) (deltaMap : delta_map) : delta_map :=
Maps.PTree.fold (fun acc b bperm =>
match f b with
| Some b' =>
Maps.PTree.set b' bperm acc
| None =>
acc end)
deltaMap (Maps.PTree.empty _).
(* Blocks that are not mapped by f are set to empty permission. This makes the invariant preservation easier. *)
Definition projectMap (f : memren) (pmap : access_map) : access_map :=
(pmap#1, Maps.PTree.fold (fun acc b bperm =>
match f b with
| Some b' =>
Maps.PTree.set b' bperm acc
| None =>
acc end)
pmap.2 (Maps.PTree.empty _)).
(** Proofs about [internal_execution] and [internal_step] *)
Existing Instance HybridCoarseMachine.DilMem.
Existing Instance HybridCoarseMachine.scheduler.
Notation corestepN := (corestepN (AsmContext.coarse_semantics initU)).
(*NOTE: how can we have tactics inside sections? *)
(** Solves absurd cases from fine-grained internal steps *)
Ltac absurd_internal Hstep :=
inversion Hstep; try inversion Htstep; subst; simpl in *; Tactics.pf_cleanup;
try match goal with
| [H: Some _ = Some _ |- _] => inversion H; subst
end; Tactics.pf_cleanup;
repeat match goal with
| [H: OrdinalPool.getThreadC ?Pf = _, Hint: ?Pf$_ @ I |- _] =>
unfold getStepType, ctlType in Hint; simpl in *;
rewrite H in Hint; simpl in Hint
| [H: getThreadC ?Pf = _, Hint: ?Pf$_ @ I |- _] =>
unfold getStepType, ctlType in Hint; simpl in *;
rewrite H in Hint; simpl in Hint
| [H1: match ?Expr with _ => _ end = _,
H2: ?Expr = _ |- _] => rewrite H2 in H1
(*     | [H1: is_true (isSome (halted ?Sem ?C)), H2: match at_external _ _ _ with _ => _ end = _ |- _] => destruct (at_external_halted_excl Sem C) as [Hext | Hcontra]; [rewrite Hext in H2; destruct (halted Sem C) eqn:Hh; [discriminate | by exfalso] | rewrite Hcontra in H1; by exfalso]*)
end; try discriminate; try (exfalso; by auto).
(** Starting from a well-defined state, an internal execution retains the well-definedeness for any injection that corresponds to the domain of the new memory. *)
(** Profs about [mem_obs_eq] and [weak_mem_obs_eq] *)
(** ** Proofs of internal step safety and simulation*)
(** *** Lemmas about renaming pools [fpool]*)
Definition updateFP {tpc i} (cnti: containsThread tpc i)
(fp: fpool tpc) (f' : memren) :=
fun j cntj => if i == j then f' else fp j cntj.
Definition addFP {tp} (fp: fpool tp) (f': memren) vf arg pmap :
fpool (addThread tp vf arg pmap).
Opaque OrdinalPool.containsThread.
(** *** Safety and simulation lemmas *)
Opaque containsThread getThreadC getThreadR.

Lemma mem_obs_eq_execution:
forall tp1 tp2 tp1' m1 m2 m1' j xs f fg
(pf1j: containsThread tp1 j)
(pf1j': containsThread tp1' j)
(Hcomp1: mem_compatible tp1 m1)
(Hcomp1': mem_compatible tp1' m1')
(Hinv: invariant tp1')
(Hfg: forall b1 b2, fg b1 = Some b2 -> b1 = b2)
(Hge_wd: ge_wd fg the_ge)
(Hge_incr: ren_incr fg f)
(Hvalid_mem: valid_mem m1)
(Htp_wd: tp_wd f tp1)
(Hcode_eq: ctl_inj f (getThreadC pf1j) (getThreadC pf1j'))
(Hmem_obs_eq: mem_obs_eq f (restrPermMap (compat_th _ _ Hcomp1 pf1j).1)
(restrPermMap (compat_th _ _ Hcomp1' pf1j').1))
(Hexec: internal_execution [seq x <- xs | x == j] tp1 m1 tp2 m2),
exists tp2' m2' f',
internal_execution [seq x <- xs | x == j] tp1' m1' tp2' m2' /\
ren_incr f f' /\
ren_separated f f' m1 m1' /\
((exists p, ((Mem.nextblock m2 = Mem.nextblock m1 + p)%positive /\
(Mem.nextblock m2' = Mem.nextblock m1' + p)%positive))
\/ ((Mem.nextblock m2 = Mem.nextblock m1) /\
(Mem.nextblock m2' = Mem.nextblock m1'))) /\
(forall b,
Mem.valid_block m2' b ->
~ Mem.valid_block m1' b ->
let bz := ((Zpos b) - ((Zpos (Mem.nextblock m1')) -
(Zpos (Mem.nextblock m1))))%Z in
f' (Z.to_pos bz) = Some b /\
f (Z.to_pos bz) = None) /\
(exists (pf2j: containsThread tp2 j)
(pf2j': containsThread tp2' j)
(Hcomp2: mem_compatible tp2 m2)
(Hcomp2': mem_compatible tp2' m2'),
ctl_inj f' (getThreadC pf2j) (getThreadC pf2j') /\
mem_obs_eq f' (restrPermMap (compat_th _ _ Hcomp2 pf2j).1)
(restrPermMap (compat_th _ _ Hcomp2' pf2j').1)) /\
(Mem.nextblock m1 = Mem.nextblock m1' ->
(forall b1 b2 : block, f b1 = Some b2 -> b1 = b2) ->
forall b1 b2 : block, f' b1 = Some b2 -> b1 = b2).