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.

588 lines
25 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, prettyVl)
  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. wiType WiSEq = forAll' "A" VTypeω \a -> a ~> a ~> VTypeω
  47. wiType WiSRefl = forAll' "A" VTypeω \a -> forAll' "x" a \x -> VEqStrict a x x
  48. wiType WiSK = forAll' "A" VTypeω \a -> forAll' "x" a \x -> dprod' "P" (VEqStrict a x x ~> VTypeω) \bigp -> (bigp @@ VReflStrict a x) ~> dprod' "p" (VEqStrict a x x) \p -> bigp @@ p
  49. wiType WiSJ = forAll' "A" VTypeω \a -> forAll' "x" a \x -> dprod' "P" (dprod' "y" a \y -> VEqStrict a x y ~> VTypeω) \bigp -> bigp @@ x @@ VReflStrict a x ~> forAll' "y" a \y -> dprod' "p" (VEqStrict a x y) \p -> bigp @@ y @@ p
  50. wiValue :: WiredIn -> Value
  51. wiValue WiType = VType
  52. wiValue WiPretype = VTypeω
  53. wiValue WiInterval = VI
  54. wiValue WiI0 = VI0
  55. wiValue WiI1 = VI1
  56. wiValue WiIAnd = fun \x -> fun \y -> iand x y
  57. wiValue WiIOr = fun \x -> fun \y -> ior x y
  58. wiValue WiINot = fun inot
  59. wiValue WiPathP = fun \a -> fun \x -> fun \y -> VPath a x y
  60. wiValue WiIsOne = fun VIsOne
  61. wiValue WiItIsOne = VItIsOne
  62. wiValue WiPartial = fun \phi -> fun \r -> VPartial phi r
  63. wiValue WiPartialP = fun \phi -> fun \r -> VPartialP phi r
  64. wiValue WiSub = fun \a -> fun \phi -> fun \u -> VSub a phi u
  65. wiValue WiInS = forallI \a -> forallI \phi -> fun \u -> VInc a phi u
  66. wiValue WiOutS = forallI \a -> forallI \phi -> forallI \u -> fun \x -> outS a phi u x
  67. wiValue WiComp = fun \a -> forallI \phi -> fun \u -> fun \x -> comp a phi u x
  68. wiValue WiGlue = fun \a -> forallI \phi -> fun \t -> fun \e -> glueType a phi t e
  69. wiValue WiGlueElem = forallI \a -> forallI \phi -> forallI \ty -> forallI \eqv -> fun \x -> fun \y -> glueElem a phi ty eqv x y
  70. wiValue WiUnglue = forallI \a -> forallI \phi -> forallI \ty -> forallI \eqv -> fun \x -> unglue a phi ty eqv x
  71. wiValue WiSEq = forallI \a -> fun \x -> fun \y -> VEqStrict a x y
  72. wiValue WiSRefl = forallI \a -> forallI \x -> VReflStrict a x
  73. wiValue WiSK = forallI \a -> forallI \x -> fun \bigp -> fun \pr -> fun \p -> strictK a x bigp pr p
  74. wiValue WiSJ = forallI \a -> forallI \x -> fun \bigp -> fun \pr -> forallI \y -> fun \p -> strictJ a x bigp pr y p
  75. (~>) :: Value -> Value -> Value
  76. a ~> b = VPi P.Ex a (Closure (Bound "_" 0) (const b))
  77. infixr 7 ~>
  78. fun, line :: (Value -> Value) -> Value
  79. fun k = VLam P.Ex $ Closure (Bound "x" 0) (k . force)
  80. line k = VLam P.Ex $ Closure (Bound "i" 0) (k . force)
  81. fun' :: String -> (Value -> Value) -> Value
  82. fun' x k = VLam P.Ex $ Closure (Bound (T.pack x) 0) (k . force)
  83. forallI :: (Value -> Value) -> Value
  84. forallI k = VLam P.Im $ Closure (Bound "x" 0) (k . force)
  85. dprod' :: String -> Value -> (Value -> Value) -> Value
  86. dprod' t a b = VPi P.Ex a (Closure (Bound (T.pack t) 0) b)
  87. dprod :: Value -> (Value -> Value) -> Value
  88. dprod = dprod' "x"
  89. exists' :: String -> Value -> (Value -> Value) -> Value
  90. exists' s a b = VSigma a (Closure (Bound (T.pack s) 0) b)
  91. exists :: Value -> (Value -> Value) -> Value
  92. exists = exists' "x"
  93. forAll' :: String -> Value -> (Value -> Value) -> Value
  94. forAll' n a b = VPi P.Im a (Closure (Bound (T.pack n) 0) b)
  95. forAll :: Value -> (Value -> Value) -> Value
  96. forAll = forAll' "x"
  97. wiredInNames :: Map Text WiredIn
  98. wiredInNames = Map.fromList
  99. [ ("Pretype", WiPretype)
  100. , ("Type", WiType)
  101. , ("Interval", WiInterval)
  102. , ("i0", WiI0)
  103. , ("i1", WiI1)
  104. , ("iand", WiIAnd)
  105. , ("ior", WiIOr)
  106. , ("inot", WiINot)
  107. , ("PathP", WiPathP)
  108. , ("IsOne", WiIsOne)
  109. , ("itIs1", WiItIsOne)
  110. , ("Partial", WiPartial)
  111. , ("PartialP", WiPartialP)
  112. , ("Sub", WiSub)
  113. , ("inS", WiInS)
  114. , ("outS", WiOutS)
  115. , ("comp", WiComp)
  116. , ("Glue", WiGlue)
  117. , ("glue", WiGlueElem)
  118. , ("unglue", WiUnglue)
  119. , ("Eq_s", WiSEq)
  120. , ("refl_s", WiSRefl)
  121. , ("K_s", WiSK)
  122. , ("J_s", WiSJ)
  123. ]
  124. newtype NoSuchPrimitive = NoSuchPrimitive { getUnknownPrim :: Text }
  125. deriving (Show, Typeable)
  126. deriving anyclass (Exception)
  127. -- Interval operations
  128. iand, ior :: Value -> Value -> Value
  129. iand x = case force x of
  130. VI1 -> id
  131. VI0 -> const VI0
  132. VIAnd x y -> \z -> case force z of
  133. VI0 -> VI0
  134. VI1 -> VI1
  135. z -> iand x (iand y z)
  136. x -> \y -> case force y of
  137. VI0 -> VI0
  138. VI1 -> x
  139. y -> VIAnd x y
  140. ior x = case force x of
  141. VI0 -> id
  142. VI1 -> const VI1
  143. VIOr x y -> \z -> case force z of
  144. VI1 -> VI1
  145. VI0 -> VIOr x y
  146. _ -> ior x (ior y z)
  147. x -> \y -> case force y of
  148. VI1 -> VI1
  149. VI0 -> x
  150. y -> VIOr x y
  151. inot :: Value -> Value
  152. inot x = case force x of
  153. VI0 -> VI1
  154. VI1 -> VI0
  155. VIOr x y -> VIAnd (inot x) (inot y)
  156. VIAnd x y -> VIOr (inot x) (inot y)
  157. VINot x -> x
  158. x -> VINot x
  159. ielim :: Value -> Value -> Value -> Value -> NFEndp -> Value
  160. ielim line left right (GluedVl h sp vl) i =
  161. GluedVl h (sp Seq.:|> PIElim line left right i) (ielim line left right vl i)
  162. ielim line left right fn i =
  163. case force fn of
  164. VLine _ _ _ fun -> fun @@ i
  165. VLam _ (Closure _ k) -> k i
  166. x -> case force i of
  167. VI1 -> right
  168. VI0 -> left
  169. _ -> case x of
  170. VNe n sp -> VNe n (sp Seq.:|> PIElim line left right i)
  171. VSystem map -> VSystem (fmap (flip (ielim line left right) i) map)
  172. VInc (VPath _ _ _) _ u -> ielim line left right u i
  173. VCase env r x xs -> VCase env r x (fmap (fmap (flip (IElim (quote line) (quote left) (quote right)) (quote i))) xs)
  174. _ -> error $ "can't ielim " ++ show (prettyTm (quote fn))
  175. outS :: HasCallStack => NFSort -> NFEndp -> Value -> Value -> Value
  176. outS _ (force -> VI1) u _ = u @@ VItIsOne
  177. outS _ _phi _ (VInc _ _ x) = x
  178. outS _ VI0 _ x = x
  179. outS a phi u (GluedVl x sp vl) = GluedVl x (sp Seq.:|> POuc a phi u) (outS a phi u vl)
  180. outS a phi u (VNe x sp) = VNe x (sp Seq.:|> POuc a phi u)
  181. outS a phi u (VSystem fs) = mkVSystem (fmap (outS a phi u) fs)
  182. outS _ _ _ v = error $ "can't outS " ++ show (prettyTm (quote v))
  183. -- Composition
  184. comp :: HasCallStack => NFLine -> NFEndp -> Value -> Value -> Value
  185. comp _a VI1 u _a0 = u @@ VI1 @@ VItIsOne
  186. comp a psi@phi u incA0@(compOutS (a @@ VI1) phi (u @@ VI1 @@ VItIsOne) -> a0) =
  187. case force (a @@ VVar name) of
  188. VPi{} ->
  189. let
  190. plic i = let VPi p _ _ = force (a @@ i) in p
  191. dom i = let VPi _ d _ = force (a @@ i) in d
  192. rng i = let VPi _ _ (Closure _ r) = force (a @@ i) in r
  193. y' i y = fill (fun (dom . inot)) VI0 (fun \_ -> fun \_ -> VSystem mempty) (VInc (dom VI0) phi y) i
  194. ybar i y = y' (inot i) y
  195. in VLam (plic VI1) . Closure (Bound "x" 0) $ \arg ->
  196. comp (line \i -> rng i (ybar i arg))
  197. phi
  198. (system \i isone -> vApp (plic i) (u @@ i @@ isone) (ybar i arg))
  199. (VInc (rng VI0 (ybar VI0 arg)) phi (vApp (plic VI0) a0 (ybar VI0 arg)))
  200. VSigma{} ->
  201. let
  202. dom i = let VSigma d _ = force (a @@ i) in d
  203. rng i = let VSigma _ (Closure _ r) = force (a @@ i) in r
  204. w i = fill (fun dom) phi (system \i isone -> vProj1 (u @@ i @@ isone)) (VInc (dom VI0) phi (vProj1 a0)) i
  205. c2 = comp (fun \x -> rng x (w x)) phi (system \i isone -> vProj2 (u @@ i @@ isone)) (VInc (rng VI0 (w VI0)) phi (vProj2 a0))
  206. in
  207. VPair (w VI1) c2
  208. VPath{} ->
  209. let
  210. a' i = let VPath thea _ _ = force (a @@ i) in thea
  211. u' i = let VPath _ theu _ = force (a @@ i) in theu
  212. v' i = let VPath _ _ thev = force (a @@ i) in thev
  213. in
  214. VLine (a' VI1 @@ VI1) (u' VI1) (v' VI1) $ fun \j ->
  215. comp (fun \x -> a' x @@ x)
  216. (phi `ior` j `ior` inot j)
  217. (system \i isone -> mkVSystem (Map.fromList [ (phi, ielim (a' VI0) (u' VI0) (v' VI0) (u @@ i @@ isone) j)
  218. , (j, v' i)
  219. , (inot j, u' i)]))
  220. (VInc (a' VI0 @@ VI0 @@ j) phi (ielim (a' VI0 @@ VI0) (u' VI0) (v' VI0) a0 j))
  221. VGlueTy _ thePhi theTypes theEquivs ->
  222. let
  223. b = u
  224. b0 = a0
  225. fam = a
  226. in
  227. let
  228. base i = let VGlueTy b _ _ _ = forceAndGlue (fam @@ i) in b
  229. phi i = substitute (Map.singleton name i) thePhi
  230. types i = substitute (Map.singleton name i) theTypes @@ VItIsOne
  231. equivs i = substitute (Map.singleton name i) theEquivs
  232. a i u = unglue (base i) (phi i) (types i @@ u) (equivs i) (b @@ i @@ u)
  233. a0 = unglue (base VI0) (phi VI0) (types VI0) (equivs VI0) b0
  234. del = faceForall phi
  235. a1' = comp (line base) psi (system a) (VInc (base VI0) psi a0)
  236. t1' = comp (line (const (types VI0))) psi (line (b @@)) (VInc (base VI0) psi b0)
  237. (omega_st, omega_t, omega_rep) = pres types base equivs psi (b @@) b0
  238. omega = outS omega_t psi omega_rep omega_st
  239. (t1alpha_st, t1a_t, t1a_rep) = opEquiv (base VI1) (types VI1) (equivs VI1 @@ VItIsOne) (del `ior` psi) (fun ts) (fun ps) a1'
  240. t1alpha = outS t1a_t (del `ior` psi) t1a_rep t1alpha_st
  241. (t1, alpha) = (vProj1 t1alpha, vProj2 t1alpha)
  242. ts isone = mkVSystem . Map.fromList $ [(del, t1'), (psi, (b @@ VI1 @@ isone))]
  243. ps _isone = mkVSystem . Map.fromList $ [(del, omega), (psi, VLine (line (const (base VI1))) a1' a1' (fun (const a1')))]
  244. a1 = comp
  245. (fun (const (base VI1)))
  246. (phi VI1 `ior` psi)
  247. (system \j _u -> mkVSystem (Map.fromList [ (phi VI1, ielim (base VI1) a1' (vProj1 (equivs VI1 @@ VItIsOne)) alpha j)
  248. , (psi, a VI1 VItIsOne)]))
  249. (VInc (base VI1) (phi VI1 `ior` psi) a1')
  250. b1 = glueElem (base VI1) (phi VI1) (types VI1) (equivs VI1) (fun (const t1)) a1
  251. in b1
  252. VType -> VGlueTy a0 phi (fun' "is1" \is1 -> u @@ VI1 @@ is1)
  253. (fun' "is1" \_ -> mapVSystem (makeEquiv equivVar) (u @@ VVar equivVar @@ VItIsOne))
  254. VNe (HData False _) Seq.Empty -> a0
  255. VNe (HData False _) args ->
  256. case force a0 of
  257. VNe (HCon con_type con_name) con_args ->
  258. VNe (HCon con_type con_name) $ compConArgs makeSetFiller (length args) (a @@) con_type con_args phi u
  259. _ -> VComp a phi u (VInc (a @@ VI0) phi a0)
  260. VNe (HData True name) args -> compHIT name (length args) (a @@) phi u incA0
  261. VLam{} -> error $ "comp VLam " ++ show (prettyTm (quote a))
  262. sys@VSystem{} -> error $ "comp VSystem: " ++ show (prettyTm (quote sys))
  263. _ -> VComp a phi u (VInc (a @@ VI0) phi a0)
  264. where
  265. {-# NOINLINE name #-}
  266. name = unsafePerformIO newName
  267. {-# NOINLINE equivVar #-}
  268. equivVar = unsafePerformIO newName
  269. mapVSystem :: (Value -> Value) -> Value -> Value
  270. mapVSystem f (VSystem fs) = VSystem (fmap f fs)
  271. mapVSystem f x = f x
  272. forceAndGlue :: Value -> Value
  273. forceAndGlue v =
  274. case force v of
  275. v@VGlueTy{} -> v
  276. y -> VGlueTy y VI1 (fun (const y)) (fun (const (idEquiv y)))
  277. compHIT :: HasCallStack => Name -> Int -> (NFEndp -> NFSort) -> NFEndp -> Value -> Value -> Value
  278. compHIT name n a phi u a0 =
  279. case force phi of
  280. VI1 -> u @@ VI1 @@ VItIsOne
  281. VI0 | n == 0 -> compOutS (a VI0) phi u a0
  282. | otherwise -> transHit name a VI0 (compOutS (a VI0) phi u a0)
  283. x -> go n a x u a0
  284. where
  285. go 0 a phi u a0 = VHComp (a VI0) phi u a0
  286. go _ a phi u a0 = VHComp (a VI1) phi (system \i n -> transSqueeze name a VI0 (\i -> u @@ i @@ n) i) (transHit name a VI0 (compOutS (a VI1) phi (u @@ VI1 @@ VItIsOne) a0))
  287. compConArgs :: (Name -> Int -> Value -> t1 -> t2 -> Value -> Value)
  288. -> Int
  289. -> (Value -> Value)
  290. -> Value
  291. -> Seq.Seq Projection
  292. -> t1 -> t2
  293. -> Seq.Seq Projection
  294. compConArgs makeFiller total_args fam = go total_args where
  295. go _ _ Seq.Empty _ _ = Seq.Empty
  296. go nargs (VPi p dom (Closure _ rng)) (PApp p' y Seq.:<| xs) phi u
  297. | nargs > 0 = assert (p == p') $
  298. PApp p' (nthArg (total_args - nargs) (fam VI1)) Seq.:<| go (nargs - 1) (rng (smuggle (fun (\i -> nthArg (total_args - nargs) (fam i))))) xs phi u
  299. | otherwise = assert (p == p') $
  300. let fill = makeFiller typeArgument nargs dom phi u y
  301. in PApp p' (fill @@ VI1) Seq.:<| go (nargs - 1) (rng fill) xs phi u
  302. go _ _ _ _ _ = error $ "invalid constructor"
  303. smuggle x = VNe (HData False typeArgument) (Seq.singleton (PApp P.Ex x))
  304. typeArgument = unsafePerformIO newName
  305. {-# NOINLINE typeArgument #-}
  306. makeSetFiller :: Name -> Int -> Value -> NFEndp -> Value -> Value -> Value
  307. makeSetFiller typeArgument nth (VNe (HData _ n') args) phi u a0
  308. | n' == typeArgument =
  309. fun $ fill (makeDomain args) phi (system \i is1 -> nthArg nth (u @@ i @@ is1) ) a0
  310. where
  311. makeDomain (PApp _ x Seq.:<| xs) = fun \i -> foldl (\t (~(PApp _ x)) -> t @@ (x @@ i)) (x @@ i) xs
  312. makeDomain _ = error "somebody smuggled something that smells"
  313. makeSetFiller _ _ _ _ _ a0 = fun (const a0)
  314. nthArg :: Int -> Value -> Value
  315. nthArg i (force -> VNe hd s) =
  316. case s Seq.!? i of
  317. Just (PApp _ t) -> t
  318. _ -> error $ "invalid " ++ show i ++ "th argument to data type " ++ show hd
  319. nthArg i (force -> VSystem vs) = VSystem (fmap (nthArg i) vs)
  320. nthArg i xs = error $ "can't get " ++ show i ++ "th argument of " ++ show (prettyTm (quote xs))
  321. compOutS :: HasCallStack => NFSort -> NFEndp -> Value -> Value -> Value
  322. compOutS a b c d = compOutS a b c (force d) where
  323. compOutS _ _hi _0 vl@VComp{} = error $ "unwrapped composition given as input to composition operation is fuckign illegal " ++ show (prettyTm (quote (zonk vl)))
  324. compOutS _ _hi _0 vl@VHComp{} = error $ "unwrapped composition (gay) given as input to composition operation is fuckign illegal " ++ show (prettyTm (quote (zonk vl)))
  325. compOutS _ _hi _0 (VInc _ _ x) = x
  326. compOutS a phi a0 v = outS a phi a0 v
  327. system :: (Value -> Value -> Value) -> Value
  328. system k = VLam P.Ex $ Closure (Bound "i" 0) \i -> VLam P.Ex $ Closure (Bound "[i]" 0) \isone -> k i isone
  329. fill :: HasCallStack => NFLine -> NFEndp -> Value -> Value -> NFEndp -> Value
  330. fill a phi u a0 j =
  331. comp (line \i -> a @@ (i `iand` j))
  332. (phi `ior` inot j)
  333. (system \i isone -> mkVSystem (Map.fromList [ (phi, u @@ (i `iand` j) @@ isone)
  334. , (inot j, outS a phi (u @@ VI0) a0)]))
  335. a0
  336. hComp :: NFSort -> NFEndp -> Value -> Value -> Value
  337. hComp _ (force -> VI1) u _ = u @@ VI1 @@ VItIsOne
  338. hComp a phi u a0 = VHComp a phi u a0
  339. glueType :: NFSort -> NFEndp -> NFPartial -> NFPartial -> Value
  340. glueType a phi tys eqvs = VGlueTy a phi tys eqvs
  341. glueElem :: NFSort -> NFEndp -> NFPartial -> NFPartial -> NFPartial -> Value -> Value
  342. glueElem _a (force -> VI1) _tys _eqvs t _vl = t @@ VItIsOne
  343. glueElem a phi tys eqvs t vl = VGlue a phi tys eqvs t vl
  344. unglue :: HasCallStack => NFSort -> NFEndp -> NFPartial -> NFPartial -> Value -> Value
  345. unglue _a (force -> VI1) _tys eqvs x = vProj1 (eqvs @@ VItIsOne) @@ x
  346. unglue _a _phi _tys _eqvs (force -> VGlue _ _ _ _ _ vl) = vl
  347. unglue a phi tys eqvs (force -> VSystem fs) = VSystem (fmap (unglue a phi tys eqvs) fs)
  348. unglue a phi tys eqvs vl = VUnglue a phi tys eqvs vl
  349. -- Definition of equivalences
  350. faceForall :: (NFEndp -> NFEndp) -> Value
  351. faceForall phi = T.length (getNameText name) `seq` go (phi (VVar name)) where
  352. {-# NOINLINE name #-}
  353. name = unsafePerformIO newName
  354. go x@(VVar n)
  355. | n == name = VI0
  356. | otherwise = x
  357. go x@(VINot (VVar n))
  358. | n == name = VI0
  359. | otherwise = x
  360. go (VIAnd x y) = iand (go x) (go y)
  361. go (VIOr x y) = ior (go x) (go y)
  362. go (VINot x) = inot (go x)
  363. go vl = vl
  364. isContr :: Value -> Value
  365. isContr a = exists' "x" a \x -> dprod' "y" a \y -> VPath (line (const a)) x y
  366. fiber :: NFSort -> NFSort -> Value -> Value -> Value
  367. fiber a b f y = exists' "x" a \x -> VPath (line (const b)) (f @@ x) y
  368. isEquiv :: NFSort -> NFSort -> Value -> Value
  369. isEquiv a b f = dprod' "y" b \y -> isContr (fiber a b f y)
  370. equiv :: NFSort -> NFSort -> Value
  371. equiv a b = exists' "f" (a ~> b) \f -> isEquiv a b f
  372. pres :: (NFEndp -> NFSort) -> (NFEndp -> NFSort) -> (NFEndp -> Value) -> NFEndp -> (NFEndp -> Value) -> Value -> (Value, NFSort, Value)
  373. 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
  374. pathT = VPath (fun (const (tyA VI1))) c1 c2
  375. c1 = comp (line tyA) phi (system \i u -> f i @@ (t i @@ u)) (VInc (tyA VI0) phi (f VI0 @@ t0))
  376. c2 = f VI1 @@ comp (line tyT) phi (system \i u -> t i @@ u) t0
  377. a0 = f VI0 @@ t0
  378. v = fill (fun tyT) phi (system \i u -> t i @@ u) t0
  379. path j = comp (fun tyA) (phi `ior` j) (system \i _ -> f i @@ (v i)) a0
  380. opEquiv :: HasCallStack => Value -> Value -> Value -> NFEndp -> Value -> Value -> Value -> (Value, NFSort, Value)
  381. opEquiv aT tT f phi t p a = (VInc ty phi v, ty, fun \u -> VPair (t @@ u) (p @@ u)) where
  382. fn = vProj1 f
  383. ty = exists' "f" tT \x -> VPath (line (const aT)) a (fn @@ x)
  384. v = contr ty (vProj2 f @@ a) phi (\u -> VPair (t @@ u) (p @@ u))
  385. contr :: HasCallStack => Value -> Value -> NFEndp -> (Value -> Value) -> Value
  386. contr a aC phi u =
  387. comp (line (const a))
  388. phi
  389. (system \i is1 -> ielim (line (const a)) (vProj1 aC) (u is1) (vProj2 aC @@ u is1) i)
  390. (VInc a phi (vProj1 aC))
  391. transp :: (NFEndp -> Value) -> Value -> Value
  392. transp line a0 = comp (fun line) VI0 (system \_ _ -> VSystem mempty) (VInc (line VI0) VI0 a0)
  393. gtrans :: (NFEndp -> Value) -> NFEndp -> Value -> Value
  394. gtrans line phi a0 = comp (fun line) phi (system \_ _ -> mkVSystem (Map.singleton phi a0)) (VInc (line VI0) VI0 a0)
  395. transHit :: Name -> (NFEndp -> Value) -> NFEndp -> Value -> Value
  396. transHit name line phi x = transHit name line phi (force x) where
  397. transHit name line phi (VHComp _ psi u u0) = VHComp (line VI1) psi (system \i j -> transHit name line phi (u @@ i @@ j)) (transHit name line phi (compOutS (line VI0) phi u u0))
  398. transHit name line phi (VNe (HCon con_type con_name) spine) | ourType = x' where
  399. x' = VNe (HCon con_type con_name) $ compConArgs (makeTransFiller name) nargs line con_type spine phi ()
  400. (_, VNe hd (length -> nargs)) = unPi con_type
  401. ourType = case hd of
  402. HData True n' -> n' == name
  403. _ -> False
  404. transHit name line phi (VNe (HPCon sys con_type con_name) spine) | ourType = x' where
  405. x' = VNe (HPCon (mapVSystem rec sys) con_type con_name) $ compConArgs (makeTransFiller name) nargs line con_type spine phi ()
  406. rec = transHit name line phi
  407. (_, VNe hd (length -> nargs)) = unPi con_type
  408. ourType = case hd of
  409. HData True n' -> n' == name
  410. _ -> False
  411. transHit name line phi (VSystem xs) = mkVSystem (fmap (transHit name line phi) xs)
  412. transHit _ line phi a0 = gtrans line phi a0
  413. transFill :: Name -> (NFEndp -> Value) -> NFEndp -> Value -> NFEndp -> Value
  414. transFill name a phi a0 i = transHit name (\j -> a (iand i j)) (phi `ior` inot i) a0 where
  415. transSqueeze :: Name -> (NFEndp -> Value) -> NFEndp -> (NFEndp -> Value) -> NFEndp -> Value
  416. transSqueeze name a phi x i = transHit name (\j -> a (ior i j)) (phi `ior` i) (x i)
  417. makeTransFiller :: Name -> Name -> p -> Value -> NFEndp -> () -> Value -> Value
  418. makeTransFiller thedata typeArgument _ (VNe (HData _ n') args) phi () a0
  419. | n' == typeArgument = fun (transFill thedata (makeDomain args) phi a0)
  420. where
  421. makeDomain (PApp _ x Seq.:<| xs) = \i -> foldl (\t (~(PApp _ x)) -> t @@ (x @@ i)) (x @@ i) xs
  422. makeDomain _ = error "somebody smuggled something that smells"
  423. makeTransFiller _ _ _ _ _ _ a0 = fun (const a0)
  424. makeEquiv :: Name -> Value -> Value
  425. makeEquiv var vne = VPair f $ fun \y -> VPair (fib y) (fun \u -> p (vProj1 u) (vProj2 u) y)
  426. where
  427. line = fun \i -> substitute (Map.singleton var (inot i)) vne
  428. a = line @@ VI0
  429. b = line @@ VI1
  430. f = fun \x -> transp (line @@) x
  431. g = fun \x -> transp ((line @@) . inot) x
  432. u i = fun \x -> fill line VI0 (system \_ _ -> mkVSystem mempty) (VInc a VI0 x) i
  433. v i = fun \x -> fill (fun ((line @@) . inot)) VI0 (system \_ _ -> mkVSystem mempty) (VInc a VI1 x) (inot i)
  434. fib y = VPair (g @@ y) (VLine b y (f @@ (g @@ y)) (fun (theta0 y VI1)))
  435. 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
  436. theta1 x beta y i j =
  437. fill (fun ((line @@) . inot))
  438. (ior j (inot j))
  439. (system \i _ -> mkVSystem (Map.fromList [ (inot j, v (inot i) @@ y)
  440. , (j, u (inot i) @@ x)]))
  441. (VInc b (ior j (inot j)) (ielim b y (f @@ x) beta y))
  442. (inot i)
  443. omega x beta y = theta1 x beta y VI0
  444. delta x beta y j k = comp line (ior k (ior (inot k) (ior j (inot j))))
  445. (system \i _ -> mkVSystem (Map.fromList [ (inot k, theta0 y i j)
  446. , (k, theta1 x beta y i j)
  447. , (inot j, v i @@ y)
  448. , (j, u i @@ omega x beta y k)]))
  449. (VInc a (ior k (ior (inot k) (ior j (inot j)))) (omega x beta y (iand j k)))
  450. p x beta y = VLine (exists a \x -> VPath b y (f @@ x)) (fib y) (VPair x beta) $ fun \k ->
  451. VPair (omega x beta y k) (VLine (VPath b y (f @@ x)) (vProj2 (fib y)) beta $ fun \j -> delta x beta y j k)
  452. idEquiv :: NFSort -> Value
  453. idEquiv a = VPair idfun idisequiv where
  454. idfun = fun id
  455. u_ty = exists' "y" a \x -> VPath (fun (const a)) x x
  456. idisequiv = fun \y -> VPair (id_fiber y) $ fun \u ->
  457. VLine u_ty (id_fiber y) u $ fun \i -> VPair (ielim (fun (const a)) y y (vProj2 u) i) $
  458. VLine (fun (const a)) y (vProj1 u) $ fun \j ->
  459. ielim (fun (const a)) y y (vProj2 u) (iand i j)
  460. id_fiber y = VPair y (VLine a y y (fun (const y)))
  461. strictK :: Value -> Value -> Value -> Value -> Value -> Value
  462. strictK _ _ _ pr (VReflStrict _ _) = pr
  463. strictK a x bigp pr (VNe h sp) = VNe h (sp Seq.:|> PK a x bigp pr)
  464. strictK a x bigp pr (VCase env rng sc cases) = VCase env rng sc (map (projIntoCase func) cases) where
  465. func = AxK (quote a) (quote x) (quote bigp) (quote pr)
  466. strictK a x bigp pr (GluedVl h sp vl) = GluedVl h (sp Seq.:|> PK a x bigp pr) (strictK a x bigp pr vl)
  467. strictK _ _ _ _r eq = error $ "can't K " ++ show (prettyVl eq)
  468. strictJ :: Value -> Value -> Value -> Value -> Value -> Value -> Value
  469. strictJ _a _x _bigp pr _ (VReflStrict _ _) = pr
  470. strictJ a x bigp pr y (VNe h sp) = VNe h (sp Seq.:|> PJ a x bigp pr y)
  471. strictJ a x bigp pr y (VCase env rng sc cases) = VCase env rng sc (map (projIntoCase func) cases) where
  472. func = AxJ (quote a) (quote x) (quote bigp) (quote pr) (quote y)
  473. strictJ a x bigp pr y (GluedVl h sp vl) = GluedVl h (sp Seq.:|> PJ a x bigp pr y) (strictJ a x bigp pr y vl)
  474. strictJ _ _ _ _r _ eq = error $ "can't J " ++ show eq
  475. projIntoCase :: (Term -> Term) -> (Term, Int, Term) -> (Term, Int, Term)
  476. projIntoCase fun (pat, nLams, term) = (pat, nLams, go nLams term) where
  477. go 0 x = fun x
  478. go n (Lam p x r) = Lam p x (go (n - 1) r)
  479. go n (PathIntro l a b r) = PathIntro l a b (go (n - 1) r)
  480. go _ x = x