End OneThreadCompiledMatch.
Arguments same_length_contains {ms1 ms2}.
Arguments memcompat1 {ocd j cstate1 m1 cstate2 m2}.
Arguments memcompat2 {ocd j cstate1 m1 cstate2 m2}.
Section HybridThreadDiagram.
Notation the_simulation := compiler_simulation.
Parameter option_compiler_order: option (Injindex the_simulation) -> option (Injindex the_simulation) -> Prop.
(* automatic rewrite gsoThreadCode *)
Ltac auto_gsoThreadCode:=
match goal with
| [ |- context[getThreadC ?cntJ'] ] =>
match type of cntJ' with
| containsThread (updThread ?cntI _ _) ?J =>
match type of cntI with
| containsThread ?hb ?I =>
match goal with
| [ cntJ: containsThread hb J |- _ ] =>
rewrite gsoThreadCode with
(cnti:= cntI)
(cntj:= cntJ)
(cntj':= cntJ') by eassumption
end
end
end
end.
Ltac simpl_getThreadC':=
first[ rewrite gssThreadCode |
auto_gsoThreadCode
].
(*Produces all the "containThreads" that might be missing "*)
Ltac containsThread_helper:=
match goal with
| [ cntj': containsThread (updThread ?cntj _ _) ?i  |- _ ] =>
match type of cntj with
| containsThread ?hb ?j =>
progress (
first [match goal with
| [ cnti: containsThread hb i |- _ ] => idtac
end |
let CNTI:= fresh "cnti" in
assert (CNTI: containsThread hb i)
by
(eapply cntUpdate'; eapply cntj') ])
| _ => fail
end
end.
Ltac simpl_getThreadC:=
match goal with
| [  |- context [@getThreadC _ _ ?j (@updThread _ _ _ ?hb _ _ _) _ ] ] =>
let cntJ:= fresh "cntj" in
evar (cntj : containsThread hb j);
let cntJ':= eval unfold cntJ in cntJ in
rewrite gsoThreadCode with
(tp := hb)
(cntj:= cntJ')
by eassumption;
instantiate (cntJ:= ltac:(eauto));
clear cntJ
end.
Ltac containsThread_cleanup:=
repeat match goal with
| [H1: containsThread ?TP ?M,
H2: containsThread (@updThread _ _ _ ?TP _ _ _) ?M |- _] =>
replace H2 with H1 in * by apply proof_irr; clear H2
| [H1: containsThread ?TP ?M,
H2: containsThread ?TP ?M |- _] =>
replace H2 with H1 in * by apply proof_irr; clear H2
end.

(* Specifications: *)
Lemma mem_step_same_visible:
forall hb Sems Semt i j tp1 tp2 m1 m2
(cnti1: containsThread tp1 i)
(cntj1: containsThread tp1 j)
(cnti2: containsThread tp2 i)
(compat1: HybridMachine.mem_compatible hb Sems Semt tp1 m1)
(compat2: HybridMachine.mem_compatible hb Sems Semt tp2 m2),
j <> i ->
semantics.mem_step (restrPermMap (FST (compat1 j cntj1))) m2 ->
same_visible (restrPermMap (FST (compat1 i cnti1)))
(restrPermMap (FST (compat2 i cnti2))).
Lemma match_thread_source_same_vis:
forall f f' m1 m1' m2 m2' cs ct,
same_visible m1 m1' ->
same_visible m2 m2' ->
is_ext f (Mem.nextblock m1) f' (Mem.nextblock m2) ->
match_thread_source f cs m1 ct m2 ->
match_thread_source f' cs m1' ct m2'.
Lemma match_thread_compiled_same_vis:
forall f f' m1 m1' m2 m2' cs ct cd,
same_visible m1 m1' ->
same_visible m2 m2' ->
is_ext f (Mem.nextblock m1) f' (Mem.nextblock m2) ->
match_thread_compiled cd f cs m1 ct m2 ->
match_thread_compiled cd f' cs m1' ct m2'.
Lemma match_thread_target_same_vis:
forall f f' m1 m1' m2 m2' cs ct,
same_visible m1 m1' ->
same_visible m2 m2' ->
is_ext f (Mem.nextblock m1) f' (Mem.nextblock m2) ->
match_thread_target f cs m1 ct m2 ->
match_thread_target f' cs m1' ct m2'.
Lemma mem_step_compatible:
forall hb1 Sems Semt c m m' code_i i
(cnti: containsThread c i)
(Hcmpt1: HybridMachine.mem_compatible hb1 Sems Semt c m),
semantics.mem_step (restrPermMap (FST (Hcmpt1 i cnti))) m'->
HybridMachine.mem_compatible hb1 Sems Semt
(updThread cnti
code_i
(getCurPerm m', (snd (getThreadR cnti)))) m'.