Skip to content

Extra-constraints wildcards in instance declarations

Extra-constraints wildcards are sometimes handy for figuring out the constraints to put on a function definition. However, they cannot be used in instance declarations:

GHCi, version 9.2.2: https://www.haskell.org/ghc/  :? for help
ghci> let { foo :: _ => a -> String ; foo = show }

<interactive>:1:14: error:
    • Found extra-constraints wildcard standing for ‘Show a’
      Where: ‘a’ is a rigid type variable bound by
               the inferred type of foo :: Show a => a -> String
               at <interactive>:1:7-29
      To use the inferred type, enable PartialTypeSignatures
    • In the type signature: foo :: _ => a -> String
ghci> data I a = I a
ghci> instance _ => Show (I a) where show (I x) = show x

<interactive>:3:10: error:
    Wildcard ‘_’ not allowed
      in an instance declaration

Is there a fundamental reason for this restriction? It seems to me that it should be possible to infer the extra constraints required by an instance, just as GHC already does for functions (though I can imagine the implementation might be slightly less trivial because it would need to gather the wanted constraints from all the methods, then simplify them).

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