# Maintaining laziness

### From HaskellWiki

(partition) |
(Wadler's force function, if-then-else) |
||

Line 15: | Line 15: | ||

=== Maybe, Either, Exceptions === |
=== Maybe, Either, Exceptions === |
||

+ | |||

+ | Wadler's force function |
||

+ | |||

+ | The following looks cumbersome: |
||

+ | <haskell> |
||

+ | let (Just x) = y |
||

+ | in Just x |
||

+ | </haskell> |
||

+ | It looks like a complicated expression for <hask>y</hask>, |
||

+ | with an added danger of failing unrecoverably when <hask>y</hask> is not <hask>Just</hask>. |
||

+ | |||

+ | ... |
||

+ | |||

+ | parsers - leave Maybe where no Maybe is required |
||

=== Early decision === |
=== Early decision === |
||

+ | |||

+ | Another source of too much strictness is |
||

+ | Be aware that the following two expression are not equivalent. |
||

+ | <haskell> |
||

+ | -- less lazy |
||

+ | if b then f x else f y |
||

+ | -- more lazy |
||

+ | f (if b then x else y) |
||

+ | </haskell> |
||

+ | It is <hask>if undefined then f x else f y</hask> is <hask>undefined</hask>, |
||

+ | whereas <hask>f (if b then x else y)</hask> if <hask>f undefined</hask>, |
||

+ | which is a difference in [[non-strict semantics]]. |
||

+ | Consider e.g. <hask>if b then 'a':x else 'a':y</hask>. |
||

+ | |||

+ | pattern match on (,) is better than pattern match on (:), because the first one has no alternative constructor |
||

+ | laziness encoded in uncurry |
||

if then else |
if then else |

## Revision as of 17:59, 28 December 2008

One of Haskell's main features is non-strict semantics, which in is implemented by lazy evaluation in all popular Haskell compilers. However many Haskell libraries found on Hackage are implemented just as if Haskell would be a strict language. This leads to unnecessary inefficiencies, memory leaks and, we suspect, unintended semantics. In this article we want to go through some techniques on how to check lazy behaviour on functions, examples of typical constructs which break laziness without need, and finally we want to link to techniques that may yield the same effect without laziness.

## Contents |

## 1 Checking laziness

undefined, cycles

unit tests

## 2 Laziness breakers

### 2.1 Maybe, Either, Exceptions

Wadler's force function

The following looks cumbersome:

let (Just x) = y in Just x

...

parsers - leave Maybe where no Maybe is required

### 2.2 Early decision

Another source of too much strictness is Be aware that the following two expression are not equivalent.

-- less lazy if b then f x else f y -- more lazy f (if b then x else y)

which is a difference in non-strict semantics.

Consider e.g.pattern match on (,) is better than pattern match on (:), because the first one has no alternative constructor

laziness encoded in uncurry

if then else

state monad

reader monad

### 2.3 Strict pattern matching in a recursion

The implementation of theWhat happened? The reason was a too strict pattern matching.

Consider the following correct implementation:

partition :: (a -> Bool) -> [a] -> ([a], [a]) partition p = foldr (\x ~(y,z) -> if p x then (x : y, z) else (y, x : z)) ([],[])

...

### 2.4 List reversal

Any use of the list functionsince when you access the first element of a reversed list, then all nodes of the input list must be evaluated and stored in memory. Think twice whether it is really needed. The article Infinity and efficiency shows how to avoid list reversal.

## 3 Alternatives

From the above issues you see that it laziness is a fragile thing. Only one moment where you do not pay attention and a function, carefully developed with laziness in mind, is no longer lazy, when you call it. The type system can almost not help you hunting laziness breakers and there is little support by debuggers. Thus detection of laziness breakers, often requires understanding of a large portion of code, which is against the idea of modularity. Maybe for your case you might prefer a different idiom, that achieves the same goals in a safer way. See e.g. the Enumerator and iteratee pattern.