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 'Monad' library defines the 'MonadPlus' class, and provides some useful operations on monads.
14 -- The functions in this library use the following naming conventions:
16 -- * A postfix `M' always stands for a function in the Kleisli category:
17 -- @m@ is added to function results (modulo currying) and nowhere else. So, for example,
19 -- > filter :: (a -> Bool) -> [a] -> [a]
20 -- > filterM :: (Monad m) => (a -> m Bool) -> [a] -> m [a]
22 -- * A postfix `_' changes the result type from @(m a)@ to @(m ())@. Thus (in the "Prelude"):
24 -- > sequence :: Monad m => [m a] -> m [a]
25 -- > sequence_ :: Monad m => [m a] -> m ()
27 -- * A prefix `m' generalises an existing function to a monadic form. Thus, for example:
29 -- > sum :: Num a => [a] -> a
30 -- > msum :: MonadPlus m => [m a] -> m a
33 ( MonadPlus ( -- class context: Monad
34 mzero -- :: (MonadPlus m) => m a
35 , mplus -- :: (MonadPlus m) => m a -> m a -> m a
37 , join -- :: (Monad m) => m (m a) -> m a
38 , guard -- :: (MonadPlus m) => Bool -> m ()
39 , when -- :: (Monad m) => Bool -> m () -> m ()
40 , unless -- :: (Monad m) => Bool -> m () -> m ()
41 , ap -- :: (Monad m) => m (a -> b) -> m a -> m b
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 ()
50 , liftM -- :: (Monad m) => (a -> b) -> (m a -> m b)
51 , liftM2 -- :: (Monad m) => (a -> b -> c) -> (m a -> m b -> m c)
56 , Monad((>>=), (>>), return, fail)
59 , mapM -- :: (Monad m) => (a -> m b) -> [a] -> m [b]
60 , mapM_ -- :: (Monad m) => (a -> m b) -> [a] -> m ()
61 , sequence -- :: (Monad m) => [m a] -> m [a]
62 , sequence_ -- :: (Monad m) => [m a] -> m ()
63 , replicateM -- :: (Monad m) => Int -> m a -> m [a]
64 , replicateM_ -- :: (Monad m) => Int -> m a -> m ()
65 , (=<<) -- :: (Monad m) => (a -> m b) -> m a -> m b
70 #ifdef __GLASGOW_HASKELL__
75 #ifdef __GLASGOW_HASKELL__
78 -- -----------------------------------------------------------------------------
79 -- Prelude monad functions
81 {-# SPECIALISE (=<<) :: (a -> [b]) -> [a] -> [b] #-}
82 (=<<) :: Monad m => (a -> m b) -> m a -> m b
85 sequence :: Monad m => [m a] -> m [a]
86 {-# INLINE sequence #-}
87 sequence ms = foldr k (return []) ms
89 k m m' = do { x <- m; xs <- m'; return (x:xs) }
91 sequence_ :: Monad m => [m a] -> m ()
92 {-# INLINE sequence_ #-}
93 sequence_ ms = foldr (>>) (return ()) ms
95 mapM :: Monad m => (a -> m b) -> [a] -> m [b]
97 mapM f as = sequence (map f as)
99 mapM_ :: Monad m => (a -> m b) -> [a] -> m ()
101 mapM_ f as = sequence_ (map f as)
102 #endif /* __GLASGOW_HASKELL__ */
104 -- -----------------------------------------------------------------------------
105 -- |The MonadPlus class definition
107 class Monad m => MonadPlus m where
109 mplus :: m a -> m a -> m a
111 instance MonadPlus [] where
115 instance MonadPlus Maybe where
118 Nothing `mplus` ys = ys
121 -- -----------------------------------------------------------------------------
122 -- Functions mandated by the Prelude
124 guard :: (MonadPlus m) => Bool -> m ()
125 guard True = return ()
128 -- This subsumes the list-based filter function.
130 filterM :: (Monad m) => (a -> m Bool) -> [a] -> m [a]
131 filterM _ [] = return []
132 filterM p (x:xs) = do
135 return (if flg then x:ys else ys)
137 -- This subsumes the list-based concat function.
139 msum :: MonadPlus m => [m a] -> m a
141 msum = foldr mplus mzero
143 -- -----------------------------------------------------------------------------
144 -- Other monad functions
146 -- | The 'join' function is the conventional monad join operator. It is used to
147 -- remove one level of monadic structure, projecting its bound argument into the
149 join :: (Monad m) => m (m a) -> m a
152 -- | The 'mapAndUnzipM' function maps its first argument over a list, returning
153 -- the result as a pair of lists. This function is mainly used with complicated
154 -- data structures or a state- transforming monad.
155 mapAndUnzipM :: (Monad m) => (a -> m (b,c)) -> [a] -> m ([b], [c])
156 mapAndUnzipM f xs = sequence (map f xs) >>= return . unzip
158 -- | The 'zipWithM' function generalises zipWith to arbitrary monads.
159 zipWithM :: (Monad m) => (a -> b -> m c) -> [a] -> [b] -> m [c]
160 zipWithM f xs ys = sequence (zipWith f xs ys)
162 -- | 'zipWithM_' is the extension of 'zipWithM' which ignores the final result.
163 zipWithM_ :: (Monad m) => (a -> b -> m c) -> [a] -> [b] -> m ()
164 zipWithM_ f xs ys = sequence_ (zipWith f xs ys)
166 {- | The 'foldM' function is analogous to 'foldl', except that its result is
167 encapsulated in a monad. Note that 'foldM' works from left-to-right over
168 the list arguments. This could be an issue where '(>>)' and the `folded
169 function' are not commutative.
172 > foldM f a1 [x1, x2, ..., xm ]
180 If right-to-left evaluation is required, the input list should be reversed.
182 The when and unless functions provide conditional execution of monadic expressions. For example,
184 > when debug (putStr "Debugging\n")
186 will output the string @Debugging\\n@ if the Boolean value @debug@ is @True@, and otherwise do nothing.
188 The monadic lifting operators promote a function to a monad. The function arguments are scanned left to right. For example,
190 > liftM2 (+) [0,1] [0,2] = [0,2,1,3]
191 > liftM2 (+) (Just 1) Nothing = Nothing
193 In many situations, the 'liftM' operations can be replaced by uses of 'ap', which promotes function application.
195 > return f `ap` x1 `ap` ... `ap` xn
199 > liftMn f x1 x2 ... xn
202 foldM :: (Monad m) => (a -> b -> m a) -> a -> [b] -> m a
203 foldM _ a [] = return a
204 foldM f a (x:xs) = f a x >>= \fax -> foldM f fax xs
206 foldM_ :: (Monad m) => (a -> b -> m a) -> a -> [b] -> m ()
207 foldM_ f a xs = foldM f a xs >> return ()
209 replicateM :: (Monad m) => Int -> m a -> m [a]
210 replicateM n x = sequence (replicate n x)
212 replicateM_ :: (Monad m) => Int -> m a -> m ()
213 replicateM_ n x = sequence_ (replicate n x)
215 unless :: (Monad m) => Bool -> m () -> m ()
216 unless p s = if p then return () else s
218 when :: (Monad m) => Bool -> m () -> m ()
219 when p s = if p then s else return ()
221 ap :: (Monad m) => m (a -> b) -> m a -> m b
224 liftM :: (Monad m) => (a1 -> r) -> m a1 -> m r
225 liftM2 :: (Monad m) => (a1 -> a2 -> r) -> m a1 -> m a2 -> m r
226 liftM3 :: (Monad m) => (a1 -> a2 -> a3 -> r) -> m a1 -> m a2 -> m a3 -> m r
227 liftM4 :: (Monad m) => (a1 -> a2 -> a3 -> a4 -> r) -> m a1 -> m a2 -> m a3 -> m a4 -> m r
228 liftM5 :: (Monad m) => (a1 -> a2 -> a3 -> a4 -> a5 -> r) -> m a1 -> m a2 -> m a3 -> m a4 -> m a5 -> m r
230 liftM f m1 = do { x1 <- m1; return (f x1) }
231 liftM2 f m1 m2 = do { x1 <- m1; x2 <- m2; return (f x1 x2) }
232 liftM3 f m1 m2 m3 = do { x1 <- m1; x2 <- m2; x3 <- m3; return (f x1 x2 x3) }
233 liftM4 f m1 m2 m3 m4 = do { x1 <- m1; x2 <- m2; x3 <- m3; x4 <- m4; return (f x1 x2 x3 x4) }
234 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) }