3

I am new to haskell, and have just come to the lazy world proramming. I read that the seq function is very special because it forces to use a strict evaluation in order to be more efficient in some cases. But I just can't find what seq stand for literally. Maybe Strict Evaluation Q*???

richard.g
  • 3,585
  • 4
  • 16
  • 26
  • You may find this answer helpful: https://stackoverflow.com/questions/61479290/why-is-the-strictness-introducing-function-called-seq . – atravers Aug 27 '20 at 02:09

2 Answers2

10

It is supposed to remind you of "sequentially" or "sequence" because it allows the programmer to specify the sequence of evaluation of its arguments.

Chris Taylor
  • 46,912
  • 15
  • 110
  • 154
-6

seq evaluates its first argument before returning the second one. It is usually introduced to improve performance by avoiding unneeded laziness. It forces evaluation of the function.

seq :: a -> b -> b
seq _ y = y

from prelude.hs

This function definition is wrapped in #ifdef __HADDOCK__ so it will only be compiled by haddock (the documentation tool), not by the actual compiler! 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. You wrote 'seq evaluates its first argument' - but first definition obviously does not do this.

via user2407038

More to read:

Community
  • 1
  • 1
d12frosted
  • 1,280
  • 12
  • 33
  • 6
    This is *not* how `seq` is defined! Do the following: `myseq _ y = y`. `seq undefined ()` == `undefined` but `myseq undefined ()` == `()`. The definition you gave for `seq` is precisely the opposite of what it actually does. `seq` is actually not defined 'in haskell'; it is a primitive. – user2407038 Jan 09 '14 at 22:28
  • look at last few lines [here](http://hackage.haskell.org/package/base-4.6.0.1/docs/src/Prelude.html#%24%21) and read [here](http://www.haskell.org/haskellwiki/Seq). Or I misunderstood something? – d12frosted Jan 09 '14 at 23:00
  • Haskell uses a nonstrict evaluation order (in the case of GHC, the specific evaluation order is lazy evaluation). This means that the function you gave never evaluates it's first argument. The function you have written is `flip const`. `seq` cannot be defined in normal Haskell. – David Young Jan 10 '14 at 01:07
  • 2
    Yes, you are. That function definition is wrapped in `#ifdef __HADDOCK__` so it will only be compiled by haddock (the documentation tool), not by the actual compiler! 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. You wrote 'seq evaluates its first argument' - but your definition obviously does not do this. – user2407038 Jan 10 '14 at 01:08
  • Thank you for explaining. I put your comment to original answer, because I don't want other to be mistaken by me. – d12frosted Jan 10 '14 at 07:32