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.

279 lines
8.2 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(..))
  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. }
  92. instance Eq MV where
  93. (==) = (==) `on` mvName
  94. instance Ord MV where
  95. (<=) = (<=) `on` mvName
  96. instance Show MV where
  97. show = ('?':) . T.unpack . mvName
  98. instance Data MV where
  99. toConstr _ = error "toConstr"
  100. gunfold _ _ = error "gunfold"
  101. dataTypeOf _ = mkNoRepType "MV"
  102. data Name
  103. = Bound {getNameText :: Text, getNameNum :: !Int}
  104. | Defined {getNameText :: Text, getNameNum :: !Int}
  105. deriving (Eq, Show, Ord, Data)
  106. type NFType = Value
  107. type NFEndp = Value
  108. type NFLine = Value
  109. type NFSort = Value
  110. type NFPartial = Value
  111. data Value
  112. = VNe Head (Seq Projection)
  113. | VLam Plicity Closure
  114. | VPi Plicity Value Closure
  115. | VSigma Value Closure
  116. | VPair Value Value
  117. | VType | VTypeω
  118. | VI
  119. | VI0 | VI1
  120. | VIAnd NFEndp NFEndp
  121. | VIOr NFEndp NFEndp
  122. | VINot NFEndp
  123. | VPath NFLine Value Value
  124. | VLine NFLine Value Value Value
  125. | VIsOne NFEndp
  126. | VItIsOne
  127. | VIsOne1 NFEndp
  128. | VIsOne2 NFEndp
  129. | VPartial NFEndp Value
  130. | VPartialP NFEndp Value
  131. | VSystem (Map Value Value)
  132. | VSub NFSort NFEndp Value
  133. | VInc NFSort NFEndp Value
  134. | VComp NFSort NFEndp Value Value
  135. | VGlueTy NFSort NFEndp NFPartial NFPartial
  136. | VGlue NFSort NFEndp NFPartial NFPartial NFPartial Value
  137. | VUnglue NFSort NFEndp NFPartial NFPartial Value
  138. | VBool
  139. | VTt
  140. | VFf
  141. | VIf Value Value Value Value
  142. deriving (Eq, Show, Ord)
  143. pattern VVar :: Name -> Value
  144. pattern VVar x = VNe (HVar x) Seq.Empty
  145. quoteWith :: Set Name -> Value -> Term
  146. quoteWith names (VNe h sp) = foldl goSpine (goHead h) sp where
  147. goHead (HVar v) = Ref v
  148. goHead (HMeta m) = Meta m
  149. goSpine t (PApp p v) = App p t (quoteWith names v)
  150. goSpine t (PIElim l x y i) = IElim (quote l) (quote x) (quote y) t (quote i)
  151. goSpine t PProj1 = Proj1 t
  152. goSpine t PProj2 = Proj2 t
  153. goSpine t (POuc a phi u) = Ouc (quote a) (quote phi) (quote u) t
  154. quoteWith names (VLam p (Closure n k)) =
  155. let n' = refresh Nothing names n
  156. in Lam p n' (quoteWith (Set.insert n' names) (k (VVar n')))
  157. quoteWith names (VPi p d (Closure n k)) =
  158. let n' = refresh (Just d) names n
  159. in Pi p n' (quoteWith names d) (quoteWith (Set.insert n' names) (k (VVar n')))
  160. quoteWith names (VSigma d (Closure n k)) =
  161. let n' = refresh (Just d) names n
  162. in Sigma n' (quoteWith names d) (quoteWith (Set.insert n' names) (k (VVar n')))
  163. quoteWith names (VPair a b) = Pair (quoteWith names a) (quoteWith names b)
  164. quoteWith _ VType = Type
  165. quoteWith _ VTypeω = Typeω
  166. quoteWith _ VI = I
  167. quoteWith _ VI0 = I0
  168. quoteWith _ VI1 = I1
  169. quoteWith names (VIAnd x y) = IAnd (quoteWith names x) (quoteWith names y)
  170. quoteWith names (VIOr x y) = IOr (quoteWith names x) (quoteWith names y)
  171. quoteWith names (VINot x) = INot (quoteWith names x)
  172. quoteWith names (VPath line x y) = PathP (quoteWith names line) (quoteWith names x) (quoteWith names y)
  173. quoteWith names (VLine p x y f) = PathIntro (quoteWith names p) (quoteWith names x) (quoteWith names y) (quoteWith names f)
  174. quoteWith names (VIsOne v) = IsOne (quoteWith names v)
  175. quoteWith names (VIsOne1 v) = IsOne1 (quoteWith names v)
  176. quoteWith names (VIsOne2 v) = IsOne2 (quoteWith names v)
  177. quoteWith _ VItIsOne = ItIsOne
  178. quoteWith names (VPartial x y) = Partial (quoteWith names x) (quoteWith names y)
  179. quoteWith names (VPartialP x y) = PartialP (quoteWith names x) (quoteWith names y)
  180. quoteWith names (VSystem fs) = System (Map.fromList (map (\(x, y) -> (quoteWith names x, quoteWith names y)) (Map.toList fs)))
  181. quoteWith names (VSub a b c) = Sub (quoteWith names a) (quoteWith names b) (quoteWith names c)
  182. quoteWith names (VInc a b c) = Inc (quoteWith names a) (quoteWith names b) (quoteWith names c)
  183. quoteWith names (VComp a phi u a0) = Comp (quoteWith names a) (quoteWith names phi) (quoteWith names u) (quoteWith names a0)
  184. quoteWith names (VGlueTy a phi t e) = GlueTy (quoteWith names a) (quoteWith names phi) (quoteWith names t) (quoteWith names e)
  185. 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)
  186. quoteWith names (VUnglue a phi ty e x) = Unglue (quoteWith names a) (quoteWith names phi) (quoteWith names ty) (quoteWith names e) (quoteWith names x)
  187. quoteWith _ames VBool = Bool
  188. quoteWith _ames VTt = Tt
  189. quoteWith _ames VFf = Ff
  190. quoteWith names (VIf a b c d) = If (quoteWith names a) (quoteWith names b) (quoteWith names c) (quoteWith names d)
  191. refresh :: Maybe Value -> Set Name -> Name -> Name
  192. refresh (Just VI) n _ = refresh Nothing n (Bound (T.pack "phi") 0)
  193. refresh x s n
  194. | T.singleton '_' == getNameText n = n
  195. | n `Set.notMember` s = n
  196. | otherwise = refresh x s (Bound (getNameText n <> T.singleton '\'') 0)
  197. quote :: Value -> Term
  198. quote = quoteWith mempty
  199. data Closure
  200. = Closure
  201. { clArgName :: Name
  202. , clCont :: Value -> Value
  203. }
  204. instance Show Closure where
  205. show (Closure n c) = "Closure \\" ++ show n ++ " -> " ++ show (c (VVar n))
  206. instance Eq Closure where
  207. Closure _ k == Closure _ k' =
  208. k (VVar (Bound (T.pack "_") 0)) == k' (VVar (Bound (T.pack "_") 0))
  209. instance Ord Closure where
  210. Closure _ k <= Closure _ k' =
  211. k (VVar (Bound (T.pack "_") 0)) <= k' (VVar (Bound (T.pack "_") 0))
  212. data Head
  213. = HVar Name
  214. | HMeta MV
  215. deriving (Eq, Show, Ord)
  216. data Projection
  217. = PApp Plicity Value
  218. | PIElim Value Value Value NFEndp
  219. | PProj1
  220. | PProj2
  221. | POuc NFSort NFEndp Value
  222. deriving (Eq, Show, Ord)