[project @ 2001-07-03 11:37:49 by simonmar]
[ghc-base.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.2 2001/07/03 11:37: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 fix :: (a -> a) -> a
41 fix f = let x = f x in x
42
43 class (Monad m) => MonadFix m where
44         mfix :: (a -> m a) -> m a
45
46 -- Perhaps these should live beside (the ST & IO) definition.
47 instance MonadFix Maybe where
48         mfix f = let
49                 a = f $ case a of
50                         Just x -> x
51                         _      -> error "empty mfix argument"
52                 in a