PROPOSAL: toBoundedIntegral: an adaptation of fromIntegral that respects bounds

Sean Leather sean.leather at gmail.com
Thu Nov 20 21:46:36 UTC 2014


On Wed, Nov 12, 2014 at 9:14 AM, Sean Leather wrote:

> Inspired by conversations recent [1] and not-so-recent [2] and by my own
> past wish for this, I propose adding the following function to base:
>
> toBoundedIntegral :: (Integral a, Integral b, Bounded b) => a -> Maybe b
>

[...]

Discussion period: ~10 days if there is a strong desire to get it in GHC
> 7.10. (I would certainly like to see it get in.)


Almost over. Here are some follow-ups on Trac and Phab.

GHC Trac: https://ghc.haskell.org/trac/ghc/ticket/9816
Phab Diff: https://phabricator.haskell.org/D512

On Mon, Nov 17, 2014 at 11:10 AM, Sean Leather wrote:

> So is there any reason not to settle on 'toIntegralBounded' and
> 'toIntegralSized'?
>

I decided to drop 'toIntegralBounded' go with only 'toIntegralSized' in my
proposal. I realized that the former was not handling Int<->Word-like cases
optimally and would require special attention to them while the latter
provides them for free.

Herbert: It was nice that 'intCastMaybe' was an easy copy-paste into
another module. I did a bit of rewriting the comments and code formatting.
Hope you don't mind, or bash me on Phab if you prefer. ;) Also, it would be
nice to have your tests in the library. Any thoughts on how to do that? I
haven't looked into it, yet.

about the RULEs... do we really need them for `toBoundedIntegral`? If
>> you make `toBoundedIntegral` INLINEABLE, shouldn't you be able to
>> inherit the ones from 'fromIntegral' (which is what `intCastMaybe`
>> exploits)?
>>
>
> I didn't try that. I'll give it a shot.
>

I did try it, and I'm not sure how to make that work. 'toBoundedIntegral'
splits the 'toInteger' and 'fromInteger' and puts the bounds check in the
middle, so 'fromIntegral' is not used. If it were used, then I believe
there would be redundant 'toInteger' conversions for types that weren't
covered by RULEs.


> > Where should the functions go? intCastMaybe/toSizedIntegral could go in
>> > Data.Bits. What about toBoundedIntegral?
>>
>> There's also
>>
>>   http://hackage.haskell.org/package/base-4.7.0.1/docs/Numeric.html
>>
>> as a candidate
>>
>
> Works for me. We could rename the section “Miscellaneous” to “Conversion”
> and put both functions there. Also, we should probably revise the module
> description since there is already a lot more than “functions for reading
> and showing RealFloat-like kind of values.”
>

I decided on Data.Bits since it seemed to be the most obvious place for
'toIntegralSized' alone. Also, I think there might be an import cycle if I
put it into Numeric. But if others have a preference and want to make it
work somewhere else, that's fine with me.

Regards,
Sean
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/libraries/attachments/20141120/4d82c94d/attachment.html>


More information about the Libraries mailing list