module Cat.Base where

PrecategoriesπŸ”—

In univalent mathematics, it makes sense to distinguish two stages in the construction of categories: A precategory is the object that directly corresponds to the definition of precategory as it is traditionally formalised, whereas a category (or univalent category) has an extra condition: Isomorphic objects must be identified.

record Precategory (o h : Level) : Type (lsuc (o βŠ” h)) where
  no-eta-equality

A precategory is a β€œproof-relevant preorder”. In a preordered set the inhabitants of a set are related by a proposition which is

  • reflexive:
  • transitive:

In a precategory, the condition that be a proposition is relaxed: A precategory has a type of objects and, between each a set of relations (or maps). The name β€œβ€ is historical and it betrays the original context in which categories where employed: algebra(ic topology), where the maps in question are homomorphisms.

  field
    Ob  : Type o
    Hom : Ob β†’ Ob β†’ Type h

Whereas reading a classical definition into a type theory where equality is a proposition, the word set may be read to mean inhabitant of a universe. But in HoTT, if we want categories to be well-behaved, we do actually mean set: A type of h-level 2

  field
    Hom-set : (x y : Ob) β†’ is-set (Hom x y)

If you are already familiar with the definition of precategory there are two things to note here:

First is that our formalization has a family of Hom-sets rather than a single Hom-set and source/target maps. This formulation is not unique to precategory theory done internally to type theory, but it is the most reasonable way to formulate things in this context.

Second is that the word β€œset” in the definition of Hom-set has nothing to do with β€œsize”. Indeed, the β€œset”/β€œnot a set” (alternatively β€œsmall”/β€œlarge”) distinction makes no sense in type theory (some may argue it makes no sense in general).

Instead, the Precategory record is parametrised by two levels: o, and h. The type of objects has to fit in the universe Type o, and the family of Hom-sets is Type h valued. As an example, the thin precategory corresponding to the natural numbers with their usual ordering would live in Precategory lzero lzero.

This means, for instance, that there is no single β€œcategory of sets” - there is a family of categories of sets, parametrised by the level in which its objects live.

  field
    id  : βˆ€ {x}     β†’ Hom x x
    _∘_ : βˆ€ {x y z} β†’ Hom y z β†’ Hom x y β†’ Hom x z

  infixr 40 _∘_

The β€œproof-relevant” version of the reflexivity and transitivity laws are, respectively, the identity morphisms and composition of morphisms. Unlike in the proof-irrelevant case, in which an inhabitant of merely witnesses that two things are related, these operations matter, and thus must satisfy laws:

  field
    idr : βˆ€ {x y} (f : Hom x y) β†’ f ∘ id ≑ f
    idl : βˆ€ {x y} (f : Hom x y) β†’ id ∘ f ≑ f

The two identity laws say that the identity morphisms serve as neutral elements for the composition operation, both on the left and on the right. The β€œtwo” associativity laws (below) say that both ways of writing parentheses around a composition of three morphisms is equal:

    assoc : βˆ€ {w x y z} (f : Hom y z) (g : Hom x y) (h : Hom w x)
          β†’ f ∘ (g ∘ h) ≑ (f ∘ g) ∘ h

OppositesπŸ”—

A common theme throughout precategory theory is that of duality: The dual of a categorical concept is same concept, with β€œall the arrows inverted”. To make this formal, we introduce the idea of opposite categories: The opposite of written has the same objects, but with

infixl 60 _^op
_^op : βˆ€ {o₁ h₁} β†’ Precategory o₁ h₁ β†’ Precategory o₁ h₁
(C ^op) .Precategory.Ob = C .Precategory.Ob
(C ^op) .Precategory.Hom     x y = C .Precategory.Hom y x
(C ^op) .Precategory.Hom-set x y = C .Precategory.Hom-set y x
(C ^op) .Precategory.id      = C .Precategory.id
(C ^op) .Precategory._∘_ f g = C .Precategory._∘_ g f

