Skip to content

panic when compiling recursive function on type changing continuations

Summary

when compiling a certain function i get the following error

<no location info>: error:
    panic! (the 'impossible' happened)
  GHC version 9.4.2:
	lookupIdSubst
  $dApplicative_aKX
  InScope {p_aKS $creturn_aLA}
  Call stack:
      CallStack (from HasCallStack):
        callStackDoc, called at compiler/GHC/Utils/Panic.hs:182:37 in ghc:GHC.Utils.Panic
        pprPanic, called at compiler/GHC/Core/Subst.hs:260:17 in ghc:GHC.Core.Subst

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

Steps to reproduce

paste the following code in a source file and try to compile it

newtype Cont o i a = Cont {runCont ::(a -> i) -> o }
t1:: Cont (i2 -> o) i1 a -> Cont o i2 (a -> i1)
t1 c = Cont $ \ati1tti2 -> (runCont c) (ati1tti2 $ \a -> evalCont (t1 c) >>== \ati1 -> return ati1 a )
evalCont:: Cont o a a -> o 
evalCont c = (runCont c)id
instance Monad (Cont p p) where
  return a = Cont ($ a)
  (>>=) = (>>==)
class PMonad m where
  (>>==):: m p q a -> (a -> m q r b) -> m p r b
instance PMonad Cont where
 (Cont cont) >>== afmb = Cont $ \bti -> cont $ \a -> (runCont . afmb) a bti
main:: IO ()
main = putStrLn "bug"

for some reason the code doesn't type check if i remove the class and define >>== as a top level function. the t1 function is the problematic one. i do believe it shouldn't type check at all as evalCont is applied to (t1 c) which is of type Cont o i2 (a -> i1) which most definitely isn't of type Cont o a a for all types o i2 a and i1

Expected behavior

the code should compile normally or it should report a compile error not panic

Environment

  • GHC version used: 9.4.2

Optional:

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