[Haskell-cafe] typesafe non-local returns in the IO monad

John Meacham john at repetae.net
Tue Feb 21 07:39:59 EST 2006

Although I am sure I am not the first to discover this, I thought it was
a neat application of the runST trick. Safe non-local returns for
the IO monad. What I mean by safe is that it is impossible to return to
a context that no longer exists. the api is simple:

> module System.IO.Continuation(IOCont(),newContinuation,callContinuation) where
> data IOCont s a = ....
> newContinuation :: (forall s . IOCont s a -> IO b) -> (a -> IO b) -> IO b
> newContinuation act cc = ...
> callContinuation :: IOCont s a -> a -> IO b
> callContinuation cont x = ...

newContinuation runs its first argument passing a fresh jumppoint into
it, if it is jumped to then the argument is passed to the second
argument of newContinuation.

the universal quantification means that the continuation is unable to
escape the action so you know its context is still available on the

in jhc these are just implemented as straight FFI calls to setjmp(2) and
longjmp(2) and an IORef.

I feel using the term 'continuation' is something of a misnomer as true
continuations would allow jumping between multiple stacks I would think
but am unsure what a good name for this is then. Is there a fully
typesafe interface for 'true' IO continuations for some definition of
'true'? hmm....


John Meacham - ⑆repetae.net⑆john⑈

More information about the Haskell-Cafe mailing list