30 Jul 03:21
A fancier Get monad or two (a la binary and binary-strict)
From: Chris Kuklewicz <haskell <at> list.mightyreason.com>
Subject: A fancier Get monad or two (a la binary and binary-strict)
Newsgroups: gmane.comp.lang.haskell.libraries
Date: 2008-07-30 01:23:36 GMT
Subject: A fancier Get monad or two (a la binary and binary-strict)
Newsgroups: gmane.comp.lang.haskell.libraries
Date: 2008-07-30 01:23:36 GMT
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
(Continue reading)
RSS Feed