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

Language | Haskell2010 |

Utilities related to Monad and Applicative classes Mostly for backwards compatibility.

## Synopsis

- class Functor f => Applicative (f :: Type -> Type) where
- (<$>) :: Functor f => (a -> b) -> f a -> f b
- class Monad m => MonadFix (m :: Type -> Type) where
- mfix :: (a -> m a) -> m a

- class Monad m => MonadIO (m :: Type -> Type) where
- zipWith3M :: Monad m => (a -> b -> c -> m d) -> [a] -> [b] -> [c] -> m [d]
- zipWith3M_ :: Monad m => (a -> b -> c -> m d) -> [a] -> [b] -> [c] -> m ()
- zipWith4M :: Monad m => (a -> b -> c -> d -> m e) -> [a] -> [b] -> [c] -> [d] -> m [e]
- zipWithAndUnzipM :: Monad m => (a -> b -> m (c, d)) -> [a] -> [b] -> m ([c], [d])
- mapAndUnzipM :: Applicative m => (a -> m (b, c)) -> [a] -> m ([b], [c])
- mapAndUnzip3M :: Monad m => (a -> m (b, c, d)) -> [a] -> m ([b], [c], [d])
- mapAndUnzip4M :: Monad m => (a -> m (b, c, d, e)) -> [a] -> m ([b], [c], [d], [e])
- mapAndUnzip5M :: Monad m => (a -> m (b, c, d, e, f)) -> [a] -> m ([b], [c], [d], [e], [f])
- mapAccumLM :: Monad m => (acc -> x -> m (acc, y)) -> acc -> [x] -> m (acc, [y])
- mapSndM :: Monad m => (b -> m c) -> [(a, b)] -> m [(a, c)]
- concatMapM :: Monad m => (a -> m [b]) -> [a] -> m [b]
- mapMaybeM :: Applicative m => (a -> m (Maybe b)) -> [a] -> m [b]
- fmapMaybeM :: Monad m => (a -> m b) -> Maybe a -> m (Maybe b)
- fmapEitherM :: Monad m => (a -> m b) -> (c -> m d) -> Either a c -> m (Either b d)
- anyM :: Monad m => (a -> m Bool) -> [a] -> m Bool
- allM :: Monad m => (a -> m Bool) -> [a] -> m Bool
- orM :: Monad m => m Bool -> m Bool -> m Bool
- foldlM :: (Foldable t, Monad m) => (b -> a -> m b) -> b -> t a -> m b
- foldlM_ :: (Monad m, Foldable t) => (a -> b -> m a) -> a -> t b -> m ()
- foldrM :: (Foldable t, Monad m) => (a -> b -> m b) -> b -> t a -> m b
- maybeMapM :: Monad m => (a -> m b) -> Maybe a -> m (Maybe b)
- whenM :: Monad m => m Bool -> m () -> m ()
- unlessM :: Monad m => m Bool -> m () -> m ()
- filterOutM :: Applicative m => (a -> m Bool) -> [a] -> m [a]

# Documentation

class Functor f => Applicative (f :: Type -> Type) where #

A functor with application, providing operations to

A minimal complete definition must include implementations of `pure`

and of either `<*>`

or `liftA2`

. If it defines both, then they must behave
the same as their default definitions:

(`<*>`

) =`liftA2`

`id`

`liftA2`

f x y = f`<$>`

x`<*>`

y

Further, any definition must satisfy the following:

- Identity
`pure`

`id`

`<*>`

v = v- Composition
`pure`

(.)`<*>`

u`<*>`

v`<*>`

w = u`<*>`

(v`<*>`

w)- Homomorphism
`pure`

f`<*>`

`pure`

x =`pure`

(f x)- Interchange
u

`<*>`

`pure`

y =`pure`

(`$`

y)`<*>`

u

The other methods have the following default definitions, which may be overridden with equivalent specialized implementations:

As a consequence of these laws, the `Functor`

instance for `f`

will satisfy

It may be useful to note that supposing

forall x y. p (q x y) = f x . g y

it follows from the above that

`liftA2`

p (`liftA2`

q u v) =`liftA2`

f u .`liftA2`

g v

If `f`

is also a `Monad`

, it should satisfy

(which implies that `pure`

and `<*>`

satisfy the applicative functor laws).

Lift a value.

(<*>) :: f (a -> b) -> f a -> f b infixl 4 #

Sequential application.

A few functors support an implementation of `<*>`

that is more
efficient than the default one.

Using `ApplicativeDo`

: '`fs `

' can be understood as
the `<*>`

as`do`

expression

do f <- fs a <- as pure (f a)

liftA2 :: (a -> b -> c) -> f a -> f b -> f c #

Lift a binary function to actions.

Some functors support an implementation of `liftA2`

that is more
efficient than the default one. In particular, if `fmap`

is an
expensive operation, it is likely better to use `liftA2`

than to
`fmap`

over the structure and then use `<*>`

.

This became a typeclass method in 4.10.0.0. Prior to that, it was
a function defined in terms of `<*>`

and `fmap`

.

Using `ApplicativeDo`

: '

' can be understood
as the `liftA2`

f as bs`do`

expression

do a <- as b <- bs pure (f a b)

(*>) :: f a -> f b -> f b infixl 4 #

