Copyright | (c) The University of Glasgow 2001 (c) Jeff Newbern 2003-2007 (c) Andriy Palamarchuk 2007 |
---|---|
License | BSD-style (see the file LICENSE) |
Maintainer | ross@soi.city.ac.uk |
Stability | experimental |
Portability | non-portable (type families) |
Safe Haskell | None |
Language | GHC2021 |
Control.Monad.Cont
Description
- Computation type:
- Computations which can be interrupted and resumed.
- Binding strategy:
- Binding a function to a monadic value creates a new continuation which uses the function as the continuation of the monadic computation.
- Useful for:
- Complex control structures, error handling, and creating co-routines.
- Zero and plus:
- None.
- Example type:
Cont
r a
The Continuation monad represents computations in continuation-passing style
(CPS).
In continuation-passing style function result is not returned,
but instead is passed to another function,
received as a parameter (continuation).
Computations are built up from sequences
of nested continuations, terminated by a final continuation (often id
)
which produces the final result.
Since continuations are functions which represent the future of a computation,
manipulation of the continuation functions can achieve complex manipulations
of the future of the computation,
such as interrupting a computation in the middle, aborting a portion
of a computation, restarting a computation, and interleaving execution of
computations.
The Continuation monad adapts CPS to the structure of a monad.
Before using the Continuation monad, be sure that you have a firm understanding of continuation-passing style and that continuations represent the best solution to your particular design problem. Many algorithms which require continuations in other languages do not require them in Haskell, due to Haskell's lazy semantics. Abuse of the Continuation monad can produce code that is impossible to understand and maintain.
Synopsis
- class Monad m => MonadCont (m :: Type -> Type) where
- callCC :: ((a -> m b) -> m a) -> m a
- type Cont r = ContT r Identity
- runCont :: Cont r a -> (a -> r) -> r
- mapCont :: (r -> r) -> Cont r a -> Cont r a
- withCont :: ((b -> r) -> a -> r) -> Cont r a -> Cont r b
- newtype ContT (r :: k) (m :: k -> Type) a = ContT {
- runContT :: (a -> m r) -> m r
- mapContT :: forall {k} m (r :: k) a. (m r -> m r) -> ContT r m a -> ContT r m a
- withContT :: forall {k} b m (r :: k) a. ((b -> m r) -> a -> m r) -> ContT r m a -> ContT r m b
- class Applicative m => Monad (m :: Type -> Type) where
- class Monad m => MonadFail (m :: Type -> Type) where
- fail :: String -> m a
- mapM_ :: (Foldable t, Monad m) => (a -> m b) -> t a -> m ()
- sequence_ :: (Foldable t, Monad m) => t (m a) -> m ()
- mapM :: (Traversable t, Monad m) => (a -> m b) -> t a -> m (t b)
- sequence :: (Traversable t, Monad m) => t (m a) -> m (t a)
- class Functor (f :: Type -> Type) where
- (=<<) :: Monad m => (a -> m b) -> m a -> m b
- join :: Monad m => m (m a) -> m a
- class (Alternative m, Monad m) => MonadPlus (m :: Type -> Type) where
- ap :: Monad m => m (a -> b) -> m a -> m b
- liftM :: Monad m => (a1 -> r) -> m a1 -> m r
- liftM2 :: Monad m => (a1 -> a2 -> r) -> m a1 -> m a2 -> m r
- liftM3 :: Monad m => (a1 -> a2 -> a3 -> r) -> m a1 -> m a2 -> m a3 -> m r
- liftM4 :: Monad m => (a1 -> a2 -> a3 -> a4 -> r) -> m a1 -> m a2 -> m a3 -> m a4 -> m r
- liftM5 :: Monad m => (a1 -> a2 -> a3 -> a4 -> a5 -> r) -> m a1 -> m a2 -> m a3 -> m a4 -> m a5 -> m r
- when :: Applicative f => Bool -> f () -> f ()
- forM_ :: (Foldable t, Monad m) => t a -> (a -> m b) -> m ()
- msum :: (Foldable t, MonadPlus m) => t (m a) -> m a
- void :: Functor f => f a -> f ()
- forM :: (Traversable t, Monad m) => t a -> (a -> m b) -> m (t b)
- (<$!>) :: Monad m => (a -> b) -> m a -> m b
- (<=<) :: Monad m => (b -> m c) -> (a -> m b) -> a -> m c
- (>=>) :: Monad m => (a -> m b) -> (b -> m c) -> a -> m c
- foldM :: (Foldable t, Monad m) => (b -> a -> m b) -> b -> t a -> m b
- foldM_ :: (Foldable t, Monad m) => (b -> a -> m b) -> b -> t a -> m ()
- replicateM :: Applicative m => Int -> m a -> m [a]
- replicateM_ :: Applicative m => Int -> m a -> m ()
- unless :: Applicative f => Bool -> f () -> f ()
- filterM :: Applicative m => (a -> m Bool) -> [a] -> m [a]
- forever :: Applicative f => f a -> f b
- mapAndUnzipM :: Applicative m => (a -> m (b, c)) -> [a] -> m ([b], [c])
- mfilter :: MonadPlus m => (a -> Bool) -> m a -> m a
- zipWithM :: Applicative m => (a -> b -> m c) -> [a] -> [b] -> m [c]
- zipWithM_ :: Applicative m => (a -> b -> m c) -> [a] -> [b] -> m ()
- guard :: Alternative f => Bool -> f ()
- module Control.Monad.Trans
MonadCont class
class Monad m => MonadCont (m :: Type -> Type) where Source #
Methods
callCC :: ((a -> m b) -> m a) -> m a Source #
callCC
(call-with-current-continuation)
calls a function with the current continuation as its argument.
Provides an escape continuation mechanism for use with Continuation monads.
Escape continuations allow to abort the current computation and return
a value immediately.
They achieve a similar effect to throwError
and catchError
within a MonadError
monad.
Advantage of this function over calling return
is that it makes
the continuation explicit,
allowing more flexibility and better control
(see examples in Control.Monad.Cont).
The standard idiom used with callCC
is to provide a lambda-expression
to name the continuation. Then calling the named continuation anywhere
within its scope will escape from the computation,
even if it is many layers deep within nested computations.
Instances
MonadCont m => MonadCont (MaybeT m) Source # | |
Defined in Control.Monad.Cont.Class | |
MonadCont m => MonadCont (ExceptT e m) Source # | |
MonadCont m => MonadCont (IdentityT m) Source # | |
Defined in Control.Monad.Cont.Class | |
MonadCont m => MonadCont (ReaderT r m) Source # | |
MonadCont m => MonadCont (StateT s m) Source # | |
MonadCont m => MonadCont (StateT s m) Source # | |
(Monoid w, MonadCont m) => MonadCont (WriterT w m) Source # | |
(Monoid w, MonadCont m) => MonadCont (WriterT w m) Source # | |
MonadCont (ContT r m) Source # | |
(Monoid w, MonadCont m) => MonadCont (RWST r w s m) Source # | |
(Monoid w, MonadCont m) => MonadCont (RWST r w s m) Source # | |
The Cont monad
The ContT monad transformer
newtype ContT (r :: k) (m :: k -> Type) a #
Instances
MonadTrans (ContT r) | |||||
Defined in Control.Monad.Trans.Cont | |||||
MonadFail m => MonadFail (ContT r m) | |||||
Defined in Control.Monad.Trans.Cont | |||||
MonadIO m => MonadIO (ContT r m) | |||||
Defined in Control.Monad.Trans.Cont | |||||
Applicative (ContT r m) | |||||
Functor (ContT r m) | |||||
Monad (ContT r m) | |||||
MonadCont (ContT r m) Source # | |||||
MonadReader m => MonadReader (ContT r m) Source # | |||||
Defined in Control.Monad.Reader.Class Associated Types
| |||||
MonadState m => MonadState (ContT r m) Source # | |||||
Generic (ContT r m a) | |||||
Defined in Control.Monad.Trans.Cont Associated Types
| |||||
type EnvType (ContT r m) Source # | |||||
Defined in Control.Monad.Reader.Class | |||||
type StateType (ContT r m) Source # | |||||
Defined in Control.Monad.State.Class | |||||
type Rep (ContT r m a) | |||||
Defined in Control.Monad.Trans.Cont type Rep (ContT r m a) = D1 ('MetaData "ContT" "Control.Monad.Trans.Cont" "transformers-0.6.1.0-a97a" 'True) (C1 ('MetaCons "ContT" 'PrefixI 'True) (S1 ('MetaSel ('Just "runContT") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 ((a -> m r) -> m r)))) |
class Applicative m => Monad (m :: Type -> Type) where #
Minimal complete definition
Instances
Monad Complex | |
Monad Identity | |
Monad First | |
Monad Last | |
Monad Down | |
Monad First | |
Monad Last | |
Monad Max | |
Monad Min | |
Monad Dual | |
Monad Product | |
Monad Sum | |
Monad NonEmpty | |
Monad STM | |
Monad NoIO | |
Monad Par1 | |
Monad P | |
Monad ReadP | |
Monad ReadPrec | |
Monad IO | |
Monad Maybe | |
Monad Solo | |
Monad [] | |
Monad m => Monad (WrappedMonad m) | |
ArrowApply a => Monad (ArrowMonad a) | |
Monad (Either e) | |
Monad (Proxy :: Type -> Type) | |
Monad (U1 :: Type -> Type) | |
Monad (ST s) | |
Monad m => Monad (MaybeT m) | |
Monoid a => Monad ((,) a) | |
Monad m => Monad (Kleisli m a) | |
Monad f => Monad (Ap f) | |
Monad f => Monad (Alt f) | |
Monad f => Monad (Rec1 f) | |
(Monoid w, Functor m, Monad m) => Monad (AccumT w m) | |
Monad m => Monad (ExceptT e m) | |
Monad m => Monad (IdentityT m) | |
Monad m => Monad (ReaderT r m) | |
Monad m => Monad (SelectT r m) | |
Monad m => Monad (StateT s m) | |
Monad m => Monad (StateT s m) | |
Monad m => Monad (WriterT w m) | |
(Monoid w, Monad m) => Monad (WriterT w m) | |
(Monoid w, Monad m) => Monad (WriterT w m) | |
Monad m => Monad (Reverse m) | |
(Monoid a, Monoid b) => Monad ((,,) a b) | |
(Monad f, Monad g) => Monad (Product f g) | |
(Monad f, Monad g) => Monad (f :*: g) | |
Monad (ContT r m) | |
(Monoid a, Monoid b, Monoid c) => Monad ((,,,) a b c) | |
Monad ((->) r) | |
Monad f => Monad (M1 i c f) | |
Monad m => Monad (RWST r w s m) | |
(Monoid w, Monad m) => Monad (RWST r w s m) | |
(Monoid w, Monad m) => Monad (RWST r w s m) | |
class Monad m => MonadFail (m :: Type -> Type) where #
Instances
class Functor (f :: Type -> Type) where #
Minimal complete definition
Instances
Functor ZipList | |
Defined in Control.Applicative | |
Functor Handler | |
Defined in Control.Exception | |
Functor Complex | |
Defined in Data.Complex | |
Functor Identity | |
Functor First | |
Functor Last | |
Functor Down | |
Functor First | |
Defined in Data.Semigroup | |
Functor Last | |
Defined in Data.Semigroup | |
Functor Max | |
Defined in Data.Semigroup | |
Functor Min | |
Defined in Data.Semigroup | |
Functor Dual | |
Functor Product | |
Functor Sum | |
Functor NonEmpty | |
Functor STM | |
Defined in GHC.Conc.Sync | |
Functor NoIO | |
Functor Par1 | |
Defined in GHC.Generics | |
Functor ArgDescr | |
Defined in System.Console.GetOpt | |
Functor ArgOrder | |
Defined in System.Console.GetOpt | |
Functor OptDescr | |
Defined in System.Console.GetOpt | |
Functor P | |
Defined in Text.ParserCombinators.ReadP | |
Functor ReadP | |
Defined in Text.ParserCombinators.ReadP | |
Functor ReadPrec | |
Defined in Text.ParserCombinators.ReadPrec | |
Functor IO | |
Functor Maybe | |
Functor Solo | |
Functor [] | |
Monad m => Functor (WrappedMonad m) | |
Defined in Control.Applicative | |
Arrow a => Functor (ArrowMonad a) | |
Defined in Control.Arrow | |
Functor (Either a) | |
Defined in Data.Either | |
Functor (Proxy :: Type -> Type) | |
Defined in Data.Proxy | |
Functor (Arg a) | |
Defined in Data.Semigroup | |
Functor (Array i) | |
Functor (U1 :: Type -> Type) | |
Defined in GHC.Generics | |
Functor (V1 :: Type -> Type) | |
Defined in GHC.Generics | |
Functor (ST s) | |
Functor f => Functor (Lift f) | |
Defined in Control.Applicative.Lift | |
Functor m => Functor (MaybeT m) | |
Defined in Control.Monad.Trans.Maybe | |
Functor ((,) a) | |
Arrow a => Functor (WrappedArrow a b) | |
Defined in Control.Applicative | |
Functor m => Functor (Kleisli m a) | |
Defined in Control.Arrow | |
Functor (Const m :: Type -> Type) | |
Defined in Data.Functor.Const | |
Functor f => Functor (Ap f) | |
Functor f => Functor (Alt f) | |
(Generic1 f, Functor (Rep1 f)) => Functor (Generically1 f) | |
Defined in GHC.Generics | |
Functor f => Functor (Rec1 f) | |
Defined in GHC.Generics | |
Functor (URec (Ptr ()) :: Type -> Type) | |
Defined in GHC.Generics | |
Functor (URec Char :: Type -> Type) | |
Defined in GHC.Generics | |
Functor (URec Double :: Type -> Type) | |
Defined in GHC.Generics | |
Functor (URec Float :: Type -> Type) | |
Defined in GHC.Generics | |
Functor (URec Int :: Type -> Type) | |
Defined in GHC.Generics | |
Functor (URec Word :: Type -> Type) | |
Defined in GHC.Generics | |
Functor f => Functor (Backwards f) | |
Defined in Control.Applicative.Backwards | |
Functor m => Functor (AccumT w m) | |
Defined in Control.Monad.Trans.Accum | |
Functor m => Functor (ExceptT e m) | |
Functor m => Functor (IdentityT m) | |
Defined in Control.Monad.Trans.Identity | |
Functor m => Functor (ReaderT r m) | |
Functor m => Functor (SelectT r m) | |
Defined in Control.Monad.Trans.Select | |
Functor m => Functor (StateT s m) | |
Functor m => Functor (StateT s m) | |
Functor m => Functor (WriterT w m) | |
Defined in Control.Monad.Trans.Writer.CPS | |
Functor m => Functor (WriterT w m) | |
Functor m => Functor (WriterT w m) | |
Functor (Constant a :: Type -> Type) | |
Defined in Data.Functor.Constant | |
Functor f => Functor (Reverse f) | |
Defined in Data.Functor.Reverse | |
Functor ((,,) a b) | |
(Functor f, Functor g) => Functor (Product f g) | |
Defined in Data.Functor.Product | |
(Functor f, Functor g) => Functor (Sum f g) | |
Defined in Data.Functor.Sum | |
(Functor f, Functor g) => Functor (f :*: g) | |
Defined in GHC.Generics | |
(Functor f, Functor g) => Functor (f :+: g) | |
Defined in GHC.Generics | |
Functor (K1 i c :: Type -> Type) | |
Defined in GHC.Generics | |
Functor (ContT r m) | |
Functor ((,,,) a b c) | |
Functor ((->) r) | |
(Functor f, Functor g) => Functor (Compose f g) | |
Defined in Data.Functor.Compose | |
(Functor f, Functor g) => Functor (f :.: g) | |
Defined in GHC.Generics | |
Functor f => Functor (M1 i c f) | |
Defined in GHC.Generics | |
Functor m => Functor (RWST r w s m) | |
Defined in Control.Monad.Trans.RWS.CPS | |
Functor m => Functor (RWST r w s m) | |
Functor m => Functor (RWST r w s m) | |
Functor ((,,,,) a b c d) | |
Functor ((,,,,,) a b c d e) | |
Functor ((,,,,,,) a b c d e f) | |
class (Alternative m, Monad m) => MonadPlus (m :: Type -> Type) where #
Minimal complete definition
Nothing
Instances
liftM5 :: Monad m => (a1 -> a2 -> a3 -> a4 -> a5 -> r) -> m a1 -> m a2 -> m a3 -> m a4 -> m a5 -> m r #
replicateM :: Applicative m => Int -> m a -> m [a] #
replicateM_ :: Applicative m => Int -> m a -> m () #
mapAndUnzipM :: Applicative m => (a -> m (b, c)) -> [a] -> m ([b], [c]) #
module Control.Monad.Trans
Example 1: Simple Continuation Usage
Calculating length of a list continuation-style:
calculateLength :: [a] -> Cont r Int calculateLength l = return (length l)
Here we use calculateLength
by making it to pass its result to print
:
main = do runCont (calculateLength "123") print -- result: 3
It is possible to chain Cont
blocks with >>=
.
double :: Int -> Cont r Int double n = return (n * 2) main = do runCont (calculateLength "123" >>= double) print -- result: 6
Example 2: Using callCC
This example gives a taste of how escape continuations work, shows a typical pattern for their usage.
-- Returns a string depending on the length of the name parameter. -- If the provided string is empty, returns an error. -- Otherwise, returns a welcome message. whatsYourName :: String -> String whatsYourName name = (`runCont` id) $ do -- 1 response <- callCC $ \exit -> do -- 2 validateName name exit -- 3 return $ "Welcome, " ++ name ++ "!" -- 4 return response -- 5 validateName name exit = do when (null name) (exit "You forgot to tell me your name!")
Here is what this example does:
- Runs an anonymous
Cont
block and extracts value from it with(`runCont` id)
. Hereid
is the continuation, passed to theCont
block. - Binds
response
to the result of the followingcallCC
block, bindsexit
to the continuation. - Validates
name
. This approach illustrates advantage of usingcallCC
overreturn
. We pass the continuation tovalidateName
, and interrupt execution of theCont
block from inside ofvalidateName
. - Returns the welcome message from the
callCC
block. This line is not executed ifvalidateName
fails. - Returns from the
Cont
block.
Example 3: Using ContT
Monad Transformer
ContT
can be used to add continuation handling to other monads.
Here is an example how to combine it with IO
monad:
import Control.Monad.Cont import System.IO main = do hSetBuffering stdout NoBuffering runContT (callCC askString) reportResult askString :: (String -> ContT () IO String) -> ContT () IO String askString next = do liftIO $ putStrLn "Please enter a string" s <- liftIO $ getLine next s reportResult :: String -> IO () reportResult s = do putStrLn ("You entered: " ++ s)
Action askString
requests user to enter a string,
and passes it to the continuation.
askString
takes as a parameter a continuation taking a string parameter,
and returning IO ()
.
Compare its signature to runContT
definition.