[Haskell-cafe] Building a monoid, continuation-passing style

Derek Elkins derek.a.elkins at gmail.com
Tue Sep 15 21:17:09 EDT 2009

On Mon, Sep 14, 2009 at 10:25 AM, Martijn van Steenbergen
<martijn at van.steenbergen.nl> wrote:
> Hello cafe,
> Inspired by Sean Leather's xformat package [1] I built a datatype with which
> you can build a monoid with holes, yielding a function type to fill in these
> holes, continuation-passing style. Here are some primitives and their types:
>> now   :: m -> ContSt m r r
>> later :: (a -> m) -> ContSt m r (a -> r)
>> run   :: ContSt m m r -> r
>> instance Monoid m => Category (ContSt m)
> Here's an example of how to use it:
>> run (now "hello" . now "world")
> "helloworld"
>> run (later id . now "world") "hello"
> "helloworld"
>> run (later id . later show) "hello" 567
> "hello567"
> The source code is available at [2].
> I have a couple of questions:
> * ContSt is a Category. Is it also an Arrow? Why (not)?
> * Did I miss any other obvious classes this type is an instance of?
> * What is its relation with the Cont and Reader monads?
> * Are there any other useful applications other than printf-like
> functionality?
> * ContSt is a horrible name. What is a better one?
> For those who have a bit more time: I appreciate any comments and
> suggestions on the code. :-)

I believe this technique is based on a technique introduced in Olivier
Danvy's "Functional Unparsing".  While not immediately applicable to
Haskell unless you want to make/use a delimited continuation monad,
you may find the paper "On Typing Delimited Continuations: Three New
Solutions to the Printf Problem" by Kenichi Asai interesting.  It is
available at the following url:

More information about the Haskell-Cafe mailing list