This package provides the Monad class and related classes and functions.
The class hierarchy is derived from the (Haskell) proposal The Other Prelude but the traditional method names have been kept.
The functions in this library use the following naming conventions:
filter :: (a -> Bool) -> [a] -> [a] filterM :: (Monad m) => (a -> m Bool) -> [a] -> m [a]
sequence :: Monad m => [m a] -> m [a] sequence_ :: Monad m => [m a] -> m ()
sum :: Num a => [a] -> a msum :: MonadPlus m => [m a] -> m a
This package is implementation specific insofar as the compiler may assume that certain items are defined here in a certain way. Changes may thus lead to compiler crashes or java code that will be rejected by the java compiler.
In particular, desugared do expressions will reference Monad, Bind.>>= and Monad.>>.
This package is implicitly imported and besides the additional stuff covers most of what one would get by importing Control.Monad and Control.Applicative in Haskell.
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
Map a function over a Functor
An infix operator that is aliased to Functor.fmap is <$>
A functor with application, providing operations to
A minimal complete definition must include implementations of these functions satisfying the following laws:
The other methods have the following default definitions, which may be overridden with equivalent specialized implementations:
u *> v = pure (const id) <*> u <*> v u <* v = pure const <*> u <*> v
As a consequence of these laws, the Functor instance for f will satisfy
fmap f x = pure f <*> x
If f is also a Monad, it should satisfy
(<*>) = ap
(which implies that Applicative.pure and Apply.<*> satisfy the applicative functor laws).
Minimal complete definition: Applicative.pure and Apply.<*>.
Sequence actions, discarding the value of the first argument.
Sequence actions, discarding the value of the second argument.
Lift a value
Sequentially compose two actions, passing any value produced by the first as an argument to the second.
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 Frege programmer, however, it is best to think of a monad as an abstract datatype of actions.
Frege’s do expressions provide a convenient syntax for writing monadic expressions.
Instances of Monad should satisfy the following laws:
pure a >>= k == k a m >>= pure == m m >>= (\x -> k x >>= h) == (m >>= k) >>= h
Since instances of Monad are also instances of Functor, they additionally shall satisfy the law:
fmap f xs == xs >>= pure • f
which is also the default implementation of Functor.fmap.
The instances of Monad for lists, Maybe and ST defined in the Prelude satisfy these laws.
Minimal complete definition: Bind.>>= and Applicative.pure
Sequentially compose two actions, discarding any value produced by the first, this works like sequencing operators (such as the semicolon) in imperative languages.
The Monad.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.
The MonadFail class augments Monad by adding the MonadFail.fail operation. This operation is not part of the mathematical definition of a monad.
Fail with a message.
A Monad with a left identity.
This value should satisfy left zero:
mzero >>= f = mzero
A Monad that also supports choice and failure and observes the following laws:
mzero `mplus` v = v v `mplus` mzero = v (a `mplus` b) `mplus` c = a `mplus` (b `mplus` c) (a `mplus` b) >>= f = (a >>= f) `mplus` (b >>= f)
an associative operation
left to right Kleisli composition of monads
Right-to-left Kleisli composition of monads. (>=>), with the arguments flipped
nowarn: deep recursion possible
repeat action forever
discard or ignore result of evaluation, such as the return value of an IO action.
msum generalizes the list-based ListMonoid.concat function.
Version of filterM that works on small lists with length < 1000 only.
Beware of stack overflow, and use filterM, when in doubt.
replicateM n act performs the action n times, gathering the results.
Like replicateM, but discards the result.
In many situations, the liftM operations can be replaced by uses of ap, which promotes function application.
pure f `ap` x1 `ap` ... `ap` xn
is equivalent to
liftMn f x1 x2 ... xn
Promote a function to a monad.
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
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).
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.
The zipWithM function generalizes zipWith to arbitrary monads.
zipWithM_ is the extension of zipWithM which ignores the final result.
Turn a list of monadic values [m a] into a monadic value with a list m [a]
sequence [Just 1, Just 3, Just 2] = Just [1,2,3]
This version of sequence runs in constant stack space, but needs heap space proportional to the size of the input list.
A version of sequence that uses the stack and may overflow with longer lists.
A length of about 500 should be ok in most cases.
fold (Monad.>>) over a list of monadic values for side effects
mapM f is equivalent to sequence • map f
mapM_ f is equivalent to sequence_ • map f
forM xs f = mapM_ f xs
for listSource f is a Java-friendly alias for forM_ that works on any list source
foldM f a xs folds a monadic function f over the list xs.
shortFoldM is suitable only for lists with a length way below 1000.
Beware of stack overflow and use foldM instead.
foldM_ is the same as foldM, but discards the result
guard b is pure () if b is true, and MonadZero.mzero otherwise.
when condition monadic returns /action/ of type Monad m => m () if /condition/ is true, otherwise Applicative.pure ().
opposite of when
inherited from Applicative.*>
inherited from Applicative.<*
inherited from Monad.join
inherited from Applicative.*>
inherited from Applicative.<*
inherited from Monad.<*>
inherited from Monad.>>
inherited from Monad.join
inherited from Applicative.*>
inherited from Applicative.<*
inherited from Monad.<*>
inherited from Monad.join
inherited from Applicative.*>
inherited from Applicative.<*
inherited from Monad.<*>
inherited from Monad.>>
inherited from Monad.join