[Haskell-beginners] Haskell Serialization

Daniel Fischer daniel.is.fischer at web.de
Tue May 11 14:16:28 EDT 2010

On Tuesday 11 May 2010 19:45:42, Stephen Tetley wrote:
> An Int8 is a bit more problematic

Not really, the Binary instances for IntN just use fromIntegral to convert 
between IntN and WordN before serialising/after deserialising a WordN.
Now, it's not written in the language definition that fromIntegral should 
be a reinterpretation of the bit-pattern for these conversions, but I think 
you can pretty much rely on that.

What might be a problem for the unwary are the instance for Int and Word 
(guess what they do and why).

> I wouldn't like to rely on it for inter-working between Haskell and C.

Int8 and Word8 are unproblematic (unless you have a ones-complement 
machine), the problems appear only for larger types, as there's no 
guarantee whether your architecture is big-endian or little-endian. But if 
you know that, you know what to do on the C end.

> For floating point numbers - representations are quite likely to
> diverge, one would hope any good protocol would pick a proper
> representation (e.g. IEEE-754).
> Data.Binary is a "Standard Library" so any Haskell compiler should use
> the same code and produce the same binary layout.
> Best wishes
> Stephen

More information about the Beginners mailing list