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.

364 lines
13 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. 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 WiIsOne1 = forallI \_ -> forallI \_ -> fun VIsOne1
  59. wiValue WiIsOne2 = forallI \_ -> forallI \_ -> fun VIsOne2
  60. wiValue WiPartial = fun \phi -> fun \r -> VPartial phi r
  61. wiValue WiPartialP = fun \phi -> fun \r -> VPartialP phi r
  62. wiValue WiSub = fun \a -> fun \phi -> fun \u -> VSub a phi u
  63. wiValue WiInS = forallI \a -> forallI \phi -> fun \u -> VInc a phi u
  64. wiValue WiOutS = forallI \a -> forallI \phi -> forallI \u -> fun \x -> outS a phi u x
  65. wiValue WiComp = fun \a -> forallI \phi -> fun \u -> fun \x -> comp a phi u x
  66. wiValue WiGlue = fun \a -> forallI \phi -> fun \t -> fun \e -> glueType a phi t e
  67. wiValue WiGlueElem = forallI \a -> forallI \phi -> forallI \ty -> forallI \eqv -> fun \x -> fun \y -> glueElem a phi ty eqv x y
  68. wiValue WiUnglue = forallI \a -> forallI \phi -> forallI \ty -> forallI \eqv -> fun \x -> unglue a phi ty eqv x
  69. (~>) :: Value -> Value -> Value
  70. a ~> b = VPi P.Ex a (Closure (Bound "_" 0) (const b))
  71. infixr 7 ~>
  72. fun, line :: (Value -> Value) -> Value
  73. fun k = VLam P.Ex $ Closure (Bound "x" 0) (k . force)
  74. line k = VLam P.Ex $ Closure (Bound "i" 0) (k . force)
  75. forallI :: (Value -> Value) -> Value
  76. forallI k = VLam P.Im $ Closure (Bound "x" 0) (k . force)
  77. dprod' :: String -> Value -> (Value -> Value) -> Value
  78. dprod' t a b = VPi P.Ex a (Closure (Bound (T.pack t) 0) b)
  79. dprod :: Value -> (Value -> Value) -> Value
  80. dprod = dprod' "x"
  81. exists' :: String -> Value -> (Value -> Value) -> Value
  82. exists' s a b = VSigma a (Closure (Bound (T.pack s) 0) b)
  83. exists :: Value -> (Value -> Value) -> Value
  84. exists = exists' "x"
  85. forAll' :: String -> Value -> (Value -> Value) -> Value
  86. forAll' n a b = VPi P.Im a (Closure (Bound (T.pack n) 0) b)
  87. forAll :: Value -> (Value -> Value) -> Value
  88. forAll = forAll' "x"
  89. wiredInNames :: Map Text WiredIn
  90. wiredInNames = Map.fromList
  91. [ ("Pretype", WiPretype)
  92. , ("Type", WiType)
  93. , ("Interval", WiInterval)
  94. , ("i0", WiI0)
  95. , ("i1", WiI1)
  96. , ("iand", WiIAnd)
  97. , ("ior", WiIOr)
  98. , ("inot", WiINot)
  99. , ("PathP", WiPathP)
  100. , ("IsOne", WiIsOne)
  101. , ("itIs1", WiItIsOne)
  102. , ("isOneL", WiIsOne1)
  103. , ("isOneR", WiIsOne2)
  104. , ("Partial", WiPartial)
  105. , ("PartialP", WiPartialP)
  106. , ("Sub", WiSub)
  107. , ("inS", WiInS)
  108. , ("outS", WiOutS)
  109. , ("comp", WiComp)
  110. , ("Glue", WiGlue)
  111. , ("glue", WiGlueElem)
  112. , ("unglue", WiUnglue)
  113. ]
  114. newtype NoSuchPrimitive = NoSuchPrimitive { getUnknownPrim :: Text }
  115. deriving (Show, Typeable)
  116. deriving anyclass (Exception)
  117. -- Interval operations
  118. iand, ior :: Value -> Value -> Value
  119. iand = \case
  120. VI1 -> id
  121. VI0 -> const VI0
  122. VIAnd x y -> \case
  123. VI0 -> VI0
  124. VI1 -> VI1
  125. z -> iand x (iand y z)
  126. x -> \case
  127. VI0 -> VI0
  128. VI1 -> x
  129. y -> VIAnd x y
  130. ior = \case
  131. VI0 -> id
  132. VI1 -> const VI1
  133. VIOr x y -> \case
  134. VI1 -> VI1
  135. VI0 -> VIOr x y
  136. z -> ior x (ior y z)
  137. x -> \case
  138. VI1 -> VI1
  139. VI0 -> x
  140. y -> VIOr x y
  141. inot :: Value -> Value
  142. inot = \case
  143. VI0 -> VI1
  144. VI1 -> VI0
  145. VIOr x y -> VIAnd (inot x) (inot y)
  146. VIAnd x y -> VIOr (inot x) (inot y)
  147. VINot x -> x
  148. x -> VINot x
  149. ielim :: Value -> Value -> Value -> Value -> NFEndp -> Value
  150. ielim _line _left _right fn i =
  151. case force fn of
  152. VLine _ _ _ fun -> fun @@ i
  153. VNe n sp -> VNe n (sp Seq.:|> PIElim _line _left _right i)
  154. VSystem (Map.toList -> []) -> VSystem (Map.fromList [])
  155. _ -> error $ "can't ielim " ++ show fn
  156. outS :: NFSort -> NFEndp -> Value -> Value -> Value
  157. outS _ (force -> VI1) u _ = u @@ VItIsOne
  158. outS _ _phi _ (VInc _ _ x) = x
  159. outS _ VI0 _ x = x
  160. outS a phi u (VNe x sp) = VNe x (sp Seq.:|> POuc a phi u)
  161. outS _ _ _ v = error $ "can't outS " ++ show v
  162. -- Composition
  163. comp :: NFLine -> NFEndp -> Value -> Value -> Value
  164. comp _ VI1 u _ = u @@ VI1 @@ VItIsOne
  165. comp a psi@phi u (outS (a @@ VI1) phi (u @@ VI1 @@ VItIsOne) -> a0) =
  166. case force $ a @@ VVar (Bound (T.pack "neutral composition") 0) of
  167. VPi{} ->
  168. let
  169. plic i = let VPi p _ _ = a @@ i in p
  170. dom i = let VPi _ d _ = a @@ i in d
  171. rng i = let VPi _ _ (Closure _ r) = a @@ i in r
  172. y' i y = fill (fun (dom . inot)) VI0 (fun \_ -> fun \_ -> VSystem mempty) (VInc (dom VI0) phi y) i
  173. ybar i y = y' (inot i) y
  174. in VLam (plic VI1) . Closure (Bound "x" 0) $ \arg ->
  175. comp (fun \i -> rng i (ybar i arg))
  176. phi
  177. (system \i isone -> vApp (plic i) (u @@ i @@ isone) (ybar i arg))
  178. (VInc (rng VI0 (ybar VI0 arg)) phi (vApp (plic VI0) a0 (ybar VI0 arg)))
  179. VSigma{} ->
  180. let
  181. dom i = let VSigma d _ = a @@ i in d
  182. rng i = let VSigma _ (Closure _ r) = a @@ i in r
  183. w i = fill (fun dom) phi (system \i isone -> vProj1 (u @@ i @@ isone)) (VInc (dom VI0) phi (vProj1 a0)) i
  184. c1 = comp (fun dom) phi (system \i isone -> vProj1 (u @@ i @@ isone)) (VInc (dom VI0) phi (vProj1 a0))
  185. c2 = comp (fun (($ w VI1) . rng)) phi (system \i isone -> vProj1 (u @@ i @@ isone)) (VInc (rng VI0 (dom VI0)) phi (vProj2 a0))
  186. in
  187. VPair c1 c2
  188. VPath{} ->
  189. let
  190. a' i = let VPath a _ _ = a @@ i in a
  191. u' i = let VPath _ u _ = a @@ i in u
  192. v' i = let VPath _ _ v = a @@ i in v
  193. in
  194. VLine (a' VI1 @@ VI1) (u' VI1) (v' VI1) $ fun \j ->
  195. comp (fun a')
  196. (phi `ior` j `ior` inot j)
  197. (system \i isone -> mkVSystem (Map.fromList [ (phi, ielim (a' VI0) (u' VI0) (v' VI0) (u @@ i @@ isone) j)
  198. , (j, v' i)
  199. , (inot j, u' i)]))
  200. (VInc (a' VI0 @@ VI0 @@ j) phi (ielim (a' VI0 @@ VI0) (u' VI0) (v' VI0) a0 j))
  201. VGlueTy{} ->
  202. let
  203. b = u
  204. b0 = a0
  205. fam = a
  206. in
  207. let
  208. base i = let VGlueTy base _ _ _ = fam @@ i in base
  209. phi i = let VGlueTy _ phi _ _ = fam @@ i in phi
  210. types i = let VGlueTy _ _ types _ = fam @@ i in types
  211. equivs i = let VGlueTy _ _ _ equivs = fam @@ i in equivs
  212. a i = fun \u -> unglue (base i) (phi i) (types i @@ u) (equivs i @@ u) (b @@ i @@ u)
  213. a0 = unglue (base VI0) (phi VI0) (types VI0) (equivs VI0) b0
  214. del = faceForall phi
  215. a1' = comp (line base) psi (line a) (VInc undefined undefined a0)
  216. t1' = comp (line types) psi (line (b @@)) (VInc undefined undefined b0)
  217. (omega_st, omega_t, omega_rep) = pres types base equivs psi (b @@) b0
  218. omega = outS omega_t psi omega_rep omega_st
  219. (t1alpha_st, t1a_t, t1a_rep) = opEquiv (base VI1) (types VI1 @@ VItIsOne) (equivs VI1 @@ VItIsOne) (del `ior` psi) (fun ts) (fun ps) a1'
  220. t1alpha = outS t1a_t (del `ior` psi) t1a_rep t1alpha_st
  221. (t1, alpha) = (vProj1 t1alpha, vProj2 t1alpha)
  222. ts isone = mkVSystem . Map.fromList $ [(del, t1'), (psi, (b @@ VI1 @@ isone))]
  223. ps _isone = mkVSystem . Map.fromList $ [(del, omega), (psi, VLine (line (const (base VI1))) a1' a1' (fun (const a1')))]
  224. a1 = comp
  225. (fun (const (base VI1 @@ VItIsOne)))
  226. (phi VI1 `ior` psi)
  227. (system \j _u -> mkVSystem (Map.fromList [ (phi VI1, ielim (base VI1) a1' (vProj1 (equivs VI1 @@ VItIsOne)) alpha j)
  228. , (psi, a VI1)]))
  229. a1'
  230. b1 = glueElem (base VI1) (phi VI1) (types VI1) (equivs VI1) (fun (const t1)) a1
  231. in b1
  232. _ -> VComp a phi u a0
  233. system :: (Value -> Value -> Value) -> Value
  234. system k = fun \i -> fun \isone -> k i isone
  235. fill :: NFLine -> NFEndp -> Value -> Value -> NFEndp -> Value
  236. fill a phi u a0 j =
  237. comp (fun \i -> a @@ (i `iand` j))
  238. (phi `ior` inot j)
  239. (fun \i -> fun \isone -> mkVSystem (Map.fromList [ (phi, u @@ (i `iand` j) @@ isone)
  240. , (inot j, a0)]))
  241. a0
  242. glueType :: NFSort -> NFEndp -> NFPartial -> NFPartial -> Value
  243. glueType a phi tys eqvs = VGlueTy a phi tys eqvs
  244. glueElem :: NFSort -> NFEndp -> NFPartial -> NFPartial -> NFPartial -> Value -> Value
  245. glueElem _a VI1 _tys _eqvs t _vl = t @@ VItIsOne
  246. glueElem a phi tys eqvs t vl = VGlue a phi tys eqvs t vl
  247. unglue :: NFSort -> NFEndp -> NFPartial -> NFPartial -> Value -> Value
  248. unglue _a VI1 _tys eqvs x = vProj1 (eqvs @@ VItIsOne) @@ x
  249. unglue _a _phi _tys _eqvs (VGlue _ _ _ _ _ vl) = vl
  250. unglue _ _ _ _ (VSystem (Map.toList -> [])) = VSystem (Map.fromList [])
  251. unglue a phi tys eqvs vl = VUnglue a phi tys eqvs vl
  252. -- Definition of equivalences
  253. faceForall :: (NFEndp -> NFEndp) -> Value
  254. faceForall phi = T.length (getNameText name) `seq` go (phi (VVar name)) where
  255. {-# NOINLINE name #-}
  256. name = unsafePerformIO newName
  257. go x@(VVar n)
  258. | n == name = VI0
  259. | otherwise = x
  260. go x@(VINot (VVar n))
  261. | n == name = VI0
  262. | otherwise = x
  263. go (VIAnd x y) = iand (go x) (go y)
  264. go (VIOr x y) = ior (go x) (go y)
  265. go (VINot x) = inot (go x)
  266. go vl = vl
  267. isContr :: Value -> Value
  268. isContr a = exists' "x" a \x -> dprod' "y" a \y -> VPath (line (const a)) x y
  269. fiber :: NFSort -> NFSort -> Value -> Value -> Value
  270. fiber a b f y = exists' "x" a \x -> VPath (line (const b)) (f @@ x) y
  271. isEquiv :: NFSort -> NFSort -> Value -> Value
  272. isEquiv a b f = dprod' "y" b \y -> isContr (fiber a b f y)
  273. equiv :: NFSort -> NFSort -> Value
  274. equiv a b = exists' "f" (a ~> b) \f -> isEquiv a b f
  275. pres :: (NFEndp -> NFSort) -> (NFEndp -> NFSort) -> (NFEndp -> Value) -> NFEndp -> (NFEndp -> Value) -> Value -> (Value, NFSort, Value)
  276. 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
  277. pathT = VPath (fun (const (tyA VI1))) c1 c2
  278. c1 = comp (fun tyA) phi (system \i u -> f i @@ (t i @@ u)) (VInc (tyA VI0) phi (f VI0 @@ t0))
  279. c2 = f VI1 @@ comp (fun tyT) phi (system \i u -> t i @@ u) t0
  280. a0 = f VI0 @@ t0
  281. v = fill (fun tyT) phi (system \i u -> t i @@ u) t0
  282. path j = comp (fun tyA) (phi `ior` j) (system \i _ -> f i @@ (v i)) a0
  283. opEquiv :: Value -> Value -> Value -> NFEndp -> Value -> Value -> Value -> (Value, NFSort, Value)
  284. opEquiv aT tT f phi t p a = (VInc ty phi v, ty, fun \u -> VPair (t @@ u) (p @@ u)) where
  285. fn = vProj1 f
  286. ty = exists' "f" tT \x -> VPath (line (const aT)) a (fn @@ x)
  287. v = contr ty (vProj2 f @@ a) phi (\u -> VPair (t @@ u) (p @@ u))
  288. contr :: Value -> Value -> NFEndp -> (Value -> Value) -> Value
  289. contr a aC phi u =
  290. comp (line (const a))
  291. phi
  292. (system \i is1 -> ielim (line (const a)) a (vProj1 (u is1)) (vProj2 (u is1)) i)
  293. (vProj1 aC)