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.

352 lines
12 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. | WiPOr -- (A : Type) (φ ψ : I) -> Partial φ A -> Partial ψ A -> Partial (ior φ ψ) A
  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
  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. | WiLineToEquiv -- {A : I -> Type} -> Equiv (P i0) (P i1)
  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. | 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. | HComp Term Term Term Term
  83. | GlueTy Term Term Term Term
  84. | Glue Term Term Term Term Term Term
  85. | Unglue Term Term Term Term Term
  86. | Case Term Term [(Term, Int, Term)]
  87. | EqS Term Term Term
  88. | Refl Term Term
  89. | AxK Term Term Term Term Term
  90. | AxJ Term Term Term Term Term Term
  91. deriving (Eq, Show, Ord, Data)
  92. data MV =
  93. MV { mvName :: Text
  94. , mvCell :: {-# UNPACK #-} !(IORef (Maybe Value))
  95. , mvType :: NFType
  96. , mvSpan :: Maybe (Text, Posn, Posn)
  97. , mvInteraction :: Bool
  98. , mvContext :: Map Name NFType
  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. | VPartial NFEndp Value
  140. | VPartialP NFEndp Value
  141. | VSystem (Map Value Value)
  142. | VSub NFSort NFEndp Value
  143. | VInc NFSort NFEndp Value
  144. | VComp NFLine NFEndp Value Value
  145. | VHComp NFSort NFEndp Value Value
  146. | VGlueTy NFSort NFEndp NFPartial NFPartial
  147. | VGlue NFSort NFEndp NFPartial NFPartial NFPartial Value
  148. | VUnglue NFSort NFEndp NFPartial NFPartial Value
  149. | VCase (Map.Map Name (NFType, Value)) Value Value [(Term, Int, Term)]
  150. | VEqStrict NFSort Value Value
  151. | VReflStrict NFSort Value
  152. deriving (Eq, Show, Ord)
  153. pattern VVar :: Name -> Value
  154. pattern VVar x = VNe (HVar x) Seq.Empty
  155. quoteWith :: Bool -> Set Name -> Value -> Term
  156. quoteWith short names (VNe h sp) = foldl goSpine (goHead h) sp where
  157. goHead (HVar v) = Ref v
  158. goHead (HMeta m) = Meta m
  159. goHead (HCon _ v) = Con v
  160. goHead (HPCon sys _ v) =
  161. case sys of
  162. VSystem f ->
  163. case Map.lookup VI1 f of
  164. Just vl -> constantly (length sp) vl
  165. _ -> PCon (quote sys) v
  166. VLam{} -> PCon (quote sys) v
  167. s -> constantly (length sp) s
  168. goHead (HData x v) = Data x v
  169. goSpine t (PApp p v) = App p t (quoteWith short names v)
  170. goSpine t (PIElim l x y i) = IElim (quote l) (quote x) (quote y) t (quote i)
  171. goSpine t (PK l x y i) = AxK (quote l) (quote x) (quote y) (quote i) t
  172. goSpine t (PJ l x y i f) = AxJ (quote l) (quote x) (quote y) (quote i) (quote f) t
  173. goSpine t PProj1 = Proj1 t
  174. goSpine t PProj2 = Proj2 t
  175. goSpine t (POuc a phi u) = Ouc (quote a) (quote phi) (quote u) t
  176. constantly 0 n = quoteWith short names n
  177. constantly k x = Lam Ex (Bound (T.pack "x") (negate 1)) $ constantly (k - 1) x
  178. quoteWith short names (GluedVl _ Seq.Empty x) = quoteWith short names x
  179. quoteWith short names (GluedVl h sp (VLam p (Closure n k))) =
  180. quoteWith short names (VLam p (Closure n (\a -> GluedVl h (sp Seq.:|> PApp p a) (k a))))
  181. quoteWith short names (GluedVl h sp (VLine ty x y (VLam p (Closure n k)))) =
  182. quoteWith short names (VLine ty x y (VLam p (Closure n (\a -> GluedVl h (sp Seq.:|> PIElim ty x y a) (k a)))))
  183. quoteWith short names (GluedVl h sp vl)
  184. | GluedVl _ _ inner <- vl = quoteWith short names (GluedVl h sp inner)
  185. | short || alwaysShort vl = quoteWith short names vl
  186. | _ Seq.:|> PIElim _ x y i <- sp =
  187. case i of
  188. VI0 -> quoteWith short names x
  189. VI1 -> quoteWith short names y
  190. _ -> quoteWith short names (VNe h sp)
  191. | otherwise = quoteWith short names (VNe h sp)
  192. quoteWith short names (VLam p (Closure n k)) =
  193. let n' = refresh Nothing names n
  194. in Lam p n' (quoteWith short (Set.insert n' names) (k (VVar n')))
  195. quoteWith short names (VPi p d (Closure n k)) =
  196. let n' = refresh (Just d) names n
  197. in Pi p n' (quoteWith short names d) (quoteWith short (Set.insert n' names) (k (VVar n')))
  198. quoteWith short names (VSigma d (Closure n k)) =
  199. let n' = refresh (Just d) names n
  200. in Sigma n' (quoteWith short names d) (quoteWith short (Set.insert n' names) (k (VVar n')))
  201. quoteWith short names (VPair a b) = Pair (quoteWith short names a) (quoteWith short names b)
  202. quoteWith _ _ VType = Type
  203. quoteWith _ _ VTypeω = Typeω
  204. quoteWith _ _ VI = I
  205. quoteWith _ _ VI0 = I0
  206. quoteWith _ _ VI1 = I1
  207. quoteWith short names (VIAnd x y) = IAnd (quoteWith short names x) (quoteWith short names y)
  208. quoteWith short names (VIOr x y) = IOr (quoteWith short names x) (quoteWith short names y)
  209. quoteWith short names (VINot x) = INot (quoteWith short names x)
  210. quoteWith short names (VPath line x y) = PathP (quoteWith short names line) (quoteWith short names x) (quoteWith short names y)
  211. quoteWith short names (VLine p x y f) = PathIntro (quoteWith short names p) (quoteWith short names x) (quoteWith short names y) (quoteWith short names f)
  212. quoteWith short names (VPartial x y) = Partial (quoteWith short names x) (quoteWith short names y)
  213. quoteWith short names (VPartialP x y) = PartialP (quoteWith short names x) (quoteWith short names y)
  214. quoteWith short names (VSystem fs) = System (Map.fromList (map (\(x, y) -> (quoteWith short names x, quoteWith short names y)) (Map.toList fs)))
  215. quoteWith short names (VSub a b c) = Sub (quoteWith short names a) (quoteWith short names b) (quoteWith short names c)
  216. quoteWith short names (VInc a b c) = Inc (quoteWith short names a) (quoteWith short names b) (quoteWith short names c)
  217. quoteWith short names (VComp a phi u a0) = Comp (quoteWith short names a) (quoteWith short names phi) (quoteWith short names u) (quoteWith short names a0)
  218. quoteWith short names (VHComp a phi u a0) = HComp (quoteWith short names a) (quoteWith short names phi) (quoteWith short names u) (quoteWith short names a0)
  219. quoteWith short names (VGlueTy a phi t e) = GlueTy (quoteWith short names a) (quoteWith short names phi) (quoteWith short names t) (quoteWith short names e)
  220. quoteWith short names (VGlue a phi ty e t x) = Glue (quoteWith short names a) (quoteWith short names phi) (quoteWith short names ty) (quoteWith short names e) (quoteWith short names t) (quoteWith short names x)
  221. quoteWith short names (VUnglue a phi ty e x) = Unglue (quoteWith short names a) (quoteWith short names phi) (quoteWith short names ty) (quoteWith short names e) (quoteWith short names x)
  222. quoteWith short names (VCase _ rng v xs) = Case (quoteWith short names rng) (quoteWith short names v) xs
  223. quoteWith short names (VEqStrict a x y) = EqS (quoteWith short names a) (quoteWith short names x) (quoteWith short names y)
  224. quoteWith short names (VReflStrict a x) = Syntax.Refl (quoteWith short names a) (quoteWith short names x)
  225. alwaysShort :: Value -> Bool
  226. alwaysShort (VNe HCon{} _) = True
  227. alwaysShort (VNe HPCon{} _) = True
  228. alwaysShort VVar{} = True
  229. alwaysShort (VLine _ _ _ v) = alwaysShort v
  230. alwaysShort (VLam _ (Closure n k)) = alwaysShort (k (VVar n))
  231. alwaysShort VHComp{} = True
  232. alwaysShort _ = False
  233. refresh :: Maybe Value -> Set Name -> Name -> Name
  234. refresh (Just VI) n _ = refresh Nothing n (Bound (T.pack "phi") 0)
  235. refresh x s n
  236. | T.singleton '_' == getNameText n = n
  237. | n `Set.notMember` s = n
  238. | otherwise = refresh x s (Bound (getNameText n <> T.singleton '\'') 0)
  239. quote :: Value -> Term
  240. quote = quoteWith True mempty
  241. data Closure
  242. = Closure
  243. { clArgName :: Name
  244. , clCont :: Value -> Value
  245. }
  246. instance Show Closure where
  247. show (Closure n c) = "Closure \\" ++ show n ++ " -> " ++ show (c (VVar n))
  248. instance Eq Closure where
  249. Closure _ k == Closure _ k' =
  250. k (VVar (Bound (T.pack "_") 0)) == k' (VVar (Bound (T.pack "_") 0))
  251. instance Ord Closure where
  252. Closure _ k <= Closure _ k' =
  253. k (VVar (Bound (T.pack "_") 0)) <= k' (VVar (Bound (T.pack "_") 0))
  254. data Head
  255. = HVar Name
  256. | HCon Value Name
  257. | HPCon Value Value Name
  258. | HMeta MV
  259. | HData Bool Name
  260. deriving (Ord, Show)
  261. instance Eq Head where
  262. HVar x == HVar y = x == y
  263. HCon _ x == HCon _ y = x == y
  264. HPCon _ _ x == HPCon _ _ y = x == y
  265. HMeta x == HMeta y = x == y
  266. HData x y == HData x' y' = x == x' && y == y'
  267. _ == _ = False
  268. data Projection
  269. = PApp Plicity Value
  270. | PIElim Value Value Value NFEndp
  271. | PProj1
  272. | PProj2
  273. | POuc NFSort NFEndp Value
  274. | PK NFSort Value NFSort Value
  275. | PJ NFSort Value NFSort Value Value
  276. deriving (Eq, Show, Ord)
  277. data Boundary = Boundary { getBoundaryNames :: [Name], getBoundaryMap :: Value }
  278. deriving (Eq, Show, Ord)
  279. unPi :: Value -> ([(Plicity, Value)], Value)
  280. unPi (VPi p d (Closure n k)) =
  281. let (a, x) = unPi (k (VVar n))
  282. in ((p, d):a, x)
  283. unPi x = ([], x)