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.

301 lines
8.9 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. import Data.Monoid
  18. import Debug.Trace (traceShow)
  19. data WiredIn
  20. = WiType
  21. | WiPretype
  22. | WiInterval
  23. | WiI0
  24. | WiI1
  25. | WiIAnd
  26. | WiIOr
  27. | WiINot
  28. | WiPathP
  29. | WiIsOne -- Proposition associated with an element of the interval
  30. | WiItIsOne -- 1 = 1
  31. | WiIsOne1 -- i j -> [i] -> [ior i j]
  32. | WiIsOne2 -- i j -> [j] -> [ior i j]
  33. | WiPartial -- (φ : I) -> Type -> Typeω
  34. | WiPartialP -- (φ : I) -> Partial r Type -> Typeω
  35. | WiSub -- (A : Type) (φ : I) -> Partial φ A -> Typeω
  36. | WiInS -- {A : Type} {φ : I} (u : A) -> Sub A φ (λ x -> u)
  37. | WiOutS -- {A : Type} {φ : I} {u : Partial φ A} -> Sub A φ u -> A
  38. | WiComp -- {A : I -> Type} {phi : I}
  39. -- -> ((i : I) -> Partial phi (A i)
  40. -- -> (A i0)[phi -> u i0] -> (A i1)[phi -> u i1]
  41. | WiGlue -- (A : Type) {phi : I} (T : Partial phi Type) (e : PartialP phi (\o -> Equiv (T o) A)) -> Type
  42. | 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
  43. | WiUnglue -- {A : Type} {phi : I} {T : Partial phi Type} {e : PartialP phi (\o -> Equiv (T o) A)} -> Glue A phi T e -> A
  44. | WiBool
  45. | WiTrue
  46. | WiFalse
  47. | WiIf
  48. deriving (Eq, Show, Ord)
  49. data Term
  50. = Ref 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. | IsOne Term
  75. | IsOne1 Term
  76. | IsOne2 Term
  77. | ItIsOne
  78. | Partial Term Term
  79. | PartialP Term Term
  80. | System (Map Term Term)
  81. | Sub Term Term Term
  82. | Inc Term Term Term
  83. | Ouc Term Term Term Term
  84. | Comp 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. -- ugly. TODO: proper inductive types
  89. | Bool | Tt | Ff | If 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. }
  97. instance Eq MV where
  98. (==) = (==) `on` mvName
  99. instance Ord MV where
  100. (<=) = (<=) `on` mvName
  101. instance Show MV where
  102. show x = show (mvName x, mvSpan x)
  103. instance Data MV where
  104. toConstr _ = error "toConstr"
  105. gunfold _ _ = error "gunfold"
  106. dataTypeOf _ = mkNoRepType "MV"
  107. data Name
  108. = Bound {getNameText :: Text, getNameNum :: !Int}
  109. | Defined {getNameText :: Text, getNameNum :: !Int}
  110. deriving (Eq, Show, Ord, Data)
  111. type NFType = Value
  112. type NFEndp = Value
  113. type NFLine = Value
  114. type NFSort = Value
  115. type NFPartial = Value
  116. data Value
  117. = VNe Head (Seq Projection)
  118. | VLam Plicity Closure
  119. | VPi Plicity Value Closure
  120. | VSigma Value Closure
  121. | VPair Value Value
  122. | GluedVl Head (Seq Projection) Value
  123. | VType | VTypeω
  124. | VI
  125. | VI0 | VI1
  126. | VIAnd NFEndp NFEndp
  127. | VIOr NFEndp NFEndp
  128. | VINot NFEndp
  129. | VPath NFLine Value Value
  130. | VLine NFLine Value Value Value
  131. | VIsOne NFEndp
  132. | VItIsOne
  133. | VIsOne1 NFEndp
  134. | VIsOne2 NFEndp
  135. | VPartial NFEndp Value
  136. | VPartialP NFEndp Value
  137. | VSystem (Map Value Value)
  138. | VSub NFSort NFEndp Value
  139. | VInc NFSort NFEndp Value
  140. | VComp NFSort NFEndp Value Value
  141. | VGlueTy NFSort NFEndp NFPartial NFPartial
  142. | VGlue NFSort NFEndp NFPartial NFPartial NFPartial Value
  143. | VUnglue NFSort NFEndp NFPartial NFPartial Value
  144. | VBool
  145. | VTt
  146. | VFf
  147. | VIf Value Value Value Value
  148. deriving (Eq, Show, Ord)
  149. pattern VVar :: Name -> Value
  150. pattern VVar x = VNe (HVar x) Seq.Empty
  151. quoteWith :: Set Name -> Value -> Term
  152. quoteWith names (VNe h sp) = foldl goSpine (goHead h) sp where
  153. goHead (HVar v) = Ref v
  154. goHead (HMeta m) = Meta m
  155. goSpine t (PApp p v) = App p t (quoteWith names v)
  156. goSpine t (PIElim l x y i) = IElim (quote l) (quote x) (quote y) t (quote i)
  157. goSpine t PProj1 = Proj1 t
  158. goSpine t PProj2 = Proj2 t
  159. goSpine t (POuc a phi u) = Ouc (quote a) (quote phi) (quote u) t
  160. quoteWith names (GluedVl h sp (VLam p (Closure n k))) =
  161. quoteWith names (VLam p (Closure n (\a -> GluedVl h (sp Seq.:|> PApp p a) (k a))))
  162. quoteWith names (GluedVl h sp vl)
  163. | GluedVl _ _ inner <- vl = quoteWith names (GluedVl h sp inner)
  164. | Seq.Empty <- sp = quoteWith names vl
  165. | alwaysShort vl = quoteWith names vl
  166. | otherwise = quoteWith names (VNe h sp)
  167. quoteWith names (VLam p (Closure n k)) =
  168. let n' = refresh Nothing names n
  169. in Lam p n' (quoteWith (Set.insert n' names) (k (VVar n')))
  170. quoteWith names (VPi p d (Closure n k)) =
  171. let n' = refresh (Just d) names n
  172. in Pi p n' (quoteWith names d) (quoteWith (Set.insert n' names) (k (VVar n')))
  173. quoteWith names (VSigma d (Closure n k)) =
  174. let n' = refresh (Just d) names n
  175. in Sigma n' (quoteWith names d) (quoteWith (Set.insert n' names) (k (VVar n')))
  176. quoteWith names (VPair a b) = Pair (quoteWith names a) (quoteWith names b)
  177. quoteWith _ VType = Type
  178. quoteWith _ VTypeω = Typeω
  179. quoteWith _ VI = I
  180. quoteWith _ VI0 = I0
  181. quoteWith _ VI1 = I1
  182. quoteWith names (VIAnd x y) = IAnd (quoteWith names x) (quoteWith names y)
  183. quoteWith names (VIOr x y) = IOr (quoteWith names x) (quoteWith names y)
  184. quoteWith names (VINot x) = INot (quoteWith names x)
  185. quoteWith names (VPath line x y) = PathP (quoteWith names line) (quoteWith names x) (quoteWith names y)
  186. quoteWith names (VLine p x y f) = PathIntro (quoteWith names p) (quoteWith names x) (quoteWith names y) (quoteWith names f)
  187. quoteWith names (VIsOne v) = IsOne (quoteWith names v)
  188. quoteWith names (VIsOne1 v) = IsOne1 (quoteWith names v)
  189. quoteWith names (VIsOne2 v) = IsOne2 (quoteWith names v)
  190. quoteWith _ VItIsOne = ItIsOne
  191. quoteWith names (VPartial x y) = Partial (quoteWith names x) (quoteWith names y)
  192. quoteWith names (VPartialP x y) = PartialP (quoteWith names x) (quoteWith names y)
  193. quoteWith names (VSystem fs) = System (Map.fromList (map (\(x, y) -> (quoteWith names x, quoteWith names y)) (Map.toList fs)))
  194. quoteWith names (VSub a b c) = Sub (quoteWith names a) (quoteWith names b) (quoteWith names c)
  195. quoteWith names (VInc a b c) = Inc (quoteWith names a) (quoteWith names b) (quoteWith names c)
  196. quoteWith names (VComp a phi u a0) = Comp (quoteWith names a) (quoteWith names phi) (quoteWith names u) (quoteWith names a0)
  197. quoteWith names (VGlueTy a phi t e) = GlueTy (quoteWith names a) (quoteWith names phi) (quoteWith names t) (quoteWith names e)
  198. 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)
  199. quoteWith names (VUnglue a phi ty e x) = Unglue (quoteWith names a) (quoteWith names phi) (quoteWith names ty) (quoteWith names e) (quoteWith names x)
  200. quoteWith _ames VBool = Bool
  201. quoteWith _ames VTt = Tt
  202. quoteWith _ames VFf = Ff
  203. quoteWith names (VIf a b c d) = If (quoteWith names a) (quoteWith names b) (quoteWith names c) (quoteWith names d)
  204. alwaysShort :: Value -> Bool
  205. alwaysShort VBool{} = True
  206. alwaysShort VTt{} = True
  207. alwaysShort VFf{} = True
  208. alwaysShort VVar{} = True
  209. alwaysShort _ = False
  210. refresh :: Maybe Value -> Set Name -> Name -> Name
  211. refresh (Just VI) n _ = refresh Nothing n (Bound (T.pack "phi") 0)
  212. refresh x s n
  213. | T.singleton '_' == getNameText n = n
  214. | n `Set.notMember` s = n
  215. | otherwise = refresh x s (Bound (getNameText n <> T.singleton '\'') 0)
  216. quote :: Value -> Term
  217. quote = quoteWith mempty
  218. data Closure
  219. = Closure
  220. { clArgName :: Name
  221. , clCont :: Value -> Value
  222. }
  223. instance Show Closure where
  224. show (Closure n c) = "Closure \\" ++ show n ++ " -> " ++ show (c (VVar n))
  225. instance Eq Closure where
  226. Closure _ k == Closure _ k' =
  227. k (VVar (Bound (T.pack "_") 0)) == k' (VVar (Bound (T.pack "_") 0))
  228. instance Ord Closure where
  229. Closure _ k <= Closure _ k' =
  230. k (VVar (Bound (T.pack "_") 0)) <= k' (VVar (Bound (T.pack "_") 0))
  231. data Head
  232. = HVar Name
  233. | HMeta MV
  234. deriving (Eq, Show, Ord)
  235. data Projection
  236. = PApp Plicity Value
  237. | PIElim Value Value Value NFEndp
  238. | PProj1
  239. | PProj2
  240. | POuc NFSort NFEndp Value
  241. deriving (Eq, Show, Ord)