Skip to content

Strange behavior when using -O

Summary

I've encountered a strange behavior using ghc -O on the program with Prelude.last, Data.List.group, System.Exit, and IO effects.

Steps to reproduce

import Data.List (group)
import System.Exit (exitFailure, exitSuccess)

main :: IO ()
main = do
  _ <- getContents
  if last (group "a") == "a" then exitSuccess else exitFailure
ghc -O snippet.hs -o snippet && ./snippet

Actual behaviour

snippet: Prelude.last: empty list

Even strange, this can be fixed in one of the following ways:

  • Compiling without -O.
  • Removing _ <- getContents.
  • Reducing (group "a") to ["a"] or last (group "a") to "a".

Expected behavior

I expect successful exit without Prelude.last: empty list error.

Environment

  • GHC version used: The Glorious Glasgow Haskell Compilation System, version 8.6.5 from deb package ghc/focal 8.8.1+dfsg1+is+8.6.5+dfsg1-3 amd64.

Optional:

  • Operating System: Linux host 5.4.0-48-generic #52-Ubuntu SMP Thu Sep 10 10:58:49 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
  • System Architecture: x86_64
Edited by Arthur Khashaev
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information