[Haskell-cafe] Trying to write 'safeFromInteger'

Kannan Goundan kannan at cakoose.com
Tue Apr 7 17:27:21 EDT 2009

Here's my code (in file "Test.hs")

  safeFromInteger :: (Num a, Integral a, Bounded a) => Integer -> Maybe a
  safeFromInteger i =
    if i > (toInteger maxBound)
      then Nothing
      else Just (fromInteger i)

Here's the error from GHCi 6.10.1:

    Ambiguous type variable `a' in the constraints:
      `Bounded a' arising from a use of `maxBound' at Test.hs:3:19-26
      `Integral a' arising from a use of `toInteger' at Test.hs:3:9-26
    Probable fix: add a type signature that fixes these type variable(s)

It's almost like GHC thinks that the type variable "a" can't be part of the
"Bounded" lass and the "Integeral" class.

I plan to use "safeFromInteger" for converting from Word8, Word16, Word32, etc.

More information about the Haskell-Cafe mailing list