Skip to content

"panic! (the 'impossible' happened)" When Combining -XImpredicativeTypes and -XQualifiedDo

Summary

Defining some custom monad operations to work with existential indexed types and using them with qualified do syntax causes the compiler to panic.

Steps to reproduce

Playground Link: https://play.haskell.org/saved/v7RiGi1I

{-# OPTIONS_GHC -Wall #-}
{-# LANGUAGE QualifiedDo #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE ImpredicativeTypes #-}
{-# LANGUAGE GADTs #-}

module Main where

import qualified Control.Monad as M
import Prelude hiding (return, (>>=))

type Exis f = (forall r. (forall t. f t -> r) -> r)

data Indexed t where
    Indexed :: Indexed Int

(>>=) :: Monad m => m (Exis f) -> (forall t. f t -> m (Exis g)) -> m (Exis g)
x >>= f = x M.>>= (\x' -> x' f)

return :: Monad m => Exis f -> m (Exis f)
return = M.return

test :: (Monad m) => Exis Indexed -> m (Exis Indexed)
test x =
  Main.do
    (reified :: Indexed t) <- return x
    return (\g -> g reified)

main :: IO ()
main = putStrLn "Hello World!"

Trying to compile this code produces:

<no location info>: error:
    panic! (the 'impossible' happened)
  GHC version 9.6.1:
	lookupIdSubst
  $dMonad_aNw
  InScope {x_axg m_aNg $dMonad_aNh >>= return}
  Call stack:
      CallStack (from HasCallStack):
        callStackDoc, called at compiler/GHC/Utils/Panic.hs:189:37 in ghc:GHC.Utils.Panic
        pprPanic, called at compiler/GHC/Core/Subst.hs:197:17 in ghc:GHC.Core.Subst
  CallStack (from HasCallStack):
    panic, called at compiler/GHC/Utils/Error.hs:454:29 in ghc:GHC.Utils.Error


Please report this as a GHC bug:  https://www.haskell.org/ghc/reportabug

Expected behavior

Compile the program without errors.

Environment

Tested on Haskell Playground with GHC version 9.2.5, 9.2.7, 9.4.4, and 9.6.1 - all panic.

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