[Haskell-beginners] Is this MR? Type restriction confusion
byorgey at seas.upenn.edu
Thu Dec 17 22:41:41 EST 2009
On Thu, Dec 17, 2009 at 04:46:34PM -0800, Mike Erickson wrote:
> On Thu, Dec 17, 2009 at 3:45 PM, Daniel Fischer
> <daniel.is.fischer at web.de> wrote:
> > 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.
I should point out that the monomorphism restriction will probably be
removed from a future version of the language standard. I recommend
turning it off, especially in ghci (by putting ':set
-XNoMonomorphismRestriction' in your .ghci file)---it is rarely useful
and just confuses people.
More information about the Beginners