1 -----------------------------------------------------------------------------
3 -- Module : Control.Monad.Cont
4 -- Copyright : (c) The University of Glasgow 2001
5 -- License : BSD-style (see the file libraries/core/LICENSE)
7 -- Maintainer : libraries@haskell.org
8 -- Stability : experimental
9 -- Portability : non-portable
11 -- $Id: Cont.hs,v 1.1 2001/06/28 14:15:02 simonmar Exp $
13 -- Continuation monads.
15 -----------------------------------------------------------------------------
17 module Control.Monad.Cont (
28 module Control.Monad.Trans,
34 import Control.Monad.Trans
35 import Control.Monad.Reader
36 import Control.Monad.Writer
37 import Control.Monad.State
38 import Control.Monad.RWS
40 class (Monad m) => MonadCont m where
41 callCC :: ((a -> m b) -> m a) -> m a
43 -- ---------------------------------------------------------------------------
44 -- Our parameterizable continuation monad
46 newtype Cont r a = Cont { runCont :: (a -> r) -> r }
48 instance Functor (Cont r) where
49 fmap f m = Cont $ \c -> runCont m (c . f)
51 instance Monad (Cont r) where
53 m >>= k = Cont $ \c -> runCont m $ \a -> runCont (k a) c
55 instance MonadCont (Cont r) where
56 callCC f = Cont $ \c -> runCont (f (\a -> Cont $ \_ -> c a)) c
58 mapCont :: (r -> r) -> Cont r a -> Cont r a
59 mapCont f m = Cont $ f . runCont m
61 withCont :: ((b -> r) -> (a -> r)) -> Cont r a -> Cont r b
62 withCont f m = Cont $ runCont m . f
64 -- ---------------------------------------------------------------------------
65 -- Our parameterizable continuation monad, with an inner monad
67 newtype ContT r m a = ContT { runContT :: (a -> m r) -> m r }
69 instance (Monad m) => Functor (ContT r m) where
70 fmap f m = ContT $ \c -> runContT m (c . f)
72 instance (Monad m) => Monad (ContT r m) where
73 return a = ContT ($ a)
74 m >>= k = ContT $ \c -> runContT m (\a -> runContT (k a) c)
76 instance (Monad m) => MonadCont (ContT r m) where
77 callCC f = ContT $ \c -> runContT (f (\a -> ContT $ \_ -> c a)) c
79 instance MonadTrans (ContT r) where
80 lift m = ContT (m >>=)
82 instance (MonadIO m) => MonadIO (ContT r m) where
83 liftIO = lift . liftIO
85 instance (MonadReader r' m) => MonadReader r' (ContT r m) where
87 local f m = ContT $ \c -> do
89 local f (runContT m (local (const r) . c))
91 instance (MonadState s m) => MonadState s (ContT r m) where
95 -- -----------------------------------------------------------------------------
96 -- MonadCont instances for other monad transformers
98 instance (MonadCont m) => MonadCont (ReaderT r m) where
99 callCC f = ReaderT $ \r ->
101 runReaderT (f (\a -> ReaderT $ \_ -> c a)) r
103 instance (MonadCont m) => MonadCont (StateT s m) where
104 callCC f = StateT $ \s ->
106 runStateT (f (\a -> StateT $ \s' -> c (a, s'))) s
108 instance (Monoid w, MonadCont m) => MonadCont (WriterT w m) where
111 runWriterT (f (\a -> WriterT $ c (a, mempty)))
113 instance (Monoid w, MonadCont m) => MonadCont (RWST r w s m) where
114 callCC f = RWST $ \r s ->
116 runRWST (f (\a -> RWST $ \_ s' -> c (a, s', mempty))) r s
118 mapContT :: (m r -> m r) -> ContT r m a -> ContT r m a
119 mapContT f m = ContT $ f . runContT m
121 withContT :: ((b -> m r) -> (a -> m r)) -> ContT r m a -> ContT r m b
122 withContT f m = ContT $ runContT m . f