Composition in the opposite precategory is β€œbackwards” with respect to This inversion, applied twice, ends up equal to what we started with by the nature of computation - An equality that arises like this, automatically from what Agda computes, is called definitional.

(C ^op) .Precategory.idl x = C .Precategory.idr x
(C ^op) .Precategory.idr x = C .Precategory.idl x

The left and right identity laws are swapped for the construction of the opposite precategory: For idr one has to show which computes into having to show that The case for idl is symmetric.

(C ^op) .Precategory.assoc f g h i = C .Precategory.assoc h g f (~ i)

For associativity, consider the case of assoc for the opposite precategory What we have to show is - by the type of assoc₁ - This computes into - which is exactly what sym (assoc C h g f) shows!

C^op^op≑C : βˆ€ {o β„“} {C : Precategory o β„“} β†’ C ^op ^op ≑ C
C^op^op≑C {C = C} i = precat i where
  open Precategory
  precat : C ^op ^op ≑ C
  precat i .Ob = C .Ob
  precat i .Hom = C .Hom
  precat i .Hom-set = C .Hom-set
  precat i .id = C .id
  precat i ._∘_ = C ._∘_
  precat i .idr = C .idr
  precat i .idl = C .idl
  precat i .assoc = C .assoc

The precategory of SetsπŸ”—

Given a universe level, we can consider the collection of all sets of that level. This assembles into a precategory quite nicely, since taking function types is an operation that preserves h-level.

module _ where
  open Precategory

  Sets : (o : _) β†’ Precategory (lsuc o) o
  Sets o .Ob = Set o
  Sets o .Hom A B = ∣ A ∣ β†’ ∣ B ∣
  Sets o .Hom-set _ B f g p q i j a =
    B .is-tr (f a) (g a) (happly p a) (happly q a) i j
  Sets o .id x = x
  Sets o ._∘_ f g x = f (g x)
  Sets o .idl f = refl
  Sets o .idr f = refl
  Sets o .assoc f g h = refl

FunctorsπŸ”—

Since a category is an algebraic structure, there is a natural definition of homomorphism of categories defined in the same fashion as, for instance, a homomorphism of groups. Since this kind of morphism is ubiquitous, it gets a shorter name: Functor.

Alternatively, functors can be characterised as the β€œproof-relevant version” of a monotone map: A monotone map is a map which preserves the ordering relation, Categorifying, β€œpreserves the ordering relation” becomes a function between Hom-sets.

  field
    Fβ‚€ : C.Ob β†’ D.Ob
    F₁ : βˆ€ {x y} β†’ C.Hom x y β†’ D.Hom (Fβ‚€ x) (Fβ‚€ y)

A Functor consists of a function between the object sets - and a function between Hom-sets - which takes to

  field
    F-id : βˆ€ {x} β†’ F₁ (C.id {x}) ≑ D.id
    F-∘ : βˆ€ {x y z} (f : C.Hom y z) (g : C.Hom x y)
        β†’ F₁ (f C.∘ g) ≑ F₁ f D.∘ F₁ g

Furthermore, the morphism mapping must be homomorphic: Identity morphisms are taken to identity morphisms (F-id) and compositions are taken to compositions (F-∘).

Functors also have duals: The opposite of is

  op : Functor (C ^op) (D ^op)
  op .Fβ‚€      = Fβ‚€
  op .F₁      = F₁
  op .F-id    = F-id
  op .F-∘ f g = F-∘ g f

CompositionπŸ”—

_F∘_ : βˆ€ {o₁ h₁ oβ‚‚ hβ‚‚ o₃ h₃}
         {C : Precategory o₁ h₁} {D : Precategory oβ‚‚ hβ‚‚} {E : Precategory o₃ h₃}
     β†’ Functor D E β†’ Functor C D β†’ Functor C E
_F∘_ {C = C} {D} {E} F G = comps

