Library bf_div_logic

Require Import base.
Require Import prelim.
Require Import lts_tau.
Require Import branch_bisim.
Require Import back_forth_bisim.
Require Import logic.

Module BF_DIV_LOGIC.
Import LTS_TAU.
Import BRANCH_BISIM.
Import BF_BISIM.
Section bf_div_logic. Context {Obs:ObservationSystem}.

  Inductive bf_mode : Type :=
    | dia : option O -> bf_mode
    | dia_back : option O -> bf_mode
    | box : option O -> bf_mode
    | box_back : option O -> bf_mode
    | div
    | conv.

  Definition interp_bf_mode (m:bf_mode) (P:history -> Prop) (w:history) : Prop :=
      match m with
        | dia o =>
            exists h, step_run (hsys w) (hrun w) o h /\ P {| hsys := hsys w; hrun := h |}
        | dia_back o =>
            exists h, step_run (hsys w) h o (hrun w) /\ P {| hsys := hsys w; hrun := h |}
        | box o =>
            forall h, step_run (hsys w) (hrun w) o h -> P {| hsys := hsys w; hrun := h |}
        | box_back o =>
            forall h, step_run (hsys w) h o (hrun w) -> P {| hsys := hsys w; hrun := h |}
        | div =>
            hdiverges (hsys w) (fun x => P (hst x)) (hrun w)
        | conv =>
            forall D, hdivergence_set (hsys w) (fun x => True) D -> D (hrun w) ->
              exists x, D x /\ P (hst x)
      end.

  Program Instance DivBackForth_LogicInput : LOGIC_INPUT :=
  { world := history
  ; accessable := hist_equiv (rtd_and back_forth back_forth_div)
  ; mode := bf_mode
  ; atom := Empty_set
  ; interp_mode := interp_bf_mode
  ; interp_atom a := match a with end
  }.

  Section adequacy.
    Require prop_logic.
    Import prop_logic.PropLogic.

    Context {Classic:EM}.

    Section inner.

      Variables X Y : LTS.
      Let R (x:run X) (y:run Y) := forall f, interp f (hst x) -> interp f (hst y).

      Hypothesis Hfin1 : weak_image_finite X.
      Hypothesis Hfin2 : weak_image_finite Y.

      Lemma adequate1 : run_simulation back' X Y R.

      Lemma adequate2 : run_simulation forth' X Y R.

      Lemma adequate3 : run_simulation back' Y X (inv R).

      Lemma adequate4 : run_simulation forth' Y X (inv R).

      Lemma adequacy_div1 : run_simulation back_forth_div X Y R.

      Lemma adequacy_div2 : run_simulation back_forth_div Y X (inv R).
    End inner.

    Theorem adequacy : forall x y,
      weak_image_finite (hsys x) ->
      weak_image_finite (hsys y) ->
      (forall f, interp f x -> interp f y) ->
      hist_equiv (rtd_and back_forth back_forth_div) x y.
  End adequacy.

  Section mu_calc.
  Import MuCalc.

  Lemma dual1 : forall f,
    entails (modality emptyE emptyE o div f)
            (neg (modality _ _ o conv (neg f))).

  Lemma dual2 : forall f,
    entails (modality emptyE emptyE o conv f)
            (neg (modality _ _ o div (neg f))).


  Section characteristic.
    Variable X:LTS.
    Let E0 := emptyE.
    Let E1 := extend emptyE (arr (run X) o).

    Definition CF_body (h:run X) (ob:option O) : formula E0 E1 o :=
       f_and (conj' (fun x':{ h' | step_run X h ob h'} => modality E0 E1 o (dia ob) (app (var E0 E1 None) (proj1_sig x'))))
      (f_and (conj' (fun x':{ h' | step_run X h' ob h} => modality E0 E1 o (dia_back ob) (app (var E0 E1 None) (proj1_sig x'))))
      (f_and (modality E0 E1 o (box ob) (disj' (fun x':{ h' | step_run X h ob h'} => (app (var E0 E1 None) (proj1_sig x')))))
      (f_and (modality E0 E1 o (box_back ob) (disj' (fun x':{ h' | step_run X h' ob h} => (app (var E0 E1 None) (proj1_sig x')))))
      (f_and (conj' (fun D:{ D | hdivergence_set X (fun _ => True) D /\ D h} =>
                         modality E0 E1 o div
                           (disj' (fun x:{ x | proj1_sig D x } => (app (var E0 E1 None) (proj1_sig x))))))
             (modality E0 E1 o conv (
               (disj' (fun x:{ h' | extended_run X h None h' } =>
                 (app (var E0 E1 None) (proj1_sig x))))))
      )))).

    Definition CF_main : formula E0 E0 (arr (run X) o) :=
      nu E0 E0 (arr (run X) o)
        (lam E0 E1 (run X) o (fun h:run X =>
          (conj' (fun ob:option O => CF_body h ob))
        )).

    Lemma characteristic_self : forall s,
      interp_cformula _ CF_main (s,tt) (hst s).

    Lemma characteristic_other : forall s w,
      interp_cformula _ CF_main (s,tt) w ->
      accessable (hst s) w.

  End characteristic.

  Definition CF (w:world) : cformula o :=
    app (CF_main (hsys w)) (hrun w).

  Theorem CF_is_characteristic : characteristic_formula CF.

  Theorem completeness : forall w w',
    accessable w w' <->
    (forall t (f:formula emptyE emptyE t) z,
      interp_cformula t f z w <-> interp_cformula t f z w').

  Theorem models_denotable : forall t (X:model t), exists f:cformula t,
    interp_cformula t f = X.

  End mu_calc.

End bf_div_logic.
End BF_DIV_LOGIC.