Library UniMath.CategoryTheory.Monads.Kleisli

********************************************************** Contents:
Written by: Joseph Helfer, Matthew Weaver, 2017
Remark that a monad on C is the same as a relative monad for the identity functor on C

Equivalence of the types of KleisliMonad and "monoidal" monads

Section monad_types_equiv.

  Definition Monad_to_Kleisli {C : category} : Monad C KleisliMonad C :=
    λ T, (functor_on_objects T ,, (pr1 (η T) ,, @bind C T))
               ,, @Monad_law2 C T ,, (@η_bind C T ,, @bind_bind C T).

  Definition Kleisli_to_functor {C : category} (T: KleisliMonad C) : C C.
  Show proof.
    use make_functor.
    - use make_functor_data.
      + exact (RelMonad_from_Kleisli T).
      + apply r_lift.
    - apply is_functor_r_lift.

  Definition Kleisli_to_μ {C : category} (T: KleisliMonad C) :
    Kleisli_to_functor T Kleisli_to_functor T Kleisli_to_functor T.
  Show proof.
    use tpair.
    - exact (λ (x : C), r_bind T (identity (T x))).
    - intros x x' f; simpl.
      unfold r_lift.
      now rewrite (r_bind_r_bind T), <- assoc, (r_eta_r_bind T (T x')), id_right, (r_bind_r_bind T), id_left.

  Definition Kleisli_to_η {C : category} (T: KleisliMonad C) :
    functor_identity C Kleisli_to_functor T.
  Show proof.
    use tpair.
    - exact (r_eta T).
    - intros x x' f; simpl.
      unfold r_lift.
      now rewrite (r_eta_r_bind T x).

  Definition Kleisli_to_Monad {C : category} (T : KleisliMonad C) : Monad C.
  Show proof.
    use (Kleisli_to_functor T,, (Kleisli_to_μ T ,, Kleisli_to_η T) ,, _).
    do 2 try apply tpair; intros; simpl.
    - apply (r_eta_r_bind T).
    - unfold r_lift. now rewrite (r_bind_r_bind T), <- assoc, (r_eta_r_bind T (T c)), id_right, (r_bind_r_eta T).
    - unfold r_lift. now rewrite !(r_bind_r_bind T), id_left, <- assoc, (r_eta_r_bind T (T c)), id_right.

  Proposition Kleisli_to_Monad_to_Kleisli {C : category} (T : KleisliMonad C) :
    Monad_to_Kleisli (Kleisli_to_Monad T) = T.
  Show proof.
    apply subtypePath.
    - intro. do 2 try apply isapropdirprod;
        do 5 try (apply impred; intro);
        apply homset_property.
    - apply (maponpaths (λ p, tpair _ _ p )); simpl.
      apply dirprod_paths.
      * apply idpath.
      * repeat (apply funextsec; unfold homot; intro).
        simpl; unfold Monads.bind; simpl; unfold r_lift.
        now rewrite (r_bind_r_bind T), <- assoc, (r_eta_r_bind T (T x0)), id_right.

  Lemma Monad_to_Kleisli_to_Monad_raw_data {C : category} (T : Monad C) :
    Monad_to_raw_data (Kleisli_to_Monad (Monad_to_Kleisli T)) = Monad_to_raw_data T.
  Show proof.
    apply (maponpaths (λ p, tpair _ _ p )); simpl.
    apply dirprod_paths.
    + apply dirprod_paths;
        repeat (apply funextsec; unfold homot; intro);
        simpl.
      * unfold r_lift, r_bind, r_eta; simpl. unfold Monads.bind.
        rewrite (functor_comp T), <- assoc.
        change (# T x1 · (# T (η T x0) · μ T x0) = #T x1).
        now rewrite (@Monad_law2 C T x0), id_right.
      * unfold Monads.bind, r_bind; simpl.
        now rewrite (functor_id T), id_left.
    + apply idpath.

  Definition Monad_to_Kleisli_to_Monad {C : category} (T : Monad C) :
    Kleisli_to_Monad (Monad_to_Kleisli T) = T.
  Show proof.
    apply Monad_eq_raw_data .
    apply Monad_to_Kleisli_to_Monad_raw_data.

  Definition isweq_Monad_to_Kleisli {C : category} :
    isweq Monad_to_Kleisli :=
    isweq_iso _ _ (Monad_to_Kleisli_to_Monad(C:=C)) Kleisli_to_Monad_to_Kleisli.

  Definition weq_Kleisli_Monad {C : category} :
    Monad C KleisliMonad C := _,, isweq_Monad_to_Kleisli.

End monad_types_equiv.