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.

633 lines
26 KiB

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