Lvc.Equiv.Sim

Require Import List.
Require Export Util Var Val Exp Env Map CSet AutoIndTac IL AllInRel.
Require Export EventsActivated StateType paco Equiv.

Set Implicit Arguments.

Simulation

A characterization of simulation equivalence on states; works only for deterministic semantics

CoInductive sim {S} `{StateType S} {} `{StateType } : SProp :=
  | simSilent (σ1 σ1´:S) (σ2 σ2´:) :
      plus2 step σ1 nil σ1´
      → plus2 step σ2 nil σ2´
      → sim σ1´ σ2´
      → sim σ1 σ2
  | simExtern (pσ1 σ1:S) (pσ2 σ2:) :
      star2 step pσ1 nil σ1
      → star2 step pσ2 nil σ2
      → activated σ1
      → activated σ2
      → ( evt σ1´, step σ1 evt σ1´ σ2´, step σ2 evt σ2´ sim σ1´ σ2´)
      → ( evt σ2´, step σ2 evt σ2´ σ1´, step σ1 evt σ1´ sim σ1´ σ2´)
      → sim pσ1 pσ2
  | simErr (σ1 σ1´:S) (σ2:)
    : result σ1´ = None
      → star2 step σ1 nil σ1´
      → normal2 step σ1´
      → sim σ1 σ2
  | simTerm (σ1 σ1´:S) (σ2 σ2´:)
    : result σ1´ = result σ2´
      → star2 step σ1 nil σ1´
      → star2 step σ2 nil σ2´
      → normal2 step σ1´
      → normal2 step σ2´
      → sim σ1 σ2.


Simulation is an equivalence relation
Lemma sim_refl {S} `{StateType S} (σ:S)
      : sim σ σ.

Inductive sim_gen
          {S} `{StateType S} {} `{StateType } (r: SProp) : SProp :=
  | sim´Silent (σ1 σ1´:S) (σ2 σ2´:) :
      plus2 step σ1 nil σ1´
      → plus2 step σ2 nil σ2´
      → r σ1´ σ2´
      → sim_gen r σ1 σ2
  | sim´Extern (pσ1 σ1:S) (pσ2 σ2:) :
      star2 step pσ1 nil σ1
      → star2 step pσ2 nil σ2
      → activated σ1
      → activated σ2
      → ( evt σ1´, step σ1 evt σ1´ σ2´, step σ2 evt σ2´ r σ1´ σ2´)
      → ( evt σ2´, step σ2 evt σ2´ σ1´, step σ1 evt σ1´ r σ1´ σ2´)
      → sim_gen r pσ1 pσ2
  | sim´Err (σ1 σ1´:S) (σ2:)
    : result σ1´ = None
      → star2 step σ1 nil σ1´
      → normal2 step σ1´
      → sim_gen r σ1 σ2
  | sim´Term (σ1 σ1´:S) (σ2 σ2´:)
    : result σ1´ = result σ2´
      → star2 step σ1 nil σ1´
      → star2 step σ2 nil σ2´
      → normal2 step σ1´
      → normal2 step σ2´
      → sim_gen r σ1 σ2.


Hint Constructors sim_gen.

Definition sim´ {S} `{StateType S} {} `{StateType } (σ1:S) (σ2:)
  := paco2 (@sim_gen S _ _) bot2 σ1 σ2.
Hint Unfold sim´.

Definition sim´r {S} `{StateType S} {} `{StateType } r (σ1:S) (σ2:)
  := paco2 (@sim_gen S _ _) r σ1 σ2.
Hint Unfold sim´r.

Lemma sim_gen_mon {S} `{StateType S} {} `{StateType }
: monotone2 (@sim_gen S _ _).


Hint Resolve sim_gen_mon : paco.

Lemma sim_sim´ {S} `{StateType S} {} `{StateType } (σ1:S) (σ2:)
: sim σ1 σ2sim´ σ1 σ2.

Lemma sim´_sim {S} `{StateType S} {} `{StateType } (σ1:S) (σ2:)
: sim´ σ1 σ2sim σ1 σ2.

Lemma sim´_refl {S} `{StateType S} (σ:S)
      : sim´ σ σ.

Lemma sim´_expansion_closed {S} `{StateType S}
      (σ1 σ1´:S) {} `{StateType } (σ2 σ2´:) r
  : sim´r r σ1´ σ2´
    → star2 step σ1 nil σ1´
    → star2 step σ2 nil σ2´
    → sim´r r σ1 σ2.

