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.

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