[Haskell-cafe] Parsec and network data

Ryan Ingram ryani.spam at gmail.com
Thu Aug 28 00:17:29 EDT 2008

On Tue, Aug 26, 2008 at 1:35 PM, brian <brianchina60221 at gmail.com> wrote:
> One unworkable approach I tried is to get a lazy String from the
> handle with hGetContents.
> [...]
> The OP had the same problem I did, so he made a variant of
> hGetContents with timeout support. The problem: he used something from
> unsafe*. I came to Haskell for rigor and reliability and it would make
> me really sad to have to use a function with 'unsafe' in its name that
> has a lot of wacky caveats about inlining, etc.

unsafeInterleaveIO has no weird inlining caveats (as opposed to
unsafePerformIO, which does).  In fact, hGetContents is implemented
using unsafeInterleaveIO; the source is here:

The "unsafe" bit is that it potentially allows side effects embedded
within pure values; if the value is never demanded, the side effect
never executes, so the observable behavior of your program can be made
to depend on whether or not (or when) that value is evaluated.

But when you are reading from a network stream, that's exactly what you want.

  -- ryan

More information about the Haskell-Cafe mailing list