Lemma sim´_reduction_closed_1 {S} `{StateType S}
      (σ1 σ1´:S) {} `{StateType } (σ2:)
  : sim´ σ1 σ2
    → star2 step σ1 nil σ1´
    → sim´ σ1´ σ2.

Lemma sim´_reduction_closed_2 {S} `{StateType S}
      (σ1:S) {} `{StateType } (σ2 σ2´:)
  : sim´ σ1 σ2
    → star2 step σ2 nil σ2´
    → sim´ σ1 σ2´.

Lemma sim´_terminate {S1} `{StateType S1} (σ1 σ1´:S1)
      {S2} `{StateType S2} (σ2:S2)
: star2 step σ1 nil σ1´
  → normal2 step σ1´
  → result σ1´ None
  → sim´ σ1 σ2
  → σ2´, star2 step σ2 nil σ2´ normal2 step σ2´ result σ1´ = result σ2´.

Lemma sim´_terminate_2 {S1} `{StateType S1} (σ2 σ2´:S1)
      {S2} `{StateType S2} (σ1:S2)
: star2 step σ2 nil σ2´
  → normal2 step σ2´
  → sim´ σ1 σ2
  → σ1´, star2 step σ1 nil σ1´ normal2 step σ1´
           (result σ1´ = result σ2´ result σ1´ = None).

Lemma sim´_activated {S1} `{StateType S1} (σ1:S1)
      {S2} `{StateType S2} (σ2:S2)
: activated σ1
  → sim´ σ1 σ2
  → σ2´, star2 step σ2 nil σ2´ activated σ2´
           ( (evt : event) (σ1´´ : S1),
               step σ1 evt σ1´´
                σ2´´ : S2,
                 step σ2´ evt σ2´´
                 (paco2 (sim_gen (:=S2)) bot2 σ1´´ σ2´´))
           
           ( (evt : event) (σ2´´ : S2),
               step σ2´ evt σ2´´
                σ1´ : S1,
                 step σ1 evt σ1´
                 (paco2 (sim_gen (:=S2)) bot2 σ1´ σ2´´)).

Lemma sim´_activated_2 {S1} `{StateType S1} (σ1:S1)
      {S2} `{StateType S2} (σ2:S2)
: activated σ1
  → sim´ σ2 σ1
  → σ2´, star2 step σ2 nil σ2´
           (activated σ2´
           ( (evt : event) (σ1´´ : S1),
               step σ1 evt σ1´´
                σ2´´ : S2,
                 step σ2´ evt σ2´´
                 (paco2 (sim_gen (:=S1)) bot2 σ2´´ σ1´´))
           
           ( (evt : event) (σ2´´ : S2),
               step σ2´ evt σ2´´
                σ1´ : S1,
                 step σ1 evt σ1´
                 (paco2 (sim_gen (:=S1)) bot2 σ2´´ σ1´))
            (normal2 step σ2´ result σ2´ = None)).

Lemma sim´_zigzag {S1} `{StateType S1}
      (σ1:S1) {S2} `{StateType S2} (σ2a σ2b:S2) {S3} `{StateType S3} (σ3:S3)
  : sim´ σ1 σ2a
    → (star2 step σ2a nil σ2b star2 step σ2b nil σ2a)
    → sim´ σ2b σ3
    → sim´ σ1 σ3.
Lemma sim´_trans {S1} `{StateType S1}
      (σ1:S1) {S2} `{StateType S2} (σ2:S2) {S3} `{StateType S3} (σ3:S3)
  : sim´ σ1 σ2sim´ σ2 σ3sim´ σ1 σ3.

