1 -----------------------------------------------------------------------------
3 -- Module : Control.Monad.MonadRec
4 -- Copyright : (c) Oregon Graduate Institute of Science and Technology, 2002
5 -- License : BSD-style (see the file libraries/base/LICENSE)
7 -- Maintainer : libraries@haskell.org, erkok@cse.ogi.edu
8 -- Stability : experimental
9 -- Portability : portable
11 -- Declaration of the MonadRec class, and instances for
12 -- maybe, list, IO, strict state, and lazy state monads
14 -- Note : There's a clear overlap with the Control.Monad.Fix
15 -- module, as they basically define the same structure
16 -- with different names. The "MonadRec" name is kept
17 -- here basically for compatibility with the current Hugs
18 -- implementation. (Note that this duplication also exist
19 -- in the current Hugs release as well.)
21 -----------------------------------------------------------------------------
23 module Control.Monad.MonadRec (
28 import qualified Control.Monad.ST.Lazy as LazyST
29 import qualified Control.Monad.ST as ST
33 fix f = let a = f a in a
35 -- The MonadRec class definition
37 class Monad m => MonadRec m where
38 mfix :: (a -> m a) -> m a
40 -- Instances of MonadRec
43 instance MonadRec Maybe where
44 mfix f = let a = f (unJust a) in a
45 where unJust (Just x) = x
48 instance MonadRec [] where
49 mfix f = case fix (f . head) of
51 (x:_) -> x : mfix (tail . f)
54 instance MonadRec IO where
58 instance MonadRec (LazyST.ST s) where
62 instance MonadRec (ST.ST s) where