generic implementation of Bits.toIntegralSized

Henning Thielemann lemming at henning-thielemann.de
Mon Jun 22 09:23:26 UTC 2020


Documentation of toIntegralSized gives following generic implementation:

toIntegral :: (Integral a, Integral b) => a -> Maybe b
toIntegral x
   | toInteger x == y = Just (fromInteger y)
   | otherwise        = Nothing
   where
     y = toInteger x


I expect that (toInteger x == toInteger x) is always true because of 
referential transparency, right? I assume the intent was to compare the 
Integer representations of the parameter and the result. Maybe it should 
be this way:

toIntegral :: (Integral a, Integral b) => a -> Maybe b
toIntegral x
   | toInteger x == toInteger y  = Just y
   | otherwise                   = Nothing
   where
     y = fromInteger $ toInteger x


More information about the Libraries mailing list