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.

503 lines
20 KiB

  1. {-# LANGUAGE BlockArguments #-}
  2. {-# LANGUAGE LambdaCase #-}
  3. {-# LANGUAGE OverloadedStrings #-}
  4. {-# LANGUAGE DerivingStrategies #-}
  5. {-# LANGUAGE DeriveAnyClass #-}
  6. {-# LANGUAGE ViewPatterns #-}
  7. module Elab.WiredIn where
  8. import Control.Exception
  9. import qualified Data.Map.Strict as Map
  10. import qualified Data.Sequence as Seq
  11. import qualified Data.Text as T
  12. import Data.Map.Strict (Map)
  13. import Data.Text (Text)
  14. import Data.Typeable
  15. import Elab.Eval
  16. import GHC.Stack (HasCallStack)
  17. import qualified Presyntax.Presyntax as P
  18. import Syntax.Pretty (prettyTm)
  19. import Syntax
  20. import System.IO.Unsafe
  21. wiType :: WiredIn -> NFType
  22. wiType WiType = VType
  23. wiType WiPretype = VTypeω
  24. wiType WiInterval = VTypeω
  25. wiType WiI0 = VI
  26. wiType WiI1 = VI
  27. wiType WiIAnd = VI ~> VI ~> VI
  28. wiType WiIOr = VI ~> VI ~> VI
  29. wiType WiINot = VI ~> VI
  30. wiType WiPathP = dprod (VI ~> VType) \a -> a @@ VI0 ~> a @@ VI1 ~> VType
  31. wiType WiIsOne = VI ~> VTypeω
  32. wiType WiItIsOne = VIsOne VI1
  33. wiType WiPartial = VI ~> VType ~> VTypeω
  34. wiType WiPartialP = dprod VI \x -> VPartial x VType ~> VTypeω
  35. wiType WiSub = dprod VType \a -> dprod VI \phi -> VPartial phi a ~> VTypeω
  36. wiType WiInS = forAll VType \a -> forAll VI \phi -> dprod a \u -> VSub a phi (fun (const u))
  37. wiType WiOutS = forAll VType \a -> forAll VI \phi -> forAll (VPartial phi a) \u -> VSub a phi u ~> a
  38. wiType WiComp = dprod' "A" (VI ~> VType) \a -> forAll VI \phi -> dprod (dprod VI \i -> VPartial phi (a @@ i)) \u -> VSub (a @@ VI0) phi (u @@ VI0) ~> a @@ VI1
  39. -- (A : Type) {phi : I} (T : Partial phi Type) (e : PartialP phi (\o -> Equiv (T o) A)) -> Type
  40. wiType WiGlue = dprod' "A" VType \a -> forAll' "phi" VI \phi -> dprod' "T" (VPartial phi VType) \t -> VPartialP phi (fun \o -> equiv (t @@ o) a) ~> VType
  41. -- {A : Type} {phi : I} {T : Partial phi Type} {e : PartialP phi (\o -> Equiv (T o) A)} -> (t : PartialP phi T) -> Sub A phi (\o -> e o (t o)) -> Glue A phi T e
  42. wiType WiGlueElem = forAll' "A" VType \a -> forAll' "phi" VI \phi -> forAll' "T" (VPartial phi VType) \ty -> forAll' "e" (VPartialP phi (fun \o -> equiv (ty @@ o) a)) \eqv ->
  43. dprod' "t" (VPartialP phi ty) \t -> VSub a phi (fun \o -> vProj1 (eqv @@ o) @@ (t @@ o)) ~> VGlueTy a phi ty eqv
  44. -- {A : Type} {phi : I} {T : Partial phi Type} {e : PartialP phi (\o -> Equiv (T o) A)} -> Glue A phi T e -> A
  45. wiType WiUnglue = forAll' "A" VType \a -> forAll' "phi" VI \phi -> forAll' "T" (VPartial phi VType) \ty -> forAll' "e" (VPartialP phi (fun \o -> equiv (ty @@ o) a)) \e -> VGlueTy a phi ty e ~> a
  46. wiValue :: WiredIn -> Value
  47. wiValue WiType = VType
  48. wiValue WiPretype = VTypeω
  49. wiValue WiInterval = VI
  50. wiValue WiI0 = VI0
  51. wiValue WiI1 = VI1
  52. wiValue WiIAnd = fun \x -> fun \y -> iand x y
  53. wiValue WiIOr = fun \x -> fun \y -> ior x y
  54. wiValue WiINot = fun inot
  55. wiValue WiPathP = fun \a -> fun \x -> fun \y -> VPath a x y
  56. wiValue WiIsOne = fun VIsOne
  57. wiValue WiItIsOne = VItIsOne
  58. wiValue WiPartial = fun \phi -> fun \r -> VPartial phi r
  59. wiValue WiPartialP = fun \phi -> fun \r -> VPartialP phi r
  60. wiValue WiSub = fun \a -> fun \phi -> fun \u -> VSub a phi u
  61. wiValue WiInS = forallI \a -> forallI \phi -> fun \u -> VInc a phi u
  62. wiValue WiOutS = forallI \a -> forallI \phi -> forallI \u -> fun \x -> outS a phi u x
  63. wiValue WiComp = fun \a -> forallI \phi -> fun \u -> fun \x -> comp a phi u x
  64. wiValue WiGlue = fun \a -> forallI \phi -> fun \t -> fun \e -> glueType a phi t e
  65. wiValue WiGlueElem = forallI \a -> forallI \phi -> forallI \ty -> forallI \eqv -> fun \x -> fun \y -> glueElem a phi ty eqv x y
  66. wiValue WiUnglue = forallI \a -> forallI \phi -> forallI \ty -> forallI \eqv -> fun \x -> unglue a phi ty eqv x
  67. (~>) :: Value -> Value -> Value
  68. a ~> b = VPi P.Ex a (Closure (Bound "_" 0) (const b))
  69. infixr 7 ~>
  70. fun, line :: (Value -> Value) -> Value
  71. fun k = VLam P.Ex $ Closure (Bound "x" 0) (k . force)
  72. line k = VLam P.Ex $ Closure (Bound "i" 0) (k . force)
  73. fun' :: String -> (Value -> Value) -> Value
  74. fun' x k = VLam P.Ex $ Closure (Bound (T.pack x) 0) (k . force)
  75. forallI :: (Value -> Value) -> Value
  76. forallI k = VLam P.Im $ Closure (Bound "x" 0) (k . force)
  77. dprod' :: String -> Value -> (Value -> Value) -> Value
  78. dprod' t a b = VPi P.Ex a (Closure (Bound (T.pack t) 0) b)
  79. dprod :: Value -> (Value -> Value) -> Value
  80. dprod = dprod' "x"
  81. exists' :: String -> Value -> (Value -> Value) -> Value
  82. exists' s a b = VSigma a (Closure (Bound (T.pack s) 0) b)
  83. exists :: Value -> (Value -> Value) -> Value
  84. exists = exists' "x"
  85. forAll' :: String -> Value -> (Value -> Value) -> Value
  86. forAll' n a b = VPi P.Im a (Closure (Bound (T.pack n) 0) b)
  87. forAll :: Value -> (Value -> Value) -> Value
  88. forAll = forAll' "x"
  89. wiredInNames :: Map Text WiredIn
  90. wiredInNames = Map.fromList
  91. [ ("Pretype", WiPretype)
  92. , ("Type", WiType)
  93. , ("Interval", WiInterval)
  94. , ("i0", WiI0)
  95. , ("i1", WiI1)
  96. , ("iand", WiIAnd)
  97. , ("ior", WiIOr)
  98. , ("inot", WiINot)
  99. , ("PathP", WiPathP)
  100. , ("IsOne", WiIsOne)
  101. , ("itIs1", WiItIsOne)
  102. , ("Partial", WiPartial)
  103. , ("PartialP", WiPartialP)
  104. , ("Sub", WiSub)
  105. , ("inS", WiInS)
  106. , ("outS", WiOutS)
  107. , ("comp", WiComp)
  108. , ("Glue", WiGlue)
  109. , ("glue", WiGlueElem)
  110. , ("unglue", WiUnglue)
  111. ]
  112. newtype NoSuchPrimitive = NoSuchPrimitive { getUnknownPrim :: Text }
  113. deriving (Show, Typeable)
  114. deriving anyclass (Exception)
  115. -- Interval operations
  116. iand, ior :: Value -> Value -> Value
  117. iand x = case force x of
  118. VI1 -> id
  119. VI0 -> const VI0
  120. VIAnd x y -> \z -> case force z of
  121. VI0 -> VI0
  122. VI1 -> VI1
  123. z -> iand x (iand y z)
  124. x -> \y -> case force y of
  125. VI0 -> VI0
  126. VI1 -> x
  127. y -> VIAnd x y
  128. ior x = case force x of
  129. VI0 -> id
  130. VI1 -> const VI1
  131. VIOr x y -> \z -> case force z of
  132. VI1 -> VI1
  133. VI0 -> VIOr x y
  134. _ -> ior x (ior y z)
  135. x -> \y -> case force y of
  136. VI1 -> VI1
  137. VI0 -> x
  138. y -> VIOr x y
  139. inot :: Value -> Value
  140. inot x = case force x of
  141. VI0 -> VI1
  142. VI1 -> VI0
  143. VIOr x y -> VIAnd (inot x) (inot y)
  144. VIAnd x y -> VIOr (inot x) (inot y)
  145. VINot x -> x
  146. x -> VINot x
  147. ielim :: Value -> Value -> Value -> Value -> NFEndp -> Value
  148. ielim line left right (GluedVl h sp vl) i =
  149. GluedVl h (sp Seq.:|> PIElim line left right i) (ielim line left right vl i)
  150. ielim line left right fn i =
  151. case force fn of
  152. VLine _ _ _ fun -> fun @@ i
  153. x -> case force i of
  154. VI1 -> right
  155. VI0 -> left
  156. _ -> case x of
  157. VNe n sp -> VNe n (sp Seq.:|> PIElim line left right i)
  158. VSystem map -> VSystem (fmap (flip (ielim line left right) i) map)
  159. VInc (VPath _ _ _) _ u -> ielim line left right u i
  160. VCase env r x xs -> VCase env r x (fmap (fmap (flip (IElim (quote line) (quote left) (quote right)) (quote i))) xs)
  161. _ -> error $ "can't ielim " ++ show (prettyTm (quote fn))
  162. outS :: HasCallStack => NFSort -> NFEndp -> Value -> Value -> Value
  163. outS _ (force -> VI1) u _ = u @@ VItIsOne
  164. outS _ _phi _ (VInc _ _ x) = x
  165. outS _ VI0 _ x = x
  166. outS a phi u (GluedVl x sp vl) = GluedVl x (sp Seq.:|> POuc a phi u) (outS a phi u vl)
  167. outS a phi u (VNe x sp) = VNe x (sp Seq.:|> POuc a phi u)
  168. outS a phi u (VSystem fs) = VSystem (fmap (outS a phi u) fs)
  169. outS _ _ _ v = error $ "can't outS " ++ show (prettyTm (quote v))
  170. -- Composition
  171. comp :: HasCallStack => NFLine -> NFEndp -> Value -> Value -> Value
  172. comp _a VI1 u _a0 = u @@ VI1 @@ VItIsOne
  173. comp a psi@phi u incA0@(compOutS (a @@ VI1) phi (u @@ VI1 @@ VItIsOne) -> a0) =
  174. case force (a @@ VVar name) of
  175. VPi{} ->
  176. let
  177. plic i = let VPi p _ _ = force (a @@ i) in p
  178. dom i = let VPi _ d _ = force (a @@ i) in d
  179. rng i = let VPi _ _ (Closure _ r) = force (a @@ i) in r
  180. y' i y = fill (fun (dom . inot)) VI0 (fun \_ -> fun \_ -> VSystem mempty) (VInc (dom VI0) phi y) i
  181. ybar i y = y' (inot i) y
  182. in VLam (plic VI1) . Closure (Bound "x" 0) $ \arg ->
  183. comp (line \i -> rng i (ybar i arg))
  184. phi
  185. (system \i isone -> vApp (plic i) (u @@ i @@ isone) (ybar i arg))
  186. (VInc (rng VI0 (ybar VI0 arg)) phi (vApp (plic VI0) a0 (ybar VI0 arg)))
  187. VSigma{} ->
  188. let
  189. dom i = let VSigma d _ = force (a @@ i) in d
  190. rng i = let VSigma _ (Closure _ r) = force (a @@ i) in r
  191. w i = fill (fun dom) phi (system \i isone -> vProj1 (u @@ i @@ isone)) (VInc (dom VI0) phi (vProj1 a0)) i
  192. -- c1 = comp (fun dom) phi (system \i isone -> vProj1 (u @@ i @@ isone)) (VInc (dom VI0) phi (vProj1 a0))
  193. c2 = comp (fun \x -> rng x (w x)) phi (system \i isone -> vProj2 (u @@ i @@ isone)) (VInc (rng VI0 (w VI0)) phi (vProj2 a0))
  194. in
  195. VPair (w VI1) c2
  196. VPath{} ->
  197. let
  198. a' i = let VPath thea _ _ = force (a @@ i) in thea
  199. u' i = let VPath _ theu _ = force (a @@ i) in theu
  200. v' i = let VPath _ _ thev = force (a @@ i) in thev
  201. in
  202. VLine (a' VI1 @@ VI1) (u' VI1) (v' VI1) $ fun \j ->
  203. comp (fun \x -> a' x @@ x)
  204. (phi `ior` j `ior` inot j)
  205. (system \i isone -> mkVSystem (Map.fromList [ (phi, ielim (a' VI0) (u' VI0) (v' VI0) (u @@ i @@ isone) j)
  206. , (j, v' i)
  207. , (inot j, u' i)]))
  208. (VInc (a' VI0 @@ VI0 @@ j) phi (ielim (a' VI0 @@ VI0) (u' VI0) (v' VI0) a0 j))
  209. VGlueTy _ thePhi theTypes theEquivs ->
  210. let
  211. b = u
  212. b0 = a0
  213. fam = a
  214. in
  215. let
  216. base i = let VGlueTy b _ _ _ = forceAndGlue (fam @@ i) in b
  217. phi i = substitute (Map.singleton name i) thePhi
  218. types i = substitute (Map.singleton name i) theTypes @@ VItIsOne
  219. equivs i = substitute (Map.singleton name i) theEquivs
  220. a i u = unglue (base i) (phi i) (types i @@ u) (equivs i) (b @@ i @@ u)
  221. a0 = unglue (base VI0) (phi VI0) (types VI0) (equivs VI0) b0
  222. del = faceForall phi
  223. a1' = comp (line base) psi (system a) (VInc (base VI0) psi a0)
  224. t1' = comp (line (const (types VI0))) psi (line (b @@)) (VInc (base VI0) psi b0)
  225. (omega_st, omega_t, omega_rep) = pres types base equivs psi (b @@) b0
  226. omega = outS omega_t psi omega_rep omega_st
  227. (t1alpha_st, t1a_t, t1a_rep) = opEquiv (base VI1) (types VI1) (equivs VI1 @@ VItIsOne) (del `ior` psi) (fun ts) (fun ps) a1'
  228. t1alpha = outS t1a_t (del `ior` psi) t1a_rep t1alpha_st
  229. (t1, alpha) = (vProj1 t1alpha, vProj2 t1alpha)
  230. ts isone = mkVSystem . Map.fromList $ [(del, t1'), (psi, (b @@ VI1 @@ isone))]
  231. ps _isone = mkVSystem . Map.fromList $ [(del, omega), (psi, VLine (line (const (base VI1))) a1' a1' (fun (const a1')))]
  232. a1 = comp
  233. (fun (const (base VI1)))
  234. (phi VI1 `ior` psi)
  235. (system \j _u -> mkVSystem (Map.fromList [ (phi VI1, ielim (base VI1) a1' (vProj1 (equivs VI1 @@ VItIsOne)) alpha j)
  236. , (psi, a VI1 VItIsOne)]))
  237. (VInc (base VI1) (phi VI1 `ior` psi) a1')
  238. b1 = glueElem (base VI1) (phi VI1) (types VI1) (equivs VI1) (fun (const t1)) a1
  239. in b1
  240. VType -> VGlueTy a0 phi (fun' "is1" \is1 -> u @@ VI1 @@ is1)
  241. (fun' "is1" \_ -> mapVSystem (makeEquiv equivVar) (u @@ VVar equivVar @@ VItIsOne))
  242. VNe (HData False _) Seq.Empty -> a0
  243. VNe (HData False _) args ->
  244. case force a0 of
  245. VNe (HCon con_type con_name) con_args ->
  246. VNe (HCon con_type con_name) $ compConArgs (length args) (a @@) con_type con_args phi u
  247. _ -> VComp a phi u (VInc (a @@ VI0) phi a0)
  248. VNe (HData True _) args -> compHIT (length args) (a @@) phi u incA0
  249. VLam{} -> error $ "comp VLam " ++ show (prettyTm (quote a))
  250. sys@VSystem{} -> error $ "comp VSystem: " ++ show (prettyTm (quote sys))
  251. _ -> VComp a phi u (VInc (a @@ VI0) phi a0)
  252. where
  253. {-# NOINLINE name #-}
  254. name = unsafePerformIO newName
  255. {-# NOINLINE equivVar #-}
  256. equivVar = unsafePerformIO newName
  257. mapVSystem :: (Value -> Value) -> Value -> Value
  258. mapVSystem f (VSystem fs) = VSystem (fmap f fs)
  259. mapVSystem f x = f x
  260. forceAndGlue :: Value -> Value
  261. forceAndGlue v =
  262. case force v of
  263. v@VGlueTy{} -> v
  264. y -> VGlueTy y VI1 (fun (const y)) (fun (const (idEquiv y)))
  265. compHIT :: HasCallStack => Int -> (NFEndp -> NFSort) -> NFEndp -> Value -> Value -> Value
  266. compHIT 0 a phi u a0 =
  267. case phi of
  268. VI1 -> u @@ VI1 @@ VItIsOne
  269. VI0 -> compOutS (a VI0) phi u a0
  270. _ -> VHComp (a VI0) phi u a0
  271. compHIT _ a phi u a0 = error $ unlines
  272. [ "*** TODO: composition for HIT: "
  273. , "domain = " ++ show (prettyTm (quote (zonk (fun a))))
  274. , "phi = " ++ show (prettyTm (quote (zonk phi)))
  275. , "u = " ++ show (prettyTm (quote (zonk u)))
  276. , "a0 = " ++ show (prettyTm (quote (zonk a0)))
  277. ]
  278. compConArgs :: Int -> (NFEndp -> Value) -> Value -> Seq.Seq Projection -> NFEndp -> Value -> Seq.Seq Projection
  279. compConArgs total_args fam = go total_args where
  280. go _ _ Seq.Empty _ _ = Seq.Empty
  281. go nargs (VPi p dom (Closure _ rng)) (PApp p' y Seq.:<| xs) phi u
  282. | nargs > 0 = assert (p == p') $ go (nargs - 1) (rng (smuggle (fun (\i -> nthArg (total_args - nargs) (fam i))))) xs phi u
  283. | otherwise = assert (p == p') $
  284. let fill = makeFiller nargs dom phi u y
  285. in PApp p' (fill @@ VI1) Seq.:<| go (nargs - 1) (rng fill) xs phi u
  286. go _ _ _ _ _ = error $ "invalid constructor"
  287. nthArg i (VNe hd s) =
  288. case s Seq.!? i of
  289. Just (PApp _ t) -> t
  290. _ -> error $ "invalid " ++ show i ++ "th argument to data type " ++ show hd
  291. nthArg i (VSystem vs) = VSystem (fmap (nthArg i) vs)
  292. nthArg i xs = error $ "can't get " ++ show i ++ "th argument of " ++ show (prettyTm (quote xs))
  293. makeFiller nth (VNe (HData _ n') args) phi u a0
  294. | n' == typeArgument =
  295. fun $ fill (makeDomain args) phi (system \i is1 -> nthArg nth (u @@ i @@ is1) ) a0
  296. makeFiller _ _ _ _ a0 = fun (const a0)
  297. makeDomain (PApp _ x Seq.:<| xs) = fun \i -> foldl (\t (~(PApp _ x)) -> t @@ (x @@ i)) (x @@ i) xs
  298. makeDomain _ = error "somebody smuggled something that smells"
  299. smuggle x = VNe (HData False typeArgument) (Seq.singleton (PApp P.Ex x))
  300. typeArgument = unsafePerformIO newName
  301. {-# NOINLINE typeArgument #-}
  302. compOutS :: HasCallStack => NFSort -> NFEndp -> Value -> Value -> Value
  303. compOutS a b c d = compOutS a b c (force d) where
  304. compOutS _ _hi _0 vl@VComp{} = vl
  305. compOutS _ _hi _0 (VInc _ _ x) = x
  306. compOutS a phi a0 v = outS a phi a0 v
  307. system :: (Value -> Value -> Value) -> Value
  308. system k = VLam P.Ex $ Closure (Bound "i" 0) \i -> VLam P.Ex $ Closure (Bound "phi" 0) \isone -> k i isone
  309. fill :: HasCallStack => NFLine -> NFEndp -> Value -> Value -> NFEndp -> Value
  310. fill a phi u a0 j =
  311. comp (line \i -> a @@ (i `iand` j))
  312. (phi `ior` inot j)
  313. (system \i isone -> mkVSystem (Map.fromList [ (phi, u @@ (i `iand` j) @@ isone)
  314. , (inot j, outS a phi (u @@ VI0) a0)]))
  315. a0
  316. hComp :: NFSort -> NFEndp -> Value -> Value -> Value
  317. hComp _ (force -> VI1) u _ = u @@ VI1 @@ VItIsOne
  318. hComp a phi u a0 = VHComp a phi u a0
  319. glueType :: NFSort -> NFEndp -> NFPartial -> NFPartial -> Value
  320. glueType a phi tys eqvs = VGlueTy a phi tys eqvs
  321. glueElem :: NFSort -> NFEndp -> NFPartial -> NFPartial -> NFPartial -> Value -> Value
  322. glueElem _a (force -> VI1) _tys _eqvs t _vl = t @@ VItIsOne
  323. glueElem a phi tys eqvs t vl = VGlue a phi tys eqvs t vl
  324. unglue :: HasCallStack => NFSort -> NFEndp -> NFPartial -> NFPartial -> Value -> Value
  325. unglue _a (force -> VI1) _tys eqvs x = vProj1 (eqvs @@ VItIsOne) @@ x
  326. unglue _a _phi _tys _eqvs (force -> VGlue _ _ _ _ _ vl) = vl
  327. unglue a phi tys eqvs (force -> VSystem fs) = VSystem (fmap (unglue a phi tys eqvs) fs)
  328. unglue a phi tys eqvs vl = VUnglue a phi tys eqvs vl
  329. -- Definition of equivalences
  330. faceForall :: (NFEndp -> NFEndp) -> Value
  331. faceForall phi = T.length (getNameText name) `seq` go (phi (VVar name)) where
  332. {-# NOINLINE name #-}
  333. name = unsafePerformIO newName
  334. go x@(VVar n)
  335. | n == name = VI0
  336. | otherwise = x
  337. go x@(VINot (VVar n))
  338. | n == name = VI0
  339. | otherwise = x
  340. go (VIAnd x y) = iand (go x) (go y)
  341. go (VIOr x y) = ior (go x) (go y)
  342. go (VINot x) = inot (go x)
  343. go vl = vl
  344. isContr :: Value -> Value
  345. isContr a = exists' "x" a \x -> dprod' "y" a \y -> VPath (line (const a)) x y
  346. fiber :: NFSort -> NFSort -> Value -> Value -> Value
  347. fiber a b f y = exists' "x" a \x -> VPath (line (const b)) (f @@ x) y
  348. isEquiv :: NFSort -> NFSort -> Value -> Value
  349. isEquiv a b f = dprod' "y" b \y -> isContr (fiber a b f y)
  350. equiv :: NFSort -> NFSort -> Value
  351. equiv a b = exists' "f" (a ~> b) \f -> isEquiv a b f
  352. pres :: (NFEndp -> NFSort) -> (NFEndp -> NFSort) -> (NFEndp -> Value) -> NFEndp -> (NFEndp -> Value) -> Value -> (Value, NFSort, Value)
  353. pres tyT tyA f phi t t0 = (VInc pathT phi (VLine (tyA VI1) c1 c2 (line path)), pathT, fun $ \u -> VLine (fun (const (tyA VI1))) c1 c2 (fun (const (f VI1 @@ (t VI1 @@ u))))) where
  354. pathT = VPath (fun (const (tyA VI1))) c1 c2
  355. c1 = comp (line tyA) phi (system \i u -> f i @@ (t i @@ u)) (VInc (tyA VI0) phi (f VI0 @@ t0))
  356. c2 = f VI1 @@ comp (line tyT) phi (system \i u -> t i @@ u) t0
  357. a0 = f VI0 @@ t0
  358. v = fill (fun tyT) phi (system \i u -> t i @@ u) t0
  359. path j = comp (fun tyA) (phi `ior` j) (system \i _ -> f i @@ (v i)) a0
  360. opEquiv :: HasCallStack => Value -> Value -> Value -> NFEndp -> Value -> Value -> Value -> (Value, NFSort, Value)
  361. opEquiv aT tT f phi t p a = (VInc ty phi v, ty, fun \u -> VPair (t @@ u) (p @@ u)) where
  362. fn = vProj1 f
  363. ty = exists' "f" tT \x -> VPath (line (const aT)) a (fn @@ x)
  364. v = contr ty (vProj2 f @@ a) phi (\u -> VPair (t @@ u) (p @@ u))
  365. contr :: HasCallStack => Value -> Value -> NFEndp -> (Value -> Value) -> Value
  366. contr a aC phi u =
  367. comp (line (const a))
  368. phi
  369. (system \i is1 -> ielim (line (const a)) (vProj1 aC) (u is1) (vProj2 aC @@ u is1) i)
  370. (VInc a phi (vProj1 aC))
  371. transp :: (NFEndp -> Value) -> Value -> Value
  372. transp line a0 = comp (fun line) VI0 (system \_ _ -> VSystem mempty) (VInc (line VI0) VI0 a0)
  373. makeEquiv :: Name -> Value -> Value
  374. makeEquiv var vne = VPair f $ fun \y -> VPair (fib y) (fun \u -> p (vProj1 u) (vProj2 u) y)
  375. where
  376. line = fun \i -> substitute (Map.singleton var (inot i)) vne
  377. a = line @@ VI0
  378. b = line @@ VI1
  379. f = fun \x -> transp (line @@) x
  380. g = fun \x -> transp ((line @@) . inot) x
  381. u i = fun \x -> fill line VI0 (system \_ _ -> mkVSystem mempty) (VInc a VI0 x) i
  382. v i = fun \x -> fill (fun ((line @@) . inot)) VI0 (system \_ _ -> mkVSystem mempty) (VInc a VI1 x) (inot i)
  383. fib y = VPair (g @@ y) (VLine b y (f @@ (g @@ y)) (fun (theta0 y VI1)))
  384. theta0 y i j = fill line (ior j (inot j)) (system \i _ -> mkVSystem (Map.fromList [(j, v i @@ y), (inot j, u i @@ (g @@ y))])) (VInc a (ior j (inot j)) (g @@ y)) i
  385. theta1 x beta y i j =
  386. fill (fun ((line @@) . inot))
  387. (ior j (inot j))
  388. (system \i _ -> mkVSystem (Map.fromList [ (inot j, v (inot i) @@ y)
  389. , (j, u (inot i) @@ x)]))
  390. (VInc b (ior j (inot j)) (ielim b y (f @@ x) beta y))
  391. (inot i)
  392. omega x beta y = theta1 x beta y VI0
  393. delta x beta y j k = comp line (ior k (ior (inot k) (ior j (inot j))))
  394. (system \i _ -> mkVSystem (Map.fromList [ (inot k, theta0 y i j)
  395. , (k, theta1 x beta y i j)
  396. , (inot j, v i @@ y)
  397. , (j, u i @@ omega x beta y k)]))
  398. (VInc a (ior k (ior (inot k) (ior j (inot j)))) (omega x beta y (iand j k)))
  399. p x beta y = VLine (exists a \x -> VPath b y (f @@ x)) (fib y) (VPair x beta) $ fun \k ->
  400. VPair (omega x beta y k) (VLine (VPath b y (f @@ x)) (vProj2 (fib y)) beta $ fun \j -> delta x beta y j k)
  401. idEquiv :: NFSort -> Value
  402. idEquiv a = VPair idfun idisequiv where
  403. idfun = fun id
  404. u_ty = exists' "y" a \x -> VPath (fun (const a)) x x
  405. idisequiv = fun \y -> VPair (id_fiber y) $ fun \u ->
  406. VLine u_ty (id_fiber y) u $ fun \i -> VPair (ielim (fun (const a)) y y (vProj2 u) i) $
  407. VLine (fun (const a)) y (vProj1 u) $ fun \j ->
  408. ielim (fun (const a)) y y (vProj2 u) (iand i j)
  409. id_fiber y = VPair y (VLine a y y (fun (const y)))