Add instance Storable Ordering

I don't know exact sementics of Storable, but it's addable anyway. instance Storable http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.h... Ordering where sizeOf http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.h... _ = sizeOf http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.h... (undefined http://hackage.haskell.org/package/base-4.12.0.0/docs/src/GHC.Err.html#undef...::HTYPE_INT) alignment http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.h... _ = alignment http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.h... (undefined http://hackage.haskell.org/package/base-4.12.0.0/docs/src/GHC.Err.html#undef...::HTYPE_INT) peekElemOff http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.h... p http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.h... i http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.h... = liftM http://hackage.haskell.org/package/base-4.12.0.0/docs/src/GHC.Base.html#lift... (`compare` (1::HTYPE_INT)) $ http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.h... peekElemOff (castPtr p) i pokeElemOff http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.h... p http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.h... i http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.h... x http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.h... = pokeElemOff http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.h... (castPtr http://hackage.haskell.org/package/base-4.12.0.0/docs/src/GHC.Ptr.html#castP... p http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.h...) i http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.h... (fromIntegral (fromEnum x http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.h...)::HTYPE_INT)

I would appreciate this too On 10/8/19 4:08 AM, Dannyu NDos wrote:
I don't know exact sementics of Storable, but it's addable anyway.
instance Storable http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.h... Ordering where sizeOf http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.h... _ = sizeOf http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.h... (undefined http://hackage.haskell.org/package/base-4.12.0.0/docs/src/GHC.Err.html#undef...::HTYPE_INT) alignment http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.h... _ = alignment http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.h... (undefined http://hackage.haskell.org/package/base-4.12.0.0/docs/src/GHC.Err.html#undef...::HTYPE_INT) peekElemOff http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.h... p http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.h... i http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.h... = liftM http://hackage.haskell.org/package/base-4.12.0.0/docs/src/GHC.Base.html#lift... (`compare` (1::HTYPE_INT)) $ http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.h... peekElemOff (castPtr p) i pokeElemOff http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.h... p http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.h... i http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.h... x http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.h... = pokeElemOff http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.h... (castPtr http://hackage.haskell.org/package/base-4.12.0.0/docs/src/GHC.Ptr.html#castP... p http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.h...) i http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.h... (fromIntegral (fromEnum x http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.h...)::HTYPE_INT)
_______________________________________________ Libraries mailing list Libraries@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

That seems like a good idea!
Can you make a feature request at https://gitlab.haskell.org/ghc/ghc/issues and
maybe even an merge request?
Cheers,
Simonx
Am Di., 8. Okt. 2019 um 11:09 Uhr schrieb Dannyu NDos : I don't know exact sementics of Storable, but it's addable anyway. instance Storable http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.h... Ordering where sizeOf http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.h... _ = sizeOf http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.h... (undefined http://hackage.haskell.org/package/base-4.12.0.0/docs/src/GHC.Err.html#undef...::HTYPE_INT) alignment http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.h... _ = alignment http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.h... (undefined http://hackage.haskell.org/package/base-4.12.0.0/docs/src/GHC.Err.html#undef...::HTYPE_INT) peekElemOff http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.h... p http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.h... i http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.h... = liftM http://hackage.haskell.org/package/base-4.12.0.0/docs/src/GHC.Base.html#lift... (`compare` (1::HTYPE_INT)) $ http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.h... peekElemOff (castPtr p) i pokeElemOff http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.h... p http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.h... i http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.h... x http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.h... = pokeElemOff http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.h... (castPtr http://hackage.haskell.org/package/base-4.12.0.0/docs/src/GHC.Ptr.html#castP... p http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.h...) i http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.h... (fromIntegral (fromEnum x http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.h...)::HTYPE_INT) _______________________________________________
Libraries mailing list
Libraries@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

Am Di., 8. Okt. 2019 um 16:57 Uhr schrieb Simon Jakobi via Libraries < libraries@haskell.org>:
That seems like a good idea! [...]
Looks OK, but let's use toEnum in peekElemOff: instance Storable Ordering where sizeOf _ = sizeOf (undefined :: CInt) alignment _ = alignment (undefined :: CInt) peekElemOff p i = toEnum . fromIntegral <$> peekElemOff (castPtr p :: Ptr CInt) i pokeElemOff p i = pokeElemOff (castPtr p :: Ptr CInt) i . fromIntegral . fromEnum This is more symmetrical with pokeElemOff and less obfuscated. Note that the instance works for every Enum, so if there are more cases like Ordering, it might be worth to abstract that out.

newtype WrappedEnum a = WrappedEnum a
instance Enum a => Storable (WrappedEnum a) where
...
deriving via (WrappedEnum Ordering)
instance Storable Ordering
On Tue, Oct 8, 2019, 11:34 AM Sven Panne
Am Di., 8. Okt. 2019 um 16:57 Uhr schrieb Simon Jakobi via Libraries < libraries@haskell.org>:
That seems like a good idea! [...]
Looks OK, but let's use toEnum in peekElemOff:
instance Storable Ordering where sizeOf _ = sizeOf (undefined :: CInt) alignment _ = alignment (undefined :: CInt) peekElemOff p i = toEnum . fromIntegral <$> peekElemOff (castPtr p :: Ptr CInt) i pokeElemOff p i = pokeElemOff (castPtr p :: Ptr CInt) i . fromIntegral . fromEnum
This is more symmetrical with pokeElemOff and less obfuscated. Note that the instance works for every Enum, so if there are more cases like Ordering, it might be worth to abstract that out. _______________________________________________ Libraries mailing list Libraries@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

But shouldn't Ordering have the alignment and size of Int8 rather than that
of Int? At least in principle?
On Tue, Oct 8, 2019, 11:38 AM David Feuer
newtype WrappedEnum a = WrappedEnum a
instance Enum a => Storable (WrappedEnum a) where ...
deriving via (WrappedEnum Ordering) instance Storable Ordering
On Tue, Oct 8, 2019, 11:34 AM Sven Panne
wrote: Am Di., 8. Okt. 2019 um 16:57 Uhr schrieb Simon Jakobi via Libraries < libraries@haskell.org>:
That seems like a good idea! [...]
Looks OK, but let's use toEnum in peekElemOff:
instance Storable Ordering where sizeOf _ = sizeOf (undefined :: CInt) alignment _ = alignment (undefined :: CInt) peekElemOff p i = toEnum . fromIntegral <$> peekElemOff (castPtr p :: Ptr CInt) i pokeElemOff p i = pokeElemOff (castPtr p :: Ptr CInt) i . fromIntegral . fromEnum
This is more symmetrical with pokeElemOff and less obfuscated. Note that the instance works for every Enum, so if there are more cases like Ordering, it might be worth to abstract that out. _______________________________________________ Libraries mailing list Libraries@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

On Tue, 8 Oct 2019, David Feuer wrote:
newtype WrappedEnum a = WrappedEnum a instance Enum a => Storable (WrappedEnum a) where ...
That's almost what I recently defined in a custom package: https://hub.darcs.net/thielema/storable-enum/browse/src/Data/Enum/Storable.h... But I also let the user choose the underlying machine type.

Yes, yours is better, but I'd go with Enum on both ends rather than relying on rewrite rules to make sure fromIntegral isn't slow. On Tue, Oct 8, 2019, 11:49 AM Henning Thielemann < lemming@henning-thielemann.de> wrote:
On Tue, 8 Oct 2019, David Feuer wrote:
newtype WrappedEnum a = WrappedEnum a instance Enum a => Storable (WrappedEnum a) where ...
That's almost what I recently defined in a custom package:
https://hub.darcs.net/thielema/storable-enum/browse/src/Data/Enum/Storable.h...
But I also let the user choose the underlying machine type.

A `Word8` would be enough instead of 4 or 8 bytes for `CInt`. On 08/10/2019 17:33, Sven Panne wrote:
Am Di., 8. Okt. 2019 um 16:57 Uhr schrieb Simon Jakobi via Libraries
mailto:libraries@haskell.org>: That seems like a good idea! [...]
Looks OK, but let's use toEnum in peekElemOff:
instance Storable Ordering where sizeOf _ = sizeOf (undefined :: CInt) alignment _ = alignment (undefined :: CInt) peekElemOff p i = toEnum . fromIntegral <$> peekElemOff (castPtr p :: Ptr CInt) i pokeElemOff p i = pokeElemOff (castPtr p :: Ptr CInt) i . fromIntegral . fromEnum
This is more symmetrical with pokeElemOff and less obfuscated. Note that the instance works for every Enum, so if there are more cases like Ordering, it might be worth to abstract that out.
_______________________________________________ Libraries mailing list Libraries@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
participants (7)
-
Dannyu NDos
-
David Feuer
-
Henning Thielemann
-
Simon Jakobi
-
Sven Panne
-
Sylvain Henry
-
Vanessa McHale