Skip to content

Class with Quantified Constraint that fails to compile with GHC 9

Summary

Class definition with superclass constraints containing quantified constraints containing equality constraints along with default methods that compiled with GHC 8 fails to compile with GHC 9.

Steps to reproduce

Try compiling the following typeclass:

class
  ( forall i j. Functor (m i j)
  , forall i j. i ~ j => Monad (m i j)
  ) => IndexedMonad m where
    ixJoin :: m i j (m j k x) -> m i k x
    ixJoin = ixBind id
    ixBind :: (x -> m j k y) -> m i j x -> m i k y
    ixBind f x = ixJoin (f <$> x)

Expected behavior

We expect it to compile. Instead it fails with the following message from GHC.

    • Could not deduce (Functor (m i j)) arising from a use of ‘<$>’
      from the context: IndexedMonad m
        bound by the class declaration for ‘IndexedMonad’ at Bug.hs:8:8-19
    • In the first argument of ‘ixJoin’, namely ‘(f <$> x)’
      In the expression: ixJoin (f <$> x)
      In an equation for ‘ixBind’: ixBind f x = ixJoin (f <$> x)
   |
12 |     ixBind f x = ixJoin (f <$> x)
   |             

This class compiles successfully for GHC 8 but not for GHC 9. It may be that this is new expected behavior because of the Simplify subsumption proposal. But if so, it is not addressed in the migration guide. This issue can be fixed by removing the type equality constraint. The following definition does compile with GHC 9.

class
  ( forall i j. Functor (m i j)
  , forall i. Monad (m i i)
  ) => IndexedMonad m where
    ixJoin :: m i j (m j k x) -> m i k x
    ixJoin = ixBind id
    ixBind :: (x -> m j k y) -> m i j x -> m i k y
    ixBind f x = ixJoin (f <$> x)

The migration guide gives no indication that this is the correct fix.

Environment

  • GHC version used: > 9

Optional:

  • Operating System: OS X
  • System Architecture: X86
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information