Skip to content

CASEFAIL with GADTs and type families in GHC 9.2.6, 9.2.7, 9.6.1, but not in 9.2.5 and strangely 9.4.4

Summary

Some GHC versions run into CASEFAIL in when working with GADTs and associated type families. Or, at least, that's where I encountered this.

Steps to reproduce

I tried to get a minimal-ish reproducer, but it turned out to be pretty hard. So, apologies for the contrived code

{-# LANGUAGE GHC2021, DataKinds, TypeFamilies #-}

import Data.Maybe
import Data.Kind

main :: IO ()
main = print $ runInstrImpl MAP (VOption Nothing :& RNil)

class MapOp (c :: T) where
  type MapOpInp c :: T
  type MapOpRes c :: T -> T
  mapOpToList :: Value c -> [Value (MapOpInp c)]
  mapOpFromList :: Value c -> [Value b] -> Value (MapOpRes c b)
instance MapOp ('TOption e) where
  type MapOpInp ('TOption e) = e
  type MapOpRes ('TOption e) = 'TOption
  mapOpToList (VOption l) = maybe [] pure l
  mapOpFromList (VOption _) l' = VOption $ listToMaybe l'

data Value t where
  VOption :: forall t. Maybe (Value t) -> Value ('TOption t)
  VUnit :: Value 'TUnit -- ^ Just to have a terminating case
deriving instance Show (Value t)

data T
  = TOption T
  | TUnit -- ^ Just to have a terminating case

data Instr (inp :: [T]) (out :: [T]) where
  MAP :: MapOp c => Instr (c ': s) (MapOpRes c (MapOpInp c) ': s)

data Rec :: (u -> Type) -> [u] -> Type where
  RNil :: Rec f '[]
  (:&) :: !(f r) -> !(Rec f rs) -> Rec f (r ': rs)

deriving instance (Show (f r), Show (Rec f rs)) => Show (Rec f (r ': rs))
deriving instance Show (Rec f '[])

runInstrImpl :: Instr inp out -> Rec Value inp -> Rec Value out
runInstrImpl i@MAP (a :& r) | _ :: Instr (c ': xs) out <- i = do
  mapOpFromList a (mapOpToList @c a) :& r

Output:

*** 9.2.5 ***
(:&) (VOption Nothing) RNil
*** 9.2.6 ***
Main.hs: internal error: interpretBCO: hit a CASEFAIL
    (GHC version 9.2.6 for x86_64_unknown_linux)
    Please report this as a GHC bug:  https://www.haskell.org/ghc/reportabug
Aborted (core dumped)
*** 9.2.7 ***
Main.hs: internal error: interpretBCO: hit a CASEFAIL
    (GHC version 9.2.7 for x86_64_unknown_linux)
    Please report this as a GHC bug:  https://www.haskell.org/ghc/reportabug
Aborted (core dumped)
*** 9.4.4 ***
(:&) (VOption Nothing) RNil
*** 9.6.1 ***
Main.hs: internal error: interpretBCO: hit a CASEFAIL
    (GHC version 9.6.1 for x86_64_unknown_linux)
    Please report this as a GHC bug:  https://www.haskell.org/ghc/reportabug
Aborted (core dumped)

Expected behavior

Well, to not CASEFAIL, naturally.

Environment

  • GHC version used: see above

Optional:

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