[Haskell-cafe] unsafeInterleaveIO, lazyness and sharing

John Dorsey haskell at colquitt.org
Tue Aug 26 21:14:39 EDT 2008


Wolfgang,

> Haskell is non-strict but not necessarily lazy.  So it's possible
> that an expression is reduced to WHNF although it is not used yet.
> Could this early reduction also happen to outputs of
> unsafeInterleaveIO actions (which might trigger the action too early)?
> While I'd expect those outputs to be evaluated lazily (reduced as
> late as possible), I cannot find anything in the docs that guarantees
> this.

"unsafeInterleaveIO allows IO computation to be deferred lazily. When
passed a value of type IO a, the IO will only be performed when the
value of the a is demanded. This is used to implement lazy file reading,
see hGetContents."

http://haskell.org/ghc/docs/latest/html/libraries/base/System-IO-Unsafe.html#v:unsafeInterleaveIO

Is this the kind of guarantee you're looking for?

I'd bet against getting any decent durable, portable guarantees for any
unsafe* functions in Haskell; but the above behavioral description may
be strong enough to suit you.

> In addition, I'd like to know whether unsafeInterleaveIO outputs are 
> guaranteed to be evaluated at most once so that the interleaved action is 
> executed at most once.  Again, I suppose that this is the case while I cannot 
> find a guarantee for it.

I'd be surprised if an implementation didn't have that behavior.

I'd also be wary of anyone claiming to guarantee it, beyond compiler X
version Y.

John Dorsey



More information about the Haskell-Cafe mailing list