Definition
In Type Theory and programming languages, the scope of a variable binding is the region of an Expression where that binding is active and the variable can be referenced.
Scope determines which bindings are visible at each point in a program and how variable names are resolved.
Lexical Scope
Most type theories use lexical scope (or static scope), where the scope of a binding is determined by the syntactic structure of the expression.
In lexical scope, a variable refers to the nearest enclosing binding in the program text.
Scope in Lambda Calculus
In the expression , the scope of the binding for is the body . Within , occurrences of refer to the lambda-bound variable.
Example: In :
- The scope of the outer binding is
- The scope of the binding is
- Both bindings are active in the expression
Shadowing
When a variable is bound in a scope that already contains a binding for the same name, the inner binding shadows the outer one.
Example: In :
- The inner shadows the outer binding
- Within , the name refers to the inner binding
- The outer binding is temporarily hidden but not destroyed
Scope in Dependent Types
In dependent types, scope becomes more complex because types can depend on values.
In a Dependent Product , the variable is in scope in the type expression .
In a Dependent Pair , the variable is similarly in scope in .
Scope and Typing Context
In type theory, scope is formalized through the typing context . The judgment:
states that has type under context , which lists all variables in scope and their types.
Extending the context with a new binding is written , bringing into scope for subsequent judgments.
Fresh Variables
When performing operations like Substitution or α-conversion, it is often necessary to choose fresh variables—names that are not already in scope—to avoid unintended variable capture.
Related Concepts
- Bound Variable: Variables whose scope is determined by a binding construct
- Free Variable: Variables that appear outside the scope of their binding
- Substitution: An operation that must respect scope to avoid variable capture
- Lambda Calculus: The foundational system with explicit scoping rules
- Expression: The context in which scope is determined
- Type Theory: The formal system using scope and contexts