MLton

A closure is a data structure that is the run-time representation of a function.

Typical Implementation

In a typical implementation, a closure consists of a code pointer (indicating what the function does) and an environment containing the values of the free variables of the function. For example, in the expression

let
   val x = 5
in
   fn y => x + y
end

the closure for fn y => x + y contains a pointer to a piece of code that knows to take its argument and add the value of x to it, plus the environment recording the value of x as 5.

To call a function, the code pointer is extracted and jumped to, passing in some agreed upon location the environment and the argument.

MLton’s Implementation

MLton does not implement closures traditionally. Instead, based on whole-program higher-order control-flow analysis, MLton represents a function as an element of a sum type, where the variant indicates which function it is and carries the free variables as arguments. See ClosureConvert and CejtinEtAl00 for details.