Add instance Storable Ordering

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Add instance Storable Ordering

Dannyu NDos
I don't know exact sementics of Storable, but it's addable anyway.

instance Storable Ordering where
   sizeOf _          = sizeOf (undefined::HTYPE_INT)
   alignment _       = alignment (undefined::HTYPE_INT)
   peekElemOff p i   = liftM (`compare` (1::HTYPE_INT)) $ peekElemOff (castPtr p) i
   pokeElemOff p i x = pokeElemOff (castPtr p) i (fromIntegral (fromEnum x)::HTYPE_INT)

_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|

Re: Add instance Storable Ordering

Vanessa McHale-2

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 Ordering where
   sizeOf _          = sizeOf (undefined::HTYPE_INT)
   alignment _       = alignment (undefined::HTYPE_INT)
   peekElemOff p i   = liftM (`compare` (1::HTYPE_INT)) $ peekElemOff (castPtr p) i
   pokeElemOff p i x = pokeElemOff (castPtr p) i (fromIntegral (fromEnum x)::HTYPE_INT)

_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

signature.asc (673 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Add instance Storable Ordering

Haskell - Libraries mailing list
In reply to this post by Dannyu NDos
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 <[hidden email]>:
I don't know exact sementics of Storable, but it's addable anyway.

instance Storable Ordering where
   sizeOf _          = sizeOf (undefined::HTYPE_INT)
   alignment _       = alignment (undefined::HTYPE_INT)
   peekElemOff p i   = liftM (`compare` (1::HTYPE_INT)) $ peekElemOff (castPtr p) i
   pokeElemOff p i x = pokeElemOff (castPtr p) i (fromIntegral (fromEnum x)::HTYPE_INT)
_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|

Re: Add instance Storable Ordering

Sven Panne-2
Am Di., 8. Okt. 2019 um 16:57 Uhr schrieb Simon Jakobi via Libraries <[hidden email]>:
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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|

Re: Add instance Storable Ordering

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 <[hidden email]> wrote:
Am Di., 8. Okt. 2019 um 16:57 Uhr schrieb Simon Jakobi via Libraries <[hidden email]>:
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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|

Re: Add instance Storable Ordering

Sylvain Henry-2
In reply to this post by Sven Panne-2

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 <[hidden email]>:
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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|

Re: Add instance Storable Ordering

David Feuer
In reply to this post by David Feuer
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 <[hidden email]> 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 <[hidden email]> wrote:
Am Di., 8. Okt. 2019 um 16:57 Uhr schrieb Simon Jakobi via Libraries <[hidden email]>:
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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|

Re: Add instance Storable Ordering

Henning Thielemann
In reply to this post by David Feuer

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.hs

But I also let the user choose the underlying machine type.
_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|

Re: Add instance Storable Ordering

David Feuer
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 <[hidden email]> 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.hs

But I also let the user choose the underlying machine type.

_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries