Quellcodebibliothek Statistik Leitseite products/Sources/formale Sprachen/Lean/Mathlib/Mathlib/CategoryTheory/Monad/     Datei vom 15.10.2026 mit Größe 10 kB image not shown  

Quelle  Basic.lean   Sprache: Lean

 

/-
Copyright (c) 2019 Kim Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kim Morrison, Bhavik Mehta, Adam Topaz
-/

module

public import Mathlib.CategoryTheory.EpiMono

/-!
# Monads

We construct the categories of monads and comonads, and their forgetful functors to endofunctors.

(Note that these are the category theorist's monads, not the programmers monads.
For the translation, see the file `Mathlib/CategoryTheory/Monad/Types.lean`.)

For the fact that monads are "just" monoids in the category of endofunctors, see the file
`CategoryTheory.Monad.EquivMon`.
-/


@[expose] public section

namespace CategoryTheory

open Category

universe v₁ u₁

-- morphism levels before object levels. See note [category theory universes].
variable (C : Type u₁) [Category.{v₁} C]

/-- The data of a monad on C consists of an endofunctor T together with natural transformations
`η : �� C ⟶ T` and `μ : T ⋙ T ⟶ T` satisfying three equations:
- `T μ_X ≫ μ_X = μ_(TX) ≫ μ_X` (associativity)
- `η_(TX) ≫ μ_X = 1_X` (left unit)
- `Tη_X ≫ μ_X = 1_X` (right unit)
-/

structure Monad extends C ⥤ C where
  /-- The unit for the monad. -/
  η : �� _ ⟶ toFunctor
  /-- The multiplication for the monad. -/
  μ : toFunctor ⋙ toFunctor ⟶ toFunctor
  assoc (X : C) : toFunctor.map (NatTrans.app μ X) ≫ μ.app _ = μ.app _ ≫ μ.app _ := by cat_disch
  left_unit (X : C) : dsimp% η.app (toFunctor.obj X) ≫ μ.app _ = �� _ := by cat_disch
  right_unit (X : C) : dsimp% toFunctor.map (η.app X) ≫ μ.app _ = �� _ := by cat_disch

/-- The data of a comonad on C consists of an endofunctor G together with natural transformations
`ε : G ⟶ �� C` and `δ : G ⟶ G ⋙ G` satisfying three equations:
- `δ_X ≫ G δ_X = δ_X ≫ δ_(GX)` (coassociativity)
- `δ_X ≫ ε_(GX) = 1_X` (left counit)
- `δ_X ≫ G ε_X = 1_X` (right counit)
-/

@[to_dual]
structure Comonad extends C ⥤ C where
  /-- The counit for the comonad. -/
  ε : toFunctor ⟶ �� _
  /-- The comultiplication for the comonad. -/
  δ : toFunctor ⟶ toFunctor ⋙ toFunctor
  coassoc (X : C) : NatTrans.app δ _ ≫ toFunctor.map (δ.app X) = δ.app _ ≫ δ.app _ := by
    cat_disch
  left_counit (X : C) : δ.app X ≫ ε.app (toFunctor.obj X) = �� _ := by cat_disch
  right_counit (X : C) : δ.app X ≫ toFunctor.map (ε.app X) = �� _ := by cat_disch

@[to_dual none, reassoc]
lemma Monad.unit_naturality (T : Monad C) ⦃X Y : C⦄ (f : X ⟶ Y) :
    f ≫ T.η.app Y = T.η.app X ≫ T.map f :=
  T.η.naturality _

@[to_dual none, reassoc]
lemma Monad.mu_naturality (T : Monad C) ⦃X Y : C⦄ (f : X ⟶ Y) :
    T.map (T.map f) ≫ T.μ.app Y = T.μ.app X ≫ T.map f :=
  T.μ.naturality _

@[to_dual none, reassoc]
lemma Comonad.counit_naturality (T : Comonad C) ⦃X Y : C⦄ (f : X ⟶ Y) :
    T.map f ≫ T.ε.app Y = T.ε.app X ≫ f :=
  T.ε.naturality _

@[to_dual none, reassoc]
lemma Comonad.delta_naturality (T : Comonad C) ⦃X Y : C⦄ (f : X ⟶ Y) :
    T.map f ≫ T.δ.app Y = T.δ.app X ≫ T.map (T.map f) :=
  T.δ.naturality _

variable {C}
variable (T : Monad C) (G : Comonad C)

@[to_dual]
instance coeMonad : Coe (Monad C) (C ⥤ C) :=
  ⟨fun T => T.toFunctor⟩

initialize_simps_projections CategoryTheory.Monad (toFunctor → coe)

initialize_simps_projections CategoryTheory.Comonad (toFunctor → coe)

-- TODO: investigate whether `Monad.assoc` can be a `simp` lemma?
attribute [reassoc (attr := simp)] Monad.left_unit Monad.right_unit
attribute [reassoc (attr := simp)] Comonad.coassoc Comonad.left_counit Comonad.right_counit

