Table of Contents

The Composition of Two Functors is a Functor

What follows is a mechanized proof that the composition of two functors is a functor. I wrote this proof to get some practice using Agda. This document is also meant as a reference of how to do some basic things with the prover.

module CompOfFunctorsIsFunctor where

Equality

The functor laws are stated in terms of an equality between terms, so we will first need to define a notion of equality. As I understand it, the following definition is the same as defined in Agda's standard library, except it is not polymorphic in the Level of its arguments.1) 2)

data _≡_ { A : Set } (x : A) : A → Set where
  refl : x ≡ x
 
infix 4 _≡_
 
{-# BUILTIN EQUALITY _≡_ #-}

In other words, given a type, A, and an object of that type, called x, produce evidence that x is equivalent to x, called x ≡ x, with type Set.3)

Transitivity of Equality

trans : ∀ { A : Set } { x y z : A }
  → x ≡ y
  → y ≡ z
  → x ≡ z
trans refl refl = refl

Eliminating refl produces evidence that its left and right-hand sides are equal. So, in this case, the first refl elimination produces evidence that x and y are the same. The second produces evidence that y and z are the same. The introduction refl introduces x ≡ z.

Congruence of Equality

In other words, if x ≡ y, then f x ≡ f y.

cong : ∀ { A B : Set } ( f : A → B ) { x y : A }
  → x ≡ y
  → f x ≡ f y
cong f refl = refl

Eliminating refl, as above, produces evidence that x ≡ y. The introduction refl produces f x ≡ f x, which can be unified with f x ≡ f y under the knowledge that x ≡ y.

The following equivalence reasoning module is taken directly from PLFA:

module ≡-Reasoning {A : Set} where
 
  infix  1 begin_
  infixr 2 step-≡-∣ step-≡-⟩
  infix  3 _∎
 
  begin_ : ∀ {x y : A} → x ≡ y → x ≡ y
  begin x≡y  =  x≡y
 
  step-≡-∣ : ∀ (x : A) {y : A} → x ≡ y → x ≡ y
  step-≡-∣ x x≡y  =  x≡y
 
  step-≡-⟩ : ∀ (x : A) {y z : A} → y ≡ z → x ≡ y → x ≡ z
  step-≡-⟩ x y≡z x≡y  =  trans x≡y y≡z
 
  syntax step-≡-∣ x x≡y      =  x ≡⟨⟩ x≡y
  syntax step-≡-⟩ x y≡z x≡y  =  x ≡⟨  x≡y ⟩ y≡z
 
  _∎ : ∀ (x : A) → x ≡ x
  x ∎  =  refl
 
open ≡-Reasoning

Definition of a Functor

It is now possible to define the properties of a functor.

A functor is any object which defines a function, fmap, which obeys:

We first give a definition of identity on (small) types:

id : ∀ { a : Set } → a → a
id x = x

And then a definition of composition on (small) types:

_∘_ : ∀ { a b c : Set } → (b → c) → (a → b)  → a → c
f ∘ g = \a → f (g a)
 
infixr 9 _∘_

Now, define 𝔽, which can be read as a property of constructors, that they define a function fmap which obeys the above two laws.

data 𝔽 (f : Set → Set) : Set₁ where
  mkF
        -- Given a definition of fmap...
      : (fmap      : ∀ { a b : Set }
                     → (a → b) → f a → f b)
        -- and a proof of the identity property...
      → (fmap-id   : ∀ { A : Set }
                     → fmap (id {A}) ≡ id {f A})
        -- and a proof the composition property...
      → (fmap-comp : ∀ { A B C : Set }
                     → (h : B → C)
                     → (k : A → B)
                     → fmap h ∘ fmap k ≡ fmap (h ∘ k))
        -- produce a law-abiding functor of constructor f.
      → 𝔽 f

We also define composition at the type-level using ◯:

_◯_ : (Set → Set) → (Set → Set) → (Set → Set)
(F ◯ G) a = F (G a)

