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.

281 lines
8.3 KiB

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