-
Notifications
You must be signed in to change notification settings - Fork 5
Scoping
This is the third of the four main algorithms discussed on the Algorithms page.
Before explaining how it works, we must begin with several definitions.
Definition (Declaration): A Declaration is a type of Output Expression that attempts to declare a set of identifiers as having their scope begin at the point in the Output Tree where the Declaration sits. Declarations can come in one of two types, constant declarations and variable declarations. These function differently in some validation and scoping algorithms, but both are intended to take previously undeclared identifiers and mark them as declared.
We do not fully specify what the interior configuration of a Declaration Structure must be at this point. We simply assume that they will contain at least enough information to specify the list of identifiers they are attempting to declare and whether they declare them as constants or variables. Other contents are also possible.
Definition (Valid/Invalid Declaration): A Declaration
Let us consider an example Structure hierarchy containing several declarations.
- The document root
- An Output Structure
$S_1$ - An Output Structure
$S_2$ - A Declaration
$S_3$ of identifiers$x,y,z$ as variables - An Output Structure
$S_4$ - A Declaration
$S_5$ of the identifier$c$ as a constant - An Output Structure
$S_6$ - A Declaration
$S_7$ of the identifiers$w,x$ as variables
- A Declaration
- An Output Structure
- A Declaration
$S_8$ of the identifiers$w,x$ as constants
- An Output Structure
The Declaration of
The Declaration of
The Declaration of
The Declaration of
Both declarations in
Definition (Explicit Declaration): A variable or constant declared by a Declaration Structure, as defined above, is said to be explicitly declared, in contrast to the new method of variable declaration we introduce below.
In particular, all declarations in Example 1 above are explicit.
The following definition assumes that Output Structures may bind variables, and that the usual definitions of free and bound variables apply in such Structure trees.
Definition (Implicit Declaration): A variable (not a constant)
It may help to view implicit declarations as if they were invisible explicit declarations inserted as the first child of the parent structure
Consider the same Structure hierarchy in Example 1, but with three modifications, marked with arrows below.
- The document root
- An Output Structure
$S_1$ - An Output Expression
$S_2$ , the statement$\forall x,z>x$ ⬅️ - A Declaration
$S_3$ of identifiers$x,y,z$ as variables - An Output Expression
$S_4$ , the equation$2+k=15$ ⬅️ - A Declaration
$S_5$ of the identifier$c$ as a constant - An Output Structure
$S_6$ - A Declaration
$S_7$ of the identifiers$w,x$ as variables
- A Declaration
- An Output Expression
- A Declaration
$S_8$ of the identifiers$w,x$ as constants - An Output Expression
$S_9$ ,$\forall x, x+1>x$ ⬅️
- An Output Structure
All Declarations would be marked valid or invalid in exactly the same way as they were in Example 1, with one exception: The declaration of
There is no change to how
Note that an implicit variable declaration can never be invalid. Furthermore, if a structure
Definition (scope of an identifier): Given any identifier
- If the identifier is bound, then there are two possibilities:
- If the identifier is validly declared to be a constant by some Declaration
$D\prec S$ , then the scope of$x$ is that given by$D$ and the attempt in$S$ to bind$x$ is invalid. - Otherwise,
$x$ is a variable and its scope is the scope of the quantifier that binds it.
- If the identifier is validly declared to be a constant by some Declaration
- If the identifier is free, then there are two possibilities:
- If it is validly declared by some Declaration
$D\prec S$ , then$x$ is either a variable or constant, as given by$D$ , and its scope is also that given by$D$ . - If it is not validly declared by some Declaration
$D\prec S$ , then by the definition of Implicit Declaration, it is implicitly declared and its scope is as given in that definition.
- If it is validly declared by some Declaration
The Scoping phase takes as input a newly created Output Tree and marks each Structure within it in the following ways. (The example that follows may help illuminate these principles.)
- If the Structure is not an Output Expression (and thus probably functions as some piece of document or proof structure), then it will be given an attribute called its "implicit declarations" list. This will contain the list of identifiers implicitly declared within that Structure. This list will often be empty, for Structures that contain no children that implicitly declare variables by using them free.
- If the Structure is a Declaration, then it will be marked with a list of "declaration failures," that is, the subset of the identifiers that it invalidly declares.
- If the Structure is an Expression, then it will be marked with a list of "binding failures," that is, the subset of the identifiers that it attempts to bind with a quantifier (or other binding expression) and yet that are explicitly declared as constants by some Declaration accessible to the Expression.
If we marked all scopes according to these rules in the Structure hierarchy shown in Example 2, we would get the following results. Here we use JSON-like notation for lists of identifiers.
| Structure | Implicit Declarations | Declaration Failures | Binding Failures |
|---|---|---|---|
[ "z" ] |
[ ] |
[ ] |
|
[ ] |
[ ] |
[ ] |
|
[ ] |
[ "z" ] |
[ ] |
|
[ ] |
[ ] |
[ ] |
|
[ ] |
[ ] |
[ ] |
|
[ ] |
[ ] |
[ ] |
|
[ ] |
[ "x" ] |
[ ] |
|
[ ] |
[ ] |
[ ] |
|
[ ] |
[ ] |
[ "x" ] |
We say that a structure "successfully declares" or "successfully" binds an identifier if it attempts to declare it and that attempt is not marked as a failure by the marking procedure given above.
We do not describe the marking algorithm in detail here, but there is a rather straightforward implementation of it. While that implementation is slightly fiddly, it does not require great invention to create.
Not seeing typeset math? GitHub doesn't render MathJax; try a browser plugin.
Main sections:
More to come!