Martin Escardo, 16th August 2023, with more improvements 18th June 2025.

Injectivity of types of mathematical structures, such as pointed
types, โˆž-magmas, magmas, monoids, groups etc.

We give a sufficient condition for types of mathematical structures to
be injective, and we apply it to examples such as the above.

This file improves InjectiveTypes.MathematicalStructuresOriginal at
the cost of perhaps being harder to understand, but with the benefit
of at the same time being more general and allowing shorter proofs in
applications. It relies on the file InjectiveTypes.Sigma, which also
arises as a generalization of the above original file.

\begin{code}

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

import InjectiveTypes.MathematicalStructuresOriginal -- For comparison only.

open import UF.Univalence

\end{code}

We assume univalence (and hence function extensionality, which,
follows from it), but no other HoTT/UF extensions, not even
propositional truncations.

\begin{code}

module InjectiveTypes.MathematicalStructures
        (ua : Univalence)
       where

open import UF.FunExt
open import UF.UA-FunExt

private
 fe : FunExt
 fe = Univalence-gives-FunExt ua

 fe' : Fun-Ext
 fe' {๐“ค} {๐“ฅ} = fe ๐“ค ๐“ฅ

open import InjectiveTypes.Blackboard fe
open import InjectiveTypes.Sigma fe
open import MLTT.Spartan
open import Taboos.Decomposability fe
open import UF.Base
open import UF.Equiv
open import UF.ClassicalLogic
open import UF.PropIndexedPiSigma
open import UF.Retracts
open import UF.Sets
open import UF.Sets-Properties
open import UF.SubtypeClassifier

\end{code}

It is convenient to work with the following definition of (algebraic)
flabbiness of a universe, which uses equivalence of types rather than
equality.

\begin{code}

Flabby : (๐“ค : Universe) โ†’ ๐“ค โบ ฬ‡
Flabby ๐“ค = ฮฃ โจ† ๊ž‰ ((p : ฮฉ ๐“ค) โ†’ (p holds โ†’ ๐“ค ฬ‡ ) โ†’ ๐“ค ฬ‡ )
                , ((p : ฮฉ ๐“ค) (A : p holds โ†’ ๐“ค ฬ‡) (h : p holds) โ†’ โจ† p A โ‰ƒ A h)

\end{code}

In the presence of univalence we can convert to the usual definition,
and we can always convert in the other direction, but in this file we
need the first one only.

\begin{code}

to-aflabby : Flabby ๐“ค โ†’ aflabby (๐“ค ฬ‡ ) ๐“ค
to-aflabby {๐“ค} (โจ† , e) P i A =
 โจ† (P , i) A , (ฮป h โ†’ eqtoid (ua ๐“ค) _ _ (e (P , i) A h))

from-afabbly : aflabby (๐“ค ฬ‡ ) ๐“ค โ†’ Flabby ๐“ค
from-afabbly {๐“ค} aflab =
 aflabby-extension aflab ,
 (ฮป p A h โ†’ idtoeq _ _ (aflabby-extension-property aflab p A h))

\end{code}

We already know that universes are flabby in two ways, using โจ† := ฮ 
and โจ† := ฮฃ, but we give constructions that they are Flabby without
univalence, and hence have better computational behaviour, which will
simplify the applications we have in mind.

If the index type is a proposition, then the projection out of a
ฮ -type is an equivalence.

\begin{code}

ฮ -๐•ก๐•ฃ๐• ๐•› : (p : ฮฉ ๐“ค) {A : p holds โ†’ ๐“ค ฬ‡ } (h : p holds)
      โ†’ ฮ  A โ‰ƒ A h
ฮ -๐•ก๐•ฃ๐• ๐•› p h = ฮ -proj h , ฮ -proj-is-equiv h fe' (holds-is-prop p)

universes-are-Flabby-ฮ  : Flabby ๐“ค
universes-are-Flabby-ฮ  = (ฮป p A โ†’ ฮ  A) ,
                         (ฮป p A โ†’ ฮ -๐•ก๐•ฃ๐• ๐•› p)

universes-are-flabby-ฮ  : aflabby (๐“ค  ฬ‡) ๐“ค
universes-are-flabby-ฮ  = to-aflabby universes-are-Flabby-ฮ 

ฮฃ-๐•š๐•Ÿ : (p : ฮฉ ๐“ค) {A : p holds โ†’ ๐“ค ฬ‡ } (h : p holds)
    โ†’ A h โ‰ƒ ฮฃ A
ฮฃ-๐•š๐•Ÿ p h = ฮฃ-in h , ฮฃ-in-is-equiv h (holds-is-prop p)

universes-are-Flabby-ฮฃ : Flabby ๐“ค
universes-are-Flabby-ฮฃ = (ฮป p A โ†’ ฮฃ A) ,
                         (ฮป p A h โ†’ โ‰ƒ-sym (ฮฃ-๐•š๐•Ÿ p h))

universes-are-flabby-ฮฃ : aflabby (๐“ค  ฬ‡) ๐“ค
universes-are-flabby-ฮฃ = to-aflabby universes-are-Flabby-ฮฃ

\end{code}

In this file we apply the above constructions only for the case of ฮ ,
but we include those for ฮฃ for the sake illustration (and perhaps for
future use).

We now work with an arbitrary notion S of structure on ๐“ค. E.g. for
monoids we will take S X := X โ†’ X โ†’ X, the type of the multiplication
operation.

\begin{code}

module _ (S : ๐“ค ฬ‡ โ†’ ๐“ฅ ฬ‡ ) where

\end{code}

