{-# 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
(SExpr atom -> SExpr atom -> Bool)
-> (SExpr atom -> SExpr atom -> Bool) -> Eq (SExpr atom)
forall atom. Eq atom => SExpr atom -> SExpr atom -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$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
/= :: SExpr atom -> SExpr atom -> Bool
Eq, Int -> SExpr atom -> ShowS
[SExpr atom] -> ShowS
SExpr atom -> String
(Int -> SExpr atom -> ShowS)
-> (SExpr atom -> String)
-> ([SExpr atom] -> ShowS)
-> Show (SExpr atom)
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
$cshowsPrec :: forall atom. Show atom => Int -> SExpr atom -> ShowS
showsPrec :: Int -> SExpr atom -> ShowS
$cshow :: forall atom. Show atom => SExpr atom -> String
show :: SExpr atom -> String
$cshowList :: forall atom. Show atom => [SExpr atom] -> ShowS
showList :: [SExpr atom] -> ShowS
Show, ReadPrec [SExpr atom]
ReadPrec (SExpr atom)
Int -> ReadS (SExpr atom)
ReadS [SExpr atom]
(Int -> ReadS (SExpr atom))
-> ReadS [SExpr atom]
-> ReadPrec (SExpr atom)
-> ReadPrec [SExpr atom]
-> Read (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
$creadsPrec :: forall atom. Read atom => Int -> ReadS (SExpr atom)
readsPrec :: Int -> ReadS (SExpr atom)
$creadList :: forall atom. Read atom => ReadS [SExpr atom]
readList :: ReadS [SExpr atom]
$creadPrec :: forall atom. Read atom => ReadPrec (SExpr atom)
readPrec :: ReadPrec (SExpr atom)
$creadListPrec :: forall atom. Read atom => ReadPrec [SExpr atom]
readListPrec :: ReadPrec [SExpr atom]
Read, (forall a b. (a -> b) -> SExpr a -> SExpr b)
-> (forall a b. a -> SExpr b -> SExpr a) -> Functor SExpr
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
$cfmap :: forall a b. (a -> b) -> SExpr a -> SExpr b
fmap :: forall a b. (a -> b) -> SExpr a -> SExpr b
$c<$ :: forall a b. a -> SExpr b -> SExpr a
<$ :: forall a b. a -> SExpr b -> SExpr a
Functor, Typeable (SExpr atom)
Typeable (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 (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c (SExpr atom))
-> (SExpr atom -> Constr)
-> (SExpr atom -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c (SExpr atom)))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c (SExpr atom)))
-> ((forall b. Data b => b -> b) -> SExpr atom -> SExpr atom)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> SExpr atom -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> SExpr atom -> r)
-> (forall u. (forall d. Data d => d -> u) -> SExpr atom -> [u])
-> (forall u.
    Int -> (forall d. Data d => d -> u) -> SExpr atom -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> SExpr atom -> m (SExpr atom))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> SExpr atom -> m (SExpr atom))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> SExpr atom -> m (SExpr atom))
