Safe Haskell | Trustworthy |
---|---|
Language | Haskell2010 |
Reexporting useful monadic stuff.
- module Monad.Maybe
- module Monad.Either
- module Monad.Trans
- class Applicative m => Monad m where
- class Monad m => MonadFail m where
- class (Alternative m, Monad m) => MonadPlus m where
- (=<<) :: Monad m => (a -> m b) -> m a -> m b
- (>=>) :: Monad m => (a -> m b) -> (b -> m c) -> a -> m c
- (<=<) :: Monad m => (b -> m c) -> (a -> m b) -> a -> m c
- forever :: Applicative f => f a -> f b
- join :: Monad m => m (m a) -> m a
- mfilter :: MonadPlus m => (a -> Bool) -> m a -> m a
- filterM :: Applicative m => (a -> m Bool) -> [a] -> m [a]
- mapAndUnzipM :: Applicative m => (a -> m (b, c)) -> [a] -> m ([b], [c])
- zipWithM :: Applicative m => (a -> b -> m c) -> [a] -> [b] -> m [c]
- zipWithM_ :: Applicative m => (a -> b -> m c) -> [a] -> [b] -> m ()
- 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 ()
- concatMapM :: (Applicative f, Monoid m, Container (l m), Traversable l) => (a -> f m) -> l a -> f m
- concatForM :: (Applicative f, Monoid m, Container (l m), Traversable l) => l a -> (a -> f m) -> f m
- allM :: (Container f, Monad m) => (Element f -> m Bool) -> f -> m Bool
- anyM :: (Container f, Monad m) => (Element f -> m Bool) -> f -> m Bool
- andM :: (Container f, Element f ~ m Bool, Monad m) => f -> m Bool
- orM :: (Container f, Element f ~ m Bool, Monad m) => f -> m Bool
- liftM :: Monad m => (a1 -> r) -> m a1 -> m r
- liftM2 :: Monad m => (a1 -> a2 -> r) -> m a1 -> m a2 -> m r
- liftM3 :: Monad m => (a1 -> a2 -> a3 -> r) -> m a1 -> m a2 -> m a3 -> m r
- liftM4 :: Monad m => (a1 -> a2 -> a3 -> a4 -> r) -> m a1 -> m a2 -> m a3 -> m a4 -> m r
- liftM5 :: Monad m => (a1 -> a2 -> a3 -> a4 -> a5 -> r) -> m a1 -> m a2 -> m a3 -> m a4 -> m a5 -> m r
- ap :: Monad m => m (a -> b) -> m a -> m b
- (<$!>) :: Monad m => (a -> b) -> m a -> m b
Documentation
module Monad.Maybe
module Monad.Either
module Monad.Trans
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.
class Monad m => MonadFail m where #
When a value is bound in do
-notation, the pattern on the left
hand side of <-
might not match. In this case, this class
provides a function to recover.
A Monad
without a MonadFail
instance may only be used in conjunction
with pattern that always match, such as newtypes, tuples, data types with
only a single data constructor, and irrefutable patterns (~pat
).
Instances of MonadFail
should satisfy the following law: fail s
should
be a left zero for >>=
,
fail s >>= f = fail s
If your Monad
is also MonadPlus
, a popular definition is
fail _ = mzero
Since: 4.9.0.0
MonadFail [] | |
MonadFail Maybe | |
MonadFail IO | |
MonadFail Q | |
MonadFail P | |
MonadFail ReadPrec | |
MonadFail ReadP | |
Monad m => MonadFail (ListT m) | |
Monad m => MonadFail (MaybeT m) | |
MonadFail m => MonadFail (StateT s m) | |
MonadFail m => MonadFail (StateT s m) | |
(Monad m, Error e) => MonadFail (ErrorT e m) | |
MonadFail m => MonadFail (ExceptT e m) | |
MonadFail m => MonadFail (StateT s m) | |
(Monoid w, MonadFail m) => MonadFail (WriterT w m) | |
(Monoid w, MonadFail m) => MonadFail (WriterT w m) | |
MonadFail m => MonadFail (IdentityT * m) | |
MonadFail m => MonadFail (ReaderT * r m) | |
(Monoid w, MonadFail m) => MonadFail (RWST r w s m) | |
(Monoid w, MonadFail m) => MonadFail (RWST r w s m) | |
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
MonadPlus [] | |
MonadPlus Maybe | |
MonadPlus IO | |
MonadPlus U1 | |
MonadPlus P | |
MonadPlus Option | |
MonadPlus STM | |
MonadPlus ReadPrec | |
MonadPlus ReadP | |
MonadPlus Seq | |
MonadPlus Array | |
MonadPlus Vector | |
MonadPlus f => MonadPlus (Rec1 f) | |
(ArrowApply a, ArrowPlus a) => MonadPlus (ArrowMonad a) | |
MonadPlus (Proxy *) | |
Monad m => MonadPlus (ListT m) | |
Monad m => MonadPlus (MaybeT m) | |
(MonadPlus f, MonadPlus g) => MonadPlus ((:*:) f g) | |
MonadPlus f => MonadPlus (Alt * f) | |
MonadPlus m => MonadPlus (StateT s m) | |
(Monad m, Error e) => MonadPlus (ErrorT e m) | |
(Monad m, Monoid e) => MonadPlus (ExceptT e m) | |
MonadPlus m => MonadPlus (StateT s m) | |
(Monoid w, MonadPlus m) => MonadPlus (WriterT w m) | |
(Monoid w, MonadPlus m) => MonadPlus (WriterT w m) | |
MonadPlus m => MonadPlus (IdentityT * m) | |
MonadPlus f => MonadPlus (M1 i c f) | |
(MonadPlus f, MonadPlus g) => MonadPlus (Product * f g) | |
MonadPlus m => MonadPlus (ReaderT * r m) | |
(Monoid w, MonadPlus m) => MonadPlus (RWST r w s m) | |
(Monoid w, MonadPlus m) => MonadPlus (RWST r w s m) | |
(=<<) :: Monad m => (a -> m b) -> m a -> m b infixr 1 #
Same as >>=
, but with the arguments interchanged.
(>=>) :: Monad m => (a -> m b) -> (b -> m c) -> a -> m c infixr 1 #
Left-to-right Kleisli composition of monads.
forever :: Applicative f => f a -> f b #
repeats the action infinitely.forever
act
join :: Monad m => m (m a) -> m a #
The 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.
filterM :: Applicative m => (a -> m Bool) -> [a] -> m [a] #
This generalizes the list-based filter
function.
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-transforming monad.
zipWithM :: Applicative m => (a -> b -> m c) -> [a] -> [b] -> m [c] #
zipWithM_ :: Applicative m => (a -> b -> m c) -> [a] -> [b] -> m () #
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.
concatMapM :: (Applicative f, Monoid m, Container (l m), Traversable l) => (a -> f m) -> l a -> f m Source #
Lifting bind into a monad. Generalized version of concatMap
that works with a monadic predicate. Old and simpler specialized to list
version had next type:
concatMapM :: Monad m => (a -> m [b]) -> [a] -> m [b]
Side note: previously it had type
concatMapM :: (Applicative q, Monad m, Traversable m) => (a -> q (m b)) -> m a -> q (m b)
Such signature didn't allow to use this function when traversed container type and type of returned by function-argument differed. Now you can use it like e.g.
concatMapM readFile files >>= putStrLn
concatForM :: (Applicative f, Monoid m, Container (l m), Traversable l) => l a -> (a -> f m) -> f m Source #
Like concatMapM
, but has its arguments flipped, so can be used
instead of the common fmap concat $ forM
pattern.
andM :: (Container f, Element f ~ m Bool, Monad m) => f -> m Bool Source #
Monadic and constrained to Container
version of and
.
>>>
andM [Just True, Just False]
Just False>>>
andM [Just True]
Just True>>>
andM [Just True, Just False, Nothing]
Just False>>>
andM [Just True, Nothing]
Nothing>>>
andM [putStrLn "1" >> pure True, putStrLn "2" >> pure False, putStrLn "3" >> undefined]
1 2 False
liftM2 :: Monad m => (a1 -> a2 -> r) -> m a1 -> m a2 -> m r #
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 r #
Promote a function to a monad, scanning the monadic arguments from
left to right (cf. liftM2
).
liftM4 :: Monad m => (a1 -> a2 -> a3 -> a4 -> r) -> m a1 -> m a2 -> m a3 -> m a4 -> m r #
Promote a function to a monad, scanning the monadic arguments from
left to right (cf. liftM2
).
liftM5 :: Monad m => (a1 -> a2 -> a3 -> a4 -> a5 -> r) -> m a1 -> m a2 -> m a3 -> m a4 -> m a5 -> m r #
Promote a function to a monad, scanning the monadic arguments from
left to right (cf. liftM2
).