What does seq actually do in Haskell?

17

From Real World Haskell I read

It operates as follows: when a seq expression is evaluated, it forces its first argument to be evaluated, then returns its second argument. It doesn't actually do anything with the first argument: seq exists solely as a way to force that value to be evaluated.

where I've emphasised the then because to me it implies an order in which the two things happen.

From Hackage I read

The value of seq a b is bottom if a is bottom, and otherwise equal to b. In other words, it evaluates the first argument a to weak head normal form (WHNF). seq is usually introduced to improve performance by avoiding unneeded laziness.

A note on evaluation order: the expression seq a b does not guarantee that a will be evaluated before b. The only guarantee given by seq is that the both a and b will be evaluated before seq returns a value. In particular, this means that b may be evaluated before a. […]

Furthermore, if I click on the # Source link from there, the page doesn't exist, so I can't see the code of seq.

That seems in line with a comment under this answer:

[…] seq cannot be defined in normal Haskell

On the other hand (or on the same hand, really), another comment reads:

The 'real' seq is defined in GHC.Prim as seq :: a -> b -> b; seq = let x = x in x. This is only a dummy definition. Basically seq is specially syntax handled particularly by the compiler.

Can anybody shed some light on this topic? Especially in terms of:

  • What source is right?
  • Is seq's implementation really not writable in Haskell?
    • If so, what does it even mean? That it is a primitive? What does this tell me about what seq actually does?
  • In seq a b is a guaranteed to be evaluated before b at least in the case that b makes use of a, e.g. seq a (a + x)?
Share
Improve this question
0

Comments

Popular posts from this blog

Meaning of `{}` for return expression

Get current scroll position of ScrollView in React Native

Chart JS +ng2-charts not working on Angular+2