And define fmap on ◯:

◯-fmap : ∀ {a b : Set} {F G : Set → Set}
  → (F-functor : 𝔽 F)
  → (G-functor : 𝔽 G)
  → (a → b)
  → (F ◯ G) a
  → (F ◯ G) b
◯-fmap (mkF fmap-f _ _) (mkF fmap-g _ _) f = fmap-f (fmap-g f)

Now, re-state and prove the identity property, specialized to ◯:

◯-id :
  -- Given two constructors...
  ∀ { Fc : Set → Set }
  → { Gc : Set → Set }
  -- which describe lawful functors F and G,
  → ( F : 𝔽 Fc )
  → ( G : 𝔽 Gc )
  -- their composition follows the identity law.
  → (∀ { A : Set }
       → ◯-fmap F G (id {A}) ≡ id {(Fc ◯ Gc) A})
◯-id F@(mkF fmap-f fmap-f-id _) G@(mkF fmap-g fmap-g-id _) =
  begin
    ◯-fmap F G id          ≡⟨⟩                        -- definition of ◯-fmap
    fmap-f (fmap-g id)     ≡⟨ cong fmap-f fmap-g-id ⟩ -- fmap-g id ≡ id
    fmap-f id              ≡⟨ fmap-f-id ⟩             -- fmap-f id ≡ id
    id                     ∎

I found it useful while writing this proof to construct intermediate states as follows:

...
  begin
    t₀  ≡⟨ ? ⟩
    tₙ  ∎
...

That way, the proof is well-formed, and the hole gives good context about how to transform from the current term (t₀, in this case), to the goal term (tₙ).

We can write a similar argument for the composition case, specializing again to ◯-fmap:

◯-comp :
  -- Given two constructors...
  ∀ { Fc : Set → Set }
  → { Gc : Set → Set }
  -- which describe lawful functors F and G,
  → ( F : 𝔽 Fc )
  → ( G : 𝔽 Gc )
  -- their composition follows the composition law.
  → (∀ { A B C : Set }
       → (h : B → C)
       → (k : A → B)
       → ◯-fmap F G h ∘ ◯-fmap F G k ≡ ◯-fmap F G (h ∘ k))
◯-comp F@(mkF fmap-f _ fmap-f-comp) G@(mkF fmap-g _ fmap-g-comp) h k =
  begin
    ◯-fmap F G h ∘ ◯-fmap F G k           ≡⟨⟩                                    -- definition of ◯-fmap
    fmap-f (fmap-g h) ∘ fmap-f (fmap-g k) ≡⟨ fmap-f-comp (fmap-g h) (fmap-g k) ⟩ -- combine fmap-fs using fmap-f-comp
    fmap-f (fmap-g h ∘ fmap-g k)          ≡⟨ cong fmap-f (fmap-g-comp h k) ⟩     -- "", but inside the argument of fmap-f
    fmap-f (fmap-g (h ∘ k))               ≡⟨⟩                                    -- definition of ◯-fmap
    ◯-fmap F G (h ∘ k)                    ∎

Finally, we can construct a law-abiding functor by giving our definition of ◯-fmap, and the argument that ◯-fmap obeys the identity and composition laws:

◯-functor :
  -- Given two constructors...
  ∀ { F : Set → Set }
  → { G : Set → Set }
  -- which describe lawful functors F and G,
  → ( 𝔽 F )
  → ( 𝔽 G )
  -- their composition forms a lawful functor.
  → 𝔽 (F ◯ G)
◯-functor F G = mkF (◯-fmap F G) (◯-id F G) (◯-comp F G)

Shortcuts

Other references

1)
Levels are a mechanism to avoid statements of the form Set : Set, since that would imply that the set of all types is itself a type, which is a paradox. Instead, we have that Set : Set₁, and Set₁ : Set₂, and so on. To avoid defining all of our functions multiple times, one for each universe in which the type is used, Agda has universe polymorphism, which allows a single function definition to be instantiated at several universe levels.