module Data.SCargot.Repr.Basic
(
R.SExpr(..)
, cons
, uncons
, pattern (:::)
, pattern A
, pattern L
, pattern DL
, pattern Nil
, _car
, _cdr
, fromPair
, fromList
, fromAtom
, asPair
, asList
, isAtom
, asAtom
, asAssoc
) where
#if !MIN_VERSION_base(4,8,0)
import Control.Applicative (Applicative, (<$>), (<*>), pure)
#endif
import Data.SCargot.Repr as R
_car :: Applicative f => (SExpr a -> f (SExpr a)) -> SExpr a -> f (SExpr a)
_car f (SCons x xs) = (:::) <$> f x <*> pure xs
_car _ (SAtom a) = pure (A a)
_car _ SNil = pure SNil
_cdr :: Applicative f => (SExpr a -> f (SExpr a)) -> SExpr a -> f (SExpr a)
_cdr f (SCons x xs) = (:::) <$> pure x <*> f xs
_cdr _ (SAtom a) = pure (A a)
_cdr _ SNil = pure Nil
uncons :: SExpr a -> Maybe (SExpr a, SExpr a)
uncons (SCons x xs) = Just (x, xs)
uncons _ = Nothing
cons :: SExpr a -> SExpr a -> SExpr a
cons = SCons
gatherDList :: SExpr a -> Maybe ([SExpr a], a)
gatherDList SNil = Nothing
gatherDList SAtom {} = Nothing
gatherDList sx = go sx
where go SNil = Nothing
go (SAtom a) = return ([], a)
go (SCons x xs) = do
(ys, a) <- go xs
return (x:ys, a)
infixr 5 :::
#if MIN_VERSION_base(4,8,0)
pattern (:::) :: SExpr a -> SExpr a -> SExpr a
#endif
pattern x ::: xs = SCons x xs
#if MIN_VERSION_base(4,8,0)
pattern A :: a -> SExpr a
#endif
pattern A x = SAtom x
#if MIN_VERSION_base(4,8,0)
pattern Nil :: SExpr a
#endif
pattern Nil = SNil
#if MIN_VERSION_base(4,8,0)
pattern L :: [SExpr a] -> SExpr a
#endif
pattern L xs <- (gatherList -> Right xs)
#if MIN_VERSION_base(4,8,0)
where L [] = SNil
L (x:xs) = SCons x (L xs)
#endif
#if MIN_VERSION_base(4,8,0)
pattern DL :: [SExpr a] -> a -> SExpr a
#endif
pattern DL xs x <- (gatherDList -> Just (xs, x))
#if MIN_VERSION_base(4,8,0)
where DL [] a = SAtom a
DL (x:xs) a = SCons x (DL xs a)
#endif
getShape :: SExpr a -> String
getShape Nil = "empty list"
getShape sx = go (0 :: Int) sx
where go n SNil = "list of length " ++ show n
go n SAtom {} = "dotted list of length " ++ show n
go n (SCons _ xs) = go (n+1) xs
fromPair :: (SExpr t -> Either String a)
-> (SExpr t -> Either String b)
-> SExpr t -> Either String (a, b)
fromPair pl pr (l ::: r ::: Nil) = (,) <$> pl l <*> pr r
fromPair _ _ sx = Left ("fromPair: expected two-element list; found " ++ getShape sx)
fromList :: (SExpr t -> Either String a) -> SExpr t -> Either String [a]
fromList p (s ::: ss) = (:) <$> p s <*> fromList p ss
fromList _ Nil = pure []
fromList _ sx = Left ("fromList: expected list; found " ++ getShape sx)
fromAtom :: SExpr t -> Either String t
fromAtom (A a) = return a
fromAtom sx = Left ("fromAtom: expected atom; found list" ++ getShape sx)
gatherList :: SExpr t -> Either String [SExpr t]
gatherList (x ::: xs) = (:) <$> pure x <*> gatherList xs
gatherList Nil = pure []
gatherList sx = Left ("gatherList: expected list; found " ++ getShape sx)
asPair :: ((SExpr t, SExpr t) -> Either String a)
-> SExpr t -> Either String a
asPair f (l ::: r ::: SNil) = f (l, r)
asPair _ sx = Left ("asPair: expected two-element list; found " ++ getShape sx)
asList :: ([SExpr t] -> Either String a) -> SExpr t -> Either String a
asList f ls = gatherList ls >>= f
isAtom :: Eq t => t -> SExpr t -> Either String ()
isAtom s (A s')
| s == s' = return ()
| otherwise = Left "isAtom: failed to match atom"
isAtom _ sx = Left ("isAtom: expected atom; found " ++ getShape sx)
asAtom :: (t -> Either String a) -> SExpr t -> Either String a
asAtom f (A s) = f s
asAtom _ sx = Left ("asAtom: expected atom; found " ++ getShape sx)
asAssoc :: ([(SExpr t, SExpr t)] -> Either String a)
-> SExpr t -> Either String a
asAssoc f ss = gatherList ss >>= mapM go >>= f
where go (a ::: b ::: Nil) = return (a, b)
go sx = Left ("asAssoc: expected pair; found " ++ getShape sx)