Sam's Wiki

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
notes:composition-of-functors-is-a-functor [2026/11/26 21:34] – Add it samnotes:composition-of-functors-is-a-functor [2026/11/26 21:44] (current) – Fix footnotes sam
Line 1: Line 1:
-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.+# The Composition of Two Functors is a Functor
  
-<code agda>+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. 
 + 
 +```agda
 module CompOfFunctorsIsFunctor where module CompOfFunctorsIsFunctor where
-</code>+```
  
-====== Equality ======+## 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.[^levels]+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.((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.)) (([Agda Sort System](https://agda.readthedocs.io/en/stable/language/sort-system.html#sort-system)))
  
-<code agda>+```agda
 data _≡_ { A : Set } (x : A) : A → Set where data _≡_ { A : Set } (x : A) : A → Set where
   refl : x ≡ x   refl : x ≡ x
Line 16: Line 21:
  
 {-# BUILTIN EQUALITY _≡_ #-} {-# BUILTIN EQUALITY _≡_ #-}
-</code>+```
  
-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%%''.[^plfa]+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`.(([PLFA](https://plfa.github.io/)))
  
-===== Transitivity of Equality =====+## Transitivity of Equality
  
-<code agda>+```agda
 trans : ∀ { A : Set } { x y z : A } trans : ∀ { A : Set } { x y z : A }
   → x ≡ y   → x ≡ y
Line 28: Line 33:
   → x ≡ z   → x ≡ z
 trans refl refl = refl trans refl refl = refl
-</code>+```
  
-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%%''.+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 =====+## Congruence of Equality
  
-In other words, if ''%%x ≡ y%%'', then ''%%f x ≡ f y%%''.+In other words, if `x ≡ y`, then `f x ≡ f y`.
  
-<code agda>+```agda
 cong : ∀ { A B : Set } ( f : A → B ) { x y : A } cong : ∀ { A B : Set } ( f : A → B ) { x y : A }
   → x ≡ y   → x ≡ y
   → f x ≡ f y   → f x ≡ f y
 cong f refl = refl cong f refl = refl
-</code>+```
  
-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%%''.+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:[^plfa]+The following equivalence reasoning module is taken directly from PLFA:
  
-<code agda>+```agda
 module ≡-Reasoning {A : Set} where module ≡-Reasoning {A : Set} where
  
Line 70: Line 78:
  
 open ≡-Reasoning open ≡-Reasoning
-</code>+```
  
-===== Definition of a Functor =====+## Definition of a Functor
  
 It is now possible to define the properties 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: * Identity: ''%%fmap id ≡ id%%'', and * Composition: ''%%fmap (f ∘ g) ≡ fmap f ∘ fmap g%%''+A functor is any object which defines a function, `fmap`, which obeys: 
 +  * Identity: `fmap id ≡ id`, and 
 +  * Composition: `fmap (f ∘ g) ≡ fmap f ∘ fmap g`
  
 We first give a definition of identity on (small) types: We first give a definition of identity on (small) types:
- +```agda
-<code agda>+
 id : ∀ { a : Set } → a → a id : ∀ { a : Set } → a → a
 id x = x id x = x
-</code>+```
  
 And then a definition of composition on (small) types: And then a definition of composition on (small) types:
- +```agda
-<code agda>+
 _∘_ : ∀ { a b c : Set } → (b → c) → (a → b)  → a → c _∘_ : ∀ { a b c : Set } → (b → c) → (a → b)  → a → c
 f ∘ g = \a → f (g a) f ∘ g = \a → f (g a)
  
 infixr 9 _∘_ infixr 9 _∘_
-</code>+```
  
-Now, define 𝔽, which can be read as a property of constructors, that they define a function ''%%fmap%%'' which obeys the above two laws.+Now, define 𝔽, which can be read as a property of constructors, that they define a function `fmap` which obeys the above two laws.
  
-<code agda>+```agda
 data 𝔽 (f : Set → Set) : Set₁ where data 𝔽 (f : Set → Set) : Set₁ where
   mkF   mkF
Line 112: Line 120:
         -- produce a law-abiding functor of constructor f.         -- produce a law-abiding functor of constructor f.
       → 𝔽 f       → 𝔽 f
-</code>+```
  
-We also define composition at the type-level using ''%%◯%%'':+We also define composition at the type-level using `◯`:
  
-<code agda>+```agda
 _◯_ : (Set → Set) → (Set → Set) → (Set → Set) _◯_ : (Set → Set) → (Set → Set) → (Set → Set)
 (F ◯ G) a = F (G a) (F ◯ G) a = F (G a)
