[Haskell-beginners] Type ambiguity - Int vs. Integer

Thomas haskell at phirho.com
Mon May 17 12:44:11 EDT 2010


Hello!

I was very surprised to see that Int and Integer seem to differ with 
respect to ambiguity. And I have no idea what is going on.

This works as intended:
data AW = AI Integer deriving (Show, Eq, Ord)

class AWC a where
     toAW :: a -> AW
     fromAW :: AW -> a

instance AWC Integer where
     toAW 		= AI
     fromAW (AI v) 	= v

 > toAW 5
5

However, the following gives an error:
data AW = AI Int deriving (Show, Eq, Ord)

class AWC a where
     toAW :: a -> AW
     fromAW :: AW -> a

instance AWC Int where
     toAW 		= AI
     fromAW (AI v) 	= v

 > toAW 5
Ambiguous type variable 't' in the constraints:
   'Num t' arising from the literal '5'
   'AWC t' arising from the use of 'toAW'

Ok, I can fix this easily with
 > toAW (5::Int)

But I still don't understand what's going on?
Why does one type work and the other does not?
Is there a way around this without type annotations?

Any hint would be greatly appreciated!
Thanks in advance,
Thomas




More information about the Beginners mailing list