On 08-Sep-2000, Jan Carlson <[EMAIL PROTECTED]> wrote:
> I'm intrigued by the following Haskell behaviour:
> 
> The type of (+) is
> 
> (+) :: (Num a) => a -> a -> a
> 
> Now, if I define
> 
> p = (+)
> 
> the type of p is inferred to be
> 
> p :: Integer -> Integer -> Integer
> 
> How come?
> 
> I guess the answer can be found somewhere in the Haskell report, but I'd
> really appreciate just a rough description of the issue.

The infamous monomorphism restriction strikes again!

Because of the syntax used to define `p', it falls foul of the monomorphism
rule (4.5.5 in the Haskell report).  This requires that its type be
monomorphic.  Then the defaulting rules (4.3.4) come in to play, and
these mean that the type variable `a' gets the value `Integer', since
that is the first type in default default list (Integer, Double)
which satifies the constraint in question, namely `Num a'.

If you define `p' as a syntactic function, e.g.

        p x y = x + y
or

        p x = (+) x

rather than via

        p = (+)

then the monomorphism restriction does not apply, and so the type inferred
for `p' will be the correct polymorphic type `Num a => a -> a -> a'.

-- 
Fergus Henderson <[EMAIL PROTECTED]>  |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>  |  of excellence is a lethal habit"
PGP: finger [EMAIL PROTECTED]        |     -- the last words of T. S. Garp.

Reply via email to