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*)

Lemma tsim_fstep_safe:
forall tpc tpc' tpf mc mc' mf i fi fg tr
(pfc: containsThread tpc i) (pff: containsThread tpf i)
(Hcompc: mem_compatible tpc mc)
(Hcompf: mem_compatible tpf mf)
(HmaxF: max_inv mf)
(HinvF: invariant tpf)
(Hge_wd: ge_wd fg the_ge)
(Hfg: forall b1 b2, fg b1 = Some b2 -> b1 = b2)
(Hren_incr: ren_incr fg fi)
(Hstrong_sim: strong_tsim fi pfc pff Hcompc Hcompf)
(Hmem_wd: valid_mem mc)
(Hstep_internal: internal_step pfc Hcompc tpc' mc'),
exists tpf' mf' fi' tr',
(forall U, fmachine_step (i :: U, tr, tpf) mf (U, tr', tpf') mf') /\
max_inv mf' /\
ren_incr fi fi' /\
ren_separated fi fi' mc mf /\
(forall (pfc': containsThread tpc' i) (pff': containsThread tpf' i)
(Hcompc': mem_compatible tpc' mc') (Hcompf': mem_compatible tpf' mf'),
strong_tsim fi' pfc' pff' Hcompc' Hcompf' /\
(forall b2, (~exists b1, fi' b1 = Some b2) ->
forall ofs, (getThreadR pff').1 # b2 ofs = (getThreadR pff).1 # b2 ofs /\
(getThreadR pff').2 # b2 ofs = (getThreadR pff).2 # b2 ofs)) /\
(forall j (pffj : containsThread tpf j),
i <> j ->
forall (b1 b2 : block),
fi' b1 = Some b2 ->
fi b1 = None ->
forall ofs, (getThreadR pffj).1 # b2 ofs = None /\ (getThreadR pffj).2 # b2 ofs = None) /\
(forall (bl : block) (ofsl : Z)
rmap
(b1 b2 : block) (ofs : Z),
fi' b1 = Some b2 ->
fi b1 = None ->
lockRes tpf (bl, ofsl) = Some rmap -> rmap.1 # b2 ofs = None /\ rmap.2 # b2 ofs = None).