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 -- Continuation monads.
13 -----------------------------------------------------------------------------
15 module Control.Monad.Cont (
26 module Control.Monad.Trans,
32 import Control.Monad.Trans
33 import Control.Monad.Reader
34 import Control.Monad.Writer
35 import Control.Monad.State
36 import Control.Monad.RWS
38 class (Monad m) => MonadCont m where
39 callCC :: ((a -> m b) -> m a) -> m a
41 -- ---------------------------------------------------------------------------
42 -- Our parameterizable continuation monad
44 newtype Cont r a = Cont { runCont :: (a -> r) -> r }
46 instance Functor (Cont r) where
47 fmap f m = Cont $ \c -> runCont m (c . f)
49 instance Monad (Cont r) where
51 m >>= k = Cont $ \c -> runCont m $ \a -> runCont (k a) c
53 instance MonadCont (Cont r) where
54 callCC f = Cont $ \c -> runCont (f (\a -> Cont $ \_ -> c a)) c
56 mapCont :: (r -> r) -> Cont r a -> Cont r a
57 mapCont f m = Cont $ f . runCont m
59 withCont :: ((b -> r) -> (a -> r)) -> Cont r a -> Cont r b
60 withCont f m = Cont $ runCont m . f
62 -- ---------------------------------------------------------------------------
63 -- Our parameterizable continuation monad, with an inner monad
65 newtype ContT r m a = ContT { runContT :: (a -> m r) -> m r }
67 instance (Monad m) => Functor (ContT r m) where
68 fmap f m = ContT $ \c -> runContT m (c . f)
70 instance (Monad m) => Monad (ContT r m) where
71 return a = ContT ($ a)
72 m >>= k = ContT $ \c -> runContT m (\a -> runContT (k a) c)
74 instance (Monad m) => MonadCont (ContT r m) where
75 callCC f = ContT $ \c -> runContT (f (\a -> ContT $ \_ -> c a)) c
77 instance MonadTrans (ContT r) where
78 lift m = ContT (m >>=)
80 instance (MonadIO m) => MonadIO (ContT r m) where
81 liftIO = lift . liftIO
83 instance (MonadReader r' m) => MonadReader r' (ContT r m) where
85 local f m = ContT $ \c -> do
87 local f (runContT m (local (const r) . c))
89 instance (MonadState s m) => MonadState s (ContT r m) where
93 -- -----------------------------------------------------------------------------
94 -- MonadCont instances for other monad transformers
96 instance (MonadCont m) => MonadCont (ReaderT r m) where
97 callCC f = ReaderT $ \r ->
99 runReaderT (f (\a -> ReaderT $ \_ -> c a)) r
101 instance (MonadCont m) => MonadCont (StateT s m) where
102 callCC f = StateT $ \s ->
104 runStateT (f (\a -> StateT $ \s' -> c (a, s'))) s
106 instance (Monoid w, MonadCont m) => MonadCont (WriterT w m) where
109 runWriterT (f (\a -> WriterT $ c (a, mempty)))
111 instance (Monoid w, MonadCont m) => MonadCont (RWST r w s m) where
112 callCC f = RWST $ \r s ->
114 runRWST (f (\a -> RWST $ \_ s' -> c (a, s', mempty))) r s
116 mapContT :: (m r -> m r) -> ContT r m a -> ContT r m a
117 mapContT f m = ContT $ f . runContT m
119 withContT :: ((b -> m r) -> (a -> m r)) -> ContT r m a -> ContT r m b
120 withContT f m = ContT $ runContT m . f