module Cat.Diagram.Colimit.Base where



Colimits are dual to limits; much like their duals, they generalize constructions in several settings to arbitrary categories. A colimit (if it exists), is the “best solution” to an “identification problem”. This is in contrast to the limit, which acts as a solution to an “equational problem”.

Therefore, we define colimits in a similar way to limits. the only difference being that we define the colimit of a diagram FF as a left Kan extension instead of a right Kan extension. This gives us the expected “mapping out” universal property, as opposed to the “mapping in” property associated to limits.

Note that approach to colimits is not what normally presented in introductory material. Instead, most books opt to define colimits via cocones, as they are less abstract, though harder to work with in the long run.

module _ {J : Precategory o₁ h₁} {C : Precategory o₂ h₂} (Diagram : Functor J C) where
    module C = Precategory C

  cocone→unit : ∀ {x : C.Ob} → (Diagram => Const x) → Diagram => const! x F∘ !F
  unquoteDef cocone→unit = define-coherence cocone→unit

  is-colimit : (x : C.Ob) → Diagram => Const x → Type _
  is-colimit x cocone =
    is-lan !F Diagram (const! x) (cocone→unit cocone)

  Colimit : Type _
  Colimit = Lan !F Diagram


As mentioned, our definition is very abstract, meaning we can directly re-use definitions and theorems about Kan extensions in the setting of colimits. The trade-off is that while working with colimits in general is easier, working with specific colimits becomes more difficult, as the data we actually care about has been obfuscated.

One particularly egregious failure is… actually constructing colimits. The definition in terms of Lan hides the concrete data behind a few abstractions, which would be very tedious to write out each time. To work around this, we provide an auxiliary record type, make-is-colimit, as an intermediate step in constructing left extensions.

First, we require morphisms from the every value of the diagram to the coapex; taken as a family, we call it ϕ\phi. Moreover, if f:x→yf : x \to y is a morphism in the “shape” category J\mathcal{J}, we require ψy∘Ff=ψx\psi y \circ Ff = \psi x, which encodes the relevant naturality.

      ψ : (j : J.Ob) → C.Hom (F₀ j) coapex
      commutes : ∀ {x y} (f : J.Hom x y) → ψ y C.∘ F₁ f ≡ ψ x

The rest of the data ensures that ψ\psi is the universal family of maps with this property; if εj:Fj→x\varepsilon_j : Fj \to x is another natural family, then each εj\varepsilon_j factors through the coapex by a unique universal morphism:

        : ∀ {x : C.Ob}
        → (eps : ∀ j → C.Hom (F₀ j) x)
        → (∀ {x y} (f : J.Hom x y) → eps y C.∘ F₁ f ≡ eps x)
        → C.Hom coapex x
        : ∀ {j : J.Ob} {x : C.Ob}
        → (eps : ∀ j → C.Hom (F₀ j) x)
        → (p : ∀ {x y} (f : J.Hom x y) → eps y C.∘ F₁ f ≡ eps x)
        → universal eps p C.∘ ψ j ≡ eps j
        : ∀ {x : C.Ob}
        → (eps : ∀ j → C.Hom (F₀ j) x)
        → (p : ∀ {x y} (f : J.Hom x y) → eps y C.∘ F₁ f ≡ eps x)
        → (other : C.Hom coapex x)
        → (∀ j → other C.∘ ψ j ≡ eps j)
        → other ≡ universal eps p

Once we have this data, we can use it to construct a value of type is-colimit. The naturality condition we required above may seem too weak, but the full naturality condition can be derived from it and the rest of the data.

    : ∀ {Diagram : Functor J C} {coapex}
    → (mc : make-is-colimit Diagram coapex)
    → is-colimit Diagram coapex (to-cocone mc)
  to-is-colimit {Diagram} {coapex} mkcolim = colim where
    open make-is-colimit mkcolim
    open is-lan
    open Functor

    colim : is-colimit Diagram coapex (to-cocone mkcolim)
    colim .σ {M = M} α .η _ =
      universal (α .η) (λ f → α .is-natural _ _ f ∙ C.eliml (M .F-id))
    colim .σ {M = M} α .is-natural _ _ _ =
       C.idr _ ∙ C.introl (M .F-id)
    colim .σ-comm {α = α} = Nat-path λ j →
      factors (α .η) _
    colim .σ-uniq {α = α} {σ′ = σ′} p = Nat-path λ _ →
      sym $ unique (α .η) _ (σ′ .η _) (λ j → sym (p ηₚ j))