Sequence actions, discarding the value of the first argument.

'`as `

' can be understood as the `*>`

bs`do`

expression

do as bs

This is a tad complicated for our `ApplicativeDo`

extension
which will give it a `Monad`

constraint. For an `Applicative`

constraint we write it of the form

do _ <- as b <- bs pure b

(<*) :: f a -> f b -> f a infixl 4 #

Sequence actions, discarding the value of the second argument.

Using `ApplicativeDo`

: '`as `

' can be understood as
the `<*`

bs`do`

expression

do a <- as bs pure a

#### Instances

(<$>) :: Functor f => (a -> b) -> f a -> f b infixl 4 #

An infix synonym for `fmap`

.

The name of this operator is an allusion to `$`

.
Note the similarities between their types:

($) :: (a -> b) -> a -> b (<$>) :: Functor f => (a -> b) -> f a -> f b

Whereas `$`

is function application, `<$>`

is function
application lifted over a `Functor`

.

#### Examples

Convert from a

to a `Maybe`

`Int`

using `Maybe`

`String`

`show`

:

`>>>`

Nothing`show <$> Nothing`

`>>>`

Just "3"`show <$> Just 3`

Convert from an

to an
`Either`

`Int`

`Int`

`Either`

`Int`

`String`

using `show`

:

`>>>`

Left 17`show <$> Left 17`

`>>>`

Right "17"`show <$> Right 17`

Double each element of a list:

`>>>`

[2,4,6]`(*2) <$> [1,2,3]`

Apply `even`

to the second element of a pair:

`>>>`

(2,True)`even <$> (2,2)`

class Monad m => MonadFix (m :: Type -> Type) where #

Monads having fixed points with a 'knot-tying' semantics.
Instances of `MonadFix`

should satisfy the following laws:

- Purity
`mfix`

(`return`

. h) =`return`

(`fix`

h)- Left shrinking (or Tightening)
`mfix`

(\x -> a >>= \y -> f x y) = a >>= \y ->`mfix`

(\x -> f x y)- Sliding

, for strict`mfix`

(`liftM`

h . f) =`liftM`

h (`mfix`

(f . h))`h`

.- Nesting
`mfix`

(\x ->`mfix`

(\y -> f x y)) =`mfix`

(\x -> f x x)

This class is used in the translation of the recursive `do`

notation
supported by GHC and Hugs.

#### Instances

class Monad m => MonadIO (m :: Type -> Type) where #

Monads in which `IO`

computations may be embedded.
Any monad built by applying a sequence of monad transformers to the
`IO`

monad will be an instance of this class.

Instances should satisfy the following laws, which state that `liftIO`

is a transformer of monads:

#### Instances

zipWith3M_ :: Monad m => (a -> b -> c -> m d) -> [a] -> [b] -> [c] -> m () Source #

zipWithAndUnzipM :: Monad m => (a -> b -> m (c, d)) -> [a] -> [b] -> m ([c], [d]) Source #

mapAndUnzipM :: Applicative m => (a -> m (b, c)) -> [a] -> m ([b], [c]) #

The `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 monad.

mapAndUnzip3M :: Monad m => (a -> m (b, c, d)) -> [a] -> m ([b], [c], [d]) Source #

mapAndUnzipM for triples

mapAndUnzip4M :: Monad m => (a -> m (b, c, d, e)) -> [a] -> m ([b], [c], [d], [e]) Source #

mapAndUnzip5M :: Monad m => (a -> m (b, c, d, e, f)) -> [a] -> m ([b], [c], [d], [e], [f]) Source #

:: Monad m | |

=> (acc -> x -> m (acc, y)) | combining function |

-> acc | initial state |

-> [x] | inputs |

-> m (acc, [y]) | final state, outputs |

Monadic version of mapAccumL

concatMapM :: Monad m => (a -> m [b]) -> [a] -> m [b] Source #

Monadic version of concatMap

mapMaybeM :: Applicative m => (a -> m (Maybe b)) -> [a] -> m [b] Source #

Applicative version of mapMaybe

fmapEitherM :: Monad m => (a -> m b) -> (c -> m d) -> Either a c -> m (Either b d) Source #

Monadic version of fmap

anyM :: Monad m => (a -> m Bool) -> [a] -> m Bool Source #

Monadic version of `any`

, aborts the computation at the first `True`

value

allM :: Monad m => (a -> m Bool) -> [a] -> m Bool Source #

Monad version of `all`

, aborts the computation at the first `False`

value

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

Monadic fold over the elements of a structure, associating to the left, i.e. from left to right.

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

Monadic version of foldl that discards its result

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

Monadic fold over the elements of a structure, associating to the right, i.e. from right to left.

maybeMapM :: Monad m => (a -> m b) -> Maybe a -> m (Maybe b) Source #

Monadic version of fmap specialised for Maybe

whenM :: Monad m => m Bool -> m () -> m () Source #

Monadic version of `when`

, taking the condition in the monad

unlessM :: Monad m => m Bool -> m () -> m () Source #

Monadic version of `unless`

, taking the condition in the monad

filterOutM :: Applicative m => (a -> m Bool) -> [a] -> m [a] Source #

Like `filterM`

, only it reverses the sense of the test.