0
votes

I am trying to find the type of the following Haskell expression:

map (\x -> x + 1)

Since the map function is partially applied, it will return another function.

In GHCI, the type of the function that is returned is:

let res = map (\x -> x + 1)
:t res
map (\x -> x + 1) :: Num b => [b] -> [b]

Would the type of the above expression be the type of the function that is returned? Any insights are appreciated.

1
Yep. Yes. Uh huh. For sure. - luqui

1 Answers

2
votes

Yes, that is the type of the function which is returned.

But how does GHC figure this out? Well, let's look at the type of just plain map:

map :: (a -> b) -> [a] -> [b]

And now let's look at the type of \x -> x + 1:

(\x -> x + 1) :: Num n => n -> n

(In case you don't know yet, this means that it converts n to n, where n can be any type which is an instance of Num i.e. n is any number type.)

So matching up the types, we get:

map           ::         (a -> b) -> [a] -> [b]
(\x -> x + 1) :: Num n => n -> n

So:

map (\x -> x + 1) :: Num n => [n] -> [n]

Which is the same as what GHCi reports.