`par` should be infixr 0
[packages/old-time.git] / Control / Monad.hs
1 {-# OPTIONS_GHC -fno-implicit-prelude #-}
2 -----------------------------------------------------------------------------
3 -- |
4 -- Module : Control.Monad
5 -- Copyright : (c) The University of Glasgow 2001
6 -- License : BSD-style (see the file libraries/base/LICENSE)
7 --
8 -- Maintainer : libraries@haskell.org
9 -- Stability : provisional
10 -- Portability : portable
11 --
12 -- The 'Functor', 'Monad' and 'MonadPlus' classes,
13 -- with some useful operations on monads.
14
15 module Control.Monad
16 (
17 -- * Functor and monad classes
18
19 Functor(fmap)
20 , Monad((>>=), (>>), return, fail)
21
22 , MonadPlus ( -- class context: Monad
23 mzero -- :: (MonadPlus m) => m a
24 , mplus -- :: (MonadPlus m) => m a -> m a -> m a
25 )
26 -- * Functions
27
28 -- ** Naming conventions
29 -- $naming
30
31 -- ** Basic functions from the "Prelude"
32
33 , mapM -- :: (Monad m) => (a -> m b) -> [a] -> m [b]
34 , mapM_ -- :: (Monad m) => (a -> m b) -> [a] -> m ()
35 , forM -- :: (Monad m) => [a] -> (a -> m b) -> m [b]
36 , forM_ -- :: (Monad m) => [a] -> (a -> m b) -> m ()
37 , sequence -- :: (Monad m) => [m a] -> m [a]
38 , sequence_ -- :: (Monad m) => [m a] -> m ()
39 , (=<<) -- :: (Monad m) => (a -> m b) -> m a -> m b
40
41 -- ** Generalisations of list functions
42
43 , join -- :: (Monad m) => m (m a) -> m a
44 , msum -- :: (MonadPlus m) => [m a] -> m a
45 , filterM -- :: (Monad m) => (a -> m Bool) -> [a] -> m [a]
46 , mapAndUnzipM -- :: (Monad m) => (a -> m (b,c)) -> [a] -> m ([b], [c])
47 , zipWithM -- :: (Monad m) => (a -> b -> m c) -> [a] -> [b] -> m [c]
48 , zipWithM_ -- :: (Monad m) => (a -> b -> m c) -> [a] -> [b] -> m ()
49 , foldM -- :: (Monad m) => (a -> b -> m a) -> a -> [b] -> m a
50 , foldM_ -- :: (Monad m) => (a -> b -> m a) -> a -> [b] -> m ()
51 , replicateM -- :: (Monad m) => Int -> m a -> m [a]
52 , replicateM_ -- :: (Monad m) => Int -> m a -> m ()
53
54 -- ** Conditional execution of monadic expressions
55
56 , guard -- :: (MonadPlus m) => Bool -> m ()
57 , when -- :: (Monad m) => Bool -> m () -> m ()
58 , unless -- :: (Monad m) => Bool -> m () -> m ()
59
60 -- ** Monadic lifting operators
61
62 , liftM -- :: (Monad m) => (a -> b) -> (m a -> m b)
63 , liftM2 -- :: (Monad m) => (a -> b -> c) -> (m a -> m b -> m c)
64 , liftM3 -- :: ...
65 , liftM4 -- :: ...
66 , liftM5 -- :: ...
67
68 , ap -- :: (Monad m) => m (a -> b) -> m a -> m b
69
70 ) where
71
72 import Data.Maybe
73
74 #ifdef __GLASGOW_HASKELL__
75 import GHC.List
76 import GHC.Base
77 #endif
78
79 #ifdef __GLASGOW_HASKELL__
80 infixr 1 =<<
81
82 -- -----------------------------------------------------------------------------
83 -- Prelude monad functions
84
85 -- | Same as '>>=', but with the arguments interchanged.
86 {-# SPECIALISE (=<<) :: (a -> [b]) -> [a] -> [b] #-}
87 (=<<) :: Monad m => (a -> m b) -> m a -> m b
88 f =<< x = x >>= f
89
90 -- | Evaluate each action in the sequence from left to right,
91 -- and collect the results.
92 sequence :: Monad m => [m a] -> m [a]
93 {-# INLINE sequence #-}
94 sequence ms = foldr k (return []) ms
95 where
96 k m m' = do { x <- m; xs <- m'; return (x:xs) }
97
98 -- | Evaluate each action in the sequence from left to right,
99 -- and ignore the results.
100 sequence_ :: Monad m => [m a] -> m ()
101 {-# INLINE sequence_ #-}
102 sequence_ ms = foldr (>>) (return ()) ms
103
104 -- | @'mapM' f@ is equivalent to @'sequence' . 'map' f@.
105 mapM :: Monad m => (a -> m b) -> [a] -> m [b]
106 {-# INLINE mapM #-}
107 mapM f as = sequence (map f as)
108
109 -- | @'mapM_' f@ is equivalent to @'sequence_' . 'map' f@.
110 mapM_ :: Monad m => (a -> m b) -> [a] -> m ()
111 {-# INLINE mapM_ #-}
112 mapM_ f as = sequence_ (map f as)
113
114 #endif /* __GLASGOW_HASKELL__ */
115
116 -- -----------------------------------------------------------------------------
117 -- The MonadPlus class definition
118
119 -- | Monads that also support choice and failure.
120 class Monad m => MonadPlus m where
121 -- | the identity of 'mplus'. It should also satisfy the equations
122 --
123 -- > mzero >>= f = mzero
124 -- > v >> mzero = mzero
125 --
126 -- (but the instance for 'System.IO.IO' defined in "Control.Monad.Error"
127 -- does not satisfy the second one).
128 mzero :: m a
129 -- | an associative operation
130 mplus :: m a -> m a -> m a
131
132 instance MonadPlus [] where
133 mzero = []
134 mplus = (++)
135
136 instance MonadPlus Maybe where
137 mzero = Nothing
138
139 Nothing `mplus` ys = ys
140 xs `mplus` _ys = xs
141
142 -- -----------------------------------------------------------------------------
143 -- Functions mandated by the Prelude
144
145 -- | @'guard' b@ is @'return' ()@ if @b@ is 'True',
146 -- and 'mzero' if @b@ is 'False'.
147 guard :: (MonadPlus m) => Bool -> m ()
148 guard True = return ()
149 guard False = mzero
150
151 -- | This generalizes the list-based 'filter' function.
152
153 filterM :: (Monad m) => (a -> m Bool) -> [a] -> m [a]
154 filterM _ [] = return []
155 filterM p (x:xs) = do
156 flg <- p x
157 ys <- filterM p xs
158 return (if flg then x:ys else ys)
159
160 -- | 'forM' is 'mapM' with its arguments flipped
161 forM :: Monad m => [a] -> (a -> m b) -> m [b]
162 {-# INLINE forM #-}
163 forM = flip mapM
164
165 -- | 'forM_' is 'mapM_' with its arguments flipped
166 forM_ :: Monad m => [a] -> (a -> m b) -> m ()
167 {-# INLINE forM_ #-}
168 forM_ = flip mapM_
169
170 -- | This generalizes the list-based 'concat' function.
171
172 msum :: MonadPlus m => [m a] -> m a
173 {-# INLINE msum #-}
174 msum = foldr mplus mzero
175
176 -- -----------------------------------------------------------------------------
177 -- Other monad functions
178
179 -- | The 'join' function is the conventional monad join operator. It is used to
180 -- remove one level of monadic structure, projecting its bound argument into the
181 -- outer level.
182 join :: (Monad m) => m (m a) -> m a
183 join x = x >>= id
184
185 -- | The 'mapAndUnzipM' function maps its first argument over a list, returning
186 -- the result as a pair of lists. This function is mainly used with complicated
187 -- data structures or a state-transforming monad.
188 mapAndUnzipM :: (Monad m) => (a -> m (b,c)) -> [a] -> m ([b], [c])
189 mapAndUnzipM f xs = sequence (map f xs) >>= return . unzip
190
191 -- | The 'zipWithM' function generalizes 'zipWith' to arbitrary monads.
192 zipWithM :: (Monad m) => (a -> b -> m c) -> [a] -> [b] -> m [c]
193 zipWithM f xs ys = sequence (zipWith f xs ys)
194
195 -- | 'zipWithM_' is the extension of 'zipWithM' which ignores the final result.
196 zipWithM_ :: (Monad m) => (a -> b -> m c) -> [a] -> [b] -> m ()
197 zipWithM_ f xs ys = sequence_ (zipWith f xs ys)
198
199 {- | The 'foldM' function is analogous to 'foldl', except that its result is
200 encapsulated in a monad. Note that 'foldM' works from left-to-right over
201 the list arguments. This could be an issue where '(>>)' and the `folded
202 function' are not commutative.
203
204
205 > foldM f a1 [x1, x2, ..., xm ]
206
207 ==
208
209 > do
210 > a2 <- f a1 x1
211 > a3 <- f a2 x2
212 > ...
213 > f am xm
214
215 If right-to-left evaluation is required, the input list should be reversed.
216 -}
217
218 foldM :: (Monad m) => (a -> b -> m a) -> a -> [b] -> m a
219 foldM _ a [] = return a
220 foldM f a (x:xs) = f a x >>= \fax -> foldM f fax xs
221
222 -- | Like 'foldM', but discards the result.
223 foldM_ :: (Monad m) => (a -> b -> m a) -> a -> [b] -> m ()
224 foldM_ f a xs = foldM f a xs >> return ()
225
226 -- | @'replicateM' n act@ performs the action @n@ times,
227 -- gathering the results.
228 replicateM :: (Monad m) => Int -> m a -> m [a]
229 replicateM n x = sequence (replicate n x)
230
231 -- | Like 'replicateM', but discards the result.
232 replicateM_ :: (Monad m) => Int -> m a -> m ()
233 replicateM_ n x = sequence_ (replicate n x)
234
235 {- | Conditional execution of monadic expressions. For example,
236
237 > when debug (putStr "Debugging\n")
238
239 will output the string @Debugging\\n@ if the Boolean value @debug@ is 'True',
240 and otherwise do nothing.
241 -}
242
243 when :: (Monad m) => Bool -> m () -> m ()
244 when p s = if p then s else return ()
245
246 -- | The reverse of 'when'.
247
248 unless :: (Monad m) => Bool -> m () -> m ()
249 unless p s = if p then return () else s
250
251 -- | Promote a function to a monad.
252 liftM :: (Monad m) => (a1 -> r) -> m a1 -> m r
253 liftM f m1 = do { x1 <- m1; return (f x1) }
254
255 -- | Promote a function to a monad, scanning the monadic arguments from
256 -- left to right. For example,
257 --
258 -- > liftM2 (+) [0,1] [0,2] = [0,2,1,3]
259 -- > liftM2 (+) (Just 1) Nothing = Nothing
260 --
261 liftM2 :: (Monad m) => (a1 -> a2 -> r) -> m a1 -> m a2 -> m r
262 liftM2 f m1 m2 = do { x1 <- m1; x2 <- m2; return (f x1 x2) }
263
264 -- | Promote a function to a monad, scanning the monadic arguments from
265 -- left to right (cf. 'liftM2').
266 liftM3 :: (Monad m) => (a1 -> a2 -> a3 -> r) -> m a1 -> m a2 -> m a3 -> m r
267 liftM3 f m1 m2 m3 = do { x1 <- m1; x2 <- m2; x3 <- m3; return (f x1 x2 x3) }
268
269 -- | Promote a function to a monad, scanning the monadic arguments from
270 -- left to right (cf. 'liftM2').
271 liftM4 :: (Monad m) => (a1 -> a2 -> a3 -> a4 -> r) -> m a1 -> m a2 -> m a3 -> m a4 -> m r
272 liftM4 f m1 m2 m3 m4 = do { x1 <- m1; x2 <- m2; x3 <- m3; x4 <- m4; return (f x1 x2 x3 x4) }
273
274 -- | Promote a function to a monad, scanning the monadic arguments from
275 -- left to right (cf. 'liftM2').
276 liftM5 :: (Monad m) => (a1 -> a2 -> a3 -> a4 -> a5 -> r) -> m a1 -> m a2 -> m a3 -> m a4 -> m a5 -> m r
277 liftM5 f m1 m2 m3 m4 m5 = do { x1 <- m1; x2 <- m2; x3 <- m3; x4 <- m4; x5 <- m5; return (f x1 x2 x3 x4 x5) }
278
279 {- | In many situations, the 'liftM' operations can be replaced by uses of
280 'ap', which promotes function application.
281
282 > return f `ap` x1 `ap` ... `ap` xn
283
284 is equivalent to
285
286 > liftMn f x1 x2 ... xn
287
288 -}
289
290 ap :: (Monad m) => m (a -> b) -> m a -> m b
291 ap = liftM2 id
292
293 {- $naming
294
295 The functions in this library use the following naming conventions:
296
297 * A postfix \'@M@\' always stands for a function in the Kleisli category:
298 The monad type constructor @m@ is added to function results
299 (modulo currying) and nowhere else. So, for example,
300
301 > filter :: (a -> Bool) -> [a] -> [a]
302 > filterM :: (Monad m) => (a -> m Bool) -> [a] -> m [a]
303
304 * A postfix \'@_@\' changes the result type from @(m a)@ to @(m ())@.
305 Thus, for example:
306
307 > sequence :: Monad m => [m a] -> m [a]
308 > sequence_ :: Monad m => [m a] -> m ()
309
310 * A prefix \'@m@\' generalizes an existing function to a monadic form.
311 Thus, for example:
312
313 > sum :: Num a => [a] -> a
314 > msum :: MonadPlus m => [m a] -> m a
315
316 -}