Section open_semantics_preservation.
Variable (F V TF TV C D Z : Type) (z_init : Z).
Variable source : @EffectSem (Genv.t F V) C.
Variable target : @EffectSem (Genv.t TF TV) D.
Variable (geS : Genv.t F V) (geT : Genv.t TF TV).
Variable entry_points : list (val*val*signature).
Variable spec : ext_spec Z.
(* External function specifications P,Q,EXIT closed under injection of arguments, return values, and memories: *)
Variable spec_closed : ExtSpecProperties.closed spec.
(* External function specs commute w/ injection in the following sense: *)
Variable spec_ok :
forall ef x tys args targs m rty rv m' tm z z' j,
ext_spec_pre spec ef x tys args z m ->
ext_spec_post spec ef x rty rv z' m' ->
Mem.inject (as_inj j) m tm ->
val_list_inject (restrict (as_inj j) (vis j)) args targs ->
mem_forward m m' ->
Mem.unchanged_on (fun b ofs => REACH m (getBlocks args) b=false) m m' ->
exists j' rv' tm',
incr j j'
/\ oval_inject (as_inj j') rv rv'
/\ Mem.inject (as_inj j') m' tm'
/\ sm_inject_separated j j' m tm
/\ SM_wd j'
/\ sm_valid j' m' tm'
/\ mem_forward tm tm'
/\ Mem.unchanged_on (fun b ofs =>
~target_accessible j m tm args b ofs) tm tm'.
Variable sim : SM_simulation_inject source target geS geT entry_points.
Variable src_det : corestep_fun source.
Variable tgt_det : corestep_fun target.
Variable ext_spec_det : ExtSpecProperties.det spec.
Notation tr_source := (TraceSemantics.coopsem z_init source spec).
Notation tr_target := (TraceSemantics.coopsem z_init target spec).
(* Follow from determinism of ext calls + src/tgt_det above *)
Definition marshal j m args tm targs :=
Build_SM_Injection
(fun _ => false) (fun _ => false)
(fun _ => false) (fun _ => false)
(fun _ => None)
(DomSrc j)
(DomTgt j)
(REACH m (getBlocks args))
(REACH tm (getBlocks targs))
(as_inj j).
Arguments reach_in_exported_src {b m args} j _.
Arguments reach_in_exported_tgt {b m args} j _.

Lemma extern_semantics_preservation c c' d m m' tm cd j ef sig args :
trace_match_state cd j c m d tm ->
corestep tr_source geS c m c' m' ->
at_external source (snd c) = Some (ef, sig, args) ->
exists cd' j' d' tm',
corestep tr_target geT d tm d' tm'
/\ trace_match_state cd' j' c' m' d' tm'.