Wanted GHC feature: warn about unused constraints
Consider the following function:
foo :: (Show a, Ord a) => a -> a -> String
foo x y = show x ++ show y
Currently (as of GHC 7.2.1) when compiling this code with -Wall
, no warnings are generated. But I’d really love to get a warning like
Foo.hs:1:1: Warning: Unused constraint: Ord a
I see no theoretical impediments here. At the level of fully desugared and elaborated GHC core, it is no harder to tell which constraints are unused than which arguments are unused, because constraints are arguments.
One possible objection is that there is some inherent ambiguity here. For example, consider:
bar :: (Eq a, Ord a) => a -> a -> String
bar x y | x == y = "yay"
| otherwise = "boo"
When elaborating bar
, GHC has a free choice of where to get the needed (==)
method: from the Eq
constraint or from the Eq
superclass of the Ord
constraint. So we might get a warning about Eq
being redundant or about Ord
being redundant, depending on which one it decides to use. But I don’t see this as a big problem.
I think this could make for a nice project for someone wanting to dig into hacking on GHC. Perhaps I’ll do it myself at some point if no one else takes it on.