Martin Escardo, 22nd October 2024 - 15 June 2025

Abstract. Both here in TypeTopology and in the publication

[0] Mathematical Structures in Computer Science, Cambridge University
    Press, 5th January 2021.
    https://doi.org/10.1017/S0960129520000225

we defined notions of "algebraically injective" and "algebraically
flabby" type.

Here we rename these notions to "injective structure" and "flabby
structure", and define new notions of "algebraically injective" and
"algebraically flabby" structure, so that the following are isomorphic
for any *set* D:

 (i)   Algebraic injective structure on D.

 (ii)  Algebraic flabby structure on D.

 (iii) 𝓛-algebra structure on D, where 𝓛 is the lifting monad, also
       known as the partial-map classifier monad.

This theorem applies to objects D of (elementary) 1-toposes, given
that we work constructively in HoTT/UF here, without assuming
univalence.

For an arbitrary type D, we only prove the above to be *logical
equivalences*, but perhaps there is a chance that they are actually
typal equivalences, and we leave this as an open problem.

The following ASSUME slides (https://tdejong.com/ASSUME/) discuss
this, but we include most of the discussion here in comments.

[1] Taking "algebraically" seriously in the definition of
algebraically injective type.
https://cs.bham.ac.uk/~mhe/.talks/2025-05-29-Note-09-58-algebraic-injectives-assume_pdf.pdf

Introduction. We give conditions on injective structure on a type D so
that it coincides with the algebraic structure for the partial-map
classifier (aka lifting) monad 𝓛 on types, when D is a set, and we
also have partial results in this direction when D is a general type.

We call these conditions "associativity" and "pullback naturality".

Associativity says that an extension (f|j)|k of an extension f|j is
the extension f|(k∘j) along the composition of the embeddings j and k,
as in the following commutative diagram:


                   j         k
              X ──────→ Y ──────→ Z
               β•²        β”‚        β•±
                β•²       β”‚       β•±
             f   β•²      β”‚ f|j  β•± (f|j)|k = f∣(k∘j)
                  β•²     β”‚     β•±
                   β•²    β”‚    β•±
                    β•²   β”‚   β•±
                     ➘  ↓  ↙
                        D.

Pullback naturality is expressed by the following diagram, where the
square is a pullback and j (and hence k) is an embedding:

                   k
              A ──────→ B
              β”‚ ⌟       β”‚
           g  β”‚         β”‚ h
              β”‚         β”‚
              ↓    j    ↓
              X ──────→ Y
               β•²        β”‚
                β•²       β”‚
             f   β•²      β”‚ f|j ∘ h = (f ∘ g) | k
                  β•²     β”‚
                   β•²    β”‚
                    β•²   β”‚
                     ➘  ↓
                        D.

It actually suffices to consider pullbacks of the form


        fiber j y ────→ πŸ™
              β”‚ ⌟       β”‚
              β”‚         β”‚ y
              β”‚         β”‚
              ↓    j    ↓
              X ──────→ Y.

This is a sense in which extensions are pointwise (or fiberwise).

One may wonder whether it is reasonable to consider naturality with
respect to all commutative squares which are not necessarily pullbacks,

                   k
              A ──────→ B
              β”‚         β”‚
           g  β”‚         β”‚ h
              β”‚         β”‚
              ↓    j    ↓
              X ──────→ Y,

where j and k are embeddings. However, a counter-example is the
commutative square


              𝟘 ──────→ πŸ™
              β”‚         β”‚
              β”‚         β”‚
              β”‚         β”‚
              ↓         ↓
              πŸ™ ──────→ πŸ™.

Now, an algebra Ξ± : 𝓛 D β†’ D of the lifting monad amounts flabbiness
structure plus an associativity law on this structure. Via the
canonical correspondence between algebraic injective structure and
algebraic flabby structure, the above associativity condition
corresponds to the associativity law for 𝓛-algebras (which justifies
our terminology in the case of injectivity). In terms of flabbinnes,
this says that if we have a situation

            P Γ— Q ────→ πŸ™
               β•²        β”‚
                β•²       β”‚
             f   β•²      β”‚
                  β•²     β”‚
                   β•²    β”‚
                    β•²   β”‚
                     ➘  ↓
                        D

where P and Q propositions, so that also P Γ— Q is a proposition, then
we can

 1. extend f at once, or
 2. extend f in its first variable and then in its second variable,

and these two processes give the same result. More precisely, rather
than P Γ— Q we have the type Ξ£ p : P , Q p, where Q depends on p : P,
but this doesn't make any difference, as shown in the study of the
lifting monad elsewhere in this development.

As for pullback naturality, it is given automatically by the canonical
construction of algebraic injectivity data from algebraic flabiness
data.

If we define homomorphisms h : D β†’ E of algebraic injectives in the
obvious way, namely, that for any f : X β†’ D and j : X β†ͺ Y we have that

    h ∘ f ∣ j = (h ∘ f) ∣ j

as (partially) illustrated by the (incomplete, due to typographical
reasons) diagram

                   j
              X ───────→ Y
               β•²       β•±
                β•²     β•±
               f β•²   β•± f/j
                  ➘ ↙
                   D
                   β”‚
                   β”‚ h
                   ↓
                   E,

then injective homomorphisms correspond to 𝓛-homomorphisms.

When we restrict to types that are sets, we get that the category of
associative, pullback-natural algebraically injective objects is
isomorphic to the category of 𝓛-algebras.

This result holds for the objects of any 1-topos, due to our
constructive reasoning in a restricted type theory.

However, at the moment we don't have a result for ∞-toposes, because,
although the associativity, pullback naturality and the algebra
equations are all property for sets, they are data for arbitrary
types, and we have proved only a logical equivalence of associativity
+ pullback-naturality with the 𝓛-algebra equations, rather than a full
type equivalence (whose possibility is an interesting open problem).

\begin{code}

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

open import MLTT.Spartan
open import UF.FunExt

\end{code}

In this file we work with an arbitrary type D in a universe 𝓦.

\begin{code}

module InjectiveTypes.Algebra
        {𝓦 : Universe}
        (D : 𝓦 Μ‡ )
       where

open import InjectiveTypes.Structure
open import UF.Embeddings renaming (_∘β†ͺ_ to _⊚_)
open import UF.Equiv
open import UF.EquivalenceExamples
open import UF.Pullback
open import UF.Sets
open import UF.Subsingletons
open import UF.Subsingletons-FunExt
open import UF.SubtypeClassifier

\end{code}

Definitions of associativity and pullback naturality for injective
structure.

\begin{code}

module _
        {𝓀 : Universe}
        (s@(_∣_ , _) : injective-structure D 𝓀 𝓀)
       where

 injective-associativity : 𝓦 βŠ” 𝓀 ⁺ Μ‡
 injective-associativity = (X Y Z : 𝓀 Μ‡ ) (f : X β†’ D) (𝕛 : X β†ͺ Y) (π•œ : Y β†ͺ Z)
                         β†’ f ∣ (π•œ ⊚ 𝕛) ∼ (f ∣ 𝕛) ∣ π•œ

\end{code}

For the following definition, we consider the standard pullback

                   pbβ‚‚
    pullback j h ─────→ B
              β”‚ ⌟       β”‚
          pb₁ β”‚         β”‚ h
              β”‚         β”‚
              ↓     j   ↓
              X ──────→ Y,

where

    pullback j h := Ξ£ (x , y) κž‰ X Γ— B , j x = h y

and pb₁ and pbβ‚‚ are the projections, rather than an abstract pullback,
for simplicity, so that the above naturality condition becomes

                   pbβ‚‚
    pullback j h ─────→ B
              β”‚ ⌟       β”‚
          pb₁ β”‚         β”‚ h
              β”‚         β”‚
              ↓     j   ↓
              X ──────→ Y
               β•²        β”‚
                β•²       β”‚
             f   β•²      β”‚ (f | j) ∘ h = (f ∘ pb₁) | pbβ‚‚
                  β•²     β”‚
                   β•²    β”‚
                    β•²   β”‚
                     ➘  ↓
                        D.

\begin{code}

 pullback-naturality : (𝓀 ⁺) βŠ” 𝓦 Μ‡
 pullback-naturality = (X Y B : 𝓀 Μ‡ )
                       (f : X β†’ D)
                       (𝕛 : X β†ͺ Y)
                       (h : B β†’ Y)
                      β†’ let open pullback ⌊ 𝕛 βŒ‹ h renaming (pullback to A)
                            𝑝𝑏₂ : A β†ͺ B
                            𝑝𝑏₂ = 𝕑𝕓₂ ⌊ 𝕛 βŒ‹-is-embedding
                        in (f ∣ 𝕛) ∘ h ∼ (f ∘ pb₁) ∣ 𝑝𝑏₂

\end{code}

The following is a particular case of this notion, but also equivalent
to it.

\begin{code}

 extensions-are-fiberwise : 𝓀 ⁺ βŠ” 𝓦 Μ‡
 extensions-are-fiberwise = (X Y : 𝓀 Μ‡ )
                            (f : X β†’ D)
                            (𝕛 : X β†ͺ Y)
                            (y : Y)
                          β†’ (f ∣ 𝕛) y = ((f ∘ fiber-point) ∣ fiber-to-πŸ™ 𝕛 y) ⋆
\end{code}

The following implicitly uses the fact that the diagram


       fiber j y ─────→ πŸ™
              β”‚ ⌟       β”‚
  fiber-point β”‚         β”‚ y
              β”‚         β”‚
              ↓     j   ↓
              X ──────→ Y

is a pullback (perhaps we should make this explicit in the proof, but
this involves adding more material to the current material on
pullabacks (TODO)).

\begin{code}

 pullback-naturality-gives-that-extensions-are-fiberwise
  : propext 𝓀
  β†’ funext 𝓀 𝓀
  β†’ pullback-naturality
  β†’ extensions-are-fiberwise
 pullback-naturality-gives-that-extensions-are-fiberwise pe fe pbn X Y f 𝕛 y
  = II
  where
   h : πŸ™ {𝓀} β†’ Y
   h _ = y

   open pullback ⌊ 𝕛 βŒ‹ h renaming (pullback to A)

   Ο• = A                                  β‰ƒβŸ¨ ≃-refl _ ⟩
       (Ξ£ z κž‰ X Γ— πŸ™ , ⌊ 𝕛 βŒ‹ (pr₁ z) = y) β‰ƒβŸ¨ Ξ£-assoc ⟩
       (Ξ£ x κž‰ X , πŸ™ Γ— (⌊ 𝕛 βŒ‹ x = y))     β‰ƒβŸ¨ Ξ£-cong (Ξ» x β†’ πŸ™-lneutral) ⟩
       fiber ⌊ 𝕛 βŒ‹ y                      β– 

   𝑝𝑏₂ : A β†ͺ πŸ™
   𝑝𝑏₂ = 𝕑𝕓₂ ⌊ 𝕛 βŒ‹-is-embedding

   π‘π‘Ÿβ‚ : X Γ— πŸ™ β†ͺ X
   π‘π‘Ÿβ‚ = 𝕑𝕣₁ (Ξ» _ β†’ πŸ™-is-prop)

   _ : pb₁ = fiber-point ∘ ⌜ Ο• ⌝
   _ = refl

   I : 𝑝𝑏₂ = embedding-to-πŸ™
   I = to-subtype-= (being-embedding-is-prop fe) refl

   ⨆ : (P : Ξ© 𝓀) β†’ (P holds β†’ D) β†’ D
   ⨆ P g = (g ∣ embedding-to-πŸ™) ⋆

   II = (f ∣ 𝕛) y                                        =⟨ by-pbn ⟩
        ((f ∘ pb₁) ∣ 𝑝𝑏₂) ⋆                              =⟨ refl ⟩
        ((f ∘ fiber-point ∘ ⌜ Ο• ⌝) ∣ 𝑝𝑏₂) ⋆              =⟨ by-I ⟩
        ((f ∘ fiber-point ∘ ⌜ Ο• ⌝) ∣ embedding-to-πŸ™) ⋆   =⟨ refl ⟩
        ⨆ (Fiber (𝕛 ⊚ π‘π‘Ÿβ‚) y) (f ∘ fiber-point ∘ ⌜ Ο• ⌝)  =⟨ change-of-var ⁻¹ ⟩
        ⨆ (Fiber 𝕛 y) (f ∘ fiber-point)                  =⟨ refl ⟩
        ((f ∘ fiber-point) ∣ fiber-to-πŸ™ 𝕛 y) ⋆           ∎
         where
          by-pbn = pbn X Y πŸ™ f 𝕛 h ⋆
          by-I = ap (Ξ» - β†’ ((f ∘ fiber-point ∘ ⌜ Ο• ⌝) ∣ -) ⋆) I
          change-of-var = ⨆-change-of-variable D pe fe ⨆ (f ∘ fiber-point)
                          (⌜ Ο• ⌝⁻¹ , ⌜ Ο• ⌝)

\end{code}

TODO. At the moment, we define pullback naturality with respect to the
canonical, or chosen, pullback. But the above argument actually shows
that this implies naturality with respect to any pullback. So we
should reformulate the above in this way, and then use the (already
proved) fact that fibers are pullbacks. This is low priority, but it
is interesting for conceptual reasons.

We now observe that the pullback requirement in the naturality
condition is essential, no matter which injective structure we have,
provided D has the property that for every d : D there is a designated
d' β‰  d. This is the case in all examples of algebraically injective
types we've identified so far (for example, for the universe, d' can
be given by negation). We also need function extensionality for
functions defined on the empty type (but we assume general function
extensionality).

\begin{code}

module counter-example-to-general-naturality
        (Ο• : D β†’ D)
        (Ξ΄ : (d : D) β†’ Ο• d β‰  d)
        (𝓀 π“₯ : Universe)
        ((_∣_ , _∣_-is-extension) : injective-structure D 𝓀 π“₯)
        (fe : funext 𝓀 𝓦)
      where

 A X : 𝓀 Μ‡
 B Y : π“₯ Μ‡

 A = 𝟘
 B = πŸ™
 X = πŸ™
 Y = πŸ™

 π•œ : A β†ͺ B
 𝕛 : X β†ͺ Y
 g : A β†’ X
 h : B β†’ Y

 π•œ = unique-from-𝟘 , unique-from-𝟘-is-embedding
 𝕛 = unique-to-πŸ™ , maps-of-props-are-embeddings _ πŸ™-is-prop πŸ™-is-prop
 g = unique-from-𝟘
 h = id

 fβ‚€ : A β†’ D
 fβ‚€ = unique-from-𝟘

 dβ‚€ : D
 dβ‚€ = (fβ‚€ ∣ π•œ) ⋆

 f : X β†’ D
 f _ = Ο• dβ‚€

 naturality-failure : Β¬ ((f ∣ 𝕛) ∘ h ∼ (f ∘ g) ∣ π•œ)
 naturality-failure p = Ξ΄ dβ‚€ II
  where
   I : f ∘ g = fβ‚€
   I = dfunext fe (Ξ» x β†’ 𝟘-elim x)

   II = Ο• dβ‚€              =⟨ refl ⟩
        f ⋆               =⟨ (_∣_-is-extension f 𝕛 ⋆)⁻¹ ⟩
        (f ∣ 𝕛) (⌊ 𝕛 βŒ‹ ⋆) =⟨ refl ⟩
        (f ∣ 𝕛) ⋆         =⟨ refl ⟩
        ((f ∣ 𝕛) ∘ h) ⋆   =⟨ p ⋆ ⟩
        ((f ∘ g) ∣ π•œ) ⋆   =⟨ ap (Ξ» - β†’ (- ∣ π•œ) ⋆) I ⟩
        (fβ‚€ ∣ π•œ) ⋆        =⟨ refl ⟩
        dβ‚€                ∎