Lemma sim_trans {S1} `{StateType S1}
      (σ1:S1) {S2} `{StateType S2} (σ2:S2) {S3} `{StateType S3} (σ3:S3)
  : sim σ1 σ2
    → sim σ2 σ3
    → sim σ1 σ3.

Lemma sim´_reduction_closed {S} `{StateType S}
      (σ1 σ1´:S) {} `{StateType } (σ2 σ2´:)
  : sim´ σ1 σ2
    → star2 step σ1 nil σ1´
    → star2 step σ2 nil σ2´
    → sim´ σ1´ σ2´.

Module I.

Class SimRelation (A:Type) := {
    ParamRel : Alist varlist varProp;
    ArgRel : onv valonv valAlist vallist valProp;
    BlockRel : AI.blockI.blockProp;
    RelsOK : E a Z VL VL´, ParamRel a Z ArgRel E a VL VL´length Z = length VL length = length VL´
}.

Inductive simB (r:rel2 I.state (fun _ : I.stateI.state)) {A} (AR:SimRelation A) : I.labenvI.labenvAI.blockI.blockProp :=
| simBI a L Z s
  : ParamRel a Z
    → BlockRel a (I.blockI Z s) (I.blockI )
    → ( E Y Yv Y´v,
         omap (exp_eval E) Y = Some Yv
         → omap (exp_eval ) = Some Y´v
         → ArgRel E a Yv Y´v
         → paco2 (@sim_gen I.state _ I.state _) r (L, E, stmtApp (LabI 0) Y)
                        (, , stmtApp (LabI 0) ))
    → simB r AR L a (I.blockI Z s) (I.blockI ).

Definition simL´ (r:rel2 I.state (fun _ : I.stateI.state))
           {A} AR (AL:list A) L := AIR5 (simB r AR) L AL L .

Definition fexteq´
           {A} AR (a:A) (AL:list A) Z s :=
   E VL VL´ L (r:rel2 I.state (fun _ : I.stateI.state)),
    ArgRel E a VL VL´
    → simL´ r AR AL L
    → ParamRel a Z
    → paco2 (@sim_gen I.state _ I.state _) r (L, E[Z <-- List.map Some VL], s)
            (, [ <-- List.map Some VL´], ).

Lemma simL_mon (r r0:rel2 I.state (fun _ : I.stateI.state)) A AR L1 L2 (AL:list A) L1´ L2´
: AIR5 (simB r AR) L1 L2 AL L1´ L2´
  → ( x0 x1 : I.state, r x0 x1r0 x0 x1)
  → L1 = L1´
  → L2 = L2´
  → AIR5 (simB r0 AR) L1 L2 AL L1´ L2´.

Lemma subst_lemma A AR (a:A) AL s V Z L1 L2 t
: fexteq´ AR a (a::AL) Z s
  → ParamRel a Z
  → BlockRel a (I.blockI Z s) (I.blockI )
  → simL´ bot2 AR AL L1 L2
  → ( r (L : list I.block),
       simL´ r AR (a :: AL) L
       sim´r r (L, V, t) (, , ))
  → sim´ (I.blockI Z s :: L1, V, t)
         (I.blockI :: L2, , ).

Lemma fix_compatible r A AR (a:A) AL s E Z L Yv Y´v
: simL´ r AR AL L
  → fexteq´ AR a (a::AL) Z s
  → ParamRel a Z
  → BlockRel a (I.blockI Z s) (I.blockI )
  → ArgRel E a Yv Y´v
  → sim´r r
            (I.blockI Z s :: L, E [Z <-- List.map Some Yv], s)
            (I.blockI :: , [ <-- List.map Some Y´v], ).

Lemma simL_extension´ r A AR (a:A) AL s Z L
: simL´ r AR AL L
  → fexteq´ AR a (a::AL) Z s
  → ParamRel a Z
  → BlockRel a (I.blockI Z s) (I.blockI )
  → simL´ r AR (a::AL) (I.blockI Z s :: L) (I.blockI :: ).

Lemma get_drop_lab0 (L:I.labenv) l blk
: get L (counted l) blk
   → get (drop (labN l) L) (counted (LabI 0)) blk.

Lemma drop_get_lab0 (L:I.labenv) l blk
: get (drop (labN l) L) (counted (LabI 0)) blk
  → get L (counted l) blk.

Lemma sim_drop_shift r l L E Y
: sim´r (S:=I.state) (:=I.state) r (drop (labN l) L, E, stmtApp (LabI 0) Y)
        (drop (labN l) , , stmtApp (LabI 0) )
  → sim´r (S:=I.state) (:=I.state) r (L, E, stmtApp l Y)
          (, , stmtApp l ).

End I.


Instance sim_progeq : ProgramEquivalence F.state F.state.
Defined.

Lemma simL_mon (r r0:rel2 F.state (fun _ : F.stateF.state)) A AR L1 L2 (AL:list A) L1´ L2´
: AIR5 (simB sim_progeq r AR) L1 L2 AL L1´ L2´
  → ( x0 x1 : F.state, r x0 x1r0 x0 x1)
  → L1 = L1´
  → L2 = L2´
  → AIR5 (simB sim_progeq r0 AR) L1 L2 AL L1´ L2´.

Lemma subst_lemma A AR (a:A) AL s V E Z L1 L2 t
: fexteq´ sim_progeq AR a (a::AL) E Z s
  → ParamRel a Z
  → BlockRel a (F.blockI E Z s) (F.blockI )
  → simL´ sim_progeq bot2 AR AL L1 L2
  → ( r (L : list F.block),
       simL´ sim_progeq r AR (a :: AL) L
       sim´r r (L, V, t) (, , ))
  → sim´ (F.blockI E Z s :: L1, V, t)
         (F.blockI :: L2, , ).

Lemma fix_compatible r A AR (a:A) AL s E Z L Yv Y´v
: simL´ sim_progeq r AR AL L
  → fexteq´ sim_progeq AR a (a::AL) E Z s
  → ParamRel a Z
  → BlockRel a (F.blockI E Z s) (F.blockI )
  → ArgRel E a Yv Y´v
  → sim´r r
            (F.blockI E Z s :: L, E [Z <-- List.map Some Yv], s)
            (F.blockI :: , [ <-- List.map Some Y´v], ).

Lemma simL_extension´ r A AR (a:A) AL s E Z L
: simL´ sim_progeq r AR AL L
  → fexteq´ sim_progeq AR a (a::AL) E Z s
  → ParamRel a Z
  → BlockRel a (F.blockI E Z s) (F.blockI )
  → simL´ sim_progeq r AR (a::AL) (F.blockI E Z s :: L) (F.blockI :: ).

Lemma get_drop_lab0 (L:F.labenv) l blk
: get L (counted l) blk
   → get (drop (labN l) L) (counted (LabI 0)) blk.

Lemma drop_get_lab0 (L:F.labenv) l blk
: get (drop (labN l) L) (counted (LabI 0)) blk
  → get L (counted l) blk.

Lemma sim_drop_shift r l L E Y
: sim´r (S:=F.state) (:=F.state) r (drop (labN l) L, E, stmtApp (LabI 0) Y)
        (drop (labN l) , , stmtApp (LabI 0) )
  → sim´r (S:=F.state) (:=F.state) r (L, E, stmtApp l Y)
          (, , stmtApp l ).

Ltac single_step :=
  match goal with
    | [ H : agree_on _ ?E ?, I : val2bool (?E ?x) = true |- step (_, ?, stmtIf ?x _ _) _ ] ⇒
      econstructor; eauto; rewrite <- H; eauto; cset_tac; intuition
    | [ H : agree_on _ ?E ?, I : val2bool (?E ?x) = false |- step (_, ?, stmtIf ?x _ _) _ ] ⇒
      econstructor 3; eauto; rewrite <- H; eauto; cset_tac; intuition
    | [ H : val2bool _ = false |- _ ] ⇒ econstructor 3 ; try eassumption; try reflexivity
    | [ H : step (?L, _ , stmtApp ?l _) _, : get ?L (counted ?l) _ |- _] ⇒
      econstructor; try eapply ; eauto
    | [ : get ?L (counted ?l) _ |- step (?L, _ , stmtApp ?l _) _] ⇒
      econstructor; try eapply ; eauto
    | _econstructor; eauto
  end.

Ltac one_step := eapply simSilent; [ eapply plus2O; single_step
                              | eapply plus2O; single_step
                              | ].

Ltac no_step := eapply simTerm;
               try eapply star2_refl; try get_functional; try subst;
                [ try reflexivity
                | stuck2
                | stuck2 ].

Ltac err_step := eapply simErr;
               try eapply star2_refl; try get_functional; try subst;
                [ try reflexivity
                | stuck2 ].

Ltac step_activated :=
  match goal with
    | [ H : omap (exp_eval ?E) ?Y = Some ?vl
        |- activated (_, ?E, stmtExtern ?x ?f ?Y ?s) ] ⇒
      eexists (ExternI f vl 0); eexists; try (now (econstructor; eauto))
  end.

Ltac extern_step :=
  let STEP := fresh "STEP" in
  eapply simExtern;
    [ eapply star2_refl
    | eapply star2_refl
    | try step_activated
    | try step_activated
    | intros ? ? STEP; inv STEP
    | intros ? ? STEP; inv STEP
    ].