TCS: Lambda Calculus

Lambda Calculus

Terms and Binding

The untyped λ\lambda-calculus builds programs from variables, function abstraction and function application. Functions are themselves values, so a function can receive another function, return one, or be applied to itself

Let V\mathcal V be a countable set of variables. The set Λ\Lambda of terms is generated by

M::=x∣(λx.M)∣(MN)M ::= x \mid (\lambda x.M) \mid (MN)

Here λx.M\lambda x.M binds xx in its body MM, and MNMN applies MM to NN

These are inductive rules: Λ\Lambda contains exactly the terms obtainable by finitely many applications of the constructors. This gives a structural induction principle: prove a property for variables, and show that abstraction and application preserve it

The corresponding inference rules can be written with the premises above a horizontal line

x∈Λ  (x∈V)M∈Λλx.M∈ΛM∈ΛN∈ΛMN∈Λ\frac{}{x\in\Lambda}\;(x\in\mathcal V) \qquad \frac{M\in\Lambda}{\lambda x.M\in\Lambda} \qquad \frac{M\in\Lambda\quad N\in\Lambda}{MN\in\Lambda}

A rule with no premises is an axiom. When additional conditions appear beside a rule, they must also hold before the rule can be applied

Subterms

The immediate subterms of MNMN are MM and NN, and the immediate subterm of λx.M\lambda x.M is MM. All subterms are obtained recursively, including the whole term itself

Abbreviations

The fully parenthesized syntax makes the structure explicit, but we usually shorten terms using the following conventions

  1. Outermost parentheses may be omitted: (MN)(MN) can be written as MNMN
  2. Application associates to the left: MNPMNP means (MN)P(MN)P, rather than M(NP)M(NP)
  3. The body of an abstraction extends as far to the right as possible: λx.MN\lambda x.MN means λx.(MN)\lambda x.(MN), rather than (λx.M)N(\lambda x.M)N
  4. Consecutive abstractions may share one λ\lambda: λxyz.M\lambda xyz.M abbreviates λx.(λy.(λz.M))\lambda x.(\lambda y.(\lambda z.M))
  5. An abstraction on the right of an application may omit its surrounding parentheses: Mλx.NM\lambda x.N abbreviates M(λx.N)M(\lambda x.N)

For example, combining these conventions gives

zλxy.xz≡z(λx.(λy.(xz)))z\lambda xy.xz \equiv z(\lambda x.(\lambda y.(xz)))

Parentheses remain necessary in (λx.M)N(\lambda x.M)N: removing them would put NN inside the abstraction’s body and change the term

Free Variables

The free-variable function is defined structurally

FV⁡(x)={x}FV⁡(MN)=FV⁡(M)∪FV⁡(N)FV⁡(λx.M)=FV⁡(M)∖{x}\begin{aligned}\operatorname{FV}(x)&=\{x\}\\\operatorname{FV}(MN)&=\operatorname{FV}(M)\cup\operatorname{FV}(N)\\\operatorname{FV}(\lambda x.M)&=\operatorname{FV}(M)\setminus\{x\}\end{aligned}

A term is closed, or a combinator, when its free-variable set is empty

Binding is a property of an occurrence. In (λx.x)y(\lambda x.x)y, the occurrence of xx in the body is bound, while yy is free. In x(λx.x)x(\lambda x.x), the first xx is free and the last xx is bound

Some useful closed terms are

I=λx.xK=λxy.xS=λxyz.xz(yz)\begin{aligned} \mathbf I&=\lambda x.x\\ \mathbf K&=\lambda xy.x\\ \mathbf S&=\lambda xyz.xz(yz) \end{aligned}

Alpha-Conversion and Substitution

Renaming Bound Variables

The choice of a bound variable’s name carries no computational information

λx.x≡αλu.u\lambda x.x\equiv_\alpha\lambda u.u

An α\alpha-conversion must preserve binding. For example, λx.y\lambda x.y cannot be renamed to λy.y\lambda y.y: the free yy would become bound

We normally identify terms up to α\alpha-equivalence and rename bound variables to fresh names whenever necessary

Capture-Avoiding Substitution

Write M[N/x]M[N/x] for the term obtained by replacing the free occurrences of xx in MM with NN

