[Haskell-beginners] Re: [Haskell-cafe] try, seq, and IO

Jeroen van Maanen jeroen at lexau.org
Fri Sep 17 07:17:30 EDT 2010

Op 2010-09-17 06:32, Dean Herington schreef:
> At 10:54 AM +0200 9/16/10, Jeroen van Maanen wrote:
>> Op 2010-09-16 08:50, Jeroen van Maanen schreef:
>> [...]
>> Gregs suggestion provided the way out: evaluate. My code now looks like this:
>>          do let maybeUpdatedModel = f update startModel
>>                 theCheckSum = maybe 0 checkSum maybeUpdatedModel
>>                 logException exception = logger "Exception" [showString label, shows (exception :: SomeException)] >> return Nothing
>>             logger "Received update" [showString label, logs update]
>>             resultOrException <- try $ evaluate $ seq theCheckSum maybeUpdatedModel
>>             maybeNextModel <- either logException return resultOrException
>>             logger "Maybe next model" [showString label, logs maybeNextModel]
>>             -- ...
>> I still don't understand why evaluating maybeUpdatedModel to WHNF after seq evaluated theCheckSum to WHNF forces the lurking exception to the surface. Or is the phrase 'when the resultant IO action is executed' in the documentation of 'evaluate' significant here?
> I still don't follow all your code, but presuming maybeUpdatedModel is of non-IO type, I'm guessing it's the evaluation of theCheckSum that causes evaluation of maybeUpdatedModel (which raises the exception).

What I don't understand is the difference between:

  try $ return $ seq theCheckSum maybeUpdatedModel

or even

  try $! return $! seq theCheckSum maybeUpdatedModel


  try $ evaluate $ seq theCheckSum maybeUpdatedModel

How is it possible that the exception escapes the former two expressions, but gets caught by the third try?

Cheers, Jeroen

More information about the Beginners mailing list