\end{code}

The notion of flabby associativity.

\begin{code}

module _
        {𝓀 : Universe}
        (s@(⨆ , _) : flabby-structure D 𝓀)
       where

 flabby-associativity : 𝓀 ⁺ βŠ” 𝓦 Μ‡
 flabby-associativity = (P : Ξ© 𝓀) (Q : P holds β†’ Ξ© 𝓀) (f : ΣΩ Q holds β†’ D)
                      β†’ ⨆ (ΣΩ Q) f = ⨆ P (Ξ» p β†’ ⨆ (Q p) (Ξ» q β†’ f (p , q)))

\end{code}

We now show that flabby associativity implies injective associativity
and pullback naturality of the derived injective structure, assuming
propositional and functional extensionality.

\begin{code}

 module _
         (pe : Prop-Ext)
         (fe : Fun-Ext)
       where

  private
   _∣_ : {X Y : 𝓀 Μ‡ } β†’ (X β†’ D) β†’ (X β†ͺ Y) β†’ (Y β†’ D)
   _∣_ = injective-extension-operator D (derived-injective-structure D s)

  derived-injective-associativity
   : flabby-associativity
   β†’ injective-associativity (derived-injective-structure D s)
  derived-injective-associativity fassoc X Y Z f 𝕛 π•œ z = V
   where
    I : ⨆ (ΣΩ w κž‰ Fiber π•œ z , Fiber 𝕛 (fiber-point w)) (Ξ» q β†’ f (fiber-point (prβ‚‚ q)))
      = ⨆ (Fiber π•œ z) (Ξ» u β†’ ⨆ (Fiber 𝕛 (fiber-point u)) (f ∘ fiber-point))
    I = fassoc
          (Fiber π•œ z)
          (Ξ» (p : fiber ⌊ π•œ βŒ‹ z) β†’ Fiber 𝕛 (fiber-point p))
          (Ξ» (q : (Ξ£ w κž‰ fiber ⌊ π•œ βŒ‹ z , fiber ⌊ 𝕛 βŒ‹ (fiber-point w)))
                β†’ f (fiber-point (prβ‚‚ q)))

    II : (fiber ⌊ π•œ ⊚ 𝕛 βŒ‹ z) ≃ (Ξ£ w κž‰ fiber ⌊ π•œ βŒ‹ z , fiber ⌊ 𝕛 βŒ‹ (fiber-point w))
    II = fiber-of-composite ⌊ 𝕛 βŒ‹ ⌊ π•œ βŒ‹ z

    III : ⨆ (Fiber (π•œ ⊚ 𝕛) z) (f ∘ fiber-point)
      = ⨆ (ΣΩ w κž‰ Fiber π•œ z , Fiber 𝕛 (fiber-point w)) (Ξ» q β†’ f (fiber-point (prβ‚‚ q)))
    III = ⨆-change-of-variable-≃ D pe fe ⨆ (f ∘ fiber-point) II

    IV : ⨆ (Fiber (π•œ ⊚ 𝕛) z) (f ∘ fiber-point)
      = ⨆ (Fiber π•œ z) (Ξ» w β†’ ⨆ (Fiber 𝕛 (fiber-point w)) (f ∘ fiber-point))
    IV = III βˆ™ I

    V : (f ∣ (π•œ ⊚ 𝕛)) z = ((f ∣ 𝕛) ∣ π•œ) z
    V = IV

  derived-injective-fiberwise-extensions
   : extensions-are-fiberwise (derived-injective-structure D s)
  derived-injective-fiberwise-extensions X Y f 𝕛 y
   = I
   where
    I : (f ∣ 𝕛) y = ((f ∘ fiber-point) ∣ fiber-to-πŸ™ 𝕛 y) ⋆
    I = derived-injective-structure-operator-lemma D s pe fe f 𝕛 y

