[Haskell-beginners] type inference question

I. J. Kennedy jack at realmode.com
Sun Aug 23 14:12:16 EDT 2009

Given a sort function that works on numbers, you can sort
in reverse order by first negating the list, then sorting, then
negating again:

 Prelude Data.List> let revsort = (map negate) . sort . (map negate)

The function sort takes any list of Ord; negate works on any Num.

 Prelude Data.List> :t negate
 negate :: (Num a) => a -> a
 Prelude Data.List> :t sort
 sort :: (Ord a) => [a] -> [a]

I'd therefore expect my revsort function to work on any type that is
both an Ord and a Num.  However:

Prelude Data.List> :t revsort
revsort :: [Integer] -> [Integer]

I was expecting something like
 revsort :: (Ord a, Num a) => [a] -> [a]

Question: Why did the GHCI's type inference mechanism jump to
the conclusion that revsort should only work with Integer lists?

More information about the Beginners mailing list