IT lexicon › Programming › Deforestation

Deforestation

Programming På svenska → Updated: 2026-11-07

The program transformation that removes intermediate lists and trees one function builds only for the next to take apart immediately.

Functional code likes to compose small steps: sum (map f (filter p xs)) allocates two complete lists on the heap that nobody needs to keep. Deforestation rewrites the expression into a single loop that produces and consumes elements in lockstep. Philip Wadler named it in 1988 — the trees are the forest being cut down — but his algorithm only worked on a restricted program form.

What caught on was short-cut fusion: GHC's foldr/build rule from 1993, where library list functions are written so a single rewrite rule can merge producer and consumer. Stream fusion in 2007 extended it to more functions. The downside is fragility; if inlining happens in the wrong order, the rule never fires and the program quietly gets slower. Rust's iterators get the same effect by design rather than by optimization.

← Back to the lexicon