[Haskell-cafe] Data.Binary Endianness

Thomas Schilling nominolo at googlemail.com
Mon Sep 10 13:50:48 EDT 2007

On Mon, 2007-09-10 at 18:40 +0200, Sven Panne wrote:

> Type classes might be used to get a slightly smaller API, but I am unsure 
> about the performance impact and how much this would really buy us in terms 
> of the ease of use of the API.

There shouldn't be any problem w.r.t. performance, the compiler can
specialize and inline.  What's the problem with reading custom data?

data MP3 = MP3 { ... }

instance Binary MP3 where
  get = MP3 <$> getHeader <*> getData -- [*]
    where getHeader = do magic <- getWord32le
	                 case magic of

  put = ...

to read a (IEEE) double you use

  do x <- (get :: Double); ...

but most of the time the right type will be inferred from the context.
If there are serious performance problems it'd could probably be
attacked using some more use of inline declarations or maybe use of
compiler rewrite rules (in the library).  I don't expect things to work
as smoothly as they maybe could (the package is in version 0.3), but so
far things don't seem too bad.  Did you do any benchmarks?

/ Thomas 

[*] .. note that <$> .. <*> is just a more convenient way of writing:
       do hdr <- getHeader
          dat <- getData
          return (MP3 hdr dat)
    Those operators live in Control.Applicative and are included in
recent versions of the 'base' package.  (

More information about the Haskell-Cafe mailing list