Non-local variable
Encyclopedia
In programming language theory
, a non-local variable is a variable that is not defined in the local scope. While the term can refer to global variables, it is primarily used in the context of nested
and anonymous function
s where some variables can be neither in the local nor the global scope.
In the Haskell example that follows the variable
and thereby first-class function
s in a programming language.
If the nested function or functions are (mutually) recursive
, it becomes hard for the compiler
to know exactly where on the stack
the non-local variable was allocated, as the frame pointer only points to the local variable of the nested function itself and there can be an arbitrary number of activation records on the stack in between. This is generally solved using access links or display registers.
If the nested function is passed as an argument to a higher-order function a closure
needs to be built in order to locate the non-local variables. If the nested function is returned as a result from its outer function (or stored in a variable) the non-local variables will no longer be available on the stack. They need to be heap allocated instead, and their lifetime extend beyond the lifetime of the outer function that declared and allocated them. This generally requires garbage-collection.
Programming language theory
Programming language theory is a branch of computer science that deals with the design, implementation, analysis, characterization, and classification of programming languages and their individual features. It falls within the discipline of computer science, both depending on and affecting...
, a non-local variable is a variable that is not defined in the local scope. While the term can refer to global variables, it is primarily used in the context of nested
Nested function
In computer programming, a nested function is a function which is lexically encapsulated within another function. It can only be called by the enclosing function or by functions directly or indirectly nested within the same enclosing function. In other words, the scope of the nested function is...
and anonymous function
Anonymous function
In programming language theory, an anonymous function is a function defined, and possibly called, without being bound to an identifier. Anonymous functions are convenient to pass as an argument to a higher-order function and are ubiquitous in languages with first-class functions such as Haskell...
s where some variables can be neither in the local nor the global scope.
Example
In the Python 3 example that follows there is a nested functioninner
defined in the scope of another function outer
. The variable x
is local to outer
, but non-local to inner
(nor is it global):In the Haskell example that follows the variable
c
is non-local in the anonymous function \x -> x + c
:Implementation issues
Non-local variables are the primary reason it is difficult to support nested, anonymous, higher-orderHigher-order function
In mathematics and computer science, higher-order functions, functional forms, or functionals are functions which do at least one of the following:*take one or more functions as an input*output a function...
and thereby first-class function
First-class function
In computer science, a programming language is said to have first-class functions if it treats functions as first-class objects. Specifically, this means that the language supports passing functions as arguments to other functions, returning them as the values from other functions, and assigning...
s in a programming language.
If the nested function or functions are (mutually) recursive
Recursive
Recursive may refer to:*Recursion, the technique of functions calling themselves*Recursive function, a total computable function*Recursive language, a language which is decidable...
, it becomes hard for the compiler
Compiler
A compiler is a computer program that transforms source code written in a programming language into another computer language...
to know exactly where on the stack
Stack
-Mathematics:* Stack , general category-theoretical concept to formalise "pull-back" operations in geometry and algebra* Algebraic stack, a generalisation of scheme and algebraic space in algebraic geometry; a specific type of the above-Computers:...
the non-local variable was allocated, as the frame pointer only points to the local variable of the nested function itself and there can be an arbitrary number of activation records on the stack in between. This is generally solved using access links or display registers.
If the nested function is passed as an argument to a higher-order function a closure
Closure (computer science)
In computer science, a closure is a function together with a referencing environment for the non-local variables of that function. A closure allows a function to access variables outside its typical scope. Such a function is said to be "closed over" its free variables...
needs to be built in order to locate the non-local variables. If the nested function is returned as a result from its outer function (or stored in a variable) the non-local variables will no longer be available on the stack. They need to be heap allocated instead, and their lifetime extend beyond the lifetime of the outer function that declared and allocated them. This generally requires garbage-collection.