Library bf_ref_div_logic

Require Import base.
Require Import prelim.
Require Import lts_ref.
Require Import branch_ref.
Require Import back_forth_ref.
Require Import logic.

Module BF_REF_DIV_LOGIC.
Section bf_ref_div_logic. Context {Obs:ObservationSystem}.
Import BF_REF. Import BRANCH_REF.

  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 bf_interp_mode (m:bf_mode) (P:history -> Prop) (w:history) : Prop :=
    match m with
      | dia o =>
        exists h, weak_must_extend (hsys w) (hrun w) o h /\ P {| hsys := hsys w; hrun := h |}
      | dia_back o =>
        exists h, weak_may_extend (hsys w) h o (hrun w) /\ P {| hsys := hsys w; hrun := h |}
      | box o =>
        forall h, weak_may_extend (hsys w) (hrun w) o h -> P {| hsys := hsys w; hrun := h |}
      | box_back o =>
        forall h, weak_may_extend (hsys w) h o (hrun w) -> P {| hsys := hsys w; hrun := h |}
      | div =>
        exists D, must_hdivergence_set (hsys w) D /\ D (hrun w) /\
          forall x, D x -> P (hst x)
      | conv =>
        forall D, may_hdivergence_set (hsys w) D -> D (hrun w) ->
          exists x, D x /\ P (hst x)
    end.

  Program Instance bf_ref_div_logic_input : LOGIC_INPUT :=
  { world := history
  ; accessable := hist_accessable (rtd_and back_forth back_forth_div)
  ; mode := bf_mode
  ; atom := Empty_set
  ; interp_mode := bf_interp_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_refinement back X Y R.

      Lemma adequate2 : run_refinement forth X Y R.

      Lemma adequacy_div1 : run_refinement back_forth_div X Y 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_accessable (rtd_and back_forth back_forth_div) x y.
  End adequacy.

  Import MuCalc.

  Lemma proof_dual1 : forall ob (P Q:cformula o),
    entails P (modality _ _ o (box ob) Q) <->
    entails (modality _ _ o (dia_back ob) P) Q.

  Lemma proof_dual2 : forall ob (P Q:cformula o),
    entails P (modality _ _ o (box_back ob) Q) ->
    entails (modality _ _ o (dia ob) P) Q.

  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' | weak_must_extend X h ob h'} => modality E0 E1 o (dia ob) (app (var E0 E1 None) (proj1_sig x'))))
      (f_and (conj' (fun x':{ h' | weak_may_extend 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' | weak_may_extend 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' | weak_may_extend X h' ob h} => (app (var E0 E1 None) (proj1_sig x')))))
      (f_and (conj' (fun D:{ D | must_hdivergence_set X 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' | may_extend 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 ->
      prelim.accessable (hst s) w.
  End characteristic.

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

  Theorem CF_is_characteristic : characteristic_formula CF.

  Theorem completeness : forall w w',
    prelim.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 bf_ref_div_logic.
End BF_REF_DIV_LOGIC.