[Haskell-beginners] Type ambiguity - Int vs. Integer
Daniel Fischer
daniel.is.fischer at web.de
Mon May 17 13:45:00 EDT 2010
On Monday 17 May 2010 18:44:11, Thomas wrote:
> 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?
The literal 5 (which is implicitly 'fromInteger 5'), can have any type
which is a member of Num, so in
toAW 5
, the type of 5 is ambiguous. For convenience, in some cases, a Haskell
implementation tries to resolve such ambiguities by defaulting
(http://haskell.org/onlinereport/decls.html#sect4.3.4).
The default-default is (Integer, Double), that means, if an ambiguity can
be resolved by choosing the type Integer, that is chosen, otherwise Double
is tried. You can give different defaults in a module, so e.g. a default
declaration
default (Int, Integer, Rational, Double)
would try Int first.
However, according to the defaulting rules in the report, no defaulting
would take place here, since there's a class (AWC) involved which isn't
defined in the standard libraries.
But ghci uses relaxed defaulting rules (to resolve more cases, otherwise
'ambiguous type' errors would be much more frequent), so it tries to
resolve the ambiguity although AWC isn't defined in the standard libraries.
In the first case, choosing Integer for the type works, so it takes Integer
for the type of 5 and all's well. In the second case, Integer doesn't work,
nor does Double, so it can't resolve the ambiguity.
Unfortunately, when ghci loads a module, it ignores default declarations,
so you couldn't get it to work by adding default declaration including Int.
> Why does one type work and the other does not?
> Is there a way around this without type annotations?
No, not at the ghci prompt. In normal code, usually the type of such a
value could be determined from other things (when you use it in a function,
there's much more contextual information than when you enter such an
expression at the prompt).
For uses inside the module, you can have some success with
{-# LANGUAGE ExtendedDefaultRules #-}
module AWC where
default (Int, Integer, Double)
data AW = AI Int deriving (...)
...
if not all uses allow the type to be determined from the context.
>
> Any hint would be greatly appreciated!
> Thanks in advance,
> Thomas
More information about the Beginners
mailing list