| Safe Haskell | Safe |
|---|---|
| Language | Haskell2010 |
Papa.Base.Export.Control.Monad
- 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
class Functor (f :: * -> *) where #
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.
Minimal complete definition
Instances
| Functor [] | Since: 2.1 |
| Functor Maybe | Since: 2.1 |
| Functor IO | Since: 2.1 |
| Functor Min | Since: 4.9.0.0 |
| Functor Max | Since: 4.9.0.0 |
| Functor First | Since: 4.9.0.0 |
| Functor Last | Since: 4.9.0.0 |
| Functor Option | Since: 4.9.0.0 |
| Functor NonEmpty | Since: 4.9.0.0 |
| Functor ZipList | |
| Functor Dual | Since: 4.8.0.0 |
| Functor Sum | Since: 4.8.0.0 |
| Functor Product | Since: 4.8.0.0 |
| Functor First | |
| Functor Last | |
| Functor (Either a) | Since: 3.0 |
| Functor ((,) a) | Since: 2.1 |
| Functor (Arg a) | Since: 4.9.0.0 |
| Monad m => Functor (WrappedMonad m) | Since: 2.1 |
| Arrow a => Functor (WrappedArrow a b) | Since: 2.1 |
| Functor (Const * m) | Since: 2.1 |
| Functor f => Functor (Alt * f) | |
| Functor ((->) LiftedRep LiftedRep r) | Since: 2.1 |
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.
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.
(>>) :: 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.
Instances
| Monad [] | Since: 2.1 |
| Monad Maybe | Since: 2.1 |
| Monad IO | Since: 2.1 |
| Monad Min | Since: 4.9.0.0 |
| Monad Max | Since: 4.9.0.0 |
| Monad First | Since: 4.9.0.0 |
| Monad Last | Since: 4.9.0.0 |
| Monad Option | Since: 4.9.0.0 |
| Monad NonEmpty | Since: 4.9.0.0 |
| Monad Dual | Since: 4.8.0.0 |
| Monad Sum | Since: 4.8.0.0 |
| Monad Product | Since: 4.8.0.0 |
| Monad First | |
| Monad Last | |
| Monad (Either e) | Since: 4.4.0.0 |
| Monoid a => Monad ((,) a) | Since: 4.9.0.0 |
| Monad m => Monad (WrappedMonad m) | |
| Monad f => Monad (Alt * f) | |
| Monad ((->) LiftedRep LiftedRep r) | Since: 2.1 |
class (Alternative m, Monad m) => MonadPlus (m :: * -> *) where #
Monads that also support choice and failure.
Methods
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 NothingNothing>>>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 xmIf 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.