RE: "interact" behaves oddly if used interactively

The real problem is that lazy I/O injects side effects into the pure world of expressions. Haskell has a perfectly good system for encapsulating side effects - the IO monad. So why put these sneaky side effects into pure values?
I fear one problem is that the word "side effect" is not properly defined.
Actually both getContents and interact are in the IO monad. Since the action "interact" is not terminated until the input stream ends, I do not see any problem there.
Yes, it is probably true that you cannot observe any non-referential-transparency using only interact. The side effects are only visible in the IO monad, and since as you say interact doesn't let you back into the IO monad until it has finished, it is probably safe. The unsafety is only visible using the other lazy I/O operations, principally hGetContents.
Evaluation never leaves the IO monad until termination. getContents is more strange. However, you can just define its semantics to yield a random string. When you are not careful you may certainly get something nearly random ;-)
So... you agree that getContents in its current form should really be called unsafeGetContents? Unless perhaps we redefine its semantics to either (a) yield a random string or (b) eagerly slurp the entire stream contents? Cheers, Simon

So... you agree that getContents in its current form should really be called unsafeGetContents? Unless perhaps we redefine its semantics to either (a) yield a random string or (b) eagerly slurp the entire stream contents?
Well, I'm not sure that the semantics of getContents is currently clearly defined and I'm not sure what the prefix `unsafe' really means. However, I don't want to continously disagree with you. unsafeGetContents would have been a better name. Olaf -- OLAF CHITIL, Dept. of Computer Science, The University of York, York YO10 5DD, UK. URL: http://www.cs.york.ac.uk/~olaf/ Tel: +44 1904 434756; Fax: +44 1904 432767

On Thu, Oct 02, 2003 at 11:48:29AM +0100, Simon Marlow wrote:
So... you agree that getContents in its current form should really be called unsafeGetContents? Unless perhaps we redefine its semantics to either (a) yield a random string or (b) eagerly slurp the entire stream contents?
personally, I think the easiest solution would be to punt the whole issue by having: getContents lazily read the file if it is mmapable or a pipe eagerly slurp the whole file if it refers to a tty this will at least make it much harder for the lazyness of IO to bite an interactive user. reading from a tty has a real side effect from the users point of view, so should be done strictly, while in the case of a presumably immutable (over the course of the program) file it is simply a (useful) harmless optimization to have the file read lazily. of course this makes 'interact' not really interactive, but at least the semantics are clear when typing stuff at it, nothing happens until you finish. if people want line-by-line interactivity, don't rely on the evaluation strategy of a particular compiler, use Readline and monads. John -- --------------------------------------------------------------------------- John Meacham - California Institute of Technology, Alum. - john@foo.net ---------------------------------------------------------------------------

John Meacham wrote:
personally, I think the easiest solution would be to punt the whole issue by having:
getContents lazily read the file if it is mmapable or a pipe
eagerly slurp the whole file if it refers to a tty
I think this kind of irregular behaviour would make the IO functions
even more difficult to understand. Why should some invokations of the
same program exhibit utterly different space/time behaviour from others?!
mycomputer% ./myprogram
mycomputer% cat | ./myprogram
mycomputer% ./myprogram of course this makes 'interact' not really interactive, Exactly, it defeats the whole purpose of the interact function (to
provide support for Landin-stream IO), so you might just as well remove
it completely...
--
Thomas H
participants (4)
-
John Meacham
-
Olaf Chitil
-
Simon Marlow
-
Thomas Hallgren