evan_tech

Previous Entry Share Next Entry
10:57 am, 12 Aug 06

applications of monads

Neal pointed out that he read a paper that was more useful than my previous explanatory post. I think anyone sufficiently interested would benefit more from reading something written by an expert, so I'm just gonna mostly skip out on continuing that post.

Instead, you get one paragraph: what I was building to is that you can abstract the idea of this sequencing operator (which I called "=>") to applications other than lining up IO operations in order. And a monad, more or less, is a well-defined sequencing operation. Haskell has special syntax to make monads more pleasant, and since it's such a general concept the syntax is reusable in many different places. (Below I switch to Haskell syntax. Instead of writing "foo => {|x| ... }", they use "x <- foo; ...", which (intentionally) feels a lot like an assignment statement.)

Here's a list of useful or interesting applications of monads, from the simple to the crazy. There are many ways to look at monads and different ways work for different people, but one way I think about it is "what does the sequencing step do?" That is, a "foo <- bar" line is a sort of pivot that defines how the code afterward is joined with the code before.
  • IO -- as before.
    x <- comp means "go run comp in the real world, producing x, and then ..."

  • Maybe -- if any step is Nothing, the whole thing is Nothing.
    x <- comp means "if comp produced a value, ..."

  • Either -- if any step fails, then the whole thing produces that error.
    x <- comp means "if comp succeeded, ..."

  • List -- list comprehensions.
    x <- mylist means "for each x in mylist, ...".

  • State Threads -- computations which internally use state but are externally pure. Sequencing is necessary for state. (I wrote a long post about how this works earlier.)

  • Parsec -- parser combinators.
    x <- parse means "if the parser successfully parses an x, ..."

  • QuickCheck -- generators of random inputs to testing code.
    x <- generator means "generate a random input x, and then ..."

  • Probabilistic Functional Programming -- probability distributions.
    x <- dist means "conditioned on each possible outcome in dist, ..." (the page has some simple and illustrative examples).

  • Continuations -- not only in a direct sense (there's a callCC function in the standard library), but also in the general sense of being able to pass around bits of code to be stopped and started. Used in Unifying events and threads, which builds a threads library that simultaneously allows both event-driven- and multithreaded-style code.


A footnote:

One final piece I hadn't noticed before is that Haskell monads also require a "fail" function, which is called on pattern match failure. So, for example, fail in the Maybe monad is also Nothing, you can make assertions about pattern matches: if you expect a computation to produce (Just (Right x)), you can write that as Right x <- comp and if any of it doesn't happen you get Nothing.

I first learned of this when gaal pointed out a cute way to write to write a function to select all the non-Nothing elements from a list:
catMaybes :: [Maybe a] -> [a]
catMaybes l = [x | Just x <- l]

In the list monad, failure is [], so when the pattern match fails on "Just" that element is effectively skipped.