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.

318 lines
9.3 KiB

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