Search code examples
haskellghci

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


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]].


Solution

  • 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.