
On Thu, Dec 17, 2009 at 3:45 PM, Daniel Fischer
Am Donnerstag 17 Dezember 2009 23:56:13 schrieb Mike Erickson:
I was trying to convert a function,
f1 x l = map (\y -> y*x) l
to so-called point-free style, but in reducing it I found two functions that seemed to work the same, until I discovered they have different types, and aren't equivalent:
f1a x = map (*x) -- has type (Num a) => a -> [a] -> [a]
f1b = \x -> map (*x) -- has type Integer -> [Integer] -> [Integer]
Can someone help me understand why the latter has a more restrictive type?
Indeed it's the monomorphism restriction. [...]
This was very helpful. I think I understand, although it's still digesting.
In short, if you declare a function point-free and the inferred type involves type classes, it cannot be polymorphic unless you give a type signature. Without a type signature, the implementation tries to determine a monomorphic type from the inferred polymorphic type by the defaulting rules (http://haskell.org/onlinereport/decls.html#sect4.3.4). If that succeeds, this type is given to the function, otherwise it's a type error and the module doesn't compile.
Shortly after posting (of course) I discovered the online report and 4.5.4 and 4.5.5 were helpful as well. Thanks to all that responded, Mike