Haskell Language Monads Definition of Monad


Example

class Monad m where
    return :: a -> m a
    (>>=) :: m a -> (a -> m b) -> m b

The most important function for dealing with monads is the bind operator >>=:

(>>=) :: m a -> (a -> m b) -> m b
  • Think of m a as "an action with an a result".
  • Think of a -> m b as “an action (depending on an a parameter) with a b result.”.

>>= sequences two actions together by piping the result from the first action to the second.

The other function defined by Monad is:

return :: a -> m a

Its name is unfortunate: this return has nothing to do with the return keyword found in imperative programming languages.

return x is the trivial action yielding x as its result. (It is trivial in the following sense:)

return x >>= f       ≡  f x     --  “left identity” monad law
       x >>= return  ≡  x       -- “right identity” monad law