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.

1266 lines
43 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. transpFill : {A : I -> Type} (x : A i0) -> PathP A x (transp (\i -> A i) x)
  230. transpFill {A} x i = fill (\i -> A i) (\k []) (inS x) i
  231. -- Reduction of composition
  232. ---------------------------
  233. --
  234. -- Composition reduces on the structure of the family A : I -> Type to create
  235. -- the element a1 : (A i1)[phi -> u i1].
  236. --
  237. -- For instance, when filling a cube of functions, the behaviour is to
  238. -- first transport backwards along the domain, apply the function, then
  239. -- forwards along the codomain.
  240. transpFun : {A : Type} {B : Type} {C : Type} {D : Type} (p : Path A B) (q : Path C D)
  241. -> (f : A -> C) -> Path (transp (\i -> p i -> q i) f)
  242. (\x -> transp (\i -> q i) (f (transp (\i -> p (inot i)) x)))
  243. transpFun p q f = refl
  244. transpDFun : {A : I -> Type} {B : (i : I) -> A i -> Type}
  245. -> (f : (x : A i0) -> B i0 x)
  246. -> Path (transp (\i -> (x : A i) -> B i x) f)
  247. (\x -> transp (\i -> B i (fill (\j -> A (inot j)) (\k []) (inS x) (inot i))) (f (fill (\j -> A (inot j)) (\k []) (inS x) i1)))
  248. transpDFun f = refl
  249. -- When considering the more general case of a composition respecing sides,
  250. -- the outer transport becomes a composition.
  251. -- Glueing and Univalence
  252. -------------------------
  253. -- First, let's get some definitions out of the way.
  254. --
  255. -- The *fiber* of a function f : A -> B at a point y : B is the type of
  256. -- inputs x : A which f takes to y, that is, for which there exists a
  257. -- path f(x) = y.
  258. fiber : {A : Type} {B : Type} -> (A -> B) -> B -> Type
  259. fiber f y = (x : A) * Path y (f x)
  260. -- An *equivalence* is a function where every fiber is contractible.
  261. -- That is, for every point in the codomain y : B, there is exactly one
  262. -- point in the input which f maps to y.
  263. isEquiv : {A : Type} {B : Type} -> (A -> B) -> Type
  264. isEquiv {A} {B} f = (y : B) -> isContr (fiber {A} {B} f y)
  265. -- By extracting this point, which must exist because the fiber is contractible,
  266. -- we can get an inverse of f:
  267. invert : {A : Type} {B : Type} {f : A -> B} -> isEquiv f -> B -> A
  268. invert eqv y = (eqv y) .1 .1
  269. retract : {A : Type} {B : Type} -> (A -> B) -> (B -> A) -> Type
  270. retract {A} {B} f g = (a : A) -> Path (g (f a)) a
  271. contr : {A : Type} {phi : I} -> isContr A -> (u : Partial phi A) -> A
  272. contr {A} {phi} p u = comp (\i -> A) {phi} (\i is1 -> p.2 (u is1) i) (inS (p.1))
  273. -- Proving that it's also a retraction is left as an exercise to the
  274. -- reader. We can package together a function and a proof that it's an
  275. -- equivalence to get a capital-E Equivalence.
  276. Equiv : (A : Type) (B : Type) -> Type
  277. Equiv A B = (f : A -> B) * isEquiv {A} {B} f
  278. -- The identity function is an equivalence between any type A and
  279. -- itself.
  280. idEquiv : {A : Type} -> isEquiv (id {A})
  281. idEquiv y = ((y, \i -> y), \u i -> (u.2 i, \j -> u.2 (iand i j)))
  282. -- The glue operation expresses that "extensibility is invariant under
  283. -- equivalence". Less concisely, the Glue type and its constructor,
  284. -- glue, let us extend a partial element of a partial type to a total
  285. -- element of a total type, by "gluing" the partial type T using a
  286. -- partial equivalence e onto a total type A.
  287. -- In particular, we have that when φ = i1, Glue A [i1 -> (T, f)] = T.
  288. primGlue : (A : Type) {phi : I}
  289. (T : Partial phi Type)
  290. (e : PartialP phi (\o -> Equiv (T o) A))
  291. -> Type
  292. {-# PRIMITIVE Glue primGlue #-}
  293. -- The glue constructor extends the partial element t : T to a total
  294. -- element of Glue A [φ -> (T, e)] as long as we have a total im : A
  295. -- which is the image of f(t).
  296. --
  297. -- Agreeing with the condition that Glue A [i1 -> (T, e)] = T,
  298. -- we have that glue {A} {i1} t im => t.
  299. prim'glue : {A : Type} {phi : I} {T : Partial phi Type} {e : PartialP phi (\o -> Equiv (T o) A)}
  300. -> (t : PartialP phi T)
  301. -> (im : Sub A phi (\o -> (e o).1 (t o)))
  302. -> primGlue A T e
  303. {-# PRIMITIVE glue prim'glue #-}
  304. glue : {A : Type} {phi : I} {Te : Partial phi ((T : Type) * Equiv T A)}
  305. -> (t : PartialP phi (\o -> (Te o).1))
  306. -> (im : Sub A phi (\o -> (Te o).2.1 (t o)))
  307. -> primGlue A {phi} (\o -> (Te o).1) (\o -> (Te o).2)
  308. glue {A} {phi} {Te} t im = prim'glue {A} {phi} {\o -> (Te o).1} {\o -> (Te o).2} t im
  309. -- The unglue operation undoes a glueing. Since when φ = i1,
  310. -- Glue A [φ -> (T, f)] = T, the argument to primUnglue {A} {i1} ...
  311. -- will have type T, and so to get back an A we need to apply the
  312. -- partial equivalence f (defined everywhere).
  313. primUnglue : {A : Type} {phi : I} {T : Partial phi Type} {e : PartialP phi (\o -> Equiv (T o) A)}
  314. -> primGlue A {phi} T e -> A
  315. {-# PRIMITIVE unglue primUnglue #-}
  316. unglue : {A : Type} (phi : I) {Te : Partial phi ((T : Type) * Equiv T A)}
  317. -> primGlue A {phi} (\o -> (Te o).1) (\o -> (Te o).2) -> A
  318. unglue {A} phi {Te} = primUnglue {A} {phi} {\o -> (Te o).1} {\o -> (Te o).2}
  319. -- Diagramatically, i : I |- Glue A [(i \/ ~i) -> (T, e)] can be drawn
  320. -- as giving us the dotted line in:
  321. --
  322. -- T i0 ......... T i1
  323. -- | |
  324. -- | |
  325. -- e i0 |~ ~| e i1
  326. -- | |
  327. -- | |
  328. -- A i0 --------- A i1
  329. -- A
  330. --
  331. -- Where the the two "e" sides are equivalences, and the Bottom side is
  332. -- the line i : I |- A.
  333. --
  334. -- Thus, by choosing a base type, a set of partial types and partial
  335. -- equivalences, we can make a line between two types (T i0) and (T i1).
  336. Glue : (A : Type) {phi : I} -> Partial phi ((X : Type) * Equiv X A) -> Type
  337. Glue A {phi} u = primGlue A {phi} (\o -> (u o).1) (\o -> (u o).2)
  338. -- For example, we can glue together the type A and the type B as long
  339. -- as there exists an Equiv A B.
  340. --
  341. -- A ............ B
  342. -- | |
  343. -- | |
  344. -- equiv |~ ua equiv ~| idEquiv {B}
  345. -- | |
  346. -- | |
  347. -- B ------------ B
  348. -- \i → B
  349. --
  350. univalence : {A : Type} {B : Type} -> Equiv A B -> Path A B
  351. univalence {A} {B} equiv i =
  352. Glue B (\[ (i = i0) -> (A, equiv),
  353. (i = i1) -> (B, the B, idEquiv {B}) ])
  354. lineToEquiv : (A : I -> Type) -> Equiv (A i0) (A i1)
  355. {-# PRIMITIVE lineToEquiv #-}
  356. idToEquiv : {A : Type} {B : Type} -> Path A B -> Equiv A B
  357. idToEquiv p = lineToEquiv (\i -> p i)
  358. -- The fact that this diagram has 2 filled-in B sides explains the
  359. -- complication in the proof below.
  360. --
  361. -- In particular, the actual behaviour of transp (\i -> univalence f i)
  362. -- (x : A) is not just to apply f x to get a B (the left side), it also
  363. -- needs to:
  364. --
  365. -- * For the Bottom side, compose along (\i -> B) (the Bottom side)
  366. -- * For the right side, apply the inverse of the identity, which
  367. -- is just identity, to get *some* b : B
  368. --
  369. -- But that b : B might not agree with the sides of the composition
  370. -- operation in a more general case, so it composes along (\i -> B)
  371. -- *again*!
  372. --
  373. -- Thus the proof: a simple cubical argument suffices, since
  374. -- for any composition, its filler connects either endpoints. So
  375. -- we need to come up with a filler for the Bottom and right faces.
  376. univalenceBeta : {A : Type} {B : Type} (f : Equiv A B) -> Path (transp (\i -> univalence f i)) f.1
  377. univalenceBeta {A} {B} f i a =
  378. let
  379. -- The Bottom left corner
  380. botLeft : B
  381. botLeft = transp (\i -> B) (f.1 a)
  382. -- The "Bottom face" filler connects the Bottom-left corner, f.1 a,
  383. -- and the Bottom-right corner, which is the transport of f.1 a
  384. -- along \i -> B.
  385. botFace : Path (f.1 a) botLeft
  386. botFace i = fill (\i -> B) (\j []) (inS (f.1 a)) i
  387. -- The "right face" filler connects the Bottom-right corner and the
  388. -- upper-right corner, which is again a simple transport along
  389. -- \i -> B.
  390. rightFace : Path (transp (\i -> B) botLeft) botLeft
  391. rightFace i = fill (\i -> B) (\j []) (inS botLeft) (inot i)
  392. -- The goal is a path between the Bottom-left and top-right corners,
  393. -- which we can get by composing (in the path sense) the Bottom and
  394. -- right faces.
  395. goal : Path (transp (\i -> B) botLeft) (f.1 a)
  396. goal = trans rightFace (\i -> botFace (inot i))
  397. in goal i
  398. -- The terms univalence + univalenceBeta suffice to prove the "full"
  399. -- univalence axiom of Voevodsky, as can be seen in the paper
  400. --
  401. -- Ian Orton, & Andrew M. Pitts. (2017). Decomposing the Univalence Axiom.
  402. --
  403. -- Available freely here: https://arxiv.org/abs/1712.04890v3
  404. J : {A : Type} {x : A}
  405. (P : (y : A) -> Path x y -> Type)
  406. (d : P x (\i -> x))
  407. {y : A} (p : Path x y)
  408. -> P y p
  409. J P d p = transp (\i -> P (p i) (\j -> p (iand i j))) d
  410. -- Isomorphisms
  411. ---------------
  412. --
  413. -- Since isomorphisms are a much more convenient notion of equivalence
  414. -- than contractible fibers, it's natural to ask why the CCHM paper, and
  415. -- this implementation following that, decided on the latter for our
  416. -- definition of equivalence.
  417. isIso : {A : Type} -> {B : Type} -> (A -> B) -> Type
  418. isIso {A} {B} f = (g : B -> A) * ((y : B) -> Path (f (g y)) y) * ((x : A) -> Path (g (f x)) x)
  419. -- The reason is that the family of types IsIso is not a proposition!
  420. -- This means that there can be more than one way for a function to be
  421. -- an equivalence. This is Lemma 4.1.1 of the HoTT book.
  422. Iso : Type -> Type -> Type
  423. Iso A B = (f : A -> B) * isIso f
  424. -- Nevertheless, we can prove that any function with an isomorphism
  425. -- structure has contractible fibers, using a cubical argument adapted
  426. -- from CCHM's implementation of cubical type theory:
  427. --
  428. -- https://github.com/mortberg/cubicaltt/blob/master/experiments/isoToEquiv.ctt#L7-L55
  429. IsoToEquiv : {A : Type} {B : Type} -> Iso A B -> Equiv A B
  430. IsoToEquiv {A} {B} iso = (f, \y -> (fCenter y, fIsCenter y)) where
  431. f = iso.1
  432. g = iso.2.1
  433. s = iso.2.2.1
  434. t = iso.2.2.2
  435. lemIso : (y : B) (x0 : A) (x1 : A) (p0 : Path y (f x0)) (p1 : Path y (f x1))
  436. -> PathP (\i -> fiber f y) (x0, p0) (x1, p1)
  437. lemIso y x0 x1 p0 p1 =
  438. let
  439. rem0 : Path x0 (g y)
  440. rem0 i = comp (\i -> A) (\k [ (i = i0) -> t x0 k, (i = i1) -> g y ]) (inS (g (p0 (inot i))))
  441. rem1 : Path x1 (g y)
  442. rem1 i = comp (\i -> A) (\k [ (i = i0) -> t x1 k, (i = i1) -> g y ]) (inS (g (p1 (inot i))))
  443. p : Path x0 x1
  444. p i = comp (\i -> A) (\k [ (i = i0) -> rem0 (inot k), (i = i1) -> rem1 (inot k) ]) (inS (g y))
  445. fill0 : I -> I -> A
  446. fill0 i j = comp (\i -> A) (\k [ (i = i0) -> t x0 (iand j k)
  447. , (i = i1) -> g y
  448. , (j = i0) -> g (p0 (inot i))
  449. ])
  450. (inS (g (p0 (inot i))))
  451. fill1 : I -> I -> A
  452. fill1 i j = comp (\i -> A) (\k [ (i = i0) -> t x1 (iand j k)
  453. , (i = i1) -> g y
  454. , (j = i0) -> g (p1 (inot i)) ])
  455. (inS (g (p1 (inot i))))
  456. fill2 : I -> I -> A
  457. fill2 i j = comp (\i -> A) (\k [ (i = i0) -> rem0 (ior j (inot k))
  458. , (i = i1) -> rem1 (ior j (inot k))
  459. , (j = i1) -> g y ])
  460. (inS (g y))
  461. sq : I -> I -> A
  462. sq i j = comp (\i -> A) (\k [ (i = i0) -> fill0 j (inot k)
  463. , (i = i1) -> fill1 j (inot k)
  464. , (j = i1) -> g y
  465. , (j = i0) -> t (p i) (inot k) ])
  466. (inS (fill2 i j))
  467. sq1 : I -> I -> B
  468. sq1 i j = comp (\i -> B) (\k [ (i = i0) -> s (p0 (inot j)) k
  469. , (i = i1) -> s (p1 (inot j)) k
  470. , (j = i0) -> s (f (p i)) k
  471. , (j = i1) -> s y k
  472. ])
  473. (inS (f (sq i j)))
  474. in \i -> (p i, \j -> sq1 i (inot j))
  475. fCenter : (y : B) -> fiber f y
  476. fCenter y = (g y, sym (s y))
  477. fIsCenter : (y : B) (w : fiber f y) -> Path (fCenter y) w
  478. fIsCenter y w = lemIso y (fCenter y).1 w.1 (fCenter y).2 w.2
  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 -> goAround_nat n
  719. neg n -> sym (goAround_nat (succ n))
  720. where
  721. goAround_nat : Nat -> Path base base
  722. goAround_nat = \case
  723. zero -> refl
  724. succ n -> trans (\i -> loop i) (goAround_nat n)
  725. -- One particularly general higher inductive type is the homotopy pushout,
  726. -- which can be seen as a kind of sum B + C with the extra condition that
  727. -- whenever x and y are in the image of f (resp. g), inl x ≡ inr y.
  728. data Pushout {A : Type} {B : Type} {C : Type} (f : A -> B) (g : A -> C) : Type where
  729. inl : (x : B) -> Pushout f g
  730. inr : (y : C) -> Pushout f g
  731. push i : (a : A) -> Pushout f g [ (i = i0) -> inl (f a), (i = i1) -> inr (g a) ]
  732. -- The name is due to the category-theoretical notion of pushout.
  733. -- TODO: finish writing this tomorrow lol
  734. data Susp (A : Type) : Type where
  735. north : Susp A
  736. south : Susp A
  737. merid i : A -> Susp A [ (i = i0) -> north, (i = i1) -> south ]
  738. data Unit : Type where
  739. tt : Unit
  740. unitEta : (x : Unit) -> Path x tt
  741. unitEta = \case tt -> refl
  742. unitContr : isContr Unit
  743. unitContr = (tt, \x -> sym (unitEta x))
  744. poSusp : Type -> Type
  745. poSusp A = Pushout {A} {Unit} {Unit} (\x -> tt) (\x -> tt)
  746. Susp_is_poSusp : {A : Type} -> Path (Susp A) (poSusp A)
  747. 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
  748. poSusp_to_Susp : {A : Type} -> poSusp A -> Susp A
  749. poSusp_to_Susp = \case
  750. inl x -> north
  751. inr x -> south
  752. push x i -> merid x i
  753. Susp_to_poSusp : {A : Type} -> Susp A -> poSusp A
  754. Susp_to_poSusp = \case
  755. north -> inl tt
  756. south -> inr tt
  757. merid x i -> push x i
  758. Susp_to_poSusp_to_Susp : {A : Type} -> (x : Susp A) -> Path (poSusp_to_Susp (Susp_to_poSusp x)) x
  759. Susp_to_poSusp_to_Susp = \case
  760. north -> refl
  761. south -> refl
  762. merid x i -> refl
  763. poSusp_to_Susp_to_poSusp : {A : Type} -> (x : poSusp A) -> Path (Susp_to_poSusp (poSusp_to_Susp x)) x
  764. poSusp_to_Susp_to_poSusp {A} = \case
  765. inl x -> cong inl (sym (unitEta x))
  766. inr x -> cong inr (sym (unitEta x))
  767. push x i -> refl
  768. data T2 : Type where
  769. baseT : T2
  770. pathOne i : T2 [ (i = i0) -> baseT, (i = i1) -> baseT ]
  771. pathTwo i : T2 [ (i = i0) -> baseT, (i = i1) -> baseT ]
  772. square i j : T2 [
  773. (j = i0) -> pathTwo i,
  774. (j = i1) -> pathTwo i,
  775. (i = i0) -> pathOne j,
  776. (i = i1) -> pathOne j
  777. ]
  778. TorusIsTwoCircles : Path T2 (S1 * S1)
  779. TorusIsTwoCircles = univalence (IsoToEquiv theIso) where
  780. torusToCircs : T2 -> S1 * S1
  781. torusToCircs = \case
  782. baseT -> (base, base)
  783. pathOne i -> (loop i, base)
  784. pathTwo i -> (base, loop i)
  785. square i j -> (loop i, loop j)
  786. circsToTorus : (S1 * S1) -> T2
  787. circsToTorus pair = go pair.1 pair.2
  788. where
  789. baseCase : S1 -> T2
  790. baseCase = \case
  791. base -> baseT
  792. loop j -> pathTwo j
  793. loopCase : Path baseCase baseCase
  794. loopCase i = \case
  795. base -> pathOne i
  796. loop j -> square i j
  797. go : S1 -> S1 -> T2
  798. go = \case
  799. base -> baseCase
  800. loop i -> loopCase i
  801. torusToCircsToTorus : (x : T2) -> Path (circsToTorus (torusToCircs x)) x
  802. torusToCircsToTorus = \case
  803. baseT -> refl
  804. pathOne i -> refl
  805. pathTwo i -> refl
  806. square i j -> refl
  807. circsToTorusToCircs : (p : S1 * S1) -> Path (torusToCircs (circsToTorus p)) p
  808. circsToTorusToCircs pair = go pair.1 pair.2 where
  809. baseCase : (y : S1) -> Path (torusToCircs (circsToTorus (base, y))) (base, y)
  810. baseCase = \case
  811. base -> refl
  812. loop j -> refl
  813. loopCase : (i : I) (y : S1) -> Path (torusToCircs (circsToTorus (loop i, y))) (loop i, y )
  814. loopCase i = \case
  815. base -> refl
  816. loop j -> refl
  817. go : (x : S1) (y : S1) -> Path (torusToCircs (circsToTorus (x, y))) (x, y)
  818. go = \case
  819. base -> baseCase
  820. loop i -> loopCase i
  821. theIso : Iso T2 (S1 * S1)
  822. theIso = (torusToCircs, circsToTorus, circsToTorusToCircs, torusToCircsToTorus)
  823. abs : Int -> Nat
  824. abs = \case
  825. pos n -> n
  826. neg n -> succ n
  827. sign : Int -> Bool
  828. sign = \case
  829. pos n -> true
  830. neg n -> false
  831. boolAnd : Bool -> Bool -> Bool
  832. boolAnd = \case
  833. true -> \case
  834. true -> true
  835. false -> false
  836. false -> \case
  837. true -> false
  838. false -> false
  839. plusNat : Nat -> Nat -> Nat
  840. plusNat = \case
  841. zero -> \x -> x
  842. succ n -> \x -> succ (plusNat n x)
  843. plusZero : (x : Nat) -> Path (plusNat zero x) x
  844. plusZero = \case
  845. zero -> refl
  846. succ n -> \i -> succ (plusZero n i)
  847. multNat : Nat -> Nat -> Nat
  848. multNat = \case
  849. zero -> \x -> zero
  850. succ n -> \x -> plusNat x (multNat n x)
  851. multInt : Int -> Int -> Int
  852. multInt x y = signify (multNat (abs x) (abs y)) (boolAnd (sign x) (sign y)) where
  853. signify : Nat -> Bool -> Int
  854. signify = \case
  855. zero -> \x -> pos zero
  856. succ n -> \case
  857. true -> pos (succ n)
  858. false -> neg n
  859. two : Int
  860. two = pos (succ (succ zero))
  861. four : Int
  862. four = multInt two two
  863. sixteen : Int
  864. sixteen = multInt four four
  865. Prop : Type
  866. Prop = (A : Type) * isProp A
  867. data Sq (A : Type) : Type where
  868. inc : A -> Sq A
  869. sq i : (x : Sq A) (y : Sq A) -> Sq A [ (i = i0) -> x, (i = i1) -> y ]
  870. isProp_isSet : {A : Type} -> isProp A -> isHSet A
  871. isProp_isSet h a b p q j i =
  872. hcomp {A}
  873. (\k [ (i = i0) -> h a a k
  874. , (i = i1) -> h a b k
  875. , (j = i0) -> h a (p i) k
  876. , (j = i1) -> h a (q i) k
  877. ])
  878. (inS a)
  879. isProp_isProp : {A : Type} -> isProp (isProp A)
  880. isProp_isProp f g i a b = isProp_isSet f a b (f a b) (g a b) i
  881. Sq_rec : {A : Type} {B : Type}
  882. -> isProp B
  883. -> (f : A -> B)
  884. -> Sq A -> B
  885. Sq_rec prop f =
  886. \case
  887. inc x -> f x
  888. sq x y i -> prop (work x) (work y) i
  889. where
  890. work : Sq A -> B
  891. work = \case
  892. inc x -> f x
  893. hitTranspExample : Path (inc false) (inc true)
  894. hitTranspExample i = transp (\i -> Sq (notp i)) (sq (inc true) (inc false) i)
  895. data S2 : Type where
  896. base2 : S2
  897. surf2 i j : S2 [ (i = i0) -> base2, (i = i1) -> base2, (j = i0) -> base2, (j = i1) -> base2]
  898. S2IsSuspS1 : Path S2 (Susp S1)
  899. S2IsSuspS1 = univalence (IsoToEquiv iso) where
  900. toS2 : Susp S1 -> S2
  901. toS2 = \case { north -> base2; south -> base2; merid x i -> sphMerid x i } where
  902. sphMerid = \case
  903. base -> \i -> base2
  904. loop j -> \i -> surf2 i j
  905. suspSurf : I -> I -> I -> Susp S1
  906. suspSurf i j = hfill {Susp S1} (\k [ (i = i0) -> north
  907. , (i = i1) -> merid base (inot k)
  908. , (j = i0) -> merid base (iand (inot k) i)
  909. , (j = i1) -> merid base (iand (inot k) i)
  910. ])
  911. (inS (merid (loop j) i))
  912. fromS2 : S2 -> Susp S1
  913. fromS2 = \case { base2 -> north; surf2 i j -> suspSurf i j i1 }
  914. toFromS2 : (x : S2) -> Path (toS2 (fromS2 x)) x
  915. toFromS2 = \case { base2 -> refl; surf2 i j -> refl }
  916. fromToS2 : (x : Susp S1) -> Path (fromS2 (toS2 x)) x
  917. fromToS2 = \case { north -> refl; south -> \i -> merid base i; merid x i -> meridCase i x } where
  918. meridCase : (i : I) (x : S1) -> Path (fromS2 (toS2 (merid x i))) (merid x i)
  919. meridCase i = \case
  920. base -> \k -> merid base (iand i k)
  921. loop j -> \k -> suspSurf i j (inot k)
  922. iso : Iso S2 (Susp S1)
  923. iso = (fromS2, toS2, fromToS2, toFromS2)
  924. data S3 : Type where
  925. base3 : S3
  926. surf3 i j k : S3 [ (i = i0) -> base3, (i = i1) -> base3, (j = i0) -> base3, (j = i1) -> base3, (k = i0) -> base3, (k = i1) -> base3 ]
  927. S3IsSuspS2 : Path S3 (Susp S2)
  928. S3IsSuspS2 = univalence (IsoToEquiv iso) where
  929. toS3 : Susp S2 -> S3
  930. toS3 = \case { north -> base3; south -> base3; merid x i -> sphMerid x i } where
  931. sphMerid = \case
  932. base2 -> \i -> base3
  933. surf2 j k -> \i -> surf3 i j k
  934. suspSurf : I -> I -> I -> I -> Susp S2
  935. suspSurf i j k = hfill {Susp S2} (\l [ (i = i0) -> north
  936. , (i = i1) -> merid base2 (inot l)
  937. , (j = i0) -> merid base2 (iand (inot l) i)
  938. , (j = i1) -> merid base2 (iand (inot l) i)
  939. , (k = i0) -> merid base2 (iand (inot l) i)
  940. , (k = i1) -> merid base2 (iand (inot l) i)
  941. ])
  942. (inS (merid (surf2 j k) i))
  943. fromS3 : S3 -> Susp S2
  944. fromS3 = \case { base3 -> north; surf3 i j k -> suspSurf i j k i1 }
  945. toFromS3 : (x : S3) -> Path (toS3 (fromS3 x)) x
  946. toFromS3 = \case { base3 -> refl; surf3 i j k -> refl }
  947. fromToS3 : (x : Susp S2) -> Path (fromS3 (toS3 x)) x
  948. fromToS3 = \case { north -> refl; south -> \i -> merid base2 i; merid x i -> meridCase i x } where
  949. meridCase : (i : I) (x : S2) -> Path (fromS3 (toS3 (merid x i))) (merid x i)
  950. meridCase i = \case
  951. base2 -> \k -> merid base2 (iand i k)
  952. surf2 j k -> \l -> suspSurf i j k (inot l)
  953. iso : Iso S3 (Susp S2)
  954. iso = (fromS3, toS3, fromToS3, toFromS3)
  955. ap_s : {A : Pretype} {B : Pretype} (f : A -> B) {x : A} {y : A} -> Eq_s x y -> Eq_s (f x) (f y)
  956. ap_s {A} {B} f {x} {y} = J_s (\y p -> Eq_s (f x) (f y)) refl_s
  957. subst_s : {A : Pretype} (P : A -> Pretype) {x : A} {y : A} -> Eq_s x y -> P x -> P y
  958. subst_s {A} P {x} {z} p px = J_s {A} {x} (\y p -> P x -> P y) id p px
  959. sym_s : {A : Pretype} {x : A} {y : A} -> Eq_s x y -> Eq_s y x
  960. sym_s {A} {x} {y} = J_s {A} {x} (\y p -> Eq_s y x) refl_s
  961. UIP : {A : Pretype} {x : A} {y : A} (p : Eq_s x y) (q : Eq_s x y) -> Eq_s p q
  962. 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
  963. uipRefl : (A : Pretype) (x : A) (p : Eq_s x x) -> Eq_s refl_s p
  964. uipRefl A x p = K_s {A} {x} (\q -> Eq_s refl_s q) refl_s p
  965. strictEq_pathEq : {A : Type} {x : A} {y : A} -> Eq_s x y -> Path x y
  966. strictEq_pathEq {A} {x} {y} eq = J_s {A} {x} (\y p -> Path x y) (\i -> x) {y} eq
  967. seq_pathRefl : {A : Type} {x : A} (p : Eq_s x x) -> Eq_s (strictEq_pathEq p) (refl {A} {x})
  968. seq_pathRefl {A} {x} p = K_s (\p -> Eq_s (strictEq_pathEq {A} {x} {x} p) (refl {A} {x})) refl_s p
  969. Path_nat_strict_nat : (x : Nat) (y : Nat) -> Path x y -> Eq_s x y
  970. Path_nat_strict_nat = \case { zero -> zeroCase; succ x -> succCase x } where
  971. zeroCase : (y : Nat) -> Path zero y -> Eq_s zero y
  972. zeroCase = \case
  973. zero -> \p -> refl_s
  974. succ x -> \p -> absurd (zeroNotSucc p)
  975. succCase : (x : Nat) (y : Nat) -> Path (succ x) y -> Eq_s (succ x) y
  976. succCase x = \case
  977. zero -> \p -> absurd (zeroNotSucc (sym p))
  978. succ y -> \p -> ap_s succ (Path_nat_strict_nat x y (succInj p))
  979. pathToEqS_K : {A : Type} {x : A}
  980. -> (s : {x : A} {y : A} -> Path x y -> Eq_s x y)
  981. -> (P : Path x x -> Type) -> P refl -> (p : Path x x) -> P p
  982. pathToEqS_K {A} {x} p_to_s P pr loop = transp (\i -> P (inv x loop i)) psloop where
  983. psloop : P (strictEq_pathEq (p_to_s loop))
  984. psloop = K_s (\l -> P (strictEq_pathEq {A} {x} {x} l)) pr (p_to_s {x} {x} loop)
  985. inv : (y : A) (l : Path x y) -> Path (strictEq_pathEq (p_to_s l)) l
  986. inv y l = J {A} {x} (\y l -> Path (strictEq_pathEq (p_to_s l)) l) (strictEq_pathEq aux) {y} l where
  987. aux : Eq_s (strictEq_pathEq (p_to_s (\i -> x))) (\i -> x)
  988. aux = seq_pathRefl (p_to_s (\i -> x))
  989. pathToEq_isSet : {A : Type} -> ({x : A} {y : A} -> Path x y -> Eq_s x y) -> isHSet A
  990. pathToEq_isSet {A} p_to_s = axK_to_isSet {A} (\{x} -> pathToEqS_K {A} {x} p_to_s) where
  991. axK_to_isSet : {A : Type} -> ({x : A} -> (P : Path x x -> Type) -> P refl -> (p : Path x x) -> P p) -> isHSet A
  992. axK_to_isSet K x y p q = J (\y p -> (q : Path x y) -> Path p q) (uipRefl x) p q where
  993. uipRefl : (x : A) (p : Path x x) -> Path refl p
  994. uipRefl x p = K {x} (\q -> Path refl q) refl p
  995. Nat_isSet : isHSet Nat
  996. Nat_isSet = pathToEq_isSet {Nat} (\{x} {y} -> Path_nat_strict_nat x y)
  997. equivCtr : {A : Type} {B : Type} (e : Equiv A B) (y : B) -> fiber e.1 y
  998. equivCtr e y = (e.2 y).1
  999. equivCtrPath : {A : Type} {B : Type} (e : Equiv A B) (y : B)
  1000. -> (v : fiber e.1 y) -> Path (equivCtr e y) v
  1001. equivCtrPath e y = (e.2 y).2
  1002. contr : {A : Type} {phi : I} -> isContr A -> (u : Partial phi A) -> Sub A phi u
  1003. contr {A} {phi} p u = primComp (\i -> A) (\i [ (phi = i1) as c -> p.2 (u c) i ]) (inS p.1)
  1004. contr' : {A : Type} -> ({phi : I} -> (u : Partial phi A) -> Sub A phi u) -> isContr A
  1005. contr' {A} contr = (x, \y i -> outS (contr (\ [ (i = i0) -> x, (i = i1) -> y ])) ) where
  1006. x : A
  1007. x = outS (contr (\ []))
  1008. leftIsOne : {a : I} {b : I} -> Eq_s a i1 -> Eq_s (ior a b) i1
  1009. leftIsOne {a} {b} p = J_s {I} {i1} (\i p -> IsOne (ior i b)) refl_s (sym_s p)
  1010. rightIsOne : {a : I} {b : I} -> Eq_s b i1 -> Eq_s (ior a b) i1
  1011. rightIsOne {a} {b} p = J_s {I} {i1} (\i p -> IsOne (ior a i)) refl_s (sym_s p)
  1012. bothAreOne : {a : I} {b : I} -> Eq_s a i1 -> Eq_s b i1 -> Eq_s (iand a b) i1
  1013. bothAreOne {a} {b} p q = J_s {I} {i1} (\i p -> IsOne (iand i b)) q (sym_s p)