Section Parching.
Context (ge : Clight.genv).
Instance Sem : Semantics := ClightSemanticsForMachines.Clight_newSem ge.
Instance JR : Resources := LocksAndResources.
Instance JTP : ThreadPool.ThreadPool := OrdinalPool.OrdinalThreadPool.
Instance JMS : MachineSig := Concur.JuicyMachineShell.
Instance DilMem : DiluteMem := HybridMachineSig.HybridCoarseMachine.DilMem.
Instance scheduler : Scheduler := HybridCoarseMachine.scheduler.
Instance JuicyMachine : HybridMachine := HybridCoarseMachine.HybridCoarseMachine.
Definition jstate := jstate ge.
Instance DR : Resources := DryHybridMachine.dryResources.
Instance DTP : ThreadPool.ThreadPool := OrdinalPool.OrdinalThreadPool.
Instance DMS : MachineSig := DryHybridMachine.DryHybridMachineSig.
Instance DryMachine : HybridMachine := HybridCoarseMachine.HybridCoarseMachine.
Definition dstate := ThreadPool.t
(ThreadPool := OrdinalPool.OrdinalThreadPool)
(* @t dryResources DSem (@OrdinalPool.OrdinalThreadPool dryResources DSem) *).  Definition dmachine_state:= MachState(ThreadPool := OrdinalPool.OrdinalThreadPool).  Import event_semantics threadPool.ThreadPool.  (** * Match relation between juicy and dry state : *)
(* 1-2. Same threads are contained in each state: 3.   Threads have the same c 4-5.   Threads have the same permissions up to erasure 6.   the locks are in the same addresses. 7-8-9. Lock contents match up to erasure. *)
Inductive match_st' : jstate ->  dstate -> Prop:=
MATCH_ST: forall (js:jstate) (ds:dstate)
(mtch_cnt: forall {tid},  containsThread js tid -> containsThread ds tid )
(mtch_cnt': forall {tid}, containsThread ds tid -> containsThread js tid )
(mtch_gtc: forall {tid} (Htid:containsThread js tid)(Htid':containsThread ds tid),
getThreadC Htid = getThreadC Htid' )
(mtch_perm1: forall b ofs {tid} (Htid:containsThread js tid)(Htid':containsThread ds tid),
juicy_mem.perm_of_res (resource_at (getThreadR Htid) (b, ofs)) =
((getThreadR Htid').1 !! b) ofs )
(mtch_perm2: forall b ofs {tid} (Htid:containsThread js tid)(Htid':containsThread ds tid),
juicy_mem.perm_of_res_lock (resource_at (getThreadR Htid) (b, ofs)) =
((getThreadR Htid').2 !! b) ofs )
(mtch_locks: forall a,
ssrbool.isSome (lockRes js a) = ssrbool.isSome (lockRes ds a))
(mtch_locksEmpty: forall lock dres,
lockRes js lock = Some (None) ->
lockRes ds lock = Some dres ->
dres = (empty_map, empty_map))
(mtch_locksRes: forall lock jres dres,
lockRes js lock = Some (Some jres) ->
lockRes ds lock = Some dres ->
forall b ofs,
juicy_mem.perm_of_res (resource_at jres (b, ofs)) = (dres.1 !! b) ofs )
(mtch_locksRes: forall lock jres dres,
lockRes js lock = Some (Some jres) ->
lockRes ds lock = Some dres ->
forall b ofs,
juicy_mem.perm_of_res_lock (resource_at jres (b, ofs)) = (dres.2 !! b) ofs )
(** *SyncStep simulation*)
(** Proof of the step diagram when the step is a synchronization step. TODO: Split the proof in each diagram. *)
Inductive deltaMap_cases (dmap:delta_map) b ofs:=
| DMAPS df p:  dmap ! b = Some df -> df ofs = Some p -> deltaMap_cases dmap b ofs
| DNONE1 df:  dmap ! b = Some df -> df ofs = None -> deltaMap_cases dmap b ofs
| DNONE2:  dmap ! b = None -> deltaMap_cases dmap b ofs.

(* Proof of the step diagram when the step is a synchronization step *)
Lemma sync_step_diagram:
forall (m m':Memory.mem) (U:seq nat) js js' ds i ev
(MATCH: match_st js ds)
(dinv: invariant ds)
(Hi: containsThread js i)
(Hcmpt: mem_compatible js m)
(HschedN: schedPeek U = Some i)
(Htstep:  syncStep true Hi Hcmpt js' m' ev),
exists ds' : dstate, exists ev',
invariant ds' /\ match_st js' ds' /\
syncStep true (MTCH_cnt MATCH Hi) (MTCH_compat _ _ _ MATCH Hcmpt) ds' m' ev'.