[Haskell-cafe] Fun with the ST monad

mokus at deepbondi.net mokus at deepbondi.net
Fri Feb 25 21:38:17 CET 2011


On Fri, February 25, 2011 11:24 am, Andrew Coppin wrote:
> On 25/02/2011 02:16 AM, wren ng thornton wrote:
>
> > Or
> > converting the whole thing to an iteratee-style computation which is
> > more explicit about the type of stream processing involved and thus what
> > kinds of laziness are possible.
>
> I've heard much about this "iteratee" things, but I've never looked into
> what the hell it actually is.
>
> Today I had a look at TMR #16, which is an explanation which I can just
> about follow. It seems that it's actually a kind of fold - not unlike
> the "streams" of the stream-fusion library (which is something like what
> I thought I might end up needing). It seems to handle *input* very
> nicely, but I don't see much in the way of handling *output* well. (Then
> again, iteratee is just too complex to really comprehend properly.)

I also have had trouble digesting a lot of the literature on iteratees.  A
while back, I wrote up sort of a critique of/alternative to the current
presentations (largely for the self-enlightenment that comes from
wrestling with the concepts myself) and came up with a rather different
perspective on the subject.  I haven't previously shared it, because it's
extremely incomplete (especially the part about enumerators, which I was
about halfway through completely rewriting when I ran out of steam) and is
addressed to a very small (quite possibly non-existent) audience, but feel
free to take a look at it.  I've type-set the document in its current
state to a PDF at:

https://github.com/mokus0/junkbox/blob/master/Papers/HighLevelIteratees/HighLevelIteratees.pdf

This very well may do more to cloud the issue than clarify, and if so I'm
sorry - feel free to disregard me ;)

The short version is that I think there is a more enlightening view of
iteratees than as a kind of a fold.  For me, it makes a lot more sense to
think of them as operations in a particular abstract monad which has one
associated operation, a blocking read.  Under that view, it is also very
easy to extend them in arbitrary directions, such as adding support for
incremental output.

In any case, regarding your original question - I think iteratees are not
the right tool, if for no other reason than that the current
implementations are in my opinion far too brain-bending to use, especially
when it comes to enumeratees which is what you probably need.  Lazy ST
should fit the bill, though.  It works just like normal ST, but acts as if
every bind has 'unsafeInterleaveST'.  There's a good chance that just
changing the imports on your existing code (Control.Monad.ST ->
Control.Monad.ST.Lazy, Data.STRef -> Data.STRef.Lazy, etc.) will make it
work.

-- James




More information about the Haskell-Cafe mailing list