[project @ 2003-06-03 22:26:44 by diatchki]
[ghc-base.git] / Control / Monad / X / Utils.hs
1 module Control.Monad.X.Utils where
2
3 -- | This is a private module and is not to be imported
4 -- directly by non-library modules.
5
6
7 import Prelude(return,fail,(.))
8 import Control.Monad(MonadPlus(..))
9 import Control.Monad.X.Trans
10
11 -- has base
12 inBase' m = lift (inBase m)
13
14 -- monad 
15 return' x = lift (return x)
16 fail' msg = lift (fail msg)
17
18 -- reader
19 ask'      :: (MonadTrans t, MonadReader r m) => t m r
20 ask'          = lift ask
21 local' map f  = map (local f)
22
23 -- writer
24 tell' w                 = lift (tell w)
25 listen1' mk unmk add m  = mk (do (x,w) <- listen (unmk m)
26                                  return (add w x))
27 listen2' mk unmk add m  = mk (\s -> do (x,w) <- listen (unmk m s)
28                                        return (add w x))
29
30 -- state
31 get'      :: (MonadTrans t, MonadState s m) => t m s
32 get'      = lift get
33 put' s    = lift (put s)
34
35 -- error
36 throwError' e             = lift (throwError e)
37 catchError1' mk unmk m h  = mk (catchError (unmk m) (unmk . h))
38 catchError2' mk unmk m h  = mk (\y -> catchError (unmk m y) (\e -> unmk (h e) y))
39
40 -- mplus
41 mzero'    :: (MonadTrans t, MonadPlus m) => t m a
42 mzero'              = lift mzero
43 mplus1' mk unmk m n = mk (mplus (unmk m) (unmk n))
44 mplus2' mk unmk m n = mk (\y -> unmk m y `mplus` unmk n y)
45
46 -- cont
47 callCC1' mk unmk ret f  = mk (callCC (\br -> unmk (f (\a -> lift (br (ret a))))))
48 callCC2' mk unmk ret f  = mk (\s -> callCC (\br -> unmk (f (\a -> lift (br (ret a s)))) s))
49
50
51
52