Skip to content

Regression: HEAD requires FlexibleContexts because of inferred, redundant constraint

Originally observed in a head.hackage build here.

The following code, minimized from the pandoc-2.11.2 library, fails to typecheck on GHC HEAD (I tried using commit 62ed6957) but does typecheck with GHC 8.10.2 and 9.0.1-alpha1:

{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
module Bug where

class C a b where
  m :: a -> b

instance C a a where
  m = id

instance C a (Maybe a) where
  m _ = Nothing

f :: a -> Maybe a
f = g
  where
    g x = h (m x) x

h :: Maybe a -> a -> Maybe a
h = const

On HEAD, this produces the following error:

$ ~/Software/ghc-9.1.0.20201208/bin/ghc Bug.hs
[1 of 1] Compiling Bug              ( Bug.hs, Bug.o )

Bug.hs:17:5: error:
    • Non type-variable argument in the constraint: C a1 (Maybe a1)
      (Use FlexibleContexts to permit this)
    • When checking the inferred type
        g :: forall {a}. C a (Maybe a) => a -> Maybe a
      In an equation for ‘f’:
          f = g
            where
                g x = h (m x) x
   |
17 |     g x = h (m x) x
   |     ^^^^^^^^^^^^^^^

Previous versions of GHC would accept this program because the inferred type of g was forall {a}. a -> Maybe a, not g :: forall {a}. C a (Maybe a) => a -> Maybe a. The presence of this extra C a (Maybe a) constraint (which seems redundant?) is suspicious, so I opted to open an issue about it.

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