Section Ksafety_Clight.
(* Import DryMachineSource.THE_DRY_MACHINE_SOURCE.DMS.DryConc. Import DryMachineSource.THE_DRY_MACHINE_SOURCE.SCH. Import DryMachineSource.THE_DRY_MACHINE_SOURCE.DMS.DryMachine. *)
(** *Clight setup *)
(** We define state and step to match the type of ksafe (in konig/safety) *)
Parameter machine_event:Type.
Definition new_state: Type:= list machine_event * machine_state * mem.
Definition mk_nstate (st:MachState) m:new_state:= (snd (fst st), snd st, m).
Definition ksafe_new_step (ge : G) (st : MachState) (m : mem) : nat -> Prop :=
ksafe _ _ (new_step ge) new_valid (mk_nstate st m) (fst (fst st)).
(* Lemma init_schedule_all: forall U pmap n g m p t init_mach om, (semantics.initial_core (MachineSemantics U pmap) n g m p t) = Some (init_mach, om) -> forall U'', correct_schedule (U'', snd (fst init_mach), snd init_mach) -> schedPeek U'' = Some 1  \/ schedPeek U'' = None .  Ad_itted. *)
End Ksafety_Clight.
(*The following variables represent a program satisfying some CSL*)
(* Section Clight_safety. Variable CPROOF: CSL_proof. Definition CS :=   CPROOF.(CSL_CS). Definition V :=   CPROOF.(CSL_V). Definition G :=   CPROOF.(CSL_G). Definition ext_link :=   CPROOF.(CSL_ext_link). Definition ext_link_inj :=   CPROOF.(CSL_ext_link_inj). Definition prog :=   CPROOF.(CSL_prog). Definition all_safe :=   CPROOF.(CSL_all_safe). Definition init_mem_not_none :=   CPROOF.(CSL_init_mem_not_none).  Variables (x: block) (block: (Genv.find_symbol (globalenv prog) (prog_main (Ctypes.program_of_program prog)) = Some x)).  Notation init_jmem n:= (initial_jm CPROOF n). Notation init_rmap n:=(Some (juicy_mem.m_phi (init_jmem n) )). Notation init_genv:=(globalenv prog). Notation init_point:=(Vptr (projT1 ((spr CPROOF))) Integers.Int.zero).   Section Csafety_Clight. (** The initial Juicy Machine *)
Definition js_initial n := initial_machine_state CPROOF n.
Definition Juicy_safety:=
safety_initial_state CPROOF.
(*Import JuicyMachineModule.THE_JUICY_MACHINE.SCH.*)
(*this is showing the similarity between JM's initial machine and CoreSemantics initial machine*)
Definition CoreInitial U r := (semantics.initial_core (MachineSemantics U r)).
intros.
unfold CoreInitial; simpl.
unfold init_machine, JuicyMachineModule.THE_JUICY_MACHINE.JSEM.init_mach.
unfold semantics.initial_core.
unfold ErasureSafety.ErasureProof.JMS.the_sem.
unfold JuicyMachineModule.THE_JUICY_MACHINE.JSEM.ThreadPool.SEM.Sem.
rewrite JuicyMachineModule.THE_JUICY_MACHINE.SEM.CLN_msem.
simpl.
rewrite <- initial_equivalence_trivial.
subst r; simpl.
unfold initial_corestate.
unfold prog in *.
destruct spr as (b' & c' & e & SPR); simpl in *.
change semax_to_juicy_machine.prog with CSL_prog in *.
subst b'.
specialize (SPR n). destruct SPR as [jm [? [? [? _]]]].
unfold juicy_extspec.j_initial_core in H2.
unfold semantics.initial_core in H2. simpl in H2.
rewrite <- H in *.
destruct (Genv.find_funct_ptr g b) eqn:?H; [ | congruence].
inversion H3; clear H3; subst.
f_equal. f_equal. f_equal. f_equal. congruence.
Qed.

Lemma CoreInitial_juicy_safety:
forall (U : semax_invariant.schedule) (n : nat),
exists init_st,
(semantics.initial_core (MachineSemantics U (init_rmap n)))
0 init_genv (proj1_sig (init_mem CPROOF)) init_point nil =
Some ((U, nil, init_st), None) /\
forall U',
csafe (globalenv prog) (U', nil, init_st)
(proj1_sig (init_mem CPROOF)) n.