markdown
束縛と自由変数の基本md 283170c
lecture/information/programming-languages/foundation/variable-binding-and-free-variables-basics.lecture.n.md
Download PDF

束縛そくばくと自由変数じゆうへんすうの基本きほん

1導入どうにゅう

この講義こうぎでは、λ項こうの構文こうぶんを定義ていぎし、変数へんすうの各かく出現しゅつげんを束縛出現そくばくしゅつげんまたは自由出現じゆうしゅつげんに分類ぶんるいする。変数名へんすうめいだけでは分類ぶんるいできず、その出現位置しゅつげんいちと束縛子そくばくしの作用域さよういきが必要ひつようである。

2構文こうぶんと構文木こうぶんぎ

可算無限かさんむげんな変数集合へんすうしゅうごうを [PARSE ERROR: Undefined("Command(\"mathcal\")")]V とする。型かたなしλ項こうの集合しゅうごう Λ は次つぎの生成規則せいせいきそくで再帰的さいきてきに定義ていぎする。

M::=x∣(MN)∣(λx.M)(x∈[PARSE ERROR: Undefined("Command(\"mathcal\")")]V)

x は変数へんすう、MN は適用てきよう、λx.M はλ抽象ちゅうしょうである。括弧かっこを省略しょうりゃくするとき、適用てきようは左結合ひだりけつごうとし、λ抽象ちゅうしょうの本体ほんたいは可能かのうな限かぎり右みぎまで延長えんちょうする。したがって MNP=(MN)P であり、λx.MN=λx.(MN) である。

3束縛子そくばくし・作用域さよういき・出現しゅつげん

λx.M の λx を x の束縛子そくばくしbinder、部分項ぶぶんこう M をその作用域さよういきscopeという。構文木こうぶんぎにおける変数へんすう x の葉はの出現しゅつげんが、ある λx の本体ほんたいに含ふくまれるとき、その出現しゅつげんは束縛そくばくされる。該当がいとうする束縛子そくばくしが複数ふくすうある場合ばあいは、構文木上こうぶんぎじょうで最もっとも近ちかい束縛子そくばくしがその出現しゅつげんを束縛そくばくする。いずれの λx の作用域さよういきにも属ぞくさない x の出現しゅつげんは自由じゆうである。

「束縛そくばくされる」という性質せいしつは変数名へんすうめいでなく出現しゅつげんに属ぞくする。例たとえば

x(λx.x)

では、左端さたんの x は自由出現じゆうしゅつげんであり、右端うたんの x は内側うちがわの λx による束縛出現そくばくしゅつげんである。また

λx.((λx.x)x)

では、内側うちがわの本体ほんたいにある x は内側うちがわの束縛子そくばくしに、最後さいごの x は外側そとがわの束縛子そくばくしに束縛そくばくされる。この現象げんしょうを遮蔽しゃへいshadowingという。

4自由変数集合じゆうへんすうしゅうごう

FV(M) は M に自由出現じゆうしゅつげんを持もつ変数名へんすうめいの集合しゅうごうであり、構文こうぶんに沿そって次つぎのように定義ていぎする。

FV(x)={x}
FV(MN)=FV(M)∪FV(N)
FV(λx.M)=FV(M)∖{x}

例たとえば、

FV(λx.xy)={y},FV(λx.λy.xy)=[PARSE ERROR: Undefined("Command(\"varnothing\")")].

FV(M)=[PARSE ERROR: Undefined("Command(\"varnothing\")")] である項こうを閉項へいこうclosed term、それ以外いがいを開項かいこうopen termという。閉項へいこうであることは、変数へんすうの各出現かくしゅつげんがいずれかの束縛子そくばくしに束縛そくばくされることと同値どうちである。

5確認かくにん

M=λx.(x(λy.xy)z) では、二ふたつの x の出現しゅつげんは外側そとがわの λx に束縛そくばくされ、y の出現しゅつげんは λy に束縛そくばくされ、z の出現しゅつげんだけが自由じゆうである。したがって 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 [PARSE ERROR: Undefined("Command(\"mathcal\")")]V be a countably infinite set of variables. The set Λ of untyped lambda terms is defined recursively by the grammar

M::=x∣(MN)∣(λx.M)(x∈[PARSE ERROR: Undefined("Command(\"mathcal\")")]V).

Here x is a variable, MN is an application, and λ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 MNP=(MN)P and λx.MN=λx.(MN).

3Binders, scopes, and occurrences

In λx.M, the prefix λ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 λ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 λx is free.

Boundness is a property of an occurrence, not merely of a variable name. For example, in

x(λx.x),

the leftmost occurrence of x is free, whereas the rightmost occurrence is bound by the inner λx. In

λx.((λ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

FV(M) is the set of variable names that have at least one free occurrence in M. It is defined structurally by

FV(x)={x},
FV(MN)=FV(M)∪FV(N),
FV(λx.M)=FV(M)∖{x}.

For example,

FV(λx.xy)={y},FV(λx.λy.xy)=[PARSE ERROR: Undefined("Command(\"varnothing\")")].

A term M is closed if FV(M)=[PARSE ERROR: Undefined("Command(\"varnothing\")")] and open otherwise. A term is closed exactly when every variable occurrence is bound by some binder.

5Check

In M=λx.(x(λy.xy)z), both occurrences of x are bound by the outer λx, the occurrence of y is bound by λy, and only the occurrence of z is free. Hence 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
raw .n.md をコピー
loc をコピー (filepath:line ~ line)
copy share link
copy encoded share link
path をコピー
copy share link
copy encoded share link
copy share link
copy encoded share link
タブを全て閉じる