Misleading error message when lacking an extra-constraints wildcard
Consider this module
{-# LANGUAGE PartialTypeSignatures #-}
module Foo where
f :: (Show a) => a -> _ -> Bool
f x y = x>x
Note the partial type signature. We get [W] Ord a
from the x>x
, but there is no extra-constraints wildcard, so we can't solve it. Currently GHC reports this:
Foo.hs:5:1: error: [GHC-39999]
Could not deduce ‘Ord a’
GHC Bug #20076 <https://gitlab.haskell.org/ghc/ghc/-/issues/20076>
Assuming you have a partial type signature, you can avoid this error
by either adding an extra-constraints wildcard (like `(..., _) => ...`,
with the underscore at the end of the constraint), or by avoiding the
use of a simplifiable constraint in your partial type signature.
from the context: Eq a
bound by the inferred type for ‘f’:
forall a {w}. Show a => a -> w -> Bool
at Foo.hs:5:1-11
The reference to #20076 is entirely bogus: there is no simplifiable constraint in sight. We simply have a constraint we can't solve.
#20076 remains valid...we "ought" to be able to solve that one. But the program reported above is much more likely to happen (who writes simplifiable constraints like #20076?), and is not a GHC error. The right thing is to add the extra-constraints wildcard.
A second complication. If the program was like this (with no Show a
constraint):
f2 :: a -> _ -> Bool
f2 x y = x>x
then we don't get the above error. We just get the fairly resonable:
Foo.hs:5:10: error: [GHC-39999]
• No instance for ‘Ord a’ arising from a use of ‘>’
Possible fix:
add (Ord a) to the context of
the inferred type of f2 :: a -> w -> Bool
Why the difference, just from adding or removing an unrelated (and unused) constraint?
It turns out to be because of Note [Partial type signatures and the monomorphism restriction]
. We apply the MR to a binding group that has partial signature(s), none of which have an extra-constraints wildcard.
Is this a good idea? I don't know. But it took me 15 mins to track down so I'm recording it here.