Same as >>=, but with the arguments interchanged.
as >>= f == f =<< as:: a typeCtrl KGHC 9.14.1 · lts/ghc-9.14.x · 3e2d568 · 2026-10-11
Modulebase-compat-batteries-0.15.0Haskell2010
Same as >>=, but with the arguments interchanged.
as >>= f == f =<< asThe 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.
'join bss' can be understood as the do expression
do bs <- bss
bs
join [[1, 2, 3], [4, 5, 6], [7, 8, 9]][1,2,3,4,5,6,7,8,9]
join (Just (Just 3))Just 3
A common use of join is to run an IO computation returned from
an GHC.Conc.STM transaction, since GHC.Conc.STM transactions
can't perform IO directly. Recall that
GHC.Internal.Conc.atomically :: STM a -> IO a
is used to run GHC.Conc.STM transactions atomically. So, by
specializing the types of GHC.Internal.Conc.atomically and join to
GHC.Internal.Conc.atomically :: STM (IO b) -> IO (IO b)
join :: IO (IO b) -> IO b
we can compose them as
join . GHC.Internal.Conc.atomically :: STM (IO b) -> IO b
to run an GHC.Conc.STM transaction and the IO action it
returns.
Promote a function to a monad. This is equivalent to fmap but specialised to Monads.
Promote a function to a monad, scanning the monadic arguments from left to right.
liftM2 (+) [0,1] [0,2][0,2,1,3]
liftM2 (+) (Just 1) NothingNothing
liftM2 (+) (+ 3) (* 2) 518
Promote a function to a monad, scanning the monadic arguments from left to right (cf. liftM2).
Promote a function to a monad, scanning the monadic arguments from left to right (cf. liftM2).
Promote a function to a monad, scanning the monadic arguments from left to right (cf. liftM2).
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.
putStr "pi:" >> when False (print 3.14159)pi:
Strict version of <$>.
This generalizes the list-based Data.List.filter function.
runIdentity (filterM (Identity . p) xs) == filter p xsfilterM (\x -> do putStrLn ("Keep: " ++ show x ++ "?") answer <- getLine pure (answer == "y")) [1, 2, 3]Keep: 1?yKeep: 2?nKeep: 3?y[1,3]
filterM (\x -> do putStr (show x) x' <- readLn pure (x == x')) [1, 2, 3]122233[2,3]
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 xmIf right-to-left evaluation is required, the input list should be reversed.
Like foldM, but discards the result.
Repeat an action indefinitely.
A common use of forever is to process input from network sockets,
System.IO.Handles, and channels
(e.g. MVar and
Chan).
For example, here is how we might implement an echo server, using forever both to listen for client connections on a network socket and to echo client input on client connection handles:
echoServer :: Socket -> IO ()
echoServer socket = forever $ do
client <- accept socket
forkFinally (echo client) (\_ -> hClose client)
where
echo :: Handle -> IO ()
echo client = forever $
hGetLine client >>= hPutStrLn client
Note that "forever" isn't necessarily non-terminating.
If the action is in a MonadPlus and short-circuits after some number of iterations.
then forever actually returns mzero, effectively short-circuiting its caller.
Conditional failure of Alternative computations. Defined by
guard True = pure ()
guard False = empty
Common uses of guard include conditionally signalling an error in an error monad and conditionally rejecting the current choice in an Alternative-based parser.
As an example of signalling an error in the error monad Maybe,
consider a safe division function safeDiv x y that returns
Nothing when the denominator y is zero and Just (x `div`
y) otherwise. For example:
safeDiv 4 0Nothing
safeDiv 4 2Just 2
A definition of safeDiv using guards, but not guard:
safeDiv :: Int -> Int -> Maybe Int
safeDiv x y | y /= 0 = Just (x `div` y)
| otherwise = Nothing
A definition of safeDiv using guard and Monad do-notation:
safeDiv :: Int -> Int -> Maybe Int
safeDiv x y = do
guard (y /= 0)
return (x `div` y)
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.
Direct MonadPlus equivalent of Data.List.filter.
The Data.List.filter function is just mfilter specialized to
the list monad:
Data.List.filter = ( mfilter :: (a -> Bool) -> [a] -> [a] )
An example using mfilter with the Maybe monad:
mfilter odd (Just 1)Just 1mfilter odd (Just 2)Nothing
replicateM n act performs the action act n times,
and then returns the list of results.
replicateM n (pure x) == replicate n xreplicateM 3 getLinehiheyahiya["hi","heya","hiya"]
import Control.Monad.StaterunState (replicateM 3 $ state $ \s -> (s, s + 1)) 1([1,2,3],4)
The reverse of when.
do x <- getLine unless (x == "hi") (putStrLn "hi!")comingupwithexamplesisdifficulthi!
unless (pi > exp 1) NothingJust ()
Evaluate each monadic action in the structure from left to right,
and ignore the results. For a version that doesn't ignore the
results see Data.Traversable.sequence.
sequence_ is just like sequenceA_, but specialised to monadic actions.
void value discards or ignores the result of evaluation, such
as the return value of an System.IO.IO action.
Replace the contents of a Maybe Int with unit:
void NothingNothing
void (Just 3)Just ()
Replace the contents of an Either Int Int
with unit, resulting in an 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 System.IO.IO action:
mapM print [1,2]12[(),()]
void $ mapM print [1,2]12
A type f is a Functor if it provides a function fmap which, given any types a and b
lets you apply any function from (a -> b) to turn an f a into an f b, preserving the
structure of f. Furthermore f needs to adhere to the following:
Note, that the second law follows from the free theorem of the type fmap and the first law, so you need only check that the former condition holds. See these articles by School of Haskell or David Luposchainsky for an explanation.
fmap :: (a -> b) -> f a -> f bfmap is used to apply a function of type (a -> b) to a value of type f a,
where f is a functor, to produce a value of type f b.
Note that for any type constructor with more than one parameter (e.g., Either),
only the last type parameter can be modified with fmap (e.g., b in Either a b).
Some type constructors with two parameters or more have a instance that allows
both the last and the penultimate parameters to be mapped over.Data.Bifunctor
Convert from a Maybe Int to a Maybe String
using show:
fmap show NothingNothingfmap show (Just 3)Just "3"
Convert from an Either Int Int to an
Either Int String using show:
fmap show (Left 17)Left 17fmap show (Right 17)Right "17"
Double each element of a list:
fmap (*2) [1,2,3][2,4,6]
Apply even to the second element of a pair:
fmap even (2,2)(2,True)
It may seem surprising that the function is only applied to the last element of the tuple
compared to the list example above which applies it to every element in the list.
To understand, remember that tuples are type constructors with multiple type parameters:
a tuple of 3 elements (a,b,c) can also be written (,,) a b c and its Functor instance
is defined for Functor ((,,) a b) (i.e., only the third parameter is free to be mapped over
with fmap).
It explains why fmap can be used with tuples containing values of different types as in the
following example:
fmap even ("hello", 1.0, 4)("hello",1.0,True)
(<$) :: a -> f b -> f ainfixl 4Replace all locations in the input with the same value.
The default definition is fmap . const, but this may be
overridden with a more efficient version.
Perform a computation with Maybe and replace the result with a constant value if it is Just:
'a' <$ Just 2Just 'a''a' <$ NothingNothing
Functor ComplexDefined in base-4.22.0.0 · Data.ComplexFunctor FirstDefined in base-4.22.0.0 · Data.SemigroupFunctor LastDefined in base-4.22.0.0 · Data.SemigroupFunctor MaxDefined in base-4.22.0.0 · Data.SemigroupFunctor MinDefined in base-4.22.0.0 · Data.SemigroupFunctor ArgDescrDefined in base-4.22.0.0 · System.Console.GetOptFunctor ArgOrderDefined in base-4.22.0.0 · System.Console.GetOptFunctor OptDescrDefined in base-4.22.0.0 · System.Console.GetOptFunctor NonEmptyDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.NonEmpty · orphanFunctor STMDefined in ghc-internal-9.1401.0 · GHC.Internal.Conc.SyncFunctor HandlerDefined in ghc-internal-9.1401.0 · GHC.Internal.Control.ExceptionFunctor IdentityDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.Functor.IdentityFunctor FirstDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.MonoidFunctor LastDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.MonoidFunctor DownDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.OrdFunctor DualDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.Semigroup.InternalFunctor ProductDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.Semigroup.InternalFunctor SumDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.Semigroup.InternalFunctor ZipListDefined in ghc-internal-9.1401.0 · GHC.Internal.Functor.ZipListFunctor NoIODefined in ghc-internal-9.1401.0 · GHC.Internal.GHCiFunctor Par1Defined in ghc-internal-9.1401.0 · GHC.Internal.GenericsFunctor GenClosureDefined in ghc-internal-9.1401.0 · GHC.Internal.Heap.ClosuresFunctor GenStackFieldDefined in ghc-internal-9.1401.0 · GHC.Internal.Heap.ClosuresFunctor GenStackFrameDefined in ghc-internal-9.1401.0 · GHC.Internal.Heap.ClosuresFunctor GenStgStackClosureDefined in ghc-internal-9.1401.0 · GHC.Internal.Heap.ClosuresFunctor MaybeDefined in ghc-internal-9.1401.0 · GHC.Internal.BaseFunctor QDefined in ghc-internal-9.1401.0 · GHC.Internal.TH.SyntaxFunctor TyVarBndrDefined in ghc-internal-9.1401.0 · GHC.Internal.TH.SyntaxFunctor PDefined in ghc-internal-9.1401.0 · GHC.Internal.Text.ParserCombinators.ReadPFunctor ReadPDefined in ghc-internal-9.1401.0 · GHC.Internal.Text.ParserCombinators.ReadPFunctor ReadPrecDefined in ghc-internal-9.1401.0 · GHC.Internal.Text.ParserCombinators.ReadPrecFunctor SoloDefined in ghc-internal-9.1401.0 · GHC.Internal.BaseFunctor IODefined in ghc-internal-9.1401.0 · GHC.Internal.BaseFunctor []Defined in ghc-internal-9.1401.0 · GHC.Internal.BaseFunctor ProxyDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.ProxyFunctor U1Defined in ghc-internal-9.1401.0 · GHC.Internal.GenericsFunctor V1Defined in ghc-internal-9.1401.0 · GHC.Internal.GenericsFunctor (Arg a)Defined in base-4.22.0.0 · Data.SemigroupFunctor (Array i)Defined in ghc-internal-9.1401.0 · GHC.Internal.ArrFunctor (ST s)Defined in ghc-internal-9.1401.0 · GHC.Internal.Control.Monad.ST.Lazy.ImpFunctor (Either a)Defined in ghc-internal-9.1401.0 · GHC.Internal.Data.EitherFunctor (StateL s)Defined in ghc-internal-9.1401.0 · GHC.Internal.Data.Functor.UtilsFunctor (StateR s)Defined in ghc-internal-9.1401.0 · GHC.Internal.Data.Functor.UtilsFunctor (ST s)Defined in ghc-internal-9.1401.0 · GHC.Internal.STFunctor (Tuple2 a)Defined in ghc-internal-9.1401.0 · GHC.Internal.BaseMonad m => Functor (WrappedMonad m)Defined in base-4.22.0.0 · Control.ApplicativeArrow a => Functor (ArrowMonad a)Defined in ghc-internal-9.1401.0 · GHC.Internal.Control.ArrowFunctor (Const m)Defined in ghc-internal-9.1401.0 · GHC.Internal.Data.Functor.ConstFunctor (URec Char)Defined in ghc-internal-9.1401.0 · GHC.Internal.GenericsFunctor (URec Double)Defined in ghc-internal-9.1401.0 · GHC.Internal.GenericsFunctor (URec Float)Defined in ghc-internal-9.1401.0 · GHC.Internal.GenericsFunctor (URec Int)Defined in ghc-internal-9.1401.0 · GHC.Internal.GenericsFunctor (URec Word)Defined in ghc-internal-9.1401.0 · GHC.Internal.GenericsFunctor (URec (Ptr ()))Defined in ghc-internal-9.1401.0 · GHC.Internal.GenericsFunctor (Tuple3 a b)Defined in ghc-internal-9.1401.0 · GHC.Internal.BaseFunctor f => Functor (Ap f)Defined in ghc-internal-9.1401.0 · GHC.Internal.Data.MonoidFunctor f => Functor (Alt f)Defined in ghc-internal-9.1401.0 · GHC.Internal.Data.Semigroup.InternalFunctor f => Functor (Rec1 f)Defined in ghc-internal-9.1401.0 · GHC.Internal.GenericsFunctor m => Functor (Kleisli m a)Defined in ghc-internal-9.1401.0 · GHC.Internal.Control.ArrowMonad m => Functor (StateT s m)Defined in ghc-internal-9.1401.0 · GHC.Internal.Data.Functor.UtilsArrow a => Functor (WrappedArrow a b)Defined in base-4.22.0.0 · Control.Applicative(Generic1 f, Functor (Rep1 f)) => Functor (Generically1 f)Defined in ghc-internal-9.1401.0 · GHC.Internal.GenericsFunctor (K1 i c)Defined in ghc-internal-9.1401.0 · GHC.Internal.GenericsFunctor (Tuple4 a b c)Defined in ghc-internal-9.1401.0 · GHC.Internal.BaseFunctor ((->) r)Defined in ghc-internal-9.1401.0 · GHC.Internal.Base(Functor f, Functor g) => Functor (Product f g)Defined in base-4.22.0.0 · Data.Functor.Product(Functor f, Functor g) => Functor (Sum f g)Defined in base-4.22.0.0 · Data.Functor.Sum(Functor f, Functor g) => Functor (f :*: g)Defined in ghc-internal-9.1401.0 · GHC.Internal.Generics(Functor f, Functor g) => Functor (f :+: g)Defined in ghc-internal-9.1401.0 · GHC.Internal.GenericsFunctor (Tuple5 a b c d)Defined in ghc-internal-9.1401.0 · GHC.Internal.BaseFunctor f => Functor (M1 i c f)Defined in ghc-internal-9.1401.0 · GHC.Internal.Generics(Functor f, Functor g) => Functor (Compose f g)Defined in base-4.22.0.0 · Data.Functor.Compose(Functor f, Functor g) => Functor (f :.: g)Defined in ghc-internal-9.1401.0 · GHC.Internal.GenericsFunctor (Tuple6 a b c d e)Defined in ghc-internal-9.1401.0 · GHC.Internal.BaseFunctor (Tuple7 a b c d e f)Defined in ghc-internal-9.1401.0 · GHC.Internal.BaseThe 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:
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 GHC.List.List, Maybe and System.IO.IO
defined in the Prelude satisfy these laws.
(>>=) :: m a -> (a -> m b) -> m binfixl 1Sequentially compose two actions, passing any value produced by the first as an argument to the second.
'as >>= bs' can be understood as the do expression
do a <- as
bs a
An alternative name for this function is 'bind', but some people may refer to it as 'flatMap', which results from it being equivalent to
\x f -> join (fmap f x) :: Monad m => m a -> (a -> m b) -> m bwhich can be seen as mapping a value with
Monad m => m a -> m (m b) and then 'flattening' m (m b) to m b using join.
(>>) :: m a -> m b -> m binfixl 1Sequentially compose two actions, discarding any value produced by the first, like sequencing operators (such as the semicolon) in imperative languages.
'as >> bs' can be understood as the do expression
do as
bs
or in terms of as(>>=)
as >>= const bsreturn :: a -> m aInject a value into the monadic type. This function should not be different from its default implementation as pure. The justification for the existence of this function is merely historic.
Monad ComplexDefined in base-4.22.0.0 · Data.ComplexMonad FirstDefined in base-4.22.0.0 · Data.SemigroupMonad LastDefined in base-4.22.0.0 · Data.SemigroupMonad MaxDefined in base-4.22.0.0 · Data.SemigroupMonad MinDefined in base-4.22.0.0 · Data.SemigroupMonad NonEmptyDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.NonEmpty · orphanMonad STMDefined in ghc-internal-9.1401.0 · GHC.Internal.Conc.SyncMonad IdentityDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.Functor.IdentityMonad FirstDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.MonoidMonad LastDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.MonoidMonad DownDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.OrdMonad DualDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.Semigroup.InternalMonad ProductDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.Semigroup.InternalMonad SumDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.Semigroup.InternalMonad NoIODefined in ghc-internal-9.1401.0 · GHC.Internal.GHCiMonad Par1Defined in ghc-internal-9.1401.0 · GHC.Internal.GenericsMonad MaybeDefined in ghc-internal-9.1401.0 · GHC.Internal.BaseMonad QDefined in ghc-internal-9.1401.0 · GHC.Internal.TH.SyntaxMonad PDefined in ghc-internal-9.1401.0 · GHC.Internal.Text.ParserCombinators.ReadPMonad ReadPDefined in ghc-internal-9.1401.0 · GHC.Internal.Text.ParserCombinators.ReadPMonad ReadPrecDefined in ghc-internal-9.1401.0 · GHC.Internal.Text.ParserCombinators.ReadPrecMonad SoloDefined in ghc-internal-9.1401.0 · GHC.Internal.BaseMonad IODefined in ghc-internal-9.1401.0 · GHC.Internal.BaseMonad []Defined in ghc-internal-9.1401.0 · GHC.Internal.BaseMonad ProxyDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.ProxyMonad U1Defined in ghc-internal-9.1401.0 · GHC.Internal.GenericsMonad (ST s)Defined in ghc-internal-9.1401.0 · GHC.Internal.Control.Monad.ST.Lazy.ImpMonad (Either e)Defined in ghc-internal-9.1401.0 · GHC.Internal.Data.EitherMonad (ST s)Defined in ghc-internal-9.1401.0 · GHC.Internal.STMonad m => Monad (WrappedMonad m)Defined in base-4.22.0.0 · Control.ApplicativeMonoid a => Monad (Tuple2 a)Defined in ghc-internal-9.1401.0 · GHC.Internal.BaseArrowApply a => Monad (ArrowMonad a)Defined in ghc-internal-9.1401.0 · GHC.Internal.Control.ArrowMonad f => Monad (Ap f)Defined in ghc-internal-9.1401.0 · GHC.Internal.Data.MonoidMonad f => Monad (Alt f)Defined in ghc-internal-9.1401.0 · GHC.Internal.Data.Semigroup.InternalMonad f => Monad (Rec1 f)Defined in ghc-internal-9.1401.0 · GHC.Internal.GenericsMonad m => Monad (Kleisli m a)Defined in ghc-internal-9.1401.0 · GHC.Internal.Control.ArrowMonad m => Monad (StateT s m)Defined in ghc-internal-9.1401.0 · GHC.Internal.Data.Functor.Utils(Monoid a, Monoid b) => Monad (Tuple3 a b)Defined in ghc-internal-9.1401.0 · GHC.Internal.BaseMonad ((->) r)Defined in ghc-internal-9.1401.0 · GHC.Internal.Base(Monad f, Monad g) => Monad (Product f g)Defined in base-4.22.0.0 · Data.Functor.Product(Monad f, Monad g) => Monad (f :*: g)Defined in ghc-internal-9.1401.0 · GHC.Internal.Generics(Monoid a, Monoid b, Monoid c) => Monad (Tuple4 a b c)Defined in ghc-internal-9.1401.0 · GHC.Internal.BaseMonad f => Monad (M1 i c f)Defined in ghc-internal-9.1401.0 · GHC.Internal.GenericsMonads that also support choice and failure.
MonadPlus STMDefined in ghc-internal-9.1401.0 · GHC.Internal.Conc.SyncMonadPlus MaybeDefined in ghc-internal-9.1401.0 · GHC.Internal.BaseMonadPlus PDefined in ghc-internal-9.1401.0 · GHC.Internal.Text.ParserCombinators.ReadPMonadPlus ReadPDefined in ghc-internal-9.1401.0 · GHC.Internal.Text.ParserCombinators.ReadPMonadPlus ReadPrecDefined in ghc-internal-9.1401.0 · GHC.Internal.Text.ParserCombinators.ReadPrecMonadPlus IODefined in ghc-internal-9.1401.0 · GHC.Internal.BaseMonadPlus []Defined in ghc-internal-9.1401.0 · GHC.Internal.BaseCombines lists by concatenation, starting from the empty list.
MonadPlus ProxyDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.ProxyMonadPlus U1Defined in ghc-internal-9.1401.0 · GHC.Internal.Generics(ArrowApply a, ArrowPlus a) => MonadPlus (ArrowMonad a)Defined in ghc-internal-9.1401.0 · GHC.Internal.Control.ArrowMonadPlus f => MonadPlus (Ap f)Defined in ghc-internal-9.1401.0 · GHC.Internal.Data.MonoidMonadPlus f => MonadPlus (Alt f)Defined in ghc-internal-9.1401.0 · GHC.Internal.Data.Semigroup.InternalMonadPlus f => MonadPlus (Rec1 f)Defined in ghc-internal-9.1401.0 · GHC.Internal.GenericsMonadPlus m => MonadPlus (Kleisli m a)Defined in ghc-internal-9.1401.0 · GHC.Internal.Control.Arrow(MonadPlus f, MonadPlus g) => MonadPlus (Product f g)Defined in base-4.22.0.0 · Data.Functor.Product(MonadPlus f, MonadPlus g) => MonadPlus (f :*: g)Defined in ghc-internal-9.1401.0 · GHC.Internal.GenericsMonadPlus f => MonadPlus (M1 i c f)Defined in ghc-internal-9.1401.0 · GHC.Internal.GenericsWhen 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
fail s should be an action that runs in the monad itself, not an
exception (except in instances of MonadIO). In particular,
fail should not be implemented in terms of error.
MonadFail MaybeDefined in ghc-internal-9.1401.0 · GHC.Internal.Control.Monad.FailMonadFail QDefined in ghc-internal-9.1401.0 · GHC.Internal.TH.SyntaxMonadFail PDefined in ghc-internal-9.1401.0 · GHC.Internal.Text.ParserCombinators.ReadPMonadFail ReadPDefined in ghc-internal-9.1401.0 · GHC.Internal.Text.ParserCombinators.ReadPMonadFail ReadPrecDefined in ghc-internal-9.1401.0 · GHC.Internal.Text.ParserCombinators.ReadPrecMonadFail IODefined in ghc-internal-9.1401.0 · GHC.Internal.Control.Monad.FailMonadFail []Defined in ghc-internal-9.1401.0 · GHC.Internal.Control.Monad.FailMonadFail f => MonadFail (Ap f)Defined in ghc-internal-9.1401.0 · GHC.Internal.Data.MonoidMap each element of a structure to a monadic action, evaluate
these actions from left to right, and collect the results. For
a version that ignores the results see Data.Foldable.mapM_.
mapM is literally a traverse with a type signature restricted to Monad. Its implementation may be more efficient due to additional power of Monad.
Evaluate each monadic action in the structure from left to
right, and collect the results. For a version that ignores the
results see Data.Foldable.sequence_.
Basic usage:
The first two examples are instances where the input and and output of sequence are isomorphic.
sequence $ Right [1,2,3,4][Right 1,Right 2,Right 3,Right 4]
sequence $ [Right 1,Right 2,Right 3,Right 4]Right [1,2,3,4]
The following examples demonstrate short circuit behavior for sequence.
sequence $ Left [1,2,3,4]Left [1,2,3,4]
sequence $ [Left 0, Right 1,Right 2,Right 3,Right 4]Left 0