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.

1335 lines
46 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 =
  390. let
  391. -- The Bottom left corner
  392. botLeft : B
  393. botLeft = transp (\i -> B) (f.1 a)
  394. -- The "Bottom face" filler connects the Bottom-left corner, f.1 a,
  395. -- and the Bottom-right corner, which is the transport of f.1 a
  396. -- along \i -> B.
  397. botFace : Path (f.1 a) botLeft
  398. botFace i = fill (\i -> B) (\j []) (inS (f.1 a)) i
  399. -- The "right face" filler connects the Bottom-right corner and the
  400. -- upper-right corner, which is again a simple transport along
  401. -- \i -> B.
  402. rightFace : Path (transp (\i -> B) botLeft) botLeft
  403. rightFace i = fill (\i -> B) (\j []) (inS botLeft) (inot i)
  404. -- The goal is a path between the Bottom-left and top-right corners,
  405. -- which we can get by composing (in the path sense) the Bottom and
  406. -- right faces.
  407. goal : Path (transp (\i -> B) botLeft) (f.1 a)
  408. goal = trans rightFace (\i -> botFace (inot i))
  409. in goal i
  410. -- The terms univalence + univalenceBeta suffice to prove the "full"
  411. -- univalence axiom of Voevodsky, as can be seen in the paper
  412. --
  413. -- Ian Orton, & Andrew M. Pitts. (2017). Decomposing the Univalence Axiom.
  414. --
  415. -- Available freely here: https://arxiv.org/abs/1712.04890v3
  416. J : {A : Type} {x : A}
  417. (P : (y : A) -> Path x y -> Type)
  418. (d : P x (\i -> x))
  419. {y : A} (p : Path x y)
  420. -> P y p
  421. J P d p = transp (\i -> P (p i) (\j -> p (iand i j))) d
  422. Jay : {A : Type} {x : A}
  423. (P : ((y : A) * Path x y) -> Type)
  424. (d : P (x, refl))
  425. (s : (y : A) * Path x y)
  426. -> P s
  427. Jay P d s = transp (\i -> P ((singContr {A} {x}).2 s i)) d
  428. -- Isomorphisms
  429. ---------------
  430. --
  431. -- Since isomorphisms are a much more convenient notion of equivalence
  432. -- than contractible fibers, it's natural to ask why the CCHM paper, and
  433. -- this implementation following that, decided on the latter for our
  434. -- definition of equivalence.
  435. isIso : {A : Type} -> {B : Type} -> (A -> B) -> Type
  436. isIso {A} {B} f = (g : B -> A) * ((y : B) -> Path (f (g y)) y) * ((x : A) -> Path (g (f x)) x)
  437. -- The reason is that the family of types IsIso is not a proposition!
  438. -- This means that there can be more than one way for a function to be
  439. -- an equivalence. This is Lemma 4.1.1 of the HoTT book.
  440. Iso : Type -> Type -> Type
  441. Iso A B = (f : A -> B) * isIso f
  442. -- Nevertheless, we can prove that any function with an isomorphism
  443. -- structure has contractible fibers, using a cubical argument adapted
  444. -- from CCHM's implementation of cubical type theory:
  445. --
  446. -- https://github.com/mortberg/cubicaltt/blob/master/experiments/isoToEquiv.ctt#L7-L55
  447. IsoToEquiv : {A : Type} {B : Type} -> Iso A B -> Equiv A B
  448. IsoToEquiv {A} {B} iso = (f, \y -> (fCenter y, fIsCenter y)) where
  449. f = iso.1
  450. g = iso.2.1
  451. s = iso.2.2.1
  452. t = iso.2.2.2
  453. lemIso : (y : B) (x0 : A) (x1 : A) (p0 : Path y (f x0)) (p1 : Path y (f x1))
  454. -> PathP (\i -> fiber f y) (x0, p0) (x1, p1)
  455. lemIso y x0 x1 p0 p1 =
  456. let
  457. rem0 : Path x0 (g y)
  458. rem0 i = comp (\i -> A) (\k [ (i = i0) -> t x0 k, (i = i1) -> g y ]) (inS (g (p0 (inot i))))
  459. rem1 : Path x1 (g y)
  460. rem1 i = comp (\i -> A) (\k [ (i = i0) -> t x1 k, (i = i1) -> g y ]) (inS (g (p1 (inot i))))
  461. p : Path x0 x1
  462. p i = comp (\i -> A) (\k [ (i = i0) -> rem0 (inot k), (i = i1) -> rem1 (inot k) ]) (inS (g y))
  463. fill0 : I -> I -> A
  464. fill0 i j = comp (\i -> A) (\k [ (i = i0) -> t x0 (iand j k)
  465. , (i = i1) -> g y
  466. , (j = i0) -> g (p0 (inot i))
  467. ])
  468. (inS (g (p0 (inot i))))
  469. fill1 : I -> I -> A
  470. fill1 i j = comp (\i -> A) (\k [ (i = i0) -> t x1 (iand j k)
  471. , (i = i1) -> g y
  472. , (j = i0) -> g (p1 (inot i)) ])
  473. (inS (g (p1 (inot i))))
  474. fill2 : I -> I -> A
  475. fill2 i j = comp (\i -> A) (\k [ (i = i0) -> rem0 (ior j (inot k))
  476. , (i = i1) -> rem1 (ior j (inot k))
  477. , (j = i1) -> g y ])
  478. (inS (g y))
  479. sq : I -> I -> A
  480. sq i j = comp (\i -> A) (\k [ (i = i0) -> fill0 j (inot k)
  481. , (i = i1) -> fill1 j (inot k)
  482. , (j = i1) -> g y
  483. , (j = i0) -> t (p i) (inot k) ])
  484. (inS (fill2 i j))
  485. sq1 : I -> I -> B
  486. sq1 i j = comp (\i -> B) (\k [ (i = i0) -> s (p0 (inot j)) k
  487. , (i = i1) -> s (p1 (inot j)) k
  488. , (j = i0) -> s (f (p i)) k
  489. , (j = i1) -> s y k
  490. ])
  491. (inS (f (sq i j)))
  492. in \i -> (p i, \j -> sq1 i (inot j))
  493. fCenter : (y : B) -> fiber f y
  494. fCenter y = (g y, sym (s y))
  495. fIsCenter : (y : B) (w : fiber f y) -> Path (fCenter y) w
  496. fIsCenter y w = lemIso y (fCenter y).1 w.1 (fCenter y).2 w.2
  497. IsoToId : {A : Type} {B : Type} -> Iso A B -> Path A B
  498. IsoToId i = univalence (IsoToEquiv i)
  499. -- We can prove that any involutive function is an isomorphism, since
  500. -- such a function is its own inverse.
  501. involToIso : {A : Type} (f : A -> A) -> ((x : A) -> Path (f (f x)) x) -> isIso f
  502. involToIso {A} f inv = (f, inv, inv)
  503. -- An example of univalence
  504. ---------------------------
  505. --
  506. -- The classic example of univalence is the equivalence
  507. -- not : Bool \simeq Bool.
  508. --
  509. -- We define it here.
  510. data Bool : Type where
  511. true : Bool
  512. false : Bool
  513. not : Bool -> Bool
  514. not = \case
  515. true -> false
  516. false -> true
  517. elimBool : (P : Bool -> Type) -> P true -> P false -> (b : Bool) -> P b
  518. elimBool P x y = \case
  519. true -> x
  520. false -> y
  521. if : {A : Type} -> A -> A -> Bool -> A
  522. if x y = \case
  523. true -> x
  524. false -> y
  525. -- By pattern matching it suffices to prove (not (not true)) ≡ true and
  526. -- not (not false) ≡ false. Since not (not true) computes to true (resp.
  527. -- false), both proofs go through by refl.
  528. notInvol : (x : Bool) -> Path (not (not x)) x
  529. notInvol = elimBool (\b -> Path (not (not b)) b) refl refl
  530. notp : Path Bool Bool
  531. notp = univalence (IsoToEquiv (not, involToIso not notInvol))
  532. -- This path actually serves to prove a simple lemma about the universes
  533. -- of HoTT, namely, that any univalent universe is not a 0-type. If we
  534. -- had HITs, we could prove that this fact holds for any n, but for now,
  535. -- proving it's not an h-set is the furthest we can go.
  536. -- First we define what it means for something to be false. In type theory,
  537. -- we take ¬P = P → ⊥, where the Bottom type is the only type satisfying
  538. -- the elimination principle
  539. --
  540. -- elimBottom : (P : Bottom -> Type) -> (b : Bottom) -> P b
  541. --
  542. -- This follows from setting Bottom := ∀ A, A.
  543. data Bottom : Type where {}
  544. elimBottom : (P : Bottom -> Pretype) -> (b : Bottom) -> P b
  545. elimBottom P = \case {}
  546. absurd : {P : Pretype} -> Bottom -> P
  547. absurd = \case {}
  548. -- We prove that true != false by transporting along the path
  549. --
  550. -- \i -> if (Bool -> Bool) A (p i)
  551. -- (Bool -> Bool) ------------------------------------ A
  552. --
  553. -- To verify that this has the correct endpoints, check out the endpoints
  554. -- for p:
  555. --
  556. -- true ------------------------------------ false
  557. --
  558. -- and evaluate the if at either end.
  559. trueNotFalse : Path true false -> Bottom
  560. trueNotFalse p = transp (\i -> if (Bool -> Bool) Bottom (p i)) id
  561. -- To be an h-Set is to have no "higher path information". Alternatively,
  562. --
  563. -- isHSet A = (x : A) (y : A) -> isHProp (Path x y)
  564. --
  565. isProp : Type -> Type
  566. isProp A = (x : A) (y : A) -> Path x y
  567. isHSet : Type -> Type
  568. isHSet A = (x : A) (y : A) -> isProp (Path x y)
  569. -- We can prove *a* contradiction (note: this is a direct proof!) by adversarially
  570. -- choosing two paths p, q that we know are not equal. Since "equal" paths have
  571. -- equal behaviour when transporting, we can choose two paths p, q and a point x
  572. -- such that transporting x along p gives a different result from x along q.
  573. --
  574. -- Since transp notp = not but transp refl = id, that's what we go with. The choice
  575. -- of false as the point x is just from the endpoints of trueNotFalse.
  576. universeNotSet : isHSet Type -> Bottom
  577. universeNotSet itIs = trueNotFalse (\i -> transp (\j -> itIs Bool Bool notp refl i j) false)
  578. -- Funext is an inverse of happly
  579. ---------------------------------
  580. --
  581. -- Above we proved function extensionality, namely, that functions
  582. -- pointwise equal everywhere are themselves equal.
  583. -- However, this formulation of the axiom is known as "weak" function
  584. -- extensionality. The strong version is as follows:
  585. Hom : {A : Type} {B : A -> Type} (f : (x : A) -> B x) -> (g : (x : A) -> B x) -> Type
  586. Hom {A} f g = (x : A) -> Path (f x) (g x)
  587. happly : {A : Type} {B : A -> Type} {f : (x : A) -> B x} {g : (x : A) -> B x}
  588. -> (p : Path f g) -> Hom f g
  589. happly p x i = p i x
  590. -- Strong function extensionality: happly is an equivalence.
  591. happlyIsIso : {A : Type} {B : A -> Type} {f : (x : A) -> B x} {g : (x : A) -> B x}
  592. -> isIso {Path f g} {Hom f g} happly
  593. happlyIsIso {A} {B} {f} {g} = (funext {A} {B} {f} {g}, \hom -> refl, \path -> refl)
  594. pathIsHom : {A : Type} {B : A -> Type} {f : (x : A) -> B x} {g : (x : A) -> B x}
  595. -> Path (Path f g) (Hom f g)
  596. pathIsHom {A} {B} {f} {g} =
  597. let
  598. theIso : Iso (Path f g) (Hom f g)
  599. theIso = (happly {A} {B} {f} {g}, happlyIsIso {A} {B} {f} {g})
  600. in univalence (IsoToEquiv theIso)
  601. -- Inductive types
  602. -------------------
  603. --
  604. -- An inductive type is a type freely generated by a finite set of
  605. -- constructors. For instance, the type of natural numbers is generated
  606. -- by the constructors for "zero" and "successor".
  607. data Nat : Type where
  608. zero : Nat
  609. succ : Nat -> Nat
  610. -- Pattern matching allows us to prove that these initial types are
  611. -- initial algebras for their corresponding functors.
  612. Nat_elim : (P : Nat -> Type) -> P zero -> ((x : Nat) -> P x -> P (succ x)) -> (x : Nat) -> P x
  613. Nat_elim P pz ps = \case
  614. zero -> pz
  615. succ x -> ps x (Nat_elim P pz ps x)
  616. zeroNotSucc : {x : Nat} -> Path zero (succ x) -> Bottom
  617. zeroNotSucc p = transp (\i -> fun (p i)) (p i0) where
  618. fun : Nat -> Type
  619. fun = \case
  620. zero -> Nat
  621. succ x -> Bottom
  622. succInj : {x : Nat} {y : Nat} -> Path (succ x) (succ y) -> Path x y
  623. succInj p i = pred (p i) where
  624. pred : Nat -> Nat
  625. pred = \case
  626. zero -> zero
  627. succ x -> x
  628. -- The type of integers can be defined as A + B, where "pos n" means +n
  629. -- and "neg n" means -(n + 1).
  630. data Int : Type where
  631. pos : Nat -> Int
  632. neg : Nat -> Int
  633. -- On this representation we can define the successor and predecessor
  634. -- functions by (nested) induction.
  635. sucZ : Int -> Int
  636. sucZ = \case
  637. pos n -> pos (succ n)
  638. neg n ->
  639. let suc_neg : Nat -> Int
  640. suc_neg = \case
  641. zero -> pos zero
  642. succ n -> neg n
  643. in suc_neg n
  644. predZ : Int -> Int
  645. predZ = \case
  646. pos n ->
  647. let pred_pos : Nat -> Int
  648. pred_pos = \case
  649. zero -> neg zero
  650. succ n -> pos n
  651. in pred_pos n
  652. neg n -> neg (succ n)
  653. -- And prove that the successor function is an isomorphism, and thus, an
  654. -- equivalence.
  655. sucEquiv : isIso sucZ
  656. sucEquiv =
  657. let
  658. sucPredZ : (x : Int) -> Path (sucZ (predZ x)) x
  659. sucPredZ = \case
  660. pos n ->
  661. let k : (n : Nat) -> Path (sucZ (predZ (pos n))) (pos n)
  662. k = \case
  663. zero -> refl
  664. succ n -> refl
  665. in k n
  666. neg n -> refl
  667. predSucZ : (x : Int) -> Path (predZ (sucZ x)) x
  668. predSucZ = \case
  669. pos n -> refl
  670. neg n ->
  671. let k : (n : Nat) -> Path (predZ (sucZ (neg n))) (neg n)
  672. k = \case
  673. zero -> refl
  674. succ n -> refl
  675. in k n
  676. in (predZ, sucPredZ, predSucZ)
  677. -- Univalence gives us a path between integers such that transp intPath
  678. -- x = suc x, transp (sym intPath) x = pred x
  679. intPath : Path Int Int
  680. intPath = univalence (IsoToEquiv (sucZ, sucEquiv))
  681. -- Higher inductive types
  682. -------------------------
  683. --
  684. -- While inductive types let us generate discrete spaces like the
  685. -- naturals or integers, they do not support defining higher-dimensional
  686. -- structures given by spaces with points and paths.
  687. -- A very simple higher inductive type is the interval, given by
  688. data Interval : Type where
  689. ii0 : Interval
  690. ii1 : Interval
  691. seg i : Interval [ (i = i0) -> ii0, (i = i1) -> ii1 ]
  692. -- This expresses that we have two points ii0 and ii1 and a path (\i ->
  693. -- seg i) with endpoints ii0 and ii1.
  694. -- With this type we can reproduce the proof of Lemma 6.3.2 from the
  695. -- HoTT book:
  696. iFunext : {A : Type} {B : A -> Type} (f : (x : A) -> B x) (g : (x : A) -> B x)
  697. -> ((x : A) -> Path (f x) (g x)) -> Path f g
  698. iFunext f g p i = h' (seg i) where
  699. h : (x : A) -> Interval -> B x
  700. h x = \case
  701. ii0 -> f x
  702. ii1 -> g x
  703. seg i -> p x i
  704. h' : Interval -> (x : A) -> B x
  705. h' i x = h x i
  706. -- Of course, Cubical Type Theory also has an interval (pre)type, but
  707. -- that, unlike the Interval here, is not Kan: it has no composition
  708. -- structure.
  709. -- Another simple higher-inductive type is the circle, with a point and
  710. -- a non-trivial loop, (\i -> loop i).
  711. data S1 : Type where
  712. base : S1
  713. loop i : S1 [ (i = i1) -> base, (i = i0) -> base ]
  714. -- By writing a function from the circle to the universe of types Type,
  715. -- we can calculate winding numbers along the circle.
  716. helix : S1 -> Type
  717. helix = \case
  718. base -> Int
  719. loop i -> intPath i
  720. loopP : Path base base
  721. loopP i = loop i
  722. winding : Path base base -> Int
  723. winding p = transp (\i -> helix (p i)) (pos zero)
  724. -- For instance, going around the loop once has a winding number of +1,
  725. windingLoop : Path (winding (\i -> loop i)) (pos (succ zero))
  726. windingLoop = refl
  727. -- Going backwards has a winding number of -1 (remember the
  728. -- representation of integers),
  729. windingSymLoop : Path (winding (\i -> loop (inot i))) (neg zero)
  730. windingSymLoop = refl
  731. -- And going around the trivial loop (\i -> base) goes around the the
  732. -- non-trivial loop (\i -> loop) zero times.
  733. windingBase : Path (winding (\i -> base)) (pos zero)
  734. windingBase = refl
  735. goAround : Int -> Path base base
  736. goAround =
  737. \case
  738. pos n -> forwards n
  739. neg n -> backwards n
  740. where
  741. forwards : Nat -> Path base base
  742. forwards = \case
  743. zero -> refl
  744. succ n -> trans (forwards n) (\i -> loop i)
  745. backwards : Nat -> Path base base
  746. backwards = \case
  747. zero -> \i -> loop (inot i)
  748. succ n -> trans (backwards n) (\i -> loop (inot i))
  749. -- One particularly general higher inductive type is the homotopy pushout,
  750. -- which can be seen as a kind of sum B + C with the extra condition that
  751. -- whenever x and y are in the image of f (resp. g), inl x ≡ inr y.
  752. data Pushout {A : Type} {B : Type} {C : Type} (f : A -> B) (g : A -> C) : Type where
  753. inl : (x : B) -> Pushout f g
  754. inr : (y : C) -> Pushout f g
  755. push i : (a : A) -> Pushout f g [ (i = i0) -> inl (f a), (i = i1) -> inr (g a) ]
  756. -- The name is due to the category-theoretical notion of pushout.
  757. -- TODO: finish writing this tomorrow lol
  758. data Susp (A : Type) : Type where
  759. north : Susp A
  760. south : Susp A
  761. merid i : A -> Susp A [ (i = i0) -> north, (i = i1) -> south ]
  762. data Unit : Type where
  763. tt : Unit
  764. unitEta : (x : Unit) -> Path x tt
  765. unitEta = \case tt -> refl
  766. unitContr : isContr Unit
  767. unitContr = (tt, \x -> sym (unitEta x))
  768. poSusp : Type -> Type
  769. poSusp A = Pushout {A} {Unit} {Unit} (\x -> tt) (\x -> tt)
  770. Susp_is_poSusp : {A : Type} -> Path (Susp A) (poSusp A)
  771. 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
  772. poSusp_to_Susp : {A : Type} -> poSusp A -> Susp A
  773. poSusp_to_Susp = \case
  774. inl x -> north
  775. inr x -> south
  776. push x i -> merid x i
  777. Susp_to_poSusp : {A : Type} -> Susp A -> poSusp A
  778. Susp_to_poSusp = \case
  779. north -> inl tt
  780. south -> inr tt
  781. merid x i -> push x i
  782. Susp_to_poSusp_to_Susp : {A : Type} -> (x : Susp A) -> Path (poSusp_to_Susp (Susp_to_poSusp x)) x
  783. Susp_to_poSusp_to_Susp = \case
  784. north -> refl
  785. south -> refl
  786. merid x i -> refl
  787. poSusp_to_Susp_to_poSusp : {A : Type} -> (x : poSusp A) -> Path (Susp_to_poSusp (poSusp_to_Susp x)) x
  788. poSusp_to_Susp_to_poSusp {A} = \case
  789. inl x -> cong inl (sym (unitEta x))
  790. inr x -> cong inr (sym (unitEta x))
  791. push x i -> refl
  792. data T2 : Type where
  793. baseT : T2
  794. pathOne i : T2 [ (i = i0) -> baseT, (i = i1) -> baseT ]
  795. pathTwo i : T2 [ (i = i0) -> baseT, (i = i1) -> baseT ]
  796. square i j : T2 [
  797. (j = i0) -> pathTwo i,
  798. (j = i1) -> pathTwo i,
  799. (i = i0) -> pathOne j,
  800. (i = i1) -> pathOne j
  801. ]
  802. TorusIsTwoCircles : Path T2 (S1 * S1)
  803. TorusIsTwoCircles = univalence (IsoToEquiv theIso) where
  804. torusToCircs : T2 -> S1 * S1
  805. torusToCircs = \case
  806. baseT -> (base, base)
  807. pathOne i -> (loop i, base)
  808. pathTwo i -> (base, loop i)
  809. square i j -> (loop i, loop j)
  810. circsToTorus : (S1 * S1) -> T2
  811. circsToTorus pair = go pair.1 pair.2
  812. where
  813. baseCase : S1 -> T2
  814. baseCase = \case
  815. base -> baseT
  816. loop j -> pathTwo j
  817. loopCase : Path baseCase baseCase
  818. loopCase i = \case
  819. base -> pathOne i
  820. loop j -> square i j
  821. go : S1 -> S1 -> T2
  822. go = \case
  823. base -> baseCase
  824. loop i -> loopCase i
  825. torusToCircsToTorus : (x : T2) -> Path (circsToTorus (torusToCircs x)) x
  826. torusToCircsToTorus = \case
  827. baseT -> refl
  828. pathOne i -> refl
  829. pathTwo i -> refl
  830. square i j -> refl
  831. circsToTorusToCircs : (p : S1 * S1) -> Path (torusToCircs (circsToTorus p)) p
  832. circsToTorusToCircs pair = go pair.1 pair.2 where
  833. baseCase : (y : S1) -> Path (torusToCircs (circsToTorus (base, y))) (base, y)
  834. baseCase = \case
  835. base -> refl
  836. loop j -> refl
  837. loopCase : (i : I) (y : S1) -> Path (torusToCircs (circsToTorus (loop i, y))) (loop i, y )
  838. loopCase i = \case
  839. base -> refl
  840. loop j -> refl
  841. go : (x : S1) (y : S1) -> Path (torusToCircs (circsToTorus (x, y))) (x, y)
  842. go = \case
  843. base -> baseCase
  844. loop i -> loopCase i
  845. theIso : Iso T2 (S1 * S1)
  846. theIso = (torusToCircs, circsToTorus, circsToTorusToCircs, torusToCircsToTorus)
  847. abs : Int -> Nat
  848. abs = \case
  849. pos n -> n
  850. neg n -> succ n
  851. sign : Int -> Bool
  852. sign = \case
  853. pos n -> true
  854. neg n -> false
  855. boolAnd : Bool -> Bool -> Bool
  856. boolAnd = \case
  857. true -> \case
  858. true -> true
  859. false -> false
  860. false -> \case
  861. true -> false
  862. false -> false
  863. plusNat : Nat -> Nat -> Nat
  864. plusNat = \case
  865. zero -> \x -> x
  866. succ n -> \x -> succ (plusNat n x)
  867. plusZero : (x : Nat) -> Path (plusNat zero x) x
  868. plusZero = \case
  869. zero -> refl
  870. succ n -> \i -> succ (plusZero n i)
  871. multNat : Nat -> Nat -> Nat
  872. multNat = \case
  873. zero -> \x -> zero
  874. succ n -> \x -> plusNat x (multNat n x)
  875. multInt : Int -> Int -> Int
  876. multInt x y = signify (multNat (abs x) (abs y)) (boolAnd (sign x) (sign y)) where
  877. signify : Nat -> Bool -> Int
  878. signify = \case
  879. zero -> \x -> pos zero
  880. succ n -> \case
  881. true -> pos (succ n)
  882. false -> neg n
  883. two : Int
  884. two = pos (succ (succ zero))
  885. four : Int
  886. four = multInt two two
  887. sixteen : Int
  888. sixteen = multInt four four
  889. Prop : Type
  890. Prop = (A : Type) * isProp A
  891. data Sq (A : Type) : Type where
  892. inc : A -> Sq A
  893. sq i : (x : Sq A) (y : Sq A) -> Sq A [ (i = i0) -> x, (i = i1) -> y ]
  894. isProp_isSet : {A : Type} -> isProp A -> isHSet A
  895. isProp_isSet h a b p q j i =
  896. hcomp {A}
  897. (\k [ (i = i0) -> h a a k
  898. , (i = i1) -> h a b k
  899. , (j = i0) -> h a (p i) k
  900. , (j = i1) -> h a (q i) k
  901. ])
  902. (inS a)
  903. isProp_isProp : {A : Type} -> isProp (isProp A)
  904. isProp_isProp f g i a b = isProp_isSet f a b (f a b) (g a b) i
  905. Sq_rec : {A : Type} {B : Type}
  906. -> isProp B
  907. -> (f : A -> B)
  908. -> Sq A -> B
  909. Sq_rec prop f =
  910. \case
  911. inc x -> f x
  912. sq x y i -> prop (work x) (work y) i
  913. where
  914. work : Sq A -> B
  915. work = \case
  916. inc x -> f x
  917. hitTranspExample : Path (inc false) (inc true)
  918. hitTranspExample i = transp (\i -> Sq (notp i)) (sq (inc true) (inc false) i)
  919. data S2 : Type where
  920. base2 : S2
  921. surf2 i j : S2 [ (i = i0) -> base2, (i = i1) -> base2, (j = i0) -> base2, (j = i1) -> base2]
  922. S2IsSuspS1 : Path S2 (Susp S1)
  923. S2IsSuspS1 = univalence (IsoToEquiv iso) where
  924. toS2 : Susp S1 -> S2
  925. toS2 = \case { north -> base2; south -> base2; merid x i -> sphMerid x i } where
  926. sphMerid = \case
  927. base -> \i -> base2
  928. loop j -> \i -> surf2 i j
  929. suspSurf : I -> I -> I -> Susp S1
  930. suspSurf i j = hfill {Susp S1} (\k [ (i = i0) -> north
  931. , (i = i1) -> merid base (inot k)
  932. , (j = i0) -> merid base (iand (inot k) i)
  933. , (j = i1) -> merid base (iand (inot k) i)
  934. ])
  935. (inS (merid (loop j) i))
  936. fromS2 : S2 -> Susp S1
  937. fromS2 = \case { base2 -> north; surf2 i j -> suspSurf i j i1 }
  938. toFromS2 : (x : S2) -> Path (toS2 (fromS2 x)) x
  939. toFromS2 = \case { base2 -> refl; surf2 i j -> \k -> toS2 (suspSurf i j (inot k)) }
  940. fromToS2 : (x : Susp S1) -> Path (fromS2 (toS2 x)) x
  941. fromToS2 = \case { north -> refl; south -> \i -> merid base i; merid x i -> meridCase i x } where
  942. meridCase : (i : I) (x : S1) -> Path (fromS2 (toS2 (merid x i))) (merid x i)
  943. meridCase i = \case
  944. base -> \k -> merid base (iand i k)
  945. loop j -> \k -> suspSurf i j (inot k)
  946. iso : Iso S2 (Susp S1)
  947. iso = (fromS2, toS2, fromToS2, toFromS2)
  948. data S3 : Type where
  949. base3 : S3
  950. surf3 i j k : S3 [ (i = i0) -> base3, (i = i1) -> base3, (j = i0) -> base3, (j = i1) -> base3, (k = i0) -> base3, (k = i1) -> base3 ]
  951. S3IsSuspS2 : Path S3 (Susp S2)
  952. S3IsSuspS2 = univalence (IsoToEquiv iso) where
  953. toS3 : Susp S2 -> S3
  954. toS3 = \case { north -> base3; south -> base3; merid x i -> sphMerid x i } where
  955. sphMerid = \case
  956. base2 -> \i -> base3
  957. surf2 j k -> \i -> surf3 i j k
  958. suspSurf : I -> I -> I -> I -> Susp S2
  959. suspSurf i j k = hfill {Susp S2} (\l [ (i = i0) -> north
  960. , (i = i1) -> merid base2 (inot l)
  961. , (j = i0) -> merid base2 (iand (inot l) i)
  962. , (j = i1) -> merid base2 (iand (inot l) i)
  963. , (k = i0) -> merid base2 (iand (inot l) i)
  964. , (k = i1) -> merid base2 (iand (inot l) i)
  965. ])
  966. (inS (merid (surf2 j k) i))
  967. fromS3 : S3 -> Susp S2
  968. fromS3 = \case { base3 -> north; surf3 i j k -> suspSurf i j k i1 }
  969. toFromS3 : (x : S3) -> Path (toS3 (fromS3 x)) x
  970. toFromS3 = \case { base3 -> refl; surf3 i j k -> \l -> toS3 (suspSurf i j k (inot l)) }
  971. fromToS3 : (x : Susp S2) -> Path (fromS3 (toS3 x)) x
  972. fromToS3 = \case { north -> refl; south -> \i -> merid base2 i; merid x i -> meridCase i x } where
  973. meridCase : (i : I) (x : S2) -> Path (fromS3 (toS3 (merid x i))) (merid x i)
  974. meridCase i = \case
  975. base2 -> \k -> merid base2 (iand i k)
  976. surf2 j k -> \l -> suspSurf i j k (inot l)
  977. iso : Iso S3 (Susp S2)
  978. iso = (fromS3, toS3, fromToS3, toFromS3)
  979. ap_s : {A : Pretype} {B : Pretype} (f : A -> B) {x : A} {y : A} -> Eq_s x y -> Eq_s (f x) (f y)
  980. ap_s {A} {B} f {x} {y} = J_s (\y p -> Eq_s (f x) (f y)) refl_s
  981. subst_s : {A : Pretype} (P : A -> Pretype) {x : A} {y : A} -> Eq_s x y -> P x -> P y
  982. subst_s {A} P {x} {z} p px = J_s {A} {x} (\y p -> P x -> P y) id p px
  983. sym_s : {A : Pretype} {x : A} {y : A} -> Eq_s x y -> Eq_s y x
  984. sym_s {A} {x} {y} = J_s {A} {x} (\y p -> Eq_s y x) refl_s
  985. UIP : {A : Pretype} {x : A} {y : A} (p : Eq_s x y) (q : Eq_s x y) -> Eq_s p q
  986. 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
  987. uipRefl : (A : Pretype) (x : A) (p : Eq_s x x) -> Eq_s refl_s p
  988. uipRefl A x p = K_s {A} {x} (\q -> Eq_s refl_s q) refl_s p
  989. strictEq_pathEq : {A : Type} {x : A} {y : A} -> Eq_s x y -> Path x y
  990. strictEq_pathEq {A} {x} {y} eq = J_s {A} {x} (\y p -> Path x y) (\i -> x) {y} eq
  991. seq_pathRefl : {A : Type} {x : A} (p : Eq_s x x) -> Eq_s (strictEq_pathEq p) (refl {A} {x})
  992. seq_pathRefl {A} {x} p = K_s (\p -> Eq_s (strictEq_pathEq {A} {x} {x} p) (refl {A} {x})) refl_s p
  993. Path_nat_strict_nat : (x : Nat) (y : Nat) -> Path x y -> Eq_s x y
  994. Path_nat_strict_nat = \case { zero -> zeroCase; succ x -> succCase x } where
  995. zeroCase : (y : Nat) -> Path zero y -> Eq_s zero y
  996. zeroCase = \case
  997. zero -> \p -> refl_s
  998. succ x -> \p -> absurd (zeroNotSucc p)
  999. succCase : (x : Nat) (y : Nat) -> Path (succ x) y -> Eq_s (succ x) y
  1000. succCase x = \case
  1001. zero -> \p -> absurd (zeroNotSucc (sym p))
  1002. succ y -> \p -> ap_s succ (Path_nat_strict_nat x y (succInj p))
  1003. pathToEqS_K : {A : Type} {x : A}
  1004. -> (s : {x : A} {y : A} -> Path x y -> Eq_s x y)
  1005. -> (P : Path x x -> Type) -> P refl -> (p : Path x x) -> P p
  1006. pathToEqS_K {A} {x} p_to_s P pr loop = transp (\i -> P (inv x loop i)) psloop where
  1007. psloop : P (strictEq_pathEq (p_to_s loop))
  1008. psloop = K_s (\l -> P (strictEq_pathEq {A} {x} {x} l)) pr (p_to_s {x} {x} loop)
  1009. inv : (y : A) (l : Path x y) -> Path (strictEq_pathEq (p_to_s l)) l
  1010. inv y l = J {A} {x} (\y l -> Path (strictEq_pathEq (p_to_s l)) l) (strictEq_pathEq aux) {y} l where
  1011. aux : Eq_s (strictEq_pathEq (p_to_s (\i -> x))) (\i -> x)
  1012. aux = seq_pathRefl (p_to_s (\i -> x))
  1013. pathToEq_isSet : {A : Type} -> ({x : A} {y : A} -> Path x y -> Eq_s x y) -> isHSet A
  1014. pathToEq_isSet {A} p_to_s = axK_to_isSet {A} (\{x} -> pathToEqS_K {A} {x} p_to_s) where
  1015. axK_to_isSet : {A : Type} -> ({x : A} -> (P : Path x x -> Type) -> P refl -> (p : Path x x) -> P p) -> isHSet A
  1016. axK_to_isSet K x y p q = J (\y p -> (q : Path x y) -> Path p q) (uipRefl x) p q where
  1017. uipRefl : (x : A) (p : Path x x) -> Path refl p
  1018. uipRefl x p = K {x} (\q -> Path refl q) refl p
  1019. Nat_isSet : isHSet Nat
  1020. Nat_isSet = pathToEq_isSet {Nat} (\{x} {y} -> Path_nat_strict_nat x y)
  1021. equivCtr : {A : Type} {B : Type} (e : Equiv A B) (y : B) -> fiber e.1 y
  1022. equivCtr e y = (e.2 y).1
  1023. equivCtrPath : {A : Type} {B : Type} (e : Equiv A B) (y : B)
  1024. -> (v : fiber e.1 y) -> Path (equivCtr e y) v
  1025. equivCtrPath e y = (e.2 y).2
  1026. contr : {A : Type} {phi : I} -> isContr A -> (u : Partial phi A) -> Sub A phi u
  1027. contr {A} {phi} p u = primComp (\i -> A) (\i [ (phi = i1) as c -> p.2 (u c) i ]) (inS p.1)
  1028. contr' : {A : Type} -> ({phi : I} -> (u : Partial phi A) -> Sub A phi u) -> isContr A
  1029. contr' {A} contr = (x, \y i -> outS (contr (\ [ (i = i0) -> x, (i = i1) -> y ])) ) where
  1030. x : A
  1031. x = outS (contr (\ []))
  1032. leftIsOne : {a : I} {b : I} -> Eq_s a i1 -> Eq_s (ior a b) i1
  1033. leftIsOne {a} {b} p = J_s {I} {i1} (\i p -> IsOne (ior i b)) refl_s (sym_s p)
  1034. rightIsOne : {a : I} {b : I} -> Eq_s b i1 -> Eq_s (ior a b) i1
  1035. rightIsOne {a} {b} p = J_s {I} {i1} (\i p -> IsOne (ior a i)) refl_s (sym_s p)
  1036. bothAreOne : {a : I} {b : I} -> Eq_s a i1 -> Eq_s b i1 -> Eq_s (iand a b) i1
  1037. bothAreOne {a} {b} p q = J_s {I} {i1} (\i p -> IsOne (iand i b)) q (sym_s p)
  1038. test : {X : Type} -> (S1 -> X) -> (a : X) * Path a a
  1039. test {X} f = (f base, \i -> f (loop i))
  1040. test' : {X : Type} -> ((a : X) * Path a a) -> S1 -> X
  1041. test' {X} p = \case
  1042. base -> p.1
  1043. loop i -> p.2 i
  1044. test_test' : {X : Type} -> (f : S1 -> X) -> Path (test' (test f)) f
  1045. test_test' {X} f = funext {S1} {\s -> X} {\x -> test' (test f) x} {f} h where
  1046. h : (x : S1) -> Path (test' (test f) x) (f x)
  1047. h = \case
  1048. base -> refl
  1049. loop i -> refl
  1050. test'_test : {X : Type} -> (x : (a : X) * Path a a) -> Path (test (test' x)) x
  1051. test'_test x = refl
  1052. test'' : {X : Type} -> Path (S1 -> X) ((a : X) * Path a a)
  1053. test'' = IsoToId {S1 -> X} {(a : X) * Path a a} (test, test', test'_test, test_test')
  1054. -- HoTT book lemma 8.9.1
  1055. encodeDecode : {A : Type} {a0 : A}
  1056. -> (code : A -> Type)
  1057. -> (c0 : code a0)
  1058. -> (decode : (x : A) -> code x -> (Path a0 x))
  1059. -> ((c : code a0) -> Path (transp (\i -> code (decode a0 c i)) c0) c)
  1060. -> Path (decode a0 c0) refl
  1061. -> Path (Path a0 a0) (code a0)
  1062. encodeDecode code c0 decode encDec based = IsoToId (encode {a0}, decode a0, encDec, decEnc) where
  1063. encode : {x : A} -> Path a0 x -> code x
  1064. encode alpha = transp (\i -> code (alpha i)) c0
  1065. encodeRefl : Path (encode refl) c0
  1066. encodeRefl = sym (transpFill {\i -> code a0} c0)
  1067. decEnc : {x : A} (p : Path a0 x) -> Path (decode x (encode p)) p
  1068. decEnc p = J (\x p -> Path (decode x (encode p)) p) q p where
  1069. q : Path (decode a0 (encode refl)) refl
  1070. q = transp (\i -> Path (decode a0 (encodeRefl (inot i))) refl) based