open import 1Lab.Reflection.Induction
open import 1Lab.Function.Embedding
open import 1Lab.Reflection.HLevel
open import 1Lab.HLevel.Closure
open import 1Lab.Path.Reasoning
open import 1Lab.Type.Sigma
open import 1Lab.Inductive
open import 1Lab.HLevel
open import 1Lab.Equiv
open import 1Lab.Path
open import 1Lab.Type

open import Data.Dec.Base

module 1Lab.HIT.Truncation where


# Propositional truncationπ

Let be a type. The propositional truncation of is a type which represents the proposition βA is inhabitedβ. In MLTT, propositional truncations can not be constructed without postulates, even in the presence of impredicative prop. However, Cubical Agda provides a tool to define them: higher inductive types.

data β₯_β₯ {β} (A : Type β) : Type β where
inc    : A β β₯ A β₯
squash : is-prop β₯ A β₯


The two constructors that generate β₯_β₯ state precisely that the truncation is inhabited when A is (inc), and that it is a proposition (squash).

is-prop-β₯-β₯ : β {β} {A : Type β} β is-prop β₯ A β₯
is-prop-β₯-β₯ = squash

instance
H-Level-truncation : β {n} {β} {A : Type β} β H-Level β₯ A β₯ (suc n)
H-Level-truncation = prop-instance squash


The eliminator for β₯_β₯ says that you can eliminate onto whenever it is a family of propositions, by providing a case for inc.

