Syntax extension for Monads in Ocaml

Jacques Carette, Lydia E. van Dijk and Oleg Kiselyov


This Camlp4 extension provides some syntactic sugar to beautify monadic expressions.

Example: A simple but realistic example of the use of a list monad looks like this

          [1; 2; 3]
          (fun a -> bind
                      [3; 4; 5]
                      (fun b -> return (a + b)))

where we assume the appropriate definitions of the functions "bind" and "return". With the help of "pa_monad" this can be written as

          a <-- [1; 2; 3];
          b <-- [3; 4; 5];
          return (a + b)

which is much clearer and thus easier to understand and maintain. By the way, the expression evaluates to

        [4; 5; 6; 5; 6; 7; 6; 7; 8]

the sum of each pair of values of the input lists.

For more examples have a look at the examples or A complete package (as a .tar.gz file) containing the extension itself, a Makefile, examples and several tests is available. See the README for more details.

Change history

For a detailed list of changes please refer to the ChangeLog.

Version Changes
6.0 Separate version information from Makefile: the package's version number now resides in file VERSION.
5.2 Remove Tuareg mode patches; use Elisp customization file instead. Rename all source files with dashes in their names to avoid a new compiler warning.
5.1 Add MIT license as a possible alternative to LGPL.
5.0 Adapt to OCaml's new preprocessor in version 3.10. Big thanks go to Till Varoquaux for his assistance with this particular change! A version for OCaml 3.09 is still included.
4.2 Add patch for Tuareg 1.46.2. Add delimited continuation monad (multi-prompt shift/reset) example.
4.1 Change syntax of recursive bindings to "rec"-"and". Attach "rec" keyword to individual recursive binding not to the "perform" keyword. Replace GPL with LGPL. Update documentation concerning the discussions on the Haskell mailing list. Add IO-monad example.
4.0 Support mutually recursive bindings.
3.0 Add patch for Tuareg-mode to recognize "perform" keyword.
2.1 Use homegrown unit-test suite. Add syntax for recursive bindings ("perform rec ..."). Drop the semicolon from "let ID = EXPR in; EXPR" construction.
2.0 Extend syntax with a "with exp1 and exp2" form. Rename "fail" to "failwith" and rename "mdo" to "perform".
1.0 Initial version.

This code is released under the GNU library general public license in a way which is compatible with OCaml's own license.

version 6.0. December 9, 2008