more unsafePerformIO questions ([...])

Simon Marlow simonmar@microsoft.com
Tue, 19 Aug 2003 15:52:04 +0100


=20
> On Tuesday, 2003-08-19, 13:18, Simon Marlow wrote:
> > [...]
>=20
> > Yes, I agree that one shouldn't rely on the "no duplication of work"
> > property.  However, folloing this argument we arrive at the=20
> conclusion that
> > hGetContents is an invalid use of unsafePerformIO.  (which=20
> is something I've
> > been saying for a while now :-).
>=20
> Can't hGetContents be implemented without unsafePerformIO but with=20
> unsafeInterleaveIO? Wouldn't this be valid? Am I'm missing=20
> something here?

unsafeInterleaveIO =3D return . unsafePerformIO

Well, almost.  unsafeInterleaveIO should also give you the guarantee
that the IO in its argument can't be performed *before* the
unsafeInterleaveIO is executed, but I don't think this distinction is
important to the present discussion.

Actually I dislike unsafeInterleaveIO even more than unsafePerformIO,
because it has an implicit laziness assumption.  The only purpose of
unsafeInterleaveIO is to "delay" some IO until it is "demanded".  But
Haskell lacks a precise definition of "demand", prefering to leave the
evaluation order up to the implementation.  Hence, unsafeInterleaveIO is
left in limbo - it might do something useful, but that depends entirely
on your implementation.  GHC doesn't guarantee much about
unsafeInterleaveIO at all.

Cheers,
	Simon