The Proper Definition of (evaluate :: a -> IO a)

Simon Marlow simonmarhaskell at gmail.com
Tue May 8 04:12:44 EDT 2007


Malcolm Wallace wrote:
> Nils Anders Danielsson <nad at cs.chalmers.se> wrote:
> 
>>> Where did you find the erroneous version?
>> Presumably he found it by reading the documentation for evaluate:
>>   http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Exception.html#7
> 
> Actually, I checked the source code for Control.Exception before asking
> the question, and those laws definitely do not appear there.  So now I'm
> kind of puzzled as to how Haddock managed to generate documentation for
> them!
> 
>>>> evaluate x `seq` y    ==>  y
>>> I'm not sure why anyone thinks this "law" should hold,
>> You seem to be assuming that evaluate x = x, which is not
>> necessarily the case.
> 
> Yes, I did make a mistake when I asked the question, in assuming some
> intuitive semantics rather than the actually written ones.  So taking
> the law at face value, it seems to say that 'evaluate' is supposed to
> force its argument, yet the result should be guaranteed to be in WHNF,
> even if the argument was in fact undefined.  Given that the result is in
> the IO monad, it can only mean one of two things:
> 
>   (1) That 'evaluate' should _not_ force its argument when it is called,
>       but that rather that the argument should be forced only when the
>       resultant IO action is executed.  This conflicts with the
>       documentation, which implies that the argument is forced _before_
>       the IO action is created or executed.  But I guess this semantics
>       accords with Isaac's suggested definition:
>           evaluate x = (x `seq` return x) >>= return

That's exactly right.  Evaluate was introduced because it does something 
different from (return $!).  We tried in the documentation to be clear about its 
semantics, but perhaps we weren't clear enough.  I'll add Isaac's suggested 
definition to the docs.

Cheers,
	Simon


More information about the Libraries mailing list