Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does `:type` sometimes show `a` and other times `t`?

Tags:

haskell

ghci

I have these two functions:

cleanUp a = Data.List.filter (/=[]) a

joinByPairs [] = []
joinByPairs (x:[]) = (x:[])
joinByPairs (x:y:xs) = (x ++ y) : joinByPairs xs

When I load them in ghci, and call :type on them, I get these results:

*Main> :type joinByPairs
joinByPairs :: [[a]] -> [[a]]
*Main> :type cleanUp
cleanUp :: Eq t => [[t]] -> [[t]]

What is the logic of it showing a vs. t? I don't think it's because of the Eq t part, since I have other functions that show something like otherFunction :: Eq a => [[a]] -> [[a]].

like image 809
cambraca Avatar asked Dec 09 '15 15:12

cambraca


1 Answers

It's because of the way type variable names are chosen. Brand-new variables get t. Type variables that are named in a type signature keep the name from the type signature. When unifying type variables, GHC prefers to keep a name that came from an explicit type signature. If no subexpression had an explicit type signature, there's no names other than t to unify with.

like image 105
Carl Avatar answered Nov 15 '22 09:11

Carl