Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two calls to `seq` in strict version of `first`

Tags:

haskell

seq

Why are there two calls to seq in the code below (sourced from here) - r is passed twice:

first' :: (a -> b) -> (a, c) -> (b, c)
first' f (x,y) = let { x' = f x; r = (x', y) } 
                   in x' `seq` r `seq` r

I would think that one call to seq would do the trick of making it strict.

like image 807
Sal Avatar asked May 25 '21 19:05

Sal


People also ask

What is seq_ARB_random?

If two sequences have equal priority, then the items from them are picked in a random order. SEQ_ARB_RANDOM: If this arbitration mode is selected, sequence items from different sequences are picked in a random order by ignoring all priorities.

What is seq_ARB_strict_random?

SEQ_ARB_STRICT_RANDOM: This is similar to SEQ_ARB_RANDOM except that the priorities are NOT ignored. The items are picked randomly from sequences with highest priority first followed by next and in that order.

Is simple lock-based protocol serializable?

Implementing this lock system without any restrictions gives us the Simple Lock-based protocol (or Binary Locking ), but it has its own disadvantages, they do not guarantee Serializability. Schedules may follow the preceding rules but a non-serializable schedule may result. Attention reader! Don’t stop learning now.


2 Answers

It's a mistake at some level or other. r `seq` r and r are completely identical, semantically.

like image 165
Daniel Wagner Avatar answered Oct 18 '22 00:10

Daniel Wagner


I'm fairly confident that it is a typo. I guess it should have been

first' :: (a -> b) -> (a, c) -> (b, c)
first' f (x,y) = let { x' = f x; r = (x', y) } 
                   in x' `seq` y `seq` r

so that both pair components are forced. Or perhaps

first' :: (a -> b) -> (a, c) -> (b, c)
first' f (x,y) = let { x' = f x; r = (x', y) } 
                   in x' `seq` r

which produces a fully evaluated pair, assuming that the second component was already evaluated. This is sensible, for instance, if all the operations we use that act on the IORef (Int, Int) state preserve the invariant "both components must always be evaluated".

like image 5
chi Avatar answered Oct 18 '22 01:10

chi