Proposal: Move primitive-Data.Primitive.Addr API into base

Sven Panne svenpanne at gmail.com
Tue Oct 30 14:10:45 UTC 2018


I am not sure if everybody fully comprehends what Storable is all about: It
is meant as the lowest-level building block in an Addr-free world
(remember: Addr is a GHCism and is *not* mentioned anywhere in the report)
to put a few well-defined simple Haskell types into memory or read them
from there. Its explicit non-goals are:

   * Achieve 100% type safety. In the presence of raw memory access,
castPtr, C calls etc. this would be a total illusion. Forcing API users to
sprinkle tons of castPtr at every possible place over their code wouldn't
improve safety at all, it would only hurt readability.

   * Handle more complicated sum/product types. How would you do this?
Respect your native ABI (i.e. automatically handle padding/alignment)?
Tightly packed? Or even handle a foreign ABI? Your own ABI? Some funny
encoding like OpenGL's packed data types? Etc. etc. You can build all of
those things in a layer above Storable, probably introducing other type
classes or some marshaling DSLs.

   * Portability of the written values. This is more in the realm of
serialization libraries.

More concretely:

Am Di., 30. Okt. 2018 um 14:34 Uhr schrieb Daniel Cartwright <
chessai1996 at gmail.com>:

> [19:26:50] <chessai_> hPutBuf :: Handle -> Ptr a -> Int -> IO () [...]
>

The signature for this is actually perfect: hPutBuf doesn't care about what
stuff has been written into the given buffer, it just cares about its start
and its size. Forcing castPtr Kung Fu here wouldn't buy you anything: The
buffer will probably contain a wild mix of Haskell values or even no
Haskell values at all, but that doesn't matter. Whatever you pass as "a" or
whatever you cast from/to is probably a lie from the typing perspective. At
this level this is no problem at all.


> [19:30:02] <chessai_> peekByteOff :: Ptr b -> Int -> IO a
> [19:30:09] <chessai_> peekByteOff :: Addr -> Int -> IO a
> [19:30:26] <chessai_> what is 'b' doing there? it's not used in any
> meaningful way by peekByteOff [...]
>

If you have a pointer pointing to something and shift that pointer by some
bytes, you are probably pointing to something completely different, so of
course "b" and "a" have nothing to do with each other. So peekByteOff
intentionally ignores "b".


> [19:32:22] <carter> pokeElemOff :: Ptr a -> Int -> a -> IO () --- way
> better than peak  [...]
>

Yes, because this is intended to be used for *arrays* of values of the same
type. Note "Elem" vs. "Byte".


> [19:33:12] <carter> hvr: lets add safePeekByteOff :: Ptr a -> Int -> IO a
> ? [...]
>

This signature doesn't make sense, see above: Shifting a pointer by an
arbitrary amount of bytes will probably change the type of what you're
pointing to. If you shift by units of the underlying type, well, that's
peekElemOff.


> [19:35:31] <chessai_> carter: i am glad we agree on the smell
>

I don't have the full chat log, but I think I don't even agree on the
smell, at least not at the places I've seen... :-)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/libraries/attachments/20181030/f9c3df88/attachment.html>


More information about the Libraries mailing list