Skip to content

internal error: stg_ap_p_ret with GADTs and TypeLits

Summary

I was playing with GADTs and TypeLits, and got an internal error in GHCi.

Note that this bug is very fragile, the provided file seems to be minimal, in the sense that:

  • manually inlining f or g fixes the issue
  • adding a type signature (the inferred one) to x fixes the issue

Steps to reproduce

Run ghci bug.hs and type :force x

Observed behavior

GHCi output:

GHCi, version 9.4.8: https://www.haskell.org/ghc/  :? for help
[1 of 1] Compiling Foo              ( bug.hs, interpreted )
Ok, one module loaded.
ghci> :force x
ghc-9.4.8: internal error: stg_ap_p_ret
    (GHC version 9.4.8 for x86_64_unknown_linux)
    Please report this as a GHC bug:  https://www.haskell.org/ghc/reportabug

Aborted (core dumped)

File

bug.hs:

{-# LANGUAGE GADTs                 #-}
{-# LANGUAGE DataKinds             #-}

module Foo where

import GHC.TypeLits
import Data.Proxy

data N (n :: Nat) where
    O :: (n ~ 0) => N n
    S :: (1 <= n) => N n

f :: forall n. KnownNat n => (1 <= n => N n) -> N n
f c = case cmpNat @1 @n Proxy Proxy of EQI -> c

g :: KnownNat n => N n
g = f S

--x :: N 1 -- With type signature: no issue
x = g @1

-- ghci> :force x --> internal error: stg_ap_p_ret

Environment

  • GHC version used: 9.4.8
  • Operating System: Arch Linux
  • System Architecture: x86_64
Edited by Victor Miquel
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information