\end{code}

The injective structure derived from a flabby structure is pullback
natural.

\begin{code}

  derived-injective-pullback-naturality
   : pullback-naturality (derived-injective-structure D s)
  derived-injective-pullback-naturality X Y B f 𝕛 h = II
   where
    open pullback ⌊ 𝕛 βŒ‹ h

    𝑝𝑏₂ : pullback β†ͺ B
    𝑝𝑏₂ = 𝕑𝕓₂ ⌊ 𝕛 βŒ‹-is-embedding

    module _ (b : B) where

     Ο• : fiber ⌊ 𝕛 βŒ‹ (h b) β†’ fiber ⌊ 𝑝𝑏₂ βŒ‹ b
     Ο• = (Ξ» (x , e) β†’ ((x , b) , e) , refl)

     ψ : fiber ⌊ 𝑝𝑏₂ βŒ‹ b β†’ fiber ⌊ 𝕛 βŒ‹ (h b)
     ψ (((x , _) , e) , refl) = (x , e)

     I : f ∘ pr₁ ∘ ψ ∼ f ∘ pb₁ ∘ fiber-point
     I (((x , _) , e) , refl) = refl


     II : (f ∣ 𝕛) (h b) = ((f ∘ pb₁) ∣ 𝑝𝑏₂) b
     II = (f ∣ 𝕛) (h b)                            =⟨ refl ⟩
          ⨆ (Fiber 𝕛 (h b)) (f ∘ fiber-point)      =⟨ IIβ‚€ ⟩
          ⨆ (Fiber 𝑝𝑏₂ b) (f ∘ fiber-point ∘ ψ)    =⟨ II₁ ⟩
          ⨆ (Fiber 𝑝𝑏₂ b) (f ∘ pb₁ ∘ fiber-point)  =⟨ refl ⟩
          ((f ∘ pb₁) ∣ 𝑝𝑏₂) b                      ∎
           where
            IIβ‚€ = ⨆-change-of-variable D pe fe ⨆ (f ∘ fiber-point) (Ο• , ψ)
            II₁ = ap (⨆ (Fiber 𝑝𝑏₂ b)) (dfunext fe I)

