binary files in haskell
Fergus Henderson
fjh@cs.mu.oz.au
Thu, 8 Feb 2001 21:27:14 +1100
On 08-Feb-2001, John Meacham <john@foo.net> wrote:
> A nice advantage of using my mid-level routines is that there are very
> little requirements placed on 'Byte' as a type, this means that as long
> as to the outside world you only read in 8 bit values and spit 8 bit
> values out you can represent it internally however you want.
>
> for example you might have a machine where a 16 bit word is the smallest
> addressable entity, if you relied on hPut Word8 then your program would
> not work since Word8 cannot exist on that platform. however if you made
> Byte be 16 bits and only used the bottom half of each word then your
> program will run unchanged even among architectures such as this.
I agree that `Byte' is a useful abstraction.
However, I think what you say about Word8 here is not correct.
Word8 can be implemented on a 16-bit machine just by computing all
arithmetic operations modulo 256. There is no requirement that Word8
be physically 8 bits, just that it represents an 8-bit quantity.
Indeed, I think ghc uses this technique, representing Word8 as a full
machine word (e.g. 32 bits for x86, of which the topmost 24 are always
zero).
--
Fergus Henderson <fjh@cs.mu.oz.au> | "I have always known that the pursuit
| of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh> | -- the last words of T. S. Garp.