/-- A morphism of monads is a natural transformation compatible with η and μ. -/
@[ext]
structure MonadHom (T₁ T₂ : Monad C) extends NatTrans (T₁ : C ⥤ C) T₂ where
  app_η : ∀ X, T₁.η.app X ≫ app X = T₂.η.app X := by cat_disch
  app_μ : ∀ X, T₁.μ.app X ≫ app X = (T₁.map (app X) ≫ app _) ≫ T₂.μ.app X := by
    cat_disch

initialize_simps_projections MonadHom (+toNatTrans, -app)

/-- A morphism of comonads is a natural transformation compatible with ε and δ. -/
@[ext, to_dual (reorder := M N)]
structure ComonadHom (M N : Comonad C) extends NatTrans (M : C ⥤ C) N where
  app_ε : ∀ X, app X ≫ N.ε.app X = M.ε.app X := by cat_disch
  app_δ : ∀ X, app X ≫ N.δ.app X = M.δ.app X ≫ app _ ≫ N.map (app X) := by cat_disch

initialize_simps_projections ComonadHom (+toNatTrans, -app)

attribute [to_dual existing] MonadHom.ext
attribute [reassoc (attr := simp)] MonadHom.app_η MonadHom.app_μ
attribute [reassoc (attr := simp)] ComonadHom.app_ε ComonadHom.app_δ

@[to_dual]
instance : Quiver (Monad C) where
  Hom := MonadHom


@[to_dual (attr := ext)]
lemma MonadHom.ext' {T₁ T₂ : Monad C} (f g : T₁ ⟶ T₂) (h : f.app = g.app) : f = g :=
  MonadHom.ext h

@[to_dual]
instance : Category (Monad C) where
  id M := { toNatTrans := �� (M : C ⥤ C) }
  comp f g :=
    { toNatTrans :=
        { app := fun X => f.app X ≫ g.app X
          naturality := fun X Y h => by rw [assoc, f.1.naturality_assoc, g.1.naturality] } }

@[to_dual]
instance {T : Monad C} : Inhabited (MonadHom T T) :=
  ⟨�� T⟩

@[to_dual (attr := simp)]
theorem MonadHom.id_toNatTrans (T : Monad C) : (�� T : T ⟶ T).toNatTrans = �� (T : C ⥤ C) :=
  rfl

@[to_dual (attr := simp)]
theorem MonadHom.comp_toNatTrans {T₁ T₂ T₃ : Monad C} (f : T₁ ⟶ T₂) (g : T₂ ⟶ T₃) :
    (f ≫ g).toNatTrans = ((f.toNatTrans : _ ⟶ (T₂ : C ⥤ C)) ≫ g.toNatTrans : (T₁ : C ⥤ C) ⟶ T₃) :=
  rfl

@[deprecated (since := "2026-10-15")] alias comp_toNatTrans := ComonadHom.comp_toNatTrans

insert_to_dual_translation CategoryTheory.MonadIso CategoryTheory.ComonadIso

/-- Construct a monad isomorphism from a natural isomorphism of functors where the forward
direction is a monad morphism. -/

@[simps]
def MonadIso.mk {M N : Monad C} (f : (M : C ⥤ C) ≅ N)
    (f_η : ∀ (X : C), M.η.app X ≫ f.hom.app X = N.η.app X := by cat_disch)
    (f_μ : ∀ (X : C), M.μ.app X ≫ f.hom.app X =
    (M.map (f.hom.app X) ≫ f.hom.app (N.obj X)) ≫ N.μ.app X := by cat_disch) : M ≅ N where
  hom :=
    { toNatTrans := f.hom
      app_η := f_η
      app_μ := f_μ }
  inv :=
    { toNatTrans := f.inv
      app_η := fun X => by simp [← f_η]
      app_μ := fun X => by
        rw [← NatIso.cancel_natIso_hom_right f]
        simp only [NatTrans.naturality, Iso.inv_hom_id_app, assoc, comp_id, f_μ,
          NatTrans.naturality_assoc, Iso.inv_hom_id_app_assoc, ← Functor.map_comp_assoc]
        simp }

/-- Construct a comonad isomorphism from a natural isomorphism of functors where the forward
direction is a comonad morphism. -/

@[simps]
def ComonadIso.mk {M N : Comonad C} (f : (M : C ⥤ C) ≅ N)
    (f_ε : ∀ (X : C), f.hom.app X ≫ N.ε.app X = M.ε.app X := by cat_disch)
    (f_δ : ∀ (X : C), f.hom.app X ≫ N.δ.app X =
    M.δ.app X ≫ f.hom.app (M.obj X) ≫ N.map (f.hom.app X) := by cat_disch) : M ≅ N where
  hom :=
    { toNatTrans := f.hom
      app_ε := f_ε
      app_δ := f_δ }
  inv :=
    { toNatTrans := f.inv
      app_ε := fun X => by simp [← f_ε]
      app_δ := fun X => by
        rw [← NatIso.cancel_natIso_hom_left f]
        simp only [reassoc_of% (f_δ X), Iso.hom_inv_id_app_assoc, NatTrans.naturality_assoc]
        simp }

