Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does ghci choose names for type variables? [duplicate]

I made this function:

compose []     = id
compose (x:xs) = x  . (compose xs)

And when I asked for the type:

:t compose
compose :: [b -> b] -> b -> b

Why does it give compose :: [b -> b] -> b -> b and not compose :: [a -> a] -> a -> a if a comes first in alphabetic order?

Saying that, I will add this other example:

badImplementationOfCompose []     = id
badImplementationOfCompose (x:xs) =  (badImplementationOfCompose xs)

:t badImplementationOfCompose
badImplementationOfCompose :: [a1] -> a2 -> a2

It might give some hints...

like image 797
A Monad is a Monoid Avatar asked Oct 26 '25 15:10

A Monad is a Monoid


1 Answers

I would guess it has to do with the type of . which is

(.) :: (b -> c) -> (a -> b) -> a -> c

and it just starts replacing from there when inferring the type.

a has to be equal to b and b has to be equal to c. So in short my guess is that it tries to keep type variable naming in line with the type variable names in used sub-functions if possible.

like image 105
Christof Schramm Avatar answered Oct 29 '25 19:10

Christof Schramm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!