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.

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