[Haskell-beginners] Type inference question

Daniel Fischer daniel.is.fischer at web.de
Sun Mar 29 12:24:04 EDT 2009


Am Sonntag 29 März 2009 18:14:00 schrieb Zachary Turner:
> Given the following two function definitions:
>
> let func4 l = map (\y -> y+2) (filter (\z -> z `elem` [1..10]) (5:l))
> let func4_pf = map (+2) . filter (`elem` [1..10]) . (5 :)
>
> which are equivalent, why does the first one have a type of
>
> func4 :: (Num a, Enum a) => [a] -> [a]
>
> while the second one has a type of
>
> func4_pf :: [Integer] -> [Integer]
>
> Shouldn't the types be the same?

It's the dreaded monomorphism restriction.
Since the second is defined without arguments, it looks like a constant 
and to avoid recomputation, it must have a monomorphic type, except a 
type signature is given. That monomorphic type is defaulted to [Integer].

If you start ghci with the flag -fno-monomorphism-restriction, it will infer 
the same general type for the second function, too.


More information about the Beginners mailing list