[project @ 2002-03-14 12:09:49 by simonmar]
[haskell-directory.git] / Control / Monad / Fix.hs
1 {-# OPTIONS -fno-implicit-prelude #-}
2 -----------------------------------------------------------------------------
3 -- 
4 -- Module      :  Control.Monad.Fix
5 -- Copyright   :  (c) Andy Gill 2001,
6 --                (c) Oregon Graduate Institute of Science and Technology, 2001
7 -- License     :  BSD-style (see the file libraries/core/LICENSE)
8 -- 
9 -- Maintainer  :  libraries@haskell.org
10 -- Stability   :  experimental
11 -- Portability :  portable
12 --
13 -- $Id: Fix.hs,v 1.3 2002/03/14 12:09:49 simonmar Exp $
14 --
15 -- The Fix monad.
16 --
17 --        Inspired by the paper:
18 --        \em{Functional Programming with Overloading and
19 --            Higher-Order Polymorphism},
20 --          \A[HREF="http://www.cse.ogi.edu/~mpj"]{Mark P Jones},
21 --                Advanced School of Functional Programming, 1995.}
22 --
23 -----------------------------------------------------------------------------
24
25 module Control.Monad.Fix (
26         MonadFix(
27            mfix -- :: (a -> m a) -> m a
28          ),
29         fix     -- :: (a -> a) -> a
30   ) where
31
32 #ifdef __GLASGOW_HASKELL__
33 -- MonadFix is needed by System.IO, so it is below the Prelude.
34 import Control.Monad
35 import GHC.Base
36 import GHC.Err
37 import Data.Maybe
38 #endif
39
40 import System.IO
41
42 fix :: (a -> a) -> a
43 fix f = let x = f x in x
44
45 class (Monad m) => MonadFix m where
46         mfix :: (a -> m a) -> m a
47
48 instance MonadFix Maybe where
49         mfix f = let
50                 a = f $ case a of
51                         Just x -> x
52                         _      -> error "empty mfix argument"
53                 in a
54
55 instance MonadFix IO where
56         mfix = fixIO
57