Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The type of (.) map const

Tags:

types

haskell

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?

like image 718
studentofcs Avatar asked May 12 '19 23:05

studentofcs


2 Answers

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.

like image 96
Joseph Sible-Reinstate Monica Avatar answered Nov 10 '22 09:11

Joseph Sible-Reinstate Monica


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]
like image 26
Thomas M. DuBuisson Avatar answered Nov 10 '22 08:11

Thomas M. DuBuisson