Questions tagged [strictness]

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

68 questions
322
votes
9 answers

What is Weak Head Normal Form?

What does Weak Head Normal Form (WHNF) mean? What does Head Normal form (HNF) and Normal Form (NF) mean? Real World Haskell states: The familiar seq function evaluates an expression to what we call head normal form (abbreviated HNF). It stops once…
user295190
92
votes
8 answers

What are Haskell's strictness points?

We all know (or should know) that Haskell is lazy by default. Nothing is evaluated until it must be evaluated. So when must something be evaluated? There are points where Haskell must be strict. I call these "strictness points", although this…
Dan Burton
  • 53,238
  • 27
  • 117
  • 198
33
votes
4 answers

Advantages of strict fields in data types

This may now be a bit fuzzy, but I've been wondering that for a while. To my knowledge with !, one can make sure a parameter for a data constructor is being evaluated before the value is constructed: data Foo = Bar !Int !Float I have often thought…
Lanbo
  • 15,118
  • 16
  • 70
  • 147
32
votes
3 answers

What is the relationship between unboxed types and strictness?

Unboxed types, like Int#, and strict functions, like f (!x) = ..., are something different, but I see conceptual similarity - they disallow thunks/laziness in some way. If Haskell was a strict language like Ocaml, every function would be strict and…
sdcvvc
  • 25,343
  • 4
  • 66
  • 102
31
votes
1 answer

What is spine strictness

In Haskell, the term spine strictness is often mentioned in relation to lazy evaluation. Though I have a vague understanding of that it means, it would be nice to have a more concrete explanation about: What is the spine of a data structure What…
Markus1189
  • 2,829
  • 1
  • 23
  • 32
24
votes
4 answers

Profiling a Haskell program

I have a piece of code that repeatedly samples from a probability distribution using sequence. Morally, it does something like this: sampleMean :: MonadRandom m => Int -> m Float -> m Float sampleMean n dist = do xs <- sequence (replicate n dist) …
Chris Taylor
  • 46,912
  • 15
  • 110
  • 154
23
votes
2 answers

Is foldl ever preferable to its strict cousin, foldl'?

Haskell has two left fold functions for lists: foldl, and a "strict" version, foldl'. The problem with the non-strict foldl is that it builds a tower of thunks: foldl (+) 0 [1..5] --> ((((0 + 1) + 2) + 3) + 4) + 5 --> 15 This wastes memory,…
Joey Adams
  • 41,996
  • 18
  • 86
  • 115
18
votes
4 answers

Is operator && strict in Haskell?

For example, I have an operation fnB :: a -> Bool that makes no sense until fnA :: Bool returns False. In C I may compose these two operations in one if block: if( fnA && fnB(a) ){ doSomething; } and C will guarantee that fnB will not execute until…
Dmitry Bespalov
  • 5,179
  • 3
  • 26
  • 33
15
votes
2 answers

Strict Maybe in data definitions

I have seen many talks / read blog posts that you should have strict fields in data to avoid various performance issues, e.g.: data Person = Person { personName :: !Text , personBirthday :: !UTCTime } This makes total sense to me.…
phadej
  • 11,947
  • 41
  • 78
13
votes
1 answer

Is there any guarantee about the evaluation order within a pattern match?

The following (&&) :: Bool -> Bool -> Bool False && _ = False True && False = False True && True = True has the desired short-circuit property False && undefined ≡ False. The first clause, which is non-strict in the right argument, is guaranteed to…
leftaroundabout
  • 117,950
  • 5
  • 174
  • 319
12
votes
1 answer

When is it useful to use "strict wildcard" in Haskell, and what does it do?

I was looking at some Haskell source code and came across a pattern match with !_, the code is here: http://hackage.haskell.org/package/base-4.9.0.0/docs/src/GHC.List.html#unsafeTake take n xs | 0 < n = unsafeTake n xs | otherwise =…
Carlos D
  • 180
  • 1
  • 8
12
votes
1 answer

A Stricter Control.Monad.Trans.Writer.Strict

So we have: import Control.Monad.Writer.Strict type M a = Writer (Map Key Val) a for some Key and Val. Everything works okay as long as we don't look at the collected outputs: report comp = do let (a,w) = runWriter comp putStrLn a However if…
Lambdageek
  • 12,465
  • 1
  • 25
  • 33
11
votes
2 answers

Forced strictness for lists in haskell

I made really time consuming algorithm which produces a short string as the result. When I try to print it (via putStrLn) it appears on the screen character by character. I did understand why that happened, and I tried to force evaluation of the…
MaksymB
  • 1,267
  • 10
  • 33
11
votes
2 answers

IO monad prevents short circuiting of embedded mapM?

Somewhat mystified by the following code. In non-toy version of the problem I'm trying to do a monadic computation in a monad Result, the values of which can only be constructed from within IO. Seems like the magic behind IO makes such computations…
NioBium
  • 583
  • 3
  • 10
11
votes
1 answer

Where can BangPatterns appear

From GHC user guide it seems like most Pat can be PBangPat, but there are a few exceptions. e.g. top-level bangs in a module (like !main) aren't allowed and x : !xs fails to parse x : (!xs) parses thanks @chi. What is the formal specification about…
rem
  • 893
  • 4
  • 18
1
2 3 4 5