Skip to content

False-positive redundant-constraint warning on constrained default instance method

Tested on GHC 8.10.1:

{-# LANGUAGE DefaultSignatures #-}

{-# OPTIONS_GHC -Werror -Wredundant-constraints #-}

class Base a where
  myList :: [a]
  default myList :: SubClass a => [a]
  myList = defaultMyList

class Base a => SubClass a where
  myList2 :: [a]

defaultMyList :: SubClass a => [a]
defaultMyList = myList2

main :: IO ()
main = pure ()

The error is as follows:

Test.hs:7:11: error: [-Wredundant-constraints, -Werror=redundant-constraints]
    • Redundant constraint: Base a
    • In the type signature for:
           myList :: forall a. (Base a, SubClass a) => [a]
      In the ambiguity check for ‘myList’
      To defer the ambiguity check to use sites, enable AllowAmbiguousTypes
      When checking the class method: myList :: forall a. Base a => [a]
  |
7 |   default myList :: SubClass a => [a]
  |           ^^^^^^

suggesting that some automatic process is adding (Base a) as an implicit constraint, but then this redundancy check is failing on the automatically-added constraint - which it shouldn't because the programmer can't do anything about it.

Adding AllowAmbiguousTypes does make the error go away, as the error message suggests, however the type is not ambiguous - there is no false-positive for defaultMyList with the same (source-level) signature. Adding ConstrainedClassMethods makes no difference.

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