GHCi and Unsafe I/O
You can reduce this problem (the exception) to:
main = getContents >> return ()
(interact
calls getContents
)
The problem is that stdin
(getContents
is really hGetContents stdin
) remains evaluated in GHCi in-between calls to main
. If you look up stdin
, it's implemented as:
stdin :: Handle
stdin = unsafePerformIO $ ...
To see why this is a problem, you could load this into GHCi:
import System.IO.Unsafe
f :: ()
f = unsafePerformIO $ putStrLn "Hi!"
Then, in GHCi:
*Main> f
Hi!
()
*Main> f
()
Since we've used unsafePerformIO
and told the compiler that f
is a pure function, it thinks it doesn't need to evaluate it a second time. In the case of stdin
, all of the initialization on the handle isn't run a second time and it's still in a semi-closed state (which hGetContents
puts it in), which causes the exception. So I think that GHCi is "correct" in this case and the problem lies in the definition of stdin
which is a practical convenience for compiled programs that will just evaluate stdin
once.
Interact and Lazy I/O
As for why interact
quits after a single line of input while the unlines . lines
version continues, let's try reducing that as well:
main :: IO ()
main = interact (const "response\n")
If you test the above version, interact won't even wait for input before printing response
. Why? Here's the source for interact
(in GHC):
interact f = do s <- getContents
putStr (f s)
getContents
is lazy I/O, and since f
in this case doesn't need s
, nothing is read from stdin
.
If you change your test program to:
main :: IO ()
main = interact test
test :: String -> String
test [] = show 0
test a = show a
you should notice different behavior. And that suggests that in your original version (test a = show 3
), the compiler is smart enough to realize that it only needs enough input to determine if the string read is empty or not (because if it's not empty, it doesn't need to know what a
is, it just needs to print "3"
). Since the input is presumably line-buffered on a terminal, it reads up until you press the return key.
:load
,:reload
or:set +r
work. Any ideas? I don't want to have to restart ghci after everygetContents >>= print
type operation. – Stunner