[Haskell-cafe] Trying to write 'safeFromInteger'

Max Rabkin max.rabkin at gmail.com
Tue Apr 7 17:34:27 EDT 2009


On Tue, Apr 7, 2009 at 11:27 PM, Kannan Goundan <kannan at cakoose.com> wrote:
> 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:
>
>  Test.hs:3:19:
>    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.

That is not what it thinks. Ambiguous is not the same as conflicting
(you could call them opposites: ambiguous means underconstrained,
conflicting means overconstrained).

The confusing thing here is that the `a' GHC is talking about is not
the `a' in your code.

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).

--Max


More information about the Haskell-Cafe mailing list