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.

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