protolude-0.3.0: A small prelude.
Safe Haskell Trustworthy
Language Haskell2010

Protolude.Monad

Synopsis

Documentation

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

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:

Left identity
return a >>= k = k a
Right identity
m >>= return = m
Associativity
m >>= (\x -> k x >>= h) = (m >>= k) >>= h

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.

Minimal complete definition

(>>=)

Methods

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

Sequentially 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

return :: a -> m a Source #

Inject a value into the monadic type.

Instances

Instances details
Monad []

Since: base-2.1

Instance details

Defined in GHC.Base

Methods

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

(>>) :: [a] -> [b] -> [b] Source #

return :: a -> [a] Source #

Monad Maybe

Since: base-2.1

Instance details

Defined in GHC.Base

Monad IO

Since: base-2.1

Instance details

Defined in GHC.Base

Monad Par1

Since: base-4.9.0.0

Instance details

Defined in GHC.Generics

Monad Complex

Since: base-4.9.0.0

Instance details

Defined in Data.Complex

Monad Min

Since: base-4.9.0.0

Instance details

Defined in Data.Semigroup

Monad Max

Since: base-4.9.0.0

Instance details

Defined in Data.Semigroup

Monad First

Since: base-4.9.0.0

Instance details

Defined in Data.Semigroup

Monad Last

Since: base-4.9.0.0

Instance details

Defined in Data.Semigroup

Monad Option

Since: base-4.9.0.0

Instance details

Defined in Data.Semigroup

Monad Identity

Since: base-4.8.0.0

Instance details

Defined in Data.Functor.Identity

Monad STM

Since: base-4.3.0.0

Instance details

Defined in GHC.Conc.Sync

Monad First

Since: base-4.8.0.0

Instance details

Defined in Data.Monoid

Monad Last

Since: base-4.8.0.0

Instance details

Defined in Data.Monoid

Monad Dual

Since: base-4.8.0.0

Instance details

Defined in Data.Semigroup.Internal

Monad Sum

Since: base-4.8.0.0

Instance details

Defined in Data.Semigroup.Internal

Monad Product

Since: base-4.8.0.0

Instance details

Defined in Data.Semigroup.Internal

Monad Down

Since: base-4.11.0.0

Instance details

Defined in Data.Ord

Monad ReadP

Since: base-2.1

Instance details

Defined in Text.ParserCombinators.ReadP

Monad NonEmpty

Since: base-4.9.0.0

Instance details

Defined in GHC.Base

Monad Seq
Instance details

Defined in Data.Sequence.Internal

Monad P

Since: base-2.1

Instance details

Defined in Text.ParserCombinators.ReadP

Methods

(>>=) :: P a -> (a -> P b) -> P b Source #

(>>) :: P a -> P b -> P b Source #

return :: a -> P a Source #

Monad ( Either e)

Since: base-4.4.0.0

Instance details

Defined in Data.Either

Monad ( U1 :: Type -> Type )

Since: base-4.9.0.0

Instance details

Defined in GHC.Generics

Monoid a => Monad ( (,) a)

Since: base-4.9.0.0

Instance details

Defined in GHC.Base

Methods

(>>=) :: (a, a0) -> (a0 -> (a, b)) -> (a, b) Source #

(>>) :: (a, a0) -> (a, b) -> (a, b) Source #

return :: a0 -> (a, a0) Source #

Monad ( ST s)

Since: base-2.1

Instance details

Defined in GHC.ST

Monad m => Monad ( WrappedMonad m)

Since: base-4.7.0.0

Instance details

Defined in Control.Applicative

ArrowApply a => Monad ( ArrowMonad a)

Since: base-2.1

Instance details

Defined in Control.Arrow

Monad ( Proxy :: Type -> Type )

Since: base-4.7.0.0

Instance details

Defined in Data.Proxy

Monad m => Monad ( ListT m)
Instance details

Defined in Control.Monad.Trans.List

Monad m => Monad ( MaybeT m)
Instance details

Defined in Control.Monad.Trans.Maybe

Monad f => Monad ( Rec1 f)

Since: base-4.9.0.0

Instance details

Defined in GHC.Generics

( Monoid a, Monoid b) => Monad ( (,,) a b)

Since: base-4.14.0.0

Instance details

Defined in GHC.Base

Methods

(>>=) :: (a, b, a0) -> (a0 -> (a, b, b0)) -> (a, b, b0) Source #

(>>) :: (a, b, a0) -> (a, b, b0) -> (a, b, b0) Source #

return :: a0 -> (a, b, a0) Source #

Monad m => Monad ( Kleisli m a)

Since: base-4.14.0.0

Instance details

Defined in Control.Arrow

Monad f => Monad ( Ap f)

Since: base-4.12.0.0

Instance details

Defined in Data.Monoid

Monad f => Monad ( Alt f)

Since: base-4.8.0.0

Instance details

Defined in Data.Semigroup.Internal

( Applicative f, Monad f) => Monad ( WhenMissing f x)

Equivalent to ReaderT k (ReaderT x (MaybeT f)) .

Since: containers-0.5.9

Instance details

Defined in Data.IntMap.Internal

Monad m => Monad ( IdentityT m)
Instance details

Defined in Control.Monad.Trans.Identity

( Monad m, Error e) => Monad ( ErrorT e m)
Instance details

Defined in Control.Monad.Trans.Error

Monad m => Monad ( ExceptT e m)
Instance details

Defined in Control.Monad.Trans.Except

Monad m => Monad ( ReaderT r m)
Instance details

Defined in Control.Monad.Trans.Reader

Monad m => Monad ( StateT s m)
Instance details

Defined in Control.Monad.Trans.State.Lazy

Monad m => Monad ( StateT s m)
Instance details

Defined in Control.Monad.Trans.State.Strict

( Monoid w, Monad m) => Monad ( WriterT w m)
Instance details

Defined in Control.Monad.Trans.Writer.Lazy

( Monoid w, Monad m) => Monad ( WriterT w m)
Instance details

Defined in Control.Monad.Trans.Writer.Strict

Monad ((->) r :: Type -> Type )

Since: base-2.1

Instance details

Defined in GHC.Base

Methods

(>>=) :: (r -> a) -> (a -> r -> b) -> r -> b Source #

(>>) :: (r -> a) -> (r -> b) -> r -> b Source #

return :: a -> r -> a Source #

( Monad f, Monad g) => Monad (f :*: g)

Since: base-4.9.0.0

Instance details

Defined in GHC.Generics

Methods

(>>=) :: (f :*: g) a -> (a -> (f :*: g) b) -> (f :*: g) b Source #

(>>) :: (f :*: g) a -> (f :*: g) b -> (f :*: g) b Source #

return :: a -> (f :*: g) a Source #

( Monoid a, Monoid b, Monoid c) => Monad ( (,,,) a b c)

Since: base-4.14.0.0

Instance details

Defined in GHC.Base

Methods

(>>=) :: (a, b, c, a0) -> (a0 -> (a, b, c, b0)) -> (a, b, c, b0) Source #

(>>) :: (a, b, c, a0) -> (a, b, c, b0) -> (a, b, c, b0) Source #

return :: a0 -> (a, b, c, a0) Source #

( Monad f, Monad g) => Monad ( Product f g)

Since: base-4.9.0.0

Instance details

Defined in Data.Functor.Product

( Monad f, Applicative f) => Monad ( WhenMatched f x y)

Equivalent to ReaderT Key (ReaderT x (ReaderT y (MaybeT f)))

Since: containers-0.5.9

Instance details

Defined in Data.IntMap.Internal

( Applicative f, Monad f) => Monad ( WhenMissing f k x)

Equivalent to ReaderT k (ReaderT x (MaybeT f)) .

Since: containers-0.5.9

Instance details

Defined in Data.Map.Internal

Monad f => Monad ( M1 i c f)

Since: base-4.9.0.0

Instance details

Defined in GHC.Generics

Methods

(>>=) :: M1 i c f a -> (a -> M1 i c f b) -> M1 i c f b Source #

(>>) :: M1 i c f a -> M1 i c f b -> M1 i c f b Source #

return :: a -> M1 i c f a Source #

( Monad f, Applicative f) => Monad ( WhenMatched f k x y)

Equivalent to ReaderT k (ReaderT x (ReaderT y (MaybeT f)))

Since: containers-0.5.9

Instance details

Defined in Data.Map.Internal

( Monoid w, Monad m) => Monad ( RWST r w s m)
Instance details

Defined in Control.Monad.Trans.RWS.Lazy

Methods

(>>=) :: RWST r w s m a -> (a -> RWST r w s m b) -> RWST r w s m b Source #

(>>) :: RWST r w s m a -> RWST r w s m b -> RWST r w s m b Source #

return :: a -> RWST r w s m a Source #

( Monoid w, Monad m) => Monad ( RWST r w s m)
Instance details

Defined in Control.Monad.Trans.RWS.Strict

Methods

(>>=) :: RWST r w s m a -> (a -> RWST r w s m b) -> RWST r w s m b Source #

(>>) :: RWST r w s m a -> RWST r w s m b -> RWST r w s m b Source #

return :: a -> RWST r w s m a Source #

class ( Alternative m, Monad m) => MonadPlus (m :: Type -> Type ) where Source #

