Skip to content

`-Wredundant-constraints` false positives in class instance contexts

Summary

In the following example:

{-# LANGUAGE UndecidableInstances, FlexibleInstances #-}

class Functor f => C f where c :: f Int

instance (Functor f, Applicative f) => C f where c = pure 42

Compiling in GHC 9.2.3 using -Wredundant-constraints produces the following warning:

test.hs:5:10: warning: [-Wredundant-constraints]
    • Redundant constraint: Functor f
    • In the instance declaration for ‘C f’
  |
5 | instance (Functor f, Applicative f) => C f where c = pure 42
  |          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

While the constraint intuitively seems redundant, in the case of instance contexts it is in fact necessary, by design, as described here

Expected behavior

As in previous GHC versions, like 9.0.2, no warning should be given in this case.

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information