JP Embedded Solutions

What is a monad?

Monads and monad transformers have a wholly undeserved reputation for being somewhat fearful and hard to understand. Unfortunately, even the best books on Haskell tend to further this reputation by choosing to introduce monads by giving a couple of examples, then stating the monad laws and running away very fast, and the treatment of monad transformers, even in the otherwise excellent Real World Haskell, is unclear at best.

As I said, this reputation is undeserved. In this paper I will attempt to give a general motivation for monads as machines which carry around information not all of which is visible at any time, and for which there is a defined recipe to, given two old machines, make a new one

. I then move on to do the same for the closely related topics of monoids, additive monads (MonadPlus in Haskell) and finally monad transformers.

For each of these types I give motivation, an explanation of why the monad laws etc are the way they are, and examples, generally simple ones.  The section on monad transformers is more complex than the rest: I demonstrate a completely general way in which given two monads m and t then Trans t m a := m (t a) is a monad, and Trans t is a monad transformer.  I then show that the classical MaybeT monad transformer is an instance of this construction.  As far as I am aware, this is not written down anywhere easily accessible, so this is a useful contribution not only to pedagogy, but to the general theory of monad transformers.

So here it is:

About these ads

Filed under: Functional Programming, Haskell, , , , , , , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Copyright © 2011, JP Embedded Solutions, All rights reserved

All content is subject to the BSD license

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: