module Cat.Diagram.Coend.Formula
  {o ā„“ o′ ℓ′} {C : Precategory o ā„“} {D : Precategory o′ ℓ′}
  where

Computing coendsšŸ”—

Using the twisted arrow category as a mediating notion, we show how to compute coends as ordinary colimits. The calculation is actually a bit more straightforward than it might seem at first. The first thing we note is that any functor F:CopƗC→DF : C^{\mathrm{op}} \times C \to D generates a functor from the twisted arrow category of Cop\mathcal{C}^{\mathrm{op}}:

Tw(Cop)op→πtCopƗC→FD \mathrm{Tw}(\mathcal{C}^{\mathrm{op}})^{\mathrm{op}} \xrightarrow{\pi_t} C^{\mathrm{op}} \times C \xrightarrow{F} D

This is the fundamental part of our theorem: The twisted arrow category, in a sense, ā€œclassifies cowedgesā€, in that cocones under FĻ€tF\pi_t (the composite above) are the same thing as cowedges from FF. The proof is entirely shuffling some data around, but the commutativity/extranaturality conditions need to be massaged a bit. Check it out, it’s not too long:

module _ (F : Functor (C ^op Ć—į¶œ C) D) where
  private
    module C = Cat C
    module D = Cat D
    module F = F-r F
    open _=>_
    open Twist
    open Bifunctor

  cocone→cowedge : āˆ€ {x} → twistįµ’įµ– F => Const x → Cowedge F
  cocone→cowedge eta .nadir = _
  cocone→cowedge eta .ψ c = eta .Ī· ((c , c) , C.id)
  cocone→cowedge eta .extranatural f =
    eta .is-natural _ _ (twist _ _ (C.eliml (C.idl _)))
    āˆ™ (sym $ eta .is-natural _ _ (twist _ _ (C.cancelr (C.idl _))))

  cowedge→cocone : (W : Cowedge F) → twistįµ’įµ– F => Const (W .nadir)
  cowedge→cocone W .Ī· ((c , c') , f) = W .ψ c D.∘ second F f
  cowedge→cocone W .is-natural ((a , b) , f) ((x , y) , g) h =
    (W .ψ x D.∘ F.F₁ (C.id , g)) D.∘ F.F₁ (_ , _)                           ā‰”āŸØ W .extranatural g D.⟩∘⟨refl āŸ©ā‰”
    (W .ψ y D.∘ F.F₁ (g , C.id)) D.∘ F.F₁ (h .before , h .after)            ā‰”āŸØ D.pullr (F.weave (C.introl refl ,ā‚š refl)) āŸ©ā‰”
    W .ψ y D.∘ ((F.F₁ (h .before C.∘ g , C.id)) D.∘ F.F₁ (C.id , h .after)) ā‰”āŸØ D.extendl (sym (W .extranatural _)) āŸ©ā‰”
    (W .ψ a D.∘ (F.F₁ (C.id , h .before C.∘ g) D.∘ F.F₁ (C.id , h .after))) ā‰”āŸØ D.refl⟩∘⟨ sym (Bifunctor.second∘second F) āˆ™ ap (Bifunctor.second F) (h .commutes) āŸ©ā‰”
    W .ψ a D.∘ F.F₁ (C.id , f)                                              ā‰”āŸØ sym (D.idl _) āŸ©ā‰”
    D.id D.∘ W .ψ a D.∘ F.F₁ (C.id , f) āˆŽ

We can now extend that correspondence to calculating coends as certain colimits: D\mathcal{D} has a coend for FF if it has a colimit for FĻ€tF\pi_t.

  colimit→coend : Colimit (twistįµ’įµ– F) → Coend F
  colimit→coend colim = coend where
    open Coend
    module W = Colimit colim
    coend : Coend F
    coend .cowedge = cocone→cowedge W.cocone
    coend .factor W′ =
      W.universal
        (cowedge→cocone W′ .Ī·)
        (Ī» f → cowedge→cocone W′ .is-natural _ _ f āˆ™ D.idl _)
    coend .commutes {W = W′} =
      W.factors _ _ āˆ™ D.elimr (Bifunctor.second-id F)
    coend .unique {W = W′} comm =
      W.unique _ _ _ $ Ī» j →
        sym $
          W′ .extranatural _
          Ā·Ā· D.pushl (sym comm)
          ·· (D.refl⟩∘⟨ (W.commutes (twist _ _ (C.cancelr (C.idl _)))))

  cocomplete→coend : is-cocomplete (o āŠ” ā„“) ā„“ D → Coend F
  cocomplete→coend colim = colimit→coend (colim _)

  module cocompleteā†’āˆ« (cocomp : is-cocomplete (o āŠ” ā„“) ā„“ D) where
    open Coend (cocomplete→coend cocomp) public