Type.hs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. {-# LANGUAGE DeriveDataTypeable #-}
  2. {-# LANGUAGE BangPatterns #-}
  3. module Data.Adnot.Type (Value(..), Array, Product, isValidSymbol) where
  4. import Control.DeepSeq (NFData(..))
  5. import qualified Data.Char as C
  6. import Data.Data (Data)
  7. import Data.Typeable (Typeable)
  8. import Data.Map.Strict (Map)
  9. import qualified Data.Map as M
  10. import Data.Text (Text)
  11. import qualified Data.Text as T
  12. import Data.Vector (Vector)
  13. import GHC.Exts (IsString(..))
  14. -- | An Adnot value represented as a Haskell value
  15. data Value
  16. = Sum !Text !Array
  17. | Product !Product
  18. | List !Array
  19. | Integer !Integer
  20. | Double !Double
  21. | String !Text
  22. deriving (Eq, Show, Read, Typeable, Data)
  23. instance NFData Value where
  24. rnf (Sum t as) = rnf t `seq` rnf as
  25. rnf (Product ls) = rnf ls
  26. rnf (List as) = rnf as
  27. rnf (Integer i) = rnf i
  28. rnf (Double d) = rnf d
  29. rnf (String t) = rnf t
  30. instance IsString Value where
  31. fromString = String . fromString
  32. type Array = Vector Value
  33. type Product = Map Text Value
  34. isValidSymbol :: Text -> Bool
  35. isValidSymbol t = case T.uncons t of
  36. Nothing -> False
  37. Just (x, xs) -> C.isAlpha x && T.all C.isAlphaNum xs