By the results of InjectiveTypes.Sigma, we get that ฮฃ S is aflabby in
two ways, assuming the compatibility condition for the flabbiness
data.

\begin{code}

 module _ (ฯ• : aflabby (๐“ค ฬ‡ ) ๐“ค) where

  aflabbiness-of-type-of-structured-types : compatibility-data S ฯ•
                                          โ†’ aflabby (ฮฃ S) ๐“ค
  aflabbiness-of-type-of-structured-types = ฮฃ-is-aflabby S ฯ•

  ainjectivity-of-type-of-structures : compatibility-data S ฯ•
                                     โ†’ ainjective-type (ฮฃ S) ๐“ค ๐“ค
  ainjectivity-of-type-of-structures = aflabby-types-are-ainjective (ฮฃ S)
                                       โˆ˜ aflabbiness-of-type-of-structured-types

\end{code}

We will apply this to get our desired examples with ฯ• taken to be the
above canonical ฮ -flabby structure on the universe.

Next we want to simplify working with compatibility data (as defined
in the module InjectiveTypes.Sigma), where we avoid transports by
working with the following function treq and suitable choices of T and
T-refl in the examples below. Notice that the definition of treq uses
univalence. The point of T and T-refl below is that they won't use
univalence in our examples of interest, so that they will have a
better computational behaviour than treq.

\begin{code}

 treq : {X Y : ๐“ค ฬ‡ } โ†’ X โ‰ƒ Y โ†’ S X โ†’ S Y
 treq {X} {Y} ๐•— = transport S (eqtoid (ua ๐“ค) X Y ๐•—)

\end{code}

The main additional work in this file on top of InjectiveTypes.Sigma
is to make it easier to work with the compatibility condition for the
purpose of injectivity of types of mathematical structures.

We work with hypothetical T and T-refl with the following types.

\begin{code}

 module _ (T      : {X Y : ๐“ค ฬ‡ } โ†’ X โ‰ƒ Y โ†’ S X โ†’ S Y)
          (T-refl : {X : ๐“ค ฬ‡ } โ†’ T (โ‰ƒ-refl X) โˆผ id)
        where

\end{code}

The point is that any such T can be equivalently expressed as a
transport and hence we may apply the theorems of the imported file
InjectiveTypes.Sigma, but it may be easier to check the compatibility
condition using T rather than transport (see examples below).

\begin{code}

  T-is-treq : {X Y : ๐“ค ฬ‡ } (๐•— : X โ‰ƒ Y)
            โ†’ T ๐•— โˆผ treq ๐•—
  T-is-treq {X} {Y} ๐•— s = JEq (ua ๐“ค) X A I Y ๐•—
   where
    A : (Y : ๐“ค ฬ‡ ) (๐•— : X โ‰ƒ Y) โ†’ ๐“ฅ ฬ‡
    A Y ๐•— = T ๐•— s ๏ผ treq ๐•— s

    I : A X (โ‰ƒ-refl X)
    I = T (โ‰ƒ-refl X) s                                ๏ผโŸจ T-refl s โŸฉ
        s                                             ๏ผโŸจ refl โŸฉ
        transport S refl s                            ๏ผโŸจ II โŸฉ
        transport S (eqtoid (ua ๐“ค) X X (โ‰ƒ-refl X)) s  ๏ผโŸจ refl โŸฉ
        treq (โ‰ƒ-refl X) s                             โˆŽ
      where
       II = (ap (ฮป - โ†’ transport S - s) (eqtoid-refl (ua ๐“ค) X))โปยน

\end{code}

In order to be able to apply the results of InjectiveTypes.Sigma, we
perform the following construction. That file requires compatibility
data of a certain kind, which we reduce to compatibility of another
kind, which will be easier to produce in our sample applications.

\begin{code}

  module compatibility-data-construction (ฯ•@(โจ† , ฮต) : Flabby ๐“ค) where

   derived-ฯ : (p : ฮฉ ๐“ค)
               (A : p holds โ†’ ๐“ค ฬ‡ )
             โ†’ S (โจ† p A) โ†’ ((h : p holds) โ†’ S (A h))
   derived-ฯ p A s h = T (ฮต p A h) s

   compatibility-data-for-derived-ฯ : ๐“ค โบ โŠ” ๐“ฅ ฬ‡
   compatibility-data-for-derived-ฯ = (p : ฮฉ ๐“ค)
                                      (A : p holds โ†’ ๐“ค ฬ‡ )
                                    โ†’ has-section (derived-ฯ p A)

   construction : compatibility-data-for-derived-ฯ
                โ†’ compatibility-data S (to-aflabby ฯ•)
   construction t p A = III
    where

     II : derived-ฯ p A โˆผ ฯ S (to-aflabby ฯ•) p A
     II s =
      derived-ฯ p A s                                     ๏ผโŸจ refl โŸฉ
      (ฮป h โ†’ T (ฮต p A h) s)                               ๏ผโŸจ Iโ‚€ โŸฉ
      (ฮป h โ†’ treq (ฮต p A h) s)                            ๏ผโŸจ refl โŸฉ
      (ฮป h โ†’ transport S (eqtoid (ua ๐“ค) _ _ (ฮต p A h)) s) ๏ผโŸจ refl โŸฉ
      ฯ S (to-aflabby ฯ•) p A s                           โˆŽ
      where
       Iโ‚€ = dfunext fe' (ฮป h โ†’ T-is-treq (ฮต p A h) s)

     III : has-section (ฯ S (to-aflabby ฯ•) p A)
     III = has-section-closed-under-โˆผ (derived-ฯ p A) _ (t p A) (โˆผ-sym II)

