Lexical Scope and Closure Environments
Follow a function value after it leaves its defining block and determine which binding each free variable uses.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 2
- Reading time
- 1 min
In this article2 chapters
A closure carries code and access to its free variables
Consider a block that binds \(x=10\) and returns a function of \(y\) computing \(x+y\). Calling that result later with \(3\) gives \(13\), even if the caller has another \(x\) bound to \(100\). The free occurrence refers to the environment of the function’s definition. A closure pairs executable code with the relevant environment. Shadowing in the caller does not change that pair.
A captured binding may denote a location
If the source permits mutation, capture can preserve a shared cell rather than a copied value. Two functions can then observe successive writes to a captured counter. Copying the cell’s first content into each closure would change behavior. Closure conversion makes the environment explicit in an intermediate representation. Big-step semantics describes application using that stored environment rather than the caller’s bindings.[1][2]