Type inference question

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Type inference question

Zachary Turner-2
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?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20090329/7053aca1/attachment-0001.htm
Reply | Threaded
Open this post in threaded view
|

Type inference question

Daniel Fischer-4
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.