Skip to content

Underscore alters semantics of unlifted let binding

Consider this program:

{-# LANGUAGE UnliftedDatatypes #-}

import GHC.Exts

type A :: UnliftedType
data A = A !Integer

fib :: Int -> Integer
fib 0 = 0
fib 1 = 1
fib n = fib (n - 1) + fib (n - 2)

main =
  let _x = A (fib 100)
  in pure ()

This will take very long to evaluate. However, if instead we change _x to _:

  let _ = A (fib 100)
  in pure ()

Then the program finishes instantly!

I've always thought of _ as just a special name that I can never refer to, but it seems GHC even gives it a different semantics. That surprised me.

Environment

  • GHC version used: 9.6.2
Edited by Jaro Reinders
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information