Debian Codegraph @

Data/SCargot/Repr/Basic.hs

raw
1 {-# LANGUAGE PatternSynonyms #-}
2 {-# LANGUAGE ScopedTypeVariables #-}
3 {-# LANGUAGE ViewPatterns #-}
4
5 module Data.SCargot.Repr.Basic
6 ( -- * Basic 'SExpr' representation
7 R.SExpr(..)
8 -- * Constructing and Deconstructing
9 , cons
10 , uncons
11 -- * Shorthand Patterns
12 , pattern (:::)
13 , pattern A
14 , pattern L
15 , pattern DL
16 , pattern Nil
17 -- * Lenses
18 , _car
19 , _cdr
20 -- * Useful processing functions
21 , fromPair
22 , fromList
23 , fromAtom
24 , asPair
25 , asList
26 , isAtom
27 , asAtom
28 , asAssoc
29 ) where
30
31 #if !MIN_VERSION_base(4,8,0)
32 import Control.Applicative (Applicative, (<$>), (<*>), pure)
33 #endif
34 import Data.SCargot.Repr as R
35
36 -- | A traversal with access to the first element of a pair.
37 --
38 -- >>> import Lens.Family
39 -- >>> set _car (A "elephant") (A "one" ::: A "two" ::: A "three" ::: Nil)
40 -- A "elelphant" ::: A "two" ::: A "three" ::: Nil
41 -- >>> set _car (A "two" ::: A "three" ::: Nil) (A "one" ::: A "elephant")
42 -- (A "two" ::: A "three" ::: Nil) ::: A "elephant"
43 _car :: Applicative f => (SExpr a -> f (SExpr a)) -> SExpr a -> f (SExpr a)
44 _car f (SCons x xs) = (:::) <$> f x <*> pure xs
45 _car _ (SAtom a) = pure (A a)
46 _car _ SNil = pure SNil
47
48 -- | A traversal with access to the second element of a pair.
49 --
50 -- >>> import Lens.Family
51 -- >>> set _cdr (A "elephant") (A "one" ::: A "two" ::: A "three" ::: Nil)
52 -- A "one" ::: A "elephant"
53 -- >>> set _cdr (A "two" ::: A "three" ::: Nil) (A "one" ::: A "elephant")
54 -- A "one" ::: A "two" ::: A "three" ::: Nil
55 _cdr :: Applicative f => (SExpr a -> f (SExpr a)) -> SExpr a -> f (SExpr a)
56 _cdr f (SCons x xs) = (:::) <$> pure x <*> f xs
57 _cdr _ (SAtom a) = pure (A a)
58 _cdr _ SNil = pure Nil
59
60 -- | Produce the head and tail of the s-expression (if possible).
61 --
62 -- >>> uncons (A "el" ::: A "eph" ::: A "ant" ::: Nil)
63 -- Just (A "el",SCons (SAtom "eph") (SCons (SAtom "ant") SNil))
64 uncons :: SExpr a -> Maybe (SExpr a, SExpr a)
65 uncons (SCons x xs) = Just (x, xs)
66 uncons _ = Nothing
67
68 -- | Combine the two s-expressions into a new one.
69 --
70 -- >>> cons (A "el") (L ["eph", A "ant"])
71 -- SCons (SAtom "el) (SCons (SAtom "eph") (SCons (SAtom "ant") SNil))
72 cons :: SExpr a -> SExpr a -> SExpr a
73 cons = SCons
74
75 gatherDList :: SExpr a -> Maybe ([SExpr a], a)
76 gatherDList SNil = Nothing
77 gatherDList SAtom {} = Nothing
78 gatherDList sx = go sx
79 where go SNil = Nothing
80 go (SAtom a) = return ([], a)
81 go (SCons x xs) = do
82 (ys, a) <- go xs
83 return (x:ys, a)
84
85 infixr 5 :::
86
87 -- | A shorter infix alias for `SCons`
88 --
89 -- >>> A "pachy" ::: A "derm"
90 -- SCons (SAtom "pachy") (SAtom "derm")
91 #if MIN_VERSION_base(4,8,0)
92 pattern (:::) :: SExpr a -> SExpr a -> SExpr a
93 #endif
94 pattern x ::: xs = SCons x xs
95
96 -- | A shorter alias for `SAtom`
97 --
98 -- >>> A "elephant"
99 -- SAtom "elephant"
100 #if MIN_VERSION_base(4,8,0)
101 pattern A :: a -> SExpr a
102 #endif
103 pattern A x = SAtom x
104
105 -- | A (slightly) shorter alias for `SNil`
106 --
107 -- >>> Nil
108 -- SNil
109 #if MIN_VERSION_base(4,8,0)
110 pattern Nil :: SExpr a
111 #endif
112 pattern Nil = SNil
113
114 -- | An alias for matching a proper list.
115 --
116 -- >>> L [A "pachy", A "derm"]
117 -- SExpr (SAtom "pachy") (SExpr (SAtom "derm") SNil)
118 #if MIN_VERSION_base(4,8,0)
119 pattern L :: [SExpr a] -> SExpr a
120 #endif
121 pattern L xs <- (gatherList -> Right xs)
122 #if MIN_VERSION_base(4,8,0)
123 where L [] = SNil
124 L (x:xs) = SCons x (L xs)
125 #endif
126
127
128 -- | An alias for matching a dotted list.
129 --
130 -- >>> DL [A "pachy"] A "derm"
131 -- SExpr (SAtom "pachy") (SAtom "derm")
132 #if MIN_VERSION_base(4,8,0)
133 pattern DL :: [SExpr a] -> a -> SExpr a
134 #endif
135 pattern DL xs x <- (gatherDList -> Just (xs, x))
136 #if MIN_VERSION_base(4,8,0)
137 where DL [] a = SAtom a
138 DL (x:xs) a = SCons x (DL xs a)
139 #endif
140
141 getShape :: SExpr a -> String
142 getShape Nil = "empty list"
143 getShape sx = go (0 :: Int) sx
144 where go n SNil = "list of length " ++ show n
145 go n SAtom {} = "dotted list of length " ++ show n
146 go n (SCons _ xs) = go (n+1) xs
147
148 -- | Utility function for parsing a pair of things.
149 --
150 -- >>> fromPair (isAtom "pachy") (asAtom return) (A "pachy" ::: A "derm" ::: Nil)
151 -- Right ((), "derm")
152 -- >>> fromPair (isAtom "pachy") fromAtom (A "pachy" ::: Nil)
153 -- Left "Expected two-element list"
154 fromPair :: (SExpr t -> Either String a)
155 -> (SExpr t -> Either String b)
156 -> SExpr t -> Either String (a, b)
157 fromPair pl pr (l ::: r ::: Nil) = (,) <$> pl l <*> pr r
158 fromPair _ _ sx = Left ("fromPair: expected two-element list; found " ++ getShape sx)
159
160 -- | Utility function for parsing a list of things.
161 fromList :: (SExpr t -> Either String a) -> SExpr t -> Either String [a]
162 fromList p (s ::: ss) = (:) <$> p s <*> fromList p ss
163 fromList _ Nil = pure []
164 fromList _ sx = Left ("fromList: expected list; found " ++ getShape sx)
165
166 -- | Utility function for parsing a single atom
167 fromAtom :: SExpr t -> Either String t
168 fromAtom (A a) = return a
169 fromAtom sx = Left ("fromAtom: expected atom; found list" ++ getShape sx)
170
171 gatherList :: SExpr t -> Either String [SExpr t]
172 gatherList (x ::: xs) = (:) <$> pure x <*> gatherList xs
173 gatherList Nil = pure []
174 gatherList sx = Left ("gatherList: expected list; found " ++ getShape sx)
175
176 -- | Parse a two-element list (NOT a dotted pair) using the
177 -- provided function.
178 --
179 -- >>> let go (A l) (A r) = return (l ++ r); go _ _ = Left "expected atoms"
180 -- >>> asPair go (A "pachy" ::: A "derm" ::: Nil)
181 -- Right "pachyderm"
182 -- >>> asPair go (A "elephant" ::: Nil)
183 -- Left "asPair: expected two-element list; found list of length 1"
184 asPair :: ((SExpr t, SExpr t) -> Either String a)
185 -> SExpr t -> Either String a
186 asPair f (l ::: r ::: SNil) = f (l, r)
187 asPair _ sx = Left ("asPair: expected two-element list; found " ++ getShape sx)
188
189 -- | Parse an arbitrary-length list using the provided function.
190 --
191 -- >>> let go xs = concat <$> mapM fromAtom xs
192 -- >>> asList go (A "el" ::: A "eph" ::: A "ant" ::: Nil)
193 -- Right "elephant"
194 -- >>> asList go (A "el" ::: A "eph" ::: A "ant")
195 -- Left "asList: expected list; found dotted list of length 3"
196 asList :: ([SExpr t] -> Either String a) -> SExpr t -> Either String a
197 asList f ls = gatherList ls >>= f
198
199 -- | Match a given literal atom, failing otherwise.
200 --
201 -- >>> isAtom "elephant" (A "elephant")
202 -- Right ()
203 -- >>> isAtom "elephant" (A "elephant" ::: Nil)
204 -- Left "isAtom: expected atom; found list"
205 isAtom :: Eq t => t -> SExpr t -> Either String ()
206 isAtom s (A s')
207 | s == s' = return ()
208 | otherwise = Left "isAtom: failed to match atom"
209 isAtom _ sx = Left ("isAtom: expected atom; found " ++ getShape sx)
210
211 -- | Parse an atom using the provided function.
212 --
213 -- >>> import Data.Char (toUpper)
214 -- >>> asAtom (return . map toUpper) (A "elephant")
215 -- Right "ELEPHANT"
216 -- >>> asAtom (return . map toUpper) Nil
217 -- Left "asAtom: expected atom; found empty list"
218 asAtom :: (t -> Either String a) -> SExpr t -> Either String a
219 asAtom f (A s) = f s
220 asAtom _ sx = Left ("asAtom: expected atom; found " ++ getShape sx)
221
222 -- | Parse an assoc-list using the provided function.
223 --
224 -- >>> let def (x, y) = do { a <- fromAtom x; b <- fromAtom y; return (a ++ ": " ++ b) }
225 -- >>> let defList xs = do { defs <- mapM def xs; return (unlines defs) }
226 -- >>> asAssoc defList ((A "legs" ::: A "four" ::: Nil) ::: (A "trunk" ::: A "one" ::: Nil) ::: Nil)
227 -- Right "legs: four\ntrunk: one\n"
228 -- >>> asAssoc defList ((A "legs" ::: A "four" ::: Nil) ::: (A "elephant") ::: Nil)
229 -- Left "asAssoc: expected pair; found list of length 1"
230 asAssoc :: ([(SExpr t, SExpr t)] -> Either String a)
231 -> SExpr t -> Either String a
232 asAssoc f ss = gatherList ss >>= mapM go >>= f
233 where go (a ::: b ::: Nil) = return (a, b)
234 go sx = Left ("asAssoc: expected pair; found " ++ getShape sx)