variable (C)

/-- The forgetful functor from the category of monads to the category of endofunctors. -/
@[to_dual (attr := implicit_reducible, simps)
/-- The forgetful functor from the category of comonads to the category of endofunctors. -/]
def monadToFunctor : Monad C ⥤ C ⥤ C where
  obj T := T
  map f := f.toNatTrans

@[to_dual]
instance : (monadToFunctor C).Faithful where

theorem monadToFunctor_mapIso_monad_iso_mk {M N : Monad C} (f : (M : C ⥤ C) ≅ N) (f_η f_μ) :
    (monadToFunctor _).mapIso (MonadIso.mk f f_η f_μ) = f := by
  ext
  rfl

instance : (monadToFunctor C).ReflectsIsomorphisms where
  reflects f _ := (MonadIso.mk (asIso ((monadToFunctor C).map f)) f.app_η f.app_μ).isIso_hom

theorem comonadToFunctor_mapIso_comonad_iso_mk {M N : Comonad C} (f : (M : C ⥤ C) ≅ N) (f_ε f_δ) :
    (comonadToFunctor _).mapIso (ComonadIso.mk f f_ε f_δ) = f := by
  ext
  rfl

instance : (comonadToFunctor C).ReflectsIsomorphisms where
  reflects f _ := (ComonadIso.mk (asIso ((comonadToFunctor C).map f)) f.app_ε f.app_δ).isIso_hom

variable {C}

/-- An isomorphism of monads gives a natural isomorphism of the underlying functors. -/
@[to_dual (attr := simps (rhsMd := .default))
/-- An isomorphism of comonads gives a natural isomorphism of the underlying functors. -/]
def MonadIso.toNatIso {M N : Monad C} (h : M ≅ N) : (M : C ⥤ C) ≅ N :=
  (monadToFunctor C).mapIso h

variable (C)

namespace Monad

/-- The identity monad. -/
@[to_dual (attr := simps!) /-- The identity comonad. -/]
def id : Monad C where
  toFunctor := �� C
  η := �� (�� C)
  μ := �� (�� C)

@[to_dual]
instance : Inhabited (Monad C) :=
  ⟨Monad.id C⟩

open Iso CategoryTheory.Functor

variable {C}

/-- Transport a monad structure on a functor along an isomorphism of functors. -/
@[to_dual
/-- Transport a comonad structure on a functor along an isomorphism of functors. -/]
def transport {F : C ⥤ C} (T : Monad C) (i : (T : C ⥤ C) ≅ F) : Monad C where
  toFunctor := F
  η := T.η ≫ i.hom
  μ := (i.inv ◫ i.inv) ≫ T.μ ≫ i.hom
  left_unit X := by
    simp only [Functor.id_obj, NatTrans.comp_app, comp_obj, NatTrans.hcomp_app, Category.assoc,
      hom_inv_id_app_assoc]
    slice_lhs 1 2 => rw [← T.η.naturality (i.inv.app X)]
    simp
  right_unit X := by
    simp only [NatTrans.comp_app, Functor.map_comp, comp_obj, NatTrans.hcomp_app,
      Category.assoc, NatTrans.naturality_assoc]
    slice_lhs 2 4 =>
      simp only [← T.map_comp]
    simp
  assoc X := by
    simp only [comp_obj, NatTrans.comp_app, NatTrans.hcomp_app, Category.assoc, Functor.map_comp,
      NatTrans.naturality_assoc, hom_inv_id_app_assoc, NatIso.cancel_natIso_inv_left]
    slice_lhs 4 5 => rw [← T.map_comp]
    simp only [hom_inv_id_app, Functor.map_id, id_comp]
    slice_lhs 1 2 => rw [← T.map_comp]
    simp only [Functor.map_comp, Category.assoc]
    congr 1
    simp only [← Category.assoc, NatIso.cancel_natIso_hom_right]
    rw [← T.μ.naturality]
    simp [T.assoc X]

@[to_dual]
lemma map_unit_app (T : Monad C) (X : C) [IsIso T.μ] :
    T.map (T.η.app X) = T.η.app (T.obj X) := by
  simp [← cancel_mono (T.μ.app _)]

@[to_dual isSplitEpi_iff_isIso_counit]
lemma isSplitMono_iff_isIso_unit (T : Monad C) (X : C) [IsIso T.μ] :
    IsSplitMono (T.η.app X) ↔ IsIso (T.η.app X) := by
  refine ⟨fun _ ↦ ⟨retraction (T.η.app X), by simp, ?_⟩, fun _ ↦ inferInstance⟩
  rw [← map_id, ← show T.η.app X ≫ retraction (T.η.app X) = �� X from IsSplitMono.id _,
    map_comp, T.map_unit_app X, ← T.unit_naturality]

end Monad

end CategoryTheory

Messung
CHZG
9110010096

¤ Dauer der Verarbeitung: 0.2 Sekunden  (vorverarbeitet am  2026-11-24) ¤

*© Formatika GbR, Deutschland