[Haskell] Re: Implicit type of numeric constants
Christian Sievers
sievers at math2.nat.tu-bs.de
Wed Sep 20 13:37:06 EDT 2006
Robert Stroud wrote:
> However, I still think there's a bit of an inconsistency here. I
> understand that if k had the type "Num a => a", then the expression
> "show k" would be ambiguous, but unless I write that expression,
> there's no ambiguity...
Actually, you can give k that type, and thanks to defaulting you
can then still use show k.
> So it seems to me that the type checker is being a bit too eager to
> prevent something that hasn't happened yet.
Here it is the monomorphism restriction (Haskell report section 4.5.5)
that enforces defaulting.
> In contrast, in the case where I write "let k = 2 ...", the type
> checker seems happy to resolve the polymorphic type within the
> context of the let expression, and does what I expect.
I hope my last mail explained this: defaulting for monomorphic types
happens quite late.
> So is the problem that the context is effectively unbounded when I
> load the definition from a file, and hence the type checker has to be
> very conservative about preventing the ambiguity?
A file is (essentially) a module. This defaulting happens
"when type inference for an entire module is complete"
(rule 2 of the above mentioned section), because we don't
want types to depend on arbitrary other modules - think of
seperate compilation.
> I'm afraid I'm also still not clear about why 2 doesn't default to
> 2::Integer in the same way that k defaults to k::Integer,
It does, that's why show 2 works.
Hope that helps
Christian Sievers
More information about the Haskell
mailing list