β₯-β₯-elim : β {β β'} {A : Type β}
{P : β₯ A β₯ β Type β'}
β ((x : _) β is-prop (P x))
β ((x : A) β P (inc x))
β (x : β₯ A β₯) β P x
β₯-β₯-elim pprop incc (inc x) = incc x
β₯-β₯-elim pprop incc (squash x y i) =
is-propβpathp (Ξ» j β pprop (squash x y j)) (β₯-β₯-elim pprop incc x)
(β₯-β₯-elim pprop incc y)
i

β₯-β₯-elimβ : β {β ββ ββ} {A : Type β} {B : Type ββ}
{P : β₯ A β₯ β β₯ B β₯ β Type ββ}
β (β x y β is-prop (P x y))
β (β x y β P (inc x) (inc y))
β β x y β P x y
β₯-β₯-elimβ {A = A} {B} {P} pprop work x y = go x y where
go : β x y β P x y
go (inc x) (inc xβ) = work x xβ
go (inc x) (squash y yβ i) =
is-propβpathp (Ξ» i β pprop (inc x) (squash y yβ i))
(go (inc x) y) (go (inc x) yβ) i

go (squash x xβ i) z =
is-propβpathp (Ξ» i β pprop (squash x xβ i) z)
(go x z) (go xβ z) i

β₯-β₯-rec : β {β β'} {A : Type β} {P : Type β'}
β is-prop P
β (A β P)
β (x : β₯ A β₯) β P
β₯-β₯-rec pprop = β₯-β₯-elim (Ξ» _ β pprop)

β₯-β₯-out : β {β} {A : Type β} β is-prop A β β₯ A β₯ β A
β₯-β₯-out ap = β₯-β₯-rec ap Ξ» x β x

β₯-β₯-recβ : β {β β' β''} {A : Type β} {B : Type β''} {P : Type β'}
β is-prop P
β (A β B β P)
β (x : β₯ A β₯) (y : β₯ B β₯) β P
β₯-β₯-recβ pprop = β₯-β₯-elimβ (Ξ» _ _ β pprop)

β₯-β₯-out! : β {β} {A : Type β} β¦ _ : H-Level A 1 β¦ β β₯ A β₯ β A
β₯-β₯-out! = β₯-β₯-out (hlevel 1)

instance
Inductive-β₯β₯
: β {β β' βm} {A : Type β} {P : β₯ A β₯ β Type β'} β¦ i : Inductive (β x β P (inc x)) βm β¦
β β¦ _ : β {x} β H-Level (P x) 1 β¦
β Inductive (β x β P x) βm
Inductive-β₯β₯ β¦ i β¦ = record
{ methods = i .Inductive.methods
; from    = Ξ» f β β₯-β₯-elim (Ξ» x β hlevel 1) (i .Inductive.from f)
}

Dec-β₯β₯ : β {β} {A : Type β} β β¦ Dec A β¦ β Dec β₯ A β₯
Dec-β₯β₯ β¦ yes a β¦ = yes (inc a)
Dec-β₯β₯ β¦ no Β¬a β¦ = no (rec! Β¬a)


The propositional truncation can be called the free proposition on a type, because it satisfies the universal property that a left adjoint would have. Specifically, let B be a proposition. We have:

β₯-β₯-univ : β {β} {A : Type β} {B : Type β}
β is-prop B β (β₯ A β₯ β B) β (A β B)
β₯-β₯-univ {A = A} {B = B} bprop = IsoβEquiv (inc' , iso rec (Ξ» _ β refl) beta) where
inc' : (x : β₯ A β₯ β B) β A β B
inc' f x = f (inc x)

rec : (f : A β B) β β₯ A β₯ β B
rec f (inc x) = f x
rec f (squash x y i) = bprop (rec f x) (rec f y) i

beta : _
beta f = funext (β₯-β₯-elim (Ξ» _ β is-propβis-set bprop _ _) (Ξ» _ β refl))


Furthermore, as required of a free construction, the propositional truncation extends to a functor:

β₯-β₯-map : β {β β'} {A : Type β} {B : Type β'}
β (A β B) β β₯ A β₯ β β₯ B β₯
β₯-β₯-map f (inc x)        = inc (f x)
β₯-β₯-map f (squash x y i) = squash (β₯-β₯-map f x) (β₯-β₯-map f y) i

β₯-β₯-mapβ : β {β β' β''} {A : Type β} {B : Type β'} {C : Type β''}
β (A β B β C) β β₯ A β₯ β β₯ B β₯ β β₯ C β₯
β₯-β₯-mapβ f (inc x) (inc y)  = inc (f x y)
β₯-β₯-mapβ f (squash x y i) z = squash (β₯-β₯-mapβ f x z) (β₯-β₯-mapβ f y z) i
β₯-β₯-mapβ f x (squash y z i) = squash (β₯-β₯-mapβ f x y) (β₯-β₯-mapβ f x z) i


Using the propositional truncation, we can define the existential quantifier as a truncated Ξ£.

β : β {a b} (A : Type a) (B : A β Type b) β Type _
β A B = β₯ Ξ£ A B β₯

β-syntax : β {a b} (A : Type a) (B : A β Type b) β Type _
β-syntax = β

syntax β-syntax A (Ξ» x β B) = β[ x β A ] B
infix 5 β-syntax


Note that if is already a proposition, then truncating it does nothing:

is-propβequivβ₯-β₯ : β {β} {P : Type β} β is-prop P β P β β₯ P β₯
is-propβequivβ₯-β₯ pprop = prop-ext pprop squash inc (β₯-β₯-out pprop)


In fact, an alternative definition of is-prop is given by βbeing equivalent to your own truncationβ:

is-propβequivβ₯-β₯ : β {β} {P : Type β}
β is-prop P β (P β β₯ P β₯)
is-propβequivβ₯-β₯ {P = P} =
prop-ext is-prop-is-prop eqv-prop is-propβequivβ₯-β₯ inv
where
inv : (P β β₯ P β₯) β is-prop P
inv eqv = equivβis-hlevel 1 ((eqv eβ»ΒΉ) .fst) ((eqv eβ»ΒΉ) .snd) squash

eqv-prop : is-prop (P β β₯ P β₯)
eqv-prop x y = Ξ£-path (Ξ» i p β squash (x .fst p) (y .fst p) i)
(is-equiv-is-prop _ _ _)


Throughout the 1Lab, we use the words βmereβ and βmerelyβ to modify a type to mean its propositional truncation. This terminology is adopted from the HoTT book. For example, a type is said merely equivalent to if the type is inhabited.

## Maps into setsπ

The elimination principle for says that we can only use the inside in a way that doesnβt matter: the motive of elimination must be a family of propositions, so our use of must not matter in a very strong sense. Often, itβs useful to relax this requirement slightly: Can we map out of using a constant function?

The answer is yes, provided weβre mapping into a set! In that case, the image of is a proposition, so that we can map from In the next section, weβll see a more general method for mapping into types that arenβt sets.

From the discussion in 1Lab.Counterexamples.Sigma, we know the definition of image, or more properly of

image : β {β β'} {A : Type β} {B : Type β'} β (A β B) β Type _
image {A = A} {B = B} f = Ξ£[ b β B ] β[ a β A ] (f a β‘ b)


To see that the image indeed implements the concept of image, we define a way to factor any map through its image. By the definition of image, we have that the map image-inc is always surjective, and since β is a family of props, the first projection out of image is an embedding. Thus we factor a map as

image-inc
: β {β β'} {A : Type β} {B : Type β'}
β (f : A β B) β A β image f
image-inc f x = f x , inc (x , refl)


We now prove the theorem that will let us map out of a propositional truncation using a constant function into sets: if is a set, and is a constant function, then is a proposition.

is-constantβimage-is-prop
: β {β β'} {A : Type β} {B : Type β'}
β is-set B
β (f : A β B) β (β x y β f x β‘ f y) β is-prop (image f)


This is intuitively true (if the function is constant, then there is at most one thing in the image!), but formalising it turns out to be slightly tricky, and the requirement that be a set is perhaps unexpected.

A sketch of the proof is as follows. Suppose that we have some and in the image. We know, morally, that (respectively give us some and (resp β which would establish that as we need, since we have where the middle equation is by constancy of β but and are hidden under propositional truncations, so we crucially need to use the fact that is a set so that is a proposition.

is-constantβimage-is-prop bset f fconst (a , x) (b , y) =
Ξ£-prop-path (Ξ» _ β squash)
(β₯-β₯-elimβ (Ξ» _ _ β bset _ _)
(Ξ» { (f*a , p) (f*b , q) β sym p Β·Β· fconst f*a f*b Β·Β· q }) x y)


Using the image factorisation, we can project from a propositional truncation onto a set using a constant map.

β₯-β₯-rec-set : β {β β'} {A : Type β} {B : Type β'}
β is-set B
β (f : A β B)
β (β x y β f x β‘ f y)
β β₯ A β₯ β B
β₯-β₯-rec-set {A = A} {B} bset f fconst x =
β₯-β₯-elim {P = Ξ» _ β image f}
(Ξ» _ β is-constantβimage-is-prop bset f fconst) (image-inc f) x .fst

β₯-β₯-rec-set!
: β {β β'} {A : Type β} {B : Type β'} β¦ _ : H-Level B 2 β¦
β (f : A β B) (c : β x y β f x β‘ f y)
β β₯ A β₯ β B
β₯-β₯-rec-set! {A = A} f c x = go x .fst where
go : β₯ A β₯ β image f
go (inc x)        = f x , inc (x , refl)
go (squash x y i) = is-constantβimage-is-prop (hlevel 2) f c (go x) (go y) i


## Maps into groupoidsπ

We can push this idea further: as discussed in , in general, functions are equivalent to coherently constant functions This involves an infinite tower of conditions, each relating to the previous one, which isnβt something we can easily formulate in the language of type theory.

However, when is an it is enough to ask for the first levels of the tower. In the case of sets, weβve seen that the naΓ―ve notion of constancy is enough. We now deal with the case of groupoids, which requires an additional step: we ask for a function equipped with a witness of constancy and a coherence

This time, we cannot hope to show that the image of is a proposition: the image of a map is Instead, we use the following higher inductive type, which can be thought of as the βcodiscrete groupoidβ on

data β₯_β₯Β³ {β} (A : Type β) : Type β where
inc : A β β₯ A β₯Β³
iconst : β a b β inc a β‘ inc b
icoh : β a b c β PathP (Ξ» i β inc a β‘ iconst b c i) (iconst a b) (iconst a c)
squash : is-groupoid β₯ A β₯Β³


The recursion principle for this type says exactly that any coherently constant function into a groupoid factors through

β₯-β₯Β³-rec
: β {β β'} {A : Type β} {B : Type β'}
β is-groupoid B
β (f : A β B)
β (fconst : β x y β f x β‘ f y)
β (β x y z β fconst x y β fconst y z β‘ fconst x z)
β β₯ A β₯Β³ β B
β₯-β₯Β³-rec {A = A} {B} bgrpd f fconst fcoh = go where
go : β₯ A β₯Β³ β B
go (inc x) = f x
go (iconst a b i) = fconst a b i
go (icoh a b c i j) = ββsquare (sym (fcoh a b c)) i j
go (squash x y a b p q i j k) = bgrpd
(go x) (go y)
(Ξ» i β go (a i)) (Ξ» i β go (b i))
(Ξ» i j β go (p i j)) (Ξ» i j β go (q i j))
i j k


All that remains to show is that is a proposition1, which mainly requires writing more elimination principles.

β₯-β₯Β³-elim-set
: β {β} {A : Type β} {β'} {P : β₯ A β₯Β³ β Type β'}
β (β a β is-set (P a))
β (f : (a : A) β P (inc a))
β (β a b β PathP (Ξ» i β P (iconst a b i)) (f a) (f b))
β β a β P a
unquoteDef β₯-β₯Β³-elim-set = make-elim-n 2 β₯-β₯Β³-elim-set (quote β₯_β₯Β³)

β₯-β₯Β³-elim-prop
: β {β} {A : Type β} {β'} {P : β₯ A β₯Β³ β Type β'}
β (β a β is-prop (P a))
β (f : (a : A) β P (inc a))
β β a β P a
unquoteDef β₯-β₯Β³-elim-prop = make-elim-n 1 β₯-β₯Β³-elim-prop (quote β₯_β₯Β³)

β₯-β₯Β³-is-prop : β {β} {A : Type β} β is-prop β₯ A β₯Β³
β₯-β₯Β³-is-prop = is-contr-if-inhabitedβis-prop $β₯-β₯Β³-elim-prop (Ξ» _ β hlevel 1) (Ξ» a β contr (inc a) (β₯-β₯Β³-elim-set (Ξ» _ β squash _ _) (iconst a) (icoh a)))  Hence we get the desired recursion principle for the usual propositional truncation. β₯-β₯-rec-groupoid : β {β β'} {A : Type β} {B : Type β'} β is-groupoid B β (f : A β B) β (fconst : β x y β f x β‘ f y) β (β x y z β fconst x y β fconst y z β‘ fconst x z) β β₯ A β₯ β B β₯-β₯-rec-groupoid bgrpd f fconst fcoh = β₯-β₯Β³-rec bgrpd f fconst fcoh β β₯-β₯-rec β₯-β₯Β³-is-prop inc  As we hinted at above, this method generalises (externally) to we sketch the details of the next level for the curious reader. The next coherence involves a tetrahedron all of whose faces are or, since weβre doing cubical type theory, a βcubical tetrahedronβ: data β₯_β₯β΄ {β} (A : Type β) : Type β where inc : A β β₯ A β₯β΄ iconst : β a b β inc a β‘ inc b icoh : β a b c β PathP (Ξ» i β inc a β‘ iconst b c i) (iconst a b) (iconst a c) iassoc : β a b c d β PathP (Ξ» i β PathP (Ξ» j β inc a β‘ icoh b c d i j) (iconst a b) (icoh a c d i)) (icoh a b c) (icoh a b d) squash : is-hlevel β₯ A β₯β΄ 4 β₯-β₯β΄-rec : β {β} {A : Type β} {β'} {B : Type β'} β is-hlevel B 4 β (f : A β B) β (fconst : β a b β f a β‘ f b) β (fcoh : β a b c β PathP (Ξ» i β f a β‘ fconst b c i) (fconst a b) (fconst a c)) β (β a b c d β PathP (Ξ» i β PathP (Ξ» j β f a β‘ fcoh b c d i j) (fconst a b) (fcoh a c d i)) (fcoh a b c) (fcoh a b d)) β β₯ A β₯β΄ β B unquoteDef β₯-β₯β΄-rec = make-rec-n 4 β₯-β₯β΄-rec (quote β₯_β₯β΄)  open import Meta.Idiom open import Meta.Bind instance Map-β₯β₯ : Map (eff β₯_β₯) Map-β₯β₯ .Map.map = β₯-β₯-map Idiom-β₯β₯ : Idiom (eff β₯_β₯) Idiom-β₯β₯ .Idiom.pure = inc Idiom-β₯β₯ .Idiom._<*>_ {A = A} {B = B} = go where go : β₯ (A β B) β₯ β β₯ A β₯ β β₯ B β₯ go (inc f) (inc x) = inc (f x) go (inc f) (squash x y i) = squash (go (inc f) x) (go (inc f) y) i go (squash f g i) y = squash (go f y) (go g y) i Bind-β₯β₯ : Bind (eff β₯_β₯) Bind-β₯β₯ .Bind._>>=_ {A = A} {B = B} = go where go : β₯ A β₯ β (A β β₯ B β₯) β β₯ B β₯ go (inc x) f = f x go (squash x y i) f = squash (go x f) (go y f) i  is-embeddingβimage-inc-is-equiv : β {β β'} {A : Type β} {B : Type β'} {f : A β B} β is-embedding f β is-equiv (image-inc f) is-embeddingβimage-inc-is-equiv {f = f} f-emb = is-isoβis-equiv$
iso (Ξ» im β fst $β₯-β₯-out (f-emb _) (im .snd)) (Ξ» im β Ξ£-prop-path! (snd$ β₯-β₯-out (f-emb _) (im .snd)))
(Ξ» _ β refl)

is-embeddingβimage-equiv
: β {β β'} {A : Type β} {B : Type β'} {f : A β B}
β is-embedding f
β A β image f
is-embeddingβimage-equiv {f = f} f-emb =
image-inc f , is-embeddingβimage-inc-is-equiv f-emb


1. in fact, itβs even a propositional truncation of in that it satisfies the same universal property as β©οΈ