-> Data (SExpr atom)
SExpr atom -> Constr
SExpr atom -> DataType
(forall b. Data b => b -> b) -> SExpr atom -> SExpr atom
forall {atom}. Data atom => Typeable (SExpr atom)
forall atom. Data atom => SExpr atom -> Constr
forall atom. Data atom => SExpr atom -> DataType
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 u. Int -> (forall d. Data d => d -> u) -> SExpr atom -> u
forall u. (forall d. Data d => d -> u) -> SExpr atom -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> SExpr atom -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> SExpr atom -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> SExpr atom -> m (SExpr atom)
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> SExpr atom -> m (SExpr atom)
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))
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (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)
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> SExpr atom -> 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)
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (SExpr atom)
$ctoConstr :: forall atom. Data atom => SExpr atom -> Constr
toConstr :: SExpr atom -> Constr
$cdataTypeOf :: forall atom. Data atom => SExpr atom -> DataType
dataTypeOf :: SExpr atom -> DataType
$cdataCast1 :: forall atom (t :: * -> *) (c :: * -> *).
(Data atom, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (SExpr atom))
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> 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))
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (SExpr atom))
$cgmapT :: forall atom.
Data atom =>
(forall b. Data b => b -> b) -> SExpr atom -> SExpr atom
gmapT :: (forall b. Data b => b -> b) -> SExpr atom -> SExpr atom
$cgmapQl :: 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
$cgmapQr :: forall atom r r'.
Data atom =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> SExpr atom -> r
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> SExpr atom -> r
$cgmapQ :: forall atom u.
Data atom =>
(forall d. Data d => d -> u) -> SExpr atom -> [u]
gmapQ :: forall u. (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
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> SExpr atom -> u
$cgmapM :: forall atom (m :: * -> *).
(Data atom, Monad 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)
$cgmapMp :: 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)
$cgmapMo :: forall atom (m :: * -> *).
(Data atom, MonadPlus m) =>
(forall d. Data d => d -> m d) -> SExpr atom -> m (SExpr atom)
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> SExpr atom -> m (SExpr atom)
Data, Typeable, (forall m. Monoid m => SExpr m -> m)
-> (forall m a. Monoid m => (a -> m) -> SExpr a -> m)
-> (forall m a. Monoid m => (a -> m) -> SExpr a -> m)
-> (forall a b. (a -> b -> b) -> b -> SExpr a -> b)
-> (forall a b. (a -> b -> b) -> b -> SExpr a -> b)
-> (forall b a. (b -> a -> b) -> b -> SExpr a -> b)
-> (forall b a. (b -> a -> b) -> b -> SExpr a -> b)
-> (forall a. (a -> a -> a) -> SExpr a -> a)
-> (forall a. (a -> a -> a) -> SExpr a -> a)
-> (forall a. SExpr a -> [a])
-> (forall a. SExpr a -> Bool)
-> (forall a. SExpr a -> Int)
-> (forall a. Eq a => a -> SExpr a -> Bool)
-> (forall a. Ord a => SExpr a -> a)
-> (forall a. Ord a => SExpr a -> a)
-> (forall a. Num a => SExpr a -> a)
-> (forall a. Num a => SExpr a -> a)
-> Foldable SExpr
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
$cfold :: forall m. Monoid m => SExpr m -> m
fold :: forall m. Monoid m => SExpr m -> 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
foldMap' :: forall m a. Monoid m => (a -> m) -> SExpr a -> m
$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
foldr' :: forall a b. (a -> b -> 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
foldl' :: forall b a. (b -> a -> b) -> b -> SExpr a -> b
$cfoldr1 :: forall a. (a -> a -> a) -> SExpr a -> a
foldr1 :: forall a. (a -> a -> a) -> SExpr a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> SExpr a -> a
foldl1 :: forall a. (a -> a -> a) -> SExpr a -> a
$ctoList :: forall a. SExpr a -> [a]
toList :: forall a. SExpr a -> [a]
$cnull :: forall a. SExpr a -> Bool
null :: forall a. SExpr a -> Bool
$clength :: forall a. SExpr a -> Int
length :: forall a. SExpr a -> Int
$celem :: forall a. Eq a => a -> SExpr a -> Bool
elem :: forall a. Eq a => a -> SExpr a -> Bool
$cmaximum :: forall a. Ord a => SExpr a -> a
maximum :: forall a. Ord a => SExpr a -> a
$cminimum :: forall a. Ord a => SExpr a -> a
minimum :: forall a. Ord a => SExpr a -> a
$csum :: forall a. Num a => SExpr a -> a
sum :: forall a. Num a => SExpr a -> a
$cproduct :: forall a. Num a => SExpr a -> a
product :: forall a. Num a => SExpr a -> a
Foldable, Functor SExpr
Foldable SExpr
Functor SExpr
-> Foldable SExpr
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> SExpr a -> f (SExpr b))
-> (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 (m :: * -> *) a. Monad m => SExpr (m a) -> m (SExpr a))
-> Traversable 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)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> SExpr a -> f (SExpr b)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> SExpr a -> f (SExpr b)
$csequenceA :: forall (f :: * -> *) a. Applicative f => SExpr (f a) -> f (SExpr a)
sequenceA :: forall (f :: * -> *) a. Applicative f => SExpr (f a) -> f (SExpr a)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> SExpr a -> m (SExpr b)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> SExpr a -> m (SExpr b)
$csequence :: forall (m :: * -> *) a. Monad m => SExpr (m a) -> m (SExpr a)
sequence :: forall (m :: * -> *) a. Monad m => SExpr (m a) -> m (SExpr a)
Traversable)

instance IsString atom => IsString (SExpr atom) where
  fromString :: String -> SExpr atom
fromString = atom -> SExpr atom
forall atom. atom -> SExpr atom
SAtom (atom -> SExpr atom) -> (String -> atom) -> String -> SExpr atom
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> atom
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 = (SExpr atom -> SExpr atom -> SExpr atom)
-> SExpr atom -> [SExpr atom] -> SExpr atom
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr SExpr atom -> SExpr atom -> SExpr atom
forall atom. SExpr atom -> SExpr atom -> SExpr atom
SCons SExpr atom
forall atom. SExpr atom
SNil
  toList :: SExpr atom -> [Item (SExpr atom)]
toList   = SExpr atom -> [Item (SExpr atom)]
SExpr atom -> [SExpr atom]
forall {atom}. SExpr atom -> [SExpr atom]
go
    where go :: SExpr atom -> [SExpr atom]
go (SCons SExpr atom
x SExpr atom
xs) = SExpr atom
x SExpr atom -> [SExpr atom] -> [SExpr atom]
forall a. a -> [a] -> [a]
: SExpr atom -> [SExpr atom]
go SExpr atom
xs
          go SExpr atom
