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.

413 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 = let VPi _ _ (Closure _ r) = force (a @@ i) in r
  188. y' i y = fill (fun (dom . inot)) VI0 (fun \_ -> fun \_ -> VSystem mempty) (VInc (dom VI0) phi y) i
  189. ybar i y = y' (inot i) y
  190. in VLam (plic VI1) . Closure (Bound "x" 0) $ \arg ->
  191. comp (fun \i -> rng i (ybar i arg))
  192. phi
  193. (system \i isone -> vApp (plic i) (u @@ i @@ isone) (ybar i arg))
  194. (VInc (rng VI0 (ybar VI0 arg)) phi (vApp (plic VI0) a0 (ybar VI0 arg)))
  195. VSigma{} ->
  196. let
  197. dom i = let VSigma d _ = force (a @@ i) in d
  198. rng i = let VSigma _ (Closure _ r) = force (a @@ i) in r
  199. w i = fill (fun dom) phi (system \i isone -> vProj1 (u @@ i @@ isone)) (VInc (dom VI0) phi (vProj1 a0)) i
  200. c1 = comp (fun dom) phi (system \i isone -> vProj1 (u @@ i @@ isone)) (VInc (dom VI0) phi (vProj1 a0))
  201. c2 = comp (fun (($ w VI1) . rng)) phi (system \i isone -> vProj1 (u @@ i @@ isone)) (VInc (rng VI0 (dom VI0)) phi (vProj2 a0))
  202. in
  203. VPair c1 c2
  204. VPath{} ->
  205. let
  206. a' i = let VPath thea _ _ = force (a @@ i) in thea
  207. u' i = let VPath _ theu _ = force (a @@ i) in theu
  208. v' i = let VPath _ _ thev = force (a @@ i) in thev
  209. in
  210. VLine (a' VI1 @@ VI1) (u' VI1) (v' VI1) $ fun \j ->
  211. comp (fun a')
  212. (phi `ior` j `ior` inot j)
  213. (system \i isone -> mkVSystem (Map.fromList [ (phi, ielim (a' VI0) (u' VI0) (v' VI0) (u @@ i @@ isone) j)
  214. , (j, v' i)
  215. , (inot j, u' i)]))
  216. (VInc (a' VI0 @@ VI0 @@ j) phi (ielim (a' VI0 @@ VI0) (u' VI0) (v' VI0) a0 j))
  217. VGlueTy{} ->
  218. let
  219. b = u
  220. b0 = a0
  221. fam = a
  222. in
  223. let
  224. base i = let VGlueTy base _ _ _ = force (fam @@ i) in base
  225. phi i = let VGlueTy _ phi _ _ = force (fam @@ i) in phi
  226. types i = let VGlueTy _ _ types _ = force (fam @@ i) in types
  227. equivs i = let VGlueTy _ _ _ equivs = force (fam @@ i) in equivs
  228. a i = fun \u -> unglue (base i) (phi i) (types i @@ u) (equivs i @@ u) (b @@ i @@ u)
  229. a0 = unglue (base VI0) (phi VI0) (types VI0) (equivs VI0) b0
  230. del = faceForall phi
  231. a1' = comp (line base) psi (line a) (VInc undefined undefined a0)
  232. t1' = comp (line types) psi (line (b @@)) (VInc undefined undefined b0)
  233. (omega_st, omega_t, omega_rep) = pres types base equivs psi (b @@) b0
  234. omega = outS omega_t psi omega_rep omega_st
  235. (t1alpha_st, t1a_t, t1a_rep) = opEquiv (base VI1) (types VI1 @@ VItIsOne) (equivs VI1 @@ VItIsOne) (del `ior` psi) (fun ts) (fun ps) a1'
  236. t1alpha = outS t1a_t (del `ior` psi) t1a_rep t1alpha_st
  237. (t1, alpha) = (vProj1 t1alpha, vProj2 t1alpha)
  238. ts isone = mkVSystem . Map.fromList $ [(del, t1'), (psi, (b @@ VI1 @@ isone))]
  239. ps _isone = mkVSystem . Map.fromList $ [(del, omega), (psi, VLine (line (const (base VI1))) a1' a1' (fun (const a1')))]
  240. a1 = comp
  241. (fun (const (base VI1 @@ VItIsOne)))
  242. (phi VI1 `ior` psi)
  243. (system \j _u -> mkVSystem (Map.fromList [ (phi VI1, ielim (base VI1) a1' (vProj1 (equivs VI1 @@ VItIsOne)) alpha j)
  244. , (psi, a VI1)]))
  245. a1'
  246. b1 = glueElem (base VI1) (phi VI1) (types VI1) (equivs VI1) (fun (const t1)) a1
  247. in b1
  248. VType -> VGlueTy a0 phi (system \_ _ -> mkVSystem (Map.fromList [(phi, u @@ VI1 @@ VItIsOne)]))
  249. (system \_ _ -> mkVSystem (Map.fromList [(phi, makeEquiv (\j -> (u @@ inot j)))]))
  250. -- fibrancy structure of the booleans is trivial
  251. VBool{} -> a0
  252. _ -> VComp a phi u a0
  253. compOutS :: NFSort -> NFEndp -> Value -> Value -> Value
  254. compOutS _ _hi _0 vl@VComp{} = vl
  255. compOutS _ _hi _0 (VInc _ _ x) = x
  256. compOutS _ _ _ v = v
  257. system :: (Value -> Value -> Value) -> Value
  258. system k = fun \i -> fun \isone -> k i isone
  259. fill :: NFLine -> NFEndp -> Value -> Value -> NFEndp -> Value
  260. fill a phi u a0 j =
  261. comp (fun \i -> a @@ (i `iand` j))
  262. (phi `ior` inot j)
  263. (fun \i -> fun \isone -> mkVSystem (Map.fromList [ (phi, u @@ (i `iand` j) @@ isone)
  264. , (inot j, a0)]))
  265. a0
  266. glueType :: NFSort -> NFEndp -> NFPartial -> NFPartial -> Value
  267. glueType a phi tys eqvs = VGlueTy a phi tys eqvs
  268. glueElem :: NFSort -> NFEndp -> NFPartial -> NFPartial -> NFPartial -> Value -> Value
  269. glueElem _a VI1 _tys _eqvs t _vl = t @@ VItIsOne
  270. glueElem a phi tys eqvs t vl = VGlue a phi tys eqvs t vl
  271. unglue :: NFSort -> NFEndp -> NFPartial -> NFPartial -> Value -> Value
  272. unglue _a VI1 _tys eqvs x = vProj1 (eqvs @@ VItIsOne) @@ x
  273. unglue _a _phi _tys _eqvs (VGlue _ _ _ _ _ vl) = vl
  274. unglue _ _ _ _ (VSystem (Map.toList -> [])) = VSystem (Map.fromList [])
  275. unglue a phi tys eqvs vl = VUnglue a phi tys eqvs vl
  276. -- Definition of equivalences
  277. faceForall :: (NFEndp -> NFEndp) -> Value
  278. faceForall phi = T.length (getNameText name) `seq` go (phi (VVar name)) where
  279. {-# NOINLINE name #-}
  280. name = unsafePerformIO newName
  281. go x@(VVar n)
  282. | n == name = VI0
  283. | otherwise = x
  284. go x@(VINot (VVar n))
  285. | n == name = VI0
  286. | otherwise = x
  287. go (VIAnd x y) = iand (go x) (go y)
  288. go (VIOr x y) = ior (go x) (go y)
  289. go (VINot x) = inot (go x)
  290. go vl = vl
  291. isContr :: Value -> Value
  292. isContr a = exists' "x" a \x -> dprod' "y" a \y -> VPath (line (const a)) x y
  293. fiber :: NFSort -> NFSort -> Value -> Value -> Value
  294. fiber a b f y = exists' "x" a \x -> VPath (line (const b)) (f @@ x) y
  295. isEquiv :: NFSort -> NFSort -> Value -> Value
  296. isEquiv a b f = dprod' "y" b \y -> isContr (fiber a b f y)
  297. equiv :: NFSort -> NFSort -> Value
  298. equiv a b = exists' "f" (a ~> b) \f -> isEquiv a b f
  299. pres :: (NFEndp -> NFSort) -> (NFEndp -> NFSort) -> (NFEndp -> Value) -> NFEndp -> (NFEndp -> Value) -> Value -> (Value, NFSort, Value)
  300. 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
  301. pathT = VPath (fun (const (tyA VI1))) c1 c2
  302. c1 = comp (fun tyA) phi (system \i u -> f i @@ (t i @@ u)) (VInc (tyA VI0) phi (f VI0 @@ t0))
  303. c2 = f VI1 @@ comp (fun tyT) phi (system \i u -> t i @@ u) t0
  304. a0 = f VI0 @@ t0
  305. v = fill (fun tyT) phi (system \i u -> t i @@ u) t0
  306. path j = comp (fun tyA) (phi `ior` j) (system \i _ -> f i @@ (v i)) a0
  307. opEquiv :: Value -> Value -> Value -> NFEndp -> Value -> Value -> Value -> (Value, NFSort, Value)
  308. opEquiv aT tT f phi t p a = (VInc ty phi v, ty, fun \u -> VPair (t @@ u) (p @@ u)) where
  309. fn = vProj1 f
  310. ty = exists' "f" tT \x -> VPath (line (const aT)) a (fn @@ x)
  311. v = contr ty (vProj2 f @@ a) phi (\u -> VPair (t @@ u) (p @@ u))
  312. contr :: Value -> Value -> NFEndp -> (Value -> Value) -> Value
  313. contr a aC phi u =
  314. comp (line (const a))
  315. phi
  316. (system \i is1 -> ielim (line (const a)) a (vProj1 (u is1)) (vProj2 (u is1)) i)
  317. (vProj1 aC)
  318. makeEquiv :: (NFEndp -> Value) -> Value
  319. makeEquiv line = comp (fun \i -> equiv a (line i)) VI0 (system \_ _ -> VSystem mempty) (VPair idfun idisequiv) where
  320. a = line VI0
  321. idfun = fun id
  322. -- idEquiv y = ((y, \i -> y), \u i -> (u.2 (inot i), \j -> u.2 (ior (inot i) j)))
  323. u_ty = exists' "y" a \x -> VPath (fun (const a)) x x
  324. idisequiv = fun \y -> VPair (id_fiber y) $ fun \u ->
  325. VLine u_ty (id_fiber y) u $ fun \i -> VPair (ielim (fun (const a)) y y (vProj2 u) i) $
  326. VLine (fun (const a)) y (vProj1 u) $ fun \j ->
  327. ielim (fun (const a)) y y (vProj2 u) (iand i j)
  328. id_fiber y = VPair y (VLine a y y (fun (const y)))
  329. elimBool :: NFSort -> Value -> Value -> Value -> Value
  330. elimBool prop x y bool =
  331. case force bool of
  332. VTt -> x
  333. VFf -> y
  334. _ -> VIf prop x y bool