Safe Haskell | Safe-Inferred |
---|---|

Language | Haskell98 |

- class Monad m where
- class Monad m => MonadPlus m where
- foldM :: (Foldable t, Monad m) => (b -> a -> m b) -> b -> t a -> m b
- foldM_ :: (Foldable t, Monad m) => (b -> a -> m b) -> b -> t a -> m ()
- forM :: (Traversable t, Monad m) => t a -> (a -> m b) -> m (t b)
- forM_ :: (Foldable t, Monad m) => t a -> (a -> m b) -> m ()
- guard :: Alternative f => Bool -> f ()
- mapM :: Traversable t => forall a m b. Monad m => (a -> m b) -> t a -> m (t b)
- mapM_ :: (Foldable t, Monad m) => (a -> m b) -> t a -> m ()
- msum :: (Foldable t, MonadPlus m) => t (m a) -> m a
- sequence :: Traversable t => forall m a. Monad m => t (m a) -> m (t a)
- sequence_ :: (Foldable t, Monad m) => t (m a) -> m ()
- unless :: Applicative f => Bool -> f () -> f ()
- when :: Applicative f => Bool -> f () -> f ()
- (<$!>) :: Monad m => (a -> b) -> m a -> m b

# Documentation

class Monad m where

The `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: `>>=`

and `return`

.

Instances of `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`

and `Functor`

should additionally satisfy the law:

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

The instances of `Monad`

for lists, `Maybe`

and `IO`

defined in the Prelude satisfy these laws.

(>>=) :: m a -> (a -> m b) -> m b infixl 1

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

(>>) :: m a -> m b -> m b infixl 1

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

return :: a -> m a

Inject a value into the monadic type.

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 where

Monads that also support choice and failure.

foldM :: (Foldable t, Monad m) => (b -> a -> m b) -> b -> t a -> m b Source

The `foldM`

function is analogous to `foldl`

, except that its result is
encapsulated in a monad. Note that `foldM`

works from left-to-right over
the list arguments. This could be an issue where `(`

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.

foldM_ :: (Foldable t, Monad m) => (b -> a -> m b) -> b -> t a -> m () Source

Like `foldM`

, but discards the result.

forM :: (Traversable t, Monad m) => t a -> (a -> m b) -> m (t b)

guard :: Alternative f => Bool -> f () Source

mapM :: Traversable t => forall a m b. Monad m => (a -> m b) -> t a -> m (t b)

Map each element of a structure to a monadic action, evaluate these actions from left to right, and collect the results.

mapM_ :: (Foldable t, Monad m) => (a -> m b) -> t a -> m ()

Map each element of a structure to a monadic action, evaluate these actions from left to right, and ignore the results.

msum :: (Foldable t, MonadPlus m) => t (m a) -> m a

The sum of a collection of actions, generalizing `concat`

.

sequence :: Traversable t => forall m a. Monad m => t (m a) -> m (t a)

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

sequence_ :: (Foldable t, Monad m) => t (m a) -> m ()

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

unless :: Applicative f => Bool -> f () -> f () Source

The reverse of `when`

.

when :: Applicative f => Bool -> f () -> f () Source

Conditional execution of `Applicative`

expressions. For example,

when debug (putStrLn "Debugging")

will output the string `Debugging`

if the Boolean value `debug`

is `True`

, and otherwise do nothing.