Functors, being made up of functions, can themselves be composed. The object mapping of is given by and similarly for the morphism mapping. Alternatively, composition of functors is a categorification of the fact that monotone maps compose.

    Fβ‚€ : C.Ob β†’ E.Ob
    Fβ‚€ x = F.Fβ‚€ (G.Fβ‚€ x)

    F₁ : {x y : C.Ob} β†’ C.Hom x y β†’ E.Hom (Fβ‚€ x) (Fβ‚€ y)
    F₁ f = F.F₁ (G.F₁ f)

To verify that the result is functorial, equational reasoning is employed, using the witnesses that and are functorial.

    abstract
      F-id : {x : C.Ob} β†’ F₁ (C.id {x}) ≑ E.id {Fβ‚€ x}
      F-id {x} =
          F.F₁ (G.F₁ C.id) β‰‘βŸ¨ ap F.F₁ G.F-id βŸ©β‰‘
          F.F₁ D.id        β‰‘βŸ¨ F.F-id βŸ©β‰‘
          E.id             ∎

      F-∘ : {x y z : C.Ob} (f : C.Hom y z) (g : C.Hom x y)
          β†’ F₁ (f C.∘ g) ≑ (F₁ f E.∘ F₁ g)
      F-∘ f g =
          F.F₁ (G.F₁ (f C.∘ g))     β‰‘βŸ¨ ap F.F₁ (G.F-∘ f g) βŸ©β‰‘
          F.F₁ (G.F₁ f D.∘ G.F₁ g)  β‰‘βŸ¨ F.F-∘ _ _ βŸ©β‰‘
          F₁ f E.∘ F₁ g             ∎

    comps : Functor _ _
    comps .Functor.Fβ‚€ = Fβ‚€
    comps .Functor.F₁ = F₁
    comps .Functor.F-id = F-id
    comps .Functor.F-∘ = F-∘

The identity functor can be defined using the identity funct_ion_ for both its object and morphism mappings. That functors have an identity and compose would seem to imply that categories form a category: However, since there is no upper bound on the h-level of Ob, we can not form a β€œcategory of categories”. If we do impose a bound, however, we can obtain a category of strict categories, those which have a set of objects.

Id : βˆ€ {o₁ h₁} {C : Precategory o₁ h₁} β†’ Functor C C
Id .Functor.Fβ‚€ x = x
Id .Functor.F₁ f = f
Id .Functor.F-id    = refl
Id .Functor.F-∘ f g = refl

Natural transformationsπŸ”—

Another common theme in category theory is that roughly every concept can be considered the objects of a category. This is the case for functors, as well! The functors between and assemble into a category, notated - the functor category between and

record _=>_ {o₁ h₁ oβ‚‚ hβ‚‚}
            {C : Precategory o₁ h₁}
            {D : Precategory oβ‚‚ hβ‚‚}
            (F G : Functor C D)
      : Type (o₁ βŠ” h₁ βŠ” hβ‚‚)
  where
  no-eta-equality
  constructor NT

The morphisms between functors are called natural transformations. A natural transformation can be thought of as a way of turning into that doesn’t involve any β€œarbitrary choices”.

  private
    module F = Functor F
    module G = Functor G
    module D = Precategory D
    module C = Precategory C

  field
    Ξ· : (x : _) β†’ D.Hom (F.β‚€ x) (G.β‚€ x)

The transformation itself is given by Ξ·, the family of components, where the component at is a map The β€œwithout arbitrary choices” part is encoded in the field is-natural, which encodes commutativity of the square below:

    is-natural : (x y : _) (f : C.Hom x y)
               β†’ Ξ· y D.∘ F.₁ f ≑ G.₁ f D.∘ Ξ· x

Natural transformations also dualise. The opposite of is

  op : Functor.op G => Functor.op F
  op = record
    { Ξ· = Ξ·
    ; is-natural = Ξ» x y f β†’ sym (is-natural _ _ f)
    }

Since the type of natural transformations is defined as a record, we can not a priori reason about its h-level in a convenient way. However, using Agda’s metaprogramming facilities (both reflection and instance search), we can automatically derive an equivalence between the type of natural transformations and a certain type; This type can then be shown to be a set using the standard hlevel machinery.

  opaque
    Nat-is-set : is-set (F => G)
    Nat-is-set = hlevel 2