\end{code}

This completes the construction, but we record that the section map of
the above construction is literally the same as that of the
hypothesis t.

\begin{code}

     _ = section-map (ฯ S (to-aflabby ฯ•) p A) III  ๏ผโŸจ refl โŸฉ
         section-map (derived-ฯ p A) (t p A)        โˆŽ

\end{code}

What is necessarily different is the proof that this map is a
section. In fact, it is different in the strong sense that the
comparison for equality doesn't even make sense - it wouldn't even
typecheck.

A way to verify this in Agda is to try to supply the following naive
definition.

   construction' : compatibility-data-for-derived-ฯ
                 โ†’ compatibility-data S (to-aflabby ฯ•)
   construction' t = t -- Doesn't type check (of course).

We can sensibly have only that the *section map* of the construction
agrees with the given section map, which is what we have already
observed in the above proof, but record again with full type
information, outside the above proof.

\begin{code}

   construction-fact : (p : ฮฉ ๐“ค)
                       (A : p holds โ†’ ๐“ค ฬ‡)
                       (t : compatibility-data-for-derived-ฯ)
                     โ†’ section-map (ฯ S (to-aflabby ฯ•) p A) (construction t p A)
                     ๏ผ section-map (derived-ฯ p A)         (t p A)
   construction-fact p A t = refl

\end{code}

This fact about the construction will be rather useful in practice,
for the applications we have in mind.

We can specialize this to the ฮ  and ฮฃ flabbiness structures discussed
above, to get the following.

\begin{code}

  module _ where

   open compatibility-data-construction universes-are-Flabby-ฮ 

   ฯฮ  : (p : ฮฉ ๐“ค)
        (A : p holds โ†’ ๐“ค ฬ‡ )
      โ†’ S (ฮ  A) โ†’ ((h : p holds) โ†’ S (A h))
   ฯฮ  = derived-ฯ

   compatibility-data-ฮ  : ๐“ค โบ โŠ” ๐“ฅ ฬ‡
   compatibility-data-ฮ  = (p : ฮฉ ๐“ค)
                          (A : p holds โ†’ ๐“ค ฬ‡ )
                        โ†’ has-section (ฯฮ  p A)

   ฮ -construction : compatibility-data-ฮ 
                  โ†’ compatibility-data S universes-are-flabby-ฮ 
   ฮ -construction = construction

\end{code}

We use the following definitional equality a number of times (and we
try to record this explicitly when we do so).

\begin{code}

   _ : ฯฮ  ๏ผ ฮป p A s h โ†’ T (ฮ -๐•ก๐•ฃ๐• ๐•› p h) s
   _ = refl

\end{code}

