束縛と自由変数の基本
informationprogramming-languageslambda-calculuslecture
1導入
この講義では、λ項の構文を定義し、変数の各出現を束縛出現または自由出現に分類する。変数名だけでは分類できず、その出現位置と束縛子の作用域が必要である。
2構文と構文木
可算無限な変数集合を \mathcal V とする。型なしλ項の集合 \Lambda は次の生成規則で再帰的に定義する。
M ::= x \mid (M\;N) \mid (\lambda x.M) \qquad (x\in\mathcal V)
x は変数、M\;N は適用、\lambda x.M はλ抽象である。括弧を省略するとき、適用は左結合とし、λ抽象の本体は可能な限り右まで延長する。したがって M\;N\;P=(M\;N)\;P であり、\lambda x.M\;N=\lambda x.(M\;N) である。
3束縛子・作用域・出現
\lambda x.M の \lambda x を x の束縛子、部分項 M をその作用域という。構文木における変数 x の葉の出現が、ある \lambda x の本体に含まれるとき、その出現は束縛される。該当する束縛子が複数ある場合は、構文木上で最も近い束縛子がその出現を束縛する。いずれの \lambda x の作用域にも属さない x の出現は自由である。
「束縛される」という性質は変数名でなく出現に属する。例えば
x\;(\lambda x.x)
では、左端の x は自由出現であり、右端の x は内側の \lambda x による束縛出現である。また
\lambda x.((\lambda x.x)\;x)
では、内側の本体にある x は内側の束縛子に、最後の x は外側の束縛子に束縛される。この現象を遮蔽という。
4自由変数集合
\mathrm{FV}(M) は M に自由出現を持つ変数名の集合であり、構文に沿って次のように定義する。
\mathrm{FV}(x)=\{x\}
\mathrm{FV}(M\;N)=\mathrm{FV}(M)\cup\mathrm{FV}(N)
\mathrm{FV}(\lambda x.M)=\mathrm{FV}(M)\setminus\{x\}
例えば、
\mathrm{FV}(\lambda x.x\;y)=\{y\},\qquad
\mathrm{FV}(\lambda x.\lambda y.x\;y)=\varnothing.
\mathrm{FV}(M)=\varnothing である項を閉項、それ以外を開項という。閉項であることは、変数の各出現がいずれかの束縛子に束縛されることと同値である。
5確認
M=\lambda x.(x\;(\lambda y.x\;y)\;z) では、二つの x の出現は外側の \lambda x に束縛され、y の出現は \lambda y に束縛され、z の出現だけが自由である。したがって \mathrm{FV}(M)=\{z\} である。次の講義では、この出現ごとの分類を用いて捕縛を回避する置換を定義する。
6関連リンク
data/lecture/information/programming-languages/foundation/introduction-to-functional-programming.lecture.n.md
data/lecture/information/programming-languages/foundation/substitution-and-alpha-equivalence-basics.lecture.n.md
Basics of Binding and Free Variables
1Introduction
This lecture defines the syntax of lambda terms and classifies every variable occurrence as bound or free. A name alone does not determine the classification; the occurrence's position and the scope of its binder are essential.
2Syntax and syntax trees
Let \mathcal V be a countably infinite set of variables. The set \Lambda of untyped lambda terms is defined recursively by the grammar
M ::= x \mid (M\;N) \mid (\lambda x.M) \qquad (x\in\mathcal V).
Here x is a variable, M\;N is an application, and \lambda x.M is an abstraction. When parentheses are omitted, application associates to the left and the body of an abstraction extends as far to the right as possible. Thus M\;N\;P=(M\;N)\;P and \lambda x.M\;N=\lambda x.(M\;N).
3Binders, scopes, and occurrences
In \lambda x.M, the prefix \lambda x is a binder for x, and the subterm M is its scope. An occurrence of x at a leaf of the syntax tree is bound if it lies inside the body of some \lambda x. If several such binders enclose the occurrence, the nearest one in the syntax tree binds it. An occurrence of x that lies in the scope of no \lambda x is free.
Boundness is a property of an occurrence, not merely of a variable name. For example, in
x\;(\lambda x.x),
the leftmost occurrence of x is free, whereas the rightmost occurrence is bound by the inner \lambda x. In
\lambda x.((\lambda x.x)\;x),
the occurrence in the inner body is bound by the inner binder, while the final occurrence is bound by the outer binder. This phenomenon is called shadowing.
4The set of free variables
\mathrm{FV}(M) is the set of variable names that have at least one free occurrence in M. It is defined structurally by
\mathrm{FV}(x)=\{x\},
\mathrm{FV}(M\;N)=\mathrm{FV}(M)\cup\mathrm{FV}(N),
\mathrm{FV}(\lambda x.M)=\mathrm{FV}(M)\setminus\{x\}.
For example,
\mathrm{FV}(\lambda x.x\;y)=\{y\},\qquad
\mathrm{FV}(\lambda x.\lambda y.x\;y)=\varnothing.
A term M is closed if \mathrm{FV}(M)=\varnothing and open otherwise. A term is closed exactly when every variable occurrence is bound by some binder.
5Check
In M=\lambda x.(x\;(\lambda y.x\;y)\;z), both occurrences of x are bound by the outer \lambda x, the occurrence of y is bound by \lambda y, and only the occurrence of z is free. Hence \mathrm{FV}(M)=\{z\}. The next lecture uses this occurrence-level classification to define substitution that avoids capture.
6Related links
data/lecture/information/programming-languages/foundation/introduction-to-functional-programming.lecture.n.md
data/lecture/information/programming-languages/foundation/substitution-and-alpha-equivalence-basics.lecture.n.md