[Haskell-cafe] enforcing strictness on arbitrary subexpressions

Udo Stenzel u.stenzel at web.de
Thu Feb 16 07:10:14 EST 2006

Matthias Fischmann wrote:
> I want to force evaluation on an arbitrary expression.
> [...]

> main :: IO ()
> main = do
>        hPutStr stdout veryLongString  -- lazy
>        veryLongString `seq` hPutStr stdout veryLongString  -- still lazy?
>        (StrictThingy veryLongString) `seq` hPutStr stdout veryLongString  -- strict (or at least i hope it is)

The last line is actually equivalent to the second.  Strict data
constructors are defined in term of seq, and seq only forces the
evaluation of the outermost constructor.  So after seq'ing a string, it
is either empty or has at least one element, but that element and the
whole tail are still unevaluated.  You have to recurse into the string
to evaluate everything:

> hPutStr stdout $ foldr seq veryLongString veryLongString

There is no primitive to do this for arbitrary data types, but the
DeepSeq type class comes close.  You can find DeepSeq and some more
hints on strict evaluation at

Today is the tomorrow you worried about yesterday.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://www.haskell.org//pipermail/haskell-cafe/attachments/20060216/02ffe28b/attachment.bin

More information about the Haskell-Cafe mailing list