| 1 |
{-# LANGUAGE PatternSynonyms #-} |
| 2 |
{-# LANGUAGE ViewPatterns #-} |
| 3 |
|
| 4 |
module Data.SCargot.Repr.Rich |
| 5 |
( -- * 'RichSExpr' representation |
| 6 |
R.RichSExpr(..) |
| 7 |
, R.toRich |
| 8 |
, R.fromRich |
| 9 |
-- * Constructing and Deconstructing |
| 10 |
, cons |
| 11 |
, uncons |
| 12 |
-- * Useful pattern synonyms |
| 13 |
, pattern (:::) |
| 14 |
, pattern A |
| 15 |
, pattern L |
| 16 |
, pattern DL |
| 17 |
, pattern Nil |
| 18 |
-- * Lenses |
| 19 |
, _car |
| 20 |
, _cdr |
| 21 |
-- * Useful processing functions |
| 22 |
, fromPair |
| 23 |
, fromList |
| 24 |
, fromAtom |
| 25 |
, asPair |
| 26 |
, asList |
| 27 |
, isAtom |
| 28 |
, isNil |
| 29 |
, asAtom |
| 30 |
, asAssoc |
| 31 |
, car |
| 32 |
, cdr |
| 33 |
) where |
| 34 |
|
| 35 |
#if !MIN_VERSION_base(4,8,0) |
| 36 |
import Control.Applicative (Applicative, (<$>), (<*>), pure) |
| 37 |
#endif |
| 38 |
import Data.SCargot.Repr as R |
| 39 |
|
| 40 |
-- | A traversal with access to the first element of a pair. |
| 41 |
-- |
| 42 |
-- >>> import Lens.Family |
| 43 |
-- >>> set _car (A "elephant") (L [A "one", A "two", A "three"]) |
| 44 |
-- L [A "elelphant",A "two",A "three"] |
| 45 |
-- >>> set _car (L [A "two", A "three"]) (DL [A "one"] "elephant") |
| 46 |
-- DL [L[A "two",A "three"]] "elephant" |
| 47 |
_car :: Applicative f => (RichSExpr a -> f (RichSExpr a)) -> RichSExpr a -> f (RichSExpr a) |
| 48 |
_car f (RSList (x:xs)) = (\ y -> L (y:xs)) `fmap` f x |
| 49 |
_car f (RSDotted (x:xs) a) = (\ y -> DL (y:xs) a) `fmap` f x |
| 50 |
_car _ (RSAtom a) = pure (A a) |
| 51 |
_car _ (RSList []) = pure Nil |
| 52 |
_car _ (RSDotted [] a) = pure (A a) |
| 53 |
|
| 54 |
-- | A traversal with access to the second element of a pair. Using |
| 55 |
-- this to modify an s-expression may result in changing the |
| 56 |
-- constructor used, changing a list to a dotted list or vice |
| 57 |
-- versa. |
| 58 |
-- |
| 59 |
-- >>> import Lens.Family |
| 60 |
-- >>> set _cdr (A "elephant") (L [A "one", A "two", A "three"]) |
| 61 |
-- DL [A "one"] "elephant" |
| 62 |
-- >>> set _cdr (L [A "two", A "three"]) (DL [A "one"] "elephant") |
| 63 |
-- L [A "one",A "two",A "three"] |
| 64 |
_cdr :: Applicative f => (RichSExpr a -> f (RichSExpr a)) -> RichSExpr a -> f (RichSExpr a) |
| 65 |
_cdr f (RSList (x:xs)) = |
| 66 |
let go (RSList []) = L [x] |
| 67 |
go (RSAtom a) = DL [x] a |
| 68 |
go (RSList xs') = L (x:xs') |
| 69 |
go (RSDotted ys a') = DL (x:ys) a' |
| 70 |
in go `fmap` f (L xs) |
| 71 |
_cdr f (RSDotted [x] a) = |
| 72 |
let go (RSList []) = L [x] |
| 73 |
go (RSAtom a') = DL [x] a' |
| 74 |
go (RSList xs) = L (x:xs) |
| 75 |
go (RSDotted ys a') = DL (x:ys) a' |
| 76 |
in go `fmap` f (A a) |
| 77 |
_cdr f (RSDotted (x:xs) a) = |
| 78 |
let go (RSList []) = L [x] |
| 79 |
go (RSAtom a') = DL [x] a' |
| 80 |
go (RSList ys) = L (x:ys) |
| 81 |
go (RSDotted ys a') = DL (x:ys) a' |
| 82 |
in go `fmap` f (DL xs a) |
| 83 |
_cdr _ (RSAtom a) = pure (A a) |
| 84 |
_cdr _ (RSList []) = pure Nil |
| 85 |
_cdr _ (RSDotted [] a) = pure (A a) |
| 86 |
|
| 87 |
-- | Produce the head and tail of the s-expression (if possible). |
| 88 |
-- |
| 89 |
-- >>> uncons (L [A "el", A "eph", A "ant"]) |
| 90 |
-- Just (A "el",L [A "eph",A "ant"]) |
| 91 |
uncons :: RichSExpr a -> Maybe (RichSExpr a, RichSExpr a) |
| 92 |
uncons (R.RSList (x:xs)) = Just (x, R.RSList xs) |
| 93 |
uncons (R.RSDotted (x:xs) a) = Just (x, R.RSDotted xs a) |
| 94 |
uncons _ = Nothing |
| 95 |
|
| 96 |
-- | Combine the two s-expressions into a new one. |
| 97 |
-- |
| 98 |
-- >>> cons (A "el") (L [A "eph", A "ant"]) |
| 99 |
-- L [A "el",A "eph",A "ant"] |
| 100 |
cons :: RichSExpr a -> RichSExpr a -> RichSExpr a |
| 101 |
cons x (R.RSList xs) = R.RSList (x:xs) |
| 102 |
cons x (R.RSDotted xs a) = R.RSDotted (x:xs) a |
| 103 |
cons x (R.RSAtom a) = R.RSDotted [x] a |
| 104 |
|
| 105 |
-- | A shorter infix alias to grab the head |
| 106 |
-- and tail of an `RSList`. |
| 107 |
-- |
| 108 |
-- >>> A "one" ::: L [A "two", A "three"] |
| 109 |
-- RSList [RSAtom "one",RSAtom "two",RSAtom "three"] |
| 110 |
#if MIN_VERSION_base(4,8,0) |
| 111 |
pattern (:::) :: RichSExpr a -> RichSExpr a -> RichSExpr a |
| 112 |
#endif |
| 113 |
pattern x ::: xs <- (uncons -> Just (x, xs)) |
| 114 |
#if MIN_VERSION_base(4,8,0) |
| 115 |
where x ::: xs = cons x xs |
| 116 |
#endif |
| 117 |
|
| 118 |
-- | A shorter alias for `RSAtom` |
| 119 |
-- |
| 120 |
-- >>> A "elephant" |
| 121 |
-- RSAtom "elephant" |
| 122 |
#if MIN_VERSION_base(4,8,0) |
| 123 |
pattern A :: a -> RichSExpr a |
| 124 |
#endif |
| 125 |
pattern A a = R.RSAtom a |
| 126 |
|
| 127 |
-- | A shorter alias for `RSList` |
| 128 |
-- |
| 129 |
-- >>> L [A "pachy", A "derm"] |
| 130 |
-- RSList [RSAtom "pachy",RSAtom "derm"] |
| 131 |
#if MIN_VERSION_base(4,8,0) |
| 132 |
pattern L :: [RichSExpr a] -> RichSExpr a |
| 133 |
#endif |
| 134 |
pattern L xs = R.RSList xs |
| 135 |
|
| 136 |
-- | A shorter alias for `RSDotted` |
| 137 |
-- |
| 138 |
-- >>> DL [A "pachy"] "derm" |
| 139 |
-- RSDotted [RSAtom "pachy"] "derm" |
| 140 |
#if MIN_VERSION_base(4,8,0) |
| 141 |
pattern DL :: [RichSExpr a] -> a -> RichSExpr a |
| 142 |
#endif |
| 143 |
pattern DL xs x = R.RSDotted xs x |
| 144 |
|
| 145 |
-- | A shorter alias for `RSList` @[]@ |
| 146 |
-- |
| 147 |
-- >>> Nil |
| 148 |
-- RSList [] |
| 149 |
#if MIN_VERSION_base(4,8,0) |
| 150 |
pattern Nil :: RichSExpr a |
| 151 |
#endif |
| 152 |
pattern Nil = R.RSList [] |
| 153 |
|
| 154 |
-- | Utility function for parsing a pair of things: this parses a two-element list, |
| 155 |
-- and not a cons pair. |
| 156 |
-- |
| 157 |
-- >>> fromPair (isAtom "pachy") (asAtom return) (L [A "pachy", A "derm"]) |
| 158 |
-- Right ((), "derm") |
| 159 |
-- >>> fromPair (isAtom "pachy") fromAtom (L [A "pachy"]) |
| 160 |
-- Left "Expected two-element list" |
| 161 |
fromPair :: (RichSExpr t -> Either String a) |
| 162 |
-> (RichSExpr t -> Either String b) |
| 163 |
-> RichSExpr t -> Either String (a, b) |
| 164 |
fromPair pl pr = asPair $ \(l,r) -> (,) <$> pl l <*> pr r |
| 165 |
|
| 166 |
-- | Utility function for parsing a proper list of things. |
| 167 |
-- |
| 168 |
-- >>> fromList fromAtom (L [A "this", A "that", A "the-other"]) |
| 169 |
-- Right ["this","that","the-other"] |
| 170 |
-- >>> fromList fromAtom (DL [A "this", A "that"] "the-other"]) |
| 171 |
-- Left "asList: expected proper list; found dotted list" |
| 172 |
fromList :: (RichSExpr t -> Either String a) -> RichSExpr t -> Either String [a] |
| 173 |
fromList p = asList $ \ss -> mapM p ss |
| 174 |
|
| 175 |
-- | Utility function for parsing a single atom |
| 176 |
-- |
| 177 |
-- >>> fromAtom (A "elephant") |
| 178 |
-- Right "elephant" |
| 179 |
-- >>> fromAtom (L [A "elephant"]) |
| 180 |
-- Left "fromAtom: expected atom; found list" |
| 181 |
fromAtom :: RichSExpr t -> Either String t |
| 182 |
fromAtom (RSList _) = Left "fromAtom: expected atom; found list" |
| 183 |
fromAtom (RSDotted _ _) = Left "fromAtom: expected atom; found dotted list" |
| 184 |
fromAtom (RSAtom a) = return a |
| 185 |
|
| 186 |
-- | Parses a two-element list using the provided function. |
| 187 |
-- |
| 188 |
-- >>> let go (A l) (A r) = return (l ++ r); go _ _ = Left "expected atoms" |
| 189 |
-- >>> asPair go (L [A "pachy", A "derm"]) |
| 190 |
-- Right "pachyderm" |
| 191 |
-- >>> asPair go (L [A "elephant"]) |
| 192 |
-- Left "asPair: expected two-element list; found list of length 1" |
| 193 |
asPair :: ((RichSExpr t, RichSExpr t) -> Either String a) |
| 194 |
-> RichSExpr t -> Either String a |
| 195 |
asPair f (RSList [l, r]) = f (l, r) |
| 196 |
asPair _ (RSList ls) = Left ("asPair: expected two-element list; found list of lenght " ++ show (length ls)) |
| 197 |
asPair _ RSDotted {} = Left ("asPair: expected two-element list; found dotted list") |
| 198 |
asPair _ RSAtom {} = Left ("asPair: expected two-element list; found atom") |
| 199 |
|
| 200 |
-- | Parse an arbitrary-length list using the provided function. |
| 201 |
-- |
| 202 |
-- >>> let go xs = concat <$> mapM fromAtom xs |
| 203 |
-- >>> asList go (L [A "el", A "eph", A "ant"]) |
| 204 |
-- Right "elephant" |
| 205 |
-- >>> asList go (DL [A "el", A "eph"] "ant") |
| 206 |
-- Left "asList: expected list; found dotted list" |
| 207 |
asList :: ([RichSExpr t] -> Either String a) |
| 208 |
-> RichSExpr t -> Either String a |
| 209 |
asList f (RSList ls) = f ls |
| 210 |
asList _ RSDotted {} = Left ("asList: expected list; found dotted list") |
| 211 |
asList _ RSAtom { } = Left ("asList: expected list; found dotted list") |
| 212 |
|
| 213 |
-- | Match a given literal atom, failing otherwise. |
| 214 |
-- |
| 215 |
-- >>> isAtom "elephant" (A "elephant") |
| 216 |
-- Right () |
| 217 |
-- >>> isAtom "elephant" (L [A "elephant"]) |
| 218 |
-- Left "isAtom: expected atom; found list" |
| 219 |
isAtom :: Eq t => t -> RichSExpr t -> Either String () |
| 220 |
isAtom s (RSAtom s') |
| 221 |
| s == s' = return () |
| 222 |
| otherwise = Left "isAtom: failed to match atom" |
| 223 |
isAtom _ RSList {} = Left "isAtom: expected atom; found list" |
| 224 |
isAtom _ RSDotted {} = Left "isAtom: expected atom; found dotted list" |
| 225 |
|
| 226 |
-- | Match an empty list, failing otherwise. |
| 227 |
-- |
| 228 |
-- >>> isNil (L []) |
| 229 |
-- Right () |
| 230 |
-- >>> isNil (A "elephant") |
| 231 |
-- Left "isNil: expected nil; found atom" |
| 232 |
isNil :: RichSExpr t -> Either String () |
| 233 |
isNil (RSList []) = return () |
| 234 |
isNil RSList {} = Left "isNil: expected nil; found non-nil list" |
| 235 |
isNil RSDotted {} = Left "isNil: expected nil; found dotted list" |
| 236 |
isNil RSAtom {} = Left "isNil: expected nil; found atom" |
| 237 |
|
| 238 |
-- | Parse an atom using the provided function. |
| 239 |
-- |
| 240 |
-- >>> import Data.Char (toUpper) |
| 241 |
-- >>> asAtom (return . map toUpper) (A "elephant") |
| 242 |
-- Right "ELEPHANT" |
| 243 |
-- >>> asAtom (return . map toUpper) (L []) |
| 244 |
-- Left "asAtom: expected atom; found list" |
| 245 |
asAtom :: (t -> Either String a) -> RichSExpr t -> Either String a |
| 246 |
asAtom f (RSAtom s) = f s |
| 247 |
asAtom _ RSList {} = Left ("asAtom: expected atom; found list") |
| 248 |
asAtom _ RSDotted {} = Left ("asAtom: expected atom; found dotted list") |
| 249 |
|
| 250 |
-- | Parse an assoc-list using the provided function. |
| 251 |
-- |
| 252 |
-- >>> let def (x, y) = do { a <- fromAtom x; b <- fromAtom y; return (a ++ ": " ++ b) } |
| 253 |
-- >>> let defList xs = do { defs <- mapM def xs; return (unlines defs) } |
| 254 |
-- >>> asAssoc defList (L [ L [A "legs", A "four"], L [ A "trunk", A "one"] ]) |
| 255 |
-- Right "legs: four\ntrunk: one\n" |
| 256 |
-- >>> asAssoc defList (L [ L [A "legs", A "four"], L [ A "elephant"] ]) |
| 257 |
-- Left "asAssoc: expected pair; found list of length 1" |
| 258 |
asAssoc :: ([(RichSExpr t, RichSExpr t)] -> Either String a) |
| 259 |
-> RichSExpr t -> Either String a |
| 260 |
asAssoc f (RSList ss) = gatherPairs ss >>= f |
| 261 |
where gatherPairs (RSList [a, b] : ts) = (:) <$> pure (a, b) <*> gatherPairs ts |
| 262 |
gatherPairs [] = pure [] |
| 263 |
gatherPairs (RSAtom {} : _) = Left ("asAssoc: expected pair; found atom") |
| 264 |
gatherPairs (RSDotted {} : _) = Left ("asAssoc: expected pair; found dotted list") |
| 265 |
gatherPairs (RSList ls : _) = Left ("asAssoc: expected pair; found list of length " ++ show (length ls)) |
| 266 |
asAssoc _ RSDotted {} = Left "asAssoc: expected assoc list; found dotted list" |
| 267 |
asAssoc _ RSAtom {} = Left "asAssoc: expected assoc list; found atom" |
| 268 |
|
| 269 |
car :: (RichSExpr t -> Either String t') -> [RichSExpr t] -> Either String t' |
| 270 |
car f (x:_) = f x |
| 271 |
car _ [] = Left "car: Taking car of zero-element list" |
| 272 |
|
| 273 |
cdr :: ([RichSExpr t] -> Either String t') -> [RichSExpr t] -> Either String t' |
| 274 |
cdr f (_:xs) = f xs |
| 275 |
cdr _ [] = Left "cdr: Taking cdr of zero-element list" |