Basic.hs 7.3 KB

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