ezyang's blog

the arc of software bends towards understanding

Evaluation on the Haskell Heap

New to the series? Go to the beginning.

image

The Ghost of the Christmas Present

In today’s post, we’ll do a brief survey of the various things that can happen when you open haunted presents in the Haskell heap. Asides from constants and things that have already been evaluated, mostly everything on the Haskell heap is haunted. The real question is what the ghost haunting the present does.

In the simplest case, almost nothing!

image

Unlike gift-cards, you have to open the next present (Haskell doesn’t let you evaluate a thunk, and then decide not to follow the indirection…)

image

More commonly, the ghost was lazy and, when woken up, has to open other presents to figure out what was in your present in the first place!

image

Simple primitive operations need to open all of the presents involved.

image

But the ghost may also open another present for no particular reason…

image

or execute some IO…

image

Note that any presents he opens may trigger more ghosts:

image

Resulting in a veritable ghost jamboree, all to open one present!

image

The fact that opening a present (thunk) can cause such a cascading effect is precisely what makes the timing of lazy evaluation surprising to people who are used to all of the objects in their heap being unwrapped (evaluated) already. So the key to getting rid of this surprise is understanding when a ghost will decide it needs to unwrap a present (strictness analysis) and whether or not your presents are unwrapped already (amortized analysis).

Last time: The Haskell Heap

Next time: IO evaluates the Haskell Heap

image

This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.

1 Comment

  1. gasche
    I’m finding your post a bit light on the technical content, but the drawings are so enjoyable that you are forgiven.