s-cargot-0.1.6.0: A flexible, extensible s-expression library.

Safe Haskell | None
Language | Haskell2010

Data.SCargot.Repr

Contents

- Rich SExpr representation
- Well-Formed SExpr representation

Synopsis

- data SExpr atom
  - = SCons (SExpr atom) (SExpr atom)
  - | SAtom atom
  - | SNil
- data RichSExpr atom
  - = RSList [RichSExpr atom]
  - | RSDotted [RichSExpr atom] atom
  - | RSAtom atom
- toRich :: SExpr atom -> RichSExpr atom
- fromRich :: RichSExpr atom -> SExpr atom
- data WellFormedSExpr atom
  - = WFSList [WellFormedSExpr atom]
  - | WFSAtom atom
- toWellFormed :: SExpr atom -> Either String (WellFormedSExpr atom)
- fromWellFormed :: WellFormedSExpr atom -> SExpr atom

# Documentation

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.

data SExpr atom Source #

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`.

Constructors

SCons (SExpr atom) (SExpr atom) |
SAtom atom |
SNil |

#### Instances

Instances details

Functor SExpr Source # |
Instance details Defined in Data.SCargot.Repr Methods fmap :: (a -> b) -> SExpr a -> SExpr b Source # (<$) :: a -> SExpr b -> SExpr a Source #
Foldable SExpr Source # |

Instance details

Defined in Data.SCargot.Repr

Methods

fold :: Monoid m => SExpr m -> m Source #

foldMap :: Monoid m => (a -> m) -> SExpr a -> m Source #

foldMap' :: Monoid m => (a -> m) -> SExpr a -> m Source #

foldr :: (a -> b -> b) -> b -> SExpr a -> b Source #

foldr' :: (a -> b -> b) -> b -> SExpr a -> b Source #

foldl :: (b -> a -> b) -> b -> SExpr a -> b Source #

foldl' :: (b -> a -> b) -> b -> SExpr a -> b Source #

foldr1 :: (a -> a -> a) -> SExpr a -> a Source #

foldl1 :: (a -> a -> a) -> SExpr a -> a Source #

toList :: SExpr a -> [a] Source #

null :: SExpr a -> Bool Source #

length :: SExpr a -> Int Source #

elem :: Eq a => a -> SExpr a -> Bool Source #

maximum :: Ord a => SExpr a -> a Source #

minimum :: Ord a => SExpr a -> a Source #

sum :: Num a => SExpr a -> a Source #

product :: Num a => SExpr a -> a Source #

Traversable SExpr Source # |
Instance details Defined in Data.SCargot.Repr Methods traverse :: Applicative f => (a -> f b) -> SExpr a -> f (SExpr b) Source # sequenceA :: Applicative f => SExpr (f a) -> f (SExpr a) Source # mapM :: Monad m => (a -> m b) -> SExpr a -> m (SExpr b) Source # sequence :: Monad m => SExpr (m a) -> m (SExpr a) Source #
Data atom => Data (SExpr atom) Source # |

Instance details

Defined in Data.SCargot.Repr

Methods

gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> SExpr atom -> c (SExpr atom) Source #

gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (SExpr atom) Source #

toConstr :: SExpr atom -> Constr Source #

dataTypeOf :: SExpr atom -> DataType Source #

dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (SExpr atom)) Source #

dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (SExpr atom)) Source #

gmapT :: (forall b. Data b => b -> b) -> SExpr atom -> SExpr atom Source #

gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> SExpr atom -> r Source #

gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> SExpr atom -> r Source #

gmapQ :: (forall d. Data d => d -> u) -> SExpr atom -> [u] Source #

gmapQi :: Int -> (forall d. Data d => d -> u) -> SExpr atom -> u Source #

gmapM :: Monad m => (forall d. Data d => d -> m d) -> SExpr atom -> m (SExpr atom) Source #

gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> SExpr atom -> m (SExpr atom) Source #

gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> SExpr atom -> m (SExpr atom) Source #

IsString atom => IsString (SExpr atom) Source # |
Instance details Defined in Data.SCargot.Repr Methods fromString :: String -> SExpr atom Source #
IsList (SExpr atom) Source # |

Instance details

Defined in Data.SCargot.Repr

Associated Types

type Item (SExpr atom) |
Instance details Defined in Data.SCargot.Repr type Item (SExpr atom) = SExpr atom

Methods

fromList :: [Item (SExpr atom)] -> SExpr atom Source #

fromListN :: Int -> [Item (SExpr atom)] -> SExpr atom Source #

toList :: SExpr atom -> [Item (SExpr atom)] Source #

Read atom => Read (SExpr atom) Source # |
Instance details Defined in Data.SCargot.Repr Methods readsPrec :: Int -> ReadS (SExpr atom) Source # readList :: ReadS [SExpr atom] Source # readPrec :: ReadPrec (SExpr atom) Source # readListPrec :: ReadPrec [SExpr atom] Source #
Show atom => Show (SExpr atom) Source # |
Instance details Defined in Data.SCargot.Repr Methods showsPrec :: Int -> SExpr atom -> ShowS Source # show :: SExpr atom -> String Source # showList :: [SExpr atom] -> ShowS Source #
Eq atom => Eq (SExpr atom) Source # |
Instance details Defined in Data.SCargot.Repr Methods (==) :: SExpr atom -> SExpr atom -> Bool Source # (/=) :: SExpr atom -> SExpr atom -> Bool Source #
type Item (SExpr atom) Source # |
Instance details Defined in Data.SCargot.Repr type Item (SExpr atom) = SExpr atom

# Rich SExpr representation

data RichSExpr atom Source #

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.

Constructors

RSList [RichSExpr atom] |
RSDotted [RichSExpr atom] atom |
RSAtom atom |

#### Instances

Instances details

Functor RichSExpr Source # |
Instance details Defined in Data.SCargot.Repr Methods fmap :: (a -> b) -> RichSExpr a -> RichSExpr b Source # (<$) :: a -> RichSExpr b -> RichSExpr a Source #
Foldable RichSExpr Source # |

Instance details

Defined in Data.SCargot.Repr

Methods

fold :: Monoid m => RichSExpr m -> m Source #

foldMap :: Monoid m => (a -> m) -> RichSExpr a -> m Source #

foldMap' :: Monoid m => (a -> m) -> RichSExpr a -> m Source #

foldr :: (a -> b -> b) -> b -> RichSExpr a -> b Source #

foldr' :: (a -> b -> b) -> b -> RichSExpr a -> b Source #

foldl :: (b -> a -> b) -> b -> RichSExpr a -> b Source #

foldl' :: (b -> a -> b) -> b -> RichSExpr a -> b Source #

foldr1 :: (a -> a -> a) -> RichSExpr a -> a Source #

foldl1 :: (a -> a -> a) -> RichSExpr a -> a Source #

toList :: RichSExpr a -> [a] Source #

null :: RichSExpr a -> Bool Source #

length :: RichSExpr a -> Int Source #

elem :: Eq a => a -> RichSExpr a -> Bool Source #

maximum :: Ord a => RichSExpr a -> a Source #

minimum :: Ord a => RichSExpr a -> a Source #

sum :: Num a => RichSExpr a -> a Source #

product :: Num a => RichSExpr a -> a Source #

Traversable RichSExpr Source # |
Instance details Defined in Data.SCargot.Repr Methods traverse :: Applicative f => (a -> f b) -> RichSExpr a -> f (RichSExpr b) Source # sequenceA :: Applicative f => RichSExpr (f a) -> f (RichSExpr a) Source # mapM :: Monad m => (a -> m b) -> RichSExpr a -> m (RichSExpr b) Source # sequence :: Monad m => RichSExpr (m a) -> m (RichSExpr a) Source #
Data atom => Data (RichSExpr atom) Source # |

Instance details

Defined in Data.SCargot.Repr

Methods

gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> RichSExpr atom -> c (RichSExpr atom) Source #

gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (RichSExpr atom) Source #

toConstr :: RichSExpr atom -> Constr Source #

dataTypeOf :: RichSExpr atom -> DataType Source #

dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (RichSExpr atom)) Source #

dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (RichSExpr atom)) Source #

gmapT :: (forall b. Data b => b -> b) -> RichSExpr atom -> RichSExpr atom Source #

gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> RichSExpr atom -> r Source #

gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> RichSExpr atom -> r Source #

gmapQ :: (forall d. Data d => d -> u) -> RichSExpr atom -> [u] Source #

gmapQi :: Int -> (forall d. Data d => d -> u) -> RichSExpr atom -> u Source #

gmapM :: Monad m => (forall d. Data d => d -> m d) -> RichSExpr atom -> m (RichSExpr atom) Source #

gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> RichSExpr atom -> m (RichSExpr atom) Source #

gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> RichSExpr atom -> m (RichSExpr atom) Source #

IsString atom => IsString (RichSExpr atom) Source # |
Instance details Defined in Data.SCargot.Repr Methods fromString :: String -> RichSExpr atom Source #
IsList (RichSExpr atom) Source # |

Instance details

Defined in Data.SCargot.Repr

Associated Types

type Item (RichSExpr atom) |
Instance details Defined in Data.SCargot.Repr type Item (RichSExpr atom) = RichSExpr atom

Methods

fromList :: [Item (RichSExpr atom)] -> RichSExpr atom Source #

fromListN :: Int -> [Item (RichSExpr atom)] -> RichSExpr atom Source #

toList :: RichSExpr atom -> [Item (RichSExpr atom)] Source #

Read atom => Read (RichSExpr atom) Source # |
Instance details Defined in Data.SCargot.Repr Methods readsPrec :: Int -> ReadS (RichSExpr atom) Source # readList :: ReadS [RichSExpr atom] Source # readPrec :: ReadPrec (RichSExpr atom) Source # readListPrec :: ReadPrec [RichSExpr atom] Source #
Show atom => Show (RichSExpr atom) Source # |
Instance details Defined in Data.SCargot.Repr Methods showsPrec :: Int -> RichSExpr atom -> ShowS Source # show :: RichSExpr atom -> String Source # showList :: [RichSExpr atom] -> ShowS Source #
Eq atom => Eq (RichSExpr atom) Source # |
Instance details Defined in Data.SCargot.Repr Methods (==) :: RichSExpr atom -> RichSExpr atom -> Bool Source # (/=) :: RichSExpr atom -> RichSExpr atom -> Bool Source #
type Item (RichSExpr atom) Source # |
Instance details Defined in Data.SCargot.Repr type Item (RichSExpr atom) = RichSExpr atom

toRich :: SExpr atom -> RichSExpr atom Source #

It should always be true that

    fromRich (toRich x) == x

and that

    toRich (fromRich x) == x

fromRich :: RichSExpr atom -> SExpr atom Source #

This follows the same laws as `toRich`.

# Well-Formed SExpr representation

data WellFormedSExpr atom Source #

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.

Constructors

WFSList [WellFormedSExpr atom] |
WFSAtom atom |

#### Instances

Instances details

Functor WellFormedSExpr Source # |
Instance details Defined in Data.SCargot.Repr Methods fmap :: (a -> b) -> WellFormedSExpr a -> WellFormedSExpr b Source # (<$) :: a -> WellFormedSExpr b -> WellFormedSExpr a Source #
Foldable WellFormedSExpr Source # |

Instance details

Defined in Data.SCargot.Repr

Methods

fold :: Monoid m => WellFormedSExpr m -> m Source #

foldMap :: Monoid m => (a -> m) -> WellFormedSExpr a -> m Source #

foldMap' :: Monoid m => (a -> m) -> WellFormedSExpr a -> m Source #

foldr :: (a -> b -> b) -> b -> WellFormedSExpr a -> b Source #

foldr' :: (a -> b -> b) -> b -> WellFormedSExpr a -> b Source #

foldl :: (b -> a -> b) -> b -> WellFormedSExpr a -> b Source #

foldl' :: (b -> a -> b) -> b -> WellFormedSExpr a -> b Source #

foldr1 :: (a -> a -> a) -> WellFormedSExpr a -> a Source #

foldl1 :: (a -> a -> a) -> WellFormedSExpr a -> a Source #

toList :: WellFormedSExpr a -> [a] Source #

null :: WellFormedSExpr a -> Bool Source #

length :: WellFormedSExpr a -> Int Source #

elem :: Eq a => a -> WellFormedSExpr a -> Bool Source #

maximum :: Ord a => WellFormedSExpr a -> a Source #

minimum :: Ord a => WellFormedSExpr a -> a Source #

sum :: Num a => WellFormedSExpr a -> a Source #

product :: Num a => WellFormedSExpr a -> a Source #

Traversable WellFormedSExpr Source # |
Instance details Defined in Data.SCargot.Repr Methods traverse :: Applicative f => (a -> f b) -> WellFormedSExpr a -> f (WellFormedSExpr b) Source # sequenceA :: Applicative f => WellFormedSExpr (f a) -> f (WellFormedSExpr a) Source # mapM :: Monad m => (a -> m b) -> WellFormedSExpr a -> m (WellFormedSExpr b) Source # sequence :: Monad m => WellFormedSExpr (m a) -> m (WellFormedSExpr a) Source #
Data atom => Data (WellFormedSExpr atom) Source # |

Instance details

Defined in Data.SCargot.Repr

Methods

gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> WellFormedSExpr atom -> c (WellFormedSExpr atom) Source #

gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (WellFormedSExpr atom) Source #

toConstr :: WellFormedSExpr atom -> Constr Source #

dataTypeOf :: WellFormedSExpr atom -> DataType Source #

dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (WellFormedSExpr atom)) Source #

dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (WellFormedSExpr atom)) Source #

gmapT :: (forall b. Data b => b -> b) -> WellFormedSExpr atom -> WellFormedSExpr atom Source #

gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> WellFormedSExpr atom -> r Source #

gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> WellFormedSExpr atom -> r Source #

gmapQ :: (forall d. Data d => d -> u) -> WellFormedSExpr atom -> [u] Source #

gmapQi :: Int -> (forall d. Data d => d -> u) -> WellFormedSExpr atom -> u Source #

gmapM :: Monad m => (forall d. Data d => d -> m d) -> WellFormedSExpr atom -> m (WellFormedSExpr atom) Source #

gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> WellFormedSExpr atom -> m (WellFormedSExpr atom) Source #

gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> WellFormedSExpr atom -> m (WellFormedSExpr atom) Source #

IsString atom => IsString (WellFormedSExpr atom) Source # |
Instance details Defined in Data.SCargot.Repr Methods fromString :: String -> WellFormedSExpr atom Source #
IsList (WellFormedSExpr atom) Source # |

Instance details

Defined in Data.SCargot.Repr

Associated Types

type Item (WellFormedSExpr atom) |
Instance details Defined in Data.SCargot.Repr type Item (WellFormedSExpr atom) = WellFormedSExpr atom

Methods

fromList :: [Item (WellFormedSExpr atom)] -> WellFormedSExpr atom Source #

fromListN :: Int -> [Item (WellFormedSExpr atom)] -> WellFormedSExpr atom Source #

toList :: WellFormedSExpr atom -> [Item (WellFormedSExpr atom)] Source #

Read atom => Read (WellFormedSExpr atom) Source # |
Instance details Defined in Data.SCargot.Repr Methods readsPrec :: Int -> ReadS (WellFormedSExpr atom) Source # readList :: ReadS [WellFormedSExpr atom] Source # readPrec :: ReadPrec (WellFormedSExpr atom) Source # readListPrec :: ReadPrec [WellFormedSExpr atom] Source #
Show atom => Show (WellFormedSExpr atom) Source # |
Instance details Defined in Data.SCargot.Repr Methods showsPrec :: Int -> WellFormedSExpr atom -> ShowS Source # show :: WellFormedSExpr atom -> String Source # showList :: [WellFormedSExpr atom] -> ShowS Source #
Eq atom => Eq (WellFormedSExpr atom) Source # |
Instance details Defined in Data.SCargot.Repr Methods (==) :: WellFormedSExpr atom -> WellFormedSExpr atom -> Bool Source # (/=) :: WellFormedSExpr atom -> WellFormedSExpr atom -> Bool Source #
type Item (WellFormedSExpr atom) Source # |
Instance details Defined in Data.SCargot.Repr type Item (WellFormedSExpr atom) = WellFormedSExpr atom

toWellFormed :: SExpr atom -> Either String (WellFormedSExpr atom) Source #

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

fromWellFormed :: WellFormedSExpr atom -> SExpr atom Source #

Convert a WellFormedSExpr back into a SExpr.