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 , sequence -- :: (Monad m) => [m a] -> m [a]
36 , sequence_ -- :: (Monad m) => [m a] -> m ()
37 , (=<<) -- :: (Monad m) => (a -> m b) -> m a -> m b
39 -- ** Generalisations of list functions
41 , join -- :: (Monad m) => m (m a) -> m a
42 , msum -- :: (MonadPlus m) => [m a] -> m a
43 , filterM -- :: (Monad m) => (a -> m Bool) -> [a] -> m [a]
44 , mapAndUnzipM -- :: (Monad m) => (a -> m (b,c)) -> [a] -> m ([b], [c])
45 , zipWithM -- :: (Monad m) => (a -> b -> m c) -> [a] -> [b] -> m [c]
46 , zipWithM_ -- :: (Monad m) => (a -> b -> m c) -> [a] -> [b] -> m ()
47 , foldM -- :: (Monad m) => (a -> b -> m a) -> a -> [b] -> m a
48 , foldM_ -- :: (Monad m) => (a -> b -> m a) -> a -> [b] -> m ()
49 , replicateM -- :: (Monad m) => Int -> m a -> m [a]
50 , replicateM_ -- :: (Monad m) => Int -> m a -> m ()
52 -- ** Conditional execution of monadic expressions
54 , guard -- :: (MonadPlus m) => Bool -> m ()
55 , when -- :: (Monad m) => Bool -> m () -> m ()
56 , unless -- :: (Monad m) => Bool -> m () -> m ()
58 -- ** Monadic lifting operators
60 , liftM -- :: (Monad m) => (a -> b) -> (m a -> m b)
61 , liftM2 -- :: (Monad m) => (a -> b -> c) -> (m a -> m b -> m c)
66 , ap -- :: (Monad m) => m (a -> b) -> m a -> m b
72 #ifdef __GLASGOW_HASKELL__
77 #ifdef __GLASGOW_HASKELL__
80 -- -----------------------------------------------------------------------------
81 -- Prelude monad functions
83 -- | Same as '>>=', but with the arguments interchanged.
84 {-# SPECIALISE (=<<) :: (a -> [b]) -> [a] -> [b] #-}
85 (=<<) :: Monad m => (a -> m b) -> m a -> m b
88 -- | Evaluate each action in the sequence from left to right,
89 -- and collect the results.
90 sequence :: Monad m => [m a] -> m [a]
91 {-# INLINE sequence #-}
92 sequence ms = foldr k (return []) ms
94 k m m' = do { x <- m; xs <- m'; return (x:xs) }
96 -- | Evaluate each action in the sequence from left to right,
97 -- and ignore the results.
98 sequence_ :: Monad m => [m a] -> m ()
99 {-# INLINE sequence_ #-}
100 sequence_ ms = foldr (>>) (return ()) ms
102 -- | @'mapM' f@ is equivalent to @'sequence' . 'map' f@.
103 mapM :: Monad m => (a -> m b) -> [a] -> m [b]
105 mapM f as = sequence (map f as)
107 -- | @'mapM_' f@ is equivalent to @'sequence_' . 'map' f@.
108 mapM_ :: Monad m => (a -> m b) -> [a] -> m ()
110 mapM_ f as = sequence_ (map f as)
111 #endif /* __GLASGOW_HASKELL__ */
113 -- -----------------------------------------------------------------------------
114 -- The MonadPlus class definition
116 -- | Monads that also support choice and failure.
117 class Monad m => MonadPlus m where
118 -- | the identity of 'mplus'. It should also satisfy the equations
120 -- > mzero >>= f = mzero
121 -- > v >> mzero = mzero
123 -- (but the instance for 'System.IO.IO' defined in "Control.Monad.Error"
124 -- does not satisfy the second one).
126 -- | an associative operation
127 mplus :: m a -> m a -> m a
129 instance MonadPlus [] where
133 instance MonadPlus Maybe where
136 Nothing `mplus` ys = ys
139 -- -----------------------------------------------------------------------------
140 -- Functions mandated by the Prelude
142 -- | @'guard' b@ is @'return' ()@ if @b@ is 'True',
143 -- and 'mzero' if @b@ is 'False'.
144 guard :: (MonadPlus m) => Bool -> m ()
145 guard True = return ()
148 -- | This generalizes the list-based 'filter' function.
150 filterM :: (Monad m) => (a -> m Bool) -> [a] -> m [a]
151 filterM _ [] = return []
152 filterM p (x:xs) = do
155 return (if flg then x:ys else ys)
157 -- | This generalizes the list-based 'concat' function.
159 msum :: MonadPlus m => [m a] -> m a
161 msum = foldr mplus mzero
163 -- -----------------------------------------------------------------------------
164 -- Other monad functions
166 -- | The 'join' function is the conventional monad join operator. It is used to
167 -- remove one level of monadic structure, projecting its bound argument into the
169 join :: (Monad m) => m (m a) -> m a
172 -- | The 'mapAndUnzipM' function maps its first argument over a list, returning
173 -- the result as a pair of lists. This function is mainly used with complicated
174 -- data structures or a state-transforming monad.
175 mapAndUnzipM :: (Monad m) => (a -> m (b,c)) -> [a] -> m ([b], [c])
176 mapAndUnzipM f xs = sequence (map f xs) >>= return . unzip
178 -- | The 'zipWithM' function generalizes 'zipWith' to arbitrary monads.
179 zipWithM :: (Monad m) => (a -> b -> m c) -> [a] -> [b] -> m [c]
180 zipWithM f xs ys = sequence (zipWith f xs ys)
182 -- | 'zipWithM_' is the extension of 'zipWithM' which ignores the final result.
183 zipWithM_ :: (Monad m) => (a -> b -> m c) -> [a] -> [b] -> m ()
184 zipWithM_ f xs ys = sequence_ (zipWith f xs ys)
186 {- | The 'foldM' function is analogous to 'foldl', except that its result is
187 encapsulated in a monad. Note that 'foldM' works from left-to-right over
188 the list arguments. This could be an issue where '(>>)' and the `folded
189 function' are not commutative.
192 > foldM f a1 [x1, x2, ..., xm ]
202 If right-to-left evaluation is required, the input list should be reversed.
205 foldM :: (Monad m) => (a -> b -> m a) -> a -> [b] -> m a
206 foldM _ a [] = return a
207 foldM f a (x:xs) = f a x >>= \fax -> foldM f fax xs
209 -- | Like 'foldM', but discards the result.
210 foldM_ :: (Monad m) => (a -> b -> m a) -> a -> [b] -> m ()
211 foldM_ f a xs = foldM f a xs >> return ()
213 -- | @'replicateM' n act@ performs the action @n@ times,
214 -- gathering the results.
215 replicateM :: (Monad m) => Int -> m a -> m [a]
216 replicateM n x = sequence (replicate n x)
218 -- | Like 'replicateM', but discards the result.
219 replicateM_ :: (Monad m) => Int -> m a -> m ()
220 replicateM_ n x = sequence_ (replicate n x)
222 {- | Conditional execution of monadic expressions. For example,
224 > when debug (putStr "Debugging\n")
226 will output the string @Debugging\\n@ if the Boolean value @debug@ is 'True',
227 and otherwise do nothing.
230 when :: (Monad m) => Bool -> m () -> m ()
231 when p s = if p then s else return ()
233 -- | The reverse of 'when'.
235 unless :: (Monad m) => Bool -> m () -> m ()
236 unless p s = if p then return () else s
238 -- | Promote a function to a monad.
239 liftM :: (Monad m) => (a1 -> r) -> m a1 -> m r
240 liftM f m1 = do { x1 <- m1; return (f x1) }
242 -- | Promote a function to a monad, scanning the monadic arguments from
243 -- left to right. For example,
245 -- > liftM2 (+) [0,1] [0,2] = [0,2,1,3]
246 -- > liftM2 (+) (Just 1) Nothing = Nothing
248 liftM2 :: (Monad m) => (a1 -> a2 -> r) -> m a1 -> m a2 -> m r
249 liftM2 f m1 m2 = do { x1 <- m1; x2 <- m2; return (f x1 x2) }
251 -- | Promote a function to a monad, scanning the monadic arguments from
252 -- left to right (cf. 'liftM2').
253 liftM3 :: (Monad m) => (a1 -> a2 -> a3 -> r) -> m a1 -> m a2 -> m a3 -> m r
254 liftM3 f m1 m2 m3 = do { x1 <- m1; x2 <- m2; x3 <- m3; return (f x1 x2 x3) }
256 -- | Promote a function to a monad, scanning the monadic arguments from
257 -- left to right (cf. 'liftM2').
258 liftM4 :: (Monad m) => (a1 -> a2 -> a3 -> a4 -> r) -> m a1 -> m a2 -> m a3 -> m a4 -> m r
259 liftM4 f m1 m2 m3 m4 = do { x1 <- m1; x2 <- m2; x3 <- m3; x4 <- m4; return (f x1 x2 x3 x4) }
261 -- | Promote a function to a monad, scanning the monadic arguments from
262 -- left to right (cf. 'liftM2').
263 liftM5 :: (Monad m) => (a1 -> a2 -> a3 -> a4 -> a5 -> r) -> m a1 -> m a2 -> m a3 -> m a4 -> m a5 -> m r
264 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) }
266 {- | In many situations, the 'liftM' operations can be replaced by uses of
267 'ap', which promotes function application.
269 > return f `ap` x1 `ap` ... `ap` xn
273 > liftMn f x1 x2 ... xn
277 ap :: (Monad m) => m (a -> b) -> m a -> m b
282 The functions in this library use the following naming conventions:
284 * A postfix \'@M@\' always stands for a function in the Kleisli category:
285 The monad type constructor @m@ is added to function results
286 (modulo currying) and nowhere else. So, for example,
288 > filter :: (a -> Bool) -> [a] -> [a]
289 > filterM :: (Monad m) => (a -> m Bool) -> [a] -> m [a]
291 * A postfix \'@_@\' changes the result type from @(m a)@ to @(m ())@.
294 > sequence :: Monad m => [m a] -> m [a]
295 > sequence_ :: Monad m => [m a] -> m ()
297 * A prefix \'@m@\' generalizes an existing function to a monadic form.
300 > sum :: Num a => [a] -> a
301 > msum :: MonadPlus m => [m a] -> m a