Control.Monad (original) (raw)

Functor and monad classes

class Functor f whereSource

The [Functor](Control-Monad.html#t:Functor) class is used for types that can be mapped over. Instances of [Functor](Control-Monad.html#t:Functor) should satisfy the following laws:

fmap id == id fmap (f . g) == fmap f . fmap g

The instances of [Functor](Control-Monad.html#t:Functor) for lists, [Maybe](Data-Maybe.html#t:Maybe) and [IO](System-IO.html#t:IO)satisfy these laws.

class Monad m whereSource

The [Monad](Control-Monad.html#t:Monad) class defines the basic operations over a monad, a concept from a branch of mathematics known as category theory. From the perspective of a Haskell programmer, however, it is best to think of a monad as an abstract datatype of actions. Haskell's do expressions provide a convenient syntax for writing monadic expressions.

Minimal complete definition: [>>=](Control-Monad.html#v:-62--62--61-) and [return](Control-Monad.html#v:return).

Instances of [Monad](Control-Monad.html#t:Monad) should satisfy the following laws:

return a >>= k == k a m >>= return == m m >>= (\x -> k x >>= h) == (m >>= k) >>= h

Instances of both [Monad](Control-Monad.html#t:Monad) and [Functor](Control-Monad.html#t:Functor) should additionally satisfy the law:

fmap f xs == xs >>= return . f

The instances of [Monad](Control-Monad.html#t:Monad) for lists, [Maybe](Data-Maybe.html#t:Maybe) and [IO](System-IO.html#t:IO)defined in the Prelude satisfy these laws.

Methods

(>>=) :: forall a b. m a -> (a -> m b) -> m bSource

Sequentially compose two actions, passing any value produced by the first as an argument to the second.

(>>) :: forall a b. m a -> m b -> m bSource

Sequentially compose two actions, discarding any value produced by the first, like sequencing operators (such as the semicolon) in imperative languages.

return :: a -> m aSource

Inject a value into the monadic type.

fail :: String -> m aSource

Fail with a message. This operation is not part of the mathematical definition of a monad, but is invoked on pattern-match failure in a do expression.

class Monad m => MonadPlus m whereSource

Monads that also support choice and failure.

Methods

mzero :: m aSource

the identity of [mplus](Control-Monad.html#v:mplus). It should also satisfy the equations

mzero >>= f = mzero v >> mzero = mzero

mplus :: m a -> m a -> m aSource

an associative operation

Functions

Naming conventions

The functions in this library use the following naming conventions:

Basic Monad functions

sequence :: Monad m => [m a] -> m [a]Source

Evaluate each action in the sequence from left to right, and collect the results.

sequence_ :: Monad m => [m a] -> m ()Source

Evaluate each action in the sequence from left to right, and ignore the results.

(=<<) :: Monad m => (a -> m b) -> m a -> m bSource

Same as [>>=](Control-Monad.html#v:-62--62--61-), but with the arguments interchanged.

(>=>) :: Monad m => (a -> m b) -> (b -> m c) -> a -> m cSource

Left-to-right Kleisli composition of monads.

(<=<) :: Monad m => (b -> m c) -> (a -> m b) -> a -> m cSource

Right-to-left Kleisli composition of monads. (`[>=>](Control-Monad.html#v:-62--61--62-)`), with the arguments flipped

void :: Functor f => f a -> f ()Source

`[void](Control-Monad.html#v:void)` value discards or ignores the result of evaluation, such as the return value of an [IO](System-IO.html#t:IO) action.

Generalisations of list functions

join :: Monad m => m (m a) -> m aSource

The [join](Control-Monad.html#v:join) function is the conventional monad join operator. It is used to remove one level of monadic structure, projecting its bound argument into the outer level.

mapAndUnzipM :: Monad m => (a -> m (b, c)) -> [a] -> m ([b], [c])Source

The [mapAndUnzipM](Control-Monad.html#v:mapAndUnzipM) function maps its first argument over a list, returning the result as a pair of lists. This function is mainly used with complicated data structures or a state-transforming monad.

foldM :: Monad m => (a -> b -> m a) -> a -> [b] -> m aSource

The [foldM](Control-Monad.html#v:foldM) function is analogous to [foldl](Data-List.html#v:foldl), except that its result is encapsulated in a monad. Note that [foldM](Control-Monad.html#v:foldM) works from left-to-right over the list arguments. This could be an issue where (`[>>](Control-Monad.html#v:-62--62-)`) and the `folded function' are not commutative.

   foldM f a1 [x1, x2, ..., xm]

==

   do
     a2 <- f a1 x1
     a3 <- f a2 x2
     ...
     f am xm

If right-to-left evaluation is required, the input list should be reversed.

Conditional execution of monadic expressions

when :: Monad m => Bool -> m () -> m ()Source

Conditional execution of monadic expressions. For example,

   when debug (putStr "Debugging\n")

will output the string Debugging\n if the Boolean value debug is [True](Data-Bool.html#v:True), and otherwise do nothing.

Monadic lifting operators

liftM :: Monad m => (a1 -> r) -> m a1 -> m rSource

Promote a function to a monad.

liftM2 :: Monad m => (a1 -> a2 -> r) -> m a1 -> m a2 -> m rSource

Promote a function to a monad, scanning the monadic arguments from left to right. For example,

liftM2 (+) [0,1] [0,2] = [0,2,1,3]
liftM2 (+) (Just 1) Nothing = Nothing

liftM3 :: Monad m => (a1 -> a2 -> a3 -> r) -> m a1 -> m a2 -> m a3 -> m rSource

Promote a function to a monad, scanning the monadic arguments from left to right (cf. [liftM2](Control-Monad.html#v:liftM2)).

liftM4 :: Monad m => (a1 -> a2 -> a3 -> a4 -> r) -> m a1 -> m a2 -> m a3 -> m a4 -> m rSource

Promote a function to a monad, scanning the monadic arguments from left to right (cf. [liftM2](Control-Monad.html#v:liftM2)).

liftM5 :: Monad m => (a1 -> a2 -> a3 -> a4 -> a5 -> r) -> m a1 -> m a2 -> m a3 -> m a4 -> m a5 -> m rSource

Promote a function to a monad, scanning the monadic arguments from left to right (cf. [liftM2](Control-Monad.html#v:liftM2)).

ap :: Monad m => m (a -> b) -> m a -> m bSource

In many situations, the [liftM](Control-Monad.html#v:liftM) operations can be replaced by uses of[ap](Control-Monad.html#v:ap), which promotes function application.

   return f `ap` x1 `ap` ... `ap` xn

is equivalent to

   liftMn f x1 x2 ... xn