Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the strictness-introducing function called seq?

I understand the seq function and why it's necessary to introduce strictness for efficiency. What I don't understand is, why is this primitive called seq (and not something to do with strictness)?

like image 219
psquid Avatar asked Apr 28 '20 11:04

psquid


People also ask

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 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.


1 Answers

TL;DR: Miranda called it seq, it was introduced when sequence was (probably) already a thing for Monads, and ($!) was known as strict for a short time.

Miranda was first

It is called seq because it was called seq in Miranda and previous languages, at least according to A History of Haskell: Being Lazy With Class by Paul Hudak, John Hughes, Simon Peyton Jones and Philip Wadler.

Both seq and strict components of data structures were already present in Miranda for the same reasons (Turner, 1985), and indeed seq had been used to fix space leaks in lazy programs since the early 1980s (Scheevel, 1984; Hughes, 1983)

Note that Turner only introduced the strict components in the 1985 paper, not seq itself, and Scheevel's "NORMA Sasl manual" seems to be lost or at least not available on the Internet. Hughes thesis ("Hughes, 1983" above) doesn't introduce seq either.

Either way, seq was part of Mirandas standard environment and also contains a hint why it was called seq:

`seq' applied to two values, returns the second but checks that the first value is not completely undefined. Sometimes needed, e.g. to ensure correct synchronisation in interactive programs.

Correct synchronisation or sequencing.

Other possible names

Now, why wasn't that simply called strict in Haskell? Or even sequence?

Well, it turns out that Haskell 1.3, which introduced seq, did also introduce Monad, and thus sequence :: Monad m => [m a] -> m (). Therefore, sequence was not available as a name.

Now that sequence was out of the picture, let's have a look at strict. strict was included in 1.3, since 1.3 introduced an Eval typeclass:

seq :: Eval a => a -> b -> b
strict :: Eval a => (a -> b) -> (a -> b)
strict f = \x -> seq x (f x)

Neither Eval nor strict didn't make the cut into Haskell98 as-is. Instead, Eval was completely removed, as it applied to all types either way, and strict was renamed to ($!).

like image 152
Zeta Avatar answered Oct 05 '22 11:10

Zeta