Another fundamental lemma is that equality of natural transformations depends only on equality of the family of morphisms, since being natural is a proposition:

  Nat-pathp : {F' G' : Functor C D}
            β†’ (p : F ≑ F') (q : G ≑ G')
            β†’ {a : F => G} {b : F' => G'}
            β†’ (βˆ€ x β†’ PathP _ (a .Ξ· x) (b .Ξ· x))
            β†’ PathP (Ξ» i β†’ p i => q i) a b
  Nat-pathp p q path i .Ξ· x = path x i
  Nat-pathp p q {a} {b} path i .is-natural x y f =
    is-prop→pathp
      (Ξ» i β†’ D.Hom-set _ _
        (path y i D.∘ Functor.F₁ (p i) f) (Functor.F₁ (q i) f D.∘ path x i))
      (a .is-natural x y f)
      (b .is-natural x y f) i

  _Ξ·β‚š_ : βˆ€ {a b : F => G} β†’ a ≑ b β†’ βˆ€ x β†’ a .Ξ· x ≑ b .Ξ· x
  p Ξ·β‚š x = ap (Ξ» e β†’ e .Ξ· x) p

  _ηᡈ_ : βˆ€ {F' G' : Functor C D} {p : F ≑ F'} {q : G ≑ G'}
       β†’ {a : F => G} {b : F' => G'}
       β†’ PathP (Ξ» i β†’ p i => q i) a b
       β†’ βˆ€ x β†’ PathP (Ξ» i β†’ D.Hom (p i .Fβ‚€ x) (q i .Fβ‚€ x)) (a .Ξ· x) (b .Ξ· x)
  p ηᡈ x = apd (Ξ» i e β†’ e .Ξ· x) p

  infixl 45 _Ξ·β‚š_
open Precategory
open _=>_

instance
  Underlying-Precategory : βˆ€ {o β„“} β†’ Underlying (Precategory o β„“)
  Underlying-Precategory = record { ⌞_⌟ = Precategory.Ob }

  Funlike-Functor
    : βˆ€ {o β„“ o' β„“'} {C : Precategory o β„“} {D : Precategory o' β„“'}
    β†’ Funlike (Functor C D) ⌞ C ⌟ (Ξ» x β†’ ⌞ D ⌟)
  Funlike-Functor = record { _#_ = Functor.Fβ‚€ }

  Funlike-natural-transformation
    : βˆ€ {o β„“ o' β„“'} {C : Precategory o β„“} {D : Precategory o' β„“'} {F G : Functor C D}
    β†’ Funlike (F => G) ⌞ C ⌟ (Ξ» x β†’ D .Precategory.Hom (F # x) (G # x))
  Funlike-natural-transformation = record { _#_ = _=>_.Ξ· }

  Extensional-natural-transformation
    : βˆ€ {o β„“ o' β„“' β„“r} {C : Precategory o β„“} {D : Precategory o' β„“'}
    β†’ {F G : Functor C D}
    β†’ ⦃ sa : {x : ⌞ C ⌟} β†’ Extensional (D .Hom (F # x) (G # x)) β„“r ⦄
    β†’ Extensional (F => G) (o βŠ” β„“r)
  Extensional-natural-transformation ⦃ sa ⦄ .Pathᡉ f g = βˆ€ i β†’ Pathᡉ sa (f .Ξ· i) (g .Ξ· i)
  Extensional-natural-transformation ⦃ sa ⦄ .reflᡉ x i = reflᡉ sa (x .Ξ· i)
  Extensional-natural-transformation ⦃ sa ⦄ .idsᡉ .to-path x = Nat-pathp _ _ Ξ» i β†’
    sa .idsᡉ .to-path (x i)
  Extensional-natural-transformation {D = D} ⦃ sa ⦄ .idsᡉ .to-path-over h =
    is-propβ†’pathp (Ξ» i β†’ Ξ -is-hlevel 1 Ξ» _ β†’ Pathᡉ-is-hlevel 1 sa (hlevel 2)) _ _