A fancier Get monad or two (a la binary and binary-strict)

Chris Kuklewicz haskell at list.mightyreason.com
Tue Jul 29 21:23:36 EDT 2008

Summary: I have two new Get-like monads for binary data (byte-aligned) that also
   (*) Suspend parsing and request more import when reading past the end of data.
       It is possible to respond with Nothing to indicate the hard end of data.
   (*) Return failures instead of calling error
   (*) Offer lookAhead,lookAheadM,lookAheadE like Data.Binary.Get
   (*) Are BinaryParser instances from Data.Binary.Strict.Class
   (*) Are Monad Transformers (and thus MonadIO)
   (*) Are MonadError,Plus,Reader,Writer,State and Applicative,Alternative
   (*) They differ because one is also MonadCont while the other is simpler
   (*) Simplified Non-transformer versions (applied to Identity) are defined

Note: Making the suspend/resume-with-more-input work efficiently with 
MonadError/Alternative is one improvement to binary-strict.  Make callCC work 
with sane semantics at the same time was non-trivial.

Long version and where to find the source:

In the process of writing an implementation of google's protocol buffers, I used 
binary's Put monad and binary-strict's BinaryParser class (i.e. binary-strict's 
Incremental Get monad).

The Get in the binary package works on Lazy ByteString (good for me) but tends 
to call "error" when unhappy, which was not ideal.  The Incremental Get from the 
binary-strict package does not want to work on Lazy bytestrings (it can be 
adapted), but the internals hold onto data too long and obstruct garbage 
collection (this is how it appears to me when I read the code).

So I have made a fancier Get monad that offers a large collection of features. 
I am announcing this in case other people might want to further adapt my 
solution for their own ends (or just use it outright).  Removing feature from my 
code ought to be much easier than adding them was.

The code itself is currently living in a few files inside the protocol-buffers 
project at http://darcs.haskell.org/packages/protocol-buffers/Text/ProtocolBuffers/

The files are "MyGet.hs" or "MyGetW.hs", there are two flavors.
(The "testDepth" file is annotated test output from MyGetW's testDepth 
function).  They depend on binary-strict, but this would be easy to comment out.

They both contain code "lifted" from binary and "inspired" by binary-strict.

MyGetW.hs is the version with a MonadCont instance.
MyGet.hs has the (forall b.) to simplify the types and thus has no MonadCont 

I have tested the code, but it is still quite new.  I doubt I will have time to 
make a proper cabal release. I may see if the maintainers of the binary-strict 
or binary packages are interested in a fancier Get monad.

   Chris K

More information about the Libraries mailing list