Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
868 views
in Technique[技术] by (71.8m points)

haskell - 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)?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 ($!).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...