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)?
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.
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.
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.
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
seqand strict components of data structures were already present in Miranda for the same reasons (Turner, 1985), and indeedseqhad 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.
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 ($!).
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