Monads that also support choice and failure.

Minimal complete definition

Nothing

Methods

mzero :: m a Source #

The identity of mplus . It should also satisfy the equations

mzero >>= f  =  mzero
v >> mzero   =  mzero

The default definition is

mzero = empty

mplus :: m a -> m a -> m a Source #

An associative operation. The default definition is

mplus = (<|>)

Instances

Instances details
MonadPlus []

Since: base-2.1

Instance details

Defined in GHC.Base

Methods

mzero :: [a] Source #

mplus :: [a] -> [a] -> [a] Source #

MonadPlus Maybe

Since: base-2.1

Instance details

Defined in GHC.Base

MonadPlus IO

Since: base-4.9.0.0

Instance details

Defined in GHC.Base

MonadPlus Option

Since: base-4.9.0.0

Instance details

Defined in Data.Semigroup

MonadPlus STM

Since: base-4.3.0.0

Instance details

Defined in GHC.Conc.Sync

MonadPlus ReadP

Since: base-2.1

Instance details

Defined in Text.ParserCombinators.ReadP

MonadPlus Seq
Instance details

Defined in Data.Sequence.Internal

MonadPlus P

Since: base-2.1

Instance details

Defined in Text.ParserCombinators.ReadP

Methods

mzero :: P a Source #

mplus :: P a -> P a -> P a Source #

MonadPlus ( U1 :: Type -> Type )

Since: base-4.9.0.0

Instance details

Defined in GHC.Generics

( ArrowApply a, ArrowPlus a) => MonadPlus ( ArrowMonad a)

Since: base-4.6.0.0

Instance details

Defined in Control.Arrow

MonadPlus ( Proxy :: Type -> Type )

Since: base-4.9.0.0

Instance details

Defined in Data.Proxy

Monad m => MonadPlus ( ListT m)
Instance details

Defined in Control.Monad.Trans.List

Monad m => MonadPlus ( MaybeT m)
Instance details

Defined in Control.Monad.Trans.Maybe

MonadPlus f => MonadPlus ( Rec1 f)

Since: base-4.9.0.0

Instance details

Defined in GHC.Generics

MonadPlus m => MonadPlus ( Kleisli m a)

Since: base-4.14.0.0

Instance details

Defined in Control.Arrow

MonadPlus f => MonadPlus ( Ap f)

Since: base-4.12.0.0

Instance details

Defined in Data.Monoid

MonadPlus f => MonadPlus ( Alt f)

Since: base-4.8.0.0

Instance details

Defined in Data.Semigroup.Internal

MonadPlus m => MonadPlus ( IdentityT m)
Instance details

Defined in Control.Monad.Trans.Identity

( Monad m, Error e) => MonadPlus ( ErrorT e m)
Instance details

Defined in Control.Monad.Trans.Error

( Monad m, Monoid e) => MonadPlus ( ExceptT e m)
Instance details

Defined in Control.Monad.Trans.Except

MonadPlus m => MonadPlus ( ReaderT r m)
Instance details

Defined in Control.Monad.Trans.Reader

MonadPlus m => MonadPlus ( StateT s m)
Instance details

Defined in Control.Monad.Trans.State.Lazy

MonadPlus m => MonadPlus ( StateT s m)
Instance details

Defined in Control.Monad.Trans.State.Strict

( Monoid w, MonadPlus m) => MonadPlus ( WriterT w m)
Instance details

Defined in Control.Monad.Trans.Writer.Lazy

( Monoid w, MonadPlus m) => MonadPlus ( WriterT w m)
Instance details

Defined in Control.Monad.Trans.Writer.Strict

( MonadPlus f, MonadPlus g) => MonadPlus (f :*: g)

Since: base-4.9.0.0

Instance details

Defined in GHC.Generics

Methods

mzero :: (f :*: g) a Source #

mplus :: (f :*: g) a -> (f :*: g) a -> (f :*: g) a Source #

( MonadPlus f, MonadPlus g) => MonadPlus ( Product f g)

Since: base-4.9.0.0

Instance details

Defined in Data.Functor.Product

MonadPlus f => MonadPlus ( M1 i c f)

Since: base-4.9.0.0

Instance details

Defined in GHC.Generics

Methods

mzero :: M1 i c f a Source #

mplus :: M1 i c f a -> M1 i c f a -> M1 i c f a Source #

( Monoid w, MonadPlus m) => MonadPlus ( RWST r w s m)
Instance details

Defined in Control.Monad.Trans.RWS.Lazy

Methods

mzero :: RWST r w s m a Source #

mplus :: RWST r w s m a -> RWST r w s m a -> RWST r w s m a Source #

