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

Anthony Cowley acowley at seas.upenn.edu
Thu Nov 1 01:07:49 UTC 2018


Edward Kmett writes:

> One messy sketch of how this could proceed would be to make additional
> Addr-based analogues of the report specified type-unrelated "Ptr a"
> functions and install them side by side with the existing functions. This
> would behave much like how we have both readsPrec and readPrec for standard
> vs. GHC specific Read internals. With MINIMAL pragmas it should basically
> become transparent if they are mutually defined. Since generally Storable
> dictionaries aren't built out of compositions of other Storable
> dictionaries, growing the class shouldn't do any measurable harm to
> performance.

This doesn't sound right to me. I'm probably an outlier, but I rely heavily on composite Storable instances in vinyl and Frames and general FFI situations. I don't know if a more common usage like an ad hoc record relying on the Storable instances of its fields would be greatly impacted, but this needs some investigation. More generally, I'm in agreement with the points Sven has been making in this discussion, and wouldn't like to see such an unresolved debate be resolved by imposing a performance penalty on everyone as a compromise.

> We haven't been shy about adding new members to
> report-specified classes like Bits. This doesn't strike me as much
> different.

It is similar, but I think composite Storable instances are much more common than composite Bits instances.

Anthony

>
> At some point in the future we can work out if it is worth it to get the
> report fixed up by incorporating the Addr-based API as the default and make
> the _other_ the legacy.
>
> -Edward
>
>
> On Tue, Oct 30, 2018 at 10:11 AM Sven Panne <svenpanne at gmail.com> wrote:
>
>> 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... :-)
>>
>>
>> _______________________________________________
>> Libraries mailing list
>> Libraries at haskell.org
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
>>
> _______________________________________________
> Libraries mailing list
> Libraries at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries


More information about the Libraries mailing list