Skip to content

Regression: infinite loop on quantified constraints

{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE QuantifiedConstraints #-}
module Test where

class Eq r => Op op r
class (forall r. Eq r => Op op r) => HasResult op

foo :: HasResult op => op -> Bool
foo _ = True == False

GHC 8.10.7 accepts the above program, but GHC 9.0.2, 9.2.5, 9.4.3, and a recent git release all reject it with:

Test.hs:9:14: error:
    • Reduction stack overflow; size = 201
      When simplifying the following type: Eq Bool
      Use -freduction-depth=0 to disable this check
      (any upper bound you could choose might fail unpredictably with
       minor updates to GHC, so disabling the check is recommended if
       you're sure that type checking should terminate)
    • In the expression: True == False
      In an equation for ‘foo’: foo _ = True == False

Raising the bound or disabling the check doesn't help.

Removing the HasResult op constraint from foo makes the program compile again.

It seems that GHC, when it tries to solve the Eq Bool constraint, is not using the top-level instance, but looping via the quantified constraint of the local HasResult constraint.

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