|
|
|
# Binary I/O
|
|
|
|
|
|
|
|
|
|
|
|
Haskell 98 treats I/O as character-based, and lacks a well-defined mechanism for binary I/O. However, a number of external libraries exist providing various forms of binary I/O.
|
|
|
|
Binary I/O is a critical need in Haskell for many real-world problems. Haskell 98 treats I/O as character-based, and lacks a well-defined mechanism for binary I/O. However, a number of external libraries exist providing various forms of binary I/O.
|
|
|
|
|
|
|
|
|
|
|
|
Two forms of binary I/O are considered here:
|
|
|
|
|
|
|
|
- Word8 based extensions to Syste.IO, and
|
|
|
|
- Word8 based extensions to System.IO, and
|
|
|
|
- Typeclass-based Binary I/O (referred to as Binary) for serialising arbitrary data types, layered over Word8 extensions
|
|
|
|
|
|
|
|
## Explanation
|
| ... | ... | @@ -28,7 +28,7 @@ Two forms of binary I/O are considered here: |
|
|
|
### Proposal 1
|
|
|
|
|
|
|
|
- The simplest implementation option is [ System.IO](http://www.haskell.org/ghc/docs/latest/html/libraries/base/System-IO.html), which provides hGetBuf-style I/O. More sophisticated systems can be layered on top, as external libraries.
|
|
|
|
- [ Packed strings](http://www.cse.unsw.edu.au/~dons/fps.html), layered over System.IO, are a related interface, and sometimes used for binary I/O of flat data types.
|
|
|
|
- A related library for [ Packed strings](http://www.cse.unsw.edu.au/~dons/fps.html), layered over System.IO, are a related interface, and sometimes used for binary I/O of flat data types.
|
|
|
|
|
|
|
|
### Proposal 2
|
|
|
|
|
| ... | ... | @@ -40,6 +40,12 @@ Two forms of binary I/O are considered here: |
|
|
|
- [ Lambdabot/Hmp3's Binary](http://www.cse.unsw.edu.au/~dons/code/hmp3/Binary.hs), a stripped-down Handle-only version of Binary.
|
|
|
|
- [ SerTH](http://www.cs.helsinki.fi/u/ekarttun/SerTH/) is a Binary-alike, which uses Template Haskell to derive serialiser instances for each data type. It's an alternative to using DrIFT (or handwriting) your own Binary instances. Obviously requires TH. Supports serialising cyclic structures
|
|
|
|
- [ ByteStream](http://freearc.narod.ru/), a new high-performance serialisation library, using gzip compression.
|
|
|
|
- A [ new](http://article.gmane.org/gmane.comp.lang.haskell.cafe/10803) optimised binary library
|
|
|
|
|
|
|
|
## Todo
|
|
|
|
|
|
|
|
- Clarify relationship between Haskell and C strings (or is that for the FFI?)
|
|
|
|
- More clarification of what is meant by \`Binary I/O' (i.e. packed strings, packed data, serialisation, ..)
|
|
|
|
|
|
|
|
## Pros/Cons? : System.IO
|
|
|
|
|
| ... | ... | @@ -57,11 +63,10 @@ Two forms of binary I/O are considered here: |
|
|
|
### Pros
|
|
|
|
|
|
|
|
- The Binary class (particularly as implemented in NewBinary?) is simple to implement and widely used.
|
|
|
|
- Binary IO is an oft requested feature, lack of which is sometimes considered a flaw in Haskell98.
|
|
|
|
- Binary IO is a critical feature, lack of which is sometimes considered a flaw in Haskell98.
|
|
|
|
- Difficult to serialise data without this class
|
|
|
|
|
|
|
|
### Cons
|
|
|
|
|
|
|
|
- There is an overlap with the Storable class that isn't exploited
|
|
|
|
- Doesn't support cyclic structures
|
|
|
|
- Lack of derivability can be annoying |