7af5a647215713d9043237ccab0a25953f584920
[packages/haskell2010.git] / Control / Monad.hs
1 {-# LANGUAGE CPP, PackageImports #-}
2 #if __GLASGOW_HASKELL__ >= 701
3 {-# LANGUAGE Safe #-}
4 #endif
5
6 -- |
7 -- The "Control.Monad" module provides the 'Functor', 'Monad' and
8 -- 'MonadPlus' classes, together with some useful operations on monads.
9
10 module Control.Monad (
11 -- * Functor and monad classes
12
13 Functor(fmap)
14 , Monad((>>=), (>>), return, fail)
15
16 , MonadPlus ( -- class context: Monad
17 mzero -- :: (MonadPlus m) => m a
18 , mplus -- :: (MonadPlus m) => m a -> m a -> m a
19 )
20 -- * Functions
21
22 -- ** Naming conventions
23 -- $naming
24
25 -- ** Basic @Monad@ functions
26
27 , mapM -- :: (Monad m) => (a -> m b) -> [a] -> m [b]
28 , mapM_ -- :: (Monad m) => (a -> m b) -> [a] -> m ()
29 , forM -- :: (Monad m) => [a] -> (a -> m b) -> m [b]
30 , forM_ -- :: (Monad m) => [a] -> (a -> m b) -> m ()
31 , sequence -- :: (Monad m) => [m a] -> m [a]
32 , sequence_ -- :: (Monad m) => [m a] -> m ()
33 , (=<<) -- :: (Monad m) => (a -> m b) -> m a -> m b
34 , (>=>) -- :: (Monad m) => (a -> m b) -> (b -> m c) -> (a -> m c)
35 , (<=<) -- :: (Monad m) => (b -> m c) -> (a -> m b) -> (a -> m c)
36 , forever -- :: (Monad m) => m a -> m b
37 , void
38
39 -- ** Generalisations of list functions
40
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 ()
51
52 -- ** Conditional execution of monadic expressions
53
54 , guard -- :: (MonadPlus m) => Bool -> m ()
55 , when -- :: (Monad m) => Bool -> m () -> m ()
56 , unless -- :: (Monad m) => Bool -> m () -> m ()
57
58 -- ** Monadic lifting operators
59
60 , liftM -- :: (Monad m) => (a -> b) -> (m a -> m b)
61 , liftM2 -- :: (Monad m) => (a -> b -> c) -> (m a -> m b -> m c)
62 , liftM3 -- :: ...
63 , liftM4 -- :: ...
64 , liftM5 -- :: ...
65
66 , ap -- :: (Monad m) => m (a -> b) -> m a -> m b
67
68 ) where
69 import "base" Control.Monad
70
71 {- $naming
72
73 The functions in this library use the following naming conventions:
74
75 * A postfix \'@M@\' always stands for a function in the Kleisli category:
76 The monad type constructor @m@ is added to function results
77 (modulo currying) and nowhere else. So, for example,
78
79 > filter :: (a -> Bool) -> [a] -> [a]
80 > filterM :: (Monad m) => (a -> m Bool) -> [a] -> m [a]
81
82 * A postfix \'@_@\' changes the result type from @(m a)@ to @(m ())@.
83 Thus, for example:
84
85 > sequence :: Monad m => [m a] -> m [a]
86 > sequence_ :: Monad m => [m a] -> m ()
87
88 * A prefix \'@m@\' generalizes an existing function to a monadic form.
89 Thus, for example:
90
91 > sum :: Num a => [a] -> a
92 > msum :: MonadPlus m => [m a] -> m a
93
94 -}