無型λ計算の基本
informationprogramming-languageslambda-calculuslecture
1導入
この講義では、無型λ計算の構文とβ簡約を説明する。置換による変数捕獲を回避するため、自由変数とα同値をβ簡約より先に確認する。ここでは型判断を導入しない。
21. 構文と結合規則
変数の集合を可算無限集合とする。λ項 t は次の文法で帰納的に定義する。
t ::= x \mid \lambda x.t \mid t\;t
\lambda x.t は x を束縛するλ抽象、t\;s は適用である。適用は左結合であり、λ抽象の本体は可能な限り右へ延長する。したがって、t\;u\;v=(t\;u)\;v、\lambda x.t\;u=\lambda x.(t\;u) である。
32. 自由変数とα同値
自由変数集合 \mathrm{FV}(t) を次で定義する。
\mathrm{FV}(x)=\{x\},\qquad
\mathrm{FV}(\lambda x.t)=\mathrm{FV}(t)\setminus\{x\},\qquad
\mathrm{FV}(t\;u)=\mathrm{FV}(t)\cup\mathrm{FV}(u).
束縛変数の一貫した改名だけが異なる項をα同値とする。たとえば、\lambda x.x =_\alpha \lambda z.z である。ただし、z が本体で自由に出現する場合、x を z に改名できない。
43. 捕獲回避置換
t[x:=s] は、t における x の自由出現を s で置換する項である。x\notin\mathrm{FV}(t) なら t[x:=s]=t とする。変数と適用では構造に沿って定義し、λ抽象では次の条件を用いる。
(\lambda y.t)[x:=s]=
\begin{cases}
\lambda y.t & y=x,\\
\lambda y.(t[x:=s]) & y\ne x\ \text{かつ}\ y\notin\mathrm{FV}(s),\\
\lambda z.((t[y:=z])[x:=s]) & y\ne x\ \text{かつ}\ y\in\mathrm{FV}(s),
\end{cases}(\lambda y.t)[x:=s]=
\begin{cases}
\lambda y.t & y=x,\\
\lambda y.(t[x:=s]) & y\ne x\ \text{かつ}\ y\notin\mathrm{FV}(s),\\
\lambda z.((t[y:=z])[x:=s]) & y\ne x\ \text{かつ}\ y\in\mathrm{FV}(s),
\end{cases}
最後の場合では、t と s のどこにも出現せず、x とも異なる新しい変数(fresh variable)z を選択する。たとえば、(\lambda y.x)[x:=y] をそのまま \lambda y.y とすると、置換項の自由変数 y が捕獲される。先にα改名して、(\lambda y.x)[x:=y]=_\alpha(\lambda z.x)[x:=y]=\lambda z.y とする。
54. β簡約と正規形
(\lambda x.t)\;s という部分項をβ簡約基といい、捕獲回避置換によって次のように簡約する。
(\lambda x.t)\;s\to_\beta t[x:=s].
項の任意の位置にあるβ簡約基を一段簡約できる。β簡約基を含まない項をβ正規形という。
(\lambda x.\lambda y.x)\;a\;b
\to_\beta (\lambda y.a)\;b
\to_\beta a.
一方、\Omega=(\lambda x.x\;x)(\lambda x.x\;x) のβ簡約基は項全体だけであり、その簡約は \Omega\to_\beta\Omega に戻る。したがって、どの簡約列もβ正規形へ到達しない。無型λ計算では停止性を一般に保証できない。
65. 評価戦略
β簡約は簡約基の選択順を規定しない。最左外側の簡約基を選択する正規順序と、最左内側の簡約基を選択する適用順序では、停止挙動が異なりうる。
(\lambda x.a)\;\Omega
正規順序では外側を先に簡約して a に到達する。適用順序では \Omega の簡約を反復するため停止しない。これは戦略の停止性の差であり、β簡約規則そのものの差ではない。β正規形が存在する項について、正規順序はそれに到達する。
76. η変換との区別
x\notin\mathrm{FV}(f) のとき、\lambda x.f\;x=_\eta f とするη変換は、関数の外延的な同一視を表す。これは関数適用を実行するβ簡約とは別の規則である。
8まとめ
無型λ計算の計算は、α同値を考慮した捕獲回避置換によるβ簡約である。正規形の存在と、特定の評価戦略が停止することは区別する必要がある。
data/lecture/information/programming-languages/foundation/substitution-and-alpha-equivalence-basics.lecture.n.md
Basics of the Untyped Lambda Calculus
1Introduction
This lecture explains the syntax and beta-reduction of the untyped lambda calculus. It reviews free variables and alpha-equivalence before beta-reduction because substitution must avoid variable capture. No typing judgment is introduced here.
21. Syntax and association conventions
Let the set of variables be countably infinite. Lambda terms t are defined inductively by
t ::= x \mid \lambda x.t \mid t\;t.
The term \lambda x.t is a lambda abstraction that binds x, and t\;s is an application. Application associates to the left, and the body of an abstraction extends as far right as possible. Thus t\;u\;v=(t\;u)\;v and \lambda x.t\;u=\lambda x.(t\;u).
32. Free variables and alpha-equivalence
The set \mathrm{FV}(t) of free variables is defined by
\mathrm{FV}(x)=\{x\},\qquad
\mathrm{FV}(\lambda x.t)=\mathrm{FV}(t)\setminus\{x\},\qquad
\mathrm{FV}(t\;u)=\mathrm{FV}(t)\cup\mathrm{FV}(u).
Terms that differ only by a consistent renaming of bound variables are alpha-equivalent. For example, \lambda x.x =_\alpha \lambda z.z. A binder cannot, however, be renamed to z when z occurs free in its body.
43. Capture-avoiding substitution
The term t[x:=s] replaces every free occurrence of x in t with s. If x\notin\mathrm{FV}(t), then t[x:=s]=t. Its variable and application cases follow the term structure. The abstraction case is
(\lambda y.t)[x:=s]=
\begin{cases}
\lambda y.t & y=x,\\
\lambda y.(t[x:=s]) & y\ne x\ \text{and}\ y\notin\mathrm{FV}(s),\\
\lambda z.((t[y:=z])[x:=s]) & y\ne x\ \text{and}\ y\in\mathrm{FV}(s),
\end{cases}(\lambda y.t)[x:=s]=
\begin{cases}
\lambda y.t & y=x,\\
\lambda y.(t[x:=s]) & y\ne x\ \text{and}\ y\notin\mathrm{FV}(s),\\
\lambda z.((t[y:=z])[x:=s]) & y\ne x\ \text{and}\ y\in\mathrm{FV}(s),
\end{cases}
where the last case chooses a fresh variable z that differs from x and does not occur anywhere in either t or s. For example, directly producing \lambda y.y from (\lambda y.x)[x:=y] would capture the free y of the substituted term. Alpha-renaming first gives (\lambda y.x)[x:=y]=_\alpha(\lambda z.x)[x:=y]=\lambda z.y.
54. Beta-reduction and normal forms
A subterm of the form (\lambda x.t)\;s is a beta-redex. Capture-avoiding substitution contracts it as follows:
(\lambda x.t)\;s\to_\beta t[x:=s].
A beta-redex at any position in a term may be contracted in one step. A term containing no beta-redex is a beta-normal form.
(\lambda x.\lambda y.x)\;a\;b
\to_\beta (\lambda y.a)\;b
\to_\beta a.
In contrast, the only beta-redex of \Omega=(\lambda x.x\;x)(\lambda x.x\;x) is the whole term, and contracting it returns \Omega\to_\beta\Omega. Hence no reduction sequence reaches a beta-normal form. The untyped lambda calculus does not guarantee termination in general.
65. Evaluation strategies
Beta-reduction does not prescribe an order for selecting redexes. Normal order selects the leftmost outermost redex, whereas applicative order selects the leftmost innermost redex; the two strategies can have different termination behavior. Consider
(\lambda x.a)\;\Omega.
Normal order contracts the outer redex first and reaches a. Applicative order repeatedly reduces \Omega and does not terminate. This is a difference in strategy termination, not a difference in the beta-reduction rule. If a term has a beta-normal form, normal-order reduction reaches it.
76. Distinguishing eta-conversion
When x\notin\mathrm{FV}(f), the eta-conversion \lambda x.f\;x=_\eta f expresses extensional identification of functions. It is distinct from beta-reduction, which performs function application.
8Summary
Computation in the untyped lambda calculus is beta-reduction by capture-avoiding substitution, considered modulo alpha-equivalence. The existence of a normal form must be distinguished from termination under a particular evaluation strategy.
data/lecture/information/programming-languages/foundation/substitution-and-alpha-equivalence-basics.lecture.n.md