x[N/x]=Ny[N/x]=y(y≠x)(MP)[N/x]=M[N/x]P[N/x](λx.M)[N/x]=λx.M(λy.M)[N/x]=λy.M[N/x](y≠x, y∉FV⁡(N))\begin{aligned}x[N/x]&=N\\y[N/x]&=y\qquad(y\ne x)\\(MP)[N/x]&=M[N/x]P[N/x]\\(\lambda x.M)[N/x]&=\lambda x.M\\(\lambda y.M)[N/x]&=\lambda y.M[N/x]\quad(y\ne x,\ y\notin\operatorname{FV}(N))\end{aligned}

If the condition in the last line fails, first rename yy to a fresh variable

For example, consider substituting yy for xx in λy.xy\lambda y.xy. The binder would capture the inserted yy, so rename it first

(λy.xy)[y/x]≡α(λz.xz)[y/x]=λz.yz(\lambda y.xy)[y/x] \equiv_\alpha(\lambda z.xz)[y/x] =\lambda z.yz

The result is not λy.yy\lambda y.yy. Its free occurrence of yy must remain free

Beta-Conversion and Equational Reasoning

Applying an abstraction evaluates its body with the argument substituted for its parameter

(λx.M)N=βM[N/x](\lambda x.M)N=_\beta M[N/x]

The equality relation =β=_\beta also permits reflexivity, symmetry, transitivity, and replacement inside applications and abstractions

For example, assuming zz is fresh for PP, the constant function discards its second argument

(λxz.x)PQ=β(λz.P)Q=βP(\lambda xz.x)PQ =_\beta(\lambda z.P)Q =_\beta P

An equation is established by a finite derivation from the equality rules and the β\beta rule. The notation λβ⊢M=N\lambda\beta\vdash M=N records that the equation is derivable in this theory

Equality and computation serve different purposes. Equality allows a step in either direction, whereas an evaluation step contracts an application of an abstraction. We will make this directed relation precise in Lecture 2

The Eta Rule

The term λx.Mx\lambda x.Mx passes its argument directly to MM. When xx does not occur freely in MM, the η\eta rule identifies these terms

λx.Mx=ηMx∉FV⁡(M)\lambda x.Mx=_\eta M \qquad x\notin\operatorname{FV}(M)

The side condition matters: λx.xx\lambda x.xx is not an η\eta-expansion of xx. Adding this rule gives the theory λβη\lambda\beta\eta, expressing an extensional view of functions

Fixed Points and Self-Application

A fixed point of a term FF is a term UU satisfying FU=βUFU=_\beta U. In the untyped calculus, every term has such a fixed point

Define Curry’s fixed-point combinator

Y=λf.(λx.f(xx))(λx.f(xx))\mathbf Y=\lambda f.(\lambda x.f(xx))(\lambda x.f(xx))

Then for every FF,

YF=βF(YF)\mathbf YF=_\beta F(\mathbf YF)

To verify the equation, put W=λx.F(xx)W=\lambda x.F(xx). Both sides reduce to the same term

YF→βWW→βF(WW)F(YF)→βF(WW)\begin{aligned} \mathbf YF&\to_\beta WW\to_\beta F(WW)\\ F(\mathbf YF)&\to_\beta F(WW) \end{aligned}

The useful idea is to turn a request for self-reference into self-application. If UU should satisfy U=FUU=FU, define WW so that Wx=F(xx)Wx=F(xx) and then apply WW to itself

Turing’s combinator offers a related construction

A=λxy.y(xxy)Θ=AA\begin{aligned} A&=\lambda xy.y(xxy)\\ \mathbf\Theta&=AA \end{aligned}

It satisfies the directed unfolding ΘF→β∗F(ΘF)\mathbf\Theta F\to_\beta^*F(\mathbf\Theta F). For Y\mathbf Y, the fixed-point equation above is stated as β\beta-equality; its displayed derivation uses a common reduct

Self-application also explains why evaluation may fail to terminate

Ω=(λx.xx)(λx.xx)→βΩ\Omega=(\lambda x.xx)(\lambda x.xx) \to_\beta\Omega

The syntax allows recursion and divergence without introducing a separate recursion operator. We will use fixed points to construct recursive programs in Lecture 3


© 2024 本网站由 renxiaodaifu 使用 Stellar主题 创建
总访问 次 | 本页访问 次
共发表 103 篇 Blog(s) · 总计 254.1k 字