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.

101 lines
2.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. data Term
  9. = Ref Name
  10. | App Plicity Term Term
  11. | Lam Plicity Text Term
  12. | Pi Plicity Text Term Term
  13. | Meta MV
  14. | Type
  15. | Sigma Text Term Term
  16. | Pair Term Term
  17. | Proj1 Term
  18. | Proj2 Term
  19. deriving (Eq, Show, Ord)
  20. data MV =
  21. MV { mvName :: Text
  22. , mvCell :: {-# UNPACK #-} !(IORef (Maybe Value))
  23. }
  24. instance Eq MV where
  25. (==) = (==) `on` mvName
  26. instance Ord MV where
  27. (<=) = (<=) `on` mvName
  28. instance Show MV where
  29. show = ('?':) . T.unpack . mvName
  30. data Name
  31. = Bound Text
  32. | Defined Text
  33. | Builtin Text WiredIn
  34. deriving (Eq, Show, Ord)
  35. data WiredIn
  36. = WiType
  37. deriving (Eq, Show, Ord)
  38. type NFType = Value
  39. data Value
  40. = VNe Head [Projection]
  41. | VLam Plicity Closure
  42. | VPi Plicity Value Closure
  43. | VSigma Value Closure
  44. | VPair Value Value
  45. | VType
  46. deriving (Eq, Show, Ord)
  47. pattern VVar :: Name -> Value
  48. pattern VVar x = VNe (HVar x) []
  49. quote :: Value -> Term
  50. quote (VNe h sp) = foldl goSpine (goHead h) (reverse sp) where
  51. goHead (HVar v) = Ref v
  52. goHead (HMeta m) = Meta m
  53. goSpine t (PApp p v) = App p t (quote v)
  54. goSpine t PProj1 = Proj1 t
  55. goSpine t PProj2 = Proj2 t
  56. quote (VLam p (Closure n k)) = Lam p n (quote (k (VVar (Bound n))))
  57. quote (VPi p d (Closure n k)) = Pi p n (quote d) (quote (k (VVar (Bound n))))
  58. quote (VSigma d (Closure n k)) = Sigma n (quote d) (quote (k (VVar (Bound n))))
  59. quote (VPair a b) = Pair (quote a) (quote b)
  60. quote VType = Type
  61. data Closure
  62. = Closure
  63. { clArgName :: Text
  64. , clCont :: Value -> Value
  65. }
  66. instance Show Closure where
  67. show (Closure n c) = "Closure \\" ++ show n ++ " -> " ++ show (c (VVar (Bound n)))
  68. instance Eq Closure where
  69. Closure _ k == Closure _ k' =
  70. k (VVar (Bound (T.pack "_"))) == k' (VVar (Bound (T.pack "_")))
  71. instance Ord Closure where
  72. Closure _ k <= Closure _ k' =
  73. k (VVar (Bound (T.pack "_"))) <= k' (VVar (Bound (T.pack "_")))
  74. data Head
  75. = HVar Name
  76. | HMeta MV
  77. deriving (Eq, Show, Ord)
  78. data Projection
  79. = PApp Plicity Value
  80. | PProj1
  81. | PProj2
  82. deriving (Eq, Show, Ord)