bsc-leon-vatthauer/agda/src/Monad/Instance/Maybe.lagda.md

62 lines
2.2 KiB
Markdown
Raw Normal View History

2024-02-08 16:16:21 +01:00
<!--
```agda
open import Categories.Category.Core
2024-02-18 18:15:15 +01:00
open import Categories.Category.Cocartesian using (Cocartesian)
open import Categories.Category.Cartesian using (Cartesian)
open import Categories.Category.Distributive using (Distributive)
2024-02-08 16:16:21 +01:00
open import Categories.Functor hiding (id)
2024-02-18 18:15:15 +01:00
open import Categories.Object.Terminal using (Terminal)
open import Categories.Monad using (Monad)
2024-02-08 16:16:21 +01:00
open import Categories.NaturalTransformation hiding (id)
import Categories.Morphism.Reasoning as MR
```
-->
2024-02-18 18:15:15 +01:00
# The Maybe Monad
2024-02-08 16:16:21 +01:00
```agda
module Monad.Instance.Maybe {o e} {C : Category o e} (distributive : Distributive C) where
open Category C
open MR C
open HomReasoning
open Equiv
open Distributive distributive
open Cocartesian cocartesian
open Cartesian cartesian using (terminal)
open Terminal terminal
2024-02-18 18:15:15 +01:00
```
The functor X + 1:
2024-02-08 16:16:21 +01:00
2024-02-18 18:15:15 +01:00
```agda
2024-02-08 16:16:21 +01:00
maybeFunctor : Endofunctor C
maybeFunctor = record
2024-02-18 18:15:15 +01:00
{ F₀ = λ X → X +
; F₁ = λ f → f +₁ id
; identity = +-unique id-comm-sym id-comm-sym
; homomorphism = sym (+₁∘+₁ ○ +₁-cong₂ refl identity²)
; F-resp-≈ = λ eq → +₁-cong₂ eq refl
2024-02-08 16:16:21 +01:00
}
2024-02-18 18:15:15 +01:00
```
extends to a monad:
2024-02-08 16:16:21 +01:00
2024-02-18 18:15:15 +01:00
```agda
2024-02-08 16:16:21 +01:00
open Monad renaming (identityˡ to m-identityˡ; identityʳ to m-identityʳ; assoc to m-assoc; sym-assoc to m-sym-assoc)
maybeMonad : Monad C
maybeMonad .F = maybeFunctor
2024-02-18 18:15:15 +01:00
maybeMonad .η = ntHelper (record { η = λ _ → i₁
; commute = λ _ → sym inject₁ })
maybeMonad .μ = ntHelper (record { η = λ _ → [ id , i₂ ]
; commute = λ _ → []∘+₁ ○ []-cong₂ id-comm-sym (sym inject₂) ○ sym ∘[]})
2024-02-08 16:16:21 +01:00
maybeMonad .m-assoc = begin
2024-02-18 18:15:15 +01:00
[ id , i₂ ] ∘ ([ id , i₂ ] +₁ id) ≈⟨ []∘+₁ ⟩
[ id ∘ [ id , i₂ ] , i₂ ∘ id ] ≈˘⟨ []-cong₂ id-comm (inject₂ ○ introʳ refl) ⟩
2024-02-08 16:16:21 +01:00
[ [ id , i₂ ] ∘ id , [ id , i₂ ] ∘ i₂ ] ≈˘⟨ ∘[] ⟩
2024-02-18 18:15:15 +01:00
[ id , i₂ ] ∘ [ id , i₂ ] ∎
2024-02-08 16:16:21 +01:00
maybeMonad .m-sym-assoc = sym (m-assoc maybeMonad)
maybeMonad .m-identityˡ = []∘+₁ ○ +-unique refl id-comm-sym
maybeMonad .m-identityʳ = inject₁
```