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.

207 lines
5.2 KiB

  1. {-# LANGUAGE PatternSynonyms #-}
  2. module Syntax where
  3. import Data.Function (on)
  4. import Data.Text (Text)
  5. import Presyntax.Presyntax (Plicity(..))
  6. import qualified Data.Text as T
  7. import Data.IORef (IORef)
  8. import Data.Set (Set)
  9. import qualified Data.Set as Set
  10. import Data.Sequence (Seq)
  11. import qualified Data.Sequence as Seq
  12. import Data.Map.Strict (Map)
  13. import qualified Data.Map.Strict as Map
  14. data WiredIn
  15. = WiType
  16. | WiPretype
  17. | WiInterval
  18. | WiI0
  19. | WiI1
  20. | WiIAnd
  21. | WiIOr
  22. | WiINot
  23. | WiPathP
  24. | WiIsOne -- Proposition associated with an element of the interval
  25. | WiItIsOne -- 1 = 1
  26. | WiIsOne1 -- i j -> [i] -> [ior i j]
  27. | WiIsOne2 -- i j -> [j] -> [ior i j]
  28. | WiPartial -- (φ : I) -> Type -> Typeω
  29. | WiPartialP -- (φ : I) -> Partial r Type -> Typeω
  30. deriving (Eq, Show, Ord)
  31. data Term
  32. = Ref Name
  33. | App Plicity Term Term
  34. | Lam Plicity Text Term
  35. | Pi Plicity Text Term Term
  36. | Meta MV
  37. | Type
  38. | Typeω
  39. | Sigma Text Term Term
  40. | Pair Term Term
  41. | Proj1 Term
  42. | Proj2 Term
  43. | I
  44. | I0 | I1
  45. | IAnd Term Term
  46. | IOr Term Term
  47. | INot Term
  48. | PathP Term Term Term
  49. -- ^ PathP : (A : I -> Type) -> A i0 -> A i1 -> Type
  50. | IElim Term Term Term Term Term
  51. -- ^ IElim : {A : I -> Type} {x : A i0} {y : A i1} (p : PathP A x y) (i : I) -> A i
  52. | PathIntro Term Term
  53. -- ^ PathIntro : (A : I -> Type) (f : (i : I) -> A i) -> PathP A (f i0) (f i1)
  54. -- ~~~~~~~~~ not printed at all
  55. | IsOne Term
  56. | IsOne1 Term
  57. | IsOne2 Term
  58. | ItIsOne
  59. | Partial Term Term
  60. | PartialP Term Term
  61. | System (Map Term Term)
  62. deriving (Eq, Show, Ord)
  63. data MV =
  64. MV { mvName :: Text
  65. , mvCell :: {-# UNPACK #-} !(IORef (Maybe Value))
  66. }
  67. instance Eq MV where
  68. (==) = (==) `on` mvName
  69. instance Ord MV where
  70. (<=) = (<=) `on` mvName
  71. instance Show MV where
  72. show = ('?':) . T.unpack . mvName
  73. data Name
  74. = Bound Text
  75. | Defined Text
  76. deriving (Eq, Show, Ord)
  77. type NFType = Value
  78. type NFEndp = Value
  79. data Value
  80. = VNe Head (Seq Projection)
  81. | VLam Plicity Closure
  82. | VPi Plicity Value Closure
  83. | VSigma Value Closure
  84. | VPair Value Value
  85. | VType | VTypeω
  86. | VI
  87. | VI0 | VI1
  88. | VIAnd Value Value
  89. | VIOr Value Value
  90. | VINot Value
  91. | VPath Value Value Value
  92. | VLine Value Value
  93. | VIsOne Value
  94. | VItIsOne
  95. | VIsOne1 Value
  96. | VIsOne2 Value
  97. | VPartial NFEndp Value
  98. | VPartialP NFEndp Value
  99. | VSystem (Map Value Value)
  100. deriving (Eq, Show, Ord)
  101. pattern VVar :: Name -> Value
  102. pattern VVar x = VNe (HVar x) Seq.Empty
  103. quoteWith :: Set Text -> Value -> Term
  104. quoteWith names (VNe h sp) = foldl goSpine (goHead h) sp where
  105. goHead (HVar v) = Ref v
  106. goHead (HMeta m) = Meta m
  107. goSpine t (PApp p v) = App p t (quoteWith names v)
  108. goSpine t (PIElim l x y i) = IElim (quote l) (quote x) (quote y) t (quote i)
  109. goSpine t PProj1 = Proj1 t
  110. goSpine t PProj2 = Proj2 t
  111. quoteWith names (VLam p (Closure n k)) =
  112. let n' = refresh names n
  113. in Lam p n' (quoteWith (Set.insert n' names) (k (VVar (Bound n'))))
  114. quoteWith names (VPi p d (Closure n k)) =
  115. let n' = refresh names n
  116. in Pi p n' (quoteWith names d) (quoteWith (Set.insert n' names) (k (VVar (Bound n'))))
  117. quoteWith names (VSigma d (Closure n k)) =
  118. let n' = refresh names n
  119. in Sigma n' (quoteWith names d) (quoteWith (Set.insert n' names) (k (VVar (Bound n'))))
  120. quoteWith names (VPair a b) = Pair (quoteWith names a) (quoteWith names b)
  121. quoteWith _ VType = Type
  122. quoteWith _ VTypeω = Typeω
  123. quoteWith _ VI = I
  124. quoteWith _ VI0 = I0
  125. quoteWith _ VI1 = I1
  126. quoteWith names (VIAnd x y) = IAnd (quoteWith names x) (quoteWith names y)
  127. quoteWith names (VIOr x y) = IOr (quoteWith names x) (quoteWith names y)
  128. quoteWith names (VINot x) = INot (quoteWith names x)
  129. quoteWith names (VPath line x y) = PathP (quoteWith names line) (quoteWith names x) (quoteWith names y)
  130. quoteWith names (VLine p f) = PathIntro (quoteWith names p) (quoteWith names f)
  131. quoteWith names (VIsOne v) = IsOne (quoteWith names v)
  132. quoteWith names (VIsOne1 v) = IsOne1 (quoteWith names v)
  133. quoteWith names (VIsOne2 v) = IsOne2 (quoteWith names v)
  134. quoteWith _ VItIsOne = ItIsOne
  135. quoteWith names (VPartial x y) = Partial (quoteWith names x) (quoteWith names y)
  136. quoteWith names (VPartialP x y) = Partial (quoteWith names x) (quoteWith names y)
  137. quoteWith names (VSystem fs) = System (Map.fromList (map (\(x, y) -> (quoteWith names x, quoteWith names y)) (Map.toList fs)))
  138. refresh :: Set Text -> Text -> Text
  139. refresh s n
  140. | T.singleton '_' == n = n
  141. | n `Set.notMember` s = n
  142. | otherwise = refresh s (n <> T.singleton '\'')
  143. quote :: Value -> Term
  144. quote = quoteWith mempty
  145. data Closure
  146. = Closure
  147. { clArgName :: Text
  148. , clCont :: Value -> Value
  149. }
  150. instance Show Closure where
  151. show (Closure n c) = "Closure \\" ++ show n ++ " -> " ++ show (c (VVar (Bound n)))
  152. instance Eq Closure where
  153. Closure _ k == Closure _ k' =
  154. k (VVar (Bound (T.pack "_"))) == k' (VVar (Bound (T.pack "_")))
  155. instance Ord Closure where
  156. Closure _ k <= Closure _ k' =
  157. k (VVar (Bound (T.pack "_"))) <= k' (VVar (Bound (T.pack "_")))
  158. data Head
  159. = HVar Name
  160. | HMeta MV
  161. deriving (Eq, Show, Ord)
  162. data Projection
  163. = PApp Plicity Value
  164. | PIElim Value Value Value NFEndp
  165. | PProj1
  166. | PProj2
  167. deriving (Eq, Show, Ord)