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.

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