Storable instance of () is broken

Tom Ellis tom-lists-haskell-cafe-2017 at
Wed Jan 5 12:38:44 UTC 2022

On Wed, Jan 05, 2022 at 05:39:49PM +0530, Harendra Kumar wrote:
> It is hard to objectively or mathematically prove which option is
> better.

Well, let's give it a go.  One condition that instances might be
required to satisfy is

    do { poke p a; peek p } == do { poke p a; evaluate a }
and evaluating these expressions should touch at most `sizeof a` bytes
starting from `p`.

If further you impose the reasonable condition that `sizeof a` should
be as small as possible then this fixes the behaviour of the ()
instance. (Other reasonable conditions are available.)

The existing `Storable ()` instance does *not* satisfy this condition,
because it is not sufficiently strict.  That's orthogonal to
Harendra's complaint though.  It does satisfy the "as small as
possible" part.

On the other hand, despite being no expert of Storable, it seems to me
the class exists merely to conveniently read to and write from raw
memory.  The () instance is useless for this purpose so I'm not sure
why it exists.  If the purpose of the class were to be a general
purpose serialisation API then the () instance *would* make sense, but
then we'd also have an (a, b) instance too, which we don't.

If it were up to me I probably would not have allowed the `Storable
()` instance, and instead I would have designed a "serialise to a raw
buffer" API *on top of* Storable (if that's what people really



More information about the Libraries mailing list