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.

424 lines
16 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)
  19. import Syntax
  20. import System.IO.Unsafe
  21. wiType :: WiredIn -> NFType
  22. wiType WiType = VType
  23. wiType WiPretype = VTypeω
  24. wiType WiInterval = VTypeω
  25. wiType WiI0 = VI
  26. wiType WiI1 = VI
  27. wiType WiIAnd = VI ~> VI ~> VI
  28. wiType WiIOr = VI ~> VI ~> VI
  29. wiType WiINot = VI ~> VI
  30. wiType WiPathP = dprod (VI ~> VType) \a -> a @@ VI0 ~> a @@ VI1 ~> VType
  31. wiType WiIsOne = VI ~> VTypeω
  32. wiType WiItIsOne = VIsOne VI1
  33. wiType WiPartial = VI ~> VType ~> VTypeω
  34. wiType WiPartialP = dprod VI \x -> VPartial x VType ~> VTypeω
  35. wiType WiSub = dprod VType \a -> dprod VI \phi -> VPartial phi a ~> VTypeω
  36. wiType WiInS = forAll VType \a -> forAll VI \phi -> dprod a \u -> VSub a phi (fun (const u))
  37. wiType WiOutS = forAll VType \a -> forAll VI \phi -> forAll (VPartial phi a) \u -> VSub a phi u ~> a
  38. wiType WiComp = dprod' "A" (VI ~> VType) \a -> forAll VI \phi -> dprod (dprod VI \i -> VPartial phi (a @@ i)) \u -> VSub (a @@ VI0) phi (u @@ VI0) ~> a @@ VI1
  39. -- (A : Type) {phi : I} (T : Partial phi Type) (e : PartialP phi (\o -> Equiv (T o) A)) -> Type
  40. wiType WiGlue = dprod' "A" VType \a -> forAll' "phi" VI \phi -> dprod' "T" (VPartial phi VType) \t -> VPartialP phi (fun \o -> equiv (t @@ o) a) ~> VType
  41. -- {A : Type} {phi : I} {T : Partial phi Type} {e : PartialP phi (\o -> Equiv (T o) A)} -> (t : PartialP phi T) -> Sub A phi (\o -> e o (t o)) -> Glue A phi T e
  42. wiType WiGlueElem = forAll' "A" VType \a -> forAll' "phi" VI \phi -> forAll' "T" (VPartial phi VType) \ty -> forAll' "e" (VPartialP phi (fun \o -> equiv (ty @@ o) a)) \eqv ->
  43. dprod' "t" (VPartialP phi ty) \t -> VSub a phi (fun \o -> vProj1 (eqv @@ o) @@ (t @@ o)) ~> VGlueTy a phi ty eqv
  44. -- {A : Type} {phi : I} {T : Partial phi Type} {e : PartialP phi (\o -> Equiv (T o) A)} -> Glue A phi T e -> A
  45. wiType WiUnglue = forAll' "A" VType \a -> forAll' "phi" VI \phi -> forAll' "T" (VPartial phi VType) \ty -> forAll' "e" (VPartialP phi (fun \o -> equiv (ty @@ o) a)) \e -> VGlueTy a phi ty e ~> a
  46. wiValue :: WiredIn -> Value
  47. wiValue WiType = VType
  48. wiValue WiPretype = VTypeω
  49. wiValue WiInterval = VI
  50. wiValue WiI0 = VI0
  51. wiValue WiI1 = VI1
  52. wiValue WiIAnd = fun \x -> fun \y -> iand x y
  53. wiValue WiIOr = fun \x -> fun \y -> ior x y
  54. wiValue WiINot = fun inot
  55. wiValue WiPathP = fun \a -> fun \x -> fun \y -> VPath a x y
  56. wiValue WiIsOne = fun VIsOne
  57. wiValue WiItIsOne = VItIsOne
  58. wiValue WiPartial = fun \phi -> fun \r -> VPartial phi r
  59. wiValue WiPartialP = fun \phi -> fun \r -> VPartialP phi r
  60. wiValue WiSub = fun \a -> fun \phi -> fun \u -> VSub a phi u
  61. wiValue WiInS = forallI \a -> forallI \phi -> fun \u -> VInc a phi u
  62. wiValue WiOutS = forallI \a -> forallI \phi -> forallI \u -> fun \x -> outS a phi u x
  63. wiValue WiComp = fun \a -> forallI \phi -> fun \u -> fun \x -> comp a phi u x
  64. wiValue WiGlue = fun \a -> forallI \phi -> fun \t -> fun \e -> glueType a phi t e
  65. wiValue WiGlueElem = forallI \a -> forallI \phi -> forallI \ty -> forallI \eqv -> fun \x -> fun \y -> glueElem a phi ty eqv x y
  66. wiValue WiUnglue = forallI \a -> forallI \phi -> forallI \ty -> forallI \eqv -> fun \x -> unglue a phi ty eqv x
  67. (~>) :: Value -> Value -> Value
  68. a ~> b = VPi P.Ex a (Closure (Bound "_" 0) (const b))
  69. infixr 7 ~>
  70. fun, line :: (Value -> Value) -> Value
  71. fun k = VLam P.Ex $ Closure (Bound "x" 0) (k . force)
  72. line k = VLam P.Ex $ Closure (Bound "i" 0) (k . force)
  73. forallI :: (Value -> Value) -> Value
  74. forallI k = VLam P.Im $ Closure (Bound "x" 0) (k . force)
  75. dprod' :: String -> Value -> (Value -> Value) -> Value
  76. dprod' t a b = VPi P.Ex a (Closure (Bound (T.pack t) 0) b)
  77. dprod :: Value -> (Value -> Value) -> Value
  78. dprod = dprod' "x"
  79. exists' :: String -> Value -> (Value -> Value) -> Value
  80. exists' s a b = VSigma a (Closure (Bound (T.pack s) 0) b)
  81. exists :: Value -> (Value -> Value) -> Value
  82. exists = exists' "x"
  83. forAll' :: String -> Value -> (Value -> Value) -> Value
  84. forAll' n a b = VPi P.Im a (Closure (Bound (T.pack n) 0) b)
  85. forAll :: Value -> (Value -> Value) -> Value
  86. forAll = forAll' "x"
  87. wiredInNames :: Map Text WiredIn
  88. wiredInNames = Map.fromList
  89. [ ("Pretype", WiPretype)
  90. , ("Type", WiType)
  91. , ("Interval", WiInterval)
  92. , ("i0", WiI0)
  93. , ("i1", WiI1)
  94. , ("iand", WiIAnd)
  95. , ("ior", WiIOr)
  96. , ("inot", WiINot)
  97. , ("PathP", WiPathP)
  98. , ("IsOne", WiIsOne)
  99. , ("itIs1", WiItIsOne)
  100. , ("Partial", WiPartial)
  101. , ("PartialP", WiPartialP)
  102. , ("Sub", WiSub)
  103. , ("inS", WiInS)
  104. , ("outS", WiOutS)
  105. , ("comp", WiComp)
  106. , ("Glue", WiGlue)
  107. , ("glue", WiGlueElem)
  108. , ("unglue", WiUnglue)
  109. ]
  110. newtype NoSuchPrimitive = NoSuchPrimitive { getUnknownPrim :: Text }
  111. deriving (Show, Typeable)
  112. deriving anyclass (Exception)
  113. -- Interval operations
  114. iand, ior :: Value -> Value -> Value
  115. iand x = case force x of
  116. VI1 -> id
  117. VI0 -> const VI0
  118. VIAnd x y -> \z -> case force z of
  119. VI0 -> VI0
  120. VI1 -> VI1
  121. z -> iand x (iand y z)
  122. x -> \y -> case force y of
  123. VI0 -> VI0
  124. VI1 -> x
  125. y -> VIAnd x y
  126. ior x = case force x of
  127. VI0 -> id
  128. VI1 -> const VI1
  129. VIOr x y -> \z -> case force z of
  130. VI1 -> VI1
  131. VI0 -> VIOr x y
  132. _ -> ior x (ior y z)
  133. x -> \y -> case force y of
  134. VI1 -> VI1
  135. VI0 -> x
  136. y -> VIOr x y
  137. inot :: Value -> Value
  138. inot x = case force x of
  139. VI0 -> VI1
  140. VI1 -> VI0
  141. VIOr x y -> VIAnd (inot x) (inot y)
  142. VIAnd x y -> VIOr (inot x) (inot y)
  143. VINot x -> x
  144. x -> VINot x
  145. ielim :: Value -> Value -> Value -> Value -> NFEndp -> Value
  146. ielim line left right (GluedVl h sp vl) i =
  147. GluedVl h (sp Seq.:|> PIElim line left right i) (ielim line left right vl i)
  148. ielim line left right fn i =
  149. case force fn of
  150. VLine _ _ _ fun -> fun @@ i
  151. x -> case force i of
  152. VI1 -> right
  153. VI0 -> left
  154. _ -> case x of
  155. VNe n sp -> VNe n (sp Seq.:|> PIElim line left right i)
  156. VSystem map -> VSystem (fmap (flip (ielim line left right) i) map)
  157. VInc (VPath _ _ _) _ u -> ielim line left right u i
  158. VCase x xs -> VCase x (fmap (fmap (flip (ielim line left right) i)) xs)
  159. _ -> error $ "can't ielim " ++ show (prettyTm (quote fn))
  160. outS :: NFSort -> NFEndp -> Value -> Value -> Value
  161. outS _ (force -> VI1) u _ = u @@ VItIsOne
  162. outS _ _phi _ (VInc _ _ x) = x
  163. outS _ VI0 _ x = x
  164. outS a phi u (GluedVl x sp vl) = GluedVl x (sp Seq.:|> POuc a phi u) (outS a phi u vl)
  165. outS a phi u (VNe x sp) = VNe x (sp Seq.:|> POuc a phi u)
  166. outS _ _ _ v = error $ "can't outS " ++ show (prettyTm (quote v))
  167. -- Composition
  168. comp :: NFLine -> NFEndp -> Value -> Value -> Value
  169. comp _ VI1 u _ = u @@ VI1 @@ VItIsOne
  170. comp a psi@phi u (compOutS (a @@ VI1) phi (u @@ VI1 @@ VItIsOne) -> a0) =
  171. case force $ a @@ VVar (Bound (T.pack "i") 0) of
  172. VPi{} ->
  173. let
  174. plic i = let VPi p _ _ = force (a @@ i) in p
  175. dom i = let VPi _ d _ = force (a @@ i) in d
  176. rng i = let VPi _ _ (Closure _ r) = force (a @@ i) in r
  177. y' i y = fill (fun (dom . inot)) VI0 (fun \_ -> fun \_ -> VSystem mempty) (VInc (dom VI0) phi y) i
  178. ybar i y = y' (inot i) y
  179. in VLam (plic VI1) . Closure (Bound "x" 0) $ \arg ->
  180. comp (line \i -> rng i (ybar i arg))
  181. phi
  182. (system \i isone -> vApp (plic i) (u @@ i @@ isone) (ybar i arg))
  183. (VInc (rng VI0 (ybar VI0 arg)) phi (vApp (plic VI0) a0 (ybar VI0 arg)))
  184. VSigma{} ->
  185. let
  186. dom i = let VSigma d _ = force (a @@ i) in d
  187. rng i = let VSigma _ (Closure _ r) = force (a @@ i) in r
  188. w i = fill (fun dom) phi (system \i isone -> vProj1 (u @@ i @@ isone)) (VInc (dom VI0) phi (vProj1 a0)) i
  189. c1 = comp (fun dom) phi (system \i isone -> vProj1 (u @@ i @@ isone)) (VInc (dom VI0) phi (vProj1 a0))
  190. c2 = comp (fun \x -> rng x (w x)) phi (system \i isone -> vProj2 (u @@ i @@ isone)) (VInc (rng VI0 (w VI0)) phi (vProj2 a0))
  191. in
  192. VPair c1 c2
  193. VPath{} ->
  194. let
  195. a' i = let VPath thea _ _ = force (a @@ i) in thea
  196. u' i = let VPath _ theu _ = force (a @@ i) in theu
  197. v' i = let VPath _ _ thev = force (a @@ i) in thev
  198. in
  199. VLine (a' VI1 @@ VI1) (u' VI1) (v' VI1) $ fun \j ->
  200. comp (fun \x -> a' x @@ x)
  201. (phi `ior` j `ior` inot j)
  202. (system \i isone -> mkVSystem (Map.fromList [ (phi, ielim (a' VI0) (u' VI0) (v' VI0) (u @@ i @@ isone) j)
  203. , (j, v' i)
  204. , (inot j, u' i)]))
  205. (VInc (a' VI0 @@ VI0 @@ j) phi (ielim (a' VI0 @@ VI0) (u' VI0) (v' VI0) a0 j))
  206. VGlueTy{} ->
  207. let
  208. b = u
  209. b0 = a0
  210. fam = a
  211. in
  212. let
  213. base i = let VGlueTy base _ _ _ = force (fam @@ i) in base
  214. phi i = let VGlueTy _ phi _ _ = force (fam @@ i) in phi
  215. types i = let VGlueTy _ _ types _ = force (fam @@ i) in types
  216. equivs i = let VGlueTy _ _ _ equivs = force (fam @@ i) in equivs
  217. a i = fun \u -> unglue (base i) (phi i) (types i @@ u) (equivs i @@ u) (b @@ i @@ u)
  218. a0 = unglue (base VI0) (phi VI0) (types VI0) (equivs VI0) b0
  219. del = faceForall phi
  220. a1' = comp (line base) psi (line a) (VInc undefined undefined a0)
  221. t1' = comp (line types) psi (line (b @@)) (VInc undefined undefined b0)
  222. (omega_st, omega_t, omega_rep) = pres types base equivs psi (b @@) b0
  223. omega = outS omega_t psi omega_rep omega_st
  224. (t1alpha_st, t1a_t, t1a_rep) = opEquiv (base VI1) (types VI1 @@ VItIsOne) (equivs VI1 @@ VItIsOne) (del `ior` psi) (fun ts) (fun ps) a1'
  225. t1alpha = outS t1a_t (del `ior` psi) t1a_rep t1alpha_st
  226. (t1, alpha) = (vProj1 t1alpha, vProj2 t1alpha)
  227. ts isone = mkVSystem . Map.fromList $ [(del, t1'), (psi, (b @@ VI1 @@ isone))]
  228. ps _isone = mkVSystem . Map.fromList $ [(del, omega), (psi, VLine (line (const (base VI1))) a1' a1' (fun (const a1')))]
  229. a1 = comp
  230. (fun (const (base VI1 @@ VItIsOne)))
  231. (phi VI1 `ior` psi)
  232. (system \j _u -> mkVSystem (Map.fromList [ (phi VI1, ielim (base VI1) a1' (vProj1 (equivs VI1 @@ VItIsOne)) alpha j)
  233. , (psi, a VI1)]))
  234. a1'
  235. b1 = glueElem (base VI1) (phi VI1) (types VI1) (equivs VI1) (fun (const t1)) a1
  236. in b1
  237. VType -> VGlueTy a0 phi (system \_ _ -> mkVSystem (Map.fromList [(phi, u @@ VI1 @@ VItIsOne)]))
  238. (system \_ _ -> mkVSystem (Map.fromList [(phi, makeEquiv (\j -> (u @@ inot j)))]))
  239. VNe HData{} args ->
  240. case force a0 of
  241. VNe (HCon con_type con_name) con_args ->
  242. VNe (HCon con_type con_name) $ compConArgs (length args) (a @@) con_type con_args phi u
  243. _ -> VComp a phi u (VInc (a @@ VI0) phi a0)
  244. _ -> VComp a phi u (VInc (a @@ VI0) phi a0)
  245. compConArgs :: Int -> (NFEndp -> Value) -> Value -> Seq.Seq Projection -> NFEndp -> Value -> Seq.Seq Projection
  246. compConArgs total_args fam = go total_args where
  247. go _ _ Seq.Empty _ _ = Seq.Empty
  248. go nargs (VPi p dom (Closure _ rng)) (PApp p' y Seq.:<| xs) phi u
  249. | nargs > 0 = assert (p == p') $ go (nargs - 1) (rng (smuggle (fun (\i -> nthArg (total_args - nargs) (fam i))))) xs phi u
  250. | otherwise = assert (p == p') $
  251. let fill = makeFiller nargs dom phi u y
  252. in PApp p' (fill @@ VI1) Seq.:<| go (nargs - 1) (rng fill) xs phi u
  253. go _ _ _ _ _ = error $ "invalid constructor"
  254. nthArg i (VNe hd s) =
  255. case s Seq.!? i of
  256. Just (PApp _ t) -> t
  257. _ -> error $ "invalid " ++ show i ++ "th argument to data type " ++ show hd
  258. nthArg i (VSystem vs) = VSystem (fmap (nthArg i) vs)
  259. nthArg i xs = error $ "can't get " ++ show i ++ "th argument of " ++ show (prettyTm (quote xs))
  260. makeFiller nth (VNe (HData (Bound _ (negate -> 10))) args) phi u a0 =
  261. fun $ fill (makeDomain args) phi (system \i is1 -> nthArg nth (u @@ i @@ is1) ) a0
  262. makeFiller _ _ _ _ a0 = fun (const a0)
  263. makeDomain (PApp _ x Seq.:<| xs) = fun \i -> foldl (\t (~(PApp _ x)) -> t @@ (x @@ i)) (x @@ i) xs
  264. makeDomain _ = error "somebody smuggled something that smells"
  265. smuggle x = VNe (HData (Bound "__comp_con_tyarg" (negate 10))) (Seq.singleton (PApp P.Ex x))
  266. compOutS :: NFSort -> NFEndp -> Value -> Value -> Value
  267. compOutS a b c d = compOutS a b c (force d) where
  268. compOutS _ _hi _0 vl@VComp{} = vl
  269. compOutS _ _hi _0 (VInc _ _ x) = x
  270. compOutS _ _ _ v = v
  271. system :: (Value -> Value -> Value) -> Value
  272. system k = fun \i -> fun \isone -> k i isone
  273. fill :: NFLine -> NFEndp -> Value -> Value -> NFEndp -> Value
  274. fill a phi u a0 j =
  275. comp (line \i -> a @@ (i `iand` j))
  276. (phi `ior` inot j)
  277. (fun \i -> fun \isone -> mkVSystem (Map.fromList [ (phi, u @@ (i `iand` j) @@ isone)
  278. , (inot j, a0)]))
  279. a0
  280. glueType :: NFSort -> NFEndp -> NFPartial -> NFPartial -> Value
  281. glueType a phi tys eqvs = VGlueTy a phi tys eqvs
  282. glueElem :: NFSort -> NFEndp -> NFPartial -> NFPartial -> NFPartial -> Value -> Value
  283. glueElem _a (force -> VI1) _tys _eqvs t _vl = t @@ VItIsOne
  284. glueElem a phi tys eqvs t vl = VGlue a phi tys eqvs t vl
  285. unglue :: NFSort -> NFEndp -> NFPartial -> NFPartial -> Value -> Value
  286. unglue _a (force -> VI1) _tys eqvs x = vProj1 (eqvs @@ VItIsOne) @@ x
  287. unglue _a _phi _tys _eqvs (force -> VGlue _ _ _ _ _ vl) = vl
  288. unglue a phi tys eqvs (force -> VSystem fs) = VSystem (fmap (unglue a phi tys eqvs) fs)
  289. unglue a phi tys eqvs vl = VUnglue a phi tys eqvs vl
  290. -- Definition of equivalences
  291. faceForall :: (NFEndp -> NFEndp) -> Value
  292. faceForall phi = T.length (getNameText name) `seq` go (phi (VVar name)) where
  293. {-# NOINLINE name #-}
  294. name = unsafePerformIO newName
  295. go x@(VVar n)
  296. | n == name = VI0
  297. | otherwise = x
  298. go x@(VINot (VVar n))
  299. | n == name = VI0
  300. | otherwise = x
  301. go (VIAnd x y) = iand (go x) (go y)
  302. go (VIOr x y) = ior (go x) (go y)
  303. go (VINot x) = inot (go x)
  304. go vl = vl
  305. isContr :: Value -> Value
  306. isContr a = exists' "x" a \x -> dprod' "y" a \y -> VPath (line (const a)) x y
  307. fiber :: NFSort -> NFSort -> Value -> Value -> Value
  308. fiber a b f y = exists' "x" a \x -> VPath (line (const b)) (f @@ x) y
  309. isEquiv :: NFSort -> NFSort -> Value -> Value
  310. isEquiv a b f = dprod' "y" b \y -> isContr (fiber a b f y)
  311. equiv :: NFSort -> NFSort -> Value
  312. equiv a b = exists' "f" (a ~> b) \f -> isEquiv a b f
  313. pres :: (NFEndp -> NFSort) -> (NFEndp -> NFSort) -> (NFEndp -> Value) -> NFEndp -> (NFEndp -> Value) -> Value -> (Value, NFSort, Value)
  314. 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
  315. pathT = VPath (fun (const (tyA VI1))) c1 c2
  316. c1 = comp (line tyA) phi (system \i u -> f i @@ (t i @@ u)) (VInc (tyA VI0) phi (f VI0 @@ t0))
  317. c2 = f VI1 @@ comp (line tyT) phi (system \i u -> t i @@ u) t0
  318. a0 = f VI0 @@ t0
  319. v = fill (fun tyT) phi (system \i u -> t i @@ u) t0
  320. path j = comp (fun tyA) (phi `ior` j) (system \i _ -> f i @@ (v i)) a0
  321. opEquiv :: HasCallStack => Value -> Value -> Value -> NFEndp -> Value -> Value -> Value -> (Value, NFSort, Value)
  322. opEquiv aT tT f phi t p a = (VInc ty phi v, ty, fun \u -> VPair (t @@ u) (p @@ u)) where
  323. fn = vProj1 f
  324. ty = exists' "f" tT \x -> VPath (line (const aT)) a (fn @@ x)
  325. v = contr ty (vProj2 f @@ a) phi (\u -> VPair (t @@ u) (p @@ u))
  326. contr :: HasCallStack => Value -> Value -> NFEndp -> (Value -> Value) -> Value
  327. contr a aC phi u =
  328. comp (line (const a))
  329. phi
  330. (system \i is1 -> ielim (line (const a)) (vProj1 aC) (u is1) (vProj2 aC @@ u is1) i)
  331. (vProj1 aC)
  332. makeEquiv :: (NFEndp -> Value) -> Value
  333. makeEquiv line = comp (fun \i -> equiv a (line i)) VI0 (system \_ _ -> VSystem mempty) (VPair idfun idisequiv) where
  334. a = line VI0
  335. idfun = fun id
  336. -- idEquiv y = ((y, \i -> y), \u i -> (u.2 (inot i), \j -> u.2 (ior (inot i) j)))
  337. u_ty = exists' "y" a \x -> VPath (fun (const a)) x x
  338. idisequiv = fun \y -> VPair (id_fiber y) $ fun \u ->
  339. VLine u_ty (id_fiber y) u $ fun \i -> VPair (ielim (fun (const a)) y y (vProj2 u) i) $
  340. VLine (fun (const a)) y (vProj1 u) $ fun \j ->
  341. ielim (fun (const a)) y y (vProj2 u) (iand i j)
  342. id_fiber y = VPair y (VLine a y y (fun (const y)))