LTMT Part 2: Monads
In part 1 of this tutorial we talked about types and kinds. Knowledge of kinds will help to orient yourself in today's discussion of monads.
What is a monad? When you type "monad" into Hayoo the first result takes you to the documentation for the type class Monad. If you don't already have a basic familiarity with type classes, you can think of a type class as roughly equivalent to a Java interface. A type class defines a set of functions involving a certain data type. When a data type defines all the functions required by the type class, we say that it is an instance of that type class. When a type Foo is an instance of the Monad type class, you'll commonly hear people say "Foo is a monad". Here is a version of the Monad type class.
class Monad m where return :: a -> m a (=<<) :: (a -> m b) -> m a -> m b
(Note: If you're the untrusting type and looked up the real definition to verify that mine is accurate, you'll find that my version is slightly different. Don't worry about that right now. I did it intentionally, and there is a method to my madness.)
This basically says that in order for a data type to be an instance of the Monad type class, it has to define the two functions
(=<<) (pronounced "bind") that have the above type signatures. What do these type signatures tell us? Let's look at
return first. We see that it returns a value of type
m a. This tells us that
m has the kind signature
m :: * -> *. So whenever we hear someone say "Foo is a monad" we immediately know that
Foo :: * -> *.
In part 1, you probably got tired of me emphasizing that a type is a context. When we look at return and bind, this starts to make more sense. The type
m a is just the type
a in the context
m. The type signature
return :: a -> m a tells us that the return function takes a plain value
a and puts that value into the context
m. So when we say something is a monad, we immediately know that we have a function called return that lets us put arbitrary other values into that context.
Now, what about bind? It looks much more complicated and scary, but it's really pretty simple. To see this, let's get rid of all the
m's in the type signature. Here's the before and after.
before :: (a -> m b) -> m a -> m b after :: (a -> b) -> a -> b
The type signature for
after might look familiar. It's exactly the same as the type signature for the
($) function! If you're not familiar with it, Haskell's
$ function is just syntax sugar for function application.
(f $ a) is exactly the same as
(f a). It applies the function
f to its argument
a. It is useful because it has very low precedence and is right associative, so it is a nice syntax sugar that allows us to eliminate parenthesis in certain situations. When you realize that
(=<<) is roughly analogous to the concept of function application (modulo the addition of a context
m), it suddenly makes a lot more sense.
So now what happens when we look at bind's type signature with the
m's back in?
(f =<< k) applies the function
f to the value
k. However, the crucial point is that k is a value wrapped in the context
f's parameter is an unwrapped value
a. From this we see that the bind function's main purpose is to pull a value out of the context
m and apply the function
f, which does some computation, and returns the result back in the context m again.
The monad type class does not provide any mechanism for unconditionally pulling a value out of the context. The only way to get access to the unwrapped value is with the bind function, but bind does this in a controlled way and requires the function to wrap things up again before the result is returned. This behavior, enabled by Haskell's strong static type system, provides complete control over side effects and mutability.
Some monads do provide a way to get a value out of the context, but the choice of whether to do so is completely up to the author of said monad. It is not something inherent in the concept of a monad.
Monads wouldn't be very fun to use if all you had was return, bind, and derived functions. To make them more usable, Haskell has a special syntax called "do notation". The basic idea behind do notation is that there's a bind between every line, and you can do
a <- func to unwrap the return value of func and make it available to later lines with the identifier 'a'.
In summary, a monad is a certain type of context that provides two things: a way to put things into the context, and function application within the context. There is no way to get things out. To get things out, you have to use bind to take yourself into the context. Once you have these two operations, there are lots of other more complicated operations built on the basic primitives that are provided by the API. Much of this is provided in Control.Monad. You probably won't learn all this stuff in a day. Just dive in and use these concepts in real code. Eventually you'll find that the patterns are sinking in and becoming clearer.