问题
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)?
回答1:
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 indeedseq
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 ($!)
.
来源:https://stackoverflow.com/questions/61479290/why-is-the-strictness-introducing-function-called-seq