[Haskell-cafe] Strictness is observable

Yves Parès limestrael at gmail.com
Fri Apr 1 10:49:53 CEST 2011


Then if you turn :

fs True = True
fs x = True

to:

fs x = case x of
    True -> True
    x' -> True

Is it still strict, or does 'fs' wrap the case test and defer evaluation?

2011/4/1 <oleg at okmij.org>

>
> 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"
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20110401/3becda03/attachment.htm>


More information about the Haskell-Cafe mailing list