Lvc.Equiv.NonParametricBiSim

Require Import List paco2.
Require Import Util IL.
Require Import SmallStepRelations StateType.

Set Implicit Arguments.
Unset Printing Records.

Non-parametric Definitions of Simulation and Bisimulation

Bisimulation

A characterization of bisimulation equivalence on states; works only for internally deterministic semantics.

CoInductive bisim {S} `{StateType S} {S'} `{StateType S'} : S S' Prop :=
  | BisimSilent (σ1 σ1':S) (σ2 σ2':S') :
       step σ1 nil σ1'
       step σ2 nil σ2'
       bisim σ1' σ2'
       bisim σ1 σ2
  | BisimExtern (pσ1 σ1:S) (pσ2 σ2:S') :
       step pσ1 nil σ1
       step pσ2 nil σ2
       activated σ1
       activated σ2
       ( evt σ1', step σ1 evt σ1' σ2', step σ2 evt σ2' bisim σ1' σ2')
       ( evt σ2', step σ2 evt σ2' σ1', step σ1 evt σ1' bisim σ1' σ2')
       bisim pσ1 pσ2
  | BisimTerm (σ1 σ1':S) (σ2 σ2':S')
    : result σ1' = result σ2'
       step σ1 nil σ1'
       step σ2 nil σ2'
       step σ1'
       step σ2'
       bisim σ1 σ2.

Arguments bisim [S] {H} [S'] {} _ _.

Bisimulation is an reflexive and symmetric

Lemma bisim_refl {S} `{StateType S} (σ:S)
      : bisim σ σ.
Proof.
  revert σ. cofix.
  intros. destruct (step_dec σ) as [[[] []]|].
  - eapply BisimExtern; intros; eauto using star2_refl; eexists; eauto.
  - eapply BisimSilent; eauto using plus2O.
  - eapply BisimTerm; eauto using star2_refl.
Qed.


Lemma bisim_sym {S} `{StateType S} {S'} `{StateType S'} (σ:S) (σ':S')
      : bisim σ σ' bisim σ' σ.
Proof.
  revert σ σ'. cofix; intros.
  inv .
  - eapply BisimSilent; eauto.
  - eapply BisimExtern; eauto; intros.
    edestruct ; eauto; dcr. eexists; eauto.
    edestruct ; eauto; dcr. eexists; eauto.
  - eapply BisimTerm; eauto using star2_refl.
Qed.


Simulation


CoInductive sim {S} `{StateType S} {S'} `{StateType S'} : S S' Prop :=
  | SimSilent (σ1 σ1':S) (σ2 σ2':S') :
       step σ1 nil σ1'
       step σ2 nil σ2'
       sim σ1' σ2'
       sim σ1 σ2
  | SimExtern (pσ1 σ1:S) (pσ2 σ2:S') :
       step pσ1 nil σ1
       step pσ2 nil σ2
       activated σ1
       activated σ2
       ( evt σ2', step σ2 evt σ2' σ1', step σ1 evt σ1' sim σ1' σ2')
       sim pσ1 pσ2
  | SimTerm (σ1 σ1':S) (σ2 σ2':S')
    : result σ1' = result σ2'
       step σ1 nil σ1'
       step σ2 nil σ2'
       step σ1'
       step σ2'
       sim σ1 σ2
  | SimErr (σ1 σ1':S) (σ2:S')
    : result σ1' = None
       step σ1 nil σ1'
       step σ1'
       sim σ1 σ2.

Arguments bisim [S] {H} [S'] {} _ _.

Simulation is an reflexive


Lemma sim_refl {S} `{StateType S} (σ:S)
      : sim σ σ.
Proof.
  revert σ. cofix.
  intros. destruct (step_dec σ) as [[[] []]|].
  - eapply SimExtern; intros; eauto using star2_refl; eexists; eauto.
  - eapply SimSilent; eauto using plus2O.
  - eapply SimTerm; eauto using star2_refl.
Qed.


Receptive and determinate according to CompCertTSO (Sevcík 2013)

Definition same_call (e e':extern) := extern_fnc e = extern_fnc e' extern_args e = extern_args e'.

Definition receptive S `{StateType S} :=
   σ σ' ext, step σ (EvtExtern ext) σ'
               ext', same_call ext ext' σ'', step σ (EvtExtern ext') σ''.

Arguments receptive S [H].

Definition determinate S `{StateType S} :=
   σ σ' σ'' ext ext', step σ (EvtExtern ext) σ'
               step σ (EvtExtern ext') σ'' same_call ext ext'.

Arguments determinate S [H].

Lemma bisimExternDet {S} `{StateType S} {S'} `{StateType S'} (pσ1:S) (pσ2:S') (σ1:S) (σ2:S')
      (RCPT:receptive S) (DET:determinate S')
: step pσ1 nil σ1
   step pσ2 nil σ2
   activated σ1
   ( evt σ1', step σ1 evt σ1' σ2', step σ2 evt σ2' bisim σ1' σ2')
   bisim pσ1 pσ2.
Proof.
  intros.
  econstructor 2; eauto.
  - inv . dcr. edestruct ; eauto. firstorder.
  - intros. inv . dcr. exploit ; eauto; dcr.
    destruct evt.
    + exploit DET. eauto. eapply .
      exploit RCPT; eauto. dcr.
      exploit . eapply . dcr.
      eexists; split. eapply .
      exploit step_externally_determined. Focus 3.
      instantiate (2:=σ2') in . rewrite . eapply .
      eauto. eauto.
    + exfalso. exploit step_internally_deterministic; eauto; dcr. congruence.
Qed.


Lemma bisim_expansion_closed {S} `{StateType S}
      (σ1 σ1':S) {S'} `{StateType S'} (σ2 σ2':S')
  : bisim σ1' σ2'
     step σ1 nil σ1'
     step σ2 nil σ2'
     bisim σ1 σ2.
Proof.
  intros SIM ? ?.
  inversion SIM; subst;
    eauto using bisim, star2_plus2_plus2_silent, star2_trans_silent.
Qed.


Lemma bisim_reduction_closed {S} `{StateType S}
      (σ1 σ1':S) {S'} `{StateType S'} (σ2:S')
  : bisim σ1 σ2
     step σ1 nil σ1'
     bisim σ1' σ2.
Proof.
  intros Sim Star. eapply star2_star2n in Star. destruct Star as [n StarN].
  revert σ1 σ1' σ2 Sim StarN.
  size induction n.
  inv Sim.
  - invc StarN; eauto; relsimpl.
    eapply star2_star2n in . destruct as [n' ].
    edestruct (star2n_reach ); eauto. eapply H.
    + eapply bisim_expansion_closed; eauto using star2n_star2, plus2_star2.
    + eapply ; try eapply . omega.
      eapply bisim_expansion_closed;
        eauto using star2n_star2, plus2_star2.
  - eapply star2n_star2 in StarN; relsimpl; eauto using bisim.
  - eapply star2n_star2 in StarN; relsimpl; eauto using bisim.
Qed.


Lemma bisim_reduction_closed_both {S} `{StateType S}
      (σ1 σ1':S) {S'} `{StateType S'} (σ2 σ2':S')
  : bisim σ1 σ2
     step σ1 nil σ1'
     step σ2 nil σ2'
     bisim σ1' σ2'.
Proof.
  intros. eapply bisim_reduction_closed; eauto.
  eapply bisim_sym. eapply bisim_reduction_closed; eauto.
  eapply bisim_sym. eauto.
Qed.


Lemma bisim_terminate {} `{StateType } (σ1 σ1':)
      {} `{StateType } (σ2:)
: step σ1 nil σ1'
   step σ1'
   bisim σ1 σ2
   σ2', step σ2 nil σ2' step σ2' result σ1' = result σ2'.
Proof.
  intros. general induction .
  - inv ; subst.
    + exfalso. eapply . inv ; do 2 eexists; eauto.
    + exfalso. eapply star2_normal in ; eauto. subst.
      eapply (activated_normal _ ); eauto.
    + eapply star2_normal in ; eauto; subst.
      eexists; split; eauto.
  - destruct y; isabsurd. simpl.
    eapply ; eauto.
    eapply bisim_reduction_closed; eauto using , star2_silent.
Qed.


Lemma bisim_activated {} `{StateType } (σ1:)
      {} `{StateType } (σ2:)
: activated σ1
   bisim σ1 σ2
   σ2', step σ2 nil σ2' activated σ2'
           ( (evt : event) (σ1'' : ),
               step σ1 evt σ1''
                σ2'' : ,
                 step σ2' evt σ2''
                 (bisim σ1'' σ2''))
           
           ( (evt : event) (σ2'' : ),
               step σ2' evt σ2''
                σ1' : ,
                 step σ1 evt σ1'
                 (bisim σ1' σ2'')).
Proof.
  intros.
  inv ; subst.
  - exfalso. edestruct (plus2_destr_nil ); dcr.
     destruct as [? []].
     exploit (step_internally_deterministic _ _ _ _ ); dcr; congruence.
  - assert (σ1 = σ0). {
      eapply activated_star_eq; eauto.
    }
    subst σ1.
    eexists σ3; split; eauto.
  - exfalso. refine (activated_normal_star _ _ _); eauto using .
Qed.