Skip to content

GHC enters endless loop in ghc 9.4 (fixed in ghc >= 9.6)

Summary

I was playing around with some constraints stuff and encountered a weird thing where the compiler (apparently) enters an endless loop when trying to add a Show instance. This is indeed an erroneous program, but I still don't want the compiler enters an endless loop.

I hope this is no duplicate (didn't quite read through all 1000 issues that contain "loop")

Steps to reproduce

{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE TypeFamilies #-}
{-# OPTIONS_GHC -Wall #-}

module Has where

import Data.Functor.Identity (Identity (Identity))
import Data.Kind (Constraint, Type)

type Has :: forall k. (k -> Constraint) -> (k -> Type) -> k -> Type
data Has c f a where
  Proves :: c a => f a -> Has c f a

type Top :: forall k. k -> Constraint
class Top a

instance Top a

class CFunctor c f | f -> c where
  cfmap :: c b => (a -> b) -> f a -> (c b => f b -> r) -> r

instance CFunctor c (Has (c :: Type -> Constraint) f) where
  cfmap f x k = k $ Proves (cfmap f x \(Proves y) -> y)

newtype Unconstrained f a = MkUnconstrained {getFunctor :: f a}

instance Functor f => CFunctor Top (Unconstrained f) where
  cfmap f x k = k $ MkUnconstrained (f <$> getFunctor x)

deriving via (Unconstrained Identity) instance CFunctor Top Identity

-- instance Show (Has c f a) where
--   show = cfmap show

comment out the last two lines, ghci should hang

Expected behavior

The compiler terminates.

Environment

  • GHC version used: 945

Optional:

  • system: "x86_64-linux"
  • host os: Linux 6.3.1-zen1, NixOS, 23.05 (Stoat), 23.05.20230506.897876e

Additional information

  • the issue does not occur with 961
  • commenting out TypeFamilies causes a warning to be show by the deriving via declaration
Edited by Magnus
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information