\import Equiv
\import Equiv.Fiber
\import Equiv.Sigma
\import Function.Meta
\import InfCat.Basics
\import InfCat.Functor.Conservative
\import InfCat.Functor.Equiv
\import InfCat.Functor.FullyFaithful
\import InfCat.Paths
\import InfCat.Rezk
\import InfCat.Square
\import InfCat.SubCat.Full
\import Logic
\import Logic.Unique
\import Meta
\import Paths
\import Paths.Meta
\open FullSubCat

\record QAdjunction (C D : \Cat) {
  | ladj : C ->⁺ D
  | radj : D ->⁺ C
  | heta (x :⁺ C) : x ~> radj (ladj x)
  | heps (y :⁺ D) : ladj (radj y) ~> y
  | heta_heps-left {y :⁺ D} : comp (heta (radj y)) (dmap radj (heps y)) = idd
  | heta_heps-right {x :⁺ C} : comp (dmap ladj (heta x)) (heps (ladj x)) = idd

  \func heta_heps-lr {x :⁺ C} {y :⁺ D} (g :⁺ ladj x ~> y) : x ~> radj y
    => comp (heta x) (dmap radj g)

  \func heta_heps-rl {x :⁺ C} {y :⁺ D} (h :⁺ x ~> radj y) : ladj x ~> y
    => comp (dmap ladj h) (heps y)

  \sfunc heta_heps-lrl {x :⁺ C} {y :⁺ D} {g :⁺ ladj x ~> y} : heta_heps-rl (heta_heps-lr g) = g
    => unfold (heta_heps-rl, heta_heps-lr) $ pmap+ (comp __ _) (dmap_comp ladj) *> inv comp-assoc
    *> pmap+ (comp _ __) (~>-natural heps) *> comp-assoc *> pmap+ (comp __ g) heta_heps-right *> comp_idd-left

  \sfunc heta_heps-rlr {x :⁺ C} {y :⁺ D} {h :⁺ x ~> radj y} : heta_heps-lr (heta_heps-rl h) = h
    => unfold (heta_heps-lr, heta_heps-rl) $ pmap+ (comp _ __) (dmap_comp radj) *> comp-assoc
    *> pmap+ (comp __ _) (inv (~>-natural heta)) *> inv comp-assoc *> pmap+ (comp h __) heta_heps-left *> comp_idd-right

  \func heta_heps-equiv {x : C} {y : D} : QEquiv {ladj x ~> y} {x ~> radj y} \cowith
    | f => heta_heps-lr
    | ret => heta_heps-rl
    | ret_f g => heta_heps-lrl
    | f_sec h => heta_heps-rlr

  -- | The right adjoint is fully faithful if and only if the counit is an isomorphism.
  \lemma radj-ff-char : IsFFMap radj <-> (\Pi (y : D) -> IsIso (heps y))
    => (\lam ff y => precomp-equiv=>iso \lam z => IsEquiv.leftFactor (inP heta_heps-equiv) $ transport IsEquiv
          (ext \lam g => inv $ pmap (comp _ __) (dmap_comp radj) *> comp-assoc *> pmap (comp __ _) heta_heps-left *> comp_idd-left)
          (ff y z),
        \lam ei y z => transport IsEquiv
          (ext \lam g => pmap (comp _ __) (dmap_comp radj) *> comp-assoc *> pmap (comp __ _) heta_heps-left *> comp_idd-left)
          (IsEquiv.trans (comp-iso-left-equiv (ei y)) (inP heta_heps-equiv)))

  -- | If the right adjoint is fully faithful, then the unit is an isomorphism on its image.
  \lemma radj-ff-unit-iso (ff : IsFFMap radj) (y : D) : IsIso (heta (radj y))
    => \have e => dmap_IsIso radj (radj-ff-char.1 ff y)
       \in inP (dmap radj (heps y), pmap (comp _ __) (IsIso.inv-equals _ heta_heps-left (IsIso.f_ret e)) *> IsIso.f_ret e, heta_heps-left)

  -- | If the left adjoint is fully faithful, then the unit is an isomorphism.
  \lemma ladj-ff-char : IsFFMap ladj <-> (\Pi (x : C) -> IsIso (heta x))
    => (\lam ff x => postcomp-equiv=>iso \lam w => IsEquiv.leftFactor (inP (symQEquiv heta_heps-equiv)) $ transport IsEquiv
          (ext \lam f => inv $ pmap (comp __ _) (dmap_comp ladj) *> inv comp-assoc *> pmap (comp _ __) heta_heps-right *> comp_idd-right)
          (ff w x),
        \lam ei w x => transport IsEquiv
          (ext \lam f => pmap (comp __ _) (dmap_comp ladj) *> inv comp-assoc *> pmap (comp _) heta_heps-right *> comp_idd-right)
          (IsEquiv.trans (comp-iso-right-equiv (ei x)) (inP (symQEquiv heta_heps-equiv))))

  -- | If the left adjoint is fully faithful, then the counit is an isomorphism on its image.
  \lemma ladj-ff-counit-iso (ff : IsFFMap ladj) (x : C) : IsIso (heps (ladj x))
    => \have e => dmap_IsIso ladj (ladj-ff-char.1 ff x)
       \in inP (dmap ladj (heta x), heta_heps-right, pmap (comp __ _) (inv (IsIso.inv-equals _ (IsIso.ret_f e) heta_heps-right)) *> IsIso.ret_f e)
}

