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
: Type o
Ob : Ob → Ob → Type h Hom
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
: (x y : Ob) → is-set (Hom x y) Hom-set
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
: ∀ {x} → Hom x x
id _∘_ : ∀ {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
: ∀ {x y} (f : Hom x y) → f ∘ id ≡ f
idr : ∀ {x y} (f : Hom x y) → id ∘ f ≡ f idl
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 associativity law (below) says that both ways of writing parentheses around a composition of three morphisms are equal:
: ∀ {w x y z} (f : Hom y z) (g : Hom x y) (h : Hom w x)
assoc → f ∘ (g ∘ h) ≡ (f ∘ g) ∘ h
{-# INLINE Precategory.constructor #-}
open hlevel-projection
private
: ∀ {o ℓ} (C : Precategory o ℓ) {x y} → is-set (C .Precategory.Hom x y)
hom-set = C .Precategory.Hom-set _ _
hom-set C
instance
: hlevel-projection (quote Precategory.Hom)
hlevel-proj-hom .has-level = quote hom-set
hlevel-proj-hom .get-level _ = pure (quoteTerm (suc (suc zero)))
hlevel-proj-hom .get-argument (_ ∷ _ ∷ c v∷ _) = pure c
hlevel-proj-hom {-# CATCHALL #-}
.get-argument _ = typeError [] hlevel-proj-hom
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!
: ∀ {o ℓ} {C : Precategory o ℓ} → C ^op ^op ≡ C
C^op^op≡C {C = C} i = precat i where
C^op^op≡C open Precategory
: C ^op ^op ≡ C
precat .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 precat i
{-# REWRITE C^op^op≡C #-}
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
: (o : _) → Precategory (lsuc o) o
Sets .Ob = Set o
Sets o .Hom A B = ∣ A ∣ → ∣ B ∣
Sets o .Hom-set _ B f g p q i j a =
Sets o .is-tr (f a) (g a) (happly p a) (happly q a) i j
B .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 Sets o
Functors🔗
record
Functor{o₁ h₁ o₂ h₂}
(C : Precategory o₁ h₁)
(D : Precategory o₂ h₂)
: Type (o₁ ⊔ h₁ ⊔ o₂ ⊔ h₂)
where
no-eta-equality
private
module C = Precategory C
module D = Precategory D
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
: C.Ob → D.Ob
F₀ : ∀ {x y} → C.Hom x y → D.Hom (F₀ x) (F₀ y) F₁
A Functor
consists of a function between the object sets
-
and a function between Hom-sets
- which takes
to
field
: ∀ {x} → F₁ (C.id {x}) ≡ D.id
F-id : ∀ {x y z} (f : C.Hom y z) (g : C.Hom x y)
F-∘ → 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-∘
).
-- Alias for F₀ for use in Functor record modules.
: C.Ob → D.Ob
₀ = F₀
₀
-- Alias for F₁ for use in Functor record modules.
: ∀ {x y} → C.Hom x y → D.Hom (F₀ x) (F₀ y)
₁ = F₁ ₁
Functors also have duals: The opposite of is
: Functor (C ^op) (D ^op)
op .F₀ = F₀
op .F₁ = F₁
op .F-id = F-id
op .F-∘ f g = F-∘ g f op
: ∀ {o ℓ o' ℓ'} {C : Precategory o ℓ} {D : Precategory o' ℓ'} {F : Functor C D}
F^op^op≡F → Functor.op (Functor.op F) ≡ F
{F = F} i .Functor.F₀ = F .Functor.F₀
F^op^op≡F {F = F} i .Functor.F₁ = F .Functor.F₁
F^op^op≡F {F = F} i .Functor.F-id = F .Functor.F-id
F^op^op≡F {F = F} i .Functor.F-∘ = F .Functor.F-∘
F^op^op≡F
{-# REWRITE F^op^op≡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.
module F∘ where
module C = Precategory C
module D = Precategory D
module E = Precategory E
module F = Functor F
module G = Functor G
: C.Ob → E.Ob
F₀ = F.F₀ (G.F₀ x)
F₀ x
: {x y : C.Ob} → C.Hom x y → E.Hom (F₀ x) (F₀ y)
F₁ = F.F₁ (G.F₁ f) F₁ f
To verify that the result is functorial, equational reasoning is employed, using the witnesses that and are functorial.
abstract
: {x : C.Ob} → F₁ (C.id {x}) ≡ E.id {F₀ x}
F-id {x} =
F-id .F₁ (G.F₁ C.id) ≡⟨ ap F.F₁ G.F-id ⟩
F.F₁ D.id ≡⟨ F.F-id ⟩
F.id ∎
E
: {x y z : C.Ob} (f : C.Hom y z) (g : C.Hom x y)
F-∘ → F₁ (f C.∘ g) ≡ (F₁ f E.∘ F₁ g)
=
F-∘ f g .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₁ g ∎
F₁ f E
: Functor _ _
comps .Functor.F₀ = F₀
comps .Functor.F₁ = F₁
comps .Functor.F-id = F-id
comps .Functor.F-∘ = F-∘ comps
{-# DISPLAY F∘.comps F G = F F∘ G #-}
The identity functor can be defined using the identity
function 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.
: ∀ {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 Id
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:
: (x y : _) (f : C.Hom x y)
is-natural → η y D.∘ F.₁ f ≡ G.₁ f D.∘ η x
Natural transformations also dualise. The opposite of is
: Functor.op G => Functor.op F
op = record
op { η = η
; is-natural = λ x y f → sym (is-natural _ _ f)
}
{-# INLINE NT #-}
is-natural-transformation: ∀ {o ℓ o' ℓ'} {C : Precategory o ℓ} {D : Precategory o' ℓ'}
→ (F G : Functor C D)
→ (η : ∀ x → D .Precategory.Hom (F .Functor.F₀ x) (G .Functor.F₀ x))
→ Type _
{C = C} {D = D} F G η =
is-natural-transformation ∀ x y (f : C .Precategory.Hom x y) → η y D.∘ F .F₁ f ≡ G .F₁ f D.∘ η x
where module D = Precategory D
open Functor
infixr 30 _F∘_
infix 20 _=>_
unquoteDecl H-Level-Nat = declare-record-hlevel 2 H-Level-Nat (quote _=>_)
module _ {o₁ h₁ o₂ h₂}
{C : Precategory o₁ h₁}
{D : Precategory o₂ h₂}
{F G : Functor C D} where
private
module F = Functor F
module G = Functor G
module D = Precategory D
module C = Precategory C
open Functor
open _=>_
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: is-set (F => G)
Nat-is-set = hlevel 2 Nat-is-set
Another fundamental lemma is that equality of natural transformations depends only on equality of the family of morphisms, since being natural is a proposition:
: {F' G' : Functor C D}
Nat-pathp → (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
.η x = path x i
Nat-pathp p q path i {a} {b} path i .is-natural x y f =
Nat-pathp p q
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}
Nat-path → ((x : _) → a .η x ≡ b .η x)
→ a ≡ b
= Nat-pathp refl refl
Nat-path
_ηₚ_ : ∀ {a b : F => G} → a ≡ b → ∀ x → a .η x ≡ b .η x
= ap (λ e → e .η x) p
p ηₚ x
_ηᵈ_ : ∀ {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)
= apd (λ i e → e .η x) p
p ηᵈ x
infixl 45 _ηₚ_
open Precategory
open _=>_
instance
: ∀ {o ℓ} → Underlying (Precategory o ℓ)
Underlying-Precategory = record { ⌞_⌟ = Precategory.Ob }
Underlying-Precategory
Funlike-Functor: ∀ {o ℓ o' ℓ'} {C : Precategory o ℓ} {D : Precategory o' ℓ'}
→ Funlike (Functor C D) ⌞ C ⌟ (λ x → ⌞ D ⌟)
= record { _·_ = Functor.F₀ }
Funlike-Functor
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))
= record { _·_ = _=>_.η }
Funlike-natural-transformation
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)
.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-path λ i →
Extensional-natural-transformation ⦃ sa ⦄ .idsᵉ .to-path (x i)
sa {D = D} ⦃ sa ⦄ .idsᵉ .to-path-over h =
Extensional-natural-transformation (λ i → Π-is-hlevel 1 λ _ → Pathᵉ-is-hlevel 1 sa (hlevel 2)) _ _
is-prop→pathp
_⟪_⟫_
: ∀ {o ℓ o' ℓ'} {C : Precategory o ℓ} {D : Precategory o' ℓ'}
→ (F : Functor C D) {U V : ⌞ C ⌟}
→ C .Precategory.Hom U V
→ D .Precategory.Hom (F · U) (F · V)
_⟪_⟫_ F f = Functor.F₁ F f