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.

221 lines
7.2 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 Syntax
  9. import Data.Map.Strict (Map)
  10. import Data.Text (Text)
  11. import qualified Data.Map.Strict as Map
  12. import Control.Exception
  13. import Data.Typeable
  14. import qualified Presyntax.Presyntax as P
  15. import Elab.Eval
  16. import qualified Data.Sequence as Seq
  17. import qualified Data.Text as T
  18. wiType :: WiredIn -> NFType
  19. wiType WiType = VType
  20. wiType WiPretype = VTypeω
  21. wiType WiInterval = VTypeω
  22. wiType WiI0 = VI
  23. wiType WiI1 = VI
  24. wiType WiIAnd = VI ~> VI ~> VI
  25. wiType WiIOr = VI ~> VI ~> VI
  26. wiType WiINot = VI ~> VI
  27. wiType WiPathP = dprod (VI ~> VTypeω) \a -> a @@ VI0 ~> a @@ VI1 ~> VType
  28. wiType WiIsOne = VI ~> VTypeω
  29. wiType WiItIsOne = VIsOne VI1
  30. wiType WiIsOne1 = forAll VI \i -> forAll VI \j -> VIsOne i ~> VIsOne (ior i j)
  31. wiType WiIsOne2 = forAll VI \i -> forAll VI \j -> VIsOne j ~> VIsOne (ior i j)
  32. wiType WiPartial = VI ~> VType ~> VTypeω
  33. wiType WiPartialP = dprod VI \x -> VPartial x VType ~> VTypeω
  34. wiType WiSub = dprod VType \a -> dprod VI \phi -> VPartial phi a ~> VTypeω
  35. wiType WiInS = forAll VType \a -> forAll VI \phi -> dprod a \u -> VSub a phi (VLam P.Ex (Closure "x" (const u)))
  36. wiType WiOutS = forAll VType \a -> forAll VI \phi -> forAll (VPartial phi a) \u -> VSub a phi u ~> a
  37. wiType WiComp = dprod (VI ~> VType) \a -> forAll VI \phi -> dprod (dprod VI \i -> VPartial phi (a @@ i)) \u -> VSub (a @@ VI0) phi (u @@ VI0) ~> a @@ VI1
  38. wiValue :: WiredIn -> Value
  39. wiValue WiType = VType
  40. wiValue WiPretype = VTypeω
  41. wiValue WiInterval = VI
  42. wiValue WiI0 = VI0
  43. wiValue WiI1 = VI1
  44. wiValue WiIAnd = fun \x -> fun \y -> iand x y
  45. wiValue WiIOr = fun \x -> fun \y -> ior x y
  46. wiValue WiINot = fun inot
  47. wiValue WiPathP = fun \a -> fun \x -> fun \y -> VPath a x y
  48. wiValue WiIsOne = fun VIsOne
  49. wiValue WiItIsOne = VItIsOne
  50. wiValue WiIsOne1 = forallI \_ -> forallI \_ -> fun VIsOne1
  51. wiValue WiIsOne2 = forallI \_ -> forallI \_ -> fun VIsOne2
  52. wiValue WiPartial = fun \phi -> fun \r -> VPartial phi r
  53. wiValue WiPartialP = fun \phi -> fun \r -> VPartialP phi r
  54. wiValue WiSub = fun \a -> fun \phi -> fun \u -> VSub a phi u
  55. wiValue WiInS = forallI \a -> forallI \phi -> fun \u -> VInc a phi u
  56. wiValue WiOutS = forallI \a -> forallI \phi -> forallI \u -> fun \x -> outS a phi u x
  57. -- wiValue WiComp = forAll (VI ~> VType) \a -> forAll VI \phi -> dprod (dprod VI \i -> VPartial phi (a @@ i)) \u -> VSub (a @@ VI0) phi (u @@ VI0) ~> a @@ VI1
  58. wiValue WiComp = fun \a -> forallI \phi -> fun \u -> fun \x -> comp a phi u x
  59. (~>) :: Value -> Value -> Value
  60. a ~> b = VPi P.Ex a (Closure "_" (const b))
  61. infixr 7 ~>
  62. fun :: (Value -> Value) -> Value
  63. fun k = VLam P.Ex $ Closure "x" (k . force)
  64. forallI :: (Value -> Value) -> Value
  65. forallI k = VLam P.Im $ Closure "x" (k . force)
  66. dprod :: Value -> (Value -> Value) -> Value
  67. dprod a b = VPi P.Ex a (Closure "x" b)
  68. forAll :: Value -> (Value -> Value) -> Value
  69. forAll a b = VPi P.Im a (Closure "x" b)
  70. wiredInNames :: Map Text WiredIn
  71. wiredInNames = Map.fromList
  72. [ ("Pretype", WiPretype)
  73. , ("Type", WiType)
  74. , ("Interval", WiInterval)
  75. , ("i0", WiI0)
  76. , ("i1", WiI1)
  77. , ("iand", WiIAnd)
  78. , ("ior", WiIOr)
  79. , ("inot", WiINot)
  80. , ("PathP", WiPathP)
  81. , ("IsOne", WiIsOne)
  82. , ("itIs1", WiItIsOne)
  83. , ("isOneL", WiIsOne1)
  84. , ("isOneR", WiIsOne2)
  85. , ("Partial", WiPartial)
  86. , ("PartialP", WiPartialP)
  87. , ("Sub", WiSub)
  88. , ("inS", WiInS)
  89. , ("outS", WiOutS)
  90. , ("comp", WiComp)
  91. ]
  92. newtype NoSuchPrimitive = NoSuchPrimitive { getUnknownPrim :: Text }
  93. deriving (Show, Typeable)
  94. deriving anyclass (Exception)
  95. -- Interval operations
  96. iand, ior :: Value -> Value -> Value
  97. iand = \case
  98. VI1 -> id
  99. VI0 -> const VI0
  100. VIAnd x y -> \case
  101. VI0 -> VI0
  102. VI1 -> VI1
  103. z -> iand x (iand y z)
  104. x -> \case
  105. VI0 -> VI0
  106. VI1 -> x
  107. y -> VIAnd x y
  108. ior = \case
  109. VI0 -> id
  110. VI1 -> const VI1
  111. VIOr x y -> \case
  112. VI1 -> VI1
  113. VI0 -> VIOr x y
  114. z -> ior x (ior y z)
  115. x -> \case
  116. VI1 -> VI1
  117. VI0 -> x
  118. y -> VIOr x y
  119. inot :: Value -> Value
  120. inot = \case
  121. VI0 -> VI1
  122. VI1 -> VI0
  123. VIOr x y -> VIAnd (inot x) (inot y)
  124. VIAnd x y -> VIOr (inot x) (inot y)
  125. VINot x -> x
  126. x -> VINot x
  127. ielim :: Value -> Value -> Value -> Value -> NFEndp -> Value
  128. ielim _line _left _right fn i =
  129. case force fn of
  130. VLine _ _ _ fun -> fun @@ i
  131. VNe n sp -> VNe n (sp Seq.:|> PIElim _line _left _right i)
  132. _ -> error $ "can't ielim " ++ show fn
  133. outS :: NFSort -> NFEndp -> Value -> Value -> Value
  134. outS _ (force -> VI1) u _ = u @@ VItIsOne
  135. outS _ _phi _ (VInc _ _ x) = x
  136. outS a phi u (VNe x sp) = VNe x (sp Seq.:|> POuc a phi u)
  137. outS _ _ _ v = error $ "can't outS " ++ show v
  138. -- Composition
  139. comp :: NFLine -> NFEndp -> Value -> Value -> Value
  140. comp _ VI1 u _ = u @@ VI1 @@ VItIsOne
  141. comp a phi u (VInc _ _ a0) =
  142. case a @@ VNe (HVar (Bound (T.pack "x"))) Seq.empty of
  143. VPi{} ->
  144. let
  145. plic i = let VPi p _ _ = a @@ i in p
  146. dom i = let VPi _ d _ = a @@ i in d
  147. rng i = let VPi _ _ (Closure _ r) = a @@ i in r
  148. y' i y = fill (fun (dom . inot)) VI0 (fun \_ -> fun \_ -> VSystem mempty) (VInc (dom VI0) phi y) i
  149. ybar i y = y' (inot i) y
  150. in VLam (plic VI1) . Closure "x" $ \arg ->
  151. comp (fun \i -> rng i (ybar i arg))
  152. phi
  153. (system \i isone -> vApp (plic i) (u @@ i @@ isone) (ybar i arg))
  154. (VInc (rng VI0 (ybar VI0 arg)) phi (vApp (plic VI0) a0 (ybar VI0 arg)))
  155. VSigma{} ->
  156. let
  157. dom i = let VSigma d _ = a @@ i in d
  158. rng i = let VSigma _ (Closure _ r) = a @@ i in r
  159. w i = fill (fun dom) phi (system \i isone -> vProj1 (u @@ i @@ isone)) (VInc (dom VI0) phi (vProj1 a0)) i
  160. c1 = comp (fun dom) phi (system \i isone -> vProj1 (u @@ i @@ isone)) (VInc (dom VI0) phi (vProj1 a0))
  161. c2 = comp (fun (($ w VI1) . rng)) phi (system \i isone -> vProj1 (u @@ i @@ isone)) (VInc (rng VI0 (dom VI0)) phi (vProj2 a0))
  162. in
  163. VPair c1 c2
  164. VPath{} ->
  165. let
  166. a' i = let VPath a _ _ = a @@ i in a
  167. u' i = let VPath _ u _ = a @@ i in u
  168. v' i = let VPath _ _ v = a @@ i in v
  169. in
  170. VLine (a' VI1 @@ VI1) (u' VI1) (v' VI1) $ fun \j ->
  171. comp (fun a')
  172. (phi `ior` j `ior` inot j)
  173. (system \i isone -> mkVSystem (Map.fromList [ (phi, ielim (a' VI0) (u' VI0) (v' VI0) (u @@ i @@ isone) j)
  174. , (j, v' i)
  175. , (inot j, u' i)]))
  176. (VInc (a' VI0 @@ VI0 @@ j) phi (ielim (a' VI0 @@ VI0) (u' VI0) (v' VI0) a0 j))
  177. _ -> VComp a phi u a0
  178. comp a phi u a0 = VComp a phi u a0
  179. system :: (Value -> Value -> Value) -> Value
  180. system k = fun \i -> fun \isone -> k i isone
  181. fill :: NFLine -> NFEndp -> Value -> Value -> NFEndp -> Value
  182. fill a phi u a0 j =
  183. comp (fun \i -> a @@ (i `iand` j))
  184. (phi `ior` inot j)
  185. (fun \i -> fun \isone -> mkVSystem (Map.fromList [ (phi, u @@ (i `iand` j) @@ isone)
  186. , (inot j, a0)]))
  187. a0