-- | Naturality of `g ↦ comp (eta x) (dmap R g)` in `x`.
\sfunc unit-comp-natural {C D :⁺ \Cat} (L :⁺ C ->⁺ D) (R :⁺ D ->⁺ C) (eta :⁺ \Pi (x :⁺ C) -> x ~> R (L x))
                         {x x' :⁺ C} {y :⁺ D} (f :⁺ x ~> x') (g :⁺ L x' ~> y)
  : comp (eta x) (dmap R (comp (dmap L f) g)) = comp f (comp (eta x') (dmap R g))
  => pmap+ (comp _ __) (dmap_comp R) *> comp-assoc *> pmap+ (comp __ _) (inv (~>-natural eta)) *> inv comp-assoc

-- | Naturality of `h ↦ comp (dmap L h) (eps y)` in `y`.
\sfunc counit-comp-natural {C D :⁺ \Cat} (L :⁺ C ->⁺ D) (R :⁺ D ->⁺ C) (eps :⁺ \Pi (y :⁺ D) -> L (R y) ~> y)
                           {x :⁺ C} {y y' :⁺ D} (h :⁺ x ~> R y) (g :⁺ y ~> y')
  : comp (dmap L (comp h (dmap R g))) (eps y') = comp (comp (dmap L h) (eps y)) g
  => pmap+ (comp __ _) (dmap_comp L) *> inv comp-assoc *> pmap+ (comp _ __) (~>-natural eps) *> comp-assoc

\record FunctorPair {C D : \Cat} (F : C ->⁺ D) (G : D ->⁺ C)

-- | A pair of functors with a natural universal unit.
\record UnivUnit \extends FunctorPair {
  | ueta (x :⁺ C) : x ~> G (F x)
  | ueta-univ (x : C) (y : D) : IsEquiv {F x ~> y} {x ~> G y} (\lam g => comp (ueta x) (dmap G g))

  -- | The left adjoint structure on {F} given by the universal unit.
  \func leftAdjoint : LeftAdjoint F \cowith
    | rob y => G y
    | eps0 => eps0
    | eps0-adjoint {x} {y} => IsEquiv.leftFactor (ueta-univ x y) $ transportInv IsEquiv
        (ext \lam f => unit-comp-natural F G ueta f (eps0 y)
                        *> pmap (comp f __) (IsEquiv.f_ret (ueta-univ (G y) y))
                        *> comp_idd-right)
        (inP idEquiv)
    \where {
      -- | The objectwise counit of {F}.
      \protected \func eps0 (y : D) : F (G y) ~> y
        => IsEquiv.ret (ueta-univ (G y) y) idd
    }

  \func toAdjunction : QAdjunction C D F G ueta counitTransport.eps \cowith
    | heta_heps-left => counitTransport.triangle-left
    | heta_heps-right {x} => pmap+ (comp __ _) (pmap+ (dmap F __) heta-char *> dmap_comp F) *> inv comp-assoc
       *> pmap+ (comp _ __) (comp-assoc *> pmap+ (comp __ _) (inv (dmap_comp F) *> pmap+ (dmap F __) counitTransport.tau_sigma) *> comp_idd-left)
       *> unitTransport.triangle-right
    \where \private {
      \func counitTransport : CounitTransport F G \cowith
        | UnivUnit => \this
        | G' => leftAdjoint.radj
        | counit => leftAdjoint.univCounit

      \func unitTransport : UnitTransport F leftAdjoint.radj \cowith
        | UnivCounit => leftAdjoint.univCounit
        | F' => leftAdjoint.rightAdjoint.ladj
        | unit => leftAdjoint.rightAdjoint.univUnit

      \sfunc heta-char {x :⁺ C} : ueta x = comp (unitTransport.eta x) (counitTransport.tau (F x))
        => inv comp_idd-right *> inv (pmap+ (\lam g => comp (ueta x) (dmap G g)) unitTransport.triangle-right)
           *> unit-comp-natural F G ueta (unitTransport.eta x) (leftAdjoint.heps (F x))
    }
}

-- | A pair of functors with a natural universal counit.
\record UnivCounit \extends FunctorPair {
  | ueps (y :⁺ D) : F (G y) ~> y
  | ueps-univ (y : D) (x : C) : IsEquiv {x ~> G y} {F x ~> y} (\lam f => comp (dmap F f) (ueps y))

  -- | The right adjoint structure on {G} given by the universal counit.
  \func rightAdjoint : RightAdjoint G \cowith
    | lob x => F x
    | eta0 => eta0
    | eta0-adjoint {x} {y}
      => IsEquiv.leftFactor (ueps-univ y x) $ transportInv IsEquiv
          (ext \lam g => counit-comp-natural F G ueps (eta0 x) g
                          *> pmap (comp __ g) (IsEquiv.f_ret (ueps-univ (F x) x))
                          *> comp_idd-left)
          (inP idEquiv)
    \where {
      -- | The objectwise unit of {G}.
      \protected \func eta0 (x : C) : x ~> G (F x)
        => IsEquiv.ret (ueps-univ (F x) x) idd
    }

  \func toAdjunction : QAdjunction C D F G unitTransport.eta ueps \cowith
    | heta_heps-left {y} => pmap+ (comp _ __) (pmap+ (dmap G __) heps-char *> dmap_comp G) *> comp-assoc
      *> pmap+ (comp __ _) (inv comp-assoc *> pmap+ (comp _ __) (inv (dmap_comp G) *> pmap+ (dmap G __) unitTransport.pi_rho) *> comp_idd-right)
      *> counitTransport.triangle-left
    | heta_heps-right => unitTransport.triangle-right
    \where \private {
      \func unitTransport : UnitTransport F G \cowith
        | UnivCounit => \this
        | F' => rightAdjoint.ladj
        | unit => rightAdjoint.univUnit

      \func counitTransport : CounitTransport rightAdjoint.ladj G \cowith
        | UnivUnit => rightAdjoint.univUnit
        | G' => rightAdjoint.leftAdjoint.radj
        | counit => rightAdjoint.leftAdjoint.univCounit

      \sfunc heps-char {y :⁺ D} : ueps y = comp (unitTransport.rho (G y)) (counitTransport.eps y)
        => inv comp_idd-left *> inv (pmap+ (\lam h => comp (dmap F h) (ueps y)) counitTransport.triangle-left)
            *> counit-comp-natural F G ueps (rightAdjoint.heta (G y)) (counitTransport.eps y)
    }
}

{- | Given a universal unit for {G} and a universal counit for {G'}, the canonical transformation `G' ~> G` is a natural isomorphism.
     Transporting the counit along its inverse gives a counit for {G} satisfying the left triangle identity.
 -}
\protected \record CounitTransport \extends UnivUnit {
  | G' : D ->⁺ C
  | counit : UnivCounit F G'

  \func tau (y :⁺ D) : G' y ~> G y
    => comp (ueta (G' y)) (dmap G (counit.ueps y))

  \lemma tau-iso (y : D) : IsIso (tau y)
    => postcomp-equiv=>iso \lam x => transport IsEquiv (ext \lam f => unit-comp-natural F G ueta f (counit.ueps y)) (IsEquiv.trans (counit.ueps-univ y x) (ueta-univ x y))

  \func tauNat : G' ~> G
    => dpath \lam i y => tau y i

  \lemma tauNat-iso : IsIso tauNat
    => objectwise-iso tau-iso

  \func sigma (y :⁺ D) : G y ~> G' y
    => dmap (\lam u => u y) (IsIso.ret tauNat-iso)

  \sfunc sigma_tau {y :⁺ D} : comp (sigma y) (tau y) = idd
    => inv (dmap_comp {D ->⁺ C} \lam u => u y) *> pmap+ (dmap (\lam u => u y) __) (IsIso.ret_f tauNat-iso)

  \sfunc tau_sigma {y :⁺ D} : comp (tau y) (sigma y) = idd
    => inv (dmap_comp {D ->⁺ C} \lam u => u y) *> pmap+ (dmap (\lam u => u y) __) (IsIso.f_ret tauNat-iso)

  \func eps (y :⁺ D) : F (G y) ~> y
    => comp (dmap F (sigma y)) (counit.ueps y)

  \sfunc triangle-left {y :⁺ D} : comp (ueta (G y)) (dmap G (eps y)) = idd
    => unit-comp-natural F G ueta (sigma y) (counit.ueps y) *> sigma_tau
}

{- | Given a universal counit for {F} and a universal unit for {F'}, the canonical transformation `F ~> F'` is a natural isomorphism.
     Transporting the unit along its inverse gives a unit for {F} satisfying the right triangle identity.
 -}
\protected \record UnitTransport \extends UnivCounit {
  | F' : C ->⁺ D
  | unit : UnivUnit F' G

  \func rho (x :⁺ C) : F x ~> F' x
    => comp (dmap F (unit.ueta x)) (ueps (F' x))

  \lemma rho-iso (x : C) : IsIso (rho x)
    => precomp-equiv=>iso \lam y => transport IsEquiv (ext \lam g => counit-comp-natural F G ueps (unit.ueta x) g) (IsEquiv.trans (unit.ueta-univ x y) (ueps-univ y x))

  \func rhoNat : F ~> F'
    => dpath \lam i x => rho x i

  \lemma rhoNat-iso : IsIso rhoNat
    => objectwise-iso rho-iso

  \func pi (x :⁺ C) : F' x ~> F x
    => dmap (\lam u => u x) (IsIso.ret rhoNat-iso)

  \sfunc rho_pi {x :⁺ C} : comp (rho x) (pi x) = idd
    => inv (dmap_comp {C ->⁺ D} \lam u => u x) *> pmap+ (dmap (\lam u => u x) __) (IsIso.f_ret rhoNat-iso)

  \sfunc pi_rho {x :⁺ C} : comp (pi x) (rho x) = idd
    => inv (dmap_comp {C ->⁺ D} \lam u => u x) *> pmap+ (dmap (\lam u => u x) __) (IsIso.ret_f rhoNat-iso)

  \func eta (x :⁺ C) : x ~> G (F x)
    => comp (unit.ueta x) (dmap G (pi x))

  \sfunc triangle-right {x :⁺ C} : comp (dmap F (eta x)) (ueps (F x)) = idd
    => counit-comp-natural F G ueps (unit.ueta x) (pi x) *> rho_pi
}

-- | A universal arrow from {L} to {y}.
\record CounivArrow {C D : \Cat} (L : C ->⁺ D) (y : D)
  | R0y : C
  | eps0y : L R0y ~> y
  | eps0y-univ {x' : C} : IsEquiv {x' ~> R0y} {L x' ~> y} (\lam f => comp (dmap L f) eps0y)
  \where {
    -- | Universal arrows are unique.
    \use \level levelProp {C D : \Cat} {L : C ->⁺ D} {y : D} (s t : CounivArrow L y) : s = t
      => \have | gp => IsEquiv.f_ret t.eps0y-univ {s.eps0y}
               | r => IsIso_= $ postcomp-equiv=>iso \lam x => IsEquiv.leftFactor t.eps0y-univ $ transport IsEquiv
                        (ext \lam f => pmap (comp _) (inv gp) *> comp-assoc *> pmap (comp __ _) (inv (dmap_comp L)))
                        s.eps0y-univ
         \in ext (r.1, transport_~>_dmap L (\lam _ => y) (comp_idd-right *> inv (pmap (\lam z => comp (dmap L z) _) r.2 *> gp)))
  }

-- | A universal arrow from {x} to {R}.
\record UnivArrow {C D : \Cat} (R : D ->⁺ C) (x : C)
  | L0x : D
  | eta0x : x ~> R L0x
  | eta0x-univ {y' : D} : IsEquiv {L0x ~> y'} {x ~> R y'} (\lam g => comp eta0x (dmap R g))
  \where {
    -- | Universal arrows are unique.
    \use \level levelProp {C D : \Cat} {R : D ->⁺ C} {x : C} (s t : UnivArrow R x) : s = t
      => \have | gp => IsEquiv.f_ret s.eta0x-univ
               | r => IsIso_= $ precomp-equiv=>iso \lam y => IsEquiv.leftFactor s.eta0x-univ $ transport IsEquiv
                        (ext \lam k => pmap (comp __ _) (inv gp) *> inv comp-assoc *> pmap (comp s.eta0x) (inv (dmap_comp R)))
                        t.eta0x-univ
         \in ext (r.1, transport_~>_dmap-right R *> pmap (\lam z => comp s.eta0x (dmap R z)) r.2 *> gp)
  }

\record LeftAdjoint {C D : \Cat} (L : C ->⁺ D) {
  | rob : D -> C
  | eps0 (y : D) : L (rob y) ~> y
  | eps0-adjoint {x : C} {y : D} : IsEquiv {x ~> rob y} {L x ~> y} (\lam f => comp (dmap L f) (eps0 y))

  -- | The comma category {L} ↓ {D}.
  \protected \func Comma => \Sigma⁺ (y : D) (\Sigma⁺ (x : C) (L x ~> y))

  \protected \func IsUniversal {x : C} {y : D} (h : L x ~> y) : \Prop
    => \Pi (x' : C) -> IsEquiv {x' ~> x} {L x' ~> y} (\lam f => comp (dmap L f) h)

  -- | The full subcategory of {Comma} on universal arrows.
  \protected \func Univ : \Cat => FullSubCat {Comma} (\lam s => IsUniversal s.2.2)

  \protected \func proj (u :⁺ Univ) : D => (inc u).1

  \protected \lemma proj-equiv : IsHEquiv proj
    => ff-surj-equiv.1 $ later
        (\lam u v => IsEquiv.trans (inc-ff u v) (comma-ff (inc-con {_} {_} {v})),
         \lam y => \case inc-sat {Comma} {\lam s => IsUniversal s.2.2} {y, (rob y, eps0 y)} (\lam _ => eps0-adjoint) \with {
           | inP (u,p) => inP (u, pmap __.1 p)
         })
    \where {
      -- | The projection from {Comma} is fully faithful relative to universal arrows.
      \protected \lemma comma-ff {s t : Comma} (tu : IsUniversal t.2.2) : IsEquiv {s ~> t} {s.1 ~> t.1} (dmap __.1)
        => inP $ transQEquiv (dpath-sigma-equiv \lam _ y => \Sigma⁺ (x : C) (L x ~> y)) $
             contr-right {s.1 ~> t.1} \lam f => Contr.Contr_QEquiv
               (symQEquiv $ transQEquiv (dpath-sigma-equiv \lam i x => L x ~> f i) $ sigma-right \lam g =>
                  square-equiv (dmap L g) _)
               (IsEquiv=>contrFibers (tu s.2.1) (comp s.2.2 f))
    }

  \protected \func univ-ret (y :⁺ D) : Univ
    => IsHEquiv.ret proj-equiv y

  \protected \func proj_univ-ret {y :⁺ D} : proj (univ-ret y) = y
    => IsHEquiv.f_ret proj-equiv

  \protected \func radj (y :⁺ D) : C
    => (inc (univ-ret y)).2.1

  \protected \func heps (y :⁺ D) : L (radj y) ~> y
    => comp (inc (univ-ret y)).2.2 (=_~> proj_univ-ret)

  \protected \lemma heps-univ (y : D) : IsUniversal (heps y)
    => \lam x' => transport IsEquiv (ext \lam f => inv comp-assoc) (IsEquiv.trans (inc-con {_} {_} {univ-ret y} x') (comp-iso-right-equiv (=_~>-iso proj_univ-ret)))

  \protected \func univCounit : UnivCounit L radj \cowith
    | ueps => heps
    | ueps-univ => heps-univ

  \protected \func rightAdjoint : RightAdjoint radj
    => univCounit.rightAdjoint

  \func toAdjunction : QAdjunction C D L radj { | heps => heps }
    => univCounit.toAdjunction
} \where {
  \use \func counivArrow {C D : \Cat} {L : C ->⁺ D} (A : LeftAdjoint L) (y : D) : CounivArrow L y
    => \new CounivArrow L y (A.rob y) (A.eps0 y) A.eps0-adjoint

  -- | Left adjoints are unique since universal arrows are.
  \use \level levelProp {C D : \Cat} {L : C ->⁺ D} (A B : LeftAdjoint L) : A = B
    => pmap {\Pi (y : D) -> CounivArrow L y}
            (\lam t => \new LeftAdjoint L { | rob y => R0y {t y} | eps0 y => eps0y | eps0-adjoint => eps0y-univ })
            (path \lam i y => CounivArrow.levelProp (counivArrow A y) (counivArrow B y) i)
}

\record RightAdjoint {C D : \Cat} (R : D ->⁺ C) {
  | lob : C -> D
  | eta0 (x : C) : x ~> R (lob x)
  | eta0-adjoint {x : C} {y : D} : IsEquiv {lob x ~> y} {x ~> R y} (\lam g => comp (eta0 x) (dmap R g))

  -- | The comma category {C} ↓ {R}.
  \protected \func Comma => \Sigma⁺ (x : C) (\Sigma⁺ (y : D) (x ~> R y))

  \protected \func IsUniversal {x : C} {y : D} (h : x ~> R y) : \Prop
    => \Pi (y' : D) -> IsEquiv {y ~> y'} {x ~> R y'} (\lam g => comp h (dmap R g))

  -- | The full subcategory of {Comma} on universal arrows.
  \protected \func Univ : \Cat => FullSubCat {Comma} (\lam s => IsUniversal s.2.2)

  \protected \func proj (u :⁺ Univ) : C => (inc u).1

  \protected \lemma proj-equiv : IsHEquiv proj
    => ff-surj-equiv.1 $ later
        (\lam u v => IsEquiv.trans (inc-ff u v) (comma-ff (inc-con {_} {_} {u})),
         \lam x => \case inc-sat {Comma} {\lam s => IsUniversal s.2.2} {x, (lob x, eta0 x)} (\lam _ => eta0-adjoint) \with {
           | inP (u,p) => inP (u, pmap __.1 p)
         })
    \where {
      -- | The projection from {Comma} is fully faithful relative to universal arrows.
      \protected \lemma comma-ff {s t : Comma} (su : IsUniversal s.2.2) : IsEquiv {s ~> t} {s.1 ~> t.1} (dmap __.1)
        => inP $ transQEquiv (dpath-sigma-equiv \lam _ x => \Sigma⁺ (y : D) (x ~> R y)) $
             contr-right {s.1 ~> t.1} \lam f => Contr.Contr_QEquiv
               (symQEquiv $ transQEquiv (dpath-sigma-equiv \lam i y => f i ~> R y) $ sigma-right \lam g =>
                  transQEquiv (square-equiv f (dmap R g)) inv-equiv)
               (IsEquiv=>contrFibers (su t.2.1) (comp f t.2.2))
    }

  \protected \func univ-ret (x :⁺ C) : Univ
    => IsHEquiv.ret proj-equiv x

  \protected \func proj_univ-ret {x :⁺ C} : proj (univ-ret x) = x
    => IsHEquiv.f_ret proj-equiv

  \protected \func ladj (x :⁺ C) : D
    => (inc (univ-ret x)).2.1

  \protected \func heta (x :⁺ C) : x ~> R (ladj x)
    => comp (=_~> (inv proj_univ-ret)) (inc (univ-ret x)).2.2

  \protected \lemma heta-univ (x : C) : IsUniversal (heta x)
    => \lam y' => transport IsEquiv (ext \lam g => comp-assoc)
          (IsEquiv.trans (inc-con {_} {_} {univ-ret x} y')
                         (comp-iso-left-equiv (=_~>-iso (inv proj_univ-ret))))

  \protected \func univUnit : UnivUnit ladj R \cowith
    | ueta => heta
    | ueta-univ => heta-univ

  \protected \func leftAdjoint : LeftAdjoint ladj
    => univUnit.leftAdjoint

  \func toAdjunction : QAdjunction C D ladj R heta
    => univUnit.toAdjunction
} \where {
  \use \func univArrow {C D : \Cat} {R : D ->⁺ C} (A : RightAdjoint R) (x : C) : UnivArrow R x
    => \new UnivArrow R x (A.lob x) (A.eta0 x) A.eta0-adjoint

  -- | Right adjoints are unique since universal arrows are.
  \use \level levelProp {C D : \Cat} {R : D ->⁺ C} (A B : RightAdjoint R) : A = B
    => pmap {\Pi (x : C) -> UnivArrow R x}
            (\lam t => \new RightAdjoint R { | lob x => L0x {t x} | eta0 x => eta0x | eta0-adjoint => eta0x-univ })
            (path \lam i x => UnivArrow.levelProp (univArrow A x) (univArrow B x) i)

  \func fromUnivArrow {C D : \Cat} {R : D ->⁺ C} (ua : \Pi (x : C) -> UnivArrow R x) : RightAdjoint R \cowith
    | lob x => (ua x).L0x
    | eta0 x => (ua x).eta0x
    | eta0-adjoint {x} => (ua x).eta0x-univ
}