Redundant constraints [GHC-30606]

Flag: -Wredundant-constraints

This warning is emitted when a binding has a type signature which contains constraints that are unnecessary.

Examples

Redundant equality constraints

Error Message

Example.hs:5:6: warning: [-Wredundant-constraints] [GHC-30606]
    Redundant constraint: b ~ a
    In the type signature for:
         f :: forall a b. (a ~ b, b ~ a) => a -> b
  |
5 | f :: (a ~ b, b ~ a) => a -> b
  |      ^^^^^^^^^^^^^^

Explanation

This example declares f with the constraints (a ~ b, b ~ a) (a is equal to b and b is equal to a), but a ~ b already implies that b ~ a (in other words, ~ is a symmetric relation).

We fix this by removing the constraint which is marked as redundant in the error message, namely b ~ a.

Example.hs
Before
{-# OPTIONS_GHC -Wredundant-constraints #-}

module Example where

f :: (a ~ b, b ~ a) => a -> b
f x = x
After
{-# OPTIONS_GHC -Wredundant-constraints #-}

module Example where

f :: (a ~ b) => a -> b
f x = x
Redundant typeclass constraints

Error Message

Example.hs:5:6: warning: [-Wredundant-constraints] [GHC-30606]
    Redundant constraint: Eq a
    In the type signature for:
         f :: forall a. (Eq a, Ord a) => a -> a -> Bool
  |
5 | f :: (Eq a, Ord a) => a -> a -> Bool
  |      ^^^^^^^^^^^^^

Explanation

This example declares f with the constraints (Eq a, Ord a), but an Ord a instance already implies an Eq a instance, because Eq is a superclass of Ord.

We fix this by removing the constraint which is marked as redundant in the error message, namely Eq a. Note that we can still use the (==) method of Eq in the body of f because of the aforementioned superclass constraint.

Example.hs
Before
{-# OPTIONS_GHC -Wredundant-constraints #-}

module Example where

f :: (Eq a, Ord a) => a -> a -> Bool
f x y = (x < y) || x == y
After
{-# OPTIONS_GHC -Wredundant-constraints #-}

module Example where

f :: Ord a => a -> a -> Bool
f x y = (x < y) || x == y