53d5c424ab624c3c7fde0429a5b0614eb927c442
[packages/random.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 -- /Functional Programming with Overloading and
16 -- Higher-Order Polymorphism/,
17 -- Mark P Jones (<http://www.cse.ogi.edu/~mpj>)
18 -- Advanced School of Functional Programming, 1995.
19 -----------------------------------------------------------------------------
20
21 module Control.Monad.Fix (
22 MonadFix(
23 mfix -- :: (a -> m a) -> m a
24 ),
25 fix -- :: (a -> a) -> a
26 ) where
27
28 import Prelude
29 import System.IO
30
31 fix :: (a -> a) -> a
32 fix f = let x = f x in x
33
34 class (Monad m) => MonadFix m where
35 mfix :: (a -> m a) -> m a
36
37 instance MonadFix Maybe where
38 mfix f = let
39 a = f $ case a of
40 Just x -> x
41 _ -> error "empty mfix argument"
42 in a
43
44 instance MonadFix IO where
45 mfix = fixIO
46