Basic.hs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. {-# LANGUAGE PatternSynonyms #-}
  2. {-# LANGUAGE ScopedTypeVariables #-}
  3. {-# LANGUAGE ViewPatterns #-}
  4. module Data.SCargot.Repr.Basic
  5. ( -- * Basic 'SExpr' representation
  6. R.SExpr(..)
  7. -- * Constructing and Deconstructing
  8. , cons
  9. , uncons
  10. -- * Shorthand Patterns
  11. , pattern (:::)
  12. , pattern A
  13. , pattern L
  14. , pattern DL
  15. , pattern Nil
  16. -- * Lenses
  17. , _car
  18. , _cdr
  19. -- * Useful processing functions
  20. , fromPair
  21. , fromList
  22. , fromAtom
  23. , asPair
  24. , asList
  25. , isAtom
  26. , asAtom
  27. , asAssoc
  28. ) where
  29. #if !MIN_VERSION_base(4,8,0)
  30. import Control.Applicative (Applicative, (<$>), (<*>), pure)
  31. #endif
  32. import Data.SCargot.Repr as R
  33. -- | A traversal with access to the first element of a pair.
  34. --
  35. -- >>> import Lens.Family
  36. -- >>> set _car (A "elephant") (A "one" ::: A "two" ::: A "three" ::: Nil)
  37. -- A "elelphant" ::: A "two" ::: A "three" ::: Nil
  38. -- >>> set _car (A "two" ::: A "three" ::: Nil) (A "one" ::: A "elephant")
  39. -- (A "two" ::: A "three" ::: Nil) ::: A "elephant"
  40. _car :: Applicative f => (SExpr a -> f (SExpr a)) -> SExpr a -> f (SExpr a)
  41. _car f (SCons x xs) = (:::) <$> f x <*> pure xs
  42. _car _ (SAtom a) = pure (A a)
  43. _car _ SNil = pure SNil
  44. -- | A traversal with access to the second element of a pair.
  45. --
  46. -- >>> import Lens.Family
  47. -- >>> set _cdr (A "elephant") (A "one" ::: A "two" ::: A "three" ::: Nil)
  48. -- A "one" ::: A "elephant"
  49. -- >>> set _cdr (A "two" ::: A "three" ::: Nil) (A "one" ::: A "elephant")
  50. -- A "one" ::: A "two" ::: A "three" ::: Nil
  51. _cdr :: Applicative f => (SExpr a -> f (SExpr a)) -> SExpr a -> f (SExpr a)
  52. _cdr f (SCons x xs) = (:::) <$> pure x <*> f xs
  53. _cdr _ (SAtom a) = pure (A a)
  54. _cdr _ SNil = pure Nil
  55. -- | Produce the head and tail of the s-expression (if possible).
  56. --
  57. -- >>> uncons (A "el" ::: A "eph" ::: A "ant" ::: Nil)
  58. -- Just (A "el",SCons (SAtom "eph") (SCons (SAtom "ant") SNil))
  59. uncons :: SExpr a -> Maybe (SExpr a, SExpr a)
  60. uncons (SCons x xs) = Just (x, xs)
  61. uncons _ = Nothing
  62. -- | Combine the two s-expressions into a new one.
  63. --
  64. -- >>> cons (A "el") (L ["eph", A "ant"])
  65. -- SCons (SAtom "el) (SCons (SAtom "eph") (SCons (SAtom "ant") SNil))
  66. cons :: SExpr a -> SExpr a -> SExpr a
  67. cons = SCons
  68. mkList :: [SExpr a] -> SExpr a
  69. mkList [] = SNil
  70. mkList (x:xs) = SCons x (mkList xs)
  71. mkDList :: [SExpr a] -> a -> SExpr a
  72. mkDList [] a = SAtom a
  73. mkDList (x:xs) a = SCons x (mkDList xs a)
  74. gatherDList :: SExpr a -> Maybe ([SExpr a], a)
  75. gatherDList SNil = Nothing
  76. gatherDList SAtom {} = Nothing
  77. gatherDList sx = go sx
  78. where go SNil = Nothing
  79. go (SAtom a) = return ([], a)
  80. go (SCons x xs) = do
  81. (ys, a) <- go xs
  82. return (x:ys, a)
  83. infixr 5 :::
  84. -- | A shorter infix alias for `SCons`
  85. --
  86. -- >>> A "pachy" ::: A "derm"
  87. -- SCons (SAtom "pachy") (SAtom "derm")
  88. pattern x ::: xs = SCons x xs
  89. -- | A shorter alias for `SAtom`
  90. --
  91. -- >>> A "elephant"
  92. -- SAtom "elephant"
  93. pattern A x = SAtom x
  94. -- | A (slightly) shorter alias for `SNil`
  95. --
  96. -- >>> Nil
  97. -- SNil
  98. pattern Nil = SNil
  99. -- | An alias for matching a proper list.
  100. --
  101. -- >>> L [A "pachy", A "derm"]
  102. -- SCons (SAtom "pachy") (SCons (SAtom "derm") SNil)
  103. pattern L xs <- (gatherList -> Right xs)
  104. #if MIN_VERSION_base(4,8,0)
  105. where L xs = mkList xs
  106. #endif
  107. -- | An alias for matching a dotted list.
  108. --
  109. -- >>> DL [A "pachy"] A "derm"
  110. -- SCons (SAtom "pachy") (SAtom "derm")
  111. pattern DL xs x <- (gatherDList -> Just (xs, x))
  112. #if MIN_VERSION_base(4,8,0)
  113. where DL xs x = mkDList xs x
  114. #endif
  115. getShape :: SExpr a -> String
  116. getShape Nil = "empty list"
  117. getShape sx = go (0 :: Int) sx
  118. where go n SNil = "list of length " ++ show n
  119. go n SAtom {} = "dotted list of length " ++ show n
  120. go n (SCons _ xs) = go (n+1) xs
  121. -- | Utility function for parsing a pair of things.
  122. --
  123. -- >>> fromPair (isAtom "pachy") (asAtom return) (A "pachy" ::: A "derm" ::: Nil)
  124. -- Right ((), "derm")
  125. -- >>> fromPair (isAtom "pachy") fromAtom (A "pachy" ::: Nil)
  126. -- Left "Expected two-element list"
  127. fromPair :: (SExpr t -> Either String a)
  128. -> (SExpr t -> Either String b)
  129. -> SExpr t -> Either String (a, b)
  130. fromPair pl pr (l ::: r ::: Nil) = (,) <$> pl l <*> pr r
  131. fromPair _ _ sx = Left ("fromPair: expected two-element list; found " ++ getShape sx)
  132. -- | Utility function for parsing a list of things.
  133. fromList :: (SExpr t -> Either String a) -> SExpr t -> Either String [a]
  134. fromList p (s ::: ss) = (:) <$> p s <*> fromList p ss
  135. fromList _ Nil = pure []
  136. fromList _ sx = Left ("fromList: expected list; found " ++ getShape sx)
  137. -- | Utility function for parsing a single atom
  138. fromAtom :: SExpr t -> Either String t
  139. fromAtom (A a) = return a
  140. fromAtom sx = Left ("fromAtom: expected atom; found list" ++ getShape sx)
  141. gatherList :: SExpr t -> Either String [SExpr t]
  142. gatherList (x ::: xs) = (:) <$> pure x <*> gatherList xs
  143. gatherList Nil = pure []
  144. gatherList sx = Left ("gatherList: expected list; found " ++ getShape sx)
  145. -- | Parse a two-element list (NOT a dotted pair) using the
  146. -- provided function.
  147. --
  148. -- >>> let go (A l) (A r) = return (l ++ r); go _ _ = Left "expected atoms"
  149. -- >>> asPair go (A "pachy" ::: A "derm" ::: Nil)
  150. -- Right "pachyderm"
  151. -- >>> asPair go (A "elephant" ::: Nil)
  152. -- Left "asPair: expected two-element list; found list of length 1"
  153. asPair :: ((SExpr t, SExpr t) -> Either String a)
  154. -> SExpr t -> Either String a
  155. asPair f (l ::: r ::: SNil) = f (l, r)
  156. asPair _ sx = Left ("asPair: expected two-element list; found " ++ getShape sx)
  157. -- | Parse an arbitrary-length list using the provided function.
  158. --
  159. -- >>> let go xs = concat <$> mapM fromAtom xs
  160. -- >>> asList go (A "el" ::: A "eph" ::: A "ant" ::: Nil)
  161. -- Right "elephant"
  162. -- >>> asList go (A "el" ::: A "eph" ::: A "ant")
  163. -- Left "asList: expected list; found dotted list of length 3"
  164. asList :: ([SExpr t] -> Either String a) -> SExpr t -> Either String a
  165. asList f ls = gatherList ls >>= f
  166. -- | Match a given literal atom, failing otherwise.
  167. --
  168. -- >>> isAtom "elephant" (A "elephant")
  169. -- Right ()
  170. -- >>> isAtom "elephant" (A "elephant" ::: Nil)
  171. -- Left "isAtom: expected atom; found list"
  172. isAtom :: Eq t => t -> SExpr t -> Either String ()
  173. isAtom s (A s')
  174. | s == s' = return ()
  175. | otherwise = Left "isAtom: failed to match atom"
  176. isAtom _ sx = Left ("isAtom: expected atom; found " ++ getShape sx)
  177. -- | Parse an atom using the provided function.
  178. --
  179. -- >>> import Data.Char (toUpper)
  180. -- >>> asAtom (return . map toUpper) (A "elephant")
  181. -- Right "ELEPHANT"
  182. -- >>> asAtom (return . map toUpper) Nil
  183. -- Left "asAtom: expected atom; found empty list"
  184. asAtom :: (t -> Either String a) -> SExpr t -> Either String a
  185. asAtom f (A s) = f s
  186. asAtom _ sx = Left ("asAtom: expected atom; found " ++ getShape sx)
  187. -- | Parse an assoc-list using the provided function.
  188. --
  189. -- >>> let def (x, y) = do { a <- fromAtom x; b <- fromAtom y; return (a ++ ": " ++ b) }
  190. -- >>> let defList xs = do { defs <- mapM def xs; return (unlines defs) }
  191. -- >>> asAssoc defList ((A "legs" ::: A "four" ::: Nil) ::: (A "trunk" ::: A "one" ::: Nil) ::: Nil)
  192. -- Right "legs: four\ntrunk: one\n"
  193. -- >>> asAssoc defList ((A "legs" ::: A "four" ::: Nil) ::: (A "elephant") ::: Nil)
  194. -- Left "asAssoc: expected pair; found list of length 1"
  195. asAssoc :: ([(SExpr t, SExpr t)] -> Either String a)
  196. -> SExpr t -> Either String a
  197. asAssoc f ss = gatherList ss >>= mapM go >>= f
  198. where go (a ::: b ::: Nil) = return (a, b)
  199. go sx = Left ("asAssoc: expected pair; found " ++ getShape sx)