( Monoid w, MonadPlus m) => MonadPlus ( RWST r w s m)
Instance details

Defined in Control.Monad.Trans.RWS.Strict

Methods

mzero :: RWST r w s m a Source #

mplus :: RWST r w s m a -> RWST r w s m a -> RWST r w s m a Source #

(=<<) :: Monad m => (a -> m b) -> m a -> m b infixr 1 Source #

Same as >>= , but with the arguments interchanged.

(>=>) :: Monad m => (a -> m b) -> (b -> m c) -> a -> m c infixr 1 Source #

Left-to-right composition of Kleisli arrows.

' (bs >=> cs) a ' can be understood as the do expression

do b <- bs a
   cs b

(<=<) :: Monad m => (b -> m c) -> (a -> m b) -> a -> m c infixr 1 Source #

Right-to-left composition of Kleisli arrows. ( >=> ) , with the arguments flipped.

Note how this operator resembles function composition ( . ) :

(.)   ::            (b ->   c) -> (a ->   b) -> a ->   c
(<=<) :: Monad m => (b -> m c) -> (a -> m b) -> a -> m c

(>>) :: Monad m => m a -> m b -> m b infixl 1 Source #

Sequentially 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

forever :: Applicative f => f a -> f b Source #

Repeat an action indefinitely.

Using ApplicativeDo : ' forever as ' can be understood as the pseudo- do expression

do as
   as
   ..

with as repeating.

Examples

Expand

A common use of forever is to process input from network sockets, Handle s, 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

join :: Monad m => m (m a) -> m a Source #

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.

' join bss ' can be understood as the do expression

do bs <- bss
   bs

Examples

Expand

A common use of join is to run an IO computation returned from an STM transaction, since STM transactions can't perform IO directly. Recall that

atomically :: STM a -> IO a

is used to run STM transactions atomically. So, by specializing the types of atomically and join to

atomically :: STM (IO b) -> IO (IO b)
join       :: IO (IO b)  -> IO b

we can compose them as

join . atomically :: STM (IO b) -> IO b

to run an STM transaction and the IO action it returns.

mfilter :: MonadPlus m => (a -> Bool ) -> m a -> m a Source #

Direct MonadPlus equivalent of filter .

Examples

Expand

The filter function is just mfilter specialized to the list monad:

filter = ( mfilter :: (a -> Bool) -> [a] -> [a] )

An example using mfilter with the Maybe monad:

>>> mfilter odd (Just 1)
Just 1
>>> mfilter odd (Just 2)
Nothing

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

This generalizes the list-based filter function.

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

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.

zipWithM :: Applicative m => (a -> b -> m c) -> [a] -> [b] -> m [c] Source #

The zipWithM function generalizes zipWith to arbitrary applicative functors.

zipWithM_ :: Applicative m => (a -> b -> m c) -> [a] -> [b] -> m () Source #

zipWithM_ is the extension of zipWithM which ignores the final result.

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.

Note: foldM is the same as foldlM

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

Like foldM , but discards the result.

replicateM :: Applicative m => Int -> m a -> m [a] Source #

replicateM n act performs the action n times, gathering the results.

Using ApplicativeDo : ' replicateM 5 as ' can be understood as the do expression

do a1 <- as
   a2 <- as
   a3 <- as
   a4 <- as
   a5 <- as
   pure [a1,a2,a3,a4,a5]

Note the Applicative constraint.

replicateM_ :: Applicative m => Int -> m a -> m () Source #

Like replicateM , but discards the result.

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

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

Conditional failure of Alternative computations. Defined by

guard True  = pure ()
guard False = empty

Examples

Expand

Common uses of guard include conditionally signaling an error in an error monad and conditionally rejecting the current choice in an Alternative -based parser.

As an example of signaling 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 0
Nothing
>>> safeDiv 4 2
Just 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)

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.

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

The reverse of when .

liftM :: Monad m => (a1 -> r) -> m a1 -> m r Source #

Promote a function to a monad.

liftM2 :: Monad m => (a1 -> a2 -> r) -> m a1 -> m a2 -> m r Source #

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 Source #

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 Source #

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 Source #

Promote a function to a monad, scanning the monadic arguments from left to right (cf. liftM2 ).

liftM' :: Monad m => (a -> b) -> m a -> m b Source #

liftM2' :: Monad m => (a -> b -> c) -> m a -> m b -> m c Source #

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

In many situations, the liftM operations can be replaced by uses of ap , which promotes function application.

return f `ap` x1 `ap` ... `ap` xn

is equivalent to

liftMn f x1 x2 ... xn

(<$!>) :: Monad m => (a -> b) -> m a -> m b infixl 4 Source #

Strict version of <$> .

Since: base-4.8.0.0