Martin Escardo, 13th June 2025

We repackage the definitions of algebraic injective and flabby types
in a more convenient way, which we refer to as injective structure and
flabby structure.

We also prove some useful lemmas about them.

\begin{code}

{-# OPTIONS --safe --without-K --lossy-unification #-}

open import MLTT.Spartan

module InjectiveTypes.Structure
        {๐“ฆ : Universe}
        (D : ๐“ฆ ฬ‡ )
       where

open import UF.Embeddings
open import UF.SubtypeClassifier

flabby-structure : (๐“ค : Universe) โ†’ ๐“ค โบ โŠ” ๐“ฆ ฬ‡
flabby-structure ๐“ค
 = ฮฃ โจ† ๊ž‰ ((P : ฮฉ ๐“ค) โ†’ (P holds โ†’ D) โ†’ D)
       , ((P : ฮฉ ๐“ค) (f : P holds โ†’ D) (p : P holds) โ†’ โจ† P f ๏ผ f p)

injective-structure : (๐“ค ๐“ฅ : Universe) โ†’ ๐“ค โบ โŠ” ๐“ฅ โบ โŠ” ๐“ฆ ฬ‡
injective-structure ๐“ค ๐“ฅ
 = ฮฃ _โˆฃ_ ๊ž‰ ({X : ๐“ค ฬ‡ } {Y : ๐“ฅ ฬ‡ } โ†’ (X โ†’ D) โ†’ (X โ†ช Y) โ†’ (Y โ†’ D))
         , ({X : ๐“ค ฬ‡ } {Y : ๐“ฅ ฬ‡ } (f : X โ†’ D) (๐•› : X โ†ช Y) โ†’ (f โˆฃ ๐•›) โˆ˜ โŒŠ ๐•› โŒ‹ โˆผ f)

derived-injective-structure
 : flabby-structure (๐“ค โŠ” ๐“ฅ) โ†’ injective-structure ๐“ค ๐“ฅ
derived-injective-structure {๐“ค} {๐“ฅ} (โจ† , e)
 = _โˆฃ_ , e'
 where
  _โˆฃ_ : {X : ๐“ค ฬ‡ } {Y : ๐“ฅ ฬ‡ } โ†’ (X โ†’ D) โ†’ (X โ†ช Y) โ†’ (Y โ†’ D)
  (f โˆฃ ๐•›) y = โจ† (Fiber ๐•› y) (f โˆ˜ fiber-point)

  e' : {X : ๐“ค ฬ‡ } {Y : ๐“ฅ ฬ‡ } (f : X โ†’ D) (๐•› : X โ†ช Y) โ†’ (f โˆฃ ๐•›) โˆ˜ โŒŠ ๐•› โŒ‹ โˆผ f
  e' f ๐•› x = e (Fiber ๐•› (โŒŠ ๐•› โŒ‹ x)) (f โˆ˜ fiber-point) (x , refl)

derived-flabby-structure
 : injective-structure ๐“ค ๐“ฅ โ†’ flabby-structure ๐“ค
derived-flabby-structure {๐“ค} (_โˆฃ_ , e) = โจ† , e'
 where
  โจ† : (P : ฮฉ ๐“ค) โ†’ (P holds โ†’ D) โ†’ D
  โจ† P f = (f โˆฃ embedding-to-๐Ÿ™) โ‹†

  e' : (P : ฮฉ ๐“ค) (f : P holds โ†’ D) (p : P holds) โ†’ โจ† P f ๏ผ f p
  e' P f = e f embedding-to-๐Ÿ™

\end{code}

We had already given (in InjectiveTypes.BlackBoard) conversions
between ainjective types and a flabby types. We now record that the
ones we gave here agree definitionally with those there, via the
"repackaging" equivalences gives below.

Unfortunately, InjectiveTypes has a global assumption of function
extensionality, which is not used for the definitions of algebraic
injective and flabby structure. Fortunately, we don't need to use the
proofs below (particularly because they are proved with refl), which
are here only for the purpose of emphasizing that we are working with
the same definitions repackaged in a different way.

\begin{code}

open import UF.FunExt
open import UF.Equiv

module _ (fe : FunExt) where

 open import InjectiveTypes.Blackboard fe

 ainjective-type-repackaging : injective-structure ๐“ค ๐“ฅ โ‰ƒ ainjective-type D ๐“ค ๐“ฅ
 ainjective-type-repackaging =
  qinveq
   (ฮป (_โˆฃ_ , e) โ†’ ฮป {X} {Y} j i f โ†’ (f โˆฃ (j , i)) , e f (j , i))
   ((ฮป ainj โ†’
       (ฮป {X} {Y} f ๐•› โ†’ prโ‚ (ainj โŒŠ ๐•› โŒ‹ โŒŠ ๐•› โŒ‹-is-embedding f)) ,
       (ฮป {X} {Y} f ๐•› โ†’ prโ‚‚ (ainj โŒŠ ๐•› โŒ‹ โŒŠ ๐•› โŒ‹-is-embedding f))) ,
    (ฮป _ โ†’ refl) ,
    (ฮป _ โ†’ refl))

 aflabby-repackaging : flabby-structure ๐“ค โ‰ƒ aflabby D ๐“ค
 aflabby-repackaging
  = qinveq
     (ฮป (โจ† , e) P i f โ†’ โจ† (P , i) f , e (P , i) f)
     ((ฮป aflab โ†’
        (ฮป P f โ†’ prโ‚ (aflab (P holds) (holds-is-prop P) f)) ,
        (ฮป P f โ†’ prโ‚‚ (aflab (P holds) (holds-is-prop P) f))) ,
      (ฮป _ โ†’ refl) ,
      (ฮป _ โ†’ refl))

\end{code}

TODO. Write the commutative squares corresponding to the following two
proofs as a comment.

\begin{code}

 derived-flabby-structure-agreement
  : (s : injective-structure ๐“ค ๐“ฅ)
  โ†’ โŒœ aflabby-repackaging โŒ (derived-flabby-structure s)
  ๏ผ ainjective-types-are-aflabby D (โŒœ ainjective-type-repackaging โŒ s)
 derived-flabby-structure-agreement s = refl

 \end{code}

 For the second one we need to do a manual eta expasion to deal with
 the way Agda works with implicit arguments, which gives unsolved
 constraints otherwise (this is a well known design issue).

 \begin{code}

 derived-injective-structure-agreement
  : (s : flabby-structure ๐“ค)
  โ†’ (ฮป {X Y : ๐“ค ฬ‡} (j : X โ†’ Y)
     โ†’ โŒœ ainjective-type-repackaging โŒ (derived-injective-structure s) {X} {Y} j)
  ๏ผ aflabby-types-are-ainjective D (โŒœ aflabby-repackaging โŒ s)
 derived-injective-structure-agreement s = refl

\end{code}

We can change variables in โจ† in the following sense. Notice that there
is a similar fact proved with the stronger assumption of univalence in
the development of the lifting monad. But propositional and functional
extensionality are enough.

\begin{code}

open import UF.Subsingletons

module _
        (pe : propext ๐“ค)
        (fe : funext ๐“ค ๐“ค)
        (โจ† : (P : ฮฉ ๐“ค) โ†’ (P holds โ†’ D) โ†’ D)
        {P Q : ฮฉ ๐“ค}
        (f : P holds โ†’ D)
       where

 โจ†-change-of-variable : ((g , h) : (P holds) โ†” Q holds)
                      โ†’ โจ† P f ๏ผ โจ† Q (f โˆ˜ h)
 โจ†-change-of-variable (g , h) = IV
  where
   h' : (e : P ๏ผ Q) โ†’ Q holds โ†’ P holds
   h' e = โŒœ idtoeq _ _ (ap _holds e) โŒโปยน

   I : (e : P ๏ผ Q) โ†’ h' e ๏ผ h
   I e = dfunext fe (ฮป p โ†’ holds-is-prop P (h' e p) (h p))

   II : (e : P ๏ผ Q) โ†’ โจ† P f ๏ผ โจ† Q (f โˆ˜ h' e)
   II refl = refl

   e : P ๏ผ Q
   e = ฮฉ-extensionality pe fe g h

   III : โจ† P f ๏ผ โจ† Q (f โˆ˜ h' e)
   III = II e

   IV : โจ† P f ๏ผ โจ† Q (f โˆ˜ h)
   IV = transport (ฮป - โ†’ โจ† P f ๏ผ โจ† Q (f โˆ˜ -)) (I e) III

 โจ†-change-of-variable-โ‰ƒ : (๐•˜ : (P holds) โ‰ƒ Q holds)
                        โ†’ โจ† P f ๏ผ โจ† Q (f โˆ˜ โŒœ ๐•˜ โŒโปยน)
 โจ†-change-of-variable-โ‰ƒ ๐•˜ = โจ†-change-of-variable (โŒœ ๐•˜ โŒ , โŒœ ๐•˜ โŒโปยน)

\end{code}

We give names to the projections.

\begin{code}

injective-extension-operator
 : injective-structure ๐“ค ๐“ฅ
 โ†’ {X : ๐“ค ฬ‡ } {Y : ๐“ฅ ฬ‡ } โ†’ (X โ†’ D) โ†’ (X โ†ช Y) โ†’ (Y โ†’ D)
injective-extension-operator (_โˆฃ_ , e) = _โˆฃ_

injective-identification
 : ((_โˆฃ_ , e) : injective-structure ๐“ค ๐“ฅ)
 โ†’ {X : ๐“ค ฬ‡ } {Y : ๐“ฅ ฬ‡ } (f : X โ†’ D) (๐•› : X โ†ช Y) โ†’ (f โˆฃ ๐•›) โˆ˜ โŒŠ ๐•› โŒ‹ โˆผ f
injective-identification (_โˆฃ_ , e) = e

flabby-extension-operator
 : flabby-structure ๐“ค
 โ†’ (P : ฮฉ ๐“ค) โ†’ (P holds โ†’ D) โ†’ D
flabby-extension-operator (โจ† , h) = โจ†

flabby-identification
 : ((โจ† , e) : flabby-structure ๐“ค)
 โ†’ (P : ฮฉ ๐“ค) (f : P holds โ†’ D) (p : P holds) โ†’ โจ† P f ๏ผ f p
flabby-identification (_โˆฃ_ , e) = e

\end{code}

\end{code}

Maybe we should have worked with the following equivalent derived
injective structure, as this would have avoided some detours in
proofs in the module InjectiveTypes.Algebra.

\begin{code}

module _
        {๐“ค ๐“ฅ : Universe}
        (s@(โจ† , e) : flabby-structure (๐“ค โŠ” ๐“ฅ))
       where

 private
  module _ {X : ๐“ค ฬ‡ } {Y : ๐“ฅ ฬ‡ } (๐•› : X โ†ช Y) (y : Y) where
   k : fiber โŒŠ ๐•› โŒ‹ y โ†’ ๐Ÿ™
   k = unique-to-๐Ÿ™ {๐“ค โŠ” ๐“ฅ} {๐“ค โŠ” ๐“ฅ}

   g : fiber โŒŠ ๐•› โŒ‹ y โ†’ fiber k โ‹†
   g w = w , refl

   h : fiber k โ‹† โ†’ fiber โŒŠ ๐•› โŒ‹ y
   h = prโ‚

 derived-injective-structure' : injective-structure ๐“ค ๐“ฅ
 derived-injective-structure' = _โˆฃ_ , e'
  where
   _โˆฃ_ : {X : ๐“ค ฬ‡ } {Y : ๐“ฅ ฬ‡ } โ†’ (X โ†’ D) โ†’ (X โ†ช Y) โ†’ (Y โ†’ D)
   (f โˆฃ ๐•›) y = โจ† (Fiber (fiber-to-๐Ÿ™ ๐•› y) โ‹†) (f โˆ˜ fiber-point โˆ˜ h ๐•› y)

   e' : {X : ๐“ค ฬ‡ } {Y : ๐“ฅ ฬ‡ } (f : X โ†’ D) (๐•› : X โ†ช Y) โ†’ (f โˆฃ ๐•›) โˆ˜ โŒŠ ๐•› โŒ‹ โˆผ f
   e' f ๐•› x = e (Fiber (fiber-to-๐Ÿ™ ๐•› (โŒŠ ๐•› โŒ‹ x)) โ‹†)
                (f โˆ˜ fiber-point โˆ˜ h ๐•› (โŒŠ ๐•› โŒ‹ x))
                ((x , refl) , refl)

 private
  _โˆฃ_ _โˆฃ'_ : {X : ๐“ค ฬ‡ } {Y : ๐“ฅ ฬ‡ } โ†’ (X โ†’ D) โ†’ (X โ†ช Y) โ†’ (Y โ†’ D)
  _โˆฃ_  = injective-extension-operator (derived-injective-structure s)
  _โˆฃ'_ = injective-extension-operator derived-injective-structure'

\end{code}

The agreement of these two extension operators is a direct application
of change of variables in โจ†, defined above.

\begin{code}

 derived-injective-structure-operator-lemma
  : propext (๐“ค โŠ” ๐“ฅ)
  โ†’ funext (๐“ค โŠ” ๐“ฅ) (๐“ค โŠ” ๐“ฅ)
  โ†’ {X : ๐“ค ฬ‡ } {Y : ๐“ฅ ฬ‡ } (f : X โ†’ D) (๐•› : X โ†ช Y)
  โ†’ f โˆฃ ๐•› โˆผ f โˆฃ' ๐•›
 derived-injective-structure-operator-lemma pe fe f ๐•› y
  = (f โˆฃ ๐•›) y                                              ๏ผโŸจ refl โŸฉ
    โจ† (Fiber ๐•› y) (f โˆ˜ fiber-point)                       ๏ผโŸจ I โŸฉ
    โจ† (Fiber (fiber-to-๐Ÿ™ ๐•› y) โ‹†) (f โˆ˜ fiber-point โˆ˜ h ๐•› y) ๏ผโŸจ refl โŸฉ
    (f โˆฃ' ๐•›) y โˆŽ
    where
     I = โจ†-change-of-variable pe fe โจ† (f โˆ˜ fiber-point) (g ๐•› y , h ๐•› y)

\end{code}