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.

234 lines
6.3 KiB

  1. {-# LANGUAGE PatternSynonyms #-}
  2. module Syntax where
  3. import Data.Function (on)
  4. import Data.Text (Text)
  5. import Presyntax.Presyntax (Plicity(..))
  6. import qualified Data.Text as T
  7. import Data.IORef (IORef)
  8. import Data.Set (Set)
  9. import qualified Data.Set as Set
  10. import Data.Sequence (Seq)
  11. import qualified Data.Sequence as Seq
  12. import Data.Map.Strict (Map)
  13. import qualified Data.Map.Strict as Map
  14. data WiredIn
  15. = WiType
  16. | WiPretype
  17. | WiInterval
  18. | WiI0
  19. | WiI1
  20. | WiIAnd
  21. | WiIOr
  22. | WiINot
  23. | WiPathP
  24. | WiIsOne -- Proposition associated with an element of the interval
  25. | WiItIsOne -- 1 = 1
  26. | WiIsOne1 -- i j -> [i] -> [ior i j]
  27. | WiIsOne2 -- i j -> [j] -> [ior i j]
  28. | WiPartial -- (φ : I) -> Type -> Typeω
  29. | WiPartialP -- (φ : I) -> Partial r Type -> Typeω
  30. | WiSub -- (A : Type) (φ : I) -> Partial φ A -> Typeω
  31. | WiInS -- {A : Type} {φ : I} (u : A) -> Sub A φ (λ x -> u)
  32. | WiOutS -- {A : Type} {φ : I} {u : Partial φ A} -> Sub A φ u -> A
  33. | WiComp -- {A : I -> Type} {phi : I}
  34. -- -> ((i : I) -> Partial phi (A i)
  35. -- -> (A i0)[phi -> u i0] -> (A i1)[phi -> u i1]
  36. deriving (Eq, Show, Ord)
  37. data Term
  38. = Ref Name
  39. | App Plicity Term Term
  40. | Lam Plicity Text Term
  41. | Pi Plicity Text Term Term
  42. | Meta MV
  43. | Type
  44. | Typeω
  45. | Sigma Text Term Term
  46. | Pair Term Term
  47. | Proj1 Term
  48. | Proj2 Term
  49. | I
  50. | I0 | I1
  51. | IAnd Term Term
  52. | IOr Term Term
  53. | INot Term
  54. | PathP Term Term Term
  55. -- ^ PathP : (A : I -> Type) -> A i0 -> A i1 -> Type
  56. | IElim Term Term Term Term Term
  57. -- ^ IElim : {A : I -> Type} {x : A i0} {y : A i1} (p : PathP A x y) (i : I) -> A i
  58. | PathIntro Term Term Term Term
  59. -- ^ PathIntro : (A : I -> Type) (f : (i : I) -> A i) -> PathP A (f i0) (f i1)
  60. -- ~~~~~~~~~ not printed at all
  61. | IsOne Term
  62. | IsOne1 Term
  63. | IsOne2 Term
  64. | ItIsOne
  65. | Partial Term Term
  66. | PartialP Term Term
  67. | System (Map Term Term)
  68. | Sub Term Term Term
  69. | Inc Term Term Term
  70. | Ouc Term Term Term Term
  71. | Comp Term Term Term Term
  72. deriving (Eq, Show, Ord)
  73. data MV =
  74. MV { mvName :: Text
  75. , mvCell :: {-# UNPACK #-} !(IORef (Maybe Value))
  76. }
  77. instance Eq MV where
  78. (==) = (==) `on` mvName
  79. instance Ord MV where
  80. (<=) = (<=) `on` mvName
  81. instance Show MV where
  82. show = ('?':) . T.unpack . mvName
  83. data Name
  84. = Bound Text
  85. | Defined Text
  86. deriving (Eq, Show, Ord)
  87. type NFType = Value
  88. type NFEndp = Value
  89. type NFLine = Value
  90. type NFSort = Value
  91. data Value
  92. = VNe Head (Seq Projection)
  93. | VLam Plicity Closure
  94. | VPi Plicity Value Closure
  95. | VSigma Value Closure
  96. | VPair Value Value
  97. | VType | VTypeω
  98. | VI
  99. | VI0 | VI1
  100. | VIAnd NFEndp NFEndp
  101. | VIOr NFEndp NFEndp
  102. | VINot NFEndp
  103. | VPath NFLine Value Value
  104. | VLine NFLine Value Value Value
  105. | VIsOne NFEndp
  106. | VItIsOne
  107. | VIsOne1 NFEndp
  108. | VIsOne2 NFEndp
  109. | VPartial NFEndp Value
  110. | VPartialP NFEndp Value
  111. | VSystem (Map Value Value)
  112. | VSub NFSort NFEndp Value
  113. | VInc NFSort NFEndp Value
  114. | VComp NFSort NFEndp Value Value
  115. deriving (Eq, Show, Ord)
  116. pattern VVar :: Name -> Value
  117. pattern VVar x = VNe (HVar x) Seq.Empty
  118. quoteWith :: Set Text -> Value -> Term
  119. quoteWith names (VNe h sp) = foldl goSpine (goHead h) sp where
  120. goHead (HVar v) = Ref v
  121. goHead (HMeta m) = Meta m
  122. goSpine t (PApp p v) = App p t (quoteWith names v)
  123. goSpine t (PIElim l x y i) = IElim (quote l) (quote x) (quote y) t (quote i)
  124. goSpine t PProj1 = Proj1 t
  125. goSpine t PProj2 = Proj2 t
  126. goSpine t (POuc a phi u) = Ouc (quote a) (quote phi) (quote u) t
  127. quoteWith names (VLam p (Closure n k)) =
  128. let n' = refresh Nothing names n
  129. in Lam p n' (quoteWith (Set.insert n' names) (k (VVar (Bound n'))))
  130. quoteWith names (VPi p d (Closure n k)) =
  131. let n' = refresh (Just d) names n
  132. in Pi p n' (quoteWith names d) (quoteWith (Set.insert n' names) (k (VVar (Bound n'))))
  133. quoteWith names (VSigma d (Closure n k)) =
  134. let n' = refresh (Just d) names n
  135. in Sigma n' (quoteWith names d) (quoteWith (Set.insert n' names) (k (VVar (Bound n'))))
  136. quoteWith names (VPair a b) = Pair (quoteWith names a) (quoteWith names b)
  137. quoteWith _ VType = Type
  138. quoteWith _ VTypeω = Typeω
  139. quoteWith _ VI = I
  140. quoteWith _ VI0 = I0
  141. quoteWith _ VI1 = I1
  142. quoteWith names (VIAnd x y) = IAnd (quoteWith names x) (quoteWith names y)
  143. quoteWith names (VIOr x y) = IOr (quoteWith names x) (quoteWith names y)
  144. quoteWith names (VINot x) = INot (quoteWith names x)
  145. quoteWith names (VPath line x y) = PathP (quoteWith names line) (quoteWith names x) (quoteWith names y)
  146. quoteWith names (VLine p x y f) = PathIntro (quoteWith names p) (quoteWith names x) (quoteWith names y) (quoteWith names f)
  147. quoteWith names (VIsOne v) = IsOne (quoteWith names v)
  148. quoteWith names (VIsOne1 v) = IsOne1 (quoteWith names v)
  149. quoteWith names (VIsOne2 v) = IsOne2 (quoteWith names v)
  150. quoteWith _ VItIsOne = ItIsOne
  151. quoteWith names (VPartial x y) = Partial (quoteWith names x) (quoteWith names y)
  152. quoteWith names (VPartialP x y) = PartialP (quoteWith names x) (quoteWith names y)
  153. quoteWith names (VSystem fs) = System (Map.fromList (map (\(x, y) -> (quoteWith names x, quoteWith names y)) (Map.toList fs)))
  154. quoteWith names (VSub a b c) = Sub (quoteWith names a) (quoteWith names b) (quoteWith names c)
  155. quoteWith names (VInc a b c) = Inc (quoteWith names a) (quoteWith names b) (quoteWith names c)
  156. quoteWith names (VComp a phi u a0) = Comp (quoteWith names a) (quoteWith names phi) (quoteWith names u) (quoteWith names a0)
  157. refresh :: Maybe Value -> Set Text -> Text -> Text
  158. refresh (Just VI) n _ = refresh Nothing n (T.pack "phi")
  159. refresh x s n
  160. | T.singleton '_' == n = n
  161. | n `Set.notMember` s = n
  162. | otherwise = refresh x s (n <> T.singleton '\'')
  163. quote :: Value -> Term
  164. quote = quoteWith mempty
  165. data Closure
  166. = Closure
  167. { clArgName :: Text
  168. , clCont :: Value -> Value
  169. }
  170. instance Show Closure where
  171. show (Closure n c) = "Closure \\" ++ show n ++ " -> " ++ show (c (VVar (Bound n)))
  172. instance Eq Closure where
  173. Closure _ k == Closure _ k' =
  174. k (VVar (Bound (T.pack "_"))) == k' (VVar (Bound (T.pack "_")))
  175. instance Ord Closure where
  176. Closure _ k <= Closure _ k' =
  177. k (VVar (Bound (T.pack "_"))) <= k' (VVar (Bound (T.pack "_")))
  178. data Head
  179. = HVar Name
  180. | HMeta MV
  181. deriving (Eq, Show, Ord)
  182. data Projection
  183. = PApp Plicity Value
  184. | PIElim Value Value Value NFEndp
  185. | PProj1
  186. | PProj2
  187. | POuc NFSort NFEndp Value
  188. deriving (Eq, Show, Ord)