Proposal: add `Proxy`fied versions of `bitSizeMaybe` and `finiteBitSize`

I propose to add the following methods to `Bits` and `FiniteBits` classes: class Bits a where ... bitSizeMaybe' :: Proxy a -> Maybe Int class FiniteBits b where ... finiteBitSize' :: Proxy b -> Int Rationale: working with an arbitrary `Bits a => a`, one may not have a value of type `a` as argument to `bitSizeMaybe` or `finiteBitSize`, and writing `undefined` makes me feel dirty.

+1, but we should also do this for sizeOf and alignment in Foreign.Storable.
On Dec 13, 2017 10:53 PM, "M Farkas-Dyck"
I propose to add the following methods to `Bits` and `FiniteBits` classes:
class Bits a where ...
bitSizeMaybe' :: Proxy a -> Maybe Int
class FiniteBits b where ...
finiteBitSize' :: Proxy b -> Int
Rationale: working with an arbitrary `Bits a => a`, one may not have a value of type `a` as argument to `bitSizeMaybe` or `finiteBitSize`, and writing `undefined` makes me feel dirty. _______________________________________________ Libraries mailing list Libraries@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

I honestly think we should make it a Const Int a, instead of a
function, so that it's a number in the typeclass dictionary and not a
function.
On Wed, Dec 13, 2017 at 10:41 PM, M Farkas-Dyck
On 2017-12-13 08:19 PM, David Feuer wrote:
+1, but we should also do this for sizeOf and alignment in Foreign.Storable.
Yes, good idea
_______________________________________________ Libraries mailing list Libraries@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

The Const type does not see much direct use in other common Haskell
packages. (I only ever encounter it through lens.) So while that might be
an elegant design, I think it will make the API more confusing for
newcomers.
On Dec 13, 2017 9:38 PM, "Zemyla"
I honestly think we should make it a Const Int a, instead of a function, so that it's a number in the typeclass dictionary and not a function.
On Wed, Dec 13, 2017 at 10:41 PM, M Farkas-Dyck
wrote: On 2017-12-13 08:19 PM, David Feuer wrote:
+1, but we should also do this for sizeOf and alignment in Foreign.Storable.
Yes, good idea
_______________________________________________ Libraries mailing list Libraries@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Libraries mailing list Libraries@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

On 2017-12-13 09:37 PM, Zemyla wrote:
I honestly think we should make it a Const Int a, instead of a function, so that it's a number in the typeclass dictionary and not a function.
I like that idea! On 2017-12-13 09:41 PM, Tikhon Jelvis wrote:
The Const type does not see much direct use in other common Haskell packages. (I only ever encounter it through lens.) So while that might be an elegant design, I think it will make the API more confusing for newcomers.
I doubt whether it's much more confusing than `Proxy`, and would be more efficient in cases where the typeclass dictionary can't be inline. Anyhow, if we began using it in base it would thus gain exposure; it's self-fulfilling either way.

Alternatively, we could have it be `Proxy# a -> Int`, which should have
identical performance characteristics to `Const Int a` but with the added
benefit that it's a little easier to understand how it's supposed to be
used.
On Thu, Dec 14, 2017 at 12:37 AM, Zemyla
I honestly think we should make it a Const Int a, instead of a function, so that it's a number in the typeclass dictionary and not a function.
On Wed, Dec 13, 2017 at 10:41 PM, M Farkas-Dyck
wrote: On 2017-12-13 08:19 PM, David Feuer wrote:
+1, but we should also do this for sizeOf and alignment in Foreign.Storable.
Yes, good idea
_______________________________________________ Libraries mailing list Libraries@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Libraries mailing list Libraries@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
-- -Andrew Thaddeus Martin

