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.

95 lines
2.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. 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. deriving (Eq, Show, Ord)
  33. type NFType = Value
  34. data Value
  35. = VNe Head [Projection]
  36. | VLam Plicity Closure
  37. | VPi Plicity Value Closure
  38. | VSigma Value Closure
  39. | VPair Value Value
  40. | VType
  41. deriving (Eq, Show, Ord)
  42. pattern VVar :: Name -> Value
  43. pattern VVar x = VNe (HVar x) []
  44. quote :: Value -> Term
  45. quote (VNe h sp) = foldl goSpine (goHead h) (reverse sp) where
  46. goHead (HVar v) = Ref v
  47. goHead (HMeta m) = Meta m
  48. goSpine t (PApp p v) = App p t (quote v)
  49. goSpine t PProj1 = Proj1 t
  50. goSpine t PProj2 = Proj2 t
  51. quote (VLam p (Closure n k)) = Lam p n (quote (k (VVar (Bound n))))
  52. quote (VPi p d (Closure n k)) = Pi p n (quote d) (quote (k (VVar (Bound n))))
  53. quote (VSigma d (Closure n k)) = Sigma n (quote d) (quote (k (VVar (Bound n))))
  54. quote (VPair a b) = Pair (quote a) (quote b)
  55. quote VType = Type
  56. data Closure
  57. = Closure
  58. { clArgName :: Text
  59. , clCont :: Value -> Value
  60. }
  61. instance Show Closure where
  62. show (Closure n c) = "Closure \\" ++ show n ++ " -> " ++ show (c (VVar (Bound n)))
  63. instance Eq Closure where
  64. Closure _ k == Closure _ k' =
  65. k (VVar (Bound (T.pack "_"))) == k' (VVar (Bound (T.pack "_")))
  66. instance Ord Closure where
  67. Closure _ k <= Closure _ k' =
  68. k (VVar (Bound (T.pack "_"))) <= k' (VVar (Bound (T.pack "_")))
  69. data Head
  70. = HVar Name
  71. | HMeta MV
  72. deriving (Eq, Show, Ord)
  73. data Projection
  74. = PApp Plicity Value
  75. | PProj1
  76. | PProj2
  77. deriving (Eq, Show, Ord)