Skip to content

Wrong code given for documentation version of toIntegralSized

Summary

Location of documentation issue: https://hackage.haskell.org/package/base-4.15.0.0/docs/Data-Bits.html#v:toIntegralSized

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

The naive toIntegral given is wrong. The condition simplifies to toInteger x == toInteger x so the function never fails.

Proposed improvements or changes

The documentation for the original version in int-cast gives a function closer to the semantics of toIntegralSized:

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

Environment

  • GHC version used (if appropriate): base-4.15 (GHC 9.0.1)
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information