open import Cat.Functor.Equivalence
open import Cat.Instances.Functor
open import Cat.Functor.Base
open import Cat.Prelude

import Cat.Reasoning

module Cat.Instances.Functor.Duality where

Duality of functor categoriesπŸ”—

The duality involution Cop\ca{C}\op on categories extends to a β€œduality involution” FopF\op on functors. However, since this changes the type of the functor β€” the dual of F:Cβ†’DF : \ca{C} \to \ca{D} is Fop:Cβ†’DF\op : \ca{C} \to \ca{D} β€” it does not represent an involution on functor categories; Rather, it is an equivalence (βˆ’)op:[C,D]opβ‰…[Cop,Dop](-)\op : [\ca{C},\ca{D}]\op \cong [\ca{C}\op,\ca{D}\op].

op-functor→ : Functor (Cat[ C , D ] ^op) Cat[ C ^op , D ^op ]
op-functor→ .F₀ = Functor.op
op-functorβ†’ .F₁ nt .Ξ· = nt .Ξ·
op-functorβ†’ .F₁ nt .is-natural x y f = sym (nt .is-natural y x f)
op-functor→ .F-id = Nat-path (λ x → refl)
op-functorβ†’ .F-∘ f g = Nat-path Ξ» x β†’ refl

op-functor-is-iso : is-precat-iso (op-functor→ {C = C} {D = D})
op-functor-is-iso = isom where
  open is-precat-iso
  open is-iso

  isom : is-precat-iso _
  isom .has-is-ff = is-iso→is-equiv ff where
    ff : is-iso (F₁ op-functorβ†’)
    ff .inv nt .Ξ· = nt .Ξ·
    ff .inv nt .is-natural x y f = sym (nt .is-natural y x f)
    ff .rinv x = Nat-path Ξ» x β†’ refl
    ff .linv x = Nat-path Ξ» x β†’ refl
  isom .has-is-iso = is-iso→is-equiv (iso Functor.op
    (Ξ» x β†’ Functor-path (Ξ» x β†’ refl) (Ξ» x β†’ refl)) (Ξ» x β†’ F^op^op≑F))

This means, in particular, that it is an adjoint equivalence:

op-functor-is-equiv : is-equivalence (op-functor→ {C = C} {D = D})
op-functor-is-equiv = is-precat-iso→is-equivalence op-functor-is-iso

op-functor← : Functor Cat[ C ^op , D ^op ] (Cat[ C , D ] ^op)
op-functor← = is-equivalence.F⁻¹ op-functor-is-equiv

op-functor←→ : op-functor← {C = C} {D = D} F∘ op-functorβ†’ ≑ Id
op-functor←→ {C = C} {D = D} = Functor-path (Ξ» x β†’ refl) Ξ» {X} {Y} f β†’ Nat-path Ξ» x β†’
  (_ D.∘ f .Ξ· x) D.∘ _ β‰‘βŸ¨ D.elimr (lemma {Y = Y}) βŸ©β‰‘
  _ D.∘ f .Ξ· x         β‰‘βŸ¨ D.eliml (lemma {Y = X}) βŸ©β‰‘
  f .η x               ∎
  where
    module D = Cat.Reasoning D
    module C = Cat.Reasoning C

    lemma : βˆ€ {Y : Functor C D} {x}
      β†’ coe0β†’1 (Ξ» i β†’ D.Hom (Fβ‚€ Y (transp (Ξ» j β†’ C.Ob) i x)) (Fβ‚€ Y (transp (Ξ» j β†’ C.Ob) i x))) D.id
      ≑ D.id
    lemma {Y} {x} =
      from-pathp {A = Ξ» i β†’ D.Hom (Fβ‚€ Y (transp (Ξ» j β†’ C.Ob) i x)) (Fβ‚€ Y (transp (Ξ» j β†’ C.Ob) i x))}
        Ξ» i β†’ hcomp (βˆ‚ i) Ξ» where
          j (i = i0) β†’ D.id
          j (i = i1) β†’ transport-filler (Ξ» j β†’ D.Hom (Fβ‚€ Y x) (Fβ‚€ Y x)) D.id (~ j)
          j (j = i0) β†’ coe0β†’i
            (Ξ» j β†’ D.Hom (Fβ‚€ Y (transp (Ξ» j β†’ C.Ob) (i ∨ j) x))
                         (Fβ‚€ Y (transp (Ξ» j β†’ C.Ob) (i ∨ j) x)))
            i D.id

module _ {o β„“ oβ€² β„“β€²} {C : Precategory o β„“} {D : Precategory oβ€² β„“β€²} {F G : Functor C D} where
  private
    module CD = Cat.Reasoning Cat[ C , D ]
    module CopDop = Cat.Reasoning Cat[ C ^op , D ^op ]

  op-natural-iso : F CD.β‰… G β†’ (Functor.op F) CopDop.β‰… (Functor.op G)
  op-natural-iso isom = CopDop.make-iso (_=>_.op isom.from) (_=>_.op isom.to)
    (Nat-path (Ξ» x β†’ ap (Ξ» e β†’ e .Ξ· x) isom.invl))
    (Nat-path Ξ» x β†’ ap (Ξ» e β†’ e .Ξ· x) isom.invr)
    where module isom = CD._β‰…_ isom