Skip to content

Adding explicit type signature always produces warning with -XInstanceSigs and -Wredundant-constraints

Summary

GHC displays a warning when a typeclass has constraints which are not used in the instance declaration but when the type signature is written explicitly.

Consider the following code:

{-# LANGUAGE InstanceSigs #-}
{-# OPTIONS_GHC -Wredundant-constraints #-}

module InstanceSigs where

class Fold f where
    fold :: Monoid m => f m -> m

newtype Identity a = Identity a

instance Fold Identity where
    fold :: Identity a -> a
    fold (Identity a) = a

When the code above is compiled, I see the following warning:

    • Redundant constraint: Monoid m
    • In the type signature for:
           fold :: forall m. Monoid m => Identity m -> m
      When checking that instance signature for ‘fold’
        is more general than its signature in the class
        Instance sig: forall a. Identity a -> a
           Class sig: forall m. Monoid m => Identity m -> m
      In the instance declaration for ‘Fold Identity’
   |
12 |     fold :: Identity a -> a 
   |             ^^^^^^^^^^^^^^^

If I add the Monoid a constraint in the instance declaration, I still see the warning:

    • Redundant constraint: Monoid a
    • In the type signature for:
           fold :: forall a. Monoid a => Identity a -> a
      In the instance declaration for ‘Fold Identity’
   |
12 |     fold :: Monoid a => Identity a -> a
   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^

So there's no way to have both: explicit type signature using the -XInstanceSigs language extension and -Wredundant-constraints warning enabled. The only way not to see the warning is to not write type signature at all. Unfortunately, I don't like this solution, since I want to have explicit type signatures in the instance declarations for better code quality.

Steps to reproduce

Compile code snippets from above and see the warning.

Expected behavior

No warnings in the output.

Environment

  • GHC version used: 8.10.1

Optional:

  • Operating System: Ubuntu-18.04
  • System Architecture: x86_64
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information