Question

I'm trying to use the interact function, but I'm having an issue with the following code:

main::IO()
main = interact test

test :: String -> String
test [] = show 0
test a = show 3

I'm using EclipseFP and taking one input it seems like there is an error. Trying to run main again leads to a:

*** Exception: <stdin>: hGetContents: illegal operation (handle is closed)

I'm not sure why this is not working, the type of test is String -> String and show is Show a => a -> String, so it seems like it should be a valid input for interact.

EDIT/UPDATE

I've tried the following and it works fine. How does the use of unlines and lines cause interact to work as expected?

main::IO()
main = interact respondPalindromes

respondPalindromes :: String -> String
respondPalindromes =
    unlines .
    map (\xs -> if isPal xs then "palindrome" else "not a palindrome") .
    lines

isPal :: String -> Bool
isPal xs = xs == reverse xs
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top