For our examples below, we only need the above functions ฯฮ ,
compatibility-data-ฮ  and ฮ -construction, but we take the opportunity
to remark that we also have the following, with ฮ  replaced by ฮฃ (for
which we don't have any application so far).

\begin{code}

  module _ where

   open compatibility-data-construction universes-are-Flabby-ฮฃ

   ฯฮฃ : (p : ฮฉ ๐“ค)
        (A : p holds โ†’ ๐“ค ฬ‡ )
      โ†’ S (ฮฃ A) โ†’ ((h : p holds) โ†’ S (A h))
   ฯฮฃ = derived-ฯ

   compatibility-data-ฮฃ : ๐“ค โบ โŠ” ๐“ฅ ฬ‡
   compatibility-data-ฮฃ = (p : ฮฉ ๐“ค)
                          (A : p holds โ†’ ๐“ค ฬ‡ )
                        โ†’ has-section (ฯฮฃ p A)

   ฮฃ-construction : compatibility-data-ฮฃ
                  โ†’ compatibility-data S universes-are-flabby-ฮฃ
   ฮฃ-construction = construction

\end{code}

Example. The type of pointed types is algebraically injective. We use
the ฮ -flabbiness of the universe.

\begin{code}

Pointed-type : (๐“ค : Universe) โ†’ ๐“ค โบ ฬ‡
Pointed-type ๐“ค = ฮฃ X ๊ž‰ ๐“ค ฬ‡ , X

Pointed : ๐“ค ฬ‡ โ†’ ๐“ค ฬ‡
Pointed X = X

Pointed-ฮ -data : compatibility-data (Pointed {๐“ค}) universes-are-flabby-ฮ 
Pointed-ฮ -data {๐“ค} = ฮ -construction Pointed T T-refl c
 where
  S = Pointed

  T : {X Y : ๐“ค ฬ‡ } โ†’ (X โ‰ƒ Y) โ†’ X โ†’ Y
  T = โŒœ_โŒ

  T-refl : {X : ๐“ค ฬ‡ } โ†’ T (โ‰ƒ-refl X) โˆผ id
  T-refl x = refl

  _ : (p : ฮฉ ๐“ค) (A : p holds โ†’ ๐“ค ฬ‡) โ†’ ฯฮ  S T T-refl p A ๏ผ ๐‘–๐‘‘ (S (ฮ  A))
  _ = ฮป p A โ†’ refl

  c : compatibility-data-ฮ  S T T-refl
  c p A = equivs-have-sections id (id-is-equiv (ฮ  A))

\end{code}

Hence we conclude that the type of pointed types is ainjective.

\begin{code}

ainjectivity-of-type-of-pointed-types : ainjective-type (Pointed-type ๐“ค) ๐“ค ๐“ค
ainjectivity-of-type-of-pointed-types =
 ainjectivity-of-type-of-structures
  Pointed
  universes-are-flabby-ฮ 
  Pointed-ฮ -data

\end{code}

Example. The type of โˆž-magmas is algebraically injective. The proof is
an entirely routine application of the above general theorem after we
guess what T should be.

\begin{code}

โˆž-Magma : (๐“ค : Universe) โ†’ ๐“ค โบ ฬ‡
โˆž-Magma ๐“ค = ฮฃ X ๊ž‰ ๐“ค ฬ‡ , (X โ†’ X โ†’ X)

โˆž-magma-structure : ๐“ค ฬ‡ โ†’ ๐“ค ฬ‡
โˆž-magma-structure = ฮป X โ†’ X โ†’ X โ†’ X

โˆž-Magma-structure-ฮ -data : compatibility-data
                            (โˆž-magma-structure {๐“ค})
                            universes-are-flabby-ฮ 
โˆž-Magma-structure-ฮ -data {๐“ค} =
 ฮ -construction S T T-refl ฯฮ -has-section
 where
  S = โˆž-magma-structure

  T : {X Y : ๐“ค ฬ‡ } โ†’ (X โ‰ƒ Y) โ†’ S X โ†’ S Y
  T ๐•— _ยท_ = ฮป y y' โ†’ โŒœ ๐•— โŒ (โŒœ ๐•— โŒโปยน y ยท โŒœ ๐•— โŒโปยน y')

  T-refl : {X : ๐“ค ฬ‡ } โ†’ T (โ‰ƒ-refl X) โˆผ id
  T-refl _ยท_ = refl

  module _ (p : ฮฉ ๐“ค)
           (A : p holds โ†’ ๐“ค ฬ‡ )
         where

   ฯ€ : (h : p holds) โ†’ ฮ  A โ‰ƒ A h
   ฯ€ = ฮ -๐•ก๐•ฃ๐• ๐•› p

   r : S (ฮ  A) โ†’ ((h : p holds) โ†’ S (A h))
   r _ยท_ h a b = โŒœ ฯ€ h โŒ (โŒœ ฯ€ h โŒโปยน a ยท โŒœ ฯ€ h โŒโปยน b)

   _ : r ๏ผ ฯฮ  S T T-refl p A
   _ = refl -- Which is crucial for the proof below to work.

   ฯƒ : ((h : p holds) โ†’ S (A h)) โ†’ S (ฮ  A)
   ฯƒ g ฮฑ ฮฒ h = g h (โŒœ ฯ€ h โŒ ฮฑ) (โŒœ ฯ€ h โŒ ฮฒ)

   rฯƒ : r โˆ˜ ฯƒ โˆผ id
   rฯƒ g =
    r (ฯƒ g)                                                         ๏ผโŸจ refl โŸฉ
    (ฮป h a b โ†’ g h (โŒœ ฯ€ h โŒ (โŒœ ฯ€ h โŒโปยน a)) (โŒœ ฯ€ h โŒ (โŒœ ฯ€ h โŒโปยน b))) ๏ผโŸจ II โŸฉ
    (ฮป h a b โ†’ g h a b)                                             ๏ผโŸจ refl โŸฉ
    g                                                               โˆŽ
     where
      II = dfunext fe' (ฮป h โ†’
           dfunext fe' (ฮป a โ†’
           dfunext fe' (ฮป b โ†’
            apโ‚‚ (g h)
             (inverses-are-sections' (ฯ€ h) a)
             (inverses-are-sections' (ฯ€ h) b))))

   ฯฮ -has-section : has-section (ฯฮ  S T T-refl p A)
   ฯฮ -has-section = ฯƒ , rฯƒ

ainjectivity-of-โˆž-Magma : ainjective-type (โˆž-Magma ๐“ค) ๐“ค ๐“ค
ainjectivity-of-โˆž-Magma =
 ainjectivity-of-type-of-structures
  โˆž-magma-structure
  universes-are-flabby-ฮ 
  โˆž-Magma-structure-ฮ -data

\end{code}

A corollary is that the type โˆž-Magma ๐“ค doesn't have any non-trivial
decidable property unless weak excluded middle holds.

\begin{code}

decomposition-of-โˆž-Magma-gives-WEM : decomposition (โˆž-Magma ๐“ค) โ†’ typal-WEM ๐“ค
decomposition-of-โˆž-Magma-gives-WEM {๐“ค} =
 decomposition-of-ainjective-type-gives-WEM
  (univalence-gives-propext (ua ๐“ค))
  (โˆž-Magma ๐“ค)
  ainjectivity-of-โˆž-Magma

\end{code}

The same is true for the type of pointed types, of course, and for any
injective type.

Example. The type of pointed โˆž-magmas is injective.

\begin{code}

open import UF.SIP-Examples
open monoid

โˆž-Magmaโˆ™ : (๐“ค : Universe) โ†’ ๐“ค โบ ฬ‡
โˆž-Magmaโˆ™ ๐“ค = ฮฃ X ๊ž‰ ๐“ค ฬ‡ , (X โ†’ X โ†’ X) ร— X

โˆž-Magmaโˆ™-structure : ๐“ค ฬ‡ โ†’ ๐“ค ฬ‡
โˆž-Magmaโˆ™-structure = monoid-structure

โˆž-Magmaโˆ™-structure-ฮ -data : compatibility-data
                             (โˆž-Magmaโˆ™-structure {๐“ค})
                             universes-are-flabby-ฮ 
โˆž-Magmaโˆ™-structure-ฮ -data =
 compatibility-data-ร—
  universes-are-flabby-ฮ 
  โˆž-Magma-structure-ฮ -data
  Pointed-ฮ -data

ainjectivity-of-โˆž-Magmaโˆ™ : ainjective-type (โˆž-Magmaโˆ™ ๐“ค) ๐“ค ๐“ค
ainjectivity-of-โˆž-Magmaโˆ™ =
 ainjectivity-of-type-of-structures
  โˆž-Magmaโˆ™-structure
  universes-are-flabby-ฮ 
  โˆž-Magmaโˆ™-structure-ฮ -data

\end{code}

Example. The type of monoids is injective. We just have to check that
the monoid axioms are closed under ฮ .

\begin{code}

Monoid-ฮ -data : compatibility-data {๐“ค โบ}
                 (ฮป X โ†’ ฮฃ s ๊ž‰ monoid-structure X , monoid-axioms X s)
                 universes-are-flabby-ฮ 
Monoid-ฮ -data {๐“ค} =
 compatibility-data-with-axioms
  universes-are-flabby-ฮ 
  monoid-structure
  โˆž-Magmaโˆ™-structure-ฮ -data
  monoid-axioms
  (monoid-axioms-is-prop fe')
  axioms-ฮ -data
 where
  ฯƒ : (p : ฮฉ ๐“ค) (A : p holds โ†’ ๐“ค ฬ‡ )
    โ†’ ((h : p holds) โ†’ monoid-structure (A h)) โ†’ monoid-structure (ฮ  A)
  ฯƒ p A = section-map
           (ฯ monoid-structure universes-are-flabby-ฮ  p A)
           (โˆž-Magmaโˆ™-structure-ฮ -data p A)

  axioms-ฮ -data
   : (p : ฮฉ ๐“ค)
     (A : p holds โ†’ ๐“ค ฬ‡ )
     (ฮฑ : (h : p holds) โ†’ monoid-structure (A h))
     (F : (h : p holds) โ†’ monoid-axioms (A h) (ฮฑ h))
   โ†’ monoid-axioms (ฮ  A) (ฯƒ p A ฮฑ)
  axioms-ฮ -data p A ฮฑ F = I , II , III , IV
   where
    _*_ : {h : p holds} โ†’ A h โ†’ A h โ†’ A h
    _*_ {h} = prโ‚ (ฮฑ h)

    _ยท_ : ฮ  A โ†’ ฮ  A โ†’ ฮ  A
    f ยท g = ฮป h โ†’ f h * g h

    e : ฮ  A
    e h = prโ‚‚ (ฮฑ h)

    _ : ฯƒ p A ฮฑ ๏ผ (_ยท_ , e)
    _ = refl -- Which is crucial for the proof below to work.

    I : is-set (ฮ  A)
    I = ฮ -is-set fe' (ฮป h โ†’
         case F h of
          ฮป (Ah-is-set , ln , rn , assoc) โ†’ Ah-is-set)

    II : left-neutral e _ยท_
    II f = dfunext fe' (ฮป h โ†’
            case F h of
             ฮป (Ah-is-set , ln , rn , assoc) โ†’ ln (f h))

    III : right-neutral e _ยท_
    III g = dfunext fe' (ฮป h โ†’
             case F h of
              ฮป (Ah-is-set , ln , rn , assoc) โ†’ rn (g h))

    IV : associative _ยท_
    IV f g k = dfunext fe' (ฮป h โ†’
                case F h of
                 ฮป (Ah-is-set , ln , rn , assoc) โ†’ assoc (f h) (g h) (k h))

