studentofcs
studentofcs

Reputation: 65

The type of (.) map const

I'm trying to determine the type of (.) map const.

The type of (.) map gives me (a1 -> a2 -> b) -> a1 -> [a2] -> [b]

The type of const is a -> b -> a

So when I'm trying to do (.) map const, shouldn't the (a1 -> a2 -> b) of (.) map unify with (a -> b -> a) from const?

Thus, a1 unifies with a, a2 unifies with b, b unifies with a. I'm left with a1 -> [a2] -> b With the unification it should leave me with a -> [b] -> [a]

However, the correct answer is b -> [a] -> [b]

Can someone explain how and why?

Upvotes: 5

Views: 268

Answers (2)

Thomas M. DuBuisson
Thomas M. DuBuisson

Reputation: 64740

Answering your comment on @JosephSible's answer, it appears GHCi unification is favoring the type variables from map. This is mere order of operations and arbitrary:

map' :: (mapA -> mapB) -> [mapA] -> [mapB]
map' = map

dot :: (dotB -> dotC) -> (dotA -> dotB) -> dotA -> dotC
dot = (.)

const' :: constA -> constB -> constA
const' = const

And

*Main> :t dot map' const'
dot map' const' :: mapB -> [mapA] -> [mapB]

Upvotes: 7

a -> [b] -> [a] and b -> [a] -> [b] are the same type (and so is, for example, foo -> [bar] -> [foo]). The names of type variables carry no meaning, so as long as the same ones are in the same places, the labels they have don't matter.

Upvotes: 12

Related Questions