
Am Freitag 16 April 2010 21:14:54 schrieb Stephen Blackheath [to Haskell- Beginners]:
"evaluate x" is defined as "return $! x".
Not quite: Prelude Control.Exception> evaluate (undefined :: Int) `seq` True True Prelude Control.Exception> ((return $! undefined) :: IO Int) `seq` True *** Exception: Prelude.undefined Haddocks: "Forces its argument to be evaluated to weak head normal form when the resultant IO action is executed. It can be used to order evaluation with respect to other IO operations; its semantics are given by evaluate x `seq` y ==> y evaluate x `catch` f ==> (return $! x) `catch` f evaluate x >>= f ==> (return $! x) >>= f Note: the first equation implies that (evaluate x) is not the same as (return $! x). A correct definition is evaluate x = (return $! x) >>= return "
This indeed evaluates the value immediately then and there. It *only* evaluates to WHNF (weak head normal form). For number values, WHNF (= evaluate to outer constructor only) and NF (= completely evaluate) are the same thing, as shown in your example above. The (a * 2) makes no difference, because whether WHNF = NF or not depends on the type of the value only, not the expression used to calculate it.
To evaluate x to NF (completely) then and there in general, you have to say
evaluate (rnf x)
where rnf is defined in Control.DeepSeq in the 'deepseq' package. The type of x has to be an NFData instance. Haskell does not provide any other way to force evaluation to NF other than to explicitly use `seq` on each element of the structure. The purpose of DeepSeq is to make this task easier.
Steve