[Haskell-cafe] Stream processors

Peter Simons simons at cryp.to
Thu Oct 21 10:48:57 EDT 2004


Hi,

I have run across the following idiom several times in the
last few weeks: Some part of my program reads input from a
'Handle' and repeatedly calls a "stream processor" to
incrementally compute some result, say an SHA1 hash, for
example.

All my stream processors can be reduced to the following
interface:

  import Foreign ( Ptr, Word8 )

  type Buffer = (Ptr Word8, Int)

  data StreamProc ctx a
    = SP
      { start  :: IO ctx
      , feed   :: ctx -> Buffer -> IO ctx
      , commit :: ctx -> IO a
      }

I have to initialize some SP-specific context, then I can
feed the data into a stateful computation as it comes in,
and after receiving EOF I finalize the SP to obtain the
result I wanted.

The StreamProc interface works nicely for all kind of
things, be it message digests, encryption, encoding, or
parsers. And obviously, it's a rather generic concept. One
that doesn't have to be re-invented every time ...

Which leads to my question: Is there any generic module
available that implements such a SP? Or something similar?

I know the stream processors as described in Hughes' paper
about Arrows, but those are pure stream processors -- they
don't allow for I/O, which I need to handle the Ptr.

Does anyone have a recommendation for me?

Peter



More information about the Haskell-Cafe mailing list