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.

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