1 {-# OPTIONS_GHC -XNoImplicitPrelude #-}
2 -----------------------------------------------------------------------------
4 -- Module : Control.Monad
5 -- Copyright : (c) The University of Glasgow 2001
6 -- License : BSD-style (see the file libraries/base/LICENSE)
8 -- Maintainer : libraries@haskell.org
9 -- Stability : provisional
10 -- Portability : portable
12 -- The 'Functor', 'Monad' and 'MonadPlus' classes,
13 -- with some useful operations on monads.
17 -- * Functor and monad classes
20 , Monad((>>=), (>>), return, fail)
22 , MonadPlus ( -- class context: Monad
23 mzero -- :: (MonadPlus m) => m a
24 , mplus -- :: (MonadPlus m) => m a -> m a -> m a
28 -- ** Naming conventions
31 -- ** Basic @Monad@ functions
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 , (>=>) -- :: (Monad m) => (a -> m b) -> (b -> m c) -> (a -> m c)
41 , (<=<) -- :: (Monad m) => (b -> m c) -> (a -> m b) -> (a -> m c)
42 , forever -- :: (Monad m) => m a -> m b
45 -- ** Generalisations of list functions
47 , join -- :: (Monad m) => m (m a) -> m a
48 , msum -- :: (MonadPlus m) => [m a] -> m a
49 , mfilter -- :: (MonadPlus m) => (a -> Bool) -> m a -> m a
50 , filterM -- :: (Monad m) => (a -> m Bool) -> [a] -> m [a]
51 , mapAndUnzipM -- :: (Monad m) => (a -> m (b,c)) -> [a] -> m ([b], [c])
52 , zipWithM -- :: (Monad m) => (a -> b -> m c) -> [a] -> [b] -> m [c]
53 , zipWithM_ -- :: (Monad m) => (a -> b -> m c) -> [a] -> [b] -> m ()
54 , foldM -- :: (Monad m) => (a -> b -> m a) -> a -> [b] -> m a
55 , foldM_ -- :: (Monad m) => (a -> b -> m a) -> a -> [b] -> m ()
56 , replicateM -- :: (Monad m) => Int -> m a -> m [a]
57 , replicateM_ -- :: (Monad m) => Int -> m a -> m ()
59 -- ** Conditional execution of monadic expressions
61 , guard -- :: (MonadPlus m) => Bool -> m ()
62 , when -- :: (Monad m) => Bool -> m () -> m ()
63 , unless -- :: (Monad m) => Bool -> m () -> m ()
65 -- ** Monadic lifting operators
67 , liftM -- :: (Monad m) => (a -> b) -> (m a -> m b)
68 , liftM2 -- :: (Monad m) => (a -> b -> c) -> (m a -> m b -> m c)
73 , ap -- :: (Monad m) => m (a -> b) -> m a -> m b
79 #ifdef __GLASGOW_HASKELL__
84 #ifdef __GLASGOW_HASKELL__
87 -- -----------------------------------------------------------------------------
88 -- Prelude monad functions
90 -- | Same as '>>=', but with the arguments interchanged.
91 {-# SPECIALISE (=<<) :: (a -> [b]) -> [a] -> [b] #-}
92 (=<<) :: Monad m => (a -> m b) -> m a -> m b
95 -- | Evaluate each action in the sequence from left to right,
96 -- and collect the results.
97 sequence :: Monad m => [m a] -> m [a]
98 {-# INLINE sequence #-}
99 sequence ms = foldr k (return []) ms
101 k m m' = do { x <- m; xs <- m'; return (x:xs) }
103 -- | Evaluate each action in the sequence from left to right,
104 -- and ignore the results.
105 sequence_ :: Monad m => [m a] -> m ()
106 {-# INLINE sequence_ #-}
107 sequence_ ms = foldr (>>) (return ()) ms
109 -- | @'mapM' f@ is equivalent to @'sequence' . 'map' f@.
110 mapM :: Monad m => (a -> m b) -> [a] -> m [b]
112 mapM f as = sequence (map f as)
114 -- | @'mapM_' f@ is equivalent to @'sequence_' . 'map' f@.
115 mapM_ :: Monad m => (a -> m b) -> [a] -> m ()
117 mapM_ f as = sequence_ (map f as)
119 #endif /* __GLASGOW_HASKELL__ */
121 -- -----------------------------------------------------------------------------
122 -- The MonadPlus class definition
124 -- | Monads that also support choice and failure.
125 class Monad m => MonadPlus m where
126 -- | the identity of 'mplus'. It should also satisfy the equations
128 -- > mzero >>= f = mzero
129 -- > v >> mzero = mzero
132 -- | an associative operation
133 mplus :: m a -> m a -> m a
135 instance MonadPlus [] where
139 instance MonadPlus Maybe where
142 Nothing `mplus` ys = ys
145 -- -----------------------------------------------------------------------------
146 -- Functions mandated by the Prelude
148 -- | @'guard' b@ is @'return' ()@ if @b@ is 'True',
149 -- and 'mzero' if @b@ is 'False'.
150 guard :: (MonadPlus m) => Bool -> m ()
151 guard True = return ()
154 -- | This generalizes the list-based 'filter' function.
156 filterM :: (Monad m) => (a -> m Bool) -> [a] -> m [a]
157 filterM _ [] = return []
158 filterM p (x:xs) = do
161 return (if flg then x:ys else ys)
163 -- | 'forM' is 'mapM' with its arguments flipped
164 forM :: Monad m => [a] -> (a -> m b) -> m [b]
168 -- | 'forM_' is 'mapM_' with its arguments flipped
169 forM_ :: Monad m => [a] -> (a -> m b) -> m ()
173 -- | This generalizes the list-based 'concat' function.
175 msum :: MonadPlus m => [m a] -> m a
177 msum = foldr mplus mzero
181 -- | Left-to-right Kleisli composition of monads.
182 (>=>) :: Monad m => (a -> m b) -> (b -> m c) -> (a -> m c)
183 f >=> g = \x -> f x >>= g
185 -- | Right-to-left Kleisli composition of monads. @('>=>')@, with the arguments flipped
186 (<=<) :: Monad m => (b -> m c) -> (a -> m b) -> (a -> m c)
189 -- | @'forever' act@ repeats the action infinitely.
190 forever :: (Monad m) => m a -> m b
191 forever a = a >> forever a
193 -- | @'void' value@ discards or ignores the result of evaluation, such as the return value of an 'IO' action.
194 void :: Functor f => f a -> f ()
195 void = fmap (const ())
197 -- -----------------------------------------------------------------------------
198 -- Other monad functions
200 -- | The 'join' function is the conventional monad join operator. It is used to
201 -- remove one level of monadic structure, projecting its bound argument into the
203 join :: (Monad m) => m (m a) -> m a
206 -- | The 'mapAndUnzipM' function maps its first argument over a list, returning
207 -- the result as a pair of lists. This function is mainly used with complicated
208 -- data structures or a state-transforming monad.
209 mapAndUnzipM :: (Monad m) => (a -> m (b,c)) -> [a] -> m ([b], [c])
210 mapAndUnzipM f xs = sequence (map f xs) >>= return . unzip
212 -- | The 'zipWithM' function generalizes 'zipWith' to arbitrary monads.
213 zipWithM :: (Monad m) => (a -> b -> m c) -> [a] -> [b] -> m [c]
214 zipWithM f xs ys = sequence (zipWith f xs ys)
216 -- | 'zipWithM_' is the extension of 'zipWithM' which ignores the final result.
217 zipWithM_ :: (Monad m) => (a -> b -> m c) -> [a] -> [b] -> m ()
218 zipWithM_ f xs ys = sequence_ (zipWith f xs ys)
220 {- | The 'foldM' function is analogous to 'foldl', except that its result is
221 encapsulated in a monad. Note that 'foldM' works from left-to-right over
222 the list arguments. This could be an issue where @('>>')@ and the `folded
223 function' are not commutative.
226 > foldM f a1 [x1, x2, ..., xm]
236 If right-to-left evaluation is required, the input list should be reversed.
239 foldM :: (Monad m) => (a -> b -> m a) -> a -> [b] -> m a
240 foldM _ a [] = return a
241 foldM f a (x:xs) = f a x >>= \fax -> foldM f fax xs
243 -- | Like 'foldM', but discards the result.
244 foldM_ :: (Monad m) => (a -> b -> m a) -> a -> [b] -> m ()
245 foldM_ f a xs = foldM f a xs >> return ()
247 -- | @'replicateM' n act@ performs the action @n@ times,
248 -- gathering the results.
249 replicateM :: (Monad m) => Int -> m a -> m [a]
250 replicateM n x = sequence (replicate n x)
252 -- | Like 'replicateM', but discards the result.
253 replicateM_ :: (Monad m) => Int -> m a -> m ()
254 replicateM_ n x = sequence_ (replicate n x)
256 {- | Conditional execution of monadic expressions. For example,
258 > when debug (putStr "Debugging\n")
260 will output the string @Debugging\\n@ if the Boolean value @debug@ is 'True',
261 and otherwise do nothing.
264 when :: (Monad m) => Bool -> m () -> m ()
265 when p s = if p then s else return ()
267 -- | The reverse of 'when'.
269 unless :: (Monad m) => Bool -> m () -> m ()
270 unless p s = if p then return () else s
272 -- | Promote a function to a monad.
273 liftM :: (Monad m) => (a1 -> r) -> m a1 -> m r
274 liftM f m1 = do { x1 <- m1; return (f x1) }
276 -- | Promote a function to a monad, scanning the monadic arguments from
277 -- left to right. For example,
279 -- > liftM2 (+) [0,1] [0,2] = [0,2,1,3]
280 -- > liftM2 (+) (Just 1) Nothing = Nothing
282 liftM2 :: (Monad m) => (a1 -> a2 -> r) -> m a1 -> m a2 -> m r
283 liftM2 f m1 m2 = do { x1 <- m1; x2 <- m2; return (f x1 x2) }
285 -- | Promote a function to a monad, scanning the monadic arguments from
286 -- left to right (cf. 'liftM2').
287 liftM3 :: (Monad m) => (a1 -> a2 -> a3 -> r) -> m a1 -> m a2 -> m a3 -> m r
288 liftM3 f m1 m2 m3 = do { x1 <- m1; x2 <- m2; x3 <- m3; return (f x1 x2 x3) }
290 -- | Promote a function to a monad, scanning the monadic arguments from
291 -- left to right (cf. 'liftM2').
292 liftM4 :: (Monad m) => (a1 -> a2 -> a3 -> a4 -> r) -> m a1 -> m a2 -> m a3 -> m a4 -> m r
293 liftM4 f m1 m2 m3 m4 = do { x1 <- m1; x2 <- m2; x3 <- m3; x4 <- m4; return (f x1 x2 x3 x4) }
295 -- | Promote a function to a monad, scanning the monadic arguments from
296 -- left to right (cf. 'liftM2').
297 liftM5 :: (Monad m) => (a1 -> a2 -> a3 -> a4 -> a5 -> r) -> m a1 -> m a2 -> m a3 -> m a4 -> m a5 -> m r
298 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) }
300 {- | In many situations, the 'liftM' operations can be replaced by uses of
301 'ap', which promotes function application.
303 > return f `ap` x1 `ap` ... `ap` xn
307 > liftMn f x1 x2 ... xn
311 ap :: (Monad m) => m (a -> b) -> m a -> m b
315 -- -----------------------------------------------------------------------------
316 -- Other MonadPlus functions
318 -- | Direct 'MonadPlus' equivalent of 'filter'
319 -- @'filter'@ = @(mfilter:: (a -> Bool) -> [a] -> [a]@
320 -- applicable to any 'MonadPlus', for example
321 -- @mfilter odd (Just 1) == Just 1@
322 -- @mfilter odd (Just 2) == Nothing@
324 mfilter :: (MonadPlus m) => (a -> Bool) -> m a -> m a
327 if p a then return a else mzero
331 The functions in this library use the following naming conventions:
333 * A postfix \'@M@\' always stands for a function in the Kleisli category:
334 The monad type constructor @m@ is added to function results
335 (modulo currying) and nowhere else. So, for example,
337 > filter :: (a -> Bool) -> [a] -> [a]
338 > filterM :: (Monad m) => (a -> m Bool) -> [a] -> m [a]
340 * A postfix \'@_@\' changes the result type from @(m a)@ to @(m ())@.
343 > sequence :: Monad m => [m a] -> m [a]
344 > sequence_ :: Monad m => [m a] -> m ()
346 * A prefix \'@m@\' generalizes an existing function to a monadic form.
349 > sum :: Num a => [a] -> a
350 > msum :: MonadPlus m => [m a] -> m a