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.

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