Safe Haskell | Safe |
---|---|
Language | Haskell2010 |
- class Functor f where
- class Applicative m => Monad m where
- class (Alternative m, Monad m) => MonadPlus m where
- forever :: Applicative f => f a -> f b
- void :: Functor f => f a -> f ()
- msum :: (Foldable t, MonadPlus m) => t (m a) -> m a
- mfilter :: MonadPlus m => (a -> Bool) -> m a -> m a
- filterM :: Applicative m => (a -> m Bool) -> [a] -> m [a]
- 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 ()
- replicateM :: Applicative m => Int -> m a -> m [a]
- replicateM_ :: Applicative m => Int -> m a -> m ()
- guard :: Alternative f => Bool -> f ()
- when :: Applicative f => Bool -> f () -> f ()
- unless :: Applicative f => Bool -> f () -> f ()
- (<$!>) :: Monad m => (a -> b) -> m a -> m b
Documentation
The Functor
class is used for types that can be mapped over.
Instances of Functor
should satisfy the following laws:
fmap id == id fmap (f . g) == fmap f . fmap g
The instances of Functor
for lists, Maybe
and IO
satisfy these laws.
Functor [] | |
Functor Maybe | |
Functor IO | |
Functor V1 | |
Functor U1 | |
Functor Par1 | |
Functor Id | |
Functor Min | |
Functor Max | |
Functor First | |
Functor Last | |
Functor Option | |
Functor NonEmpty | |
Functor ZipList | |
Functor Dual | |
Functor Sum | |
Functor Product | |
Functor First | |
Functor Last | |
Functor ((->) r) | |
Functor (Either a) | |
Functor f => Functor (Rec1 f) | |
Functor (URec Char) | |
Functor (URec Double) | |
Functor (URec Float) | |
Functor (URec Int) | |
Functor (URec Word) | |
Functor (URec (Ptr ())) | |
Functor ((,) a) | |
Functor (StateL s) | |
Functor (StateR s) | |
Functor (Arg a) | |
Monad m => Functor (WrappedMonad m) | |
Functor (Proxy *) | |
Functor (K1 i c) | |
(Functor g, Functor f) => Functor ((:+:) f g) | |
(Functor g, Functor f) => Functor ((:*:) f g) | |
(Functor g, Functor f) => Functor ((:.:) f g) | |
Arrow a => Functor (WrappedArrow a b) | |
Functor (Const * m) | |
Functor f => Functor (Alt * f) | |
Functor f => Functor (M1 i c f) | |
class Applicative m => 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.
Instances of Monad
should satisfy the following laws:
Furthermore, the Monad
and Applicative
operations should relate as follows:
The above laws imply:
and that pure
and (<*>
) satisfy the applicative functor laws.
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.
Inject a value into the monadic type.
Monad [] | |
Monad Maybe | |
Monad IO | |
Monad U1 | |
Monad Par1 | |
Monad Min | |
Monad Max | |
Monad First | |
Monad Last | |
Monad Option | |
Monad NonEmpty | |
Monad Dual | |
Monad Sum | |
Monad Product | |
Monad First | |
Monad Last | |
Monad ((->) r) | |
Monad (Either e) | |
Monad f => Monad (Rec1 f) | |
Monoid a => Monad ((,) a) | |
Monad m => Monad (WrappedMonad m) | |
Monad (Proxy *) | |
(Monad f, Monad g) => Monad ((:*:) f g) | |
Monad f => Monad (Alt * f) | |
Monad f => Monad (M1 i c f) | |
class (Alternative m, Monad m) => MonadPlus m where #
Monads that also support choice and failure.
the identity of mplus
. It should also satisfy the equations
mzero >>= f = mzero v >> mzero = mzero
an associative operation
forever :: Applicative f => f a -> f b #
repeats the action infinitely.forever
act
void :: Functor f => f a -> f () #
discards or ignores the result of evaluation, such
as the return value of an void
valueIO
action.
Examples
Replace the contents of a
with unit:Maybe
Int
>>>
void Nothing
Nothing>>>
void (Just 3)
Just ()
Replace the contents of an
with unit,
resulting in an Either
Int
Int
:Either
Int
'()'
>>>
void (Left 8675309)
Left 8675309>>>
void (Right 8675309)
Right ()
Replace every element of a list with unit:
>>>
void [1,2,3]
[(),(),()]
Replace the second element of a pair with unit:
>>>
void (1,2)
(1,())
Discard the result of an IO
action:
>>>
mapM print [1,2]
1 2 [(),()]>>>
void $ mapM print [1,2]
1 2
filterM :: Applicative m => (a -> m Bool) -> [a] -> m [a] #
This generalizes the list-based filter
function.
foldM :: (Foldable t, Monad m) => (b -> a -> m b) -> b -> t a -> m b #
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 () #
Like foldM
, but discards the result.
replicateM :: Applicative m => Int -> m a -> m [a] #
performs the action replicateM
n actn
times,
gathering the results.
replicateM_ :: Applicative m => Int -> m a -> m () #
Like replicateM
, but discards the result.
when :: Applicative f => Bool -> f () -> f () #
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.
unless :: Applicative f => Bool -> f () -> f () #
The reverse of when
.