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

Safe HaskellNone
LanguageHaskell2010

Data.SCargot.Repr

Contents

Synopsis

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

Functor SExpr Source 
Foldable SExpr Source 
Traversable SExpr Source 
IsList (SExpr atom) Source 
Eq atom => Eq (SExpr atom) Source 
Data atom => Data (SExpr atom) Source 
Read atom => Read (SExpr atom) Source 
Show atom => Show (SExpr atom) Source 
IsString atom => IsString (SExpr atom) Source 
type Item (SExpr atom) = SExpr atom Source 

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

Functor RichSExpr Source 
Foldable RichSExpr Source 
Traversable RichSExpr Source 
IsList (RichSExpr atom) Source 
Eq atom => Eq (RichSExpr atom) Source 
Data atom => Data (RichSExpr atom) Source 
Read atom => Read (RichSExpr atom) Source 
Show atom => Show (RichSExpr atom) Source 
IsString atom => IsString (RichSExpr atom) Source 
type Item (RichSExpr atom) = RichSExpr atom Source 

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

Functor WellFormedSExpr Source 
Foldable WellFormedSExpr Source 
Traversable WellFormedSExpr Source 
IsList (WellFormedSExpr atom) Source 
Eq atom => Eq (WellFormedSExpr atom) Source 
Data atom => Data (WellFormedSExpr atom) Source 
Read atom => Read (WellFormedSExpr atom) Source 
Show atom => Show (WellFormedSExpr atom) Source 
IsString atom => IsString (WellFormedSExpr atom) Source 
type Item (WellFormedSExpr atom) = WellFormedSExpr atom Source 

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.