One of the annoyances of using monads in OCaml is that the syntax is awkward. You can see why if you look at a simple example. Assume that you're using the usual option monad. If we define >>= to be the bind operator, you might end up with a piece of code that looks like this:

  1. let f x_opt y_opt z_opt =
  2. x_opt >>= (fun x ->
  3. y_opt >>= (fun y ->
  4. z_opt >>= (fun z ->
  5. return (x + y + z))))

This is awful for two reasons: the indentation is absurdly deep, and secondly, and there are too many closing parens at the end. One solution to this is

pa_monad, a camlp4 syntax extension that lets you write the same piece of code like this:

  1. let f x_opt y_opt z_opt =
  2. perform
  3. x <-- x_opt;
  4. y <-- y_opt;
  5. z <-- z_opt;
  6. return (x + y + z)

This is much less painful to look at, but introducing new syntax has a cost, and it's worth seeing if we can make things more livable without resorting to a syntax extension. We can make our original code less eye-gougingly awful by just changing the indentation:

  1. let f x_opt y_opt z_opt =
  2. x_opt >>= (fun x ->
  3. y_opt >>= (fun y ->
  4. z_opt >>= (fun z ->
  5. return (x + y + z))))

That still leaves the parens. But as it happens, the parens can simply be dropped! This was passed on to me by Adam Chlipala, who got it in turn from Xavier Leroy at POPL this year. Our code can thus be written as follows:

  1. let f x_opt y_opt z_opt =
  2. x_opt >>= fun x ->
  3. y_opt >>= fun y ->
  4. z_opt >>= fun z ->
  5. return (x + y + z)

This seems clean enough to actually use in practice. Now all I need is to coerce tuareg into indenting my code this way, and I'll be happy...