Basic.hs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. gatherDList :: SExpr a -> Maybe ([SExpr a], a)
  69. gatherDList SNil = Nothing
  70. gatherDList SAtom {} = Nothing
  71. gatherDList sx = go sx
  72. where go SNil = Nothing
  73. go (SAtom a) = return ([], a)
  74. go (SCons x xs) = do
  75. (ys, a) <- go xs
  76. return (x:ys, a)
  77. infixr 5 :::
  78. -- | A shorter infix alias for `SCons`
  79. --
  80. -- >>> A "pachy" ::: A "derm"
  81. -- SCons (SAtom "pachy") (SAtom "derm")
  82. pattern (:::) :: SExpr a -> SExpr a -> SExpr a
  83. pattern x ::: xs = SCons x xs
  84. -- | A shorter alias for `SAtom`
  85. --
  86. -- >>> A "elephant"
  87. -- SAtom "elephant"
  88. pattern A :: a -> SExpr a
  89. pattern A x = SAtom x
  90. -- | A (slightly) shorter alias for `SNil`
  91. --
  92. -- >>> Nil
  93. -- SNil
  94. pattern Nil :: SExpr a
  95. pattern Nil = SNil
  96. -- | An alias for matching a proper list.
  97. --
  98. -- >>> L [A "pachy", A "derm"]
  99. -- SExpr (SAtom "pachy") (SExpr (SAtom "derm") SNil)
  100. pattern L :: [SExpr a] -> SExpr a
  101. pattern L xs <- (gatherList -> Right xs)
  102. #if MIN_VERSION_base(4,8,0)
  103. where L [] = SNil
  104. L (x:xs) = SCons x (L xs)
  105. #endif
  106. -- | An alias for matching a dotted list.
  107. --
  108. -- >>> DL [A "pachy"] A "derm"
  109. -- SExpr (SAtom "pachy") (SAtom "derm")
  110. pattern DL :: [SExpr a] -> a -> SExpr a
  111. pattern DL xs x <- (gatherDList -> Just (xs, x))
  112. #if MIN_VERSION_base(4,8,0)
  113. where DL [] a = SAtom a
  114. DL (x:xs) a = SCons x (DL xs a)
  115. #endif
  116. getShape :: SExpr a -> String
  117. getShape Nil = "empty list"
  118. getShape sx = go (0 :: Int) sx
  119. where go n SNil = "list of length " ++ show n
  120. go n SAtom {} = "dotted list of length " ++ show n
  121. go n (SCons _ xs) = go (n+1) xs
  122. -- | Utility function for parsing a pair of things.
  123. --
  124. -- >>> fromPair (isAtom "pachy") (asAtom return) (A "pachy" ::: A "derm" ::: Nil)
  125. -- Right ((), "derm")
  126. -- >>> fromPair (isAtom "pachy") fromAtom (A "pachy" ::: Nil)
  127. -- Left "Expected two-element list"
  128. fromPair :: (SExpr t -> Either String a)
  129. -> (SExpr t -> Either String b)
  130. -> SExpr t -> Either String (a, b)
  131. fromPair pl pr (l ::: r ::: Nil) = (,) <$> pl l <*> pr r
  132. fromPair _ _ sx = Left ("fromPair: expected two-element list; found " ++ getShape sx)
  133. -- | Utility function for parsing a list of things.
  134. fromList :: (SExpr t -> Either String a) -> SExpr t -> Either String [a]
  135. fromList p (s ::: ss) = (:) <$> p s <*> fromList p ss
  136. fromList _ Nil = pure []
  137. fromList _ sx = Left ("fromList: expected list; found " ++ getShape sx)
  138. -- | Utility function for parsing a single atom
  139. fromAtom :: SExpr t -> Either String t
  140. fromAtom (A a) = return a
  141. fromAtom sx = Left ("fromAtom: expected atom; found list" ++ getShape sx)
  142. gatherList :: SExpr t -> Either String [SExpr t]
  143. gatherList (x ::: xs) = (:) <$> pure x <*> gatherList xs
  144. gatherList Nil = pure []
  145. gatherList sx = Left ("gatherList: expected list; found " ++ getShape sx)
  146. -- | Parse a two-element list (NOT a dotted pair) using the
  147. -- provided function.
  148. --
  149. -- >>> let go (A l) (A r) = return (l ++ r); go _ _ = Left "expected atoms"
  150. -- >>> asPair go (A "pachy" ::: A "derm" ::: Nil)
  151. -- Right "pachyderm"
  152. -- >>> asPair go (A "elephant" ::: Nil)
  153. -- Left "asPair: expected two-element list; found list of length 1"
  154. asPair :: ((SExpr t, SExpr t) -> Either String a)
  155. -> SExpr t -> Either String a
  156. asPair f (l ::: r ::: SNil) = f (l, r)
  157. asPair _ sx = Left ("asPair: expected two-element list; found " ++ getShape sx)
  158. -- | Parse an arbitrary-length list using the provided function.
  159. --
  160. -- >>> let go xs = concat <$> mapM fromAtom xs
  161. -- >>> asList go (A "el" ::: A "eph" ::: A "ant" ::: Nil)
  162. -- Right "elephant"
  163. -- >>> asList go (A "el" ::: A "eph" ::: A "ant")
  164. -- Left "asList: expected list; found dotted list of length 3"
  165. asList :: ([SExpr t] -> Either String a) -> SExpr t -> Either String a
  166. asList f ls = gatherList ls >>= f
  167. -- | Match a given literal atom, failing otherwise.
  168. --
  169. -- >>> isAtom "elephant" (A "elephant")
  170. -- Right ()
  171. -- >>> isAtom "elephant" (A "elephant" ::: Nil)
  172. -- Left "isAtom: expected atom; found list"
  173. isAtom :: Eq t => t -> SExpr t -> Either String ()
  174. isAtom s (A s')
  175. | s == s' = return ()
  176. | otherwise = Left "isAtom: failed to match atom"
  177. isAtom _ sx = Left ("isAtom: expected atom; found " ++ getShape sx)
  178. -- | Parse an atom using the provided function.
  179. --
  180. -- >>> import Data.Char (toUpper)
  181. -- >>> asAtom (return . map toUpper) (A "elephant")
  182. -- Right "ELEPHANT"
  183. -- >>> asAtom (return . map toUpper) Nil
  184. -- Left "asAtom: expected atom; found empty list"
  185. asAtom :: (t -> Either String a) -> SExpr t -> Either String a
  186. asAtom f (A s) = f s
  187. asAtom _ sx = Left ("asAtom: expected atom; found " ++ getShape sx)
  188. -- | Parse an assoc-list using the provided function.
  189. --
  190. -- >>> let def (x, y) = do { a <- fromAtom x; b <- fromAtom y; return (a ++ ": " ++ b) }
  191. -- >>> let defList xs = do { defs <- mapM def xs; return (unlines defs) }
  192. -- >>> asAssoc defList ((A "legs" ::: A "four" ::: Nil) ::: (A "trunk" ::: A "one" ::: Nil) ::: Nil)
  193. -- Right "legs: four\ntrunk: one\n"
  194. -- >>> asAssoc defList ((A "legs" ::: A "four" ::: Nil) ::: (A "elephant") ::: Nil)
  195. -- Left "asAssoc: expected pair; found list of length 1"
  196. asAssoc :: ([(SExpr t, SExpr t)] -> Either String a)
  197. -> SExpr t -> Either String a
  198. asAssoc f ss = gatherList ss >>= mapM go >>= f
  199. where go (a ::: b ::: Nil) = return (a, b)
  200. go sx = Left ("asAssoc: expected pair; found " ++ getShape sx)