ainjectivity-of-Monoid : ainjective-type (Monoid {๐“ค}) ๐“ค ๐“ค
ainjectivity-of-Monoid {๐“ค} =
 ainjectivity-of-type-of-structures
  (ฮป X โ†’ ฮฃ s ๊ž‰ monoid-structure X , monoid-axioms X s)
  universes-are-flabby-ฮ 
  Monoid-ฮ -data

\end{code}

It is easy to add further axioms to monoids to get groups, and then
show that the type of groups is injective using the above
technique. This is just as routine as the example of monoids. All one
needs to do is to show that the group axioms are closed under
prop-indexed products.

TODO. Maybe implement this.

NB. The type Ordinal ๐“ค of well-ordered sets in ๐“ค is also injective,
but for different reasons, two of them given in two different modules.

Added 20th June 2025. The type of all families in a universe is
injective.

\begin{code}

Fam : (๐“ค : Universe) โ†’ ๐“ค โบ ฬ‡
Fam ๐“ค = ฮฃ X ๊ž‰ ๐“ค ฬ‡ , (X โ†’ ๐“ค ฬ‡)

Fam-structure : ๐“ค ฬ‡ โ†’ ๐“ค โบ ฬ‡
Fam-structure {๐“ค} X = X โ†’ ๐“ค ฬ‡

open import UF.EquivalenceExamples
open import UF.Subsingletons

