I get that seq
is used to improve performance by avoiding unneeded laziness. I just want to know where the name is derived from? Is it from "sequence" or "sequential"? And how does the name relate to strict evaluation?
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.
Advertisements. Functions play a major role in Haskell, as it is a functional programming language. Like other languages, Haskell does have its own functional definition and declaration. Function declaration consists of the function name and its argument list along with its output.
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. In essence, seq is defined by the following two equations: ⊥ `seq` b = ⊥ a `seq` b = b.
Haskell - Functions. Functions play a major role in Haskell, as it is a functional programming language. Like other languages, Haskell does have its own functional definition and declaration. Function declaration consists of the function name and its argument list along with its output.
The Seq a type represents a finite sequence of values of type a. Sequences generally behave very much like lists. The class instances for sequences are all based very closely on those for lists. Many functions in this module have the same names as functions in the Prelude or in Data.List. In almost all cases, these functions behave analogously.
It is just a basic practice syntax, in the coming section of the tutorial we will see the internal working and implementation of where clause in the Haskell programing language. How does Where Function work in Haskell? As we already know that where is a keyword or a function that helps to dived the complex calculations into smaller parts.
It comes from sequence point. That's a well-known concept in C, and it's indeed quite similar to the seq
operator in Haskell: every computation on the left should be done before any computation on the right.
Of course, Haskell seq
is a bit less demanding than that: it merely requests that the thing on the left is evaluated to weak head normal form before the result on the right is evaluated. And it doesn't really guarantee any particular evaluation order at all†, only that if the expression on the left is ⊥ then the one on the right must not be evaluated.
See pseq
or deepseq
for stronger alternatives, which come closer to what C calls sequence points.
†Actually, C or C++ sequence points don't guarantee computation order either, only that any side effects are in the right order. But, in C side effects are ubiquitous so apart from low-level optimisations you can usually assume that sequence-point order will be upheld, whereas GHC will in fact quite often throw seq
s away if only it knows that the expressions do not diverge.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With