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

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

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

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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|

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

David Feuer
+1, but we should also do this for sizeOf and alignment in Foreign.Storable.

On Dec 13, 2017 10:53 PM, "M Farkas-Dyck" <[hidden email]> 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

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
[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: Proposal: add `Proxy`fied versions of `bitSizeMaybe` and `finiteBitSize`

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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|

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

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 <[hidden email]> 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
> [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: Proposal: add `Proxy`fied versions of `bitSizeMaybe` and `finiteBitSize`

Tikhon Jelvis
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" <[hidden email]> 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.

On Wed, Dec 13, 2017 at 10:41 PM, M Farkas-Dyck <[hidden email]> 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
> [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

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

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

M Farkas-Dyck
In reply to this post by Zemyla
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.
_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|

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

Henning Thielemann
In reply to this post by David Feuer

On Wed, 13 Dec 2017, David Feuer wrote:

> +1, but we should also do this for sizeOf and alignment in Foreign.Storable.

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

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

Henning Thielemann
In reply to this post by Zemyla

On Wed, 13 Dec 2017, 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.

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

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

Henning Thielemann
In reply to this post by M Farkas-Dyck

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

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

Haskell - Libraries mailing list
In reply to this post by M Farkas-Dyck
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:[hidden email]] On Behalf Of M
|  Farkas-Dyck
|  Sent: 14 December 2017 03:48
|  To: [hidden email]
|  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
|  [hidden email]
|  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
_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|

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

Andrew Martin
In reply to this post by Zemyla
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 <[hidden email]> 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.

On Wed, Dec 13, 2017 at 10:41 PM, M Farkas-Dyck <[hidden email]> 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
> [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



--
-Andrew Thaddeus Martin

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

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

Ryan Scott
In reply to this post by M Farkas-Dyck
I don't have a particular opinion on this proposal, but I did want to
chime in to note that there are several other functions in base
besides biteSizeMaybe, finiteBitSize, sizeOf, and alignment that take
a dummy argument. The last time I checked, all of these functions also
fit the bill [1]:

* Data.Bits.isSigned :: Bits a => a -> Bool
* Data.Data.dataTypeOf :: Data a => a -> DataType
* GHC.Generics.datatypeName :: Datatype d => t d f a -> String
* GHC.Generics.moduleName :: Datatype d => t d f a -> String
* GHC.Generics.isNewtype :: Datatype d => t d f a -> Bool
* GHC.Generics.packageName :: Datatype d => t d f a -> String
* GHC.Generics.conName :: Constructor c => t c f a -> String
* GHC.Generics.conFixity :: Constructor c => t c f a -> Fixity
* GHC.Generics.conIsRecord :: Constructor c => t c f a -> Bool
* GHC.Generics.selName :: Selector s => t s f a -> String
* GHC.Generics.selSourceUnpackedness :: Selector s => t s f a ->
SourceUnpackedness
* GHC.Generics.selSourceStrictness :: Selector s => t s f a -> SourceStrictness
* GHC.Generics.selDecidedStrictness :: Selector s => t s f a ->
DecidedStrictness
* Prelude.floatRadix :: RealFloat => a -> Integer
* Prelude.floatDigits :: RealFloat => a -> (Int, Int)
* Text.Printf :: PrintfArg a => a -> ModifierParser

Ryan S.
-----
[1] This list was taken from
http://hackage.haskell.org/package/proxied-0.3/docs/Data-Proxied.html
_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|

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

Henning Thielemann
In reply to this post by Haskell - Libraries mailing list

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

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

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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|

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

Edward Kmett-2
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 <[hidden email]> wrote:
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
[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
|

Const versions of sizeOf and alignment (Was: add `Proxy`fied versions of `bitSizeMaybe` and `finiteBitSize`)

Henning Thielemann
In reply to this post by David Feuer

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