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.

335 lines
11 KiB

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