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.

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