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.

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