IO confusion

Ian Lynagh igloo@earth.li
Wed, 18 Dec 2002 14:13:42 +0000


Hi all,

With "Foo" in the file c.out and the module

\begin{code}
module Main (main) where

import IO (openFile, hGetContents, hClose, IOMode(ReadMode))
import System (getArgs)
import Monad (when)

main :: IO ()
main = do [x] <- getArgs
          let you_want_it_to_work = read x
          cout <- openFile "c.out" ReadMode
          s <- hGetContents cout
          putStrLn ""
          when you_want_it_to_work $ putStrLn $ "Got this: " ++ s
          putStrLn ""
          hClose cout
          putStrLn $ "The answer is: " ++ s
\end{code}

I expected "The answer is: Foo" to be printed whether the argument was
True or False. When it is False, however, GHC (5.02.2, 5.04 and recent
CVS HEAD) think s is the empty string and nhc98 (1.10 and a 1.11
from about a year ago) produces a binary that segfaults. At first I
thought it was a GHC bug, but now nhc98 also exhibits it I am wondering
if it is a bug in my understanding?


Thanks
Ian