No, no, no! GHC is right and Hugs is wrong. This is a well known bugNo! That's where an error message should be generated. The price we pay is that some programs won't pass the type checker. We shouldn't go out of our way to assign a type to a program when it's ambiguous.
in Hugs. It doesn't have anything to do with 4.5.1 - it goes like
this:module Foo where
x = 13
y = x :: Bool
instance Num Bool
Anyway, we typecheck y and get the SAME typevariable that defines x.
That gets unified with Bool. Aha! The type of x is now Bool. That's
monomorphism and everyone is happy (except Jeff!). But that's the way
it's supposed to work - the uses of a monomorphicly typed value affect
the type of the value. Here, the use of x sets its type to Bool.
This happened because of the monomorphism restriction, not due to
dependency analysis. Furthermore, this shouldn't be troubling: the
compiler has chosen a completely appropriate type for x. It's not the
most general, but that's the price you pay to avoid type ambiguity.
OK, this is fun, but I'm starting to feel like a lawyer, consulting
the Haskell law books. I cite: section 4.4.1, top of page 46:
If a variable f is defined without providing a corresponding type signature declaration, then each use of f outside its own declaration group (see Section 4.5) is treated as having the corresponding inferred, or principal type .Outside of its declaration group, an identifier is treated as having its principal type. It's nonsense to talk about a principal type that contains free type variables that may later be bound. I rest my case ;-)
--Jeff
P.S. I read thru all of sections 4 and 5 of the report. I could
find nothing to support GHC's version of the story The best
I could find was the following oblique reference in section 4.4.5:
Lastly, Rule 2 is required because there is no way to enforce monomorphic use of an exported binding, except by performing type inference on modules outside the current module. Exported variables are handled in the same way as non-exported ones even though their usage outside the module could theoreticly be used to determine monomorphic type.
