open import Algebra.Group.NAry
open import Algebra.Group.Ab

open import Cat.Diagram.Coproduct.Indexed
open import Cat.Diagram.Product.Indexed
open import Cat.Diagram.Limit.Finite
open import Cat.Abelian.Base
open import Cat.Prelude hiding (_-_ ; _+_)

open import Data.Id.Base
open import Data.Dec
open import Data.Fin

import Cat.Abelian.NAry

module Cat.Abelian.Limits {o β} {C : Precategory o β} where


# Limitsπ

Recall that every pre-abelian category admits kernels and cokernels, and is also additive, so it additionally has products and coproducts1. It sounds like weβre missing some finite limits (dually, missing some finite colimits), but it turns out that this is enough: We can construct the equaliser of $f, g : A \to B$ as $\ker(f - g)$ β whence the name difference kernel!

The calculation is straightforward: To map out of $\ker f$, we must have $(fe' - ge') = 0$, but this is immediate assuming that $fe' = ge'$ β an assumption we have to map out of $\mathrm{eq}(f,g)$. Similarly, to show that $f\ker(f-g) = g\ker(f-g)$, we calculate

$f\ker(f-g) - g\ker(f-g) = (f-g)\ker(f-g) = 0\text{.}$

module _ (A : is-pre-abelian C) where
open is-pre-abelian A
difference-kernel
: β {A B} {f g : Hom A B}
β is-equaliser f g (Ker.kernel (f - g))
difference-kernel {f = f} {g} = equ where
open is-equaliser
equ : is-equaliser f g (Ker.kernel (f - g))
equ .equal = zero-diff $(f β Ker.kernel (f - g)) - (g β Ker.kernel (f - g)) β‘β¨ β-minus-l f g (Ker.kernel (f - g)) β©β‘ (f - g) β Ker.kernel (f - g) β‘β¨ Ker.equal (f - g) β©β‘ β .zeroβ β Ker.kernel (f - g) β‘β¨ β .zero-βr _ β 0m-unique β©β‘ 0m β equ .universal {eβ² = eβ²} p = Ker.universal (f - g) {eβ² = eβ²}$
(f - g) β eβ²         β‘Λβ¨ β-minus-l _ _ _ β©β‘Λ
f β eβ² - g β eβ²      β‘β¨ ap (f β eβ² -_) (sym p) β©β‘
f β eβ² - f β eβ²      β‘β¨ Hom.inverser β©β‘
0m                   β‘Λβ¨ β.zero-βr _ β 0m-unique β©β‘Λ
Zero.zeroβ β β eβ²    β
equ .factors = Ker.factors _
equ .unique = Ker.unique (f - g)


By a standard characterisation of finite limits in terms of finite products and binary equalisers, the construction of βdifference kernelsβ above implies that any pre-abelian category is finitely complete.

  finitely-complete : Finitely-complete C
finitely-complete =
with-equalisers C has-terminal has-prods Ξ» f g β
record { has-is-eq = difference-kernel }


## Finite biproductsπ

An interesting property of $\mathbf{Ab}$-enriched categories is the coincidence of finite products and finite coproducts: not only is there a map2 $a \sqcup b \to a \times b$, defined from the universal properties, but it is also an isomorphism. This is, at least to the author, mildly unexpected, but it follows from the properties of zero morphisms.

More generally, suppose that $F : [n] \to \mathcal{A}$ is a finite family of objects in an $\mathbf{Ab}$-category. If $F$ has both a coproduct $\coprod F$ and a product $\prod F$ in $\mathcal{A}$, then we can define a map $\coprod F \to \prod F$ by giving a family of morphisms $F_i \to \prod F$, which amounts to defining a family of morphisms $F_i \to F_j$3. Since $[n]$ is discrete, we can define this family to be $\operatorname{id}_{} : F_i \to F_i$ when $i = j$ and $0 : F_i \to F_j$ everywhere else!

What we actually prove in this module is a slight variation: we directly construct, given a product $\prod F$, the structure of a coproduct on $\prod F$. Uniqueness of coproducts then implies that, if some other $\coprod F$ exists, then it must be isomorphic to $\coprod F$.

