a596f445fcec06f327637bedde2de163c473fdc8
[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 :  non-portable (reqruires multi-param type classes)
11 --
12 -- $Id: Fix.hs,v 1.1 2001/06/28 14:15:02 simonmar Exp $
13 --
14 -- The Fix monad.
15 --
16 --        Inspired by the paper:
17 --        \em{Functional Programming with Overloading and
18 --            Higher-Order Polymorphism},
19 --          \A[HREF="http://www.cse.ogi.edu/~mpj"]{Mark P Jones},
20 --                Advanced School of Functional Programming, 1995.}
21 --
22 -----------------------------------------------------------------------------
23
24 module Control.Monad.Fix (
25         MonadFix(
26            mfix -- :: (a -> m a) -> m a
27          ),
28         fix     -- :: (a -> a) -> a
29   ) where
30
31 import Prelude
32
33 import System.IO
34 import Control.Monad.ST
35
36
37 fix :: (a -> a) -> a
38 fix f = let x = f x in x
39
40 class (Monad m) => MonadFix m where
41         mfix :: (a -> m a) -> m a
42
43 -- Perhaps these should live beside (the ST & IO) definition.
44 instance MonadFix IO where
45         mfix = fixIO
46
47 instance MonadFix (ST s) where
48         mfix = fixST
49
50 instance MonadFix Maybe where
51         mfix f = let
52                 a = f $ case a of
53                         Just x -> x
54                         _      -> error "empty mfix argument"
55                 in a