-</code>+```
  
-And define ''%%fmap%%'' on ''%%◯%%'':+And define `fmap` on `◯`:
  
-<code agda>+```agda
 ◯-fmap : ∀ {a b : Set} {F G : Set → Set} ◯-fmap : ∀ {a b : Set} {F G : Set → Set}
   → (F-functor : 𝔽 F)   → (F-functor : 𝔽 F)
Line 131: Line 139:
   → (F ◯ G) b   → (F ◯ G) b
 ◯-fmap (mkF fmap-f _ _) (mkF fmap-g _ _) f = fmap-f (fmap-g f) ◯-fmap (mkF fmap-f _ _) (mkF fmap-g _ _) f = fmap-f (fmap-g f)
-</code>+```
  
 Now, re-state and prove the identity property, specialized to ◯: Now, re-state and prove the identity property, specialized to ◯:
  
-<code agda>+```agda
 ◯-id : ◯-id :
   -- Given two constructors...   -- Given two constructors...
Line 152: Line 160:
     fmap-f id              ≡⟨ fmap-f-id ⟩             -- fmap-f id ≡ id     fmap-f id              ≡⟨ fmap-f-id ⟩             -- fmap-f id ≡ id
     id                     ∎     id                     ∎
-</code>+```
  
 I found it useful while writing this proof to construct intermediate states as follows: I found it useful while writing this proof to construct intermediate states as follows:
- +```text
-<code text>+
 ... ...
   begin   begin
Line 162: Line 169:
     tₙ  ∎     tₙ  ∎
 ... ...
-</code> +``` 
- +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ₙ`).
-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: We can write a similar argument for the composition case, specializing again to ◯-fmap:
- +```agda
-<code agda>+
 ◯-comp : ◯-comp :
   -- Given two constructors...   -- Given two constructors...
Line 188: Line 193:
     fmap-f (fmap-g (h ∘ k))               ≡⟨⟩                                    -- definition of ◯-fmap     fmap-f (fmap-g (h ∘ k))               ≡⟨⟩                                    -- definition of ◯-fmap
     ◯-fmap F G (h ∘ k)                    ∎     ◯-fmap F G (h ∘ k)                    ∎
-</code>+```
  
-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:+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:
  
-<code agda>+```agda
 ◯-functor : ◯-functor :
   -- Given two constructors...   -- Given two constructors...
Line 203: Line 208:
   → 𝔽 (F ◯ G)   → 𝔽 (F ◯ G)
 ◯-functor F G = mkF (◯-fmap F G) (◯-id F G) (◯-comp F G) ◯-functor F G = mkF (◯-fmap F G) (◯-id F G) (◯-comp F G)
-</code> +```
- +
-===== Shortcuts: ===== +
- +
-  * ''%%C-c C-l%%'': (Re)load file. +
-  * ''%%C-c C-,%%'': Show goal and context. +
-  * ''%%C-c C-r%%'': Make new holes for missing arguments. +
-  * ''%%C-c C-SPC%%'': Fill the hole. +
-  * ''%%C-c C-a%%'': Search for a term to fill the hole. +
-  * ''%%C-c C-c <var>%%'': Split the given var into cases. +
-  * [[https://agda.readthedocs.io/en/v2.5.4.1/tools/emacs-mode.html#how-can-i-write-unicode-characters-using-emacs|More]] +
- +
-[^levels] 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. [^agda-sort-system] +
- +
-[^agda-sort-system] [[https://agda.readthedocs.io/en/stable/language/sort-system.html#sort-system|Agda Sort System]]+
  
-[^plfa] [[https://plfa.github.io/|PLFA]]+## Shortcuts 
 +  * `C-c C-l`: (Re)load file. 
 +  * `C-c C-,`: Show goal and context. 
 +  * `C-c C-r`: Make new holes for missing arguments. 
 +  * `C-c C-SPC`: Fill the hole. 
 +  * `C-c C-a`: Search for a term to fill the hole. 
 +  * `C-c C-c <var>`: Split the given var into cases. 
 +  * [More](https://agda.readthedocs.io/en/v2.5.4.1/tools/emacs-mode.html#how-can-i-write-unicode-characters-using-emacs)
  
-[^fta-in-agda] [[https://byorgey.github.io/blog/posts/2026/06/26/FTA.lagda.html|Proving the Fundamental Theorem of Arithmetic in Agda]]+## Other references 
 +  * [Proving the Fundamental Theorem of Arithmetic in Agda](https://byorgey.github.io/blog/posts/2026/06/26/FTA.lagda.html)
  
notes/composition-of-functors-is-a-functor.1795728848.txt.gz · Last modified: by sam