[Haskell-cafe] Strictness is observable
oleg at okmij.org
oleg at okmij.org
Fri Apr 1 09:00:56 CEST 2011
Daniel Fischer wrote:
> If you have a strict function, you may evaluate its argument eagerly
> without changing the result^1, while eager evaluation of a non-strict
> function's argument may produce _|_ where deferred evaluation wouldn't.
Sadly, that is quite untrue. Strictness is observable, already in
Haskell98. That distressing result has nothing to do with imprecise
exceptions, seq, non-termination, lack of resources, or the use of
unsafe features. Plainly, just by changing the strictness of a
function one may cause the program to print different results, such as
"strict" or "non-strict" in the code below.
-- Haskell98!
-- Strictness is observable
-- With no seq, no unsafe operations
import Control.Exception
-- fs and fns are both essentially (const True) functions,
-- but differ in strictness
fs,fns :: Bool -> Bool
-- non-strict
fns x = True
-- strict
fs True = True
fs x = True
handler :: SomeException -> IO ()
handler _ = print "strict"
test f = handle handler $
if f (error "Bang!") then print "non-strict" else return ()
main_s = test fs
-- prints "strict"
main_ns = test fns
-- prints "non-strict"
More information about the Haskell-Cafe
mailing list