Haskell type variables
Monday, 1st June, 2009
-- file:ch03/MySecond.hs -- edited excerpt -- safeSecond :: [a] -> Maybe a safeSecond  = Nothing safeSecond xs = if null (tail xs) then Nothing else Just (head (tail xs)) -- tidySecond :: [a] -> Maybe a tidySecond (_:x:_) = Just x tidySecond _ = Nothing
In December, Helge Stenström made an interesting comment about the above code:
If the type declarations using :: are omitted from safeSecond and tidySecond, they have visually different types, at least in ghci.safeSecond :: [a] -> Maybe a tidySecond :: [t] -> Maybe t
Why is it so?
In Hugs both functions have the same type
[a] -> Maybe a.
The ghci effect is reproduced in simpler code:
safe xs = head xs tidy (x:_) = x
*Main> :type safe safe :: [a] -> a *Main> :type tidy tidy :: [t] -> t
On Sun, May 31, 2009 at 05:42:44PM +0200, Daniel Fischer wrote:
> But that has nothing to do with the phenomenon, in the inferred type signatures of
> safeSecond and tidySecond, the ‘a’ resp. ‘t’ are both type variables of kind *.
> The only difference is that in one case the name supply delivered ‘a’ and in the other it
> delivered ‘t’.
I think one source of difference is that ghci tries to maintain type
variable names from declared type signatures. So perhaps one of the
library functions used to define ‘safeSecond’ has an explicitly
declared type signature that mentions ‘a’, and a library function used
to defined ‘tidySecond’ has one that mentions ‘t’.