\end{code}

We now consider the flabby structure derived from the injective
structure derived from the flabby structure, and show that it is the
identity on extension operators.

\begin{code}

  private
   ⨆' : (P : Ξ© 𝓀) β†’ (P holds β†’ D) β†’ D
   ⨆' = flabby-extension-operator D
          (derived-flabby-structure D {𝓀} {𝓀}
            (derived-injective-structure D s))

\end{code}

The round trip ⨆ ↦ _∣_ ↦ ↦ ⨆' is the identity.

\begin{code}

  ⨆-round-trip : ⨆ = ⨆'
  ⨆-round-trip = dfunext fe (Ξ» P β†’ dfunext fe (I P))
   where
    I :  (P : Ξ© 𝓀) (f : P holds β†’ D) β†’ ⨆ P f = ⨆' P f
    I P f = ⨆ P f                                        =⟨ Iβ‚€ ⟩
            ⨆ (Fiber embedding-to-πŸ™ ⋆) (f ∘ fiber-point) =⟨ refl ⟩
            ⨆' P f                                       ∎
      where
       Iβ‚€ = ⨆-change-of-variable D pe fe ⨆ f ((Ξ» p β†’ p , refl) , fiber-point)

\end{code}

Notice that we didn't use the extension properties of the flabby
structure or the derived injective structure. The same is the case
below.

