Question

Why is type inferred differently when the anonymous function is bound to a name?

Prelude> :type (+)
(+) :: Num a => a -> a -> a

Prelude> let bar (x,y) = x+y
Prelude> :type bar
bar :: Num a => (a, a) -> a

Prelude> :type \(x,y)->x+y
\(x,y)->x+y :: Num a => (a, a) -> a

Prelude> let foo =  \(x,y)->x+y
Prelude> :type foo
foo :: (Integer, Integer) -> Integer
Était-ce utile?

La solution

It is a type defaulting in GHCi for polymorphic types.

These rules allow to use gchi as a "user friendly" "calculator" and don't use signatures.

But sure, these rules not only help, but hinder sometimes.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top