e8ba03dd9a57831cc72f78e76fde7b369676eb13
[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, 2002
6 -- License     :  BSD-style (see the file libraries/base/LICENSE)
7 -- Maintainer  :  libraries@haskell.org
8 -- Stability   :  experimental
9 -- Portability :  portable
10 --
11 -- Monadic fixpoints.
12 --
13 -- For a detailed discussion, see Levent Erkok's thesis,
14 -- /Value Recursion in Monadic Computations/, Oregon Graduate Institute, 2002.
15 --
16 -----------------------------------------------------------------------------
17
18 module Control.Monad.Fix (
19         MonadFix(
20            mfix -- :: (a -> m a) -> m a
21          ),
22         fix     -- :: (a -> a) -> a
23   ) where
24
25 import Prelude
26 import System.IO
27 import Control.Monad.Instances ()
28 import Data.Function (fix)
29 #ifdef __HUGS__
30 import Hugs.Prelude (MonadFix(mfix))
31 #endif
32 #if defined(__GLASGOW_HASKELL__)
33 import GHC.ST
34 #endif
35
36 #ifndef __HUGS__
37 -- | Monads having fixed points with a \'knot-tying\' semantics.
38 -- Instances of 'MonadFix' should satisfy the following laws:
39 --
40 -- [/purity/]
41 --      @'mfix' ('return' . h)  =  'return' ('fix' h)@
42 --
43 -- [/left shrinking/ (or /tightening/)]
44 --      @'mfix' (\\x -> a >>= \\y -> f x y)  =  a >>= \\y -> 'mfix' (\\x -> f x y)@
45 --
46 -- [/sliding/]
47 --      @'mfix' ('Control.Monad.liftM' h . f)  =  'Control.Monad.liftM' h ('mfix' (f . h))@,
48 --      for strict @h@.
49 --
50 -- [/nesting/]
51 --      @'mfix' (\\x -> 'mfix' (\\y -> f x y))  =  'mfix' (\\x -> f x x)@
52 --
53 -- This class is used in the translation of the recursive @do@ notation
54 -- supported by GHC and Hugs.
55 class (Monad m) => MonadFix m where
56         -- | The fixed point of a monadic computation.
57         -- @'mfix' f@ executes the action @f@ only once, with the eventual
58         -- output fed back as the input.  Hence @f@ should not be strict,
59         -- for then @'mfix' f@ would diverge.
60         mfix :: (a -> m a) -> m a
61 #endif /* !__HUGS__ */
62
63 -- Instances of MonadFix for Prelude monads
64
65 -- Maybe:
66 instance MonadFix Maybe where
67     mfix f = let a = f (unJust a) in a
68              where unJust (Just x) = x
69                    unJust Nothing  = error "mfix Maybe: Nothing"
70
71 -- List:
72 instance MonadFix [] where
73     mfix f = case fix (f . head) of
74                []    -> []
75                (x:_) -> x : mfix (tail . f)
76
77 -- IO:
78 instance MonadFix IO where
79     mfix = fixIO 
80
81 -- Prelude types with Monad instances in Control.Monad.Instances
82
83 instance MonadFix ((->) r) where
84     mfix f = \ r -> let a = f a r in a
85
86 instance MonadFix (Either e) where
87     mfix f = let a = f (unRight a) in a
88              where unRight (Right x) = x
89                    unRight (Left  _) = error "mfix Either: Left"
90
91 #if defined(__GLASGOW_HASKELL__)
92 instance MonadFix (ST s) where
93         mfix = fixST
94 #endif
95