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.


About Brent

Associate Professor of Computer Science at Hendrix College. Functional programmer, mathematician, teacher, pianist, follower of Jesus.
This entry was posted in haskell and tagged , , , . Bookmark the permalink.

5 Responses to Wanted GHC feature: warn about unused constraints

  1. +1. Also: it’s be nice if extraneous LANGUAGE pragmas were identified.

  2. Bas van Dijk says:

    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.

    • Brent says:

      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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.