[Haskell-cafe] Re: Abstraction leak

Donald Bruce Stewart dons at cse.unsw.edu.au
Wed Jul 4 07:44:13 EDT 2007


phil:
> On Wed, Jul 04, 2007 at 09:02:15PM +1000, Donald Bruce Stewart wrote:
> >phil:
> >>On Sun, Jul 01, 2007 at 06:07:13PM +0100, Andrew Coppin wrote:
> >>>I haven't actually tried, but presumably a TCP connection is represented 
> >>>in the same way as a file, and so has the same problems.
> >>>
> >>>Basically doing binary I/O seems to be one of those things that in 
> >>Haskell >falls into the class of "it's possibly but annoyingly messy"...
> >>
> >>In an ideal world there would be a 'deriving Serializable[1]' you
> >
> >derive Binary
> >   (use an external tool for this)
> 
> such as?

Binary instances are pretty easy to write. For a simple data type:

       > instance Binary Exp where
       >       put (IntE i)          = do put (0 :: Word8)
       >                                  put i
       >       put (OpE s e1 e2)     = do put (1 :: Word8)
       >                                  put s
       >                                  put e1
       >                                  put e2

       >       get = do tag <- getWord8
       >                case tag of
       >                    0 -> liftM  IntE get
       >                    1 -> liftM3 OpE  get get get

The Data.Binary comes with one tool to derive these. The DrIFT preprocessor
also can, as can Stefan O'Rear's SYB deriver.

I just write them by hand, or use the tool that comes with the lib.

More docs here,
    http://hackage.haskell.org/packages/archive/binary/0.3/doc/html/Data-Binary.html

> 
> >>could do on datatypes which would get this right. In a really ideal
> >>world, you could specify the data layout somehow[2][2a], which would
> >
> >Directly in Haskell data type decls -- see the ICFP 05 paper on the
> >House OS, which added packing annotations, and bit syntax. In current
> >Haskell, you specify the layout in instances Storable or Binary.
> 
> I'll have a look.
> 
> >>make integrating Haskell code into a wider distributed network of
> >>processes exchanging binary data a cinch. In a super really ideal
> >
> >Definitely. See things like the zlib or iconv
> >Data.Binary/Data.ByteString bindings, for prototypes. The 'tar'
> >reader/writer on hackage.haskell.org is also a good example.
> 
> OK. Maybe this is the sort of stuff which ought to go into the new
> Haskell book? 'Integrating Haskell with external data sources' or
> something...

Indeed :-)
  
> >>world, you could operate on the packets in place in Haskell where
> >>possible and save the deserialization overhead...
> >
> >Data.ByteString.* for this.
> >
> >>Anyone trying to do any of this?
> >
> >Yeah, its a bit of a hot topic currently. :)
> 
> >Gotta chase Erlang for hacking network data.
> 
> Absolutely. Ta for the pointers anyway.

Yeah, so:

    Data.ByteString
    Data.Bits
    Data.Binary

Hack those bytes! Quickly! :-)

-- Don


More information about the Haskell-Cafe mailing list