open import 1Lab.Equiv.Fibrewise
open import 1Lab.HLevel.Retracts
open import 1Lab.HLevel.Universe
open import 1Lab.Type.Sigma
open import 1Lab.Univalence
open import 1Lab.HLevel
open import 1Lab.Equiv
open import 1Lab.Path
open import 1Lab.Type

module 1Lab.Equiv.Embedding where

private variable
β ββ : Level
A B : Type β
w x : A


# Embeddingsπ

One of the most important observations leading to the development of categorical set theory is that injective maps into a set $S$ correspond to maps from $S$ into a universe of propositions, normally denoted $\Omega$. Classically, this object is $\Omega = \{ 0 , 1 \}$, but there are other settings in which this idea makes sense (elementary topoi) where the subobject classifier is not a coproduct $1 \coprod 1$.

To develop this correspondence, we note that, if a map is injective and its codomain is a set, then all the fibres $f^*(x)$ of $f$ are propositions.

injective : (A β B) β Type _
injective f = β {x y} β f x β‘ f y β x β‘ y

injectiveβis-embedding
: is-set B β (f : A β B) β injective f
β β x β is-prop (fibre f x)
injectiveβis-embedding bset f inj x (f*x , p) (f*xβ² , q) =
Ξ£-prop-path (Ξ» x β bset _ _) (inj (p β sym q))


In fact, this condition is not only necessary, it is also sufficient. Thus, we conclude that, for maps between sets, these notions are equivalent, and we could take either as the definition of βsubset inclusionβ.

has-prop-fibresβinjective
: (f : A β B) β (β x β is-prop (fibre f x))
β injective f
has-prop-fibresβinjective _ prop p = ap fst (prop _ (_ , p) (_ , refl))

between-sets-injectiveβhas-prop-fibres
: is-set A β is-set B β (f : A β B)
β injective f β (β x β is-prop (fibre f x))
between-sets-injectiveβhas-prop-fibres aset bset f =
prop-ext (Ξ» p q i x β aset _ _ (p x) (q x) i)
(Ξ -is-hlevel 1 Ξ» _ β is-prop-is-prop)
(injectiveβis-embedding bset f)
(has-prop-fibresβinjective f)


Since we want βis a subtype inclusionβ to be a property β that is, we really want to not care about how a function is a subtype inclusion, only that it is, we define embeddings as those functions which have propositional fibres:

is-embedding : (A β B) β Type _
is-embedding f = β x β is-prop (fibre f x)

_βͺ_ : Type β β Type ββ β Type _
A βͺ B = Ξ£[ f β (A β B) ] is-embedding f


Univalence β specifically, the existence of classifying objects for maps with $P$-fibres β tells us that the embeddings into $B$ correspond to the families of propositional types over $B$.

subtype-classifier
: β {β} {B : Type β}
β (Ξ£[ A β Type β ] (A βͺ B)) β (B β Ξ£[ T β Type β ] (is-prop T))
subtype-classifier {β} = Map-classifier {β = β} is-prop
module subtype-classifier {β} {B : Type β} = Equiv (subtype-classifier {B = B})


A canonical source of embedding, then, are the first projections from total spaces of propositional families. This is because, as Fibre-equiv tells us, the fibre of $\pi_1$ over $x$ is equivalent to βthe space of possible second coordinatesβ, i.e., $B(x)$. Since $B(x)$ was assumed to be a prop., then so are the fibres of fst.

Subset-proj-embedding
: β {B : A β Type β} β (β x β is-prop (B x))
β is-embedding {A = Ξ£ A B} fst
Subset-proj-embedding {B = B} Bprop x = is-hlevelβ 1 (Fibre-equiv B x) (Bprop _)

embeddingβmonic
: β {β ββ² ββ²β²} {A : Type β} {B : Type ββ²} {f : A β B}
β is-embedding f
β β {C : Type ββ²β²} (g h : C β A) β f β g β‘ f β h β g β‘ h
embeddingβmonic {f = f} emb g h p =
funext Ξ» x β ap fst (emb _ (g x , refl) (h x , happly (sym p) x))

is-equivβis-embedding
: β {β β'} {A : Type β} {B : Type β'} {f : A β B}
β is-equiv f
β is-embedding f
is-equivβis-embedding eqv x = is-contrβis-prop (eqv .is-eqv x)

monicβis-embedding
: β {β ββ² ββ²β²} {A : Type β} {B : Type ββ²} {f : A β B}
β is-set B
β (β {C : Set ββ²β²} (g h : β£ C β£ β A) β f β g β‘ f β h β g β‘ h)
β is-embedding f
monicβis-embedding {f = f} bset monic =
injectiveβis-embedding bset _ Ξ» {x} {y} p β
happly (monic {C = el (Lift _ β€) (Ξ» _ _ _ _ i j β lift tt)} (Ξ» _ β x) (Ξ» _ β y) (funext (Ξ» _ β p))) _


# As ff morphismsπ

A fully faithful functor is a functor whose action on morphisms is an isomorphism everywhere. By the βtypes are higher groupoidsβ analogy, functors are functions, so weβre left to consider: what is a fully faithful function? The answer turns out to be precisely βan embeddingβ, as long as we interpret βfully faithfulβ to mean βaction on morphisms is an equivalenceβ everywhere.

module _ {β ββ²} {A : Type β} {B : Type ββ²} {f : A β B} where
embedding-lemma : (β x β is-contr (fibre f (f x))) β is-embedding f
embedding-lemma cffx y (x , p) q =
is-contrβis-prop (subst is-contr (ap (fibre f) p) (cffx x)) (x , p) q

cancellableβembedding : (β {x y} β (f x β‘ f y) β (x β‘ y)) β is-embedding f
cancellableβembedding eqv =
embedding-lemma Ξ» x β is-hlevelβ 0 (Ξ£-ap-snd (Ξ» _ β eqv)) $contr (x , refl) Ξ» (y , p) i β p (~ i) , Ξ» j β p (~ i β¨ j) embeddingβcancellable : is-embedding f β β {x y} β is-equiv {B = f x β‘ f y} (ap f) embeddingβcancellable emb = totalβequiv {f = Ξ» y p β ap f {y = y} p} (is-contrβis-equiv (contr (_ , refl) Ξ» (y , p) i β p i , Ξ» j β p (i β§ j)) (contr (_ , refl) (is-hlevelβ 1 (Ξ£-ap-snd Ξ» _ β sym-equiv) (emb _) _)))   abstract embeddingβis-hlevel : β n β is-embedding f β is-hlevel B (suc n) β is-hlevel A (suc n) embeddingβis-hlevel n emb a-hl = is-hlevelβ (suc n) (Total-equiv f)$
Ξ£-is-hlevel (suc n) a-hl Ξ» x β is-propβis-hlevel-suc (emb x)