Skip to content

Premature Evalutation when using -O

Hi all,
the following program crashes (Prelude.head: empty
list) when compiled
with optimizations by both ghc-6.0 and ghc-5.04.3 .
When the out-commented line is activated it runs fine
with 5.04.3,
but still crashes when compiled by 6.0 .
Without -O both run perfectly fine.

Before each command the file is touched:

Without the commented-line:
    % ghc-5.04.3 test1.hs && ./a.out   
    % ghc-5.04.3 -O test1.hs && ./a.out

    Fail: Prelude.head: empty list
    % ghc-6.0 test1.hs && ./a.out 
    % ghc-6.0 -O test1.hs && ./a.out

    Fail: Prelude.head: empty list

With it:
    % ghc-5.04.3 test1.hs && ./a.out   
    % ghc-5.04.3 -O test1.hs && ./a.out
    % ghc-6.0 test1.hs && ./a.out 
    % ghc-6.0 -O test1.hs && ./a.out

    Fail: Prelude.head: empty list


Full test done on:
    % ghc --version
    The Glorious Glasgow Haskell Compilation System,
version 6.0
    % uname -a
    Linux localhost 2.4.20 #1 Sat Nov 30 14:46:26 CET
2002 i686 unknown

I also tested the prebuilt binaries for linux-x86 and
win98, but only
with ghc-5.04.3 -O and ghc-6.0 -O (same behaviour).

AFAICS this really seems to be a bug in GHC, as
    a) Laziness should prevent `len' to be evaluated
       until after program-exit ;o)
    b) I fail to see why this behaviour would change
       when upgrading my compiler
    c) AFAIK -O shouldn't change semantics
       (well, not in this way at least)
    d) I don't see why the "_ <- return ()"  would make any
       difference. (Except for messing up the optimizer ;))

If it isn't a bug, please enlighten me.

Happy hacking,
Remi


module Main where

import System
import Monad

main    = do
            args <- getArgs

            when (null args) $
                exitWith ExitSuccess

            -- _ <- return ()

            let len = read (head args) :: Int

            print (show len)
            print len
Trac metadata
Trac field Value
Version 6.0
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution ResolvedFixed
Component Compiler
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information