The concrete interface of make-is-colimit is also handy for consuming specific colimits. To enable this use case, we provide a function which unmakes a colimit.

    : ∀ {D : Functor J C} {F : Functor ⊤Cat C} {eta}
    → is-lan !F D F eta
    → make-is-colimit D (Functor.F₀ F tt)
  unmake-colimit {D} {F} {eta} colim = mc module unmake-colimit where
    coapex = Functor.F₀ F tt
    module eta = _=>_ eta
    open is-lan colim
    open Functor D
    open make-is-colimit
    open _=>_

    module _ {x} (eps : ∀ j → C.Hom (F₀ j) x)
                 (p : ∀ {x y} (f : J.Hom x y) →  eps y C.∘ F₁ f ≡ eps x)

      eps-nt : D => const! x F∘ !F
      eps-nt .η = eps
      eps-nt .is-natural _ _ f = p f ∙ sym (C.idl _)

      hom : C.Hom coapex x
      hom = σ {M = const! x} eps-nt .η tt

    mc : make-is-colimit D coapex
    mc .ψ = eta.η
    mc .commutes f = _ _ f ∙ C.eliml (Functor.F-id F)
    mc .universal = hom
    mc .factors e p = σ-comm {α = eps-nt e p} ηₚ _
    mc .unique {x = x} eta p other q =
      sym $ σ-uniq {σ′ = other-nt} (Nat-path λ j → sym (q j)) ηₚ tt
        other-nt : F => const! x
        other-nt .η _ = other
        other-nt .is-natural _ _ _ = C.elimr (Functor.F-id F) ∙ sym (C.idl _)

We also provide a similar interface for the bundled form of colimits.

module Colimit
  {J : Precategory o₁ h₁} {C : Precategory o₂ h₂} {D : Functor J C} (L : Colimit D)

The coapex of the colimit can be obtained by applying the extension functor to the single object of ⊤Cat.

  coapex : C.Ob
  coapex = Ext .F₀ tt

Furthermore, we can show that the apex is the colimit, in the sense of is-colimit, of the diagram. You’d think this is immediate, but unfortunately, proof assistants: is-colimit asks for the constant functor functor {∗}→C\{*\} \to \mathcal{C} with value coapex to be a Kan extension, but Colimit, being an instance of Lan, packages an arbitrary functor {∗}→C\{*\} \to \mathcal{C}.

Since Agda does not compare functors for η\eta-equality, we have to shuffle our data around manually. Fortunately, this isn’t a very long computation.

  cocone : D => Const coapex
  cocone .η = eta .η
  cocone .is-natural x y f =
    eta .is-natural x y f ∙ ap (C._∘ _) (Ext .F-id)

  has-colimit : is-colimit D coapex cocone
  has-colimit .is-lan.σ α .η = σ α .η
  has-colimit .is-lan.σ α .is-natural x y f =
    ap (_ C.∘_) (sym (Ext .F-id)) ∙ σ α .is-natural tt tt tt
  has-colimit .is-lan.σ-comm =
    Nat-path (λ _ → σ-comm ηₚ _)
  has-colimit .is-lan.σ-uniq {M = M} {σ′ = σ′} p =
    Nat-path (λ _ → σ-uniq {σ′ = nt} (Nat-path (λ j → p ηₚ j)) ηₚ _)
      nt : Ext => M
      nt .η = σ′ .η
      nt .is-natural x y f = ap (_ C.∘_) (Ext .F-id) ∙ σ′ .is-natural x y f

  open is-colimit has-colimit public


