{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveFoldable #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE TypeFamilies #-}

module Data.SCargot.Repr
       ( -- $reprs
         -- * Elementary SExpr representation
         SExpr(..)
         -- * Rich SExpr representation
       , RichSExpr(..)
       , toRich
       , fromRich
         -- * Well-Formed SExpr representation
       , WellFormedSExpr(..)
       , toWellFormed
       , fromWellFormed
       ) where

import Data.Data (Data)
import Data.Foldable (Foldable(..))
import Data.Traversable (Traversable(..))
import Data.Typeable (Typeable)
import GHC.Exts (IsList(..), IsString(..))

#if !MIN_VERSION_base(4,8,0)
import Prelude hiding (foldr)
#endif

-- | All S-Expressions can be understood as a sequence
--   of @cons@ cells (represented here by 'SCons'), the
--   empty list @nil@ (represented by 'SNil') or an
--   @atom@.
data SExpr atom
  = SCons (SExpr atom) (SExpr atom)
  | SAtom atom
  | SNil
    deriving (SExpr atom -> SExpr atom -> Bool
forall atom. Eq atom => SExpr atom -> SExpr atom -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SExpr atom -> SExpr atom -> Bool
$c/= :: forall atom. Eq atom => SExpr atom -> SExpr atom -> Bool
== :: SExpr atom -> SExpr atom -> Bool
$c== :: forall atom. Eq atom => SExpr atom -> SExpr atom -> Bool
Eq, Int -> SExpr atom -> ShowS
forall atom. Show atom => Int -> SExpr atom -> ShowS
forall atom. Show atom => [SExpr atom] -> ShowS
forall atom. Show atom => SExpr atom -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SExpr atom] -> ShowS
$cshowList :: forall atom. Show atom => [SExpr atom] -> ShowS
show :: SExpr atom -> String
$cshow :: forall atom. Show atom => SExpr atom -> String
showsPrec :: Int -> SExpr atom -> ShowS
$cshowsPrec :: forall atom. Show atom => Int -> SExpr atom -> ShowS
Show, ReadPrec [SExpr atom]
ReadPrec (SExpr atom)
ReadS [SExpr atom]
forall atom. Read atom => ReadPrec [SExpr atom]
forall atom. Read atom => ReadPrec (SExpr atom)
forall atom. Read atom => Int -> ReadS (SExpr atom)
forall atom. Read atom => ReadS [SExpr atom]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [SExpr atom]
$creadListPrec :: forall atom. Read atom => ReadPrec [SExpr atom]
readPrec :: ReadPrec (SExpr atom)
$creadPrec :: forall atom. Read atom => ReadPrec (SExpr atom)
readList :: ReadS [SExpr atom]
$creadList :: forall atom. Read atom => ReadS [SExpr atom]
readsPrec :: Int -> ReadS (SExpr atom)
$creadsPrec :: forall atom. Read atom => Int -> ReadS (SExpr atom)
Read, forall a b. a -> SExpr b -> SExpr a
forall a b. (a -> b) -> SExpr a -> SExpr b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> SExpr b -> SExpr a
$c<$ :: forall a b. a -> SExpr b -> SExpr a
fmap :: forall a b. (a -> b) -> SExpr a -> SExpr b
$cfmap :: forall a b. (a -> b) -> SExpr a -> SExpr b
Functor, SExpr atom -> DataType
SExpr atom -> Constr
forall {atom}. Data atom => Typeable (SExpr atom)
forall atom. Data atom => SExpr atom -> DataType
forall atom. Data atom => SExpr atom -> Constr
forall atom.
Data atom =>
(forall b. Data b => b -> b) -> SExpr atom -> SExpr atom
forall atom u.
Data atom =>
Int -> (forall d. Data d => d -> u) -> SExpr atom -> u
forall atom u.
Data atom =>
(forall d. Data d => d -> u) -> SExpr atom -> [u]
forall atom r r'.
Data atom =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> SExpr atom -> r
forall atom r r'.
Data atom =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> SExpr atom -> r
forall atom (m :: * -> *).
(Data atom, Monad m) =>
(forall d. Data d => d -> m d) -> SExpr atom -> m (SExpr atom)
forall atom (m :: * -> *).
(Data atom, MonadPlus m) =>
(forall d. Data d => d -> m d) -> SExpr atom -> m (SExpr atom)
forall atom (c :: * -> *).
Data atom =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (SExpr atom)
forall atom (c :: * -> *).
Data atom =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> SExpr atom -> c (SExpr atom)
forall atom (t :: * -> *) (c :: * -> *).
(Data atom, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (SExpr atom))
forall atom (t :: * -> * -> *) (c :: * -> *).
(Data atom, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (SExpr atom))
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (SExpr atom)
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> SExpr atom -> c (SExpr atom)
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (SExpr atom))
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> SExpr atom -> m (SExpr atom)
$cgmapMo :: forall atom (m :: * -> *).
(Data atom, MonadPlus m) =>
(forall d. Data d => d -> m d) -> SExpr atom -> m (SExpr atom)
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> SExpr atom -> m (SExpr atom)
$cgmapMp :: forall atom (m :: * -> *).
(Data atom, MonadPlus m) =>
(forall d. Data d => d -> m d) -> SExpr atom -> m (SExpr atom)
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> SExpr atom -> m (SExpr atom)
$cgmapM :: forall atom (m :: * -> *).
(Data atom, Monad m) =>
(forall d. Data d => d -> m d) -> SExpr atom -> m (SExpr atom)
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> SExpr atom -> u
$cgmapQi :: forall atom u.
Data atom =>
Int -> (forall d. Data d => d -> u) -> SExpr atom -> u
gmapQ :: forall u. (forall d. Data d => d -> u) -> SExpr atom -> [u]
$cgmapQ :: forall atom u.
Data atom =>
(forall d. Data d => d -> u) -> SExpr atom -> [u]
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> SExpr atom -> r
$cgmapQr :: forall atom r r'.
Data atom =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> SExpr atom -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> SExpr atom -> r
$cgmapQl :: forall atom r r'.
Data atom =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> SExpr atom -> r
gmapT :: (forall b. Data b => b -> b) -> SExpr atom -> SExpr atom
$cgmapT :: forall atom.
Data atom =>
(forall b. Data b => b -> b) -> SExpr atom -> SExpr atom
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (SExpr atom))
$cdataCast2 :: forall atom (t :: * -> * -> *) (c :: * -> *).
(Data atom, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (SExpr atom))
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (SExpr atom))
$cdataCast1 :: forall atom (t :: * -> *) (c :: * -> *).
(Data atom, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (SExpr atom))
dataTypeOf :: SExpr atom -> DataType
$cdataTypeOf :: forall atom. Data atom => SExpr atom -> DataType
toConstr :: SExpr atom -> Constr
$ctoConstr :: forall atom. Data atom => SExpr atom -> Constr
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (SExpr atom)
$cgunfold :: forall atom (c :: * -> *).
Data atom =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (SExpr atom)
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> SExpr atom -> c (SExpr atom)
$cgfoldl :: forall atom (c :: * -> *).
Data atom =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> SExpr atom -> c (SExpr atom)
Data, Typeable, forall a. Eq a => a -> SExpr a -> Bool
forall a. Num a => SExpr a -> a
forall a. Ord a => SExpr a -> a
forall m. Monoid m => SExpr m -> m
forall a. SExpr a -> Bool
forall a. SExpr a -> Int
forall a. SExpr a -> [a]
forall a. (a -> a -> a) -> SExpr a -> a
forall m a. Monoid m => (a -> m) -> SExpr a -> m
forall b a. (b -> a -> b) -> b -> SExpr a -> b
forall a b. (a -> b -> b) -> b -> SExpr a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
product :: forall a. Num a => SExpr a -> a
$cproduct :: forall a. Num a => SExpr a -> a
sum :: forall a. Num a => SExpr a -> a
$csum :: forall a. Num a => SExpr a -> a
minimum :: forall a. Ord a => SExpr a -> a
$cminimum :: forall a. Ord a => SExpr a -> a
maximum :: forall a. Ord a => SExpr a -> a
$cmaximum :: forall a. Ord a => SExpr a -> a
elem :: forall a. Eq a => a -> SExpr a -> Bool
$celem :: forall a. Eq a => a -> SExpr a -> Bool
length :: forall a. SExpr a -> Int
$clength :: forall a. SExpr a -> Int
null :: forall a. SExpr a -> Bool
$cnull :: forall a. SExpr a -> Bool
toList :: forall a. SExpr a -> [a]
$ctoList :: forall a. SExpr a -> [a]
foldl1 :: forall a. (a -> a -> a) -> SExpr a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> SExpr a -> a
foldr1 :: forall a. (a -> a -> a) -> SExpr a -> a
$cfoldr1 :: forall a. (a -> a -> a) -> SExpr a -> a
foldl' :: forall b a. (b -> a -> b) -> b -> SExpr a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> SExpr a -> b
foldl :: forall b a. (b -> a -> b) -> b -> SExpr a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> SExpr a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> SExpr a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> SExpr a -> b
foldr :: forall a b. (a -> b -> b) -> b -> SExpr a -> b
$cfoldr :: forall a b. (a -> b -> b) -> b -> SExpr a -> b
foldMap' :: forall m a. Monoid m => (a -> m) -> SExpr a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> SExpr a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> SExpr a -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> SExpr a -> m
fold :: forall m. Monoid m => SExpr m -> m
$cfold :: forall m. Monoid m => SExpr m -> m
Foldable, Functor SExpr
Foldable SExpr
forall (t :: * -> *).
Functor t
-> Foldable t
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a. Monad m => SExpr (m a) -> m (SExpr a)
forall (f :: * -> *) a. Applicative f => SExpr (f a) -> f (SExpr a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> SExpr a -> m (SExpr b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> SExpr a -> f (SExpr b)
sequence :: forall (m :: * -> *) a. Monad m => SExpr (m a) -> m (SExpr a)
$csequence :: forall (m :: * -> *) a. Monad m => SExpr (m a) -> m (SExpr a)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> SExpr a -> m (SExpr b)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> SExpr a -> m (SExpr b)
sequenceA :: forall (f :: * -> *) a. Applicative f => SExpr (f a) -> f (SExpr a)
$csequenceA :: forall (f :: * -> *) a. Applicative f => SExpr (f a) -> f (SExpr a)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> SExpr a -> f (SExpr b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> SExpr a -> f (SExpr b)
Traversable)

instance IsString atom => IsString (SExpr atom) where
  fromString :: String -> SExpr atom
fromString = forall atom. atom -> SExpr atom
SAtom forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. IsString a => String -> a
fromString

instance IsList (SExpr atom) where
  type Item (SExpr atom) = SExpr atom
  fromList :: [Item (SExpr atom)] -> SExpr atom
fromList = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr forall atom. SExpr atom -> SExpr atom -> SExpr atom
SCons forall atom. SExpr atom
SNil
  toList :: SExpr atom -> [Item (SExpr atom)]
toList   = forall {atom}. SExpr atom -> [SExpr atom]
go
    where go :: SExpr atom -> [SExpr atom]
go (SCons SExpr atom
x SExpr atom
xs) = SExpr atom
x forall a. a -> [a] -> [a]
: SExpr atom -> [SExpr atom]
go SExpr atom
xs
          go SExpr atom
SNil         = []
          go (SAtom {})   = forall a. HasCallStack => String -> a
error String
"Unable to turn atom into list"

-- | Sometimes the cons-based interface is too low
--   level, and we'd rather have the lists themselves
--   exposed. In this case, we have 'RSList' to
--   represent a well-formed cons list, and 'RSDotted'
--   to represent an improper list of the form
--   @(a b c . d)@. This representation is based on
--   the structure of the parsed S-Expression, and not on
--   how it was originally represented: thus, @(a . (b))@ is going to
--   be represented as @RSList[RSAtom a, RSAtom b]@
--   despite having been originally represented as a
--   dotted list.
data RichSExpr atom
  = RSList [RichSExpr atom]
  | RSDotted [RichSExpr atom] atom
  | RSAtom atom
    deriving (RichSExpr atom -> RichSExpr atom -> Bool
forall atom. Eq atom => RichSExpr atom -> RichSExpr atom -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: RichSExpr atom -> RichSExpr atom -> Bool
$c/= :: forall atom. Eq atom => RichSExpr atom -> RichSExpr atom -> Bool
== :: RichSExpr atom -> RichSExpr atom -> Bool
$c== :: forall atom. Eq atom => RichSExpr atom -> RichSExpr atom -> Bool
Eq, Int -> RichSExpr atom -> ShowS
forall atom. Show atom => Int -> RichSExpr atom -> ShowS
forall atom. Show atom => [RichSExpr atom] -> ShowS
forall atom. Show atom => RichSExpr atom -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [RichSExpr atom] -> ShowS
$cshowList :: forall atom. Show atom => [RichSExpr atom] -> ShowS
show :: RichSExpr atom -> String
$cshow :: forall atom. Show atom => RichSExpr atom -> String
showsPrec :: Int -> RichSExpr atom -> ShowS
$cshowsPrec :: forall atom. Show atom => Int -> RichSExpr atom -> ShowS
Show, ReadPrec [RichSExpr atom]
ReadPrec (RichSExpr atom)
ReadS [RichSExpr atom]
forall atom. Read atom => ReadPrec [RichSExpr atom]
forall atom. Read atom => ReadPrec (RichSExpr atom)
forall atom. Read atom => Int -> ReadS (RichSExpr atom)
forall atom. Read atom => ReadS [RichSExpr atom]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [RichSExpr atom]
$creadListPrec :: forall atom. Read atom => ReadPrec [RichSExpr atom]
readPrec :: ReadPrec (RichSExpr atom)
$creadPrec :: forall atom. Read atom => ReadPrec (RichSExpr atom)
readList :: ReadS [RichSExpr atom]
$creadList :: forall atom. Read atom => ReadS [RichSExpr atom]
readsPrec :: Int -> ReadS (RichSExpr atom)
$creadsPrec :: forall atom. Read atom => Int -> ReadS (RichSExpr atom)
Read, forall a b. a -> RichSExpr b -> RichSExpr a
forall a b. (a -> b) -> RichSExpr a -> RichSExpr b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> RichSExpr b -> RichSExpr a
$c<$ :: forall a b. a -> RichSExpr b -> RichSExpr a
fmap :: forall a b. (a -> b) -> RichSExpr a -> RichSExpr b
$cfmap :: forall a b. (a -> b) -> RichSExpr a -> RichSExpr b
Functor, RichSExpr atom -> DataType
RichSExpr atom -> Constr
forall {atom}. Data atom => Typeable (RichSExpr atom)
forall atom. Data atom => RichSExpr atom -> DataType
forall atom. Data atom => RichSExpr atom -> Constr
forall atom.
Data atom =>
(forall b. Data b => b -> b) -> RichSExpr atom -> RichSExpr atom
forall atom u.
Data atom =>
Int -> (forall d. Data d => d -> u) -> RichSExpr atom -> u
forall atom u.
Data atom =>
(forall d. Data d => d -> u) -> RichSExpr atom -> [u]
forall atom r r'.
Data atom =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> RichSExpr atom -> r
forall atom r r'.
Data atom =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> RichSExpr atom -> r
forall atom (m :: * -> *).
(Data atom, Monad m) =>
(forall d. Data d => d -> m d)
-> RichSExpr atom -> m (RichSExpr atom)
forall atom (m :: * -> *).
(Data atom, MonadPlus m) =>
(forall d. Data d => d -> m d)
-> RichSExpr atom -> m (RichSExpr atom)
forall atom (c :: * -> *).
Data atom =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (RichSExpr atom)
forall atom (c :: * -> *).
Data atom =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> RichSExpr atom -> c (RichSExpr atom)
forall atom (t :: * -> *) (c :: * -> *).
(Data atom, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (RichSExpr atom))
forall atom (t :: * -> * -> *) (c :: * -> *).
(Data atom, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (RichSExpr atom))
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (RichSExpr atom)
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> RichSExpr atom -> c (RichSExpr atom)
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (RichSExpr atom))
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> RichSExpr atom -> m (RichSExpr atom)
$cgmapMo :: forall atom (m :: * -> *).
(Data atom, MonadPlus m) =>
(forall d. Data d => d -> m d)
-> RichSExpr atom -> m (RichSExpr atom)
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> RichSExpr atom -> m (RichSExpr atom)
$cgmapMp :: forall atom (m :: * -> *).
(Data atom, MonadPlus m) =>
(forall d. Data d => d -> m d)
-> RichSExpr atom -> m (RichSExpr atom)
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> RichSExpr atom -> m (RichSExpr atom)
$cgmapM :: forall atom (m :: * -> *).
(Data atom, Monad m) =>
(forall d. Data d => d -> m d)
-> RichSExpr atom -> m (RichSExpr atom)
gmapQi :: forall u.
Int -> (forall d. Data d => d -> u) -> RichSExpr atom -> u
$cgmapQi :: forall atom u.
Data atom =>
Int -> (forall d. Data d => d -> u) -> RichSExpr atom -> u
gmapQ :: forall u. (forall d. Data d => d -> u) -> RichSExpr atom -> [u]
$cgmapQ :: forall atom u.
Data atom =>
(forall d. Data d => d -> u) -> RichSExpr atom -> [u]
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> RichSExpr atom -> r
$cgmapQr :: forall atom r r'.
Data atom =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> RichSExpr atom -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> RichSExpr atom -> r
$cgmapQl :: forall atom r r'.
Data atom =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> RichSExpr atom -> r
gmapT :: (forall b. Data b => b -> b) -> RichSExpr atom -> RichSExpr atom
$cgmapT :: forall atom.
Data atom =>
(forall b. Data b => b -> b) -> RichSExpr atom -> RichSExpr atom
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (RichSExpr atom))
$cdataCast2 :: forall atom (t :: * -> * -> *) (c :: * -> *).
(Data atom, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (RichSExpr atom))
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (RichSExpr atom))
$cdataCast1 :: forall atom (t :: * -> *) (c :: * -> *).
(Data atom, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (RichSExpr atom))
dataTypeOf :: RichSExpr atom -> DataType
$cdataTypeOf :: forall atom. Data atom => RichSExpr atom -> DataType
toConstr :: RichSExpr atom -> Constr
$ctoConstr :: forall atom. Data atom => RichSExpr atom -> Constr
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (RichSExpr atom)
$cgunfold :: forall atom (c :: * -> *).
Data atom =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (RichSExpr atom)
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> RichSExpr atom -> c (RichSExpr atom)
$cgfoldl :: forall atom (c :: * -> *).
Data atom =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> RichSExpr atom -> c (RichSExpr atom)
Data, Typeable, forall a. Eq a => a -> RichSExpr a -> Bool
forall a. Num a => RichSExpr a -> a
forall a. Ord a => RichSExpr a -> a
forall m. Monoid m => RichSExpr m -> m
forall a. RichSExpr a -> Bool
forall a. RichSExpr a -> Int
forall a. RichSExpr a -> [a]
forall a. (a -> a -> a) -> RichSExpr a -> a
forall m a. Monoid m => (a -> m) -> RichSExpr a -> m
forall b a. (b -> a -> b) -> b -> RichSExpr a -> b
forall a b. (a -> b -> b) -> b -> RichSExpr a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
product :: forall a. Num a => RichSExpr a -> a
$cproduct :: forall a. Num a => RichSExpr a -> a
sum :: forall a. Num a => RichSExpr a -> a
$csum :: forall a. Num a => RichSExpr a -> a
minimum :: forall a. Ord a => RichSExpr a -> a
$cminimum :: forall a. Ord a => RichSExpr a -> a
maximum :: forall a. Ord a => RichSExpr a -> a
$cmaximum :: forall a. Ord a => RichSExpr a -> a
elem :: forall a. Eq a => a -> RichSExpr a -> Bool
$celem :: forall a. Eq a => a -> RichSExpr a -> Bool
length :: forall a. RichSExpr a -> Int
$clength :: forall a. RichSExpr a -> Int
null :: forall a. RichSExpr a -> Bool
$cnull :: forall a. RichSExpr a -> Bool
toList :: forall a. RichSExpr a -> [a]
$ctoList :: forall a. RichSExpr a -> [a]
foldl1 :: forall a. (a -> a -> a) -> RichSExpr a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> RichSExpr a -> a
foldr1 :: forall a. (a -> a -> a) -> RichSExpr a -> a
$cfoldr1 :: forall a. (a -> a -> a) -> RichSExpr a -> a
foldl' :: forall b a. (b -> a -> b) -> b -> RichSExpr a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> RichSExpr a -> b
foldl :: forall b a. (b -> a -> b) -> b -> RichSExpr a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> RichSExpr a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> RichSExpr a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> RichSExpr a -> b
foldr :: forall a b. (a -> b -> b) -> b -> RichSExpr a -> b
$cfoldr :: forall a b. (a -> b -> b) -> b -> RichSExpr a -> b
foldMap' :: forall m a. Monoid m => (a -> m) -> RichSExpr a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> RichSExpr a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> RichSExpr a -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> RichSExpr a -> m
fold :: forall m. Monoid m => RichSExpr m -> m
$cfold :: forall m. Monoid m => RichSExpr m -> m
Foldable, Functor RichSExpr
Foldable RichSExpr
forall (t :: * -> *).
Functor t
-> Foldable t
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a.
Monad m =>
RichSExpr (m a) -> m (RichSExpr a)
forall (f :: * -> *) a.
Applicative f =>
RichSExpr (f a) -> f (RichSExpr a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> RichSExpr a -> m (RichSExpr b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> RichSExpr a -> f (RichSExpr b)
sequence :: forall (m :: * -> *) a.
Monad m =>
RichSExpr (m a) -> m (RichSExpr a)
$csequence :: forall (m :: * -> *) a.
Monad m =>
RichSExpr (m a) -> m (RichSExpr a)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> RichSExpr a -> m (RichSExpr b)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> RichSExpr a -> m (RichSExpr b)
sequenceA :: forall (f :: * -> *) a.
Applicative f =>
RichSExpr (f a) -> f (RichSExpr a)
$csequenceA :: forall (f :: * -> *) a.
Applicative f =>
RichSExpr (f a) -> f (RichSExpr a)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> RichSExpr a -> f (RichSExpr b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> RichSExpr a -> f (RichSExpr b)
Traversable)

instance IsString atom => IsString (RichSExpr atom) where
  fromString :: String -> RichSExpr atom
fromString = forall atom. atom -> RichSExpr atom
RSAtom forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. IsString a => String -> a
fromString

instance IsList (RichSExpr atom) where
  type Item (RichSExpr atom) = RichSExpr atom
  fromList :: [Item (RichSExpr atom)] -> RichSExpr atom
fromList = forall atom. [RichSExpr atom] -> RichSExpr atom
RSList
  toList :: RichSExpr atom -> [Item (RichSExpr atom)]
toList (RSList [RichSExpr atom]
xs)   = [RichSExpr atom]
xs
  toList (RSDotted {}) = forall a. HasCallStack => String -> a
error String
"Unable to turn dotted list into haskell list"
  toList (RSAtom {})   = forall a. HasCallStack => String -> a
error String
"Unable to turn atom into Haskell list"

-- |  It should always be true that
--
--   > fromRich (toRich x) == x
--
--   and that
--
--   > toRich (fromRich x) == x
toRich :: SExpr atom -> RichSExpr atom
toRich :: forall atom. SExpr atom -> RichSExpr atom
toRich (SAtom atom
a) = forall atom. atom -> RichSExpr atom
RSAtom atom
a
toRich (SCons SExpr atom
x SExpr atom
xs) = forall {atom}.
SExpr atom
-> ([RichSExpr atom] -> [RichSExpr atom]) -> RichSExpr atom
go SExpr atom
xs (forall atom. SExpr atom -> RichSExpr atom
toRich SExpr atom
xforall a. a -> [a] -> [a]
:)
  where go :: SExpr atom
-> ([RichSExpr atom] -> [RichSExpr atom]) -> RichSExpr atom
go (SAtom atom
a) [RichSExpr atom] -> [RichSExpr atom]
rs    = forall atom. [RichSExpr atom] -> atom -> RichSExpr atom
RSDotted ([RichSExpr atom] -> [RichSExpr atom]
rs []) atom
a
        go SExpr atom
SNil [RichSExpr atom] -> [RichSExpr atom]
rs         = forall atom. [RichSExpr atom] -> RichSExpr atom
RSList ([RichSExpr atom] -> [RichSExpr atom]
rs [])
        go (SCons SExpr atom
y SExpr atom
ys) [RichSExpr atom] -> [RichSExpr atom]
rs = SExpr atom
-> ([RichSExpr atom] -> [RichSExpr atom]) -> RichSExpr atom
go SExpr atom
ys ([RichSExpr atom] -> [RichSExpr atom]
rs forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall atom. SExpr atom -> RichSExpr atom
toRich SExpr atom
yforall a. a -> [a] -> [a]
:))
toRich SExpr atom
SNil = forall atom. [RichSExpr atom] -> RichSExpr atom
RSList []

-- | This follows the same laws as 'toRich'.
fromRich :: RichSExpr atom -> SExpr atom
fromRich :: forall atom. RichSExpr atom -> SExpr atom
fromRich (RSAtom atom
a) = forall atom. atom -> SExpr atom
SAtom atom
a
fromRich (RSList [RichSExpr atom]
xs) = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr forall atom. SExpr atom -> SExpr atom -> SExpr atom
SCons forall atom. SExpr atom
SNil (forall a b. (a -> b) -> [a] -> [b]
map forall atom. RichSExpr atom -> SExpr atom
fromRich [RichSExpr atom]
xs)
fromRich (RSDotted [RichSExpr atom]
xs atom
x) = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr forall atom. SExpr atom -> SExpr atom -> SExpr atom
SCons (forall atom. atom -> SExpr atom
SAtom atom
x) (forall a b. (a -> b) -> [a] -> [b]
map forall atom. RichSExpr atom -> SExpr atom
fromRich [RichSExpr atom]
xs)

-- | A well-formed s-expression is one which does not
--   contain any dotted lists. This means that not
--   every value of @SExpr a@ can be converted to a
--   @WellFormedSExpr a@, although the opposite is
--   fine.
data WellFormedSExpr atom
  = WFSList [WellFormedSExpr atom]
  | WFSAtom atom
    deriving (WellFormedSExpr atom -> WellFormedSExpr atom -> Bool
forall atom.
Eq atom =>
WellFormedSExpr atom -> WellFormedSExpr atom -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: WellFormedSExpr atom -> WellFormedSExpr atom -> Bool
$c/= :: forall atom.
Eq atom =>
WellFormedSExpr atom -> WellFormedSExpr atom -> Bool
== :: WellFormedSExpr atom -> WellFormedSExpr atom -> Bool
$c== :: forall atom.
Eq atom =>
WellFormedSExpr atom -> WellFormedSExpr atom -> Bool
Eq, Int -> WellFormedSExpr atom -> ShowS
forall atom. Show atom => Int -> WellFormedSExpr atom -> ShowS
forall atom. Show atom => [WellFormedSExpr atom] -> ShowS
forall atom. Show atom => WellFormedSExpr atom -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [WellFormedSExpr atom] -> ShowS
$cshowList :: forall atom. Show atom => [WellFormedSExpr atom] -> ShowS
show :: WellFormedSExpr atom -> String
$cshow :: forall atom. Show atom => WellFormedSExpr atom -> String
showsPrec :: Int -> WellFormedSExpr atom -> ShowS
$cshowsPrec :: forall atom. Show atom => Int -> WellFormedSExpr atom -> ShowS
Show, ReadPrec [WellFormedSExpr atom]
ReadPrec (WellFormedSExpr atom)
ReadS [WellFormedSExpr atom]
forall atom. Read atom => ReadPrec [WellFormedSExpr atom]
forall atom. Read atom => ReadPrec (WellFormedSExpr atom)
forall atom. Read atom => Int -> ReadS (WellFormedSExpr atom)
forall atom. Read atom => ReadS [WellFormedSExpr atom]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [WellFormedSExpr atom]
$creadListPrec :: forall atom. Read atom => ReadPrec [WellFormedSExpr atom]
readPrec :: ReadPrec (WellFormedSExpr atom)
$creadPrec :: forall atom. Read atom => ReadPrec (WellFormedSExpr atom)
readList :: ReadS [WellFormedSExpr atom]
$creadList :: forall atom. Read atom => ReadS [WellFormedSExpr atom]
readsPrec :: Int -> ReadS (WellFormedSExpr atom)
$creadsPrec :: forall atom. Read atom => Int -> ReadS (WellFormedSExpr atom)
Read, forall a b. a -> WellFormedSExpr b -> WellFormedSExpr a
forall a b. (a -> b) -> WellFormedSExpr a -> WellFormedSExpr b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> WellFormedSExpr b -> WellFormedSExpr a
$c<$ :: forall a b. a -> WellFormedSExpr b -> WellFormedSExpr a
fmap :: forall a b. (a -> b) -> WellFormedSExpr a -> WellFormedSExpr b
$cfmap :: forall a b. (a -> b) -> WellFormedSExpr a -> WellFormedSExpr b
Functor, WellFormedSExpr atom -> DataType
WellFormedSExpr atom -> Constr
forall {atom}. Data atom => Typeable (WellFormedSExpr atom)
forall atom. Data atom => WellFormedSExpr atom -> DataType
forall atom. Data atom => WellFormedSExpr atom -> Constr
forall atom.
Data atom =>
(forall b. Data b => b -> b)
-> WellFormedSExpr atom -> WellFormedSExpr atom
forall atom u.
Data atom =>
Int -> (forall d. Data d => d -> u) -> WellFormedSExpr atom -> u
forall atom u.
Data atom =>
(forall d. Data d => d -> u) -> WellFormedSExpr atom -> [u]
forall atom r r'.
Data atom =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> WellFormedSExpr atom -> r
forall atom r r'.
Data atom =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> WellFormedSExpr atom -> r
forall atom (m :: * -> *).
(Data atom, Monad m) =>
(forall d. Data d => d -> m d)
-> WellFormedSExpr atom -> m (WellFormedSExpr atom)
forall atom (m :: * -> *).
(Data atom, MonadPlus m) =>
(forall d. Data d => d -> m d)
-> WellFormedSExpr atom -> m (WellFormedSExpr atom)
forall atom (c :: * -> *).
Data atom =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (WellFormedSExpr atom)
forall atom (c :: * -> *).
Data atom =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> WellFormedSExpr atom
-> c (WellFormedSExpr atom)
forall atom (t :: * -> *) (c :: * -> *).
(Data atom, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (WellFormedSExpr atom))
forall atom (t :: * -> * -> *) (c :: * -> *).
(Data atom, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (WellFormedSExpr atom))
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (WellFormedSExpr atom)
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> WellFormedSExpr atom
-> c (WellFormedSExpr atom)
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (WellFormedSExpr atom))
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> WellFormedSExpr atom -> m (WellFormedSExpr atom)
$cgmapMo :: forall atom (m :: * -> *).
(Data atom, MonadPlus m) =>
(forall d. Data d => d -> m d)
-> WellFormedSExpr atom -> m (WellFormedSExpr atom)
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> WellFormedSExpr atom -> m (WellFormedSExpr atom)
$cgmapMp :: forall atom (m :: * -> *).
(Data atom, MonadPlus m) =>
(forall d. Data d => d -> m d)
-> WellFormedSExpr atom -> m (WellFormedSExpr atom)
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> WellFormedSExpr atom -> m (WellFormedSExpr atom)
$cgmapM :: forall atom (m :: * -> *).
(Data atom, Monad m) =>
(forall d. Data d => d -> m d)
-> WellFormedSExpr atom -> m (WellFormedSExpr atom)
gmapQi :: forall u.
Int -> (forall d. Data d => d -> u) -> WellFormedSExpr atom -> u
$cgmapQi :: forall atom u.
Data atom =>
Int -> (forall d. Data d => d -> u) -> WellFormedSExpr atom -> u
gmapQ :: forall u.
(forall d. Data d => d -> u) -> WellFormedSExpr atom -> [u]
$cgmapQ :: forall atom u.
Data atom =>
(forall d. Data d => d -> u) -> WellFormedSExpr atom -> [u]
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> WellFormedSExpr atom -> r
$cgmapQr :: forall atom r r'.
Data atom =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> WellFormedSExpr atom -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> WellFormedSExpr atom -> r
$cgmapQl :: forall atom r r'.
Data atom =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> WellFormedSExpr atom -> r
gmapT :: (forall b. Data b => b -> b)
-> WellFormedSExpr atom -> WellFormedSExpr atom
$cgmapT :: forall atom.
Data atom =>
(forall b. Data b => b -> b)
-> WellFormedSExpr atom -> WellFormedSExpr atom
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (WellFormedSExpr atom))
$cdataCast2 :: forall atom (t :: * -> * -> *) (c :: * -> *).
(Data atom, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (WellFormedSExpr atom))
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (WellFormedSExpr atom))
$cdataCast1 :: forall atom (t :: * -> *) (c :: * -> *).
(Data atom, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (WellFormedSExpr atom))
dataTypeOf :: WellFormedSExpr atom -> DataType
$cdataTypeOf :: forall atom. Data atom => WellFormedSExpr atom -> DataType
toConstr :: WellFormedSExpr atom -> Constr
$ctoConstr :: forall atom. Data atom => WellFormedSExpr atom -> Constr
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (WellFormedSExpr atom)
$cgunfold :: forall atom (c :: * -> *).
Data atom =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (WellFormedSExpr atom)
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> WellFormedSExpr atom
-> c (WellFormedSExpr atom)
$cgfoldl :: forall atom (c :: * -> *).
Data atom =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> WellFormedSExpr atom
-> c (WellFormedSExpr atom)
Data, Typeable, forall a. Eq a => a -> WellFormedSExpr a -> Bool
forall a. Num a => WellFormedSExpr a -> a
forall a. Ord a => WellFormedSExpr a -> a
forall m. Monoid m => WellFormedSExpr m -> m
forall a. WellFormedSExpr a -> Bool
forall a. WellFormedSExpr a -> Int
forall a. WellFormedSExpr a -> [a]
forall a. (a -> a -> a) -> WellFormedSExpr a -> a
forall m a. Monoid m => (a -> m) -> WellFormedSExpr a -> m
forall b a. (b -> a -> b) -> b -> WellFormedSExpr a -> b
forall a b. (a -> b -> b) -> b -> WellFormedSExpr a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
product :: forall a. Num a => WellFormedSExpr a -> a
$cproduct :: forall a. Num a => WellFormedSExpr a -> a
sum :: forall a. Num a => WellFormedSExpr a -> a
$csum :: forall a. Num a => WellFormedSExpr a -> a
minimum :: forall a. Ord a => WellFormedSExpr a -> a
$cminimum :: forall a. Ord a => WellFormedSExpr a -> a
maximum :: forall a. Ord a => WellFormedSExpr a -> a
$cmaximum :: forall a. Ord a => WellFormedSExpr a -> a
elem :: forall a. Eq a => a -> WellFormedSExpr a -> Bool
$celem :: forall a. Eq a => a -> WellFormedSExpr a -> Bool
length :: forall a. WellFormedSExpr a -> Int
$clength :: forall a. WellFormedSExpr a -> Int
null :: forall a. WellFormedSExpr a -> Bool
$cnull :: forall a. WellFormedSExpr a -> Bool
toList :: forall a. WellFormedSExpr a -> [a]
$ctoList :: forall a. WellFormedSExpr a -> [a]
foldl1 :: forall a. (a -> a -> a) -> WellFormedSExpr a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> WellFormedSExpr a -> a
foldr1 :: forall a. (a -> a -> a) -> WellFormedSExpr a -> a
$cfoldr1 :: forall a. (a -> a -> a) -> WellFormedSExpr a -> a
foldl' :: forall b a. (b -> a -> b) -> b -> WellFormedSExpr a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> WellFormedSExpr a -> b
foldl :: forall b a. (b -> a -> b) -> b -> WellFormedSExpr a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> WellFormedSExpr a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> WellFormedSExpr a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> WellFormedSExpr a -> b
foldr :: forall a b. (a -> b -> b) -> b -> WellFormedSExpr a -> b
$cfoldr :: forall a b. (a -> b -> b) -> b -> WellFormedSExpr a -> b
foldMap' :: forall m a. Monoid m => (a -> m) -> WellFormedSExpr a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> WellFormedSExpr a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> WellFormedSExpr a -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> WellFormedSExpr a -> m
fold :: forall m. Monoid m => WellFormedSExpr m -> m
$cfold :: forall m. Monoid m => WellFormedSExpr m -> m
Foldable, Functor WellFormedSExpr
Foldable WellFormedSExpr
forall (t :: * -> *).
Functor t
-> Foldable t
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a.
Monad m =>
WellFormedSExpr (m a) -> m (WellFormedSExpr a)
forall (f :: * -> *) a.
Applicative f =>
WellFormedSExpr (f a) -> f (WellFormedSExpr a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> WellFormedSExpr a -> m (WellFormedSExpr b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> WellFormedSExpr a -> f (WellFormedSExpr b)
sequence :: forall (m :: * -> *) a.
Monad m =>
WellFormedSExpr (m a) -> m (WellFormedSExpr a)
$csequence :: forall (m :: * -> *) a.
Monad m =>
WellFormedSExpr (m a) -> m (WellFormedSExpr a)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> WellFormedSExpr a -> m (WellFormedSExpr b)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> WellFormedSExpr a -> m (WellFormedSExpr b)
sequenceA :: forall (f :: * -> *) a.
Applicative f =>
WellFormedSExpr (f a) -> f (WellFormedSExpr a)
$csequenceA :: forall (f :: * -> *) a.
Applicative f =>
WellFormedSExpr (f a) -> f (WellFormedSExpr a)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> WellFormedSExpr a -> f (WellFormedSExpr b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> WellFormedSExpr a -> f (WellFormedSExpr b)
Traversable)

instance IsList (WellFormedSExpr atom) where
  type Item (WellFormedSExpr atom) = WellFormedSExpr atom
  fromList :: [Item (WellFormedSExpr atom)] -> WellFormedSExpr atom
fromList = forall atom. [WellFormedSExpr atom] -> WellFormedSExpr atom
WFSList
  toList :: WellFormedSExpr atom -> [Item (WellFormedSExpr atom)]
toList (WFSList [WellFormedSExpr atom]
xs) = [WellFormedSExpr atom]
xs
  toList (WFSAtom {}) = forall a. HasCallStack => String -> a
error String
"Unable to turn atom into Haskell list"

instance IsString atom => IsString (WellFormedSExpr atom) where
  fromString :: String -> WellFormedSExpr atom
fromString = forall atom. atom -> WellFormedSExpr atom
WFSAtom forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. IsString a => String -> a
fromString

-- | This will be @Nothing@ if the argument contains an
--   improper list. It should hold that
--
--   > toWellFormed (fromWellFormed x) == Right x
--
--   and also (more tediously) that
--
--   > case toWellFormed x of
--   >   Left _  -> True
--   >   Right y -> x == fromWellFormed y
toWellFormed :: SExpr atom -> Either String (WellFormedSExpr atom)
toWellFormed :: forall atom. SExpr atom -> Either String (WellFormedSExpr atom)
toWellFormed SExpr atom
SNil      = forall (m :: * -> *) a. Monad m => a -> m a
return (forall atom. [WellFormedSExpr atom] -> WellFormedSExpr atom
WFSList [])
toWellFormed (SAtom atom
a) = forall (m :: * -> *) a. Monad m => a -> m a
return (forall atom. atom -> WellFormedSExpr atom
WFSAtom atom
a)
toWellFormed (SCons SExpr atom
x SExpr atom
xs) = do
  WellFormedSExpr atom
x' <- forall atom. SExpr atom -> Either String (WellFormedSExpr atom)
toWellFormed SExpr atom
x
  forall {atom} {atom}.
SExpr atom
-> ([WellFormedSExpr atom] -> [WellFormedSExpr atom])
-> Either String (WellFormedSExpr atom)
go SExpr atom
xs (WellFormedSExpr atom
x'forall a. a -> [a] -> [a]
:)
  where go :: SExpr atom
-> ([WellFormedSExpr atom] -> [WellFormedSExpr atom])
-> Either String (WellFormedSExpr atom)
go (SAtom atom
_) [WellFormedSExpr atom] -> [WellFormedSExpr atom]
_  = forall a b. a -> Either a b
Left String
"Found atom in cdr position"
        go SExpr atom
SNil [WellFormedSExpr atom] -> [WellFormedSExpr atom]
rs      = forall (m :: * -> *) a. Monad m => a -> m a
return (forall atom. [WellFormedSExpr atom] -> WellFormedSExpr atom
WFSList ([WellFormedSExpr atom] -> [WellFormedSExpr atom]
rs []))
        go (SCons SExpr atom
y SExpr atom
ys) [WellFormedSExpr atom] -> [WellFormedSExpr atom]
rs = do
          WellFormedSExpr atom
y' <- forall atom. SExpr atom -> Either String (WellFormedSExpr atom)
toWellFormed SExpr atom
y
          SExpr atom
-> ([WellFormedSExpr atom] -> [WellFormedSExpr atom])
-> Either String (WellFormedSExpr atom)
go SExpr atom
ys ([WellFormedSExpr atom] -> [WellFormedSExpr atom]
rs forall b c a. (b -> c) -> (a -> b) -> a -> c
. (WellFormedSExpr atom
y'forall a. a -> [a] -> [a]
:))

-- | Convert a WellFormedSExpr back into a SExpr.
fromWellFormed :: WellFormedSExpr atom -> SExpr atom
fromWellFormed :: forall atom. WellFormedSExpr atom -> SExpr atom
fromWellFormed (WFSAtom atom
a)  = forall atom. atom -> SExpr atom
SAtom atom
a
fromWellFormed (WFSList [WellFormedSExpr atom]
xs) =
  forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr forall atom. SExpr atom -> SExpr atom -> SExpr atom
SCons forall atom. SExpr atom
SNil (forall a b. (a -> b) -> [a] -> [b]
map forall atom. WellFormedSExpr atom -> SExpr atom
fromWellFormed [WellFormedSExpr atom]
xs)

{- $reprs

This module contains several different representations for
s-expressions. The s-cargot library underlying uses the
'SExpr' type as its representation type, which is a binary
tree representation with an arbitrary type for its leaves.

This type is not always convenient to manipulate in Haskell
code, this module defines two alternate representations
which turn a sequence of nested right-branching cons pairs
into Haskell lists: that is to say, they transform between

@
SCons a (SCons b (SCons c SNil))  \<=\>  RSList [a, b, c]
@

These two types differ in how they handle non-well-formed
lists, i.e. lists that end with an atom. The 'RichSExpr'
format handles this with a special constructor for lists
that end in an atom:

@
SCons a (SCons b (SAtom c))  \<=\>  RSDotted [a, b] c
@

On the other hand, the 'WellFormedSExpr' type elects
not to handle this case. This is unusual for Lisp source code,
but is a reasonable choice for configuration or data
storage formats that use s-expressions, where
non-well-formed lists would be an unnecessary
complication.

To make working with these types less verbose, there are other
modules that export pattern aliases and helper functions: these
can be found at "Data.SCargot.Repr.Basic",
"Data.SCargot.Repr.Rich", and "Data.SCargot.Repr.WellFormed".
-}