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.

261 lines
7.8 KiB

  1. {-# LANGUAGE PatternSynonyms #-}
  2. {-# LANGUAGE DeriveDataTypeable #-}
  3. module Syntax where
  4. import qualified Data.Map.Strict as Map
  5. import qualified Data.Sequence as Seq
  6. import qualified Data.Set as Set
  7. import qualified Data.Text as T
  8. import Data.Map.Strict (Map)
  9. import Data.Sequence (Seq)
  10. import Data.Function (on)
  11. import Data.IORef (IORef)
  12. import Data.Text (Text)
  13. import Data.Set (Set)
  14. import Data.Data
  15. import Presyntax.Presyntax (Plicity(..))
  16. data WiredIn
  17. = WiType
  18. | WiPretype
  19. | WiInterval
  20. | WiI0
  21. | WiI1
  22. | WiIAnd
  23. | WiIOr
  24. | WiINot
  25. | WiPathP
  26. | WiIsOne -- Proposition associated with an element of the interval
  27. | WiItIsOne -- 1 = 1
  28. | WiIsOne1 -- i j -> [i] -> [ior i j]
  29. | WiIsOne2 -- i j -> [j] -> [ior i j]
  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. | App Plicity Term Term
  45. | Lam Plicity Name Term
  46. | Pi Plicity Name Term Term
  47. | Let [(Name, Term, Term)] Term
  48. | Meta MV
  49. | Type
  50. | Typeω
  51. | Sigma Name Term Term
  52. | Pair Term Term
  53. | Proj1 Term
  54. | Proj2 Term
  55. | I
  56. | I0 | I1
  57. | IAnd Term Term
  58. | IOr Term Term
  59. | INot Term
  60. | PathP Term Term Term
  61. -- ^ PathP : (A : I -> Type) -> A i0 -> A i1 -> Type
  62. | IElim Term Term Term Term Term
  63. -- ^ IElim : {A : I -> Type} {x : A i0} {y : A i1} (p : PathP A x y) (i : I) -> A i
  64. | PathIntro Term Term Term Term
  65. -- ^ PathIntro : (A : I -> Type) (f : (i : I) -> A i) -> PathP A (f i0) (f i1)
  66. -- ~~~~~~~~~ not printed at all
  67. | IsOne Term
  68. | IsOne1 Term
  69. | IsOne2 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. deriving (Eq, Show, Ord, Data)
  82. data MV =
  83. MV { mvName :: Text
  84. , mvCell :: {-# UNPACK #-} !(IORef (Maybe Value))
  85. }
  86. instance Eq MV where
  87. (==) = (==) `on` mvName
  88. instance Ord MV where
  89. (<=) = (<=) `on` mvName
  90. instance Show MV where
  91. show = ('?':) . T.unpack . mvName
  92. instance Data MV where
  93. toConstr _ = error "toConstr"
  94. gunfold _ _ = error "gunfold"
  95. dataTypeOf _ = mkNoRepType "MV"
  96. data Name
  97. = Bound {getNameText :: Text, getNameNum :: !Int}
  98. | Defined {getNameText :: Text, getNameNum :: !Int}
  99. deriving (Eq, Show, Ord, Data)
  100. type NFType = Value
  101. type NFEndp = Value
  102. type NFLine = Value
  103. type NFSort = Value
  104. type NFPartial = Value
  105. data Value
  106. = VNe Head (Seq Projection)
  107. | VLam Plicity Closure
  108. | VPi Plicity Value Closure
  109. | VSigma Value Closure
  110. | VPair Value Value
  111. | VType | VTypeω
  112. | VI
  113. | VI0 | VI1
  114. | VIAnd NFEndp NFEndp
  115. | VIOr NFEndp NFEndp
  116. | VINot NFEndp
  117. | VPath NFLine Value Value
  118. | VLine NFLine Value Value Value
  119. | VIsOne NFEndp
  120. | VItIsOne
  121. | VIsOne1 NFEndp
  122. | VIsOne2 NFEndp
  123. | VPartial NFEndp Value
  124. | VPartialP NFEndp Value
  125. | VSystem (Map Value Value)
  126. | VSub NFSort NFEndp Value
  127. | VInc NFSort NFEndp Value
  128. | VComp NFSort NFEndp Value Value
  129. | VGlueTy NFSort NFEndp NFPartial NFPartial
  130. | VGlue NFSort NFEndp NFPartial NFPartial NFPartial Value
  131. | VUnglue NFSort NFEndp NFPartial NFPartial Value
  132. deriving (Eq, Show, Ord)
  133. pattern VVar :: Name -> Value
  134. pattern VVar x = VNe (HVar x) Seq.Empty
  135. quoteWith :: Set Name -> Value -> Term
  136. quoteWith names (VNe h sp) = foldl goSpine (goHead h) sp where
  137. goHead (HVar v) = Ref v
  138. goHead (HMeta m) = Meta m
  139. goSpine t (PApp p v) = App p t (quoteWith names v)
  140. goSpine t (PIElim l x y i) = IElim (quote l) (quote x) (quote y) t (quote i)
  141. goSpine t PProj1 = Proj1 t
  142. goSpine t PProj2 = Proj2 t
  143. goSpine t (POuc a phi u) = Ouc (quote a) (quote phi) (quote u) t
  144. quoteWith names (VLam p (Closure n k)) =
  145. let n' = refresh Nothing names n
  146. in Lam p n' (quoteWith (Set.insert n' names) (k (VVar n')))
  147. quoteWith names (VPi p d (Closure n k)) =
  148. let n' = refresh (Just d) names n
  149. in Pi p n' (quoteWith names d) (quoteWith (Set.insert n' names) (k (VVar n')))
  150. quoteWith names (VSigma d (Closure n k)) =
  151. let n' = refresh (Just d) names n
  152. in Sigma n' (quoteWith names d) (quoteWith (Set.insert n' names) (k (VVar n')))
  153. quoteWith names (VPair a b) = Pair (quoteWith names a) (quoteWith names b)
  154. quoteWith _ VType = Type
  155. quoteWith _ VTypeω = Typeω
  156. quoteWith _ VI = I
  157. quoteWith _ VI0 = I0
  158. quoteWith _ VI1 = I1
  159. quoteWith names (VIAnd x y) = IAnd (quoteWith names x) (quoteWith names y)
  160. quoteWith names (VIOr x y) = IOr (quoteWith names x) (quoteWith names y)
  161. quoteWith names (VINot x) = INot (quoteWith names x)
  162. quoteWith names (VPath line x y) = PathP (quoteWith names line) (quoteWith names x) (quoteWith names y)
  163. quoteWith names (VLine p x y f) = PathIntro (quoteWith names p) (quoteWith names x) (quoteWith names y) (quoteWith names f)
  164. quoteWith names (VIsOne v) = IsOne (quoteWith names v)
  165. quoteWith names (VIsOne1 v) = IsOne1 (quoteWith names v)
  166. quoteWith names (VIsOne2 v) = IsOne2 (quoteWith names v)
  167. quoteWith _ VItIsOne = ItIsOne
  168. quoteWith names (VPartial x y) = Partial (quoteWith names x) (quoteWith names y)
  169. quoteWith names (VPartialP x y) = PartialP (quoteWith names x) (quoteWith names y)
  170. quoteWith names (VSystem fs) = System (Map.fromList (map (\(x, y) -> (quoteWith names x, quoteWith names y)) (Map.toList fs)))
  171. quoteWith names (VSub a b c) = Sub (quoteWith names a) (quoteWith names b) (quoteWith names c)
  172. quoteWith names (VInc a b c) = Inc (quoteWith names a) (quoteWith names b) (quoteWith names c)
  173. quoteWith names (VComp a phi u a0) = Comp (quoteWith names a) (quoteWith names phi) (quoteWith names u) (quoteWith names a0)
  174. quoteWith names (VGlueTy a phi t e) = GlueTy (quoteWith names a) (quoteWith names phi) (quoteWith names t) (quoteWith names e)
  175. 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)
  176. quoteWith names (VUnglue a phi ty e x) = Unglue (quoteWith names a) (quoteWith names phi) (quoteWith names ty) (quoteWith names e) (quoteWith names x)
  177. refresh :: Maybe Value -> Set Name -> Name -> Name
  178. refresh (Just VI) n _ = refresh Nothing n (Bound (T.pack "phi") 0)
  179. refresh x s n
  180. | T.singleton '_' == getNameText n = n
  181. | n `Set.notMember` s = n
  182. | otherwise = refresh x s (Bound (getNameText n <> T.singleton '\'') 0)
  183. quote :: Value -> Term
  184. quote = quoteWith mempty
  185. data Closure
  186. = Closure
  187. { clArgName :: Name
  188. , clCont :: Value -> Value
  189. }
  190. instance Show Closure where
  191. show (Closure n c) = "Closure \\" ++ show n ++ " -> " ++ show (c (VVar n))
  192. instance Eq Closure where
  193. Closure _ k == Closure _ k' =
  194. k (VVar (Bound (T.pack "_") 0)) == k' (VVar (Bound (T.pack "_") 0))
  195. instance Ord Closure where
  196. Closure _ k <= Closure _ k' =
  197. k (VVar (Bound (T.pack "_") 0)) <= k' (VVar (Bound (T.pack "_") 0))
  198. data Head
  199. = HVar Name
  200. | HMeta MV
  201. deriving (Eq, Show, Ord)
  202. data Projection
  203. = PApp Plicity Value
  204. | PIElim Value Value Value NFEndp
  205. | PProj1
  206. | PProj2
  207. | POuc NFSort NFEndp Value
  208. deriving (Eq, Show, Ord)