From e982249359ca541e85a55cff28d98b656e487e51 Mon Sep 17 00:00:00 2001 From: erkok Date: Sat, 28 Sep 2002 00:29:32 +0000 Subject: [PATCH] [project @ 2002-09-28 00:29:32 by erkok] documentation for the mdo-notation. (I've run this through jade, no syntax errors, but couldn't get any html output as my docbook installation is a bit weird: couldn't find style-files etc. it'll be great if someone can run it through to make sure what I added looks acceptable..) --- ghc/docs/users_guide/glasgow_exts.sgml | 68 ++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/ghc/docs/users_guide/glasgow_exts.sgml b/ghc/docs/users_guide/glasgow_exts.sgml index a780c37..bcc7d1e 100644 --- a/ghc/docs/users_guide/glasgow_exts.sgml +++ b/ghc/docs/users_guide/glasgow_exts.sgml @@ -4024,6 +4024,74 @@ classes usually have one "main" parameter for which deriving new instances is most interesting. + + +The recursive do-notation + + + The recursive do-notation (a.k.a. mdo-notation) is implemented as described in +"A recursive do for Haskell", +Levent Erkok, John Launchbury", +Haskell Workshop 2002, pages: 29-37. Pittsburgh, Pennsylvania. + + +The do-notation of Haskell does not allow recursive bindings, +that is, the variables bound in a do-expression are visible only in the textually following +code block. Compare this to a let-expression, where bound variables are visible in the entire binding +group. It turns out that several applications can benefit from recursive bindings in +the do-notation, and this extension provides the necessary syntactic support. + + +Here is a simple (yet contrived) example: + + +import Control.Monad.MonadRec + +justOnes = mdo xs <- Just (1:xs) + return xs + + +There are three important points in using the recursive-do notation: + + +The recursive version of the do-notation uses the keyword mdo (rather +than do). + + + +The scripts using mdo should import Control.Monad.MonadRec + + + +As with other extensions, ghc should be given the flag -fglasgow-exts + + + + + +The MonadRec library introduces the MonadRec class. It's definition is: + + +class Monad m => MonadRec m where + mfix :: (a -> m a) -> m a + + +The MonadRec class declares the function mfix, +which dictates how the recursion should behave. If recursive bindings are required for a monad, +then that monad must be declared an instance of the MonadRec class. +For details, see the above mentione reference. + + +The MonadRec library automatically declares List, Maybe, IO, and +state monads (both lazy and strict) as instances of the MonadRec class. + + + +The web page: http://www.cse.ogi.edu/PacSoft/projects/rmb +contains up to date information on recursive monadic bindings. + + + -- 1.7.10.4