1 {-# OPTIONS -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
61 , liftM -- :: (Monad m) => (a -> b) -> (m a -> m b)
62 , liftM2 -- :: (Monad m) => (a -> b -> c) -> (m a -> m b -> m c)
67 , ap -- :: (Monad m) => m (a -> b) -> m a -> m b
73 #ifdef __GLASGOW_HASKELL__
78 #ifdef __GLASGOW_HASKELL__
81 -- -----------------------------------------------------------------------------
82 -- Prelude monad functions
84 {-# SPECIALISE (=<<) :: (a -> [b]) -> [a] -> [b] #-}
85 (=<<) :: Monad m => (a -> m b) -> m a -> m b
88 sequence :: Monad m => [m a] -> m [a]
89 {-# INLINE sequence #-}
90 sequence ms = foldr k (return []) ms
92 k m m' = do { x <- m; xs <- m'; return (x:xs) }
94 sequence_ :: Monad m => [m a] -> m ()
95 {-# INLINE sequence_ #-}
96 sequence_ ms = foldr (>>) (return ()) ms
98 mapM :: Monad m => (a -> m b) -> [a] -> m [b]
100 mapM f as = sequence (map f as)
102 mapM_ :: Monad m => (a -> m b) -> [a] -> m ()
104 mapM_ f as = sequence_ (map f as)
105 #endif /* __GLASGOW_HASKELL__ */
107 -- -----------------------------------------------------------------------------
108 -- |The MonadPlus class definition
110 class Monad m => MonadPlus m where
112 mplus :: m a -> m a -> m a
114 instance MonadPlus [] where
118 instance MonadPlus Maybe where
121 Nothing `mplus` ys = ys
124 -- -----------------------------------------------------------------------------
125 -- Functions mandated by the Prelude
127 guard :: (MonadPlus m) => Bool -> m ()
128 guard True = return ()
131 -- This subsumes the list-based filter function.
133 filterM :: (Monad m) => (a -> m Bool) -> [a] -> m [a]
134 filterM _ [] = return []
135 filterM p (x:xs) = do
138 return (if flg then x:ys else ys)
140 -- This subsumes the list-based concat function.
142 msum :: MonadPlus m => [m a] -> m a
144 msum = foldr mplus mzero
146 -- -----------------------------------------------------------------------------
147 -- Other monad functions
149 -- | The 'join' function is the conventional monad join operator. It is used to
150 -- remove one level of monadic structure, projecting its bound argument into the
152 join :: (Monad m) => m (m a) -> m a
155 -- | The 'mapAndUnzipM' function maps its first argument over a list, returning
156 -- the result as a pair of lists. This function is mainly used with complicated
157 -- data structures or a state-transforming monad.
158 mapAndUnzipM :: (Monad m) => (a -> m (b,c)) -> [a] -> m ([b], [c])
159 mapAndUnzipM f xs = sequence (map f xs) >>= return . unzip
161 -- | The 'zipWithM' function generalises 'zipWith' to arbitrary monads.
162 zipWithM :: (Monad m) => (a -> b -> m c) -> [a] -> [b] -> m [c]
163 zipWithM f xs ys = sequence (zipWith f xs ys)
165 -- | 'zipWithM_' is the extension of 'zipWithM' which ignores the final result.
166 zipWithM_ :: (Monad m) => (a -> b -> m c) -> [a] -> [b] -> m ()
167 zipWithM_ f xs ys = sequence_ (zipWith f xs ys)
169 {- | The 'foldM' function is analogous to 'foldl', except that its result is
170 encapsulated in a monad. Note that 'foldM' works from left-to-right over
171 the list arguments. This could be an issue where '(>>)' and the `folded
172 function' are not commutative.
175 > foldM f a1 [x1, x2, ..., xm ]
185 If right-to-left evaluation is required, the input list should be reversed.
188 foldM :: (Monad m) => (a -> b -> m a) -> a -> [b] -> m a
189 foldM _ a [] = return a
190 foldM f a (x:xs) = f a x >>= \fax -> foldM f fax xs
192 foldM_ :: (Monad m) => (a -> b -> m a) -> a -> [b] -> m ()
193 foldM_ f a xs = foldM f a xs >> return ()
195 replicateM :: (Monad m) => Int -> m a -> m [a]
196 replicateM n x = sequence (replicate n x)
198 replicateM_ :: (Monad m) => Int -> m a -> m ()
199 replicateM_ n x = sequence_ (replicate n x)
201 {- | Conditional execution of monadic expressions. For example,
203 > when debug (putStr "Debugging\n")
205 will output the string @Debugging\\n@ if the Boolean value @debug@ is 'True',
206 and otherwise do nothing.
209 when :: (Monad m) => Bool -> m () -> m ()
210 when p s = if p then s else return ()
212 -- | The reverse of 'when'.
214 unless :: (Monad m) => Bool -> m () -> m ()
215 unless p s = if p then return () else s
219 The monadic lifting operators promote a function to a monad.
220 The function arguments are scanned left to right. For example,
222 > liftM2 (+) [0,1] [0,2] = [0,2,1,3]
223 > liftM2 (+) (Just 1) Nothing = Nothing
227 liftM :: (Monad m) => (a1 -> r) -> m a1 -> m r
228 liftM2 :: (Monad m) => (a1 -> a2 -> r) -> m a1 -> m a2 -> m r
229 liftM3 :: (Monad m) => (a1 -> a2 -> a3 -> r) -> m a1 -> m a2 -> m a3 -> m r
230 liftM4 :: (Monad m) => (a1 -> a2 -> a3 -> a4 -> r) -> m a1 -> m a2 -> m a3 -> m a4 -> m r
231 liftM5 :: (Monad m) => (a1 -> a2 -> a3 -> a4 -> a5 -> r) -> m a1 -> m a2 -> m a3 -> m a4 -> m a5 -> m r
233 liftM f m1 = do { x1 <- m1; return (f x1) }
234 liftM2 f m1 m2 = do { x1 <- m1; x2 <- m2; return (f x1 x2) }
235 liftM3 f m1 m2 m3 = do { x1 <- m1; x2 <- m2; x3 <- m3; return (f x1 x2 x3) }
236 liftM4 f m1 m2 m3 m4 = do { x1 <- m1; x2 <- m2; x3 <- m3; x4 <- m4; return (f x1 x2 x3 x4) }
237 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) }
239 {- | In many situations, the 'liftM' operations can be replaced by uses of
240 'ap', which promotes function application.
242 > return f `ap` x1 `ap` ... `ap` xn
246 > liftMn f x1 x2 ... xn
250 ap :: (Monad m) => m (a -> b) -> m a -> m b
255 The functions in this library use the following naming conventions:
257 * A postfix \`M\' always stands for a function in the Kleisli category:
258 @m@ is added to function results (modulo currying) and nowhere else.
261 > filter :: (a -> Bool) -> [a] -> [a]
262 > filterM :: (Monad m) => (a -> m Bool) -> [a] -> m [a]
264 * A postfix \`_\' changes the result type from @(m a)@ to @(m ())@.
265 Thus (in the "Prelude"):
267 > sequence :: Monad m => [m a] -> m [a]
268 > sequence_ :: Monad m => [m a] -> m ()
270 * A prefix \`m\' generalises an existing function to a monadic form.
273 > sum :: Num a => [a] -> a
274 > msum :: MonadPlus m => [m a] -> m a