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.

168 lines
4.1 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. data WiredIn
  13. = WiType
  14. | WiPretype
  15. | WiInterval
  16. | WiI0
  17. | WiI1
  18. | WiIAnd
  19. | WiIOr
  20. | WiINot
  21. | WiPathP
  22. deriving (Eq, Show, Ord)
  23. data Term
  24. = Ref Name
  25. | App Plicity Term Term
  26. | Lam Plicity Text Term
  27. | Pi Plicity Text Term Term
  28. | Meta MV
  29. | Type
  30. | Typeω
  31. | Sigma Text Term Term
  32. | Pair Term Term
  33. | Proj1 Term
  34. | Proj2 Term
  35. | I
  36. | I0 | I1
  37. | IAnd Term Term
  38. | IOr Term Term
  39. | INot Term
  40. | PathP Term Term Term
  41. -- ^ PathP : (A : I -> Type) -> A i0 -> A i1 -> Type
  42. | IElim Term Term Term Term Term
  43. -- ^ IElim : {A : I -> Type} {x : A i0} {y : A i1} (p : PathP A x y) (i : I) -> A i
  44. | PathIntro Term Term
  45. -- ^ PathIntro : (A : I -> Type) (f : (i : I) -> A i) -> PathP A (f i0) (f i1)
  46. -- ~~~~~~~~~ not printed at all
  47. deriving (Eq, Show, Ord)
  48. data MV =
  49. MV { mvName :: Text
  50. , mvCell :: {-# UNPACK #-} !(IORef (Maybe Value))
  51. }
  52. instance Eq MV where
  53. (==) = (==) `on` mvName
  54. instance Ord MV where
  55. (<=) = (<=) `on` mvName
  56. instance Show MV where
  57. show = ('?':) . T.unpack . mvName
  58. data Name
  59. = Bound Text
  60. | Defined Text
  61. deriving (Eq, Show, Ord)
  62. type NFType = Value
  63. type NFEndp = Value
  64. data Value
  65. = VNe Head (Seq Projection)
  66. | VLam Plicity Closure
  67. | VPi Plicity Value Closure
  68. | VSigma Value Closure
  69. | VPair Value Value
  70. | VType | VTypeω
  71. | VI
  72. | VI0 | VI1
  73. | VIAnd Value Value
  74. | VIOr Value Value
  75. | VINot Value
  76. | VPath Value Value Value
  77. | VLine Value Value
  78. deriving (Eq, Show, Ord)
  79. pattern VVar :: Name -> Value
  80. pattern VVar x = VNe (HVar x) Seq.Empty
  81. quoteWith :: Set Text -> Value -> Term
  82. quoteWith names (VNe h sp) = foldl goSpine (goHead h) sp where
  83. goHead (HVar v) = Ref v
  84. goHead (HMeta m) = Meta m
  85. goSpine t (PApp p v) = App p t (quoteWith names v)
  86. goSpine t (PIElim l x y i) = IElim (quote l) (quote x) (quote y) t (quote i)
  87. goSpine t PProj1 = Proj1 t
  88. goSpine t PProj2 = Proj2 t
  89. quoteWith names (VLam p (Closure n k)) =
  90. let n' = refresh names n
  91. in Lam p n' (quoteWith (Set.insert n' names) (k (VVar (Bound n'))))
  92. quoteWith names (VPi p d (Closure n k)) =
  93. let n' = refresh names n
  94. in Pi p n' (quoteWith names d) (quoteWith (Set.insert n' names) (k (VVar (Bound n'))))
  95. quoteWith names (VSigma d (Closure n k)) =
  96. let n' = refresh names n
  97. in Sigma n' (quoteWith names d) (quoteWith (Set.insert n' names) (k (VVar (Bound n'))))
  98. quoteWith names (VPair a b) = Pair (quoteWith names a) (quoteWith names b)
  99. quoteWith _ VType = Type
  100. quoteWith _ VTypeω = Typeω
  101. quoteWith _ VI = I
  102. quoteWith _ VI0 = I0
  103. quoteWith _ VI1 = I1
  104. quoteWith names (VIAnd x y) = IAnd (quoteWith names x) (quoteWith names y)
  105. quoteWith names (VIOr x y) = IOr (quoteWith names x) (quoteWith names y)
  106. quoteWith names (VINot x) = INot (quoteWith names x)
  107. quoteWith names (VPath line x y) = PathP (quoteWith names line) (quoteWith names x) (quoteWith names y)
  108. quoteWith names (VLine p f) = PathIntro (quoteWith names p) (quoteWith names f)
  109. refresh :: Set Text -> Text -> Text
  110. refresh s n
  111. | T.singleton '_' == n = n
  112. | n `Set.notMember` s = n
  113. | otherwise = refresh s (n <> T.singleton '\'')
  114. quote :: Value -> Term
  115. quote = quoteWith mempty
  116. data Closure
  117. = Closure
  118. { clArgName :: Text
  119. , clCont :: Value -> Value
  120. }
  121. instance Show Closure where
  122. show (Closure n c) = "Closure \\" ++ show n ++ " -> " ++ show (c (VVar (Bound n)))
  123. instance Eq Closure where
  124. Closure _ k == Closure _ k' =
  125. k (VVar (Bound (T.pack "_"))) == k' (VVar (Bound (T.pack "_")))
  126. instance Ord Closure where
  127. Closure _ k <= Closure _ k' =
  128. k (VVar (Bound (T.pack "_"))) <= k' (VVar (Bound (T.pack "_")))
  129. data Head
  130. = HVar Name
  131. | HMeta MV
  132. deriving (Eq, Show, Ord)
  133. data Projection
  134. = PApp Plicity Value
  135. | PIElim Value Value Value NFEndp
  136. | PProj1
  137. | PProj2
  138. deriving (Eq, Show, Ord)