1 {-# OPTIONS_GHC -fno-implicit-prelude #-}
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 functions from the "Prelude"
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
41 -- ** Generalisations of list functions
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 ()
54 -- ** Conditional execution of monadic expressions
56 , guard -- :: (MonadPlus m) => Bool -> m ()
57 , when -- :: (Monad m) => Bool -> m () -> m ()
58 , unless -- :: (Monad m) => Bool -> m () -> m ()
60 -- ** Monadic lifting operators
62 , liftM -- :: (Monad m) => (a -> b) -> (m a -> m b)
63 , liftM2 -- :: (Monad m) => (a -> b -> c) -> (m a -> m b -> m c)
68 , ap -- :: (Monad m) => m (a -> b) -> m a -> m b
74 #ifdef __GLASGOW_HASKELL__
79 #ifdef __GLASGOW_HASKELL__
82 -- -----------------------------------------------------------------------------
83 -- Prelude monad functions
85 -- | Same as '>>=', but with the arguments interchanged.
86 {-# SPECIALISE (=<<) :: (a -> [b]) -> [a] -> [b] #-}
87 (=<<) :: Monad m => (a -> m b) -> m a -> m b
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
96 k m m' = do { x <- m; xs <- m'; return (x:xs) }
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
104 -- | @'mapM' f@ is equivalent to @'sequence' . 'map' f@.
105 mapM :: Monad m => (a -> m b) -> [a] -> m [b]
107 mapM f as = sequence (map f as)
109 -- | @'mapM_' f@ is equivalent to @'sequence_' . 'map' f@.
110 mapM_ :: Monad m => (a -> m b) -> [a] -> m ()
112 mapM_ f as = sequence_ (map f as)
114 #endif /* __GLASGOW_HASKELL__ */
116 -- -----------------------------------------------------------------------------
117 -- The MonadPlus class definition
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
123 -- > mzero >>= f = mzero
124 -- > v >> mzero = mzero
126 -- (but the instance for 'System.IO.IO' defined in "Control.Monad.Error"
127 -- does not satisfy the second one).
129 -- | an associative operation
130 mplus :: m a -> m a -> m a
132 instance MonadPlus [] where
136 instance MonadPlus Maybe where
139 Nothing `mplus` ys = ys
142 -- -----------------------------------------------------------------------------
143 -- Functions mandated by the Prelude
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 ()
151 -- | This generalizes the list-based 'filter' function.
153 filterM :: (Monad m) => (a -> m Bool) -> [a] -> m [a]
154 filterM _ [] = return []
155 filterM p (x:xs) = do
158 return (if flg then x:ys else ys)
160 -- | 'forM' is 'mapM' with its arguments flipped
161 forM :: Monad m => [a] -> (a -> m b) -> m [b]
165 -- | 'forM_' is 'mapM_' with its arguments flipped
166 forM_ :: Monad m => [a] -> (a -> m b) -> m ()
170 -- | This generalizes the list-based 'concat' function.
172 msum :: MonadPlus m => [m a] -> m a
174 msum = foldr mplus mzero
176 -- -----------------------------------------------------------------------------
177 -- Other monad functions
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
182 join :: (Monad m) => m (m a) -> m a
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
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)
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)
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.
205 > foldM f a1 [x1, x2, ..., xm ]
215 If right-to-left evaluation is required, the input list should be reversed.
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
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 ()
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)
231 -- | Like 'replicateM', but discards the result.
232 replicateM_ :: (Monad m) => Int -> m a -> m ()
233 replicateM_ n x = sequence_ (replicate n x)
235 {- | Conditional execution of monadic expressions. For example,
237 > when debug (putStr "Debugging\n")
239 will output the string @Debugging\\n@ if the Boolean value @debug@ is 'True',
240 and otherwise do nothing.
243 when :: (Monad m) => Bool -> m () -> m ()
244 when p s = if p then s else return ()
246 -- | The reverse of 'when'.
248 unless :: (Monad m) => Bool -> m () -> m ()
249 unless p s = if p then return () else s
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) }
255 -- | Promote a function to a monad, scanning the monadic arguments from
256 -- left to right. For example,
258 -- > liftM2 (+) [0,1] [0,2] = [0,2,1,3]
259 -- > liftM2 (+) (Just 1) Nothing = Nothing
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) }
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) }
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) }
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) }
279 {- | In many situations, the 'liftM' operations can be replaced by uses of
280 'ap', which promotes function application.
282 > return f `ap` x1 `ap` ... `ap` xn
286 > liftMn f x1 x2 ... xn
290 ap :: (Monad m) => m (a -> b) -> m a -> m b
295 The functions in this library use the following naming conventions:
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,
301 > filter :: (a -> Bool) -> [a] -> [a]
302 > filterM :: (Monad m) => (a -> m Bool) -> [a] -> m [a]
304 * A postfix \'@_@\' changes the result type from @(m a)@ to @(m ())@.
307 > sequence :: Monad m => [m a] -> m [a]
308 > sequence_ :: Monad m => [m a] -> m ()
310 * A prefix \'@m@\' generalizes an existing function to a monadic form.
313 > sum :: Num a => [a] -> a
314 > msum :: MonadPlus m => [m a] -> m a