SNil         = []
          go (SAtom {})   = String -> [SExpr atom]
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
(RichSExpr atom -> RichSExpr atom -> Bool)
-> (RichSExpr atom -> RichSExpr atom -> Bool)
-> Eq (RichSExpr atom)
forall atom. Eq atom => RichSExpr atom -> RichSExpr atom -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$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
/= :: RichSExpr atom -> RichSExpr atom -> Bool
Eq, Int -> RichSExpr atom -> ShowS
[RichSExpr atom] -> ShowS
RichSExpr atom -> String
(Int -> RichSExpr atom -> ShowS)
-> (RichSExpr atom -> String)
-> ([RichSExpr atom] -> ShowS)
-> Show (RichSExpr atom)
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
$cshowsPrec :: forall atom. Show atom => Int -> RichSExpr atom -> ShowS
showsPrec :: Int -> RichSExpr atom -> ShowS
$cshow :: forall atom. Show atom => RichSExpr atom -> String
show :: RichSExpr atom -> String
$cshowList :: forall atom. Show atom => [RichSExpr atom] -> ShowS
showList :: [RichSExpr atom] -> ShowS
Show, ReadPrec [RichSExpr atom]
ReadPrec (RichSExpr atom)
Int -> ReadS (RichSExpr atom)
ReadS [RichSExpr atom]
(Int -> ReadS (RichSExpr atom))
-> ReadS [RichSExpr atom]
-> ReadPrec (RichSExpr atom)
-> ReadPrec [RichSExpr atom]
-> Read (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
$creadsPrec :: forall atom. Read atom => Int -> ReadS (RichSExpr atom)
readsPrec :: Int -> ReadS (RichSExpr atom)
$creadList :: forall atom. Read atom => ReadS [RichSExpr atom]
readList :: ReadS [RichSExpr atom]
$creadPrec :: forall atom. Read atom => ReadPrec (RichSExpr atom)
readPrec :: ReadPrec (RichSExpr atom)
$creadListPrec :: forall atom. Read atom => ReadPrec [RichSExpr atom]
readListPrec :: ReadPrec [RichSExpr atom]
Read, (forall a b. (a -> b) -> RichSExpr a -> RichSExpr b)
-> (forall a b. a -> RichSExpr b -> RichSExpr a)
-> Functor RichSExpr
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
$cfmap :: forall a b. (a -> b) -> RichSExpr a -> RichSExpr b
fmap :: forall a b. (a -> b) -> RichSExpr a -> RichSExpr b
$c<$ :: forall a b. a -> RichSExpr b -> RichSExpr a
<$ :: forall a b. a -> RichSExpr b -> RichSExpr a
Functor, Typeable (RichSExpr atom)
Typeable (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 (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c (RichSExpr atom))
-> (RichSExpr atom -> Constr)
-> (RichSExpr atom -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c (RichSExpr atom)))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c (RichSExpr atom)))
-> ((forall b. Data b => b -> b)
    -> RichSExpr atom -> RichSExpr atom)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> RichSExpr atom -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> RichSExpr atom -> r)
-> (forall u.
    (forall d. Data d => d -> u) -> RichSExpr atom -> [u])
-> (forall u.
    Int -> (forall d. Data d => d -> u) -> RichSExpr atom -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d)
    -> RichSExpr atom -> m (RichSExpr atom))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> RichSExpr atom -> m (RichSExpr atom))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> RichSExpr atom -> m (RichSExpr atom))