module _ (A : Ab-category C) {I : Nat} (F : Fin I β C .Precategory.Ob)
(ip : Indexed-product C F) where
private
module A = Ab-category A
module ip = Indexed-product ip
open Cat.Abelian.NAry A


The first thing weβll prove is a compatibility condition between tupling and sums: A sum of tuples is a tuple of sums. In the binary case, weβre showing that $(a, b) + (c, d)$ is $(a + c, b + d)$.

  tuple-sum : β {j} {R} (f : Fin j β β i β A.Hom R (F i))
β ip.tuple (Ξ» i β ββ j (Ξ» j β f j i))
β‘ ββ j Ξ» j β ip.tuple (f j)
tuple-sum {j} f = sym $ip.unique _ Ξ» i β ip.Ο i A.β ββ j (Ξ» i β ip.tuple (f i)) β‘β¨ β-β-left {j = j} _ β©β‘ ββ j (Ξ» j β ip.Ο i A.β ip.tuple (f j)) β‘β¨ β-path {j} _ (Ξ» j β ip.commute) β©β‘ ββ j (Ξ» j β f j i) β  We then define our function $\delta_{i,j} : F_i \to F_j$ which is the identity on the diagonal.  private Ξ΄β² : (i j : Fin I) β Dec (i β‘α΅’ j) β A.Hom (F i) (F j) Ξ΄β² i j (yes reflα΅’) = A.id Ξ΄β² i j (no x) = A.0m Ξ΄ : β i j β A.Hom (F i) (F j) Ξ΄ i j = Ξ΄β² i j (Discreteα΅’-Fin i j) Ξ΄α΅’α΅’ : β i d β Ξ΄β² i i d β‘ A.id Ξ΄α΅’α΅’ i (yes x) = ap (Ξ΄β² i i)$
ap yes (is-setβis-setα΅’ Fin-is-set i i x reflα΅’)
Ξ΄α΅’α΅’ i (no Β¬x=x) = absurd (Β¬x=x reflα΅’)

Ξ΄α΅’β±Ό : β i j β Β¬ i β‘ j β (d : Dec (i β‘α΅’ j)) β Ξ΄β² i j d β‘ A.0m
Ξ΄α΅’β±Ό i j iβ j (yes i=j) = absurd (iβ j (Idβpath.to i=j))
Ξ΄α΅’β±Ό i j iβ j (no _)    = refl


We can now define a factoring of the identity on $\prod F$ through a β hypothetical β $\coprod F$. A βsplittingβ map $\prod F \to \prod F$, which works by summing (over $j$) the tupling (over $i$) of $\delta_{i,j}\pi_j$; and since tupling commutes with sums, we conclude that this is the tupling over $i$ of a bunch of tuples, zero on every component except for the one corresponding to their index in the sum. In the binary case, weβre showing that

$(1, 0)\pi_1 + (0,1)\pi_2 = (\pi_1, 0) + (0, \pi_2) = (\pi_1, \pi_2) = \operatorname{id}_{}\text{.}$

  split = ββ I Ξ» j β ip.tuple Ξ» i β Ξ΄ j i A.β ip.Ο j

private
split-remark : A.id β‘ split
split-remark = ip.unique ip.Ο (Ξ» _ β A.idr _) β sym (ip.unique _ ΟΞ£Ξ΄Ο) where
sum-Ξ΄-Ο : β i β β {I} _ (Ξ» j β Ξ΄ j i A.β ip.Ο j) β‘ ip.Ο i
sum-Ξ΄-Ο i = β-diagonal-lemma (AbelianβGroup-on (A.Abelian-group-on-hom _ _)) {I} i _
(A.eliml (Ξ΄α΅’α΅’ i (Discreteα΅’-Fin i i)))
Ξ» j iβ j β
apβ A._β_ (Ξ΄α΅’β±Ό j i (Ξ» e β iβ j (sym e)) (Discreteα΅’-Fin j i))
refl
β A.β-zero-l

