Haskell: Interact use causing error
Asked Answered
L

1

4

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
Lepto answered 27/1, 2013 at 7:5 Comment(2)
This is a known annoying feature of GHCI. See here.Nevers
@n.m. thanks for the link, however none of the "solutions" in that link, neither :load, :reload or :set +r work. Any ideas? I don't want to have to restart ghci after every getContents >>= print type operation.Stunner
C
7

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.

Clavius answered 27/1, 2013 at 10:12 Comment(2)
Could you look at my update and explain why the code added works? I'm unsure how the newly added code affects stdin/unsafePerformIO?Lepto
I added an explanation of why interact behaves differently based on the function you pass it and how lazy I/O works. Your question is about 2 separate issues. I hope that helps clarify.Clavius

© 2022 - 2024 — McMap. All rights reserved.