Unsure about 'IO'

Simon Marlow simonmar@microsoft.com
Wed, 6 Feb 2002 16:59:37 -0000


> We've been working on bulding an interpreter in haskell
> and we seem to have encountered a problem.
> At the end of the email I've pasted a minimal version of the program,=20
> that shows the error.
>=20
> The problem is that we 'putStr' a prompt onto (unbuffered) stdout
> and then read a line, process it and 'print' output, all very basic.
>=20
> This is done in the function 'processline' and works fine is this=20
> function is used directly from 'main', however, if used from within=20
> the 'while' construct it fails.
> In the 'while' context it appears that the 'getLine' is executed=20
> before the 'putStr' is (or perhaps stdout is not as 'unbuffered'
> as we expect ?).

In your example it is the 'isEOF' that is waiting for input.  The I/O =
system can't determine whether the end of the input has been reached yet =
without waiting for the user to type something: the user might type ^D =
which would indicate EOF, or they might type some characters.

This is arguably wrong, although it does seem to follow the Haskell =
Library Report (section 11.4.1).  Strictly speaking, it would be wrong =
not to block in isEOF because we might be at the end of file and not =
know it yet.

You can use hReady to determine whether isEOF will block (although =
interestingly the report says that hReady can fail with an EOF error; =
GHC won't do this in practice).

Cheers,
	Simon