[project @ 2002-04-26 13:34:05 by simonmar]
[ghc-base.git] / Control / Monad / Fix.hs
1 -----------------------------------------------------------------------------
2 -- |
3 -- Module      :  Control.Monad.Fix
4 -- Copyright   :  (c) Andy Gill 2001,
5 --                (c) Oregon Graduate Institute of Science and Technology, 2001
6 -- License     :  BSD-style (see the file libraries/core/LICENSE)
7 -- 
8 -- Maintainer  :  libraries@haskell.org
9 -- Stability   :  experimental
10 -- Portability :  portable
11 --
12 -- The Fix monad.
13 --
14 --        Inspired by the paper:
15 --        \em{Functional Programming with Overloading and
16 --            Higher-Order Polymorphism},
17 --          \A[HREF="http://www.cse.ogi.edu/~mpj"]{Mark P Jones},
18 --                Advanced School of Functional Programming, 1995.}
19 --
20 -----------------------------------------------------------------------------
21
22 module Control.Monad.Fix (
23         MonadFix(
24            mfix -- :: (a -> m a) -> m a
25          ),
26         fix     -- :: (a -> a) -> a
27   ) where
28
29 import Prelude
30 import System.IO
31
32 fix :: (a -> a) -> a
33 fix f = let x = f x in x
34
35 class (Monad m) => MonadFix m where
36         mfix :: (a -> m a) -> m a
37
38 instance MonadFix Maybe where
39         mfix f = let
40                 a = f $ case a of
41                         Just x -> x
42                         _      -> error "empty mfix argument"
43                 in a
44
45 instance MonadFix IO where
46         mfix = fixIO
47