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.

343 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. , mvInteraction :: Bool
  97. , mvContext :: Map Name NFType
  98. }
  99. instance Eq MV where
  100. (==) = (==) `on` mvName
  101. instance Ord MV where
  102. (<=) = (<=) `on` mvName
  103. instance Show MV where
  104. show x = show (mvName x, mvSpan x)
  105. instance Data MV where
  106. toConstr _ = error "toConstr"
  107. gunfold _ _ = error "gunfold"
  108. dataTypeOf _ = mkNoRepType "MV"
  109. data Name
  110. = Bound {getNameText :: Text, getNameNum :: !Int}
  111. | Defined {getNameText :: Text, getNameNum :: !Int}
  112. | ConName {getNameText :: Text, getNameNum :: !Int, conSkip :: !Int, conArity :: !Int}
  113. deriving (Show, Data)
  114. instance Eq Name where
  115. x == y = getNameText x == getNameText y && getNameNum x == getNameNum y
  116. instance Ord Name where
  117. compare x y = getNameText x `compare` getNameText y <> getNameNum x `compare` getNameNum y
  118. type NFType = Value
  119. type NFEndp = Value
  120. type NFLine = Value
  121. type NFSort = Value
  122. type NFPartial = Value
  123. data Value
  124. = VNe Head (Seq Projection)
  125. | VLam Plicity Closure
  126. | VPi Plicity Value Closure
  127. | VSigma Value Closure
  128. | VPair Value Value
  129. | GluedVl Head (Seq Projection) Value
  130. | VType | VTypeω
  131. | VI
  132. | VI0 | VI1
  133. | VIAnd NFEndp NFEndp
  134. | VIOr NFEndp NFEndp
  135. | VINot NFEndp
  136. | VPath NFLine Value Value
  137. | VLine NFLine Value Value Value
  138. | VPartial NFEndp Value
  139. | VPartialP NFEndp Value
  140. | VSystem (Map Value Value)
  141. | VSub NFSort NFEndp Value
  142. | VInc NFSort NFEndp Value
  143. | VComp NFLine NFEndp Value Value
  144. | VHComp NFSort NFEndp Value Value
  145. | VGlueTy NFSort NFEndp NFPartial NFPartial
  146. | VGlue NFSort NFEndp NFPartial NFPartial NFPartial Value
  147. | VUnglue NFSort NFEndp NFPartial NFPartial Value
  148. | VCase (Map.Map Name (NFType, Value)) Value Value [(Term, Int, Term)]
  149. | VEqStrict NFSort Value Value
  150. | VReflStrict NFSort Value
  151. deriving (Eq, Show, Ord)
  152. pattern VVar :: Name -> Value
  153. pattern VVar x = VNe (HVar x) Seq.Empty
  154. quoteWith :: Set Name -> Value -> Term
  155. quoteWith names (VNe h sp) = foldl goSpine (goHead h) sp where
  156. goHead (HVar v) = Ref v
  157. goHead (HMeta m) = Meta m
  158. goHead (HCon _ v) = Con v
  159. goHead (HPCon sys _ v) =
  160. case sys of
  161. VSystem f ->
  162. case Map.lookup VI1 f of
  163. Just vl -> constantly (length sp) vl
  164. _ -> PCon (quote sys) v
  165. VLam{} -> PCon (quote sys) v
  166. s -> constantly (length sp) s
  167. goHead (HData x v) = Data x v
  168. goSpine t (PApp p v) = App p t (quoteWith names v)
  169. goSpine t (PIElim l x y i) = IElim (quote l) (quote x) (quote y) t (quote i)
  170. goSpine t (PK l x y i) = AxK (quote l) (quote x) (quote y) (quote i) t
  171. goSpine t (PJ l x y i f) = AxJ (quote l) (quote x) (quote y) (quote i) (quote f) t
  172. goSpine t PProj1 = Proj1 t
  173. goSpine t PProj2 = Proj2 t
  174. goSpine t (POuc a phi u) = Ouc (quote a) (quote phi) (quote u) t
  175. constantly 0 n = quoteWith names n
  176. constantly k x = Lam Ex (Bound (T.pack "x") (negate 1)) $ constantly (k - 1) x
  177. quoteWith names (GluedVl _ Seq.Empty x) = quoteWith names x
  178. quoteWith names (GluedVl h sp (VLam p (Closure n k))) =
  179. quoteWith names (VLam p (Closure n (\a -> GluedVl h (sp Seq.:|> PApp p a) (k a))))
  180. quoteWith names (GluedVl h sp (VLine ty x y (VLam p (Closure n k)))) =
  181. quoteWith names (VLine ty x y (VLam p (Closure n (\a -> GluedVl h (sp Seq.:|> PIElim ty x y 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. | _ Seq.:|> PIElim _ x y i <- sp =
  186. case i of
  187. VI0 -> quoteWith names x
  188. VI1 -> quoteWith names y
  189. _ -> quoteWith names (VNe h sp)
  190. | otherwise = quoteWith names (VNe h sp)
  191. quoteWith names (VLam p (Closure n k)) =
  192. let n' = refresh Nothing names n
  193. in Lam p n' (quoteWith (Set.insert n' names) (k (VVar n')))
  194. quoteWith names (VPi p d (Closure n k)) =
  195. let n' = refresh (Just d) names n
  196. in Pi p n' (quoteWith names d) (quoteWith (Set.insert n' names) (k (VVar n')))
  197. quoteWith names (VSigma d (Closure n k)) =
  198. let n' = refresh (Just d) names n
  199. in Sigma n' (quoteWith names d) (quoteWith (Set.insert n' names) (k (VVar n')))
  200. quoteWith names (VPair a b) = Pair (quoteWith names a) (quoteWith names b)
  201. quoteWith _ VType = Type
  202. quoteWith _ VTypeω = Typeω
  203. quoteWith _ VI = I
  204. quoteWith _ VI0 = I0
  205. quoteWith _ VI1 = I1
  206. quoteWith names (VIAnd x y) = IAnd (quoteWith names x) (quoteWith names y)
  207. quoteWith names (VIOr x y) = IOr (quoteWith names x) (quoteWith names y)
  208. quoteWith names (VINot x) = INot (quoteWith names x)
  209. quoteWith names (VPath line x y) = PathP (quoteWith names line) (quoteWith names x) (quoteWith names y)
  210. quoteWith names (VLine p x y f) = PathIntro (quoteWith names p) (quoteWith names x) (quoteWith names y) (quoteWith names f)
  211. quoteWith names (VPartial x y) = Partial (quoteWith names x) (quoteWith names y)
  212. quoteWith names (VPartialP x y) = PartialP (quoteWith names x) (quoteWith names y)
  213. quoteWith names (VSystem fs) = System (Map.fromList (map (\(x, y) -> (quoteWith names x, quoteWith names y)) (Map.toList fs)))
  214. quoteWith names (VSub a b c) = Sub (quoteWith names a) (quoteWith names b) (quoteWith names c)
  215. quoteWith names (VInc a b c) = Inc (quoteWith names a) (quoteWith names b) (quoteWith names c)
  216. quoteWith names (VComp a phi u a0) = Comp (quoteWith names a) (quoteWith names phi) (quoteWith names u) (quoteWith names a0)
  217. quoteWith names (VHComp a phi u a0) = HComp (quoteWith names a) (quoteWith names phi) (quoteWith names u) (quoteWith names a0)
  218. quoteWith names (VGlueTy a phi t e) = GlueTy (quoteWith names a) (quoteWith names phi) (quoteWith names t) (quoteWith names e)
  219. 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)
  220. quoteWith names (VUnglue a phi ty e x) = Unglue (quoteWith names a) (quoteWith names phi) (quoteWith names ty) (quoteWith names e) (quoteWith names x)
  221. quoteWith names (VCase _ rng v xs) = Case (quoteWith names rng) (quoteWith names v) xs
  222. quoteWith names (VEqStrict a x y) = EqS (quoteWith names a) (quoteWith names x) (quoteWith names y)
  223. quoteWith names (VReflStrict a x) = Syntax.Refl (quoteWith names a) (quoteWith names x)
  224. alwaysShort :: Value -> Bool
  225. alwaysShort (VNe HCon{} _) = True
  226. alwaysShort (VNe HPCon{} _) = True
  227. alwaysShort VVar{} = True
  228. alwaysShort (VLine _ _ _ v) = alwaysShort v
  229. alwaysShort (VLam _ (Closure n k)) = alwaysShort (k (VVar n))
  230. alwaysShort VHComp{} = True
  231. alwaysShort _ = False
  232. refresh :: Maybe Value -> Set Name -> Name -> Name
  233. refresh (Just VI) n _ = refresh Nothing n (Bound (T.pack "phi") 0)
  234. refresh x s n
  235. | T.singleton '_' == getNameText n = n
  236. | n `Set.notMember` s = n
  237. | otherwise = refresh x s (Bound (getNameText n <> T.singleton '\'') 0)
  238. quote :: Value -> Term
  239. quote = quoteWith mempty
  240. data Closure
  241. = Closure
  242. { clArgName :: Name
  243. , clCont :: Value -> Value
  244. }
  245. instance Show Closure where
  246. show (Closure n c) = "Closure \\" ++ show n ++ " -> " ++ show (c (VVar n))
  247. instance Eq Closure where
  248. Closure _ k == Closure _ k' =
  249. k (VVar (Bound (T.pack "_") 0)) == k' (VVar (Bound (T.pack "_") 0))
  250. instance Ord Closure where
  251. Closure _ k <= Closure _ k' =
  252. k (VVar (Bound (T.pack "_") 0)) <= k' (VVar (Bound (T.pack "_") 0))
  253. data Head
  254. = HVar Name
  255. | HCon Value Name
  256. | HPCon Value Value Name
  257. | HMeta MV
  258. | HData Bool Name
  259. deriving (Eq, Ord, Show)
  260. data Projection
  261. = PApp Plicity Value
  262. | PIElim Value Value Value NFEndp
  263. | PProj1
  264. | PProj2
  265. | POuc NFSort NFEndp Value
  266. | PK NFSort Value NFSort Value
  267. | PJ NFSort Value NFSort Value Value
  268. deriving (Eq, Show, Ord)
  269. data Boundary = Boundary { getBoundaryNames :: [Name], getBoundaryMap :: Value }
  270. deriving (Eq, Show, Ord)
  271. unPi :: Value -> ([(Plicity, Value)], Value)
  272. unPi (VPi p d (Closure n k)) =
  273. let (a, x) = unPi (k (VVar n))
  274. in ((p, d):a, x)
  275. unPi x = ([], x)