On Wed, 13 Dec 2017, David Feuer wrote:
+1, but we should also do this for sizeOf and alignment in Foreign.Storable.
If Storable would be intended for tuples, too, and there would be no padding, we could use special Monoids in Const, like so: class Storable a where sizeOf :: Const (Sum Int) a alignment :: Const (LCM Int) a instance (Storable a, Storable b) => Storable (a,b) where sizeOf = liftA2 (,) sizeOf sizeOf alignment = liftA2 (,) alignment alignment Taking alignment into account we could define data SizeAlign = SignAlign {sizeOf_, alignment_ :: Int} instance Monoid SizeAlign where mempty = SizeAlign 0 1 mappend x y = SizeAlign (sizeOf_ x + mod (- sizeOf_ x) (alignment_ y) + sizeOf_ y) (lcm (alignment_ x) (alignment_ y)) class Storable a where sizeAlign :: Const SizeAlign a instance (Storable a, Storable b) => Storable (a,b) where sizealign = liftA2 (,) sizeOf sizeOf However, SizeAlign.mappend this way is not valid because it violates associativity (e.g. sizes 1, 1, 2 with corresponding alignments). Even if it would be valid, it would still differ e.g. from Linux-x86 ABI, since structs are already padded to the next aligned size. At least for the alignments an LCM monoid would work.

On Wed, 13 Dec 2017, M Farkas-Dyck wrote:
I propose to add the following methods to `Bits` and `FiniteBits` classes:
class Bits a where ...
bitSizeMaybe' :: Proxy a -> Maybe Int
class FiniteBits b where ...
finiteBitSize' :: Proxy b -> Int
Alternative suggestion for names (I frequently overlook the primes): bitSizeMaybeConst :: Const (Maybe Int) a finiteBitSizeConst :: Const Int a sizeOfConst :: Const Int a alignmentConst :: Const Int a Then we must add mutual default implementations in order to preserve existing instances.

Now that we have visible type application, we could stop using these Proxy arguments, thus class Bits a where ... bitSizeMaybe' :: Maybe Int And you invoke it by saying bitSizeMaybe @ T rather than bitSizeMaybe (Proxy :: Proxy T) Looks like a straight win to me. Simon | -----Original Message----- | From: Libraries [mailto:libraries-bounces@haskell.org] On Behalf Of M | Farkas-Dyck | Sent: 14 December 2017 03:48 | To: libraries@haskell.org | Subject: Proposal: add `Proxy`fied versions of `bitSizeMaybe` and | `finiteBitSize` | | I propose to add the following methods to `Bits` and `FiniteBits` | classes: | | class Bits a where | ... | | bitSizeMaybe' :: Proxy a -> Maybe Int | | class FiniteBits b where | ... | | finiteBitSize' :: Proxy b -> Int | | Rationale: working with an arbitrary `Bits a => a`, one may not have a | value of type `a` as argument to `bitSizeMaybe` or `finiteBitSize`, | and writing `undefined` makes me feel dirty. | _______________________________________________ | Libraries mailing list | Libraries@haskell.org | https://na01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fmail.h | askell.org%2Fcgi- | bin%2Fmailman%2Flistinfo%2Flibraries&data=04%7C01%7Csimonpj%40microsof | t.com%7Cd88222263d04465efcf608d542a6473f%7C72f988bf86f141af91ab2d7cd01 | 1db47%7C1%7C0%7C636488204282827644%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4 | wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwifQ%3D%3D%7C- | 1&sdata=GUcUpHRarbB8uky4m%2Fve1l3ZbtPnDIndzPM9FG5Jj44%3D&reserved=0

On Thu, 14 Dec 2017, Simon Peyton Jones via Libraries wrote:
Now that we have visible type application, we could stop using these Proxy arguments, thus
class Bits a where ... bitSizeMaybe' :: Maybe Int
And you invoke it by saying bitSizeMaybe @ T rather than bitSizeMaybe (Proxy :: Proxy T)
Looks like a straight win to me.
I was not aware of this type extension, but I still prefer Haskell 98 / 2010.

This was my knee-jerk reaction as well, but interestingly, you only
actually need to turn on AllowAmbiguousTypes to compile the class. You can
actually _use_ the methods without the extension. This matters to me
because AllowAmbiguousTypes allows far too many errors through in user code.
This renders it a somewhat less bad option than I previously assumed and
leaves me somewhat ambivalent about whether we use the extension or not.
On Thu, Dec 14, 2017 at 3:11 PM, M Farkas-Dyck
On 2017-12-14 05:46 AM, Henning Thielemann wrote:
I still prefer Haskell 98 / 2010.
I agree. I would hesitate to introduce ambiguous types here.
_______________________________________________ Libraries mailing list Libraries@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
participants (8)
-
Andrew Martin
-
David Feuer
-
Edward Kmett
-
Henning Thielemann
-
M Farkas-Dyck
-
Simon Peyton Jones
-
Tikhon Jelvis
-
Zemyla