-> Data (RichSExpr atom)
RichSExpr atom -> Constr
RichSExpr atom -> DataType
(forall b. Data b => b -> b) -> RichSExpr atom -> RichSExpr atom
forall {atom}. Data atom => Typeable (RichSExpr atom)
forall atom. Data atom => RichSExpr atom -> Constr
forall atom. Data atom => RichSExpr atom -> DataType
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 u.
Int -> (forall d. Data d => d -> u) -> RichSExpr atom -> u
forall u. (forall d. Data d => d -> u) -> RichSExpr atom -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> RichSExpr atom -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> RichSExpr atom -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> RichSExpr atom -> m (RichSExpr atom)
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> RichSExpr atom -> m (RichSExpr atom)
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))
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (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)
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> RichSExpr atom -> 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)
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (RichSExpr atom)
$ctoConstr :: forall atom. Data atom => RichSExpr atom -> Constr
toConstr :: RichSExpr atom -> Constr
$cdataTypeOf :: forall atom. Data atom => RichSExpr atom -> DataType
dataTypeOf :: RichSExpr atom -> DataType
$cdataCast1 :: forall atom (t :: * -> *) (c :: * -> *).
(Data atom, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (RichSExpr atom))
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> 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))
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (RichSExpr atom))
$cgmapT :: forall atom.
Data atom =>
(forall b. Data b => b -> b) -> RichSExpr atom -> RichSExpr atom
gmapT :: (forall b. Data b => b -> b) -> RichSExpr atom -> RichSExpr atom
$cgmapQl :: 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
$cgmapQr :: forall atom r r'.
Data atom =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> RichSExpr atom -> r
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> RichSExpr atom -> r
$cgmapQ :: forall atom u.
Data atom =>
(forall d. Data d => d -> u) -> RichSExpr atom -> [u]
gmapQ :: forall u. (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
gmapQi :: forall u.
Int -> (forall d. Data d => d -> u) -> RichSExpr atom -> u
$cgmapM :: forall atom (m :: * -> *).
(Data atom, Monad 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)
$cgmapMp :: 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)
$cgmapMo :: forall atom (m :: * -> *).
(Data atom, MonadPlus m) =>
(forall d. Data d => d -> m d)
-> RichSExpr atom -> m (RichSExpr atom)
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> RichSExpr atom -> m (RichSExpr atom)
Data, Typeable, (forall m. Monoid m => RichSExpr m -> m)
-> (forall m a. Monoid m => (a -> m) -> RichSExpr a -> m)
-> (forall m a. Monoid m => (a -> m) -> RichSExpr a -> m)
-> (forall a b. (a -> b -> b) -> b -> RichSExpr a -> b)
-> (forall a b. (a -> b -> b) -> b -> RichSExpr a -> b)
-> (forall b a. (b -> a -> b) -> b -> RichSExpr a -> b)
-> (forall b a. (b -> a -> b) -> b -> RichSExpr a -> b)
-> (forall a. (a -> a -> a) -> RichSExpr a -> a)
-> (forall a. (a -> a -> a) -> RichSExpr a -> a)
-> (forall a. RichSExpr a -> [a])
-> (forall a. RichSExpr a -> Bool)
-> (forall a. RichSExpr a -> Int)
-> (forall a. Eq a => a -> RichSExpr a -> Bool)
-> (forall a. Ord a => RichSExpr a -> a)
-> (forall a. Ord a => RichSExpr a -> a)
-> (forall a. Num a => RichSExpr a -> a)
-> (forall a. Num a => RichSExpr a -> a)
-> Foldable RichSExpr
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
$cfold :: forall m. Monoid m => RichSExpr m -> m
fold :: forall m. Monoid m => RichSExpr m -> 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
foldMap' :: forall m a. Monoid m => (a -> m) -> RichSExpr a -> m
$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
foldr' :: forall a b. (a -> b -> 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
foldl' :: forall b a. (b -> a -> b) -> b -> RichSExpr a -> b
$cfoldr1 :: forall a. (a -> a -> a) -> RichSExpr a -> a
foldr1 :: forall a. (a -> a -> a) -> RichSExpr a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> RichSExpr a -> a
foldl1 :: forall a. (a -> a -> a) -> RichSExpr a -> a
$ctoList :: forall a. RichSExpr a -> [a]
toList :: forall a. RichSExpr a -> [a]
$cnull :: forall a. RichSExpr a -> Bool
null :: forall a. RichSExpr a -> Bool
$clength :: forall a. RichSExpr a -> Int
length :: forall a. RichSExpr a -> Int
$celem :: forall a. Eq a => a -> RichSExpr a -> Bool
elem :: forall a. Eq a => a -> RichSExpr a -> Bool
$cmaximum :: forall a. Ord a => RichSExpr a -> a
maximum :: forall a. Ord a => RichSExpr a -> a
$cminimum :: forall a. Ord a => RichSExpr a -> a
minimum :: forall a. Ord a => RichSExpr a -> a
$csum :: forall a. Num a => RichSExpr a -> a
sum :: forall a. Num a => RichSExpr a -> a
$cproduct :: forall a. Num a => RichSExpr a -> a
product :: forall a. Num a => RichSExpr a -> a
Foldable, Functor RichSExpr
Foldable RichSExpr
Functor RichSExpr
-> Foldable RichSExpr
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> RichSExpr a -> f (RichSExpr b))
-> (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 (m :: * -> *) a.
    Monad m =>
    RichSExpr (m a) -> m (RichSExpr a))
-> Traversable 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)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> RichSExpr a -> f (RichSExpr b)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> RichSExpr a -> f (RichSExpr b)
$csequenceA :: forall (f :: * -> *) a.
Applicative f =>
RichSExpr (f a) -> f (RichSExpr a)
sequenceA :: forall (f :: * -> *) a.
Applicative f =>
RichSExpr (f a) -> f (RichSExpr a)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> RichSExpr a -> m (RichSExpr b)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> RichSExpr a -> m (RichSExpr b)
$csequence :: forall (m :: * -> *) a.
Monad m =>
RichSExpr (m a) -> m (RichSExpr a)
sequence :: forall (m :: * -> *) a.
Monad m =>
RichSExpr (m a) -> m (RichSExpr a)
Traversable)

instance IsString atom => IsString (RichSExpr atom) where
  fromString :: String -> RichSExpr atom
fromString = atom -> RichSExpr atom
forall atom. atom -> RichSExpr atom
RSAtom (atom -> RichSExpr atom)
-> (String -> atom) -> String -> RichSExpr atom
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> atom
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 = [Item (RichSExpr atom)] -> RichSExpr atom
[RichSExpr atom] -> RichSExpr atom
forall atom. [RichSExpr atom] -> RichSExpr atom
RSList
  toList :: RichSExpr atom -> [Item (RichSExpr atom)]
toList (RSList [RichSExpr atom]
xs)   = [Item (RichSExpr atom)]
[RichSExpr atom]
xs
  toList (RSDotted {}) = String -> [RichSExpr atom]
forall a. HasCallStack => String -> a
error String
"Unable to turn dotted list into haskell list"
  toList (RSAtom {})   = String -> [RichSExpr atom]
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) = atom -> RichSExpr atom
forall atom. atom -> RichSExpr atom
RSAtom atom
a
toRich (SCons SExpr atom
x SExpr atom
xs) = SExpr atom
-> ([RichSExpr atom] -> [RichSExpr atom]) -> RichSExpr atom
forall {atom}.
SExpr atom
-> ([RichSExpr atom] -> [RichSExpr atom]) -> RichSExpr atom
go SExpr atom
xs (SExpr atom -> RichSExpr atom
forall atom. SExpr atom -> RichSExpr atom
toRich SExpr atom
xRichSExpr atom -> [RichSExpr atom] -> [RichSExpr atom]
forall a. a -> [a] -> [a]
:)
  where go :: SExpr atom
-> ([RichSExpr atom] -> [RichSExpr atom]) -> RichSExpr atom
go (SAtom atom
a) [RichSExpr atom] -> [RichSExpr atom]
rs    = [RichSExpr atom] -> atom -> RichSExpr atom
forall atom. [RichSExpr atom] -> atom -> RichSExpr atom
RSDotted ([RichSExpr atom] -> [RichSExpr atom]
rs []) atom
a
        go SExpr atom
