Questions tagged [strictness]

In the semantics of Haskell, strictness relates to whether evaluating an expression forces evaluation of a sub-expression.

68 questions
3
votes
1 answer

Weak head normal form and order of evaluation

I've read lots on weak head normal form and seq. But I'm still have trouble imagining the logic behind Haskell's order of evaluation A common example demonstrating when and how to use but I still don't understand how the common example foldl (+) 0…
3
votes
1 answer

Stack space overflow (possibly related to mapM)

I'm writing a program that creates a shell script containing one command for each image file in a directory. There are 667,944 images in the directory, so I need to handle the strictness/laziness issue properly. Here's a simple example that gives me…
mhwombat
  • 8,026
  • 28
  • 53
2
votes
2 answers

Strictness and how to tell GHC/GHCi to just store a value in a variable once and for all

Apologies if this is a common question, I couldn't find anything similar, but I may just be too inexperienced to know the proper vocabulary. Here's an example of the fundamental problem in GHCi: -- foo is something relatively expensive to compute,…
Syncrossus
  • 570
  • 3
  • 17
2
votes
0 answers

How to Avoid Lifted Values in Worker of Tuple-Valued Recursive Function?

I have the following minimal example: import Test.Tasty.Bench {-# INLINE loop #-} loop :: Int -> Int -> Int loop a 0 = a loop a n = loop (a + x + y) (n - 1) where (x, y) = foo n {-# INLINE foo #-} foo :: Int -> (Int, Int) foo n = if n > 0 then (n…
Jules
  • 487
  • 3
  • 10
2
votes
2 answers

Understanding non-strictness in Haskell with a recursive example

What is the difference between this two, in terms of evaluation? Why this "obeys" (how to say?) non-strictness recFilter :: (a -> Bool) -> [a] -> [a] recFilter _ [] = [] recFilter p (h:tl) = if (p h) then h : recFilter p tl else recFilter p…
LowFieldTheory
  • 1,722
  • 1
  • 26
  • 39
2
votes
3 answers

How to set strictness in list comprehension?

I'm bit stuck how to rewrite following strict-evaluated list comprehension to use seq instead of bang pattern: zipWith' f l1 l2 = [ f e1 e2 | (!e1, !e2) <- zip l1 l2 ] Any idea ? I've tried zipWith' f l1 l2 = [ e1 `seq` e2 `seq` f e1 e2 | (e1, e2)…
David Unric
  • 7,421
  • 1
  • 37
  • 65
2
votes
1 answer

Avoiding thunks in sparsely evaluated list generated by monadic unfold

I have a simulation library that uses the FFI wrapped in a monad M, carrying a context. All the foreign functions are pure, so I've decided to make the monad lazy, which is normally convenient for flow-control. I represent my simulation as a list of…
notBob
  • 143
  • 8
2
votes
1 answer

Could liftM be too strict for a Functor instance, if we are writing down mfix with it?

fmap for Monads often defaults to liftM: liftM :: (Monad m) => (a1 -> r) -> m a1 -> m r liftM f m1 = do { x1 <- m1; return (f x1) } Yet, as one can see it uses binding (as the right hand desugars into m1 >>= \ x1 -> return (f x1)). I wonder…
Zhiltsoff Igor
  • 1,812
  • 8
  • 24
2
votes
1 answer

How can seq evaluate an inifinite list in Haskell?

It is said that the Haskell seq function forces the evaluation of its first argument and returns the second. It is used to add strictness to evaluation of expressions. So how can the following simply return 5: seq [1..] 5 Shouldn't it get stuck in…
user3560270
  • 413
  • 2
  • 7
2
votes
1 answer

How to fully evaluate a recursive data type using Control.DeepSeq in Haskell?

I am trying to benchmark (with Criterion) a function, which uses a recursive data type. I found a similar question with an answer that I haven't been able to apply for my case. For non-recursive data types, the following works: data ExampleDataType1…
ProgrammerPotato
  • 505
  • 1
  • 4
  • 11
2
votes
2 answers

Is evaluate or $! sufficient to WHNF-force a value in a multithreaded monadic context, or do I need pseq?

The following seems to work (as in: it keeps saying Surely tomorrow every second) import Control.Concurrent import Control.Concurrent.MVar import Control.Exception (evaluate) main :: IO () main = do godot <- newEmptyMVar forkIO $ do …
leftaroundabout
  • 117,950
  • 5
  • 174
  • 319
2
votes
1 answer

Irrefutable/lazy pattern exercise in Haskell wikibook

Half way down here... https://en.wikibooks.org/wiki/Haskell/Laziness ...is an exercise asking about the effects of changes to an alternative implementation of the head function that uses irrefutable patterns. It provides a definition of head' as…
2
votes
3 answers

Unsure of how to get the right evaluation order

I'm not sure what the difference between these two pieces of code is (with respect to x), but the first one completes: $ foldr (\x y -> if x == 4 then x else x + y) 0 [1,2 .. ] 10 and the second one doesn't (at least in GHCi): $ foldr (\x (y, n) ->…
Matt Fenwick
  • 48,199
  • 22
  • 128
  • 192
2
votes
1 answer

How does the bind operator for Eval in Control.Parallel.Strategies evaluate its argument strictly?

The source code for Control.Parallel.Strategies ( http://hackage.haskell.org/packages/archive/parallel/3.1.0.1/doc/html/src/Control-Parallel-Strategies.html#Eval ) contains a type Eval defined as: data Eval a = Done a which has the following Monad…
Benjamin Malley
  • 233
  • 4
  • 14
1
vote
2 answers

What's the intuition for recursively defined Haskell Data.Array and strictness?

Consider this Haskell program module RecursiveArray where import Data.Array ( (!), listArray, Array ) goodArray :: Array Int Int goodArray = listArray (0, 1) (go 0) where go x = x : go ((goodArray ! x) + 1) badArray :: Array Int…
Tarrasch
  • 10,199
  • 6
  • 41
  • 57