Non-local variable

Non-local variable

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 functions where some variables can be neither in the local nor the global scope.

Example

In the example that follows there is a nested function inner defined in the scope of another function outer. The variable x is local to outer, but non-local to inner (nor is it global):

def outer():
    x = 1
    def inner():
        nonlocal x
        x += 1
        print(x)
    return inner

In the next example the variable c is non-local in the anonymous function \x -> x + c:

outer = let c = 1 in map (\x -> x + c) [1, 2, 3, 4, 5]

Implementation issues

Non-local variables are the primary reason it is difficult to support nested, anonymous, higher-order and thereby first-class functions 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.

References

  • Aho, Lam, Sethi, and Ullman. "7.3 Access to Nonlocal Data on the Stack". Compilers: Principles, Techniques, & Tools. Second edition.

Wikimedia Foundation. 2010.

Игры ⚽ Поможем решить контрольную работу

Look at other dictionaries:

  • Local hidden variable theory — In quantum mechanics, a local hidden variable theory is one in which distant events are assumed to have no instantaneous (or at least faster than light) effect on local ones. According to the quantum entanglement theory of quantum mechanics, on… …   Wikipedia

  • Variable — A variable (pronEng|ˈvɛərɪəbl) is an attribute of a physical or an abstract system which may change its value while it is under observation. Examples include the height of a child, the temperature across a state, or the input to a function. This… …   Wikipedia

  • Variable cachée — En physique quantique, le terme de variable cachée désigne des paramètres physiques hypothétiques qui ne seraient pas pris en compte par les postulats de la mécanique quantique, soit dans la définition de l état quantique, ou dans l évolution… …   Wikipédia en Français

  • Non-linear least squares — is the form of least squares analysis which is used to fit a set of m observations with a model that is non linear in n unknown parameters (m > n). It is used in some forms of non linear regression. The basis of the method is to… …   Wikipedia

  • Local independence — is the underlying assumption of latent variable models. The observed items are independent of each other given an individual score on the latent variable(s). This means that the latent variable explains why the observed items are related to… …   Wikipedia

  • Non-line-of-sight propagation — Non line of sight (NLOS) or near line of sight is a term used to describe radio transmission across a path that is partially obstructed, usually by a physical object in the innermost Fresnel zone. Many types of radio transmissions depend, to… …   Wikipedia

  • Non-tariff barriers to trade — (NTBs) are trade barriers that restrict imports but are not in the usual form of a tariff. Some common examples of NTB s are anti dumping measures and countervailing duties, which, although they are called non tariff barriers, have the effect of… …   Wikipedia

  • Local consistency — In constraint satisfaction, local consistency conditions are properties of constraint satisfaction problems related to the consistency of subsets of variables or constraints. Several such conditions exist, the most known being node consistency,… …   Wikipedia

  • Non-equilibrium thermodynamics — Thermodynamics …   Wikipedia

  • Local search (constraint satisfaction) — In constraint satisfaction, local search is an incomplete method for finding a solution to a problem. It is based on iteratively improving an assignment of the variables until all constraints are satisfied. In particular, local search algorithms… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”