gdritter repos s-cargot / 0afe6a4
Some documentation improvements for Repr Getty Ritter 9 years ago
1 changed file(s) with 20 addition(s) and 8 deletion(s). Collapse all Expand all
99 ) where
1010
1111 -- | All S-Expressions can be understood as a sequence
12 -- of @cons@ cells (represented here by @SCons@), the
13 -- empty list @nil@ (represented by @SNil@) or an
12 -- of @cons@ cells (represented here by 'SCons'), the
13 -- empty list @nil@ (represented by 'SNil') or an
1414 -- @atom@.
1515 data SExpr atom
1616 = SCons (SExpr atom) (SExpr atom)
2020
2121 -- | Sometimes, the cons-based interface is too low
2222 -- level, and we'd rather have the lists themselves
23 -- exposed. In this case, we have @RSList@ to
24 -- represent a well-formed cons list, and @RSDotted@
23 -- exposed. In this case, we have 'RSList' to
24 -- represent a well-formed cons list, and 'RSDotted'
2525 -- to represent an improper list of the form
2626 -- @(a b c . d)@.
2727 data RichSExpr atom
3030 | RSAtom atom
3131 deriving (Eq, Show, Read)
3232
33 -- | A Rich S-Expression might be a nicer interface
34 -- for certain libraries. It should always be true
35 -- that
36 --
37 -- > fromRich . toRich == id
38 --
39 -- and that
40 --
41 -- > toRich . fromRich == id
3342 toRich :: SExpr atom -> RichSExpr atom
3443 toRich (SAtom a) = RSAtom a
3544 toRich (SCons x xs) = go xs [toRich x]
3746 go SNil rs = RSList rs
3847 go (SCons x xs) rs = go xs (toRich x:rs)
3948
49 -- | This follows the same laws as 'toRich'.
4050 fromRich :: RichSExpr atom -> SExpr atom
4151 fromRich (RSAtom a) = SAtom a
4252 fromRich (RSList xs) = foldr SCons SNil (map fromRich xs)
5464
5565 -- | This will be @Nothing@ is the argument contains an
5666 -- improper list. It should hold that
57 toWellFormed :: SExpr atom -> Maybe (WellFormedSExpr atom)
58 toWellFormed (SAtom a) = Just (WFSAtom a)
67 --
68 -- > toWellFormed . fromWellFormed == Right
69 toWellFormed :: SExpr atom -> Either String (WellFormedSExpr atom)
70 toWellFormed (SAtom a) = return (WFSAtom a)
5971 toWellFormed (SCons x xs) = do
6072 x' <- toWellFormed x
6173 go xs [x']
62 where go (SAtom a) rs = Nothing
63 go SNil rs = Just (WFSList rs)
74 where go (SAtom a) rs = Left "Found atom in cdr position"
75 go SNil rs = return (WFSList rs)
6476 go (SCons x xs) rs = do
6577 x' <- toWellFormed x
6678 go xs (x':rs)