Some documentation improvements for Repr
Getty Ritter
9 years ago
9 | 9 | ) where |
10 | 10 | |
11 | 11 | -- | 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 | |
14 | 14 | -- @atom@. |
15 | 15 | data SExpr atom |
16 | 16 | = SCons (SExpr atom) (SExpr atom) |
20 | 20 | |
21 | 21 | -- | Sometimes, the cons-based interface is too low |
22 | 22 | -- 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' | |
25 | 25 | -- to represent an improper list of the form |
26 | 26 | -- @(a b c . d)@. |
27 | 27 | data RichSExpr atom |
30 | 30 | | RSAtom atom |
31 | 31 | deriving (Eq, Show, Read) |
32 | 32 | |
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 | |
33 | 42 | toRich :: SExpr atom -> RichSExpr atom |
34 | 43 | toRich (SAtom a) = RSAtom a |
35 | 44 | toRich (SCons x xs) = go xs [toRich x] |
37 | 46 | go SNil rs = RSList rs |
38 | 47 | go (SCons x xs) rs = go xs (toRich x:rs) |
39 | 48 | |
49 | -- | This follows the same laws as 'toRich'. | |
40 | 50 | fromRich :: RichSExpr atom -> SExpr atom |
41 | 51 | fromRich (RSAtom a) = SAtom a |
42 | 52 | fromRich (RSList xs) = foldr SCons SNil (map fromRich xs) |
54 | 64 | |
55 | 65 | -- | This will be @Nothing@ is the argument contains an |
56 | 66 | -- 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) | |
59 | 71 | toWellFormed (SCons x xs) = do |
60 | 72 | x' <- toWellFormed x |
61 | 73 | 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) | |
64 | 76 | go (SCons x xs) rs = do |
65 | 77 | x' <- toWellFormed x |
66 | 78 | go xs (x':rs) |