## 1 Introduction

The contstuff library implements a number of monad transformers and monads, which make heavy use of [continuation passing style] (CPS). This makes them both fast and flexible. Please note that this is neither a CPS tutorial nor a monad transformer tutorial. You should understand these concepts, before attempting to use *contstuff*.

## 2 ContT

The

monad transformer is the simplest of all CPS-based monads. It essentially gives you access to the current continuation, which means that it lets you label certain points of execution and reuse these points later in interesting ways. With ContT you get an elegant encoding of computations, which support:

- abortion (premature termination),
- resumption (start a computation at a certain spot),
- branches (aka
*goto*),
- result accumulation,
- etc.

All these features are effects of

. If you don't use them, then

behaves like the identity monad. A computation of type

is a CPS computation with an intermediate result of type

and a final result of type

. The

type can be polymorphic most of the time. You only need to specify it, if you use some of the CPS effects like

. Let's have a look at a small example:

testComp1 :: ContT () IO ()
testComp1 =
forever $ do
txt <- io getLine
case txt of
"info" -> io $ putStrLn "This is a test computation."
"quit" -> abort ()
_ -> return ()

This example demonstrates the most basic feature of

. First of all,

is a monad transformer, so you can for example lift IO actions to a CPS computation. The

function is a handy tool, which corresponds to

from other transformer libraries and to

from

monadLib, but is restricted to the

monad. You can also use the more generic

function, which promotes a base monad computation to

.
Each

subcomputation receives a continuation, which is a function, to which the subcomputation is supposed to pass the result. However, the subcomputation may choose not to call the continuation at all, in which case the entire computation finishes with a final result. The

function does that.
To run a

computation you can use

or the convenience function

:

runContT :: (a -> m r) -> ContT r m a -> m r
evalContT :: Applicative m => ContT r m r -> m r

The

function takes a final continuation transforming the last intermediate result into a final result. The

function simply passes

as the final continuation.