Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

the seq function and strictness

Tags:

haskell

strict

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.

like image 844
Undreren Avatar asked Jun 15 '12 07:06

Undreren


People also ask

What is the SEQ () function in R?

seq() function in R Language is used to create a sequence of elements in a Vector. It takes the length and difference between values as optional argument. Syntax: seq(from, to, by, length.out)

What is strictness in Haskell?

Strictness analysis Optimising compilers like GHC try to reduce the cost of laziness using strictness analysis, which attempts to determine which function arguments are always evaluated by the function, and hence can be evaluated by the caller instead.

What is SEQ in Haskell?

From HaskellWiki. The seq function is the most basic method of introducing strictness to a Haskell program. seq :: a -> b -> b takes two arguments of any type, and returns the second. However, it also has the important property that it is magically strict in its first argument.

What does length out mean in R?

out equally spaced values from from to to . ( length. out is usually abbreviated to length or len , and seq_len is much faster.) The fourth form generates the integer sequence 1, 2, ..., length(along. with) .


1 Answers

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.

like image 181
John L Avatar answered Sep 20 '22 03:09

John L