Difference between revisions of "Seq"

From HaskellWiki
Jump to navigation Jump to search
m
("Controversy" section expanded, etc)
 
(2 intermediate revisions by the same user not shown)
Line 19: Line 19:
 
=== History ===
 
=== History ===
   
  +
The need to specify an order of evaluation in otherwise-nonstrict programming languages has appeared before:
The need for a primitive sequencing definition has been known since at least 1996:
 
  +
  +
<blockquote>
  +
We can use <code>VAL</code> to define a "sequential evaluation" operator which evaluates
  +
its first argument and then returns its second:
  +
  +
<pre>
  +
a ; b = (λx. b) val a
  +
</pre>
  +
  +
and we can define other functions which control evaluation order [...]
  +
  +
:<small>[https://www.cs.ox.ac.uk/files/3309/PRG40.pdf The Design and Implementation of Programming Languages] (page 88 of 159)</small>.
  +
</blockquote>
  +
  +
<blockquote>
  +
`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.
  +
<pre>
  +
> seq :: *->**->** ||defined internally
  +
</pre>
  +
  +
:<small>[https://www.cs.kent.ac.uk/people/staff/dat/miranda/stdenv.html The Miranda Standard Environment © Research Software Limited 1989]</small>
  +
</blockquote>
  +
  +
and in Haskell since at least 1996:
   
 
<blockquote>
 
<blockquote>
Line 52: Line 76:
   
 
<blockquote>
 
<blockquote>
<b>2.1 The need for</b><code>pseq</code>
+
<b>2.1 The need for</b> <code>pseq</code>
   
 
The <code>pseq</code> combinator is used for sequencing; informally, it
 
The <code>pseq</code> combinator is used for sequencing; informally, it
Line 140: Line 164:
 
which is useful for some of the same reasons.
 
which is useful for some of the same reasons.
   
=== Controversy! ===
+
=== Controversy? ===
   
  +
The presence of <code>seq</code> in Haskell does have some disadvantages:
Note that <code>seq</code> is the ''only'' way to force evaluation of a value with a function type (except by applying it, which is liable to cause other problems). As such, it is the only reason why Haskell programs are able to distinguish between the following two values:
 
  +
:{|
  +
|- style="vertical-align:top;"
  +
|1.
  +
|It is the only reason why Haskell programs are able to distinguish between the following two values:
   
 
<haskell>
 
<haskell>
undefined :: a -> b
+
undefined :: a -> b
 
const undefined :: a -> b
 
const undefined :: a -> b
 
</haskell>
 
</haskell>
   
This violates the principle from lambda calculus of extensionality of functions, or eta-conversion, because <code>f</code> and <code>\x -> f x</code> are distinct functions, even though they return the same output for ''every'' input. For this reason, <code>seq</code>, and this distinction, is sometimes ignored e.g. when assessing the correctness of [[Correctness of short cut fusion|optimisation techniques]] or type class instances.
+
This violates the principle of extensionality of functions, or eta-conversion from the lambda calculus, because <code>f</code> and <code>\x -> f x</code> are distinct functions, even though they return the same output for every input.
  +
|- style="vertical-align:top;"
  +
|2.
  +
|It can invalidate [[Correctness of short cut fusion|optimisation techniques]] which would normally be safe, causing the following two expressions to differ:
  +
  +
<haskell>
  +
foldr ⊥ 0 (build seq) = foldr ⊥ 0 (seq (:) []) = foldr ⊥ 0 [] = 0
  +
seq ⊥ 0 = ⊥
  +
</haskell>
  +
  +
This weakens the ability to use parametricity, which implies <code>foldr k z (build g) == g k z</code> for suitable values of <code>g</code>, <code>k</code> and <code>z</code>.
  +
|- style="vertical-align:top;"
  +
|3.
  +
|It can invalidate laws which would otherwise hold, also causing expressions to have differing results:
  +
  +
<haskell>
  +
seq (⊥ >>= return :: State s a) True = True
  +
seq (⊥ :: State s a) True = ⊥
  +
</haskell>
  +
  +
This violates the first [[Monad laws|monad law]], that <code>m >>= return == m</code>.
  +
|}
  +
  +
But <code>seq</code> (sequential or otherwise) isn't alone in causing such difficulties:
  +
:{|
  +
|- style="vertical-align:top;"
  +
|1.
  +
|When combined with call-by-need semantics, the use of weak-head normal form [https://www.pauldownen.com/publications/first-class-call-stacks.pdf is also detrimental to extensionality].
  +
|- style="vertical-align:top;"
  +
|2.
  +
|When combined with GADTs, the associated map functions which uphold the functor laws [https://arxiv.org/pdf/2105.03389v3 is also problematic for parametricity].
  +
|- style="vertical-align:top;"
  +
|3.
  +
|The ability to define the fixed-point combinator in Haskell using recursion:
  +
  +
<haskell>
  +
yet :: (a -> a) -> a
  +
yet f = f (yet f)
  +
</haskell>
  +
  +
means [https://dl.acm.org/doi/pdf/10.1145/99370.99404 parametricity is further restricted].
  +
|- style="vertical-align:top;"
  +
|4.
  +
|Strictness must be considered when [https://ris.utwente.nl/ws/portalfiles/portal/284115323/thesis_M_Fokkinga.pdf deriving laws for various recursive algorithms] (see chapter 6).
  +
|- style="vertical-align:top;"
  +
|5.
  +
|Similar to <code>seq</code> and <code>⊥</code>, the use of division and zero present their own challenges!
  +
|}
  +
  +
Therefore all such claims against <code>seq</code> (or calls for its outright removal) should be examined <i>in this context</i>.
   
 
== See also ==
 
== See also ==
   
  +
* [https://mail.haskell.org/pipermail/glasgow-haskell-users/2006-October/011430.html Parallelism in 6.6 and seq vs. pseq], Haskell mail archives. <!-- 2006 -->
* [http://stackoverflow.com/questions/12687392/why-is-seq-bad Why is seq bad?]
 
  +
  +
* [https://mail.haskell.org/pipermail/haskell-cafe/2011-April/091043.html Questioning seq], Haskell mail archives. <!-- 2011 -->
  +
  +
* [https://gitlab.haskell.org/ghc/ghc/-/wikis/commentary/compiler/seq-magic seq &#91;bedlam&#93;], the GHC wiki. <!-- 2011 -->
   
 
[[Category:Glossary]]
 
[[Category:Glossary]]

Latest revision as of 12:00, 24 September 2024

While its name might suggest otherwise, the seq function's purpose is to introduce strictness to a Haskell program. As indicated by its type signature:

seq :: a -> b -> b

it takes two arguments of any type, and returns the second. However, it also has the important property that it is always strict in its first argument. In essence, seq is defined by the following two equations:

 `seq` b         = 
a `seq` b | a   = b

(See Bottom for an explanation of the symbol.)


History

The need to specify an order of evaluation in otherwise-nonstrict programming languages has appeared before:

We can use VAL to define a "sequential evaluation" operator which evaluates its first argument and then returns its second:

a ; b = (λx. b) val a

and we can define other functions which control evaluation order [...]

The Design and Implementation of Programming Languages (page 88 of 159).

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

> seq :: *->**->** ||defined internally
The Miranda Standard Environment © Research Software Limited 1989

and in Haskell since at least 1996:

The seq combinator implements sequential composition. When the expression e1 ‘seq‘ e2 is evaluated, e1 is evaluated to weak head normal form first, and then the value of e2 is returned. In the following parallel nfib function, seq is used to force the evaluation of n2 before the addition takes place. This is because Haskell does not specify which operand is evaluated first, and if n1 was evaluated before n2, there would be no parallelism.

nfib :: Int -> Int
nfib n | n <= 1    = 1
       | otherwise = n1 par (n2 seq n1 + n2 + 1)
                     where
                       n1 = nfib (n-1)
                       n2 = nfib (n-2)
Accidents always Come in Threes: A Case Study of Data-intensive Programs in Parallel Haskell (page 2 of 14).

...the same year seq was introduced in Haskell 1.3 as a method of the (now-abandonded) Eval type class:

class Eval a where
   strict :: (a -> b) -> a -> b
   seq    :: a -> b -> b

   strict f x = x seq f x

However, despite that need by the time Haskell 98 was released seq had been reduced to a primitive strictness definition. But in 2009, all doubts about the need for a primitive sequencing definition were vanquished:

2.1 The need for pseq

The pseq combinator is used for sequencing; informally, it evaluates its first argument to weak-head normal form, and then evaluates its second argument, returning the value of its second argument. Consider this definition of parMap:

parMap f []     = []
parMap f (x:xs) = y par (ys pseq y:ys)
   where y  = f x
         ys = parMap f xs

The intention here is to spark the evaluation of f x, and then evaluate parMap f xs, before returning the new list y:ys. The programmer is hoping to express an ordering of the evaluation: first spark y, then evaluate ys.

Runtime Support for Multicore Haskell (page 2 of 12).

Alas, this confirmation failed to influence Haskell 2010 - to this day, seq remains just a primitive strictness definition. So for enhanced confusion the only Haskell implementation still in widespread use now provides both seq and pseq.

Demystifying seq

A common misconception regarding seq is that seq x "evaluates" x. Well, sort of. seq doesn't evaluate anything just by virtue of existing in the source file, all it does is introduce an artificial data dependency of one value on another: when the result of seq is evaluated, the first argument must also (sort of; see below) be evaluated. As an example, suppose x :: Integer, then seq x b behaves essentially like if x == 0 then b else b – unconditionally equal to b, but forcing x along the way. In particular, the expression x `seq` x is completely redundant, and always has exactly the same effect as just writing x.

Strictly speaking, the two equations of seq are all it must satisfy, and if the compiler can statically prove that the first argument is not ⊥, or that its second argument is, it doesn't have to evaluate anything to meet its obligations. In practice, this almost never happens, and would probably be considered highly counterintuitive behaviour on the part of GHC (or whatever else you use to run your code). So for example, in seq a b it is perfectly legitimate for seq to:

1. evaluate b - its second argument,
2. before evaluating a - its first argument,
3. then returning b.

In this larger example:

let x = ... in
let y = sum [0..47] in
x `seq` 3 + y + y^2

seq immediately evaluating its second argument (3 + y + y^2) avoids having to allocate space to store y:

let x = ... in
case sum [0..47] of
  y -> x `seq` 3 + y + y^2

However, sometimes this ambiguity is undesirable, hence the need for pseq.

Common uses of seq

seq is typically used in the semantic interpretation of other strictness techniques, like strictness annotations in data types, or GHC's BangPatterns extension. For example, the meaning of this:

f !x !y = z

is this:

f x y | x `seq` y `seq` False = undefined
      | otherwise = z

although that literal translation may not actually take place.

seq is frequently used with accumulating parameters to ensure that they don't become huge thunks, which will be forced at the end anyway. For example, strict foldl:

foldl' :: (a -> b -> a) -> a -> [b] -> a
foldl' _ z [] = z
foldl' f z (x:xs) = let z' = f z x in z' `seq` foldl' f z' xs

It's also used to define strict application:

($!) :: (a -> b) -> a -> b
f $! x = x `seq` f x

which is useful for some of the same reasons.

Controversy?

The presence of seq in Haskell does have some disadvantages:

1. It is the only reason why Haskell programs are able to distinguish between the following two values:
undefined       :: a -> b
const undefined :: a -> b

This violates the principle of extensionality of functions, or eta-conversion from the lambda calculus, because f and \x -> f x are distinct functions, even though they return the same output for every input.

2. It can invalidate optimisation techniques which would normally be safe, causing the following two expressions to differ:
foldr  0 (build seq) = foldr  0 (seq (:) []) = foldr  0 [] = 0
seq  0                                                       = 

This weakens the ability to use parametricity, which implies foldr k z (build g) == g k z for suitable values of g, k and z.

3. It can invalidate laws which would otherwise hold, also causing expressions to have differing results:
seq ( >>= return :: State s a) True = True
seq ( :: State s a) True            = 

This violates the first monad law, that m >>= return == m.

But seq (sequential or otherwise) isn't alone in causing such difficulties:

1. When combined with call-by-need semantics, the use of weak-head normal form is also detrimental to extensionality.
2. When combined with GADTs, the associated map functions which uphold the functor laws is also problematic for parametricity.
3. The ability to define the fixed-point combinator in Haskell using recursion:
yet :: (a -> a) -> a
yet f = f (yet f)

means parametricity is further restricted.

4. Strictness must be considered when deriving laws for various recursive algorithms (see chapter 6).
5. Similar to seq and , the use of division and zero present their own challenges!

Therefore all such claims against seq (or calls for its outright removal) should be examined in this context.

See also