As shown in this answer, seq
combined with undefined
does very strange things when it comes to equational reasoning, for example it can make any monad fail. Another example is in this question.
Recently I stumbled upon evaluate :: a -> IO a
that does a similar thing - it evaluates its argument to WHNF but only when the IO
action is evaluated. This seems to be much safer, as one expects that "in IO
we can do everything". Of course it cannot be used everywhere, but often the need to evaluate an expression is connected somehow with an IO
operation (like to force a producing thread to evaluate a computation instead of a consuming thread when working with MVar
s).
So I'd like to ask, how safe is evaluate
? Is it possible to create examples (involve IO
of course) where it breaks reasoning about code like seq
does? Or can I consider it as a safe replacement of seq
(if it's possible for a particular program)?