concept thunk in category haskell

appears as: thunks, thunk, thunks
Haskell in Depth

This is an excerpt from Manning's book Haskell in Depth.

Note that I’m using the strict modifyIORef' for the counter: incrementing is both frequent and simple so we definitely don’t want to use laziness and build thunks here.

  • Thunks are very close to functions. They are used to represent code that should be executed in order to evaluate an expression. As such they represent unevaluated expressions. Their payload contains references to free variables of the expression and their entry code in the closure’s description is a code to evaluate the corresponding expression.
  • One of the main features of thunks as closures is an ability to be updated when the value is evaluated. In that case, a thunk’s closure changes its type and becomes an indirection, a closure, which references another closure containing a result of an evaluation.

    Haskell laziness effectively boils down to creating thunks instead of evaluating expressions in the first place. The evaluation itself is then followed in the form of an updating thunk with an indirection to the results of evaluation when they are needed.