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.

315 lines
9.6 KiB

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