DeepSeq.lhs [was: Re: [Haskell] Force evaluation]

Dean Herington heringtonlacey at mindspring.com
Mon Dec 13 22:32:56 EST 2004


At 4:05 PM +0000 12/8/04, Ben Rudiak-Gould wrote:
>Dean Herington wrote:
>
>>>  deepSeq :: DeepSeq a => a -> b -> b
>
>I should point out that deepSeq with this type is the composition of 
>two simpler operations:
>
>    deepSeq = seq . eval where eval :: DeepSeq a => a -> a
>
>eval ties a demand for a value to a demand for all its subvalues, 
>while seq ties a demand for a value to a demand for another value of 
>an unrelated type.
>
>Of course you can define eval x = x `deepSeq` x instead, so it's 
>largely a matter of taste.
>
>>>   instance  DeepSeq (IO a)  where  deepSeq = seq
>
>This is an interesting instance (which is not to say I think it's 
>wrong). It means the original poster's code won't work. He wanted to 
>write
>
>    foo <- eval makeFoo `catch` \e -> defaultFoo
>
>but makeFoo has a monadic type, so eval makeFoo === makeFoo.
>
>-- Ben

Michael's exact intent was not clear to me, but he could consider the 
following.

-- Dean


import Prelude hiding (catch)
import DeepSeq
import Control.Exception (catch, evaluate)

deepForce :: DeepSeq a => a -> a
deepForce x = x `deepSeq` x

deepForceIO :: DeepSeq a => a -> IO a
deepForceIO = evaluate . deepForce


makeFooGood, makeFooBad, defaultFoo :: Int
makeFooGood = 1
makeFooBad  = error "bad makeFoo"
defaultFoo  = 0

try f = (deepForceIO f `catch` \e -> return defaultFoo) >>= print

main = try makeFooGood >> try makeFooBad


More information about the Haskell mailing list