We now show that injective associativity implies flabby associativity
of the derived flabby structure, assuming pullback naturality, and,
again, propositional and functional extensionality.

\begin{code}

module _
        {𝓀          : Universe}
        (s@(_∣_ , _) : injective-structure D 𝓀 𝓀)
        (pe          : Prop-Ext)
        (fe          : Fun-Ext)
      where

 private
  ⨆ : (P : Ξ© 𝓀) β†’ (P holds β†’ D) β†’ D
  ⨆ = flabby-extension-operator D (derived-flabby-structure D s)

 derived-flabby-associativity
  : injective-associativity s
  β†’ pullback-naturality s
  β†’ flabby-associativity (derived-flabby-structure D s)
 derived-flabby-associativity iassoc pbn P Q f
  = ⨆ (ΣΩ Q) f                             =⟨ refl ⟩
    (f ∣ w) ⋆                              =⟨ ap (Ξ» - β†’ (f ∣ -) ⋆) I ⟩
    (f ∣ (v ⊚ u)) ⋆                        =⟨ iassoc _ _ _ f u v ⋆ ⟩
    ((f ∣ u) ∣ v) ⋆                        =⟨ refl ⟩
    ⨆ P (f ∣ u)                            =⟨ ap (⨆ P) (dfunext fe III) ⟩
    ⨆ P (Ξ» p β†’ ⨆ (Q p) (Ξ» q β†’ f (p , q))) ∎
    where
     u : ΣΩ Q holds β†ͺ P holds
     u = 𝕑𝕣₁ (Ξ» p β†’ holds-is-prop (Q p))

     v : P holds β†ͺ πŸ™
     v = embedding-to-πŸ™

     w : ΣΩ Q holds β†ͺ πŸ™
     w = embedding-to-πŸ™

     I : w = v ⊚ u
     I = to-subtype-= (being-embedding-is-prop fe) refl

     II : (p : P holds)
        β†’ ⨆ (Fiber u p) (f ∘ fiber-point) = ⨆ (Q p) (Ξ» q β†’ f (p , q))
     II p = ⨆-change-of-variable D pe fe ⨆ (f ∘ fiber-point) (g , h)
      where
       g : fiber ⌊ u βŒ‹ p β†’ Q p holds
       g ((p' , q) , _) = transport (Ξ» - β†’ Q - holds) (holds-is-prop P p' p) q

       h : Q p holds β†’ fiber ⌊ u βŒ‹ p
       h q = (p , q) , holds-is-prop P (⌊ u βŒ‹ (p , q)) p

     III : (p : P holds) β†’ (f ∣ u) p = ⨆ (Q p) (Ξ» q β†’ f (p , q))
     III p = (f ∣ u) p                             =⟨ IIβ‚€ ⟩
            ((f ∘ fiber-point) ∣ fiber-to-πŸ™ u p) ⋆ =⟨ refl ⟩
            ⨆ (Fiber u p) (f ∘ fiber-point)        =⟨ II p ⟩
            ⨆ (Q p) (Ξ» q β†’ f (p , q))              ∎
             where
              IIβ‚€ = pullback-naturality-gives-that-extensions-are-fiberwise
                     s pe fe pbn (ΣΩ Q holds) (P holds) f u p

\end{code}

We now show that the round trip _∣_ ↦ ⨆ ↦ _∣'_ is the identity.

\begin{code}

 private
  s' : injective-structure D 𝓀 𝓀
  s' = derived-injective-structure D (derived-flabby-structure D s)

  _∣'_ : {X Y : 𝓀 Μ‡} β†’ (X β†’ D) β†’ X β†ͺ Y β†’ Y β†’ D
  _∣'_ = injective-extension-operator D {𝓀} {𝓀} s'

 ∣-round-trip' : pullback-naturality s
               β†’ (X Y : 𝓀 Μ‡) (f : X β†’ D) (𝕛 : X β†ͺ Y)
               β†’ f ∣ 𝕛 ∼ f ∣' 𝕛
 ∣-round-trip' pbn X Y f 𝕛 y =
  (f ∣ 𝕛) y                                 =⟨ I ⟩
  ((f ∘ fiber-point) ∣ fiber-to-πŸ™ 𝕛 y) ⋆    =⟨ refl ⟩
  (f ∣' 𝕛) y                                ∎
  where
   I = pullback-naturality-gives-that-extensions-are-fiberwise
        s pe fe pbn X Y f 𝕛 y

\end{code}

We need to eta-expand the lhs of the following equality to avoid Agda
getting lost due to the way it deals with implicit arguments. What we
are really showing is that _∣_ = _∣'_.

\begin{code}

 ∣-round-trip : pullback-naturality s
              β†’ (Ξ» {X} {Y} β†’ _∣_ {X} {Y}) = _∣'_
 ∣-round-trip pbn =
  implicit-dfunext fe (Ξ» X β†’
  implicit-dfunext fe (Ξ» Y β†’
  dfunext          fe (Ξ» f β†’
  dfunext          fe (Ξ» 𝕛 β†’
  dfunext          fe (Ξ» y β†’ ((∣-round-trip' pbn X Y f 𝕛 y)))))))

\end{code}

We now put the above together to get the main results of this file.

Motivated by the above, we (re)define algebraic injective and flabby
structure on our type D as follows.

\begin{code}

module _ {𝓀 : Universe} where

 ainjective-structure aflabby-structure : 𝓀 ⁺ βŠ” 𝓦 Μ‡

 ainjective-structure = Ξ£ s κž‰ injective-structure D 𝓀 𝓀
                            , injective-associativity s
                            Γ— pullback-naturality s

 aflabby-structure    = Ξ£ t κž‰ flabby-structure D 𝓀
                            , flabby-associativity t

\end{code}

When D is a set, pullback naturality and the two associativity
conditions are property rather than data.

\begin{code}

 module _
         (D-is-set : is-set D)
         (fe : Fun-Ext)
        where

  injective-associativity-is-prop
   : (s : injective-structure D 𝓀 𝓀)
   β†’ is-prop (injective-associativity s)
  injective-associativity-is-prop s
   = Π₇-is-prop fe (Ξ» _ _ _ _ _ _ _ β†’ D-is-set)

  pullback-naturality-is-prop
   : (s : injective-structure D 𝓀 𝓀)
   β†’ is-prop (pullback-naturality s)
  pullback-naturality-is-prop s
   = Π₇-is-prop fe (Ξ» _ _ _ _ _ _ _ β†’ D-is-set)

  flabby-associativity-is-prop
   : (t : flabby-structure D 𝓀)
   β†’ is-prop (flabby-associativity t)
  flabby-associativity-is-prop t
   = Π₃-is-prop fe (Ξ» _ _ _ β†’ D-is-set)

\end{code}

And the main theorem of this file is that the above notions of
algebraic injectivity and flabbines are equivalent (assuming
propositional and functional extensionality).

For our arbitrary type D, all we know so far is that they *logically*
equivalent.

\begin{code}

 module _
          (pe : Prop-Ext)
          (fe : Fun-Ext)
        where

  private
   Ο• : ainjective-structure β†’ aflabby-structure
   Ο• (s , iassoc , pbn) =
    derived-flabby-structure D s ,
    derived-flabby-associativity s pe fe iassoc pbn

   Ξ³ : aflabby-structure β†’ ainjective-structure
   Ξ³ (t , fassoc) =
    derived-injective-structure D t ,
    derived-injective-associativity t pe fe fassoc ,
    derived-injective-pullback-naturality t pe fe

  ainjective-structure-iff-aflabby-structure
   : ainjective-structure ↔ aflabby-structure
  ainjective-structure-iff-aflabby-structure = (Ο• , Ξ³)

\end{code}

But if D is a set, it follows that they are typally equivalent, which
is the main theorem of this file.

The essence of the proof are the above two round-trip functions
together with the fact that pullback naturality and associativity, for
both injectivity and flabbiness, are property, rather than just data,
when D is a set.

\begin{code}

  Theorem[ainjective-structure-≃-aflabby-structure-for-sets]
   : is-set D
   β†’ ainjective-structure ≃ aflabby-structure
  Theorem[ainjective-structure-≃-aflabby-structure-for-sets] D-is-set
   = qinveq Ο• (Ξ³ , Ξ³Ο• , ϕγ)
   where
    Ξ³Ο• : Ξ³ ∘ Ο• ∼ id
    Ξ³Ο• (s , _ , pbn) =
     to-subtype-=
      (Ξ» s β†’ Γ—-is-prop
              (injective-associativity-is-prop D-is-set fe s)
              (pullback-naturality-is-prop D-is-set fe s))
      (to-subtype-=
        (Ξ» (_∣_ : {X Y : 𝓀 Μ‡} β†’ (X β†’ D) β†’ X β†ͺ Y β†’ Y β†’ D)
               β†’ implicit-Ξ -is-prop fe (Ξ» X β†’
                 implicit-Ξ -is-prop fe (Ξ» Y β†’
                 Π₃-is-prop fe         (Ξ» f 𝕛 x β†’ D-is-set))))
        (∣-round-trip s pe fe pbn)⁻¹)

    ϕγ : Ο• ∘ Ξ³ ∼ id
    ϕγ (t , _) =
     to-subtype-=
      (flabby-associativity-is-prop D-is-set fe)
      (to-subtype-=
        (Ξ» _ β†’ Π₃-is-prop fe (Ξ» _ _ _ β†’ D-is-set))
        (⨆-round-trip t pe fe)⁻¹)

\end{code}

The above establishes the internal fact that, in a 1-topos,
pulback-natural, associative injective structure on D is isomorphic to
associative flabby structure on D.

But also associative flabby structure on D is equivalent to 𝓛-algebra
structure on D, where 𝓛 is the lifting (of partial-map classifier)
wild monad on types, as we record now.


\begin{code}

  open import Lifting.Algebras 𝓀

  private

   Ξ± : aflabby-structure β†’ 𝓛-alg D
   Ξ± ((⨆ , e) , a) =
    (Ξ» {P} (i : is-prop P) f
       β†’ ⨆ (P , i) f) ,
         (Ξ» (d : D) β†’ e (πŸ™ , πŸ™-is-prop) (Ξ» _ β†’ d) ⋆) ,
    (Ξ» P Q i j β†’ a (P , i) (Ξ» p β†’ Q p , j p))

   Ξ² : 𝓛-alg D β†’ aflabby-structure
   Ξ² (⨆ , lawβ‚€ , law₁) =
    ((Ξ» (P , i) β†’ ⨆ i) ,
     (Ξ» (P , i) f p β†’ 𝓛-alg-Lawβ‚€-givesβ‚€' pe fe fe ⨆ lawβ‚€ P i f p)) ,
    (Ξ» (P , i) Q β†’ law₁ P (Ξ» - β†’ Q - holds) i (Ξ» p β†’ holds-is-prop (Q p)))

\end{code}

As above, we only have a logical equivalence for our arbitrary type D.

\begin{code}

  aflabby-structure-↔-𝓛-alg : aflabby-structure ↔ 𝓛-alg D
  aflabby-structure-↔-𝓛-alg = Ξ± , Ξ²

\end{code}

But if D is a set, we again have a typal equivalence.

\begin{code}

  Theorem[aflabby-structure-≃-𝓛-alg-for-sets]
   : is-set D
   β†’ aflabby-structure ≃ 𝓛-alg D
  Theorem[aflabby-structure-≃-𝓛-alg-for-sets] D-is-set
   = qinveq Ξ± (Ξ² , Ξ²Ξ± , Ξ±Ξ²)
   where
    βα : β ∘ α ∼ id
    βα _ = to-subtype-=
            (flabby-associativity-is-prop D-is-set fe)
            (to-subtype-=
              (Ξ» _ β†’ Π₃-is-prop fe (Ξ» _ _ _ β†’ D-is-set))
              refl)

    αβ : α ∘ β ∼ id
    αβ _ = to-subtype-=
            (Ξ» _ β†’ Γ—-is-prop
                   (Ξ -is-prop fe (Ξ» _ β†’ D-is-set))
                   (Ξ β‚…-is-prop fe (Ξ» _ _ _ _ _ β†’ D-is-set)))
            refl

\end{code}

TODO (trivial). Bring homomorphisms into the picture explicitly, where
𝓛-algebras and their homomorphisms are already defined in another
module, and here we define homomorphisms of injective structures as
follows.

\begin{code}

module _
        {𝓀 π“₯ 𝓣 : Universe}
        (E : 𝓣 Μ‡ )
        ((_∣ᴰ_ , _) : injective-structure D 𝓀 π“₯)
        ((_∣ᴱ_ , _) : injective-structure E 𝓀 π“₯)
       where

 is-hom : (D β†’ E) β†’ π“₯ ⁺ βŠ” 𝓀 ⁺ βŠ” 𝓦 βŠ” 𝓣 Μ‡
 is-hom h = {X : 𝓀 Μ‡ } {Y : π“₯ Μ‡ } (f : X β†’ D) (𝕛 : X β†ͺ Y)
          β†’ h ∘ f ∣ᴰ 𝕛 ∼ (h ∘ f) ∣ᴱ 𝕛

\end{code}

TODO (more challenging and more interesting). What can we say when D
is not necessarily a set? Do we have the same theorems?

These questions are particularly interesting because in HoTT/UF, and
hence in ∞-toposes, as illustrated in other files in this development,
there is a richer supply of injective objects than in 1-toposes.