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.

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