[Haskell-cafe] Re: Trying to write 'safeFromInteger'

Kannan Goundan kannan at cakoose.com
Tue Apr 7 17:42:13 EDT 2009

Max Rabkin <max.rabkin <at> gmail.com> writes:

> The problem with your code is that the type of maxBound is
> unspecified. You need (maxBound `asTypeOf` i), or enable
> ScopedTypeVariables and use (maxBound :: a) (I think).

I tried doing the (maxBound :: a) thing, but got another confusing error:

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

# ghci -XScopedTypeVariables Test.hs

    Could not deduce (Bounded a1) from the context ()
      arising from a use of `maxBound' at TestIntegerBounds.hs:3:20-27
    Possible fix:
      add (Bounded a1) to the context of an expression type signature
    In the first argument of `toInteger', namely `(maxBound :: a)'
    In the second argument of `(>)', namely
        `(toInteger (maxBound :: a))'
    In the expression: i > (toInteger (maxBound :: a))

More information about the Haskell-Cafe mailing list