Section safety_preservation_lemmas.
Context  {F V TF TV C D Z data : Type}
{source : CoreSemantics (Genv.t F V) C mem}
{target : CoreSemantics (Genv.t TF TV) D mem}
{match_state : data -> meminj -> C -> mem -> D -> mem -> Prop}
{ord : data -> data -> Prop}
{geS : Genv.t F V}
{geT : Genv.t TF TV}
{entry_points : list (val*val*signature)}
(sim : Forward_simulation_inject source target geS geT entry_points data match_state ord)
(c : C)
(d : D)
(m : mem)
(tm: mem)
(P : val -> mem -> Prop)
(P_good : forall j v tv m tm, val_inject j v tv -> Mem.inject j m tm -> P v m -> P tv tm)
(SRC_DET : corestep_fun source)
(TGT_DET : corestep_fun target)
(source_safe : forall n, safeN source geS P n c m)
.
Definition my_P := fun (x: data) =>
forall (j : meminj) (c : C) (d : D) (m tm : mem),
(forall n : nat, safeN source geS P n c m) ->
match_state x j c m d tm ->
(exists rv : val, halted source c = Some rv) \/
(exists (cd' : data) (j' : meminj) (c' : C) (m' : mem),
corestep_plus source geS c m c' m' /\
((exists (d' : D) (tm' : mem),
corestep_plus target geT d tm d' tm' /\
match_state cd' j' c' m' d' tm') \/
(exists rv : val,
halted source c' = Some rv /\ match_state cd' j' c' m' d tm))).
Definition halt_match c m d tm :=
exists rv trv,
halted source c = Some rv
/\ halted target d = Some trv
/\ P rv m
/\ P trv tm.

Lemma corestep_ord':
forall cd j,
match_state cd j c m d tm ->
halt_match c m d tm
\/ (exists cd' j' c' m',
corestep_plus source geS c m c' m'
/\ ((match_state cd' j' c' m' d tm /\ halt_match c' m' d tm)
\/ (exists d' tm',
corestep_plus target geT d tm d' tm'
/\ match_state cd' j' c' m' d' tm'))).