| Safe Haskell | Trustworthy |
|---|---|
| Language | Haskell2010 |
Protolude.Monad
Synopsis
- class Applicative m => Monad (m :: Type -> Type) where
- class (Alternative m, Monad m) => MonadPlus (m :: Type -> Type) 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
- (>>) :: Monad m => m a -> m b -> m b
- 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 :: Monad m => (a -> m [b]) -> [a] -> m [b]
- guard :: Alternative f => Bool -> f ()
- when :: Applicative f => Bool -> f () -> f ()
- unless :: Applicative f => Bool -> f () -> f ()
- 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
- liftM' :: Monad m => (a -> b) -> m a -> m b
- liftM2' :: Monad m => (a -> b -> c) -> m a -> m b -> m c
- ap :: Monad m => m (a -> b) -> m a -> m b
- (<$!>) :: Monad m => (a -> b) -> m a -> m b
Documentation
class Applicative m => Monad (m :: Type -> Type) 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:
- Left identity
returna>>=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 List, Maybe and IO
defined in the Prelude satisfy these laws.
Minimal complete definition
Methods
(>>=) :: 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.
'as ' can be understood as the >>= bsdo 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 equivialent to
\x f ->join(fmapf x) :: Monad m => m a -> (a -> m b) -> m b
which 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.
Inject 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.
Instances
| Monad Complex | Since: base-4.9.0.0 |
| Monad First | Since: base-4.9.0.0 |
| Monad Last | Since: base-4.9.0.0 |
| Monad Max | Since: base-4.9.0.0 |
| Monad Min | Since: base-4.9.0.0 |
| Monad Put | |
| Monad Seq | |
| Monad Tree | |
| Monad NonEmpty | @since base-4.9.0.0 |
| Monad STM | @since base-4.3.0.0 |
| Monad Identity | @since base-4.8.0.0 |
| Monad First | @since base-4.8.0.0 |
| Monad Last | @since base-4.8.0.0 |
| Monad Down | @since base-4.11.0.0 |
| Monad Dual | @since base-4.8.0.0 |
| Monad Product | @since base-4.8.0.0 |
| Monad Sum | @since base-4.8.0.0 |
| Monad Par1 | @since base-4.9.0.0 |
| Monad P | @since base-2.01 |
| Monad ReadP | @since base-2.01 |
| Monad IO | @since base-2.01 |
| Monad Q | |
| Monad Maybe | @since base-2.01 |
| Monad Solo | @since base-4.15 |
| Monad [] | @since base-2.01 |
| Monad m => Monad (WrappedMonad m) | Since: base-4.7.0.0 |
Defined in Control.Applicative Methods (>>=) :: WrappedMonad m a -> (a -> WrappedMonad m b) -> WrappedMonad m b # (>>) :: WrappedMonad m a -> WrappedMonad m b -> WrappedMonad m b # return :: a -> WrappedMonad m a # | |
| Monad (SetM s) | |
| ArrowApply a => Monad (ArrowMonad a) | @since base-2.01 |
Defined in GHC.Internal.Control.Arrow Methods (>>=) :: ArrowMonad a a0 -> (a0 -> ArrowMonad a b) -> ArrowMonad a b # (>>) :: ArrowMonad a a0 -> ArrowMonad a b -> ArrowMonad a b # return :: a0 -> ArrowMonad a a0 # | |
| Monad (Either e) | @since base-4.4.0.0 |
| Monad (Proxy :: Type -> Type) | @since base-4.7.0.0 |
| Monad (U1 :: Type -> Type) | @since base-4.9.0.0 |
| Monad (ST s) | @since base-2.01 |
| Monad (IParser t) | |
| Monad m => Monad (MaybeT m) | |
| Monoid a => Monad ((,) a) | @since base-4.9.0.0 |
| (Applicative f, Monad f) => Monad (WhenMissing f x) | Equivalent to Since: containers-0.5.9 |
Defined in Data.IntMap.Internal Methods (>>=) :: WhenMissing f x a -> (a -> WhenMissing f x b) -> WhenMissing f x b # (>>) :: WhenMissing f x a -> WhenMissing f x b -> WhenMissing f x b # return :: a -> WhenMissing f x a # | |
| Monad m => Monad (Kleisli m a) | @since base-4.14.0.0 |
| Monad f => Monad (Ap f) | @since base-4.12.0.0 |
| Monad f => Monad (Alt f) | @since base-4.8.0.0 |
| Monad f => Monad (Rec1 f) | @since base-4.9.0.0 |
| Monad (t m) => Monad (LiftingAccum t m) | Since: mtl-2.3 |
Defined in Control.Monad.Accum Methods (>>=) :: LiftingAccum t m a -> (a -> LiftingAccum t m b) -> LiftingAccum t m b # (>>) :: LiftingAccum t m a -> LiftingAccum t m b -> LiftingAccum t m b # return :: a -> LiftingAccum t m a # | |
| Monad (t m) => Monad (LiftingSelect t m) | Since: mtl-2.3 |
Defined in Control.Monad.Select Methods (>>=) :: LiftingSelect t m a -> (a -> LiftingSelect t m b) -> LiftingSelect t m b # (>>) :: LiftingSelect t m a -> LiftingSelect t m b -> LiftingSelect t m b # return :: a -> LiftingSelect t m a # | |
| (Monoid w, Functor m, Monad m) => Monad (AccumT w m) | |
| Monad m => Monad (ExceptT e m) | |
| Monad m => Monad (IdentityT m) | |
| Monad m => Monad (ReaderT r m) | |
| Monad m => Monad (SelectT r m) | |
| Monad m => Monad (StateT s m) | |
| Monad m => Monad (StateT s m) | |
| Monad m => Monad (WriterT w m) | |
| (Monoid w, Monad m) => Monad (WriterT w m) | |
| (Monoid w, Monad m) => Monad (WriterT w m) | |
| Monad m => Monad (Reverse m) | Derived instance. |
| (Monoid a, Monoid b) => Monad ((,,) a b) | @since base-4.14.0.0 |
| (Monad f, Monad g) => Monad (Product f g) | Since: base-4.9.0.0 |
| (Monad f, Applicative f) => Monad (WhenMatched f x y) | Equivalent to Since: containers-0.5.9 |
Defined in Data.IntMap.Internal Methods (>>=) :: WhenMatched f x y a -> (a -> WhenMatched f x y b) -> WhenMatched f x y b # (>>) :: WhenMatched f x y a -> WhenMatched f x y b -> WhenMatched f x y b # return :: a -> WhenMatched f x y a # | |
| (Applicative f, Monad f) => Monad (WhenMissing f k x) | Equivalent to Since: containers-0.5.9 |
Defined in Data.Map.Internal Methods (>>=) :: WhenMissing f k x a -> (a -> WhenMissing f k x b) -> WhenMissing f k x b # (>>) :: WhenMissing f k x a -> WhenMissing f k x b -> WhenMissing f k x b # return :: a -> WhenMissing f k x a # | |
| (Monad f, Monad g) => Monad (f :*: g) | @since base-4.9.0.0 |
| Monad (ContT r m) | |
| (Monoid a, Monoid b, Monoid c) => Monad ((,,,) a b c) | @since base-4.14.0.0 |
| Monad ((->) r) | @since base-2.01 |
| (Monad f, Applicative f) => Monad (WhenMatched f k x y) | Equivalent to Since: containers-0.5.9 |
Defined in Data.Map.Internal Methods (>>=) :: WhenMatched f k x y a -> (a -> WhenMatched f k x y b) -> WhenMatched f k x y b # (>>) :: WhenMatched f k x y a -> WhenMatched f k x y b -> WhenMatched f k x y b # return :: a -> WhenMatched f k x y a # | |
| Monad f => Monad (M1 i c f) | @since base-4.9.0.0 |
| Monad m => Monad (RWST r w s m) | |
| (Monoid w, Monad m) => Monad (RWST r w s m) | |
| (Monoid w, Monad m) => Monad (RWST r w s m) | |
class (Alternative m, Monad m) => MonadPlus (m :: Type -> Type) where #
Monads that also support choice and failure.
Minimal complete definition
Nothing
Methods
The identity of mplus. It should also satisfy the equations
mzero >>= f = mzero v >> mzero = mzero
The default definition is
mzero = empty
An associative operation. The default definition is
mplus = (<|>)
Instances
| MonadPlus Seq | |
| MonadPlus STM | Takes the first non- @since base-4.3.0.0 |
| MonadPlus P | @since base-2.01 |
Defined in GHC.Internal.Text.ParserCombinators.ReadP | |
| MonadPlus ReadP | @since base-2.01 |
| MonadPlus IO | Takes the first non-throwing @since base-4.9.0.0 |
| MonadPlus Maybe | Picks the leftmost @since base-2.01 |
| MonadPlus [] | Combines lists by concatenation, starting from the empty list. @since base-2.01 |
Defined in GHC.Internal.Base | |
| (ArrowApply a, ArrowPlus a) => MonadPlus (ArrowMonad a) | @since base-4.6.0.0 |
Defined in GHC.Internal.Control.Arrow | |
| MonadPlus (Proxy :: Type -> Type) | @since base-4.9.0.0 |
| MonadPlus (U1 :: Type -> Type) | @since base-4.9.0.0 |
| Monad m => MonadPlus (MaybeT m) | |
| MonadPlus m => MonadPlus (Kleisli m a) | @since base-4.14.0.0 |
| MonadPlus f => MonadPlus (Ap f) | @since base-4.12.0.0 |
| MonadPlus f => MonadPlus (Alt f) | @since base-4.8.0.0 |
| MonadPlus f => MonadPlus (Rec1 f) | @since base-4.9.0.0 |
| (Monoid w, Functor m, MonadPlus m) => MonadPlus (AccumT w m) | |
| (Monad m, Monoid e) => MonadPlus (ExceptT e m) | |
| MonadPlus m => MonadPlus (IdentityT m) | |
| MonadPlus m => MonadPlus (ReaderT r m) | |
| MonadPlus m => MonadPlus (SelectT r m) | |
| MonadPlus m => MonadPlus (StateT s m) | |
| MonadPlus m => MonadPlus (StateT s m) | |
| (Functor m, MonadPlus m) => MonadPlus (WriterT w m) | |
| (Monoid w, MonadPlus m) => MonadPlus (WriterT w m) | |
| (Monoid w, MonadPlus m) => MonadPlus (WriterT w m) | |
| MonadPlus m => MonadPlus (Reverse m) | Derived instance. |
| (MonadPlus f, MonadPlus g) => MonadPlus (Product f g) | Since: base-4.9.0.0 |
| (MonadPlus f, MonadPlus g) => MonadPlus (f :*: g) | @since base-4.9.0.0 |
| MonadPlus f => MonadPlus (M1 i c f) | @since base-4.9.0.0 |
| (Functor m, MonadPlus m) => MonadPlus (RWST r w s 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.
as >>= f == f =<< as
forever :: Applicative f => f a -> f b #
Repeat an action indefinitely.
Examples
A common use of forever is to process input from network sockets,
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 socketforkFinally(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 and short-circuits after some number of iterations.
then MonadPlus actually returns forevermzero, effectively short-circuiting its caller.
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.
'' can be understood as the join bssdo expression
do bs <- bss bs
Examples
>>>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 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
filterM :: Applicative m => (a -> m Bool) -> [a] -> m [a] #
This generalizes the list-based filter function.
runIdentity (filterM (Identity . p) xs) == filter p xs
Examples
>>>filterM (\x -> doputStrLn ("Keep: " ++ show x ++ "?") answer <- getLine pure (answer == "y")) [1, 2, 3] Keep: 1? y Keep: 2? n Keep: 3? y [1,3]
>>>filterM (\x -> doputStr (show x) x' <- readLn pure (x == x')) [1, 2, 3] 12 22 33 [2,3]
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.
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 actact n times,
and then returns the list of results.
replicateM n (pure x) == replicate n xExamples
>>>replicateM 3 getLinehi heya hiya ["hi","heya","hiya"]
>>>import Control.Monad.State>>>runState (replicateM 3 $ state $ \s -> (s, s + 1)) 1([1,2,3],4)
replicateM_ :: Applicative m => Int -> m a -> m () #
concatMapM :: Monad m => (a -> m [b]) -> [a] -> m [b] #
guard :: Alternative f => Bool -> f () #
Conditional failure of Alternative computations. Defined by
guard True =pure() guard False =empty
Examples
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 otherwise. For example:Just (x `div`
y)
>>>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)
when :: Applicative f => Bool -> f () -> f () #
Conditional execution of Applicative expressions. For example,
Examples
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:
unless :: Applicative f => Bool -> f () -> f () #
The reverse of when.
Examples
>>>do x <- getLineunless (x == "hi") (putStrLn "hi!") comingupwithexamplesisdifficult hi!
>>>unless (pi > exp 1) NothingJust ()
liftM :: Monad m => (a1 -> r) -> m a1 -> m r #
Promote a function to a monad.
This is equivalent to fmap but specialised to Monads.
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.
Examples
>>>liftM2 (+) [0,1] [0,2][0,2,1,3]
>>>liftM2 (+) (Just 1) NothingNothing
>>>liftM2 (+) (+ 3) (* 2) 518
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).