Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

haskell - the seq function and strictness

I have been wondering about this a lot, but I haven't been able to find anything about it.

When using the seq function, how does it then really work? Everywhere, it is just explained saying that seq a b evaluates a, discards the result and returns b.

But what does that really mean? Would the following result in strict evaluation:

foo s t = seq q (bar q t) where
      q = s*t

What I mean is, is q strictly evaluated before being used in bar? And would the following be equivalent:

foo s t = seq (s*t) (bar (s*t) t)

I find it a little hard getting specifics on the functionality of this function.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You're not alone. seq is probably one of the most difficult Haskell functions to use properly, for a few different reasons. In your first example:

foo s t = seq q (bar q t) where
      q = s*t

q is evaluated before bar q t is evaluated. If bar q t is never evaluated, q won't be either. So if you have

main = do
    let val = foo 10 20
    return ()

as val is never used, it won't be evaluated. So q won't be evaluated either. If you instead have

main = print (foo 10 20)

the result of foo 10 20 is evaluated (by print), so within foo q is evaluated before the result of bar.

This is also why this doesn't work:

myseq x = seq x x

Semantically, this means the first x will be evaluated before the second x is evaluated. But if the second x is never evaluated, the first one doesn't need to be either. So seq x x is exactly equivalent to x.

Your second example may or may not be the same thing. Here, the expression s*t will be evaluated before bar's output, but it may not be the same s*t as the first parameter to bar. If the compiler performs common sub-expression elimination, it may common-up the two identical expressions. GHC can be fairly conservative about where it does CSE though, so you can't rely on this. If I define bar q t = q*t it does perform the CSE and evaluate s*t before using that value in bar. It may not do so for more complex expressions.

You might also want to know what is meant by strict evaluation. seq evaluates the first argument to weak head normal form (WHNF), which for data types means unpacking the outermost constructor. Consider this:

baz xs y = seq xs (map (*y) xs)

xs must be a list, because of map. When seq evaluates it, it will essentially transform the code into

case xs of
  [] -> map (*y) xs
  (_:_) -> map (*y) xs

This means it will determine if the list is empty or not, then return the second argument. Note that none of the list values are evaluated. So you can do this:

Prelude> seq [undefined] 4
4

but not this

Prelude> seq undefined 5
*** Exception: Prelude.undefined

Whatever data type you use for seqs first argument, evaluating to WHNF will go far enough to figure out the constructor and no further. Unless the data type has components that are marked as strict with a bang pattern. Then all the strict fields will also be evaluated to WHNF.

Edit: (thanks to Daniel Wagner for suggestion in comments)

For functions, seq will evaluate the expression until the function "has a lambda showing", which means that it's ready for application. Here are some examples that might demonstrate what this means:

-- ok, lambda is outermost
Prelude> seq (x -> undefined) 'a'
'a'

-- not ok.  Because of the inner seq, `undefined` must be evaluated before
-- the lambda is showing
Prelude> seq (seq undefined (x -> x)) 'b'
*** Exception: Prelude.undefined

If you think of a lambda binding as a (built-in) data constructor, seq on functions is perfectly consistent with using it on data.

Also, "lambda binding" subsumes all types of function definitions, whether defined by lambda notation or as a normal function.

The Controversy section of the HaskellWiki's seq page has a little about some of the consequences of seq in relation to functions.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...