what does seq stand for in haskell
Asked Answered
W

2

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*???

Wentworth answered 9/1, 2014 at 8:45 Comment(1)
You may find this answer helpful: #61479790 .Octavie
O
-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:

Oftentimes answered 9/1, 2014 at 12:33 Comment(5)
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.Patino
look at last few lines here and read here. Or I misunderstood something?Oftentimes
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.Jueta
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.Patino
Thank you for explaining. I put your comment to original answer, because I don't want other to be mistaken by me.Oftentimes
G
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.

Gallows answered 9/1, 2014 at 8:57 Comment(2)
Yep, "sequence" as a verb, rather than a noun.Gaikwar
With the added confusion that seq doesn't actually force the arguments to be evaluated in sequence. You need pseq for that.Focalize
O
-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:

Oftentimes answered 9/1, 2014 at 12:33 Comment(5)
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.Patino
look at last few lines here and read here. Or I misunderstood something?Oftentimes
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.Jueta
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.Patino
Thank you for explaining. I put your comment to original answer, because I don't want other to be mistaken by me.Oftentimes

© 2022 - 2024 — McMap. All rights reserved.