What is Lexical Scoping?
With small variations, lexical (or ‘static’) scoping is ubiquitous (though not universal) among successful languages. Every programmer is probably familiar with the way it works. The basic idea is that a compiler can link uses of unqualified names to definitions statically (based on the ‘lexical’ form of the program, not its running instantiation). This allows for implementations which can access variables in a small number of memory reference instructions (but it does not require them). Another key idea is that scopes nest, and scope searching (which is at compile time) is performed in a linear manner, from the point of use outward through enclosing scopes. This linear idea may be extended by importation of modules or inheritance of super types or dynamic linking of globals into particular scopes; generally this is least confusing when done on outer scopes, where there is little danger of shadowing nearby variables and surprising the user. Beyond simplifying specifications and allowin