
From: Edward Kmett
fromIntegralMaybe :: (Integral a, Num b) => a -> Maybe b fromIntegralMaybe = fromIntegerMaybe . toInteger
I’m late to the party, so might be missing something. Did someone already proposed using http://hackage.haskell.org/package/base-4.14.0.0/docs/Data-Bits.html#v:toInt..., which has a very similar signature? toIntegralSized :: (Integral a, Integral b, Bits a, Bits b) => a -> Maybe b No need for O(n^2) instances and it is already in `base`. Best regards, Andrew

This doesn't work when the target type is, e.g., Double (Num, but not
Integral), but thank you for the tip, I've already used it in my code
to fail when wrapping, etc., would occur for integral types.
On Thu, Aug 13, 2020 at 3:10 PM Andrew Lelechenko
From: Edward Kmett
fromIntegralMaybe :: (Integral a, Num b) => a -> Maybe b fromIntegralMaybe = fromIntegerMaybe . toInteger
I’m late to the party, so might be missing something. Did someone already proposed using http://hackage.haskell.org/package/base-4.14.0.0/docs/Data-Bits.html#v:toInt..., which has a very similar signature?
toIntegralSized :: (Integral a, Integral b, Bits a, Bits b) => a -> Maybe b
No need for O(n^2) instances and it is already in `base`.
Best regards, Andrew _______________________________________________ Libraries mailing list Libraries@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

Fwiw, this was proposed back then in 2014, you can find the libraries thread
and the patch over at
- https://mail.haskell.org/pipermail/libraries/2014-November/024383.html
- https://gitlab.haskell.org/ghc/ghc/-/issues/9816
respectively.
It might also be worth pointing out the function added to `base`
originated from my package `int-cast`
- https://hackage.haskell.org/package/int-cast-0.2.0.0/docs/Data-IntCast.html
which provides the means to have compile-time verified "safe" (and also
a slightly weaker lossless "iso"morphic) integer conversions without
requiring O(n^2) instances.
I typically use `int-cast` for critical code where I need more assurance
and want to prove that my integer conversions are safe.
-- hvr
Mikolaj Konarski
This doesn't work when the target type is, e.g., Double (Num, but not Integral), but thank you for the tip, I've already used it in my code to fail when wrapping, etc., would occur for integral types.
On Thu, Aug 13, 2020 at 3:10 PM Andrew Lelechenko
wrote: From: Edward Kmett
fromIntegralMaybe :: (Integral a, Num b) => a -> Maybe b fromIntegralMaybe = fromIntegerMaybe . toInteger
I’m late to the party, so might be missing something. Did someone already proposed using http://hackage.haskell.org/package/base-4.14.0.0/docs/Data-Bits.html#v:toInt..., which has a very similar signature?
toIntegralSized :: (Integral a, Integral b, Bits a, Bits b) => a -> Maybe b
No need for O(n^2) instances and it is already in `base`.
Best regards, Andrew _______________________________________________ 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

Am 13.08.20 um 17:48 schrieb Herbert Valerio Riedel:
It might also be worth pointing out the function added to `base` originated from my package `int-cast`
- https://hackage.haskell.org/package/int-cast-0.2.0.0/docs/Data-IntCast.html
which provides the means to have compile-time verified "safe" (and also a slightly weaker lossless "iso"morphic) integer conversions without requiring O(n^2) instances.
This is pretty cool. Thanks for sharing. Cheers Ben

Herbert, now I'm a fan of your package. However, I'm getting this when
trying to intCast Int64 to Rational:
engine-src/Game/LambdaHack/Common/Time.hs:265:19: error: …
• Couldn't match type ‘int-cast-0.2.0.0:Data.IntCast.IsIntBaseSubType
('int-cast-0.2.0.0:Data.IntCast.FixedIntTag 64)
(int-cast-0.2.0.0:Data.IntCast.IntBaseType Rational)’
with ‘'True’
arising from a use of ‘intCast’
• In the expression: intCast :: Int64 -> Rational
In the first argument of ‘(*)’, namely
‘(intCast :: Int64 -> Rational) v’
In the second argument of ‘($)’, namely
‘(intCast :: Int64 -> Rational) v * s’
On Thu, Aug 13, 2020 at 6:54 PM Ben Franksen
Am 13.08.20 um 17:48 schrieb Herbert Valerio Riedel:
It might also be worth pointing out the function added to `base` originated from my package `int-cast`
- https://hackage.haskell.org/package/int-cast-0.2.0.0/docs/Data-IntCast.html
which provides the means to have compile-time verified "safe" (and also a slightly weaker lossless "iso"morphic) integer conversions without requiring O(n^2) instances.
This is pretty cool. Thanks for sharing.
Cheers Ben
_______________________________________________ Libraries mailing list Libraries@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
participants (4)
-
Andrew Lelechenko
-
Ben Franksen
-
Herbert Valerio Riedel
-
Mikolaj Konarski