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.

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