ΟΞ£Ξ΄Ο : β i β ip.Ο i A.β split β‘ ip.Ο i
ΟΞ£Ξ΄Ο i =
ip.Ο i A.β ββ I (Ξ» i β ip.tuple Ξ» j β Ξ΄ i j A.β ip.Ο i) β‘β¨ ap (ip.Ο i A.β_) (sym (tuple-sum {I} _)) β©β‘
ip.Ο i A.β ip.tuple (Ξ» i β ββ I Ξ» j β Ξ΄ j i A.β ip.Ο j) β‘β¨ ip.commute β©β‘
ββ I (Ξ» j β Ξ΄ j i A.β ip.Ο j)                           β‘β¨ sum-Ξ΄-Ο i β©β‘
ip.Ο i                                                  β


We can now use this split morphism to construct the coproduct structure on $\prod F$. First, the i-th coprojection $\iota_i : F_i \to \prod F$ is a tuple where all but the $i$-th coordinate are $0$, and $i$ is one. That is: itβs the tuple over $j$ of $\delta_{i,j}$!

  open Indexed-coproduct
open is-indexed-coproduct
coprod : Indexed-coproduct C F
coprod .Ξ£F = ip.Ξ F
coprod .ΞΉ i = ip.tuple (Ξ΄ i)


We now need to define the βmatchβ function: Given a family $f : F_i \to Y$, how do we extend this to a (unique) map $m_f$ satisfying $m_f \iota_j = f_j$? Well, one potential approach is define $m_f$ to be some kind of sum β letβs say itβs a sum over $f'_i$, where $f'_i$ is something weβll define later. We can still calculate

$m_f \iota_j = (\sum_i f'_i) \iota_{j} = \sum_i f'_i \iota_j$

so we have to choose $f'_i$ such that $f'_i$ is $f$ when $i = j$, and $0$ otherwise, so only the $f$ term contributes to the above sum. And we know that, composed with $\iota_j$, the $j$-th projection is the identity function, and all others are $0$ β so if we define $f'_i = f\pi_i$, then we certainly have $(\sum_i f'_i) \iota_j$ = $f$!

  coprod .has-is-ic = ico where
m : β {Y} β (β i β A.Hom (F i) Y) β A.Hom (ip .Indexed-product.Ξ F) Y
m f = ββ I Ξ» j β f j A.β ip.Ο j

ico : is-indexed-coproduct C F _
ico .match f = m f
ico .commute {i = i} {f = f} =
m f A.β ip.tuple (Ξ΄ i)                           β‘β¨ β-β-right {I} _ β©β‘
ββ I (Ξ» j β (f j A.β ip.Ο j) A.β ip.tuple (Ξ΄ i)) β‘β¨ remark β©β‘
f i                                              β

      where
remark = β-diagonal-lemma (AbelianβGroup-on (A.Abelian-group-on-hom _ _)) {I} i _
(A.cancelr (ip.commute β Ξ΄α΅’α΅’ i (Discreteα΅’-Fin i i)))
Ξ» j iβ j β A.pullr (ip.commute β Ξ΄α΅’β±Ό i j iβ j (Discreteα΅’-Fin i j))
β A.β-zero-r


And how do we show uniqueness? Using our remark about the split morphism defined above! It shows that any map $m' : \prod F \to Y$ has to factor through something that looks a lot like our definition of $m_f$ above, and if it also satisfies $h\iota_j = f_j$, then a bit of massaging shows it is exactly $m_f$.

    ico .unique {h = h} f prf =
h                                                    β‘β¨ A.intror (sym split-remark) β©β‘
h A.β split                                          β‘β¨ β-β-left {I} _ β©β‘
ββ I (Ξ» i β h A.β ip.tuple (Ξ» j β Ξ΄ i j A.β ip.Ο i)) β‘β¨ β-path {I} _ (Ξ» i β ap (h A.β_) (sym (tupleβ C F ip _))) β©β‘
ββ I (Ξ» i β h A.β ip.tuple (Ξ» j β Ξ΄ i j) A.β ip.Ο i) β‘β¨ β-path {I} _ (Ξ» i β A.pulll (prf i)) β©β‘
ββ I (Ξ» i β f i A.β ip.Ο i)                          β‘β¨β©
m f                                                  β


1. Weβll see in this very same module that theyβre actually the same thing!β©οΈ

3. Where $i, j$ both range over $[n]$.β©οΈ