module Cat.Diagram.Colimit.Base where

ColimitsπŸ”—

IdeaπŸ”—

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 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
  private
    module C = Precategory C

  is-colimit : (x : C.Ob) β†’ Diagram => Const x β†’ Type _
  is-colimit x cocone =
    is-lan !F Diagram (!Const x) cocone

  Colimit : Type _
  Colimit = Lan !F Diagram

ConcretelyπŸ”—

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 Moreover, if is a morphism in the β€œshape” category we require which encodes the relevant naturality.

    field
      ψ : (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 is the universal family of maps with this property; if is another natural family, then each factors through the coapex by a unique universal morphism:

      universal
        : βˆ€ {x : C.Ob}
        β†’ (eta : βˆ€ j β†’ C.Hom (Fβ‚€ j) x)
        β†’ (βˆ€ {x y} (f : J.Hom x y) β†’ eta y C.∘ F₁ f ≑ eta x)
        β†’ C.Hom coapex x
      factors
        : βˆ€ {j : J.Ob} {x : C.Ob}
        β†’ (eta : βˆ€ j β†’ C.Hom (Fβ‚€ j) x)
        β†’ (p : βˆ€ {x y} (f : J.Hom x y) β†’ eta y C.∘ F₁ f ≑ eta x)
        β†’ universal eta p C.∘ ψ j ≑ eta j
      unique
        : βˆ€ {x : C.Ob}
        β†’ (eta : βˆ€ j β†’ C.Hom (Fβ‚€ j) x)
        β†’ (p : βˆ€ {x y} (f : J.Hom x y) β†’ eta y C.∘ F₁ f ≑ eta x)
        β†’ (other : C.Hom coapex x)
        β†’ (βˆ€ j β†’ other C.∘ ψ j ≑ eta j)
        β†’ other ≑ universal eta 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.

  to-is-colimit
    : βˆ€ {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 {Ξ± = Ξ±} = ext Ξ» j β†’ factors (Ξ± .Ξ·) _
    colim .Οƒ-uniq {Ξ± = Ξ±} {Οƒ' = Οƒ'} p = ext Ξ» _ β†’
      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.

  unmake-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} (eta : βˆ€ j β†’ C.Hom (Fβ‚€ j) x)
                 (p : βˆ€ {x y} (f : J.Hom x y) β†’  eta y C.∘ F₁ f ≑ eta x)
      where

      eta-nt : D => Const x
      eta-nt .Ξ· = eta
      eta-nt .is-natural _ _ f = p f βˆ™ sym (C.idl _)

      hom : C.Hom coapex x
      hom = Οƒ {M = !Const x} eta-nt .Ξ· tt

    mc : make-is-colimit D coapex
    mc .ψ = eta.η
    mc .commutes f = eta.is-natural _ _ f βˆ™ C.eliml (F .Functor.F-id)
    mc .universal = hom
    mc .factors e p = Οƒ-comm {Ξ± = eta-nt e p} Ξ·β‚š _
    mc .unique {x = x} eta p other q =
      sym $ Οƒ-uniq {Οƒ' = other-nt} (ext Ξ» j β†’ sym (q j)) Ξ·β‚š tt
      where
        other-nt : F => !Const x
        other-nt .Ξ· _ = other
        other-nt .is-natural _ _ _ = C.elimr (F .Functor.F-id) βˆ™ 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)
  where

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 with value coapex to be a Kan extension, but Colimit, being an instance of Lan, packages an arbitrary functor

Since Agda does not compare functors for 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 = ext (Οƒ-comm Ξ·β‚š_)
  has-colimit .is-lan.Οƒ-uniq {M = M} {Οƒ' = Οƒ'} p =
    ext (Ξ» _ β†’ Οƒ-uniq {Οƒ' = nt} (reext! p) Ξ·β‚š _)
    where
      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

UniquenessπŸ”—

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 correspond with isomorphisms in

  colimits→inversesp
    : βˆ€ {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

  colimits→invertiblep
    : βˆ€ {f : C.Hom x y}
    β†’ (βˆ€ {j : J.Ob} β†’ f C.∘ Cx.ψ j ≑ Cy.ψ j)
    β†’ C.is-invertible f

  colimits-unique     : x C.β‰… y
  colimitsβ†’invertible : C.is-invertible (Cx.universal Cy.ψ Cy.commutes)
  colimits→inverses
    : 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 golfing is required to obtain the correct inverse definitionally.

  is-invertible→is-colimitp
    : βˆ€ {K' : Functor ⊀Cat C} {eta : D => K' F∘ !F}
    β†’ (eta' : βˆ€ j β†’ C.Hom (D.β‚€ j) (K' .Fβ‚€ tt))
    β†’ (p : βˆ€ {x y} (f : J.Hom x y) β†’ eta' y C.∘ D.₁ f ≑ eta' x)
    β†’ (βˆ€ {j} β†’ eta' j ≑ eta .Ξ· j)
    β†’ C.is-invertible (Cy.universal eta' p)
    β†’ is-lan !F D K' eta
  is-invertible→is-colimitp {K' = K'} {eta = eta} eta' p q invert =
    generalize-colimitp
      (is-invertibleβ†’is-lan Cy $ invertibleβ†’invertibleⁿ _ Ξ» _ β†’ invert)
      q

Another useful fact is that if is a colimit of some diagram and is naturally isomorphic to some other diagram then the coapex of is also a colimit of

  natural-iso-diagram→is-colimitp
    : βˆ€ {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)
    q

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 and reflection of colimitsπŸ”—

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

  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} {eta : Diagram => K F∘ !F}
    β†’ (lan : is-lan !F (F F∘ Diagram) (F F∘ K) (nat-assoc-to (F β–Έ eta)))
    β†’ 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)

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

  universal
    : {x : C.Ob}
    β†’ {K : Functor ⊀Cat C} {eta : Dia => K F∘ !F}
    β†’ {eta' : (j : J.Ob) β†’ C.Hom (Dia.Fβ‚€ j) x}
    β†’ {p : βˆ€ {i j} (f : J.Hom i j) β†’ eta' j C.∘ Dia.F₁ f ≑ eta' i}
    β†’ (colim : is-lan !F Dia K eta)
    β†’ F.F₁ (is-colimit.universal colim eta' p) ≑ is-colimit.universal (preserves colim) (Ξ» j β†’ F.F₁ (eta' 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

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

  natural-iso→preserves-colimits
    : F ≅ⁿ F'
    β†’ preserves-colimit F Dia
    β†’ preserves-colimit F' Dia
  natural-iso→preserves-colimits α F-preserves {K = K} {eta} colim =
    natural-isos→is-lan idni (α ◂ni Dia) (α ◂ni K)
      (ext Ξ» j β†’
        ⌜ F' .F₁ (K .F₁ tt) D.∘ Ξ±.to .Ξ· _ ⌝ D.∘ (F .F₁ (eta .Ξ· j) D.∘ Ξ±.from .Ξ· _) β‰‘βŸ¨ ap! (eliml F' (K .F-id)) βŸ©β‰‘
        Ξ±.to .Ξ· _ D.∘ (F .F₁ (eta .Ξ· j) D.∘ Ξ±.from .Ξ· _)                           β‰‘βŸ¨ D.pushr (sym (Ξ±.from .is-natural _ _ _)) βŸ©β‰‘
        ((Ξ±.to .Ξ· _ D.∘ Ξ±.from .Ξ· _) D.∘ F' .F₁ (eta .Ξ· j))                        β‰‘βŸ¨ D.eliml (Ξ±.invl Ξ·β‚š _) βŸ©β‰‘
        F' .F₁ (eta .Ξ· j) ∎)
      (F-preserves colim)
    where
      module α = Isoⁿ α

CocontinuityπŸ”—

is-cocontinuous
  : βˆ€ (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

CocompletenessπŸ”—

A category is cocomplete if it admits colimits for diagrams 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 if it has colimits for any diagram indexed by a precategory with objects in and morphisms in

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 we will require coproducts indexed by type of objects and by its type of morphisms.

  colimit-as-coequaliser-of-coproduct
    : βˆ€ {oj β„“j} {J : Precategory oj β„“j}
    β†’ has-coproducts-indexed-by C ⌞ J ⌟
    β†’ has-coproducts-indexed-by C (Arrows J)
    β†’ has-coequalisers C
    β†’ (F : Functor J C) β†’ Colimit F
  colimit-as-coequaliser-of-coproduct {oj} {β„“j} {J} has-Ob-cop has-Arrows-cop has-coeq F =
    to-colimit (to-is-colimit colim) where

Given a diagram 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 and should be the quotient of by the equivalence relation generated by In full generality, for each arrow in our diagram, we should have that injecting into the component of our coproduct should give the same result as precomposing with and injecting into the 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

    Dom : Indexed-coproduct C {Idx = Arrows J} Ξ» (a , b , f) β†’ Fβ‚€ a
    Dom = has-Arrows-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.

  coproducts+coequalisers→cocomplete
    : βˆ€ {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 =
    colimit-as-coequaliser-of-coproduct
      (Ξ» _ β†’ Lift-Indexed-coproduct C β„“j (has-cop _))
      (Ξ» _ β†’ has-cop _)
      has-coeq