seq vs. pseq
Simon Marlow
simonmarhaskell at gmail.com
Wed Nov 8 08:29:04 EST 2006
Ross Paterson wrote:
> On Mon, Nov 06, 2006 at 06:25:48PM +0000, Malcolm Wallace wrote:
>
>>When I use `seq`, it is sometimes in a construction like
>>
>> unsafePerformIO (emit "squawk!) `seq` x
>>
>>where I am trying to force the impure side-effect to happen, exactly and
>>immediately before x is evaluated. Whilst this is not good style in a
>>general sense, I argue that it is perfectly safe inside certain kinds of
>>library (e.g. for calculating coverage information, or for emitting
>>tracing information). But if the language itself cannot guarantee this
>>exact placement of side-effects, then it becomes impossible to write
>>computation-reflective tools like Hat and hpc for Haskell, in Haskell.
>>That would surely be a sad state of affairs.
>
>
> Without admitting the existence of "unsafePerformIO", I submit
>
> unsafePerformIO (emit "squawk! >> return x)
>
> where >> really does imply sequencing.
Does it? What's stopping a Haskell implementation from evaluating x early? As
far as I know, the only time it wouldn't be valid is if x refers to a stream
created by hGetContents, and even then you could evaluate it early as long as
you don't move it past some I/O operation that could "tell the difference"
(although the report doesn't really say this explicitly, it's just the accepted
behaviour of lazy input streams).
Cheers,
Simon
More information about the Glasgow-haskell-users
mailing list