Much like limits, colimits are unique up to isomorphism. This all follows from general properties of Kan extensions, combined with the fact that natural isomorphisms between functors ⊤→C\top \to \mathcal{C} correspond with isomorphisms in C\mathcal{C}.

    : ∀ {f : C.Hom x y} {g : C.Hom y x}
    → (∀ {j : J.Ob} → f C.∘ Cx.ψ j ≡ Cy.ψ j)
    → (∀ {j : J.Ob} → g C.∘ Cy.ψ j ≡ Cx.ψ j)
    → C.Inverses f g

    : ∀ {f : C.Hom x y}
    → (∀ {j : J.Ob} → f C.∘ Cx.ψ j ≡ Cy.ψ j)
    → f

  colimits-unique     : x C.≅ y
  colimits→invertible : (Cx.universal Cy.ψ Cy.commutes)
    : C.Inverses (Cx.universal Cy.ψ Cy.commutes) (Cy.universal Cx.ψ Cx.commutes)

Furthermore, if the universal map is invertible, then that means its domain is also a colimit of the diagram. This also follows from a general theorem of Kan extensions, though some golfin is required to obtain the correct inverse definitionally.

    : ∀ {K' : Functor ⊤Cat C} {eta : D => K' F∘ !F}
    → (eps : ∀ j → C.Hom (D.₀ j) (K' .F₀ tt))
    → (p : ∀ {x y} (f : J.Hom x y) → eps y C.∘ D.₁ f ≡ eps x)
    → (∀ {j} → eps j ≡ eta .η j)
    → (Cy.universal eps p)
    → is-lan !F D K' eta
  is-invertible→is-colimitp {K' = K'} {eta = eta} eps p q invert =
      (is-invertible→is-lan Cy $ invertible→invertibleⁿ _ λ _ → invert)

Another useful fact is that if CC is a colimit of some diagram DiaDia, and DiaDia is naturally isomorphic to some other diagram Dia′Dia', then the coapex of CC is also a colimit of Dia′Dia'.

    : ∀ {D′ : Functor J C} {eta : D′ => K F∘ !F}
    → (isos : D ≅ⁿ D′)
    → (∀ {j} →  Cy.ψ j C.∘ Isoⁿ.from isos .η j ≡ eta .η j)
    → is-lan !F D′ K eta
  natural-iso-diagram→is-colimitp {D′ = D′} isos q = generalize-colimitp
    (natural-iso-of→is-lan Cy isos)

Since is-colimit is a proposition, and the colimiting cocones are all unique (“up to isomorphism”), if we’re talking about univalent categories, then Colimit itself is a proposition. This is also an instance of the more general uniqueness of Kan extensions.

  Colimit-is-prop : is-category C → is-prop (Colimit Diagram)
  Colimit-is-prop cat = Lan-is-prop cat

Preservation of Colimits🔗

The definitions here are the same idea as preservation of limits, just dualized.

  preserves-colimit : Type _
  preserves-colimit =
     ∀ {K : Functor ⊤Cat C} {eta : Diagram => K F∘ !F}
     → (colim : is-lan !F Diagram K eta)
     → preserves-lan F colim

  reflects-colimit : Type _
  reflects-colimit =
    ∀ {K : Functor ⊤Cat C} {eps : Diagram => K F∘ !F}
    → (lan : is-lan !F (F F∘ Diagram) (F F∘ K) (nat-assoc-to (F ▸ eps)))
    → reflects-lan F lan
module preserves-colimit
  {J : Precategory o₁ h₁} {C : Precategory o₂ h₂} {D : Precategory o₃ h₃}
  {F : Functor C D} {Dia : Functor J C}
  (preserves : preserves-colimit F Dia)

    module D = Precategory D
    module C = Precategory C
    module J = Precategory J
    module F = Func F
    module Dia = Func Dia

    : {x : C.Ob}
    → {K : Functor ⊤Cat C} {eta : Dia => K F∘ !F}
    → {eps : (j : J.Ob) → C.Hom (Dia.F₀ j) x}
    → {p : ∀ {i j} (f : J.Hom i j) → eps j C.∘ Dia.F₁ f ≡ eps i}
    → (colim : is-lan !F Dia K eta)
    → F.F₁ (is-colimit.universal colim eps p) ≡ is-colimit.universal (preserves colim) (λ j → F.F₁ (eps j)) (λ f → F.collapse (p f))
  universal colim = is-colimit.unique (preserves colim) _ _ _ (λ j → F.collapse (is-colimit.factors colim _ _))

  colimit : Colimit Dia → Colimit (F F∘ Dia)
  colimit colim = to-colimit (preserves (Colimit.has-colimit colim))

module _ {J : Precategory o₁ h₁} {C : Precategory o₂ h₂} {D : Precategory o₃ h₃}
         {F F' : Functor C D} {Dia : Functor J C} where

    module D = Cat.Reasoning D
    open Func
    open _=>_

    : F ≅ⁿ F'
    → preserves-colimit F Dia
    → preserves-colimit F' Dia
  natural-iso→preserves-colimits α F-preserves {K = K} {eps} colim =
      idni (α ◂ni Dia) (α ◂ni K)
      (Nat-path λ j →
        ⌜ F' .F₁ (K .F₁ tt) D.∘ α.to .η _ ⌝ D.∘ (F .F₁ (eps .η j) D.∘ α.from .η _) ≡⟨ ap! (eliml F' (K .F-id)) ⟩≡
        α.to .η _ D.∘ (F .F₁ (eps .η j) D.∘ α.from .η _)                           ≡⟨ D.pushr (sym (α.from .is-natural _ _ _)) ⟩≡
        ((α.to .η _ D.∘ α.from .η _) D.∘ F' .F₁ (eps .η j))                        ≡⟨ D.eliml (α.invl ηₚ _) ⟩≡
        F' .F₁ (eps .η j) ∎)
      (F-preserves colim)
      module α = Isoⁿ α


  : ∀ (oshape hshape : Level)
      {C : Precategory o₁ h₁}
      {D : Precategory o₂ h₂}
  → Functor C D → Type _

A cocontinuous functor is one that, for every shape of diagram J, and every diagram diagram of shape J in C, preserves the colimit for that diagram.

is-cocontinuous oshape hshape {C = C} F =
  ∀ {J : Precategory oshape hshape} {Diagram : Functor J C}
  → preserves-colimit F Diagram


A category is cocomplete if admits for limits of arbitrary shape. However, in the presence of excluded middle, if a category admits coproducts indexed by its class of morphisms, then it is automatically thin. Since excluded middle is independent of type theory, we can not prove that any non-thin categories have arbitrary colimits.

Instead, categories are cocomplete with respect to a pair of universes: A category is (o,ℓ)(o, \ell)-cocomplete if it has colimits for any diagram indexed by a precategory with objects in Type o\mathrm{Type}\ o and morphisms in Type ℓ\mathrm{Type}\ \ell.

is-cocomplete : ∀ {oc ℓc} o ℓ → Precategory oc ℓc → Type _
is-cocomplete oj ℓj C = ∀ {J : Precategory oj ℓj} (F : Functor J C) → Colimit F

While this condition might sound very strong, and thus that it would be hard to come by, it turns out we can get away with only two fundamental types of colimits: coproducts and coequalisers. In order to construct the colimit for a diagram of shape J\mathcal{J}, we will require coproducts indexed by J\mathcal{J}’s type of objects and by its type of morphisms.

    : ∀ {oj ℓj} {J : Precategory oj ℓj}
    → has-coproducts-indexed-by C (Precategory.Ob J)
    → has-coproducts-indexed-by C (Precategory.Mor J)
    → has-coequalisers C
    → (F : Functor J C) → Colimit F
  colimit-as-coequaliser-of-coproduct {oj} {ℓj} {J} has-Ob-cop has-Mor-cop has-coeq F =
    to-colimit (to-is-colimit colim) where

Given a diagram F:J→CF : \mathcal{J} \to \mathcal{C}, we start by building the coproduct of all the objects appearing in the diagram.

    Obs : Indexed-coproduct C λ o → F₀ o
    Obs = has-Ob-cop _

Our colimit will arise as a quotient object of this coproduct-of-objects, namely the coequaliser of two carefully chosen morphisms.

As a guiding example, the pushout of f:C→Af : C \to A and g:C→Bg : C \to B should be the quotient of A+B+CA + B + C by the equivalence relation generated by ιA(f(c))=ιC(c)=ιB(g(c))\iota_A(f(c)) = \iota_C(c) = \iota_B(g(c)). In full generality, for each arrow f:C→Af : C \to A in our diagram, we should have that injecting into the CCth component of our coproduct should give the same result as precomposing with ff and injecting into the AAth component.

This suggests to build another indexed coproduct of all the domains of arrows in the diagram, taking the first morphism to be the injection into the domain component and the second morphism to be the injection into the codomain component precomposed with ff:

    Dom : Indexed-coproduct C {Idx = J.Mor} λ (a , b , f) → F₀ a
    Dom = has-Mor-cop _

    s t : C.Hom (Dom .ΣF) (Obs .ΣF)
    s = Dom .match λ (a , b , f) → Obs .ι b C.∘ F₁ f
    t = Dom .match λ (a , b , f) → Obs .ι a

    coequ : Coequaliser C s t
    coequ = has-coeq _ _

    colim : make-is-colimit F (coequ .coapex)
The rest of the proof amounts to repackaging the data of the coequaliser and coproducts as the data for a colimit.
    colim .ψ c = coequ .coeq C.∘ Obs .ι c
    colim .commutes {a} {b} f =
      (coequ .coeq C.∘ Obs .ι b) C.∘ F₁ f          ≡˘⟨ C.extendr (Dom .commute) ⟩≡˘
      ⌜ coequ .coeq C.∘ s ⌝ C.∘ Dom .ι (a , b , f) ≡⟨ ap! (coequ .coequal) ⟩≡
      (coequ .coeq C.∘ t) C.∘ Dom .ι (a , b , f)   ≡⟨ C.pullr (Dom .commute) ⟩≡
      coequ .coeq C.∘ Obs .ι a                     ∎
    colim .universal {x} e comm = coequ .universal comm′ where
      e′ : C.Hom (Obs .ΣF) x
      e′ = Obs .match e
      comm′ : e′ C.∘ s ≡ e′ C.∘ t
      comm′ = Indexed-coproduct.unique₂ Dom λ i@(a , b , f) →
        (e′ C.∘ s) C.∘ Dom .ι i      ≡⟨ C.extendr (Dom .commute) ⟩≡
        ⌜ e′ C.∘ Obs .ι b ⌝ C.∘ F₁ f ≡⟨ ap! (Obs .commute) ⟩≡
        e b C.∘ F₁ f                 ≡⟨ comm f ⟩≡
        e a                          ≡˘⟨ Obs .commute ⟩≡˘
        e′ C.∘ Obs .ι a              ≡˘⟨ C.pullr (Dom .commute) ⟩≡˘
        (e′ C.∘ t) C.∘ Dom .ι i      ∎
    colim .factors {j} e comm =
      colim .universal e comm C.∘ (coequ .coeq C.∘ Obs .ι j) ≡⟨ C.pulll (coequ .factors) ⟩≡
      Obs .match e C.∘ Obs .ι j                              ≡⟨ Obs .commute ⟩≡
      e j                                                    ∎
    colim .unique e comm u′ fac = coequ .unique $ Obs .unique _
      λ i → sym (C.assoc _ _ _) ∙ fac i

This implies that a category with coequalisers and large enough indexed coproducts has all colimits.

    : ∀ {oj ℓj}
    → has-indexed-coproducts C (oj ⊔ ℓj)
    → has-coequalisers C
    → is-cocomplete oj ℓj C
  coproducts+coequalisers→cocomplete {oj} {ℓj} has-cop has-coeq =
      (λ _ → Lift-Indexed-coproduct C ℓj (has-cop _))
      (λ _ → has-cop _)