Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| notes:composition-of-functors-is-a-functor [2026/11/26 21:34] – Add it sam | notes: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 | + | # The Composition |
| - | < | + | 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 | ||
| - | </ | + | ``` |
| - | ====== | + | ## 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 '' | + | 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*, | ||
| - | < | + | ```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 _≡_ #-} | ||
| - | </ | + | ``` |
| - | In other words, given a type, '' | + | In other words, given a type, `A`, and an object of that type, called |
| - | ===== Transitivity of Equality | + | ## Transitivity of Equality |
| - | < | + | ```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 | ||
| - | </ | + | ``` |
| - | Eliminating | + | Eliminating |
| + | 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 | ||
| - | ===== Congruence of Equality | + | ## Congruence of Equality |
| - | In other words, if '' | + | In other words, if `x ≡ y`, then `f x ≡ f y`. |
| - | < | + | ```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 | ||
| - | </ | + | ``` |
| - | Eliminating | + | Eliminating |
| - | The following equivalence reasoning module is taken directly from PLFA:[^plfa] | + | The following equivalence reasoning module is taken directly from PLFA: |
| - | < | + | ```agda |
| module ≡-Reasoning {A : Set} where | module ≡-Reasoning {A : Set} where | ||
| Line 70: | Line 78: | ||
| open ≡-Reasoning | open ≡-Reasoning | ||
| - | </ | + | ``` |
| - | ===== 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, | + | A functor is any object which defines a function, |
| + | | ||
| + | | ||
| We first give a definition of identity on (small) types: | We first give a definition of identity on (small) types: | ||
| - | + | ```agda | |
| - | < | + | |
| id : ∀ { a : Set } → a → a | id : ∀ { a : Set } → a → a | ||
| id x = x | id x = x | ||
| - | </ | + | ``` |
| And then a definition of composition on (small) types: | And then a definition of composition on (small) types: | ||
| - | + | ```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 _∘_ | ||
| - | </ | + | ``` |
| - | Now, define 𝔽, which can be read as a property of constructors, | + | Now, define 𝔽, which can be read as a property of constructors, |
| - | < | + | ```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 | ||
| - | </ | + | ``` |
| - | We also define composition at the type-level using '' | + | We also define composition at the type-level using `◯`: |
| - | < | + | ```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) | ||
| - | </ | + | ``` |
| - | And define | + | And define |
| - | < | + | ```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) | ||
| - | </ | + | ``` |
| Now, re-state and prove the identity property, specialized to ◯: | Now, re-state and prove the identity property, specialized to ◯: | ||
| - | < | + | ```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 ∎ | ||
| - | </ | + | ``` |
| 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 | |
| - | < | + | |
| ... | ... | ||
| begin | begin | ||
| Line 162: | Line 169: | ||
| tₙ ∎ | tₙ ∎ | ||
| ... | ... | ||
| - | </ | + | ``` |
| - | + | That way, the proof is well-formed, | |
| - | That way, the proof is well-formed, | + | |
| 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 | |
| - | < | + | |
| ◯-comp : | ◯-comp : | ||
| -- Given two constructors... | -- Given two constructors... | ||
| Line 188: | Line 193: | ||
| fmap-f (fmap-g (h ∘ k)) | fmap-f (fmap-g (h ∘ k)) | ||
| ◯-fmap F G (h ∘ k) ∎ | ◯-fmap F G (h ∘ k) ∎ | ||
| - | </ | + | ``` |
| - | Finally, we can construct a law-abiding functor by giving our definition of '' | + | 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: |
| - | < | + | ```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) | ||
| - | </ | + | ``` |
| - | + | ||
| - | ===== Shortcuts: ===== | + | |
| - | + | ||
| - | * '' | + | |
| - | * '' | + | |
| - | * '' | + | |
| - | * '' | + | |
| - | * '' | + | |
| - | * '' | + | |
| - | * [[https:// | + | |
| - | + | ||
| - | [^levels] Levels are a mechanism to avoid statements of the form '' | + | |
| - | + | ||
| - | [^agda-sort-system] [[https:// | + | |
| - | [^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 < | ||
| + | * [More](https://agda.readthedocs.io/en/ | ||
| - | [^fta-in-agda] [[https:// | + | ## Other references |
| + | * [Proving the Fundamental Theorem of Arithmetic | ||