Class.hs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. {-# LANGUAGE OverloadedStrings #-}
  2. {-# LANGUAGE OverloadedLists #-}
  3. {-# LANGUAGE TypeSynonymInstances #-}
  4. {-# LANGUAGE FlexibleInstances #-}
  5. {-# LANGUAGE FlexibleContexts #-}
  6. {-# LANGUAGE TypeOperators #-}
  7. {-# LANGUAGE ScopedTypeVariables #-}
  8. {-# LANGUAGE DefaultSignatures #-}
  9. {-# LANGUAGE DataKinds #-}
  10. module Data.Adnot.Class where
  11. import Data.Adnot.Type
  12. import Data.Adnot.Emit
  13. import Data.Int
  14. import Data.Word
  15. import qualified Data.ByteString.Lazy as BSL
  16. import qualified Data.Foldable as F
  17. import qualified Data.List.NonEmpty as NE
  18. import qualified Data.Map.Lazy as ML
  19. import qualified Data.Map.Strict as MS
  20. import qualified Data.Sequence as Seq
  21. import qualified Data.Text as T
  22. import qualified Data.Text.Lazy as TL
  23. import qualified Data.Vector as V
  24. import GHC.Generics
  25. import GHC.TypeLits (KnownSymbol)
  26. encode :: ToAdnot a => a -> BSL.ByteString
  27. encode = encodeValue . toAdnot
  28. class GenToAdnot f where
  29. genToAdnot :: f p -> Value
  30. instance (GenToAdnot l, GenToAdnot r) => GenToAdnot (l :+: r) where
  31. genToAdnot (L1 x) = genToAdnot x
  32. genToAdnot (R1 y) = genToAdnot y
  33. instance ToAdnot x => GenToAdnot (K1 i x) where
  34. genToAdnot (K1 x) = toAdnot x
  35. instance (GatherSequence f, Constructor name) => GenToAdnot (C1 name f) where
  36. genToAdnot (M1 x) = Sum (T.pack (conName c)) (V.fromList (gatherSequence x))
  37. where c :: M1 c name f ()
  38. c = undefined
  39. instance (GenToAdnot f) => GenToAdnot (S1 name f) where
  40. genToAdnot (M1 x) = genToAdnot x
  41. instance (GenToAdnot f) => GenToAdnot (D1 name f) where
  42. genToAdnot (M1 x) = genToAdnot x
  43. instance (GatherSequence l, GatherSequence r) => GenToAdnot (l :*: r) where
  44. genToAdnot x = List (V.fromList (gatherSequence x))
  45. class GatherRecord f where
  46. gatherRecord :: f p -> [(T.Text, Value)]
  47. instance (GatherRecord l, GatherRecord r) => GatherRecord (l :*: r) where
  48. gatherRecord (x :*: y) = gatherRecord x ++ gatherRecord y
  49. instance (GenToAdnot f, Selector name) => GatherRecord (S1 name f) where
  50. gatherRecord (M1 x) = [(T.pack (selName s), genToAdnot x)]
  51. where s :: S1 name f ()
  52. s = undefined
  53. instance GatherRecord U1 where
  54. gatherRecord U1 = []
  55. class GatherSequence f where
  56. gatherSequence :: f p -> [Value]
  57. instance GatherSequence U1 where
  58. gatherSequence U1 = []
  59. instance (GatherSequence l, GatherSequence r) => GatherSequence (l :*: r) where
  60. gatherSequence (x :*: y) = gatherSequence x ++ gatherSequence y
  61. instance ToAdnot x => GatherSequence (K1 i x) where
  62. gatherSequence (K1 x) = [toAdnot x]
  63. instance GenToAdnot f => GatherSequence (S1 name f) where
  64. gatherSequence (M1 x) = [genToAdnot x]
  65. instance GenToAdnot f => GatherSequence (D1 name f) where
  66. gatherSequence (M1 x) = [genToAdnot x]
  67. instance GenToAdnot U1 where
  68. genToAdnot U1 = List []
  69. genericToAdnot :: (GenToAdnot (Rep t), Generic t) => t -> Value
  70. genericToAdnot x = genToAdnot (from x)
  71. class ToAdnot a where
  72. toAdnot :: a -> Value
  73. default toAdnot :: (Generic a, GenToAdnot (Rep a)) => a -> Value
  74. toAdnot = genericToAdnot
  75. -- * Integral types
  76. instance ToAdnot Int where
  77. toAdnot n = Integer (fromIntegral n)
  78. instance ToAdnot Integer where
  79. toAdnot n = Integer n
  80. instance ToAdnot Int8 where
  81. toAdnot n = Integer (fromIntegral n)
  82. instance ToAdnot Int16 where
  83. toAdnot n = Integer (fromIntegral n)
  84. instance ToAdnot Int32 where
  85. toAdnot n = Integer (fromIntegral n)
  86. instance ToAdnot Int64 where
  87. toAdnot n = Integer (fromIntegral n)
  88. instance ToAdnot Word where
  89. toAdnot n = Integer (fromIntegral n)
  90. instance ToAdnot Word8 where
  91. toAdnot n = Integer (fromIntegral n)
  92. instance ToAdnot Word16 where
  93. toAdnot n = Integer (fromIntegral n)
  94. instance ToAdnot Word32 where
  95. toAdnot n = Integer (fromIntegral n)
  96. instance ToAdnot Word64 where
  97. toAdnot n = Integer (fromIntegral n)
  98. -- * Rational/Floating types
  99. instance ToAdnot Double where
  100. toAdnot d = Double d
  101. instance ToAdnot Float where
  102. toAdnot d = Double (fromRational (toRational d))
  103. -- * String types
  104. instance {-# INCOHERENT #-} ToAdnot String where
  105. toAdnot s = String (T.pack s)
  106. instance ToAdnot T.Text where
  107. toAdnot s = String s
  108. instance ToAdnot TL.Text where
  109. toAdnot s = String (TL.toStrict s)
  110. instance ToAdnot Char where
  111. toAdnot c = String (T.singleton c)
  112. -- * List types
  113. instance ToAdnot a => ToAdnot [a] where
  114. toAdnot ls = List (fmap toAdnot (V.fromList ls))
  115. instance ToAdnot a => ToAdnot (V.Vector a) where
  116. toAdnot ls = List (fmap toAdnot ls)
  117. instance ToAdnot a => ToAdnot (Seq.Seq a) where
  118. toAdnot ls = List (V.fromList (F.toList (fmap toAdnot ls)))
  119. instance ToAdnot a => ToAdnot (NE.NonEmpty a) where
  120. toAdnot ls = List (V.fromList (F.toList (fmap toAdnot ls)))
  121. -- * Mapping types
  122. instance ToAdnot a => ToAdnot (MS.Map T.Text a) where
  123. toAdnot ls = Product (fmap toAdnot ls)
  124. -- * Tuples
  125. instance ToAdnot () where
  126. toAdnot () = List []
  127. instance (ToAdnot a, ToAdnot b) => ToAdnot (a, b) where
  128. toAdnot (a, b) = List [toAdnot a, toAdnot b]
  129. instance (ToAdnot a, ToAdnot b, ToAdnot c) => ToAdnot (a, b, c) where
  130. toAdnot (a, b, c) = List [toAdnot a, toAdnot b, toAdnot c]
  131. instance (ToAdnot a, ToAdnot b, ToAdnot c, ToAdnot d)
  132. => ToAdnot (a, b, c, d) where
  133. toAdnot (a, b, c, d) = List [toAdnot a, toAdnot b, toAdnot c, toAdnot d]
  134. -- Some common Haskell algebraic data types
  135. instance ToAdnot a => ToAdnot (Maybe a) where
  136. toAdnot Nothing = Sum "Nothing" []
  137. toAdnot (Just x) = Sum "Just" [toAdnot x]
  138. instance (ToAdnot a, ToAdnot b) => ToAdnot (Either a b) where
  139. toAdnot (Left x) = Sum "Left" [toAdnot x]
  140. toAdnot (Right y) = Sum "Right" [toAdnot y]
  141. instance ToAdnot Bool where
  142. toAdnot True = Symbol "True"
  143. toAdnot False = Symbol "False"
  144. -- * Parsing
  145. type ParseError = String
  146. type Parser a = Either ParseError a
  147. withSum :: String -> (T.Text -> Array -> Parser a) -> Value -> Parser a
  148. withSum n k val = case val of
  149. Sum t as -> k t as
  150. _ -> Left ("Expected sum in " ++ n)
  151. class FromAdnot a where
  152. parseAdnot :: Value -> Parser a