Fam-ฮ -data : compatibility-data (Fam-structure {๐“ค}) universes-are-flabby-ฮ 
Fam-ฮ -data {๐“ค} = ฮ -construction Fam-structure T T-refl c
 where
  S = Fam-structure

  T : {X Y : ๐“ค ฬ‡} โ†’ X โ‰ƒ Y โ†’ (X โ†’ ๐“ฃ ฬ‡ ) โ†’ (Y โ†’ ๐“ฃ ฬ‡ )
  T ๐•— R = ฮป y โ†’ R (โŒœ ๐•— โŒโปยน y)

  T-refl : {X : ๐“ค ฬ‡} โ†’ T (โ‰ƒ-refl X) โˆผ id
  T-refl R = refl

  module _ (p : ฮฉ ๐“ค) (A : p holds โ†’ ๐“ค ฬ‡) where

   r :  S (ฮ  A) โ†’ ((h : p holds) โ†’ S (A h))
   r s h a = s (โŒœ ฮ -๐•ก๐•ฃ๐• ๐•› p h โŒโปยน a)

   _ : ฯฮ  S T T-refl p A ๏ผ r
   _ = refl

   ฯƒ : ((h : p holds) โ†’ S (A h)) โ†’ S (ฮ  A)
   ฯƒ g f = (h : p holds) โ†’ g h (f h)

   rฯƒ : r โˆ˜ ฯƒ โˆผ id
   rฯƒ g = dfunext fe' (ฮป h โ†’ dfunext fe' (II h))
    where
     module _ (h : p holds) (a : A h) where

      ฯ€ : ฮ  A โ‰ƒ A h
      ฯ€ = ฮ -๐•ก๐•ฃ๐• ๐•› p h

      I = ((h' : p holds) โ†’ g h' (โŒœ ฯ€ โŒโปยน a h')) โ‰ƒโŸจ Iโ‚€ โŸฉ
          (p holds โ†’ g h (โŒœ ฯ€ โŒโปยน a h))          โ‰ƒโŸจ Iโ‚ โŸฉ
          (๐Ÿ™ โ†’ g h (โŒœ ฯ€ โŒโปยน a h))                โ‰ƒโŸจ Iโ‚‚ โŸฉ
          g h (โŒœ ฯ€ โŒโปยน a h)                      โ– 
        where
         Iโ‚€ = ฮ -cong fe' fe'
               (ฮป h' โ†’ transport (ฮป - โ†’ g - (โŒœ ฯ€ โŒโปยน a -))
                                 (holds-is-prop p h' h) ,
                       transports-are-equivs (holds-is-prop p h' h))
         Iโ‚ = ฮ -change-of-variable-โ‰ƒ {๐“ค} {๐“ค} fe
               (ฮป _ โ†’ g h (โŒœ ฯ€ โŒโปยน a h))
               (logically-equivalent-props-are-equivalent
                 (holds-is-prop p) ๐Ÿ™-is-prop unique-to-๐Ÿ™ (ฮป _ โ†’ h))
         Iโ‚‚ = โ‰ƒ-sym (๐Ÿ™โ†’ fe')

      II = r (ฯƒ g) h a                            ๏ผโŸจ refl โŸฉ
           ฯƒ g (โŒœ ฯ€ โŒโปยน a)                        ๏ผโŸจ refl โŸฉ
           ((h' : p holds) โ†’ g h' (โŒœ ฯ€ โŒโปยน a h')) ๏ผโŸจ IIโ‚€ โŸฉ
           g h (โŒœ ฯ€ โŒโปยน a h)                      ๏ผโŸจ refl โŸฉ
           g h (โŒœ ฯ€ โŒ (โŒœ ฯ€ โŒโปยน a))                ๏ผโŸจ IIโ‚ โŸฉ
           g h a                                  โˆŽ
            where
             IIโ‚€  = eqtoid (ua ๐“ค) _ _ I
             IIโ‚ = ap (g h) (inverses-are-sections' ฯ€ a)

  c :  compatibility-data-ฮ  Fam-structure T T-refl
  c p A = ฯƒ p A , rฯƒ p A

ainjectivity-of-Fam : ainjective-type (Fam ๐“ค) ๐“ค ๐“ค
ainjectivity-of-Fam =
 ainjectivity-of-type-of-structures
  Fam-structure
  universes-are-flabby-ฮ 
  Fam-ฮ -data

\end{code}

A corollary is that the type of all functions in a universe is injective.

\begin{code}

open import UF.Classifiers

ainjectivity-of-type-of-all-functions
 : ainjective-type (ฮฃ X ๊ž‰ ๐“ค ฬ‡ , ฮฃ Y ๊ž‰ ๐“ค ฬ‡ , (X โ†’ Y)) ๐“ค ๐“ค
ainjectivity-of-type-of-all-functions {๐“ค}
 = transport
    (ฮป - โ†’ ainjective-type - ๐“ค ๐“ค)
    (eqtoid (ua (๐“ค โบ)) _ _ (โ‰ƒ-sym I))
    ainjectivity-of-Fam
 where
  open classifier-single-universe ๐“ค

  I = (ฮฃ X ๊ž‰ ๐“ค ฬ‡ , ฮฃ Y ๊ž‰ ๐“ค ฬ‡ , (X โ†’ Y)) โ‰ƒโŸจ ฮฃ-flip โŸฉ
      (ฮฃ Y ๊ž‰ ๐“ค ฬ‡ , ฮฃ X ๊ž‰ ๐“ค ฬ‡ , (X โ†’ Y)) โ‰ƒโŸจ ฮฃ-cong (classification (ua ๐“ค) fe') โŸฉ
      (ฮฃ Y ๊ž‰ ๐“ค ฬ‡ , (Y โ†’ ๐“ค ฬ‡))           โ‰ƒโŸจ โ‰ƒ-refl _ โŸฉ
      Fam ๐“ค                           โ– 

\end{code}

The type of all type-valued relations, or multigraphs, in a universe
is injective. The proof is the binary version of the above unary proof.

\begin{code}

Graph : (๐“ค : Universe) โ†’ ๐“ค โบ ฬ‡
Graph ๐“ค = ฮฃ X ๊ž‰ ๐“ค ฬ‡ , (X โ†’ X โ†’ ๐“ค ฬ‡)

graph-structure : ๐“ค ฬ‡ โ†’ ๐“ค โบ ฬ‡
graph-structure {๐“ค} X = X โ†’ X โ†’ ๐“ค ฬ‡

open import UF.EquivalenceExamples
open import UF.Subsingletons

Graph-ฮ -data : compatibility-data (graph-structure {๐“ค}) universes-are-flabby-ฮ 
Graph-ฮ -data {๐“ค} =
 ฮ -construction graph-structure T T-refl c
 where
  S = graph-structure

  T : {X Y : ๐“ค ฬ‡} โ†’ X โ‰ƒ Y โ†’ (X โ†’ X โ†’ ๐“ฃ ฬ‡ ) โ†’ (Y โ†’ Y โ†’ ๐“ฃ ฬ‡ )
  T ๐•— R y y' = R (โŒœ ๐•— โŒโปยน y) (โŒœ ๐•— โŒโปยน y')

  T-refl : {X : ๐“ค ฬ‡} โ†’ T (โ‰ƒ-refl X) โˆผ id
  T-refl R = refl

  module _ (p : ฮฉ ๐“ค) (A : p holds โ†’ ๐“ค ฬ‡) where

   r :  S (ฮ  A) โ†’ ((h : p holds) โ†’ S (A h))
   r s h a a' = s (โŒœ ฮ -๐•ก๐•ฃ๐• ๐•› p h โŒโปยน a) (โŒœ ฮ -๐•ก๐•ฃ๐• ๐•› p h โŒโปยน a')

   _ : r ๏ผ ฯฮ  S T T-refl p A
   _ = refl

   ฯƒ : ((h : p holds) โ†’ S (A h)) โ†’ S (ฮ  A)
   ฯƒ g f f' = (h : p holds) โ†’ g h (f h) (f' h)

   rฯƒ : r โˆ˜ ฯƒ โˆผ id
   rฯƒ g = dfunext fe' (ฮป h โ†’
          dfunext fe' (ฮป a โ†’
          dfunext fe' (ฮป a' โ†’ II h a a')))
    where
     module _ (h : p holds) (a a' : A h) where

      ฯ€ : ฮ  A โ‰ƒ A h
      ฯ€ = ฮ -๐•ก๐•ฃ๐• ๐•› p h

      I = ((h' : p holds) โ†’ g h' (โŒœ ฯ€ โŒโปยน a h') (โŒœ ฯ€ โŒโปยน a' h')) โ‰ƒโŸจ Iโ‚€ โŸฉ
          (p holds โ†’ g h (โŒœ ฯ€ โŒโปยน a h) (โŒœ ฯ€ โŒโปยน a' h))           โ‰ƒโŸจ Iโ‚ โŸฉ
          (๐Ÿ™ โ†’ g h (โŒœ ฯ€ โŒโปยน a h) (โŒœ ฯ€ โŒโปยน a' h))                 โ‰ƒโŸจ Iโ‚‚ โŸฉ
          g h (โŒœ ฯ€ โŒโปยน a h) (โŒœ ฯ€ โŒโปยน a' h)                       โ– 
        where
         Iโ‚€ = ฮ -cong fe' fe'
               (ฮป h' โ†’ transport (ฮป - โ†’ g - (โŒœ ฯ€ โŒโปยน a -) (โŒœ ฯ€ โŒโปยน a' -))
                                 (holds-is-prop p h' h) ,
                       transports-are-equivs (holds-is-prop p h' h))
         Iโ‚ = ฮ -change-of-variable-โ‰ƒ {๐“ค} {๐“ค} fe
               (ฮป _ โ†’ g h (โŒœ ฯ€ โŒโปยน a h) (โŒœ ฯ€ โŒโปยน a' h))
               (logically-equivalent-props-are-equivalent
                 (holds-is-prop p) ๐Ÿ™-is-prop unique-to-๐Ÿ™ (ฮป _ โ†’ h))
         Iโ‚‚ = โ‰ƒ-sym (๐Ÿ™โ†’ fe')

      II = r (ฯƒ g) h a a'                                         ๏ผโŸจ refl โŸฉ
           ฯƒ g (โŒœ ฯ€ โŒโปยน a) (โŒœ ฯ€ โŒโปยน a')                           ๏ผโŸจ refl โŸฉ
           ((h' : p holds) โ†’ g h' (โŒœ ฯ€ โŒโปยน a h') (โŒœ ฯ€ โŒโปยน a' h')) ๏ผโŸจ IIโ‚€ โŸฉ
           g h (โŒœ ฯ€ โŒโปยน a h) (โŒœ ฯ€ โŒโปยน a' h)                       ๏ผโŸจ refl โŸฉ
           g h (โŒœ ฯ€ โŒ (โŒœ ฯ€ โŒโปยน a)) (โŒœ ฯ€ โŒ (โŒœ ฯ€ โŒโปยน a'))           ๏ผโŸจ IIโ‚ โŸฉ
           g h a a'                                               โˆŽ
            where
             IIโ‚€  = eqtoid (ua ๐“ค) _ _ I
             IIโ‚ = apโ‚‚ (g h)
                       (inverses-are-sections' ฯ€ a)
                       (inverses-are-sections' ฯ€ a')

  c :  compatibility-data-ฮ  graph-structure T T-refl
  c p A = ฯƒ p A , rฯƒ p A

ainjectivity-of-Graph : ainjective-type (Graph ๐“ค) ๐“ค ๐“ค
ainjectivity-of-Graph =
 ainjectivity-of-type-of-structures
  graph-structure
  universes-are-flabby-ฮ 
  Graph-ฮ -data

\end{code}

As a consequence, we get the injectivity of the type of posets.

\begin{code}

poset-axioms : (X : ๐“ค ฬ‡ ) โ†’ graph-structure X โ†’ ๐“ค ฬ‡
poset-axioms X _โ‰ค_ = is-set X
                   ร— ((x y : X) โ†’ is-prop (x โ‰ค y))
                   ร— reflexive     _โ‰ค_
                   ร— transitive    _โ‰ค_
                   ร— antisymmetric _โ‰ค_

Poset : (๐“ค : Universe) โ†’ ๐“ค โบ ฬ‡
Poset ๐“ค = ฮฃ X ๊ž‰ ๐“ค ฬ‡ , ฮฃ s ๊ž‰ graph-structure X , poset-axioms X s

open import UF.Subsingletons-FunExt

poset-axioms-is-prop : (X : ๐“ค ฬ‡ ) (s : graph-structure X)
                     โ†’ is-prop (poset-axioms X s)
poset-axioms-is-prop X _โ‰ค_ = prop-criterion I
 where
  I : poset-axioms X _โ‰ค_ โ†’ is-prop (poset-axioms X _โ‰ค_)
  I (s , pv , r , t , a) =
   ร—โ‚…-is-prop
    (being-set-is-prop fe')
    (ฮ โ‚‚-is-prop fe' (ฮป x y โ†’ being-prop-is-prop fe'))
    (ฮ -is-prop fe' (ฮป x โ†’ pv x x))
    (ฮ โ‚…-is-prop fe' (ฮป x _ z _ _ โ†’ pv x z))
    (ฮ โ‚„-is-prop fe' (ฮป _ _ _ _ โ†’ s))

Poset-ฮ -data : compatibility-data {๐“ค โบ}
                 (ฮป X โ†’ ฮฃ s ๊ž‰ graph-structure X , poset-axioms X s)
                 universes-are-flabby-ฮ 
Poset-ฮ -data {๐“ค} =
 compatibility-data-with-axioms
  universes-are-flabby-ฮ 
  graph-structure
  Graph-ฮ -data
  poset-axioms
  poset-axioms-is-prop
  axioms-ฮ -data
 where
  ฯƒ : (p : ฮฉ ๐“ค) (A : p holds โ†’ ๐“ค ฬ‡ )
    โ†’ ((h : p holds) โ†’ graph-structure (A h)) โ†’ graph-structure (ฮ  A)
  ฯƒ p A = section-map
           (ฯ graph-structure universes-are-flabby-ฮ  p A)
           (Graph-ฮ -data p A)

  axioms-ฮ -data
   : (p : ฮฉ ๐“ค)
     (A : p holds โ†’ ๐“ค ฬ‡ )
     (ฮฑ : (h : p holds) โ†’ graph-structure (A h))
     (F : (h : p holds) โ†’ poset-axioms (A h) (ฮฑ h))
   โ†’ poset-axioms (ฮ  A) (ฯƒ p A ฮฑ)
  axioms-ฮ -data p A ฮฑ F = I , II , III , IV , V
   where
    _โŠ‘_ : {h : p holds} โ†’ A h โ†’ A h โ†’ ๐“ค ฬ‡
    _โŠ‘_ {h} = ฮฑ h

    _โ‰ค_ : ฮ  A โ†’ ฮ  A โ†’ ๐“ค ฬ‡
    f โ‰ค g = (h : p holds) โ†’ f h โŠ‘ g h

    _ : ฯƒ p A ฮฑ ๏ผ _โ‰ค_
    _ = refl -- Which is crucial for the proof below to work.

    I : is-set (ฮ  A)
    I = ฮ -is-set fe' (ฮป h โ†’
         case F h of
          ฮป (s , pv , r , t , a) โ†’ s)

    II : (f g : ฮ  A) โ†’ is-prop (f โ‰ค g)
    II f g = ฮ -is-prop fe' (ฮป h โ†’
              case F h of
               ฮป (s , pv , r , t , a) โ†’ pv (f h) (g h))

    III : reflexive _โ‰ค_
    III f h =
     case F h of
      ฮป (s , pv , r , t , a) โ†’ r (f h)

    IV : transitive _โ‰ค_
    IV fโ‚€ fโ‚ fโ‚‚ l m h =
     case F h of
      ฮป (s , pv , r , t , a) โ†’ t (fโ‚€ h) (fโ‚ h) (fโ‚‚ h) (l h) (m h)

    V : antisymmetric _โ‰ค_
    V fโ‚€ fโ‚ l m = dfunext fe' (ฮป h โ†’
                   case F h of
                    ฮป (s , pv , r , t , a) โ†’ a (fโ‚€ h) (fโ‚ h) (l h) (m h))

ainjectivity-of-Poset : ainjective-type (Poset ๐“ค) ๐“ค ๐“ค
ainjectivity-of-Poset {๐“ค} =
 ainjectivity-of-type-of-structures
  (ฮป X โ†’ ฮฃ s ๊ž‰ graph-structure X , poset-axioms X s)
  universes-are-flabby-ฮ 
  Poset-ฮ -data

\end{code}

Notice that, just as in the case for monoids, the proof amounts to
showing that posets are closed under prop-indexed products. Using the
same idea, it is straightforward to show that the types of dcpos,
continuous dcpos, suplattices, frames etc. are all injective. (Notice
that this is different from e.g. saying that the underlying type of a
dcpos is injective, which is also true and is proved in another
module.)

TODO. Maybe implement (some of) these examples.

TODO. More techniques are needed to show that the type of 1-categories
would be injective. A category can be seen as a graph equipped with
operations (identity and composition) satisfying properties (identity
laws, associativity, univalence).