Functor.agda 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. module Functor where
  2. record IsEquivalence {A : Set} (_≈_ : A → A → Set) : Set where
  3. field
  4. refl : ∀ {x} → x ≈ x
  5. sym : ∀ {i j} → i ≈ j → j ≈ i
  6. trans : ∀ {i j k} → i ≈ j → j ≈ k → i ≈ k
  7. record Setoid : Set₁ where
  8. infix 4 _≈_
  9. field
  10. Carrier : Set
  11. _≈_ : Carrier → Carrier → Set
  12. isEquivalence : IsEquivalence _≈_
  13. open IsEquivalence isEquivalence public
  14. infixr 0 _⟶_
  15. record _⟶_ (From To : Setoid) : Set where
  16. infixl 5 _⟨$⟩_
  17. field
  18. _⟨$⟩_ : Setoid.Carrier From → Setoid.Carrier To
  19. cong : ∀ {x y} →
  20. Setoid._≈_ From x y → Setoid._≈_ To (_⟨$⟩_ x) (_⟨$⟩_ y)
  21. open _⟶_ public
  22. id : ∀ {A} → A ⟶ A
  23. id = record { _⟨$⟩_ = λ x → x; cong = λ x≈y → x≈y }
  24. infixr 9 _∘_
  25. _∘_ : ∀ {A B C} → B ⟶ C → A ⟶ B → A ⟶ C
  26. f ∘ g = record
  27. { _⟨$⟩_ = λ x → f ⟨$⟩ (g ⟨$⟩ x)
  28. ; cong = λ x≈y → cong f (cong g x≈y)
  29. }
  30. _⇨_ : (To From : Setoid) → Setoid
  31. From ⇨ To = record
  32. { Carrier = From ⟶ To
  33. ; _≈_ = λ f g → ∀ {x y} → x ≈₁ y → f ⟨$⟩ x ≈₂ g ⟨$⟩ y
  34. ; isEquivalence = record
  35. { refl = λ {f} → cong f
  36. ; sym = λ f∼g x∼y → To.sym (f∼g (From.sym x∼y))
  37. ; trans = λ f∼g g∼h x∼y → To.trans (f∼g From.refl) (g∼h x∼y)
  38. }
  39. }
  40. where
  41. open module From = Setoid From using () renaming (_≈_ to _≈₁_)
  42. open module To = Setoid To using () renaming (_≈_ to _≈₂_)
  43. record Functor (F : Setoid → Setoid) : Set₁ where
  44. field
  45. map : ∀ {A B} → (A ⇨ B) ⟶ (F A ⇨ F B)
  46. identity : ∀ {A} →
  47. let open Setoid (F A ⇨ F A) in
  48. map ⟨$⟩ id ≈ id
  49. composition : ∀ {A B C} (f : B ⟶ C) (g : A ⟶ B) →
  50. let open Setoid (F A ⇨ F C) in
  51. map ⟨$⟩ (f ∘ g) ≈ (map ⟨$⟩ f) ∘ (map ⟨$⟩ g)