a type theory with equality based on setoids
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.

124 lines
2.7 KiB

  1. {-# LANGUAGE ViewPatterns #-}
  2. {-# LANGUAGE DeriveDataTypeable #-}
  3. {-# LANGUAGE StrictData, PatternSynonyms #-}
  4. module Value where
  5. import Data.Sequence (Seq)
  6. import Data.Text (Text)
  7. import Syntax
  8. import Data.Data
  9. import qualified Data.Sequence as Seq
  10. newtype Env =
  11. Env { locals :: Seq Value }
  12. deriving (Eq, Show, Ord, Data, Typeable)
  13. emptyEnv :: Env
  14. emptyEnv = Env mempty
  15. type VTy = Value
  16. data Closure
  17. = Closure !Env !Term
  18. | ClMeta MetaFun
  19. deriving (Eq, Ord, Data, Typeable)
  20. instance Show Closure where
  21. showsPrec x (Closure _ t) = showsPrec x t
  22. showsPrec x (ClMeta f) = showsPrec x f
  23. newtype MetaFun = MetaFun { runMC :: Value -> Value }
  24. instance Eq MetaFun where
  25. _ == _ = False
  26. instance Ord MetaFun where
  27. _ <= _ = True
  28. instance Show MetaFun where
  29. show _ = "«meta»"
  30. instance Data MetaFun where
  31. gunfold _ _ _ = error "gunfold MetaFun"
  32. toConstr _ = error "gunfold MetaFun"
  33. dataTypeOf _ = mkNoRepType "MetaFun"
  34. data Value
  35. -- Universes
  36. = VType
  37. -- Canonical Π-types and λ values
  38. | VPi Plicity Text ~Value {-# UNPACK #-} Closure
  39. | VLam Plicity Text {-# UNPACK #-} Closure
  40. -- Variable applied to some values, with a
  41. -- suspended evaluated result that might
  42. -- be forced later
  43. | VGlued Head (Seq SpineThing) ~(Maybe Value)
  44. -- Canonical Σ-types and pair values
  45. | VSigma Text ~Value {-# UNPACK #-} Closure
  46. | VPair Value Value
  47. -- Id A a b
  48. | VEq Value Value Value
  49. -- Id A a b ≡ t
  50. | VEqG Value Value Value Value
  51. | VTop | VUnit
  52. deriving (Eq, Show, Ord, Data, Typeable)
  53. data SpineThing
  54. = AppEx Value
  55. | AppIm Value
  56. | SProj1
  57. | SProj2
  58. deriving (Eq, Show, Ord, Data, Typeable)
  59. flexible :: Value -> Bool
  60. flexible VGlued{} = True
  61. flexible VEqG{} = True
  62. flexible _ = False
  63. pattern VNe :: Head -> Seq SpineThing -> Value
  64. pattern VNe x y = VGlued x y Nothing
  65. pattern VProj1 :: Value -> Value
  66. pattern VProj1 t <- (matchP1 -> Just t) where
  67. VProj1 t =
  68. case t of
  69. VGlued h s n -> VGlued h (s Seq.:|> SProj1) n
  70. matchP1 :: Value -> Maybe Value
  71. matchP1 (VPair x _) = Just x
  72. matchP1 (VGlued h (s Seq.:|> SProj1) n) = Just (VGlued h s n)
  73. matchP1 _ = Nothing
  74. pattern VProj2 :: Value -> Value
  75. pattern VProj2 t <- (matchP2 -> Just t) where
  76. VProj2 t =
  77. case t of
  78. VGlued h s n -> VGlued h (s Seq.:|> SProj2) n
  79. matchP2 :: Value -> Maybe Value
  80. matchP2 (VPair _ x) = Just x
  81. matchP2 (VGlued h (s Seq.:|> SProj2) n) = Just (VGlued h s n)
  82. matchP2 _ = Nothing
  83. data Meta
  84. = Unsolved [Text] Value
  85. | Solved Value
  86. deriving (Eq, Show)
  87. vVar :: Var -> Value
  88. vVar x = VGlued (HVar x) mempty Nothing
  89. data Head
  90. = HVar Var
  91. | HCon Text
  92. | HMeta MetaVar
  93. | HRefl
  94. | HCoe
  95. | HCong
  96. | HSym
  97. deriving (Eq, Show, Ord, Data, Typeable)