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.
+1. Also: it’s be nice if extraneous LANGUAGE pragmas were identified.
Agreed, although that sounds much more difficult.
hlint does this, at least for some pragmas.
One problem with this is that constraints are not always added because of the implementation. Sometimes they are added just to add extra constraints to aid in type-safety. Take the following function from my safer-file-handles package for example:
hGetLine ∷ (FileHandle handle, pr `AncestorRegion` cr, MonadIO cr, ReadModes ioMode) ⇒ handle ioMode pr → cr String
Here the FileHandle and MonadIO constraints are needed by the implementation. However, AncestorRegion and ReadModes are only added to constrain the function.
Ah, very good point. Still, I imagine it would be useful for “most” modules — although it would be interesting to try to get some hard data to back that up. I’m imagining it would not be on by default — you’d have to enable it explicitly or with -Wall — and of course you could also explicitly turn it off even if you’re using -Wall.