[Haskell-beginners] Monad and bind operator interpretation

Rein Henrichs rein.henrichs at gmail.com
Tue Dec 15 21:00:42 UTC 2015


Raja <rajasharan at gmail.com> writes:

> Now my new hypothetical interpretation becomes:
>
> (>>=) :: (a -> m b) -> m a -> m b

You have independently discovered (=<<), which is excellent news!

> If i further add parens:
>
> (>>=) (a -> m b) -> (m a -> m b)
>
> This allows me to slightly tweak my interpretation:
>
> bind takes one param f (of type a -> m b) and returns another param f (of type m
> a -> m b)
>
> This feels like a more intuitive way to think about Monads - am I on the right
> track?
> (not that I want to switch the params permanently - just trying to get a feel
> for monads)

Yes, you are absolutely on the right track. One way to interpret bind is
as a particular way to abstract function application. Consider these
types:

    ($)    ::              (a ->   b) ->   a ->   b
    (<$>)  :: Functor f => (a ->   b) -> f a -> f b
    (=<<)  :: Monad m   => (a -> m b) -> m a -> m b

All of them take a particular type of function and "lift" it to apply to
a particular type of value. ($) performs an "identity lift", which is to
say that it does no lifting at all[1]. (<$>), aka fmap, lifts functions
to apply "in" a Functor "context". (=<<) lifts functions (of a certain
type) to apply in a Monad context. (The ability to talk about these
sorts of abstract functions is one of the motivations of Category
Theory, not that you need to know CT to do Haskell.)

Note also that these are listed in strictly increasing order of
expressiveness, which is to say that a later operator can do anything
that an earlier operator can do, but not vice versa[2]. If you explore
exactly what (=<<) can do that fmap cannot, you may learn something else
about monads!

[1]: By the way, you can also define ($) as id. id f = f is exactly the
     "identity lift" that I am taliking about! Using your "add parens"
     interpretation, ($) takes an (a -> b) and gives an (a -> b) (the
     same one you gave it).

[2]: As long as you admit that Identity x is equivalent to x.


More information about the Beginners mailing list