less prototype, less bad code implementation of CCHM type theory
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1454 lines
50 KiB

  1. -- We begin by adding some primitive bindings using the PRIMITIVE pragma.
  2. --
  3. -- It goes like this: PRIMITIVE primName varName.
  4. --
  5. -- If the varName is dropped, then it's taken to be the same as primName.
  6. --
  7. -- If there is a previous declaration for the varName, then the type
  8. -- is checked against the internally-known "proper" type for the primitive.
  9. -- Universe of fibrant types
  10. {-# PRIMITIVE Type #-}
  11. -- Universe of non-fibrant types
  12. {-# PRIMITIVE Pretype #-}
  13. -- Fibrant is a fancy word for "has a composition structure". Most types
  14. -- we inherit from MLTT are fibrant:
  15. --
  16. -- Stuff like products Π, sums Σ, naturals, booleans, lists, etc., all
  17. -- have composition structures.
  18. --
  19. -- The non-fibrant types are part of the structure of cubical
  20. -- categories: The interval, partial elements, cubical subtypes, ...
  21. -- The interval
  22. ---------------
  23. -- The interval has two endpoints i0 and i1.
  24. -- These form a de Morgan algebra.
  25. I : Pretype
  26. {-# PRIMITIVE Interval I #-}
  27. i0, i1 : I
  28. {-# PRIMITIVE i0 #-}
  29. {-# PRIMITIVE i1 #-}
  30. -- "minimum" on the interval
  31. iand : I -> I -> I
  32. {-# PRIMITIVE iand #-}
  33. -- "maximum" on the interval.
  34. ior : I -> I -> I
  35. {-# PRIMITIVE ior #-}
  36. -- The interpretation of iand as min and ior as max justifies the fact that
  37. -- ior i (inot i) != i1, since that equality only holds for the endpoints.
  38. -- inot i = 1 - i is a de Morgan involution.
  39. inot : I -> I
  40. {-# PRIMITIVE inot #-}
  41. -- Paths
  42. --------
  43. -- Since every function in type theory is internally continuous,
  44. -- and the two endpoints i0 and i1 are equal, we can take the type of
  45. -- equalities to be continuous functions out of the interval.
  46. -- That is, x ≡ y iff. ∃ f : I -> A, f i0 = x, f i1 = y.
  47. -- The type PathP generalises this to dependent products (i : I) -> A i.
  48. PathP : (A : I -> Type) -> A i0 -> A i1 -> Type
  49. {-# PRIMITIVE PathP #-}
  50. -- By taking the first argument to be constant we get the equality type
  51. -- Path.
  52. Path : {A : Type} -> A -> A -> Type
  53. Path {A} = PathP (\i -> A)
  54. -- reflexivity is given by constant paths
  55. refl : {A : Type} {x : A} -> Path x x
  56. refl {A} {x} i = x
  57. -- Symmetry (for dpeendent paths) is given by inverting the argument to the path, such that
  58. -- sym p i0 = p (inot i0) = p i1
  59. -- sym p i1 = p (inot i1) = p i0
  60. -- This has the correct endpoints.
  61. sym : {A : I -> Type} {x : A i0} {y : A i1} -> PathP A x y -> PathP (\i -> A (inot i)) y x
  62. sym p i = p (inot i)
  63. id : {A : Type} -> A -> A
  64. id x = x
  65. the : (A : Pretype) -> A -> A
  66. the A x = x
  67. -- The eliminator for the interval says that if you have x : A i0 and y : A i1,
  68. -- and x ≡ y, then you can get a proof A i for every element of the interval.
  69. iElim : {A : I -> Type} {x : A i0} {y : A i1} -> PathP A x y -> (i : I) -> A i
  70. iElim p i = p i
  71. -- This corresponds to the elimination principle for the HIT
  72. -- data I : Pretype where
  73. -- i0 i1 : I
  74. -- seg : i0 ≡ i1
  75. -- The singleton subtype of A at x is the type of elements of y which
  76. -- are equal to x.
  77. Singl : (A : Type) -> A -> Type
  78. Singl A x = (y : A) * Path x y
  79. -- Contractible types are those for which there exists an element to which
  80. -- all others are equal.
  81. isContr : Type -> Type
  82. isContr A = (x : A) * ((y : A) -> Path x y)
  83. -- Using the connection \i j -> y.2 (iand i j), we can prove that
  84. -- singletons are contracible. Together with transport later on,
  85. -- we get the J elimination principle of paths.
  86. singContr : {A : Type} {a : A} -> isContr (Singl A a)
  87. singContr {A} {a} = ((a, \i -> a), \y i -> (y.2 i, \j -> y.2 (iand i j)))
  88. -- Some more operations on paths. By rearranging parentheses we get a
  89. -- proof that the images of equal elements are themselves equal.
  90. cong : {A : Type} {B : A -> Type} (f : (x : A) -> B x) {x : A} {y : A} (p : Path x y) -> PathP (\i -> B (p i)) (f x) (f y)
  91. cong f p i = f (p i)
  92. -- These satisfy definitional equalities, like congComp and congId, which are
  93. -- propositional in vanilla MLTT.
  94. congComp : {A : Type} {B : Type} {C : Type}
  95. {f : A -> B} {g : B -> C} {x : A} {y : A}
  96. (p : Path x y)
  97. -> Path (cong g (cong f p)) (cong (\x -> g (f x)) p)
  98. congComp p = refl
  99. congId : {A : Type} {x : A} {y : A}
  100. (p : Path x y)
  101. -> Path (cong (id {A}) p) p
  102. congId p = refl
  103. -- Just like rearranging parentheses gives us cong, swapping the value
  104. -- and interval binders gives us function extensionality.
  105. funext : {A : Type} {B : A -> Type} {f : (x : A) -> B x} {g : (x : A) -> B x}
  106. (h : (x : A) -> Path (f x) (g x))
  107. -> Path f g
  108. funext h i x = h x i
  109. -- The proposition associated with an element of the interval
  110. -------------------------------------------------------------
  111. Eq_s : {A : Pretype} -> A -> A -> Pretype
  112. {-# PRIMITIVE Eq_s #-}
  113. refl_s : {A : Pretype} {x : A} -> Eq_s x x
  114. {-# PRIMITIVE refl_s #-}
  115. J_s : {A : Pretype} {x : A} (P : (y : A) -> Eq_s x y -> Pretype) -> P x (refl_s {A} {x}) -> {y : A} -> (p : Eq_s x y) -> P y p
  116. {-# PRIMITIVE J_s #-}
  117. K_s : {A : Pretype} {x : A} (P : Eq_s x x -> Pretype) -> P (refl_s {A} {x}) -> (p : Eq_s x x) -> P p
  118. {-# PRIMITIVE K_s #-}
  119. -- Associated with every element i : I of the interval, we have the type
  120. -- IsOne i which is inhabited only when i = i1. In the model, this
  121. -- corresponds to the map [φ] from the interval cubical set to the
  122. -- subobject classifier.
  123. IsOne : I -> Pretype
  124. IsOne i = Eq_s i i1
  125. -- The value itIs1 witnesses the fact that i1 = i1.
  126. itIs1 : IsOne i1
  127. itIs1 = refl_s
  128. -- Partial elements
  129. -------------------
  130. --
  131. -- Since a function I -> A has two endpoints, and a function I -> I -> A
  132. -- has four endpoints + four functions I -> A as "sides" (obtained by
  133. -- varying argument while holding the other as a bound variable), we
  134. -- refer to elements of I^n -> A as "cubes".
  135. -- This justifies the existence of partial elements, which are, as the
  136. -- name implies, partial cubes. Namely, a Partial φ A is an element of A
  137. -- which depends on a proof that IsOne φ.
  138. Partial : I -> Type -> Pretype
  139. {-# PRIMITIVE Partial #-}
  140. -- There is also a dependent version where the type A is itself a
  141. -- partial element.
  142. PartialP : (phi : I) -> Partial phi Type -> Pretype
  143. {-# PRIMITIVE PartialP #-}
  144. -- Why is Partial φ A not just defined as φ -> A? The difference is that
  145. -- Partial φ A has an internal representation which definitionally relates
  146. -- any two partial elements which "agree everywhere", that is, have
  147. -- equivalent values for every possible assignment of variables which
  148. -- makes IsOne φ hold.
  149. -- Cubical Subtypes
  150. --------------------
  151. -- Given A : Type, phi : I, and a partial element u : A defined on φ,
  152. -- we have the type Sub A phi u, notated A[phi -> u] in the output of
  153. -- the type checker, whose elements are "extensions" of u.
  154. -- That is, element of A[phi -> u] is an element of A defined everywhere
  155. -- (a total element), which, when IsOne φ, agrees with u.
  156. Sub : (A : Type) (phi : I) -> Partial phi A -> Pretype
  157. {-# PRIMITIVE Sub #-}
  158. -- Every total element u : A can be made partial on φ by ignoring the
  159. -- constraint. Furthermore, this "totally partial" element agrees with
  160. -- the original total element on φ.
  161. inS : {A : Type} {phi : I} (u : A) -> Sub A phi (\x -> u)
  162. {-# PRIMITIVE inS #-}
  163. -- When IsOne φ, outS {A} {φ} {u} x reduces to u itIs1.
  164. -- This implements the fact that x agrees with u on φ.
  165. outS : {A : Type} {phi : I} {u : Partial phi A} -> Sub A phi u -> A
  166. {-# PRIMITIVE outS #-}
  167. -- The composition operation
  168. ----------------------------
  169. -- Now that we have syntax for specifying partial cubes,
  170. -- and specifying that an element agrees with a partial cube,
  171. -- we can describe the composition operation.
  172. primComp : (A : I -> Type) {phi : I} (u : (i : I) -> Partial phi (A i)) -> Sub (A i0) phi (u i0) -> Sub (A i1) phi (u i1)
  173. {-# PRIMITIVE comp primComp #-}
  174. comp : (A : I -> Type) {phi : I} (u : (i : I) -> Partial phi (A i)) -> Sub (A i0) phi (u i0) -> A i1
  175. comp A {phi} u a0 = outS (primComp A {phi} u a0)
  176. -- In particular, when φ is a disjunction of the form
  177. -- (j = 0) || (j = 1), we can draw u as being a pair of lines forming a
  178. -- "tube", an open square with no floor or roof:
  179. --
  180. -- Given u = \j [ (i = i0) -> x, (i = i1) -> q j] on the extent i || ~i,
  181. -- we draw:
  182. --
  183. -- x q i1
  184. -- | |
  185. -- \j -> x | | \j -> q j
  186. -- | |
  187. -- x q i0
  188. --
  189. -- The composition operation says that, as long as we can provide a
  190. -- "floor" connecting x -- q i0, as a total element of A which, on
  191. -- phi, extends u i0, then we get the "roof" connecting x and q i1
  192. -- for free.
  193. --
  194. -- If we have a path p : x ≡ y, and q : y ≡ z, then we do get the
  195. -- "floor", and composition gets us the dotted line:
  196. --
  197. -- x..........z
  198. -- | |
  199. -- x | | q j
  200. -- | |
  201. -- x----------y
  202. -- p i
  203. trans : {A : Type} {x : A} {y : A} {z : A} -> PathP (\i -> A) x y -> PathP (\i -> A) y z -> PathP (\i -> A) x z
  204. trans {A} {x} p q i =
  205. comp (\i -> A)
  206. {ior i (inot i)}
  207. (\j [ (i = i0) -> x, (i = i1) -> q j ])
  208. (inS (p i))
  209. -- In particular when the formula φ = i0 we get the "opposite face" to a
  210. -- single point, which corresponds to transport.
  211. transp : (A : I -> Type) (x : A i0) -> A i1
  212. transp A x = comp A {i0} (\i [ ]) (inS x)
  213. -- Since we have the iand operator, we can also derive the *filler* of a cube,
  214. -- which connects the given face and the output of composition.
  215. fill : (A : I -> Type) {phi : I} (u : (i : I) -> Partial phi (A i)) (a0 : Sub (A i0) phi (u i0)) -> PathP A (outS a0) (comp A {phi} u a0)
  216. fill A {phi} u a0 i =
  217. comp (\j -> A (iand i j))
  218. {ior phi (inot i)}
  219. (\j [ (phi = i1) as p -> u (iand i j) p, (i = i0) -> outS a0 ])
  220. (inS (outS a0))
  221. hcomp : {A : Type} {phi : I} (u : (i : I) -> Partial phi A) -> Sub A phi (u i0) -> A
  222. hcomp {A} {phi} u a0 = comp (\i -> A) {phi} u a0
  223. hfill : {A : Type} {phi : I} (u : (i : I) -> Partial phi A) -> (a0 : Sub A phi (u i0)) -> Path (outS a0) (hcomp u a0)
  224. hfill {A} {phi} u a0 i = fill (\i -> A) {phi} u a0 i
  225. -- For instance, the filler of the previous composition square
  226. -- tells us that trans p refl = p:
  227. transRefl : {A : Type} {x : A} {y : A} (p : Path x y) -> Path (trans p refl) p
  228. transRefl p j i = fill (\i -> A) {ior i (inot i)} (\k [ (i = i0) -> x, (i = i1) -> y ]) (inS (p i)) (inot j)
  229. rightCancel : {A : Type} {x : A} {y : A} (p : Path x y) -> Path (trans p (sym p)) refl
  230. rightCancel p j i = cube p i1 j i where
  231. cube : {A : Type} {x : A} {y : A} (p : Path x y) -> I -> I -> I -> A
  232. cube {A} {x} p k j i =
  233. hfill {A} (\ k [ (i = i0) -> x
  234. , (i = i1) -> p (iand (inot k) (inot j))
  235. , (j = i1) -> x
  236. ])
  237. (inS (p (iand i (inot j)))) k
  238. leftCancel : {A : Type} {x : A} {y : A} (p : Path x y) -> Path (trans (sym p) p) refl
  239. leftCancel p = rightCancel (sym p)
  240. transpFill : {A : I -> Type} (x : A i0) -> PathP A x (transp (\i -> A i) x)
  241. transpFill {A} x i = fill (\i -> A i) (\k []) (inS x) i
  242. -- Reduction of composition
  243. ---------------------------
  244. --
  245. -- Composition reduces on the structure of the family A : I -> Type to create
  246. -- the element a1 : (A i1)[phi -> u i1].
  247. --
  248. -- For instance, when filling a cube of functions, the behaviour is to
  249. -- first transport backwards along the domain, apply the function, then
  250. -- forwards along the codomain.
  251. transpFun : {A : Type} {B : Type} {C : Type} {D : Type} (p : Path A B) (q : Path C D)
  252. -> (f : A -> C) -> Path (transp (\i -> p i -> q i) f)
  253. (\x -> transp (\i -> q i) (f (transp (\i -> p (inot i)) x)))
  254. transpFun p q f = refl
  255. transpDFun : {A : I -> Type} {B : (i : I) -> A i -> Type}
  256. -> (f : (x : A i0) -> B i0 x)
  257. -> Path (transp (\i -> (x : A i) -> B i x) f)
  258. (\x -> transp (\i -> B i (fill (\j -> A (inot j)) (\k []) (inS x) (inot i)))
  259. (f (fill (\j -> A (inot j)) (\k []) (inS x) i1)))
  260. transpDFun f = refl
  261. -- When considering the more general case of a composition respecing sides,
  262. -- the outer transport becomes a composition.
  263. -- Glueing and Univalence
  264. -------------------------
  265. -- First, let's get some definitions out of the way.
  266. --
  267. -- The *fiber* of a function f : A -> B at a point y : B is the type of
  268. -- inputs x : A which f takes to y, that is, for which there exists a
  269. -- path f(x) = y.
  270. fiber : {A : Type} {B : Type} -> (A -> B) -> B -> Type
  271. fiber f y = (x : A) * Path y (f x)
  272. -- An *equivalence* is a function where every fiber is contractible.
  273. -- That is, for every point in the codomain y : B, there is exactly one
  274. -- point in the input which f maps to y.
  275. isEquiv : {A : Type} {B : Type} -> (A -> B) -> Type
  276. isEquiv {A} {B} f = (y : B) -> isContr (fiber {A} {B} f y)
  277. -- By extracting this point, which must exist because the fiber is contractible,
  278. -- we can get an inverse of f:
  279. invert : {A : Type} {B : Type} {f : A -> B} -> isEquiv f -> B -> A
  280. invert eqv y = (eqv y) .1 .1
  281. retract : {A : Type} {B : Type} -> (A -> B) -> (B -> A) -> Type
  282. retract {A} {B} f g = (a : A) -> Path (g (f a)) a
  283. contr : {A : Type} {phi : I} -> isContr A -> (u : Partial phi A) -> A
  284. contr {A} {phi} p u = comp (\i -> A) {phi} (\i is1 -> p.2 (u is1) i) (inS (p.1))
  285. -- Proving that it's also a retraction is left as an exercise to the
  286. -- reader. We can package together a function and a proof that it's an
  287. -- equivalence to get a capital-E Equivalence.
  288. Equiv : (A : Type) (B : Type) -> Type
  289. Equiv A B = (f : A -> B) * isEquiv {A} {B} f
  290. -- The identity function is an equivalence between any type A and
  291. -- itself.
  292. idEquiv : {A : Type} -> isEquiv (id {A})
  293. idEquiv y = ((y, \i -> y), \u i -> (u.2 i, \j -> u.2 (iand i j)))
  294. -- The glue operation expresses that "extensibility is invariant under
  295. -- equivalence". Less concisely, the Glue type and its constructor,
  296. -- glue, let us extend a partial element of a partial type to a total
  297. -- element of a total type, by "gluing" the partial type T using a
  298. -- partial equivalence e onto a total type A.
  299. -- In particular, we have that when φ = i1, Glue A [i1 -> (T, f)] = T.
  300. primGlue : (A : Type) {phi : I}
  301. (T : Partial phi Type)
  302. (e : PartialP phi (\o -> Equiv (T o) A))
  303. -> Type
  304. {-# PRIMITIVE Glue primGlue #-}
  305. -- The glue constructor extends the partial element t : T to a total
  306. -- element of Glue A [φ -> (T, e)] as long as we have a total im : A
  307. -- which is the image of f(t).
  308. --
  309. -- Agreeing with the condition that Glue A [i1 -> (T, e)] = T,
  310. -- we have that glue {A} {i1} t im => t.
  311. prim'glue : {A : Type} {phi : I} {T : Partial phi Type} {e : PartialP phi (\o -> Equiv (T o) A)}
  312. -> (t : PartialP phi T)
  313. -> (im : Sub A phi (\o -> (e o).1 (t o)))
  314. -> primGlue A T e
  315. {-# PRIMITIVE glue prim'glue #-}
  316. glue : {A : Type} {phi : I} {Te : Partial phi ((T : Type) * Equiv T A)}
  317. -> (t : PartialP phi (\o -> (Te o).1))
  318. -> (im : Sub A phi (\o -> (Te o).2.1 (t o)))
  319. -> primGlue A {phi} (\o -> (Te o).1) (\o -> (Te o).2)
  320. glue {A} {phi} {Te} t im = prim'glue {A} {phi} {\o -> (Te o).1} {\o -> (Te o).2} t im
  321. -- The unglue operation undoes a glueing. Since when φ = i1,
  322. -- Glue A [φ -> (T, f)] = T, the argument to primUnglue {A} {i1} ...
  323. -- will have type T, and so to get back an A we need to apply the
  324. -- partial equivalence f (defined everywhere).
  325. primUnglue : {A : Type} {phi : I} {T : Partial phi Type} {e : PartialP phi (\o -> Equiv (T o) A)}
  326. -> primGlue A {phi} T e -> A
  327. {-# PRIMITIVE unglue primUnglue #-}
  328. unglue : {A : Type} (phi : I) {Te : Partial phi ((T : Type) * Equiv T A)}
  329. -> primGlue A {phi} (\o -> (Te o).1) (\o -> (Te o).2) -> A
  330. unglue {A} phi {Te} = primUnglue {A} {phi} {\o -> (Te o).1} {\o -> (Te o).2}
  331. -- Diagramatically, i : I |- Glue A [(i \/ ~i) -> (T, e)] can be drawn
  332. -- as giving us the dotted line in:
  333. --
  334. -- T i0 ......... T i1
  335. -- | |
  336. -- | |
  337. -- e i0 |~ ~| e i1
  338. -- | |
  339. -- | |
  340. -- A i0 --------- A i1
  341. -- A
  342. --
  343. -- Where the the two "e" sides are equivalences, and the Bottom side is
  344. -- the line i : I |- A.
  345. --
  346. -- Thus, by choosing a base type, a set of partial types and partial
  347. -- equivalences, we can make a line between two types (T i0) and (T i1).
  348. Glue : (A : Type) {phi : I} -> Partial phi ((X : Type) * Equiv X A) -> Type
  349. Glue A {phi} u = primGlue A {phi} (\o -> (u o).1) (\o -> (u o).2)
  350. -- For example, we can glue together the type A and the type B as long
  351. -- as there exists an Equiv A B.
  352. --
  353. -- A ............ B
  354. -- | |
  355. -- | |
  356. -- equiv |~ ua equiv ~| idEquiv {B}
  357. -- | |
  358. -- | |
  359. -- B ------------ B
  360. -- \i → B
  361. --
  362. univalence : {A : Type} {B : Type} -> Equiv A B -> Path A B
  363. univalence {A} {B} equiv i =
  364. Glue B (\[ (i = i0) -> (A, equiv),
  365. (i = i1) -> (B, the B, idEquiv {B}) ])
  366. lineToEquiv : (A : I -> Type) -> Equiv (A i0) (A i1)
  367. {-# PRIMITIVE lineToEquiv #-}
  368. idToEquiv : {A : Type} {B : Type} -> Path A B -> Equiv A B
  369. idToEquiv p = lineToEquiv (\i -> p i)
  370. -- The fact that this diagram has 2 filled-in B sides explains the
  371. -- complication in the proof below.
  372. --
  373. -- In particular, the actual behaviour of transp (\i -> univalence f i)
  374. -- (x : A) is not just to apply f x to get a B (the left side), it also
  375. -- needs to:
  376. --
  377. -- * For the Bottom side, compose along (\i -> B) (the Bottom side)
  378. -- * For the right side, apply the inverse of the identity, which
  379. -- is just identity, to get *some* b : B
  380. --
  381. -- But that b : B might not agree with the sides of the composition
  382. -- operation in a more general case, so it composes along (\i -> B)
  383. -- *again*!
  384. --
  385. -- Thus the proof: a simple cubical argument suffices, since
  386. -- for any composition, its filler connects either endpoints. So
  387. -- we need to come up with a filler for the Bottom and right faces.
  388. univalenceBeta : {A : Type} {B : Type} (f : Equiv A B) -> Path (transp (\i -> univalence f i)) f.1
  389. univalenceBeta {A} {B} f i a = transpFill {\i -> B} (f.1 a) (inot i)
  390. -- The terms univalence + univalenceBeta suffice to prove the "full"
  391. -- univalence axiom of Voevodsky, as can be seen in the paper
  392. --
  393. -- Ian Orton, & Andrew M. Pitts. (2017). Decomposing the Univalence Axiom.
  394. --
  395. -- Available freely here: https://arxiv.org/abs/1712.04890v3
  396. J : {A : Type} {x : A}
  397. (P : (y : A) -> Path x y -> Type)
  398. (d : P x (\i -> x))
  399. {y : A} (p : Path x y)
  400. -> P y p
  401. J P d p = transp (\i -> P (p i) (\j -> p (iand i j))) d
  402. Jay : {A : Type} {x : A}
  403. (P : ((y : A) * Path x y) -> Type)
  404. (d : P (x, refl))
  405. (s : (y : A) * Path x y)
  406. -> P s
  407. Jay P d s = transp (\i -> P ((singContr {A} {x}).2 s i)) d
  408. -- Isomorphisms
  409. ---------------
  410. --
  411. -- Since isomorphisms are a much more convenient notion of equivalence
  412. -- than contractible fibers, it's natural to ask why the CCHM paper, and
  413. -- this implementation following that, decided on the latter for our
  414. -- definition of equivalence.
  415. isIso : {A : Type} -> {B : Type} -> (A -> B) -> Type
  416. isIso {A} {B} f = (g : B -> A) * ((y : B) -> Path (f (g y)) y) * ((x : A) -> Path (g (f x)) x)
  417. -- The reason is that the family of types IsIso is not a proposition!
  418. -- This means that there can be more than one way for a function to be
  419. -- an equivalence. This is Lemma 4.1.1 of the HoTT book.
  420. Iso : Type -> Type -> Type
  421. Iso A B = (f : A -> B) * isIso f
  422. -- Nevertheless, we can prove that any function with an isomorphism
  423. -- structure has contractible fibers, using a cubical argument adapted
  424. -- from CCHM's implementation of cubical type theory:
  425. --
  426. -- https://github.com/mortberg/cubicaltt/blob/master/experiments/isoToEquiv.ctt#L7-L55
  427. IsoToEquiv : {A : Type} {B : Type} -> Iso A B -> Equiv A B
  428. IsoToEquiv {A} {B} iso = (f, \y -> (fCenter y, fIsCenter y)) where
  429. f = iso.1
  430. g = iso.2.1
  431. s = iso.2.2.1
  432. t = iso.2.2.2
  433. lemIso : (y : B) (x0 : A) (x1 : A) (p0 : Path y (f x0)) (p1 : Path y (f x1))
  434. -> PathP (\i -> fiber f y) (x0, p0) (x1, p1)
  435. lemIso y x0 x1 p0 p1 =
  436. let
  437. rem0 : Path x0 (g y)
  438. rem0 i = comp (\i -> A) (\k [ (i = i0) -> t x0 k, (i = i1) -> g y ]) (inS (g (p0 (inot i))))
  439. rem1 : Path x1 (g y)
  440. rem1 i = comp (\i -> A) (\k [ (i = i0) -> t x1 k, (i = i1) -> g y ]) (inS (g (p1 (inot i))))
  441. p : Path x0 x1
  442. p i = comp (\i -> A) (\k [ (i = i0) -> rem0 (inot k), (i = i1) -> rem1 (inot k) ]) (inS (g y))
  443. fill0 : I -> I -> A
  444. fill0 i j = comp (\i -> A) (\k [ (i = i0) -> t x0 (iand j k)
  445. , (i = i1) -> g y
  446. , (j = i0) -> g (p0 (inot i))
  447. ])
  448. (inS (g (p0 (inot i))))
  449. fill1 : I -> I -> A
  450. fill1 i j = comp (\i -> A) (\k [ (i = i0) -> t x1 (iand j k)
  451. , (i = i1) -> g y
  452. , (j = i0) -> g (p1 (inot i)) ])
  453. (inS (g (p1 (inot i))))
  454. fill2 : I -> I -> A
  455. fill2 i j = comp (\i -> A) (\k [ (i = i0) -> rem0 (ior j (inot k))
  456. , (i = i1) -> rem1 (ior j (inot k))
  457. , (j = i1) -> g y ])
  458. (inS (g y))
  459. sq : I -> I -> A
  460. sq i j = comp (\i -> A) (\k [ (i = i0) -> fill0 j (inot k)
  461. , (i = i1) -> fill1 j (inot k)
  462. , (j = i1) -> g y
  463. , (j = i0) -> t (p i) (inot k) ])
  464. (inS (fill2 i j))
  465. sq1 : I -> I -> B
  466. sq1 i j = comp (\i -> B) (\k [ (i = i0) -> s (p0 (inot j)) k
  467. , (i = i1) -> s (p1 (inot j)) k
  468. , (j = i0) -> s (f (p i)) k
  469. , (j = i1) -> s y k
  470. ])
  471. (inS (f (sq i j)))
  472. in \i -> (p i, \j -> sq1 i (inot j))
  473. fCenter : (y : B) -> fiber f y
  474. fCenter y = (g y, sym (s y))
  475. fIsCenter : (y : B) (w : fiber f y) -> Path (fCenter y) w
  476. fIsCenter y w = lemIso y (fCenter y).1 w.1 (fCenter y).2 w.2
  477. IsoToId : {A : Type} {B : Type} -> Iso A B -> Path A B
  478. IsoToId i = univalence (IsoToEquiv i)
  479. -- We can prove that any involutive function is an isomorphism, since
  480. -- such a function is its own inverse.
  481. involToIso : {A : Type} (f : A -> A) -> ((x : A) -> Path (f (f x)) x) -> isIso f
  482. involToIso {A} f inv = (f, inv, inv)
  483. -- An example of univalence
  484. ---------------------------
  485. --
  486. -- The classic example of univalence is the equivalence
  487. -- not : Bool \simeq Bool.
  488. --
  489. -- We define it here.
  490. data Bool : Type where
  491. true : Bool
  492. false : Bool
  493. not : Bool -> Bool
  494. not = \case
  495. true -> false
  496. false -> true
  497. elimBool : (P : Bool -> Type) -> P true -> P false -> (b : Bool) -> P b
  498. elimBool P x y = \case
  499. true -> x
  500. false -> y
  501. if : {A : Type} -> A -> A -> Bool -> A
  502. if x y = \case
  503. true -> x
  504. false -> y
  505. -- By pattern matching it suffices to prove (not (not true)) ≡ true and
  506. -- not (not false) ≡ false. Since not (not true) computes to true (resp.
  507. -- false), both proofs go through by refl.
  508. notInvol : (x : Bool) -> Path (not (not x)) x
  509. notInvol = elimBool (\b -> Path (not (not b)) b) refl refl
  510. notp : Path Bool Bool
  511. notp = univalence (IsoToEquiv (not, involToIso not notInvol))
  512. -- This path actually serves to prove a simple lemma about the universes
  513. -- of HoTT, namely, that any univalent universe is not a 0-type. If we
  514. -- had HITs, we could prove that this fact holds for any n, but for now,
  515. -- proving it's not an h-set is the furthest we can go.
  516. -- First we define what it means for something to be false. In type theory,
  517. -- we take ¬P = P → ⊥, where the Bottom type is the only type satisfying
  518. -- the elimination principle
  519. --
  520. -- elimBottom : (P : Bottom -> Type) -> (b : Bottom) -> P b
  521. --
  522. -- This follows from setting Bottom := ∀ A, A.
  523. data Bottom : Type where {}
  524. elimBottom : (P : Bottom -> Pretype) -> (b : Bottom) -> P b
  525. elimBottom P = \case {}
  526. absurd : {P : Pretype} -> Bottom -> P
  527. absurd = \case {}
  528. -- We prove that true != false by transporting along the path
  529. --
  530. -- \i -> if (Bool -> Bool) A (p i)
  531. -- (Bool -> Bool) ------------------------------------ A
  532. --
  533. -- To verify that this has the correct endpoints, check out the endpoints
  534. -- for p:
  535. --
  536. -- true ------------------------------------ false
  537. --
  538. -- and evaluate the if at either end.
  539. trueNotFalse : Path true false -> Bottom
  540. trueNotFalse p = transp (\i -> if (Bool -> Bool) Bottom (p i)) id
  541. -- To be an h-Set is to have no "higher path information". Alternatively,
  542. --
  543. -- isHSet A = (x : A) (y : A) -> isHProp (Path x y)
  544. --
  545. isProp : Type -> Type
  546. isProp A = (x : A) (y : A) -> Path x y
  547. isHSet : Type -> Type
  548. isHSet A = (x : A) (y : A) -> isProp (Path x y)
  549. -- We can prove *a* contradiction (note: this is a direct proof!) by adversarially
  550. -- choosing two paths p, q that we know are not equal. Since "equal" paths have
  551. -- equal behaviour when transporting, we can choose two paths p, q and a point x
  552. -- such that transporting x along p gives a different result from x along q.
  553. --
  554. -- Since transp notp = not but transp refl = id, that's what we go with. The choice
  555. -- of false as the point x is just from the endpoints of trueNotFalse.
  556. universeNotSet : isHSet Type -> Bottom
  557. universeNotSet itIs = trueNotFalse (\i -> transp (\j -> itIs Bool Bool notp refl i j) false)
  558. -- Funext is an inverse of happly
  559. ---------------------------------
  560. --
  561. -- Above we proved function extensionality, namely, that functions
  562. -- pointwise equal everywhere are themselves equal.
  563. -- However, this formulation of the axiom is known as "weak" function
  564. -- extensionality. The strong version is as follows:
  565. Hom : {A : Type} {B : A -> Type} (f : (x : A) -> B x) -> (g : (x : A) -> B x) -> Type
  566. Hom {A} f g = (x : A) -> Path (f x) (g x)
  567. happly : {A : Type} {B : A -> Type} {f : (x : A) -> B x} {g : (x : A) -> B x}
  568. -> (p : Path f g) -> Hom f g
  569. happly p x i = p i x
  570. -- Strong function extensionality: happly is an equivalence.
  571. happlyIsIso : {A : Type} {B : A -> Type} {f : (x : A) -> B x} {g : (x : A) -> B x}
  572. -> isIso {Path f g} {Hom f g} happly
  573. happlyIsIso {A} {B} {f} {g} = (funext {A} {B} {f} {g}, \hom -> refl, \path -> refl)
  574. pathIsHom : {A : Type} {B : A -> Type} {f : (x : A) -> B x} {g : (x : A) -> B x}
  575. -> Path (Path f g) (Hom f g)
  576. pathIsHom {A} {B} {f} {g} =
  577. let
  578. theIso : Iso (Path f g) (Hom f g)
  579. theIso = (happly {A} {B} {f} {g}, happlyIsIso {A} {B} {f} {g})
  580. in univalence (IsoToEquiv theIso)
  581. -- Inductive types
  582. -------------------
  583. --
  584. -- An inductive type is a type freely generated by a finite set of
  585. -- constructors. For instance, the type of natural numbers is generated
  586. -- by the constructors for "zero" and "successor".
  587. data Nat : Type where
  588. zero : Nat
  589. succ : Nat -> Nat
  590. -- Pattern matching allows us to prove that these initial types are
  591. -- initial algebras for their corresponding functors.
  592. Nat_elim : (P : Nat -> Type) -> P zero -> ((x : Nat) -> P x -> P (succ x)) -> (x : Nat) -> P x
  593. Nat_elim P pz ps = \case
  594. zero -> pz
  595. succ x -> ps x (Nat_elim P pz ps x)
  596. zeroNotSucc : {x : Nat} -> Path zero (succ x) -> Bottom
  597. zeroNotSucc p = transp (\i -> fun (p i)) (p i0) where
  598. fun : Nat -> Type
  599. fun = \case
  600. zero -> Nat
  601. succ x -> Bottom
  602. succInj : {x : Nat} {y : Nat} -> Path (succ x) (succ y) -> Path x y
  603. succInj p i = pred (p i) where
  604. pred : Nat -> Nat
  605. pred = \case
  606. zero -> zero
  607. succ x -> x
  608. -- The type of integers can be defined as A + B, where "pos n" means +n
  609. -- and "neg n" means -(n + 1).
  610. data Int : Type where
  611. pos : Nat -> Int
  612. neg : Nat -> Int
  613. -- On this representation we can define the successor and predecessor
  614. -- functions by (nested) induction.
  615. sucZ : Int -> Int
  616. sucZ = \case
  617. pos n -> pos (succ n)
  618. neg n ->
  619. let suc_neg : Nat -> Int
  620. suc_neg = \case
  621. zero -> pos zero
  622. succ n -> neg n
  623. in suc_neg n
  624. predZ : Int -> Int
  625. predZ = \case
  626. pos n ->
  627. let pred_pos : Nat -> Int
  628. pred_pos = \case
  629. zero -> neg zero
  630. succ n -> pos n
  631. in pred_pos n
  632. neg n -> neg (succ n)
  633. -- And prove that the successor function is an isomorphism, and thus, an
  634. -- equivalence.
  635. sucEquiv : isIso sucZ
  636. sucEquiv =
  637. let
  638. sucPredZ : (x : Int) -> Path (sucZ (predZ x)) x
  639. sucPredZ = \case
  640. pos n ->
  641. let k : (n : Nat) -> Path (sucZ (predZ (pos n))) (pos n)
  642. k = \case
  643. zero -> refl
  644. succ n -> refl
  645. in k n
  646. neg n -> refl
  647. predSucZ : (x : Int) -> Path (predZ (sucZ x)) x
  648. predSucZ = \case
  649. pos n -> refl
  650. neg n ->
  651. let k : (n : Nat) -> Path (predZ (sucZ (neg n))) (neg n)
  652. k = \case
  653. zero -> refl
  654. succ n -> refl
  655. in k n
  656. in (predZ, sucPredZ, predSucZ)
  657. -- Univalence gives us a path between integers such that transp intPath
  658. -- x = suc x, transp (sym intPath) x = pred x
  659. intPath : Path Int Int
  660. intPath = univalence (IsoToEquiv (sucZ, sucEquiv))
  661. -- Higher inductive types
  662. -------------------------
  663. --
  664. -- While inductive types let us generate discrete spaces like the
  665. -- naturals or integers, they do not support defining higher-dimensional
  666. -- structures given by spaces with points and paths.
  667. -- A very simple higher inductive type is the interval, given by
  668. data Interval : Type where
  669. ii0 : Interval
  670. ii1 : Interval
  671. seg i : Interval [ (i = i0) -> ii0, (i = i1) -> ii1 ]
  672. -- This expresses that we have two points ii0 and ii1 and a path (\i ->
  673. -- seg i) with endpoints ii0 and ii1.
  674. -- With this type we can reproduce the proof of Lemma 6.3.2 from the
  675. -- HoTT book:
  676. iFunext : {A : Type} {B : A -> Type} (f : (x : A) -> B x) (g : (x : A) -> B x)
  677. -> ((x : A) -> Path (f x) (g x)) -> Path f g
  678. iFunext f g p i = h' (seg i) where
  679. h : (x : A) -> Interval -> B x
  680. h x = \case
  681. ii0 -> f x
  682. ii1 -> g x
  683. seg i -> p x i
  684. h' : Interval -> (x : A) -> B x
  685. h' i x = h x i
  686. -- Of course, Cubical Type Theory also has an interval (pre)type, but
  687. -- that, unlike the Interval here, is not Kan: it has no composition
  688. -- structure.
  689. -- Another simple higher-inductive type is the circle, with a point and
  690. -- a non-trivial loop, (\i -> loop i).
  691. data S1 : Type where
  692. base : S1
  693. loop i : S1 [ (i = i1) -> base, (i = i0) -> base ]
  694. -- By writing a function from the circle to the universe of types Type,
  695. -- we can calculate winding numbers along the circle.
  696. helix : S1 -> Type
  697. helix = \case
  698. base -> Int
  699. loop i -> intPath i
  700. loopP : Path base base
  701. loopP i = loop i
  702. winding : Path base base -> Int
  703. winding p = transp (\i -> helix (p i)) (pos zero)
  704. -- For instance, going around the loop once has a winding number of +1,
  705. windingLoop : Path (winding (\i -> loop i)) (pos (succ zero))
  706. windingLoop = refl
  707. -- Going backwards has a winding number of -1 (remember the
  708. -- representation of integers),
  709. windingSymLoop : Path (winding (\i -> loop (inot i))) (neg zero)
  710. windingSymLoop = refl
  711. -- And going around the trivial loop (\i -> base) goes around the the
  712. -- non-trivial loop (\i -> loop) zero times.
  713. windingBase : Path (winding (\i -> base)) (pos zero)
  714. windingBase = refl
  715. goAround : Int -> Path base base
  716. goAround =
  717. \case
  718. pos n -> forwards n
  719. neg n -> backwards n
  720. where
  721. forwards : Nat -> Path base base
  722. forwards = \case
  723. zero -> refl
  724. succ n -> trans (forwards n) (\i -> loop i)
  725. backwards : Nat -> Path base base
  726. backwards = \case
  727. zero -> \i -> loop (inot i)
  728. succ n -> trans (backwards n) (\i -> loop (inot i))
  729. -- One particularly general higher inductive type is the homotopy pushout,
  730. -- which can be seen as a kind of sum B + C with the extra condition that
  731. -- whenever x and y are in the image of f (resp. g), inl x ≡ inr y.
  732. data Pushout {A : Type} {B : Type} {C : Type} (f : A -> B) (g : A -> C) : Type where
  733. inl : (x : B) -> Pushout f g
  734. inr : (y : C) -> Pushout f g
  735. push i : (a : A) -> Pushout f g [ (i = i0) -> inl (f a), (i = i1) -> inr (g a) ]
  736. -- The name is due to the category-theoretical notion of pushout.
  737. -- TODO: finish writing this tomorrow lol
  738. data Susp (A : Type) : Type where
  739. north : Susp A
  740. south : Susp A
  741. merid i : A -> Susp A [ (i = i0) -> north, (i = i1) -> south ]
  742. data Unit : Type where
  743. tt : Unit
  744. unitEta : (x : Unit) -> Path x tt
  745. unitEta = \case tt -> refl
  746. unitContr : isContr Unit
  747. unitContr = (tt, \x -> sym (unitEta x))
  748. poSusp : Type -> Type
  749. poSusp A = Pushout {A} {Unit} {Unit} (\x -> tt) (\x -> tt)
  750. Susp_is_poSusp : {A : Type} -> Path (Susp A) (poSusp A)
  751. Susp_is_poSusp {A} = univalence (IsoToEquiv (Susp_to_poSusp {A}, poSusp_to_Susp {A}, poSusp_to_Susp_to_poSusp {A}, Susp_to_poSusp_to_Susp {A})) where
  752. poSusp_to_Susp : {A : Type} -> poSusp A -> Susp A
  753. poSusp_to_Susp = \case
  754. inl x -> north
  755. inr x -> south
  756. push x i -> merid x i
  757. Susp_to_poSusp : {A : Type} -> Susp A -> poSusp A
  758. Susp_to_poSusp = \case
  759. north -> inl tt
  760. south -> inr tt
  761. merid x i -> push x i
  762. Susp_to_poSusp_to_Susp : {A : Type} -> (x : Susp A) -> Path (poSusp_to_Susp (Susp_to_poSusp x)) x
  763. Susp_to_poSusp_to_Susp = \case
  764. north -> refl
  765. south -> refl
  766. merid x i -> refl
  767. poSusp_to_Susp_to_poSusp : {A : Type} -> (x : poSusp A) -> Path (Susp_to_poSusp (poSusp_to_Susp x)) x
  768. poSusp_to_Susp_to_poSusp {A} = \case
  769. inl x -> cong inl (sym (unitEta x))
  770. inr x -> cong inr (sym (unitEta x))
  771. push x i -> refl
  772. data T2 : Type where
  773. baseT : T2
  774. pathOne i : T2 [ (i = i0) -> baseT, (i = i1) -> baseT ]
  775. pathTwo i : T2 [ (i = i0) -> baseT, (i = i1) -> baseT ]
  776. square i j : T2 [
  777. (j = i0) -> pathTwo i,
  778. (j = i1) -> pathTwo i,
  779. (i = i0) -> pathOne j,
  780. (i = i1) -> pathOne j
  781. ]
  782. TorusIsTwoCircles : Path T2 (S1 * S1)
  783. TorusIsTwoCircles = univalence (IsoToEquiv theIso) where
  784. torusToCircs : T2 -> S1 * S1
  785. torusToCircs = \case
  786. baseT -> (base, base)
  787. pathOne i -> (loop i, base)
  788. pathTwo i -> (base, loop i)
  789. square i j -> (loop i, loop j)
  790. circsToTorus : (S1 * S1) -> T2
  791. circsToTorus pair = go pair.1 pair.2
  792. where
  793. baseCase : S1 -> T2
  794. baseCase = \case
  795. base -> baseT
  796. loop j -> pathTwo j
  797. loopCase : Path baseCase baseCase
  798. loopCase i = \case
  799. base -> pathOne i
  800. loop j -> square i j
  801. go : S1 -> S1 -> T2
  802. go = \case
  803. base -> baseCase
  804. loop i -> loopCase i
  805. torusToCircsToTorus : (x : T2) -> Path (circsToTorus (torusToCircs x)) x
  806. torusToCircsToTorus = \case
  807. baseT -> refl
  808. pathOne i -> refl
  809. pathTwo i -> refl
  810. square i j -> refl
  811. circsToTorusToCircs : (p : S1 * S1) -> Path (torusToCircs (circsToTorus p)) p
  812. circsToTorusToCircs pair = go pair.1 pair.2 where
  813. baseCase : (y : S1) -> Path (torusToCircs (circsToTorus (base, y))) (base, y)
  814. baseCase = \case
  815. base -> refl
  816. loop j -> refl
  817. loopCase : (i : I) (y : S1) -> Path (torusToCircs (circsToTorus (loop i, y))) (loop i, y )
  818. loopCase i = \case
  819. base -> refl
  820. loop j -> refl
  821. go : (x : S1) (y : S1) -> Path (torusToCircs (circsToTorus (x, y))) (x, y)
  822. go = \case
  823. base -> baseCase
  824. loop i -> loopCase i
  825. theIso : Iso T2 (S1 * S1)
  826. theIso = (torusToCircs, circsToTorus, circsToTorusToCircs, torusToCircsToTorus)
  827. abs : Int -> Nat
  828. abs = \case
  829. pos n -> n
  830. neg n -> succ n
  831. sign : Int -> Bool
  832. sign = \case
  833. pos n -> true
  834. neg n -> false
  835. boolAnd : Bool -> Bool -> Bool
  836. boolAnd = \case
  837. true -> \case
  838. true -> true
  839. false -> false
  840. false -> \case
  841. true -> false
  842. false -> false
  843. plusNat : Nat -> Nat -> Nat
  844. plusNat = \case
  845. zero -> \x -> x
  846. succ n -> \x -> succ (plusNat n x)
  847. plusZero : (x : Nat) -> Path (plusNat zero x) x
  848. plusZero = \case
  849. zero -> refl
  850. succ n -> \i -> succ (plusZero n i)
  851. multNat : Nat -> Nat -> Nat
  852. multNat = \case
  853. zero -> \x -> zero
  854. succ n -> \x -> plusNat x (multNat n x)
  855. multInt : Int -> Int -> Int
  856. multInt x y = signify (multNat (abs x) (abs y)) (boolAnd (sign x) (sign y)) where
  857. signify : Nat -> Bool -> Int
  858. signify = \case
  859. zero -> \x -> pos zero
  860. succ n -> \case
  861. true -> pos (succ n)
  862. false -> neg n
  863. two : Int
  864. two = pos (succ (succ zero))
  865. four : Int
  866. four = multInt two two
  867. sixteen : Int
  868. sixteen = multInt four four
  869. Prop : Type
  870. Prop = (A : Type) * isProp A
  871. data Sq (A : Type) : Type where
  872. inc : A -> Sq A
  873. sq i : (x : Sq A) (y : Sq A) -> Sq A [ (i = i0) -> x, (i = i1) -> y ]
  874. isProp_isSet : {A : Type} -> isProp A -> isHSet A
  875. isProp_isSet h a b p q j i =
  876. hcomp {A}
  877. (\k [ (i = i0) -> h a a k
  878. , (i = i1) -> h a b k
  879. , (j = i0) -> h a (p i) k
  880. , (j = i1) -> h a (q i) k
  881. ])
  882. (inS a)
  883. isProp_isProp : {A : Type} -> isProp (isProp A)
  884. isProp_isProp f g i a b = isProp_isSet f a b (f a b) (g a b) i
  885. sigmaPath : {A : Type} {B : A -> Type} {s1 : (x : A) * B x} {s2 : (x : A) * B x}
  886. -> (p : Path s1.1 s2.1)
  887. -> PathP (\i -> B (p i)) s1.2 s2.2
  888. -> Path s1 s2
  889. sigmaPath p q i = (p i, q i)
  890. propExt : {A : Type} {B : Type}
  891. -> isProp A -> isProp B
  892. -> (A -> B)
  893. -> (B -> A)
  894. -> Equiv A B
  895. propExt {A} {B} propA propB f g = (f, contract) where
  896. contract : (y : B) -> isContr (fiber f y)
  897. contract y =
  898. let arg : A
  899. arg = g y
  900. in ( (arg, propB y (f arg))
  901. , \fib -> sigmaPath (propA _ _) (isProp_isSet {B} propB y (f fib.1) _ _))
  902. Sq_rec : {A : Type} {B : Type}
  903. -> isProp B
  904. -> (f : A -> B)
  905. -> Sq A -> B
  906. Sq_rec prop f =
  907. \case
  908. inc x -> f x
  909. sq x y i -> prop (work x) (work y) i
  910. where
  911. work : Sq A -> B
  912. work = \case
  913. inc x -> f x
  914. hitTranspExample : Path (inc false) (inc true)
  915. hitTranspExample i = transp (\i -> Sq (notp i)) (sq (inc true) (inc false) i)
  916. data S2 : Type where
  917. base2 : S2
  918. surf2 i j : S2 [ (i = i0) -> base2, (i = i1) -> base2, (j = i0) -> base2, (j = i1) -> base2]
  919. S2IsSuspS1 : Path S2 (Susp S1)
  920. S2IsSuspS1 = univalence (IsoToEquiv iso) where
  921. toS2 : Susp S1 -> S2
  922. toS2 = \case { north -> base2; south -> base2; merid x i -> sphMerid x i } where
  923. sphMerid = \case
  924. base -> \i -> base2
  925. loop j -> \i -> surf2 i j
  926. suspSurf : I -> I -> I -> Susp S1
  927. suspSurf i j = hfill {Susp S1} (\k [ (i = i0) -> north
  928. , (i = i1) -> merid base (inot k)
  929. , (j = i0) -> merid base (iand (inot k) i)
  930. , (j = i1) -> merid base (iand (inot k) i)
  931. ])
  932. (inS (merid (loop j) i))
  933. fromS2 : S2 -> Susp S1
  934. fromS2 = \case { base2 -> north; surf2 i j -> suspSurf i j i1 }
  935. toFromS2 : (x : S2) -> Path (toS2 (fromS2 x)) x
  936. toFromS2 = \case { base2 -> refl; surf2 i j -> \k -> toS2 (suspSurf i j (inot k)) }
  937. fromToS2 : (x : Susp S1) -> Path (fromS2 (toS2 x)) x
  938. fromToS2 = \case { north -> refl; south -> \i -> merid base i; merid x i -> meridCase i x } where
  939. meridCase : (i : I) (x : S1) -> Path (fromS2 (toS2 (merid x i))) (merid x i)
  940. meridCase i = \case
  941. base -> \k -> merid base (iand i k)
  942. loop j -> \k -> suspSurf i j (inot k)
  943. iso : Iso S2 (Susp S1)
  944. iso = (fromS2, toS2, fromToS2, toFromS2)
  945. data S3 : Type where
  946. base3 : S3
  947. surf3 i j k : S3 [ (i = i0) -> base3, (i = i1) -> base3, (j = i0) -> base3, (j = i1) -> base3, (k = i0) -> base3, (k = i1) -> base3 ]
  948. S3IsSuspS2 : Path S3 (Susp S2)
  949. S3IsSuspS2 = univalence (IsoToEquiv iso) where
  950. toS3 : Susp S2 -> S3
  951. toS3 = \case { north -> base3; south -> base3; merid x i -> sphMerid x i } where
  952. sphMerid = \case
  953. base2 -> \i -> base3
  954. surf2 j k -> \i -> surf3 i j k
  955. suspSurf : I -> I -> I -> I -> Susp S2
  956. suspSurf i j k = hfill {Susp S2} (\l [ (i = i0) -> north
  957. , (i = i1) -> merid base2 (inot l)
  958. , (j = i0) -> merid base2 (iand (inot l) i)
  959. , (j = i1) -> merid base2 (iand (inot l) i)
  960. , (k = i0) -> merid base2 (iand (inot l) i)
  961. , (k = i1) -> merid base2 (iand (inot l) i)
  962. ])
  963. (inS (merid (surf2 j k) i))
  964. fromS3 : S3 -> Susp S2
  965. fromS3 = \case { base3 -> north; surf3 i j k -> suspSurf i j k i1 }
  966. toFromS3 : (x : S3) -> Path (toS3 (fromS3 x)) x
  967. toFromS3 = \case { base3 -> refl; surf3 i j k -> \l -> toS3 (suspSurf i j k (inot l)) }
  968. fromToS3 : (x : Susp S2) -> Path (fromS3 (toS3 x)) x
  969. fromToS3 = \case { north -> refl; south -> \i -> merid base2 i; merid x i -> meridCase i x } where
  970. meridCase : (i : I) (x : S2) -> Path (fromS3 (toS3 (merid x i))) (merid x i)
  971. meridCase i = \case
  972. base2 -> \k -> merid base2 (iand i k)
  973. surf2 j k -> \l -> suspSurf i j k (inot l)
  974. iso : Iso S3 (Susp S2)
  975. iso = (fromS3, toS3, fromToS3, toFromS3)
  976. ap_s : {A : Pretype} {B : Pretype} (f : A -> B) {x : A} {y : A} -> Eq_s x y -> Eq_s (f x) (f y)
  977. ap_s {A} {B} f {x} {y} = J_s (\y p -> Eq_s (f x) (f y)) refl_s
  978. subst_s : {A : Pretype} (P : A -> Pretype) {x : A} {y : A} -> Eq_s x y -> P x -> P y
  979. subst_s {A} P {x} {z} p px = J_s {A} {x} (\y p -> P x -> P y) id p px
  980. sym_s : {A : Pretype} {x : A} {y : A} -> Eq_s x y -> Eq_s y x
  981. sym_s {A} {x} {y} = J_s {A} {x} (\y p -> Eq_s y x) refl_s
  982. UIP : {A : Pretype} {x : A} {y : A} (p : Eq_s x y) (q : Eq_s x y) -> Eq_s p q
  983. UIP {A} {x} {y} p q = J_s (\y p -> (q : Eq_s x y) -> Eq_s p q) (uipRefl A x) p q where
  984. uipRefl : (A : Pretype) (x : A) (p : Eq_s x x) -> Eq_s refl_s p
  985. uipRefl A x p = K_s {A} {x} (\q -> Eq_s refl_s q) refl_s p
  986. strictEq_pathEq : {A : Type} {x : A} {y : A} -> Eq_s x y -> Path x y
  987. strictEq_pathEq {A} {x} {y} eq = J_s {A} {x} (\y p -> Path x y) (\i -> x) {y} eq
  988. seq_pathRefl : {A : Type} {x : A} (p : Eq_s x x) -> Eq_s (strictEq_pathEq p) (refl {A} {x})
  989. seq_pathRefl {A} {x} p = K_s (\p -> Eq_s (strictEq_pathEq {A} {x} {x} p) (refl {A} {x})) refl_s p
  990. Path_nat_strict_nat : (x : Nat) (y : Nat) -> Path x y -> Eq_s x y
  991. Path_nat_strict_nat = \case { zero -> zeroCase; succ x -> succCase x } where
  992. zeroCase : (y : Nat) -> Path zero y -> Eq_s zero y
  993. zeroCase = \case
  994. zero -> \p -> refl_s
  995. succ x -> \p -> absurd (zeroNotSucc p)
  996. succCase : (x : Nat) (y : Nat) -> Path (succ x) y -> Eq_s (succ x) y
  997. succCase x = \case
  998. zero -> \p -> absurd (zeroNotSucc (sym p))
  999. succ y -> \p -> ap_s succ (Path_nat_strict_nat x y (succInj p))
  1000. pathToEqS_K : {A : Type} {x : A}
  1001. -> (s : {x : A} {y : A} -> Path x y -> Eq_s x y)
  1002. -> (P : Path x x -> Type) -> P refl -> (p : Path x x) -> P p
  1003. pathToEqS_K {A} {x} p_to_s P pr loop = transp (\i -> P (inv x loop i)) psloop where
  1004. psloop : P (strictEq_pathEq (p_to_s loop))
  1005. psloop = K_s (\l -> P (strictEq_pathEq {A} {x} {x} l)) pr (p_to_s {x} {x} loop)
  1006. inv : (y : A) (l : Path x y) -> Path (strictEq_pathEq (p_to_s l)) l
  1007. inv y l = J {A} {x} (\y l -> Path (strictEq_pathEq (p_to_s l)) l) (strictEq_pathEq aux) {y} l where
  1008. aux : Eq_s (strictEq_pathEq (p_to_s (\i -> x))) (\i -> x)
  1009. aux = seq_pathRefl (p_to_s (\i -> x))
  1010. pathToEq_isSet : {A : Type} -> ({x : A} {y : A} -> Path x y -> Eq_s x y) -> isHSet A
  1011. pathToEq_isSet {A} p_to_s = axK_to_isSet {A} (\{x} -> pathToEqS_K {A} {x} p_to_s) where
  1012. axK_to_isSet : {A : Type} -> ({x : A} -> (P : Path x x -> Type) -> P refl -> (p : Path x x) -> P p) -> isHSet A
  1013. axK_to_isSet K x y p q = J (\y p -> (q : Path x y) -> Path p q) (uipRefl x) p q where
  1014. uipRefl : (x : A) (p : Path x x) -> Path refl p
  1015. uipRefl x p = K {x} (\q -> Path refl q) refl p
  1016. Nat_isSet : isHSet Nat
  1017. Nat_isSet = pathToEq_isSet {Nat} (\{x} {y} -> Path_nat_strict_nat x y)
  1018. Bool_isSet : isHSet Bool
  1019. Bool_isSet = pathToEq_isSet {Bool} (\{x} {y} -> p x y) where
  1020. p : (x : Bool) (y : Bool) -> Path x y -> Eq_s x y
  1021. p = \case
  1022. true -> \case
  1023. true -> \p -> refl_s
  1024. false -> \p -> absurd (trueNotFalse p)
  1025. false -> \case
  1026. false -> \p -> refl_s
  1027. true -> \p -> absurd (trueNotFalse (sym p))
  1028. equivCtr : {A : Type} {B : Type} (e : Equiv A B) (y : B) -> fiber e.1 y
  1029. equivCtr e y = (e.2 y).1
  1030. equivCtrPath : {A : Type} {B : Type} (e : Equiv A B) (y : B)
  1031. -> (v : fiber e.1 y) -> Path (equivCtr e y) v
  1032. equivCtrPath e y = (e.2 y).2
  1033. contr : {A : Type} {phi : I} -> isContr A -> (u : Partial phi A) -> Sub A phi u
  1034. contr {A} {phi} p u = primComp (\i -> A) (\i [ (phi = i1) as c -> p.2 (u c) i ]) (inS p.1)
  1035. contr' : {A : Type} -> ({phi : I} -> (u : Partial phi A) -> Sub A phi u) -> isContr A
  1036. contr' {A} contr = (x, \y i -> outS (contr (\ [ (i = i0) -> x, (i = i1) -> y ])) ) where
  1037. x : A
  1038. x = outS (contr (\ []))
  1039. leftIsOne : {a : I} {b : I} -> Eq_s a i1 -> Eq_s (ior a b) i1
  1040. leftIsOne {a} {b} p = J_s {I} {i1} (\i p -> IsOne (ior i b)) refl_s (sym_s p)
  1041. rightIsOne : {a : I} {b : I} -> Eq_s b i1 -> Eq_s (ior a b) i1
  1042. rightIsOne {a} {b} p = J_s {I} {i1} (\i p -> IsOne (ior a i)) refl_s (sym_s p)
  1043. bothAreOne : {a : I} {b : I} -> Eq_s a i1 -> Eq_s b i1 -> Eq_s (iand a b) i1
  1044. bothAreOne {a} {b} p q = J_s {I} {i1} (\i p -> IsOne (iand i b)) q (sym_s p)
  1045. S1Map_to_baseLoop : {X : Type} -> (S1 -> X) -> (a : X) * Path a a
  1046. S1Map_to_baseLoop {X} f = (f base, \i -> f (loop i))
  1047. baseLoop_to_S1Map : {X : Type} -> ((a : X) * Path a a) -> S1 -> X
  1048. baseLoop_to_S1Map {X} p = \case
  1049. base -> p.1
  1050. loop i -> p.2 i
  1051. S1_univ : {X : Type} -> Path (S1 -> X) ((a : X) * Path a a)
  1052. S1_univ = IsoToId {S1 -> X} {(a : X) * Path a a} (S1Map_to_baseLoop, baseLoop_to_S1Map, ret, sec) where
  1053. to = S1Map_to_baseLoop
  1054. fro = baseLoop_to_S1Map
  1055. sec : {X : Type} -> (f : S1 -> X) -> Path (fro (to f)) f
  1056. sec {X} f = funext {S1} {\s -> X} {\x -> fro (to f) x} {f} h where
  1057. h : (x : S1) -> Path (fro (to f) x) (f x)
  1058. h = \case
  1059. base -> refl
  1060. loop i -> refl
  1061. ret : {X : Type} -> (x : (a : X) * Path a a) -> Path (to (fro x)) x
  1062. ret x = refl
  1063. -- HoTT book lemma 8.9.1
  1064. encodeDecode : {A : Type} {a0 : A}
  1065. -> (code : A -> Type)
  1066. -> (c0 : code a0)
  1067. -> (decode : (x : A) -> code x -> (Path a0 x))
  1068. -> ((c : code a0) -> Path (transp (\i -> code (decode a0 c i)) c0) c)
  1069. -> Path (decode a0 c0) refl
  1070. -> Path (Path a0 a0) (code a0)
  1071. encodeDecode code c0 decode encDec based = IsoToId (encode {a0}, decode _, encDec, decEnc) where
  1072. encode : {x : A} -> Path a0 x -> code x
  1073. encode alpha = transp (\i -> code (alpha i)) c0
  1074. encodeRefl : Path (encode refl) c0
  1075. encodeRefl = sym (transpFill {\i -> code a0} c0)
  1076. decEnc : {x : A} (p : Path a0 x) -> Path (decode _ (encode p)) p
  1077. decEnc p = J (\x p -> Path (decode _ (encode p)) p) q p where
  1078. q : Path (decode _ (encode refl)) refl
  1079. q = transp (\i -> Path (decode _ (encodeRefl (inot i))) refl) based
  1080. S1_elim : (P : S1 -> Type)
  1081. -> (pb : P base)
  1082. -> PathP (\i -> P (loop i)) pb pb
  1083. -> (x : S1) -> P x
  1084. S1_elim P pb pq = \case
  1085. base -> pb
  1086. loop i -> pq i
  1087. PathP_is_Path : (P : I -> Type) (p : P i0) (q : P i1) -> Path (PathP P p q) (Path {P i1} (transp (\i -> P i) p) q)
  1088. PathP_is_Path P p q i = PathP (\j -> P (ior i j)) (transpFill {\j -> P j} p i) q
  1089. Constant : {A : Type} {B : Type} -> (A -> B) -> Type
  1090. Constant f = (y : B) * (x : A) -> Path y (f x)
  1091. Weakly : {A : Type} {B : Type} -> (A -> B) -> Type
  1092. Weakly f = (x : A) (y : A) -> Path (f x) (f y)
  1093. Conditionally : {A : Type} {B : Type} -> (A -> B) -> Type
  1094. Conditionally f = (f' : Sq A -> B) * Path f (\x -> f' (inc x))
  1095. Constant_weakly : {A : Type} {B : Type} (f : A -> B) -> Constant f -> Weakly f
  1096. Constant_weakly f p x y = trans (sym (p.2 x)) (p.2 y)
  1097. Constant_conditionally : {A : Type} {B : Type} -> (f : A -> B) -> Constant f -> Conditionally f
  1098. Constant_conditionally {A} {B} f p = transp (\i -> Conditionally (c_const (inot i))) (const_c p.1) where
  1099. c_const : Path f (\y -> p.1)
  1100. c_const i x = p.2 x (inot i)
  1101. const_c : (y : B) -> Conditionally {A} (\x -> y)
  1102. const_c y = (\x -> y, refl)
  1103. S1_connected : (f : S1 -> Bool) -> Constant f
  1104. S1_connected f = (f'.1, p) where
  1105. f' : (x : Bool) * Path x x
  1106. f' = S1Map_to_baseLoop f
  1107. p : (y : S1) -> Path f'.1 (f y)
  1108. p = S1_elim P refl loopc where
  1109. P : S1 -> Type
  1110. P = \y -> Path f'.1 (f y)
  1111. rr = refl {Bool} {f base}
  1112. loopc : PathP (\i -> P (loop i)) rr rr
  1113. loopc = transp (\i -> PathP_is_Path (\i -> P (loop i)) rr rr (inot i))
  1114. (Bool_isSet _ _ rr (transp (\i -> P (loop i)) rr))
  1115. isProp_isEquiv : {A : Type} {B : Type} {f : A -> B} -> isProp (isEquiv f)
  1116. isProp_isEquiv {f} p q i y =
  1117. let
  1118. p2 = (p y).2
  1119. q2 = (q y).2
  1120. in
  1121. ( p2 (q y).1 i
  1122. , \w j -> hcomp (\k [ (i = i0) -> p2 w j
  1123. , (i = i1) -> q2 w (ior j (inot k))
  1124. , (j = i0) -> p2 (q2 w (inot k)) i
  1125. , (j = i1) -> w ])
  1126. (inS (p2 w (ior i j)))
  1127. )
  1128. isProp_EqvSq : {P : Type} (x : Equiv P (Sq P)) (y : Equiv P (Sq P)) -> Path x y
  1129. isProp_EqvSq x y = sigmaPath x1_is_y1 (isProp_isEquiv {P} {Sq P} {y.1} (transp (\i -> isEquiv (x1_is_y1 i)) x.2) y.2) where
  1130. x1_is_y1 : Path x.1 y.1
  1131. x1_is_y1 i e = sq (x.1 e) (y.1 e) i
  1132. equivLemma : {A : Type} {B : Type} {e : Equiv A B} {e' : Equiv A B}
  1133. -> Path e.1 e'.1
  1134. -> Path e e'
  1135. equivLemma {A} {B} {e} {e'} p = sigmaPath p (isProp_isEquiv {A} {B} {e'.1} (transp (\i -> isEquiv (p i)) e.2) e'.2)
  1136. isProp_to_Sq_equiv : {P : Type} -> isProp P -> Equiv P (Sq P)
  1137. isProp_to_Sq_equiv {P} prop = propExt prop sqProp inc proj where
  1138. proj : Sq P -> P
  1139. proj = Sq_rec prop (\x -> x)
  1140. sqProp : isProp (Sq P)
  1141. sqProp x y i = sq x y i
  1142. Sq_equiv_to_isProp : {P : Type} -> Equiv P (Sq P) -> isProp P
  1143. Sq_equiv_to_isProp eqv = transp (\i -> isProp (univalence eqv (inot i))) (\x y i -> sq x y i)
  1144. exercise_3_21 : {P : Type} -> Equiv (isProp P) (Equiv P (Sq P))
  1145. exercise_3_21 {P} = propExt (isProp_isProp {P}) (isProp_EqvSq {P}) isProp_to_Sq_equiv Sq_equiv_to_isProp
  1146. uaBeta : {A : Type} {B : Type} (e : Equiv A B) -> Path (idToEquiv (univalence e)).1 e.1
  1147. uaBeta {A} {B} e i a = univalenceBeta e i a
  1148. uaret : {A : Type} {B : Type} -> retract {Equiv A B} {Path A B} (univalence {A} {B}) (idToEquiv {A} {B})
  1149. uaret eqv = equivLemma (uaBeta eqv)
  1150. f1 : {A : Type} -> (p : (B : Type) * Equiv A B) -> (B : Type) * Path A B
  1151. f1 {A} p = (p.1, univalence p.2)
  1152. f2 : {A : Type} -> (p : (B : Type) * Path A B) -> (B : Type) * Equiv A B
  1153. f2 {A} p = (p.1, idToEquiv p.2)
  1154. uaretSig : {A : Type} (a : (B : Type) * Equiv A B) -> Path (f2 (f1 a)) a
  1155. uaretSig {A} p = sigmaPath (\i -> p.1) (uaret {A} {p.1} p.2)
  1156. retContr : {A : Type} {B : Type}
  1157. -> (f : A -> B) -> (g : B -> A)
  1158. -> (h : retract f g)
  1159. -> isContr B -> isContr A
  1160. retContr {A} {B} f g h v = (g b, p) where
  1161. b = v.1
  1162. p : (x : A) -> Path (g b) x
  1163. p x i = comp (\i -> A) (\j [ (i = i0) -> g b, (i = i1) -> h x j ]) (inS (g (v.2 (f x) i)))