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.html#Storable> Ordering where sizeOf <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.html#sizeOf> _ = sizeOf <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.html#sizeOf> (undefined <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/GHC.Err.html#undefined>::HTYPE_INT) alignment <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.html#alignment> _ = alignment <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.html#alignment> (undefined <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/GHC.Err.html#undefined>::HTYPE_INT) peekElemOff <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.html#peekElemOff> p <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.html#local-6989586621679097926> i <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.html#local-6989586621679097927> = liftM <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/GHC.Base.html#liftM> (`compare` (1::HTYPE_INT)) $ <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.html#peekElemOff> peekElemOff (castPtr p) i pokeElemOff <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.html#pokeElemOff> p <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.html#local-6989586621679097928> i <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.html#local-6989586621679097929> x <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.html#local-6989586621679097930> = pokeElemOff <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.html#pokeElemOff> (castPtr <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/GHC.Ptr.html#castPtr> p <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.html#local-6989586621679097928>) i <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.html#local-6989586621679097929> (fromIntegral (fromEnum x <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.html#local-6989586621679097930>)::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.html#Storable> Ordering where sizeOf <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.html#sizeOf> _ = sizeOf <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.html#sizeOf> (undefined <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/GHC.Err.html#undefined>::HTYPE_INT) alignment <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.html#alignment> _ = alignment <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.html#alignment> (undefined <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/GHC.Err.html#undefined>::HTYPE_INT) peekElemOff <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.html#peekElemOff> p <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.html#local-6989586621679097926> i <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.html#local-6989586621679097927> = liftM <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/GHC.Base.html#liftM> (`compare` (1::HTYPE_INT)) $ <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.html#peekElemOff> peekElemOff (castPtr p) i pokeElemOff <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.html#pokeElemOff> p <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.html#local-6989586621679097928> i <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.html#local-6989586621679097929> x <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.html#local-6989586621679097930> = pokeElemOff <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.html#pokeElemOff> (castPtr <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/GHC.Ptr.html#castPtr> p <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.html#local-6989586621679097928>) i <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.html#local-6989586621679097929> (fromIntegral (fromEnum x <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.html#local-6989586621679097930>)::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 <ndospark320@gmail.com
:
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.html#Storable> Ordering where sizeOf <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.html#sizeOf> _ = sizeOf <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.html#sizeOf> (undefined <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/GHC.Err.html#undefined>::HTYPE_INT) alignment <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.html#alignment> _ = alignment <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.html#alignment> (undefined <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/GHC.Err.html#undefined>::HTYPE_INT) peekElemOff <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.html#peekElemOff> p <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.html#local-6989586621679097926> i <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.html#local-6989586621679097927> = liftM <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/GHC.Base.html#liftM> (`compare` (1::HTYPE_INT)) $ <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.html#peekElemOff> peekElemOff (castPtr p) i pokeElemOff <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.html#pokeElemOff> p <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.html#local-6989586621679097928> i <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.html#local-6989586621679097929> x <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.html#local-6989586621679097930> = pokeElemOff <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.html#pokeElemOff> (castPtr <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/GHC.Ptr.html#castPtr> p <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.html#local-6989586621679097928>) i <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.html#local-6989586621679097929> (fromIntegral (fromEnum x <http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Foreign.Storable.html#local-6989586621679097930>)::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 <svenpanne@gmail.com> 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
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 <david.feuer@gmail.com> wrote:
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 <svenpanne@gmail.com> 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 <libraries@haskell.org <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