SNil [RichSExpr atom] -> [RichSExpr atom]
rs         = [RichSExpr atom] -> RichSExpr atom
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 ([RichSExpr atom] -> [RichSExpr atom])
-> ([RichSExpr atom] -> [RichSExpr atom])
-> [RichSExpr atom]
-> [RichSExpr atom]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (SExpr atom -> RichSExpr atom
forall atom. SExpr atom -> RichSExpr atom
toRich SExpr atom
yRichSExpr atom -> [RichSExpr atom] -> [RichSExpr atom]
forall a. a -> [a] -> [a]
:))
toRich SExpr atom
SNil = [RichSExpr atom] -> RichSExpr atom
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) = atom -> SExpr atom
forall atom. atom -> SExpr atom
SAtom atom
a
fromRich (RSList [RichSExpr atom]
xs) = (SExpr atom -> SExpr atom -> SExpr atom)
-> SExpr atom -> [SExpr atom] -> SExpr atom
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr SExpr atom -> SExpr atom -> SExpr atom
forall atom. SExpr atom -> SExpr atom -> SExpr atom
SCons SExpr atom
forall atom. SExpr atom
SNil ((RichSExpr atom -> SExpr atom) -> [RichSExpr atom] -> [SExpr atom]
forall a b. (a -> b) -> [a] -> [b]
map RichSExpr atom -> SExpr atom
forall atom. RichSExpr atom -> SExpr atom
fromRich [RichSExpr atom]
xs)
fromRich (RSDotted [RichSExpr atom]
xs atom
x) = (SExpr atom -> SExpr atom -> SExpr atom)
-> SExpr atom -> [SExpr atom] -> SExpr atom
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr SExpr atom -> SExpr atom -> SExpr atom
forall atom. SExpr atom -> SExpr atom -> SExpr atom
SCons (atom -> SExpr atom
forall atom. atom -> SExpr atom
SAtom atom
x) ((RichSExpr atom -> SExpr atom) -> [RichSExpr atom] -> [SExpr atom]
forall a b. (a -> b) -> [a] -> [b]
map RichSExpr atom -> SExpr atom
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
(WellFormedSExpr atom -> WellFormedSExpr atom -> Bool)
-> (WellFormedSExpr atom -> WellFormedSExpr atom -> Bool)
-> Eq (WellFormedSExpr atom)
forall atom.
Eq atom =>
WellFormedSExpr atom -> WellFormedSExpr atom -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$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
/= :: WellFormedSExpr atom -> WellFormedSExpr atom -> Bool
Eq, Int -> WellFormedSExpr atom -> ShowS
[WellFormedSExpr atom] -> ShowS
WellFormedSExpr atom -> String
(Int -> WellFormedSExpr atom -> ShowS)
-> (WellFormedSExpr atom -> String)
-> ([WellFormedSExpr atom] -> ShowS)
-> Show (WellFormedSExpr atom)
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
$cshowsPrec :: forall atom. Show atom => Int -> WellFormedSExpr atom -> ShowS
showsPrec :: Int -> WellFormedSExpr atom -> ShowS
$cshow :: forall atom. Show atom => WellFormedSExpr atom -> String
show :: WellFormedSExpr atom -> String
$cshowList :: forall atom. Show atom => [WellFormedSExpr atom] -> ShowS
showList :: [WellFormedSExpr atom] -> ShowS
Show, ReadPrec [WellFormedSExpr atom]
ReadPrec (WellFormedSExpr atom)
Int -> ReadS (WellFormedSExpr atom)
ReadS [WellFormedSExpr atom]
(Int -> ReadS (WellFormedSExpr atom))
-> ReadS [WellFormedSExpr atom]
-> ReadPrec (WellFormedSExpr atom)
-> ReadPrec [WellFormedSExpr atom]
-> Read (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
$creadsPrec :: forall atom. Read atom => Int -> ReadS (WellFormedSExpr atom)
readsPrec :: Int -> ReadS (WellFormedSExpr atom)
$creadList :: forall atom. Read atom => ReadS [WellFormedSExpr atom]
readList :: ReadS [WellFormedSExpr atom]
$creadPrec :: forall atom. Read atom => ReadPrec (WellFormedSExpr atom)
readPrec :: ReadPrec (WellFormedSExpr atom)
$creadListPrec :: forall atom. Read atom => ReadPrec [WellFormedSExpr atom]
readListPrec :: ReadPrec [WellFormedSExpr atom]
Read, (forall a b. (a -> b) -> WellFormedSExpr a -> WellFormedSExpr b)
-> (forall a b. a -> WellFormedSExpr b -> WellFormedSExpr a)
-> Functor WellFormedSExpr
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
$cfmap :: forall a b. (a -> b) -> WellFormedSExpr a -> WellFormedSExpr b
fmap :: forall a b. (a -> b) -> WellFormedSExpr a -> WellFormedSExpr b
$c<$ :: forall a b. a -> WellFormedSExpr b -> WellFormedSExpr a
<$ :: forall a b. a -> WellFormedSExpr b -> WellFormedSExpr a
Functor, Typeable (WellFormedSExpr atom)
Typeable (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 (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c (WellFormedSExpr atom))
-> (WellFormedSExpr atom -> Constr)
-> (WellFormedSExpr atom -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c (WellFormedSExpr atom)))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c (WellFormedSExpr atom)))
-> ((forall b. Data b => b -> b)
    -> WellFormedSExpr atom -> WellFormedSExpr atom)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> WellFormedSExpr atom -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> WellFormedSExpr atom -> r)
-> (forall u.
    (forall d. Data d => d -> u) -> WellFormedSExpr atom -> [u])
-> (forall u.
    Int -> (forall d. Data d => d -> u) -> WellFormedSExpr atom -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d)
    -> WellFormedSExpr atom -> m (WellFormedSExpr atom))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> WellFormedSExpr atom -> m (WellFormedSExpr atom))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> WellFormedSExpr atom -> m (WellFormedSExpr atom))
-> Data (WellFormedSExpr atom)
WellFormedSExpr atom -> Constr
WellFormedSExpr atom -> DataType
(forall b. Data b => b -> b)
-> WellFormedSExpr atom -> WellFormedSExpr atom
forall {atom}. Data atom => Typeable (WellFormedSExpr atom)
forall atom. Data atom => WellFormedSExpr atom -> Constr
forall atom. Data atom => WellFormedSExpr atom -> DataType
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 u.
Int -> (forall d. Data d => d -> u) -> WellFormedSExpr atom -> u
forall u.
(forall d. Data d => d -> u) -> WellFormedSExpr atom -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> WellFormedSExpr atom -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> WellFormedSExpr atom -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> WellFormedSExpr atom -> m (WellFormedSExpr atom)
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> WellFormedSExpr atom -> m (WellFormedSExpr atom)
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))
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (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)
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> WellFormedSExpr atom
-> 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)
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (WellFormedSExpr atom)
$ctoConstr :: forall atom. Data atom => WellFormedSExpr atom -> Constr
toConstr :: WellFormedSExpr atom -> Constr
$cdataTypeOf :: forall atom. Data atom => WellFormedSExpr atom -> DataType
dataTypeOf :: WellFormedSExpr atom -> DataType
$cdataCast1 :: forall atom (t :: * -> *) (c :: * -> *).
(Data atom, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (WellFormedSExpr atom))
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> 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))
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (WellFormedSExpr atom))
$cgmapT :: forall atom.
Data atom =>
(forall b. Data b => b -> b)
-> WellFormedSExpr atom -> WellFormedSExpr atom
gmapT :: (forall b. Data b => b -> b)
-> WellFormedSExpr atom -> WellFormedSExpr atom
$cgmapQl :: 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
$cgmapQr :: forall atom r r'.
Data atom =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> WellFormedSExpr atom -> r
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> WellFormedSExpr atom -> r
$cgmapQ :: forall atom u.
Data atom =>
(forall d. Data d => d -> u) -> WellFormedSExpr atom -> [u]
gmapQ :: forall u.
(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
gmapQi :: forall u.
Int -> (forall d. Data d => d -> u) -> WellFormedSExpr atom -> u
$cgmapM :: forall atom (m :: * -> *).
(Data atom, Monad 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)
$cgmapMp :: 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)
$cgmapMo :: forall atom (m :: * -> *).
(Data atom, MonadPlus m) =>
(forall d. Data d => d -> m d)
-> WellFormedSExpr atom -> m (WellFormedSExpr atom)
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> WellFormedSExpr atom -> m (WellFormedSExpr atom)
Data, Typeable, (forall m. Monoid m => WellFormedSExpr m -> m)
-> (forall m a. Monoid m => (a -> m) -> WellFormedSExpr a -> m)
-> (forall m a. Monoid m => (a -> m) -> WellFormedSExpr a -> m)
-> (forall a b. (a -> b -> b) -> b -> WellFormedSExpr a -> b)
-> (forall a b. (a -> b -> b) -> b -> WellFormedSExpr a -> b)
-> (forall b a. (b -> a -> b) -> b -> WellFormedSExpr a -> b)
-> (forall b a. (b -> a -> b) -> b -> WellFormedSExpr a -> b)
-> (forall a. (a -> a -> a) -> WellFormedSExpr a -> a)
-> (forall a. (a -> a -> a) -> WellFormedSExpr a -> a)
-> (forall a. WellFormedSExpr a -> [a])
-> (forall a. WellFormedSExpr a -> Bool)
-> (forall a. WellFormedSExpr a -> Int)
-> (forall a. Eq a => a -> WellFormedSExpr a -> Bool)
-> (forall a. Ord a => WellFormedSExpr a -> a)
-> (forall a. Ord a => WellFormedSExpr a -> a)
-> (forall a. Num a => WellFormedSExpr a -> a)
-> (forall a. Num a => WellFormedSExpr a -> a)
-> Foldable WellFormedSExpr
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
$cfold :: forall m. Monoid m => WellFormedSExpr m -> m
fold :: forall m. Monoid m => WellFormedSExpr m -> 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
foldMap' :: forall m a. Monoid m => (a -> m) -> WellFormedSExpr a -> m
$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
foldr' :: forall a b. (a -> b -> 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
foldl' :: forall b a. (b -> a -> b) -> b -> WellFormedSExpr a -> b
$cfoldr1 :: forall a. (a -> a -> a) -> WellFormedSExpr a -> a
foldr1 :: forall a. (a -> a -> a) -> WellFormedSExpr a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> WellFormedSExpr a -> a
foldl1 :: forall a. (a -> a -> a) -> WellFormedSExpr a -> a
$ctoList :: forall a. WellFormedSExpr a -> [a]
toList :: forall a. WellFormedSExpr a -> [a]
$cnull :: forall a. WellFormedSExpr a -> Bool
null :: forall a. WellFormedSExpr a -> Bool
$clength :: forall a. WellFormedSExpr a -> Int
length :: forall a. WellFormedSExpr a -> Int
$celem :: forall a. Eq a => a -> WellFormedSExpr a -> Bool
elem :: forall a. Eq a => a -> WellFormedSExpr a -> Bool
$cmaximum :: forall a. Ord a => WellFormedSExpr a -> a
maximum :: forall a. Ord a => WellFormedSExpr a -> a
$cminimum :: forall a. Ord a => WellFormedSExpr a -> a
minimum :: forall a. Ord a => WellFormedSExpr a -> a
$csum :: forall a. Num a => WellFormedSExpr a -> a
sum :: forall a. Num a => WellFormedSExpr a -> a
$cproduct :: forall a. Num a => WellFormedSExpr a -> a
product :: forall a. Num a => WellFormedSExpr a -> a
Foldable, Functor WellFormedSExpr
Foldable WellFormedSExpr
Functor WellFormedSExpr
-> Foldable WellFormedSExpr
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> WellFormedSExpr a -> f (WellFormedSExpr b))
-> (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 (m :: * -> *) a.
    Monad m =>
    WellFormedSExpr (m a) -> m (WellFormedSExpr a))
-> Traversable 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)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> WellFormedSExpr a -> f (WellFormedSExpr b)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> WellFormedSExpr a -> f (WellFormedSExpr b)
$csequenceA :: forall (f :: * -> *) a.
Applicative f =>
WellFormedSExpr (f a) -> f (WellFormedSExpr a)
sequenceA :: forall (f :: * -> *) a.
Applicative f =>
WellFormedSExpr (f a) -> f (WellFormedSExpr a)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> WellFormedSExpr a -> m (WellFormedSExpr b)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> WellFormedSExpr a -> m (WellFormedSExpr b)
$csequence :: forall (m :: * -> *) a.
Monad m =>
WellFormedSExpr (m a) -> m (WellFormedSExpr a)
sequence :: forall (m :: * -> *) a.
Monad m =>
WellFormedSExpr (m a) -> m (WellFormedSExpr a)
Traversable)

instance IsList (WellFormedSExpr atom) where
  type Item (WellFormedSExpr atom) = WellFormedSExpr atom
  fromList :: [Item (WellFormedSExpr atom)] -> WellFormedSExpr atom
fromList = [Item (WellFormedSExpr atom)] -> WellFormedSExpr atom
[WellFormedSExpr atom] -> WellFormedSExpr atom
forall atom. [WellFormedSExpr atom] -> WellFormedSExpr atom
WFSList
  toList :: WellFormedSExpr atom -> [Item (WellFormedSExpr atom)]
toList (WFSList [WellFormedSExpr atom]
xs) = [Item (WellFormedSExpr atom)]
[WellFormedSExpr atom]
xs
  toList (WFSAtom {}) = String -> [WellFormedSExpr atom]
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 = atom -> WellFormedSExpr atom
forall atom. atom -> WellFormedSExpr atom
WFSAtom (atom -> WellFormedSExpr atom)
-> (String -> atom) -> String -> WellFormedSExpr atom
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> atom
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      = WellFormedSExpr atom -> Either String (WellFormedSExpr atom)
forall a. a -> Either String a
forall (m :: * -> *) a. Monad m => a -> m a
return ([WellFormedSExpr atom] -> WellFormedSExpr atom
forall atom. [WellFormedSExpr atom] -> WellFormedSExpr atom
WFSList [])
toWellFormed (SAtom atom
a) = WellFormedSExpr atom -> Either String (WellFormedSExpr atom)
forall a. a -> Either String a
forall (m :: * -> *) a. Monad m => a -> m a
return (atom -> WellFormedSExpr atom
forall atom. atom -> WellFormedSExpr atom
WFSAtom atom
a)
toWellFormed (SCons SExpr atom
x SExpr atom
xs) = do
  WellFormedSExpr atom
x' <- SExpr atom -> Either String (WellFormedSExpr atom)
forall atom. SExpr atom -> Either String (WellFormedSExpr atom)
toWellFormed SExpr atom
x
  SExpr atom
-> ([WellFormedSExpr atom] -> [WellFormedSExpr atom])
-> Either String (WellFormedSExpr atom)
forall {atom} {atom}.
SExpr atom
-> ([WellFormedSExpr atom] -> [WellFormedSExpr atom])
-> Either String (WellFormedSExpr atom)
go SExpr atom
xs (WellFormedSExpr atom
x'WellFormedSExpr atom
-> [WellFormedSExpr atom] -> [WellFormedSExpr atom]
forall a. a -> [a] -> [a]
:)
  where go :: SExpr atom
-> ([WellFormedSExpr atom] -> [WellFormedSExpr atom])
-> Either String (WellFormedSExpr atom)
go (SAtom atom
_) [WellFormedSExpr atom] -> [WellFormedSExpr atom]
_  = String -> Either String (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      = WellFormedSExpr atom -> Either String (WellFormedSExpr atom)
forall a. a -> Either String a
forall (m :: * -> *) a. Monad m => a -> m a
return ([WellFormedSExpr atom] -> WellFormedSExpr atom
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' <- SExpr atom -> Either String (WellFormedSExpr atom)
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 ([WellFormedSExpr atom] -> [WellFormedSExpr atom])
-> ([WellFormedSExpr atom] -> [WellFormedSExpr atom])
-> [WellFormedSExpr atom]
-> [WellFormedSExpr atom]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (WellFormedSExpr atom
y'WellFormedSExpr atom
-> [WellFormedSExpr atom] -> [WellFormedSExpr atom]
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)  = atom -> SExpr atom
forall atom. atom -> SExpr atom
SAtom atom
a
fromWellFormed (WFSList [WellFormedSExpr atom]
xs) =
  (SExpr atom -> SExpr atom -> SExpr atom)
-> SExpr atom -> [SExpr atom] -> SExpr atom
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr SExpr atom -> SExpr atom -> SExpr atom
forall atom. SExpr atom -> SExpr atom -> SExpr atom
SCons SExpr atom
forall atom. SExpr atom
SNil ((WellFormedSExpr atom -> SExpr atom)
-> [WellFormedSExpr atom] -> [SExpr atom]
forall a b. (a -> b) -> [a] -> [b]
map WellFormedSExpr atom -> SExpr atom
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".
-}