Lambda-Calculuslcs.ios.ac.cn/~yzhang/fopl/01_lambda.pdf · contained in the pure λ-calculus, but...

21
T HEORY AND P RACTICE OF F UNCTIONAL P ROGRAMMING Lambda-Calculus Dr. ZHANG Yu Institute of Software, Chinese Academy of Sciences GUCAS, Beijing

Transcript of Lambda-Calculuslcs.ios.ac.cn/~yzhang/fopl/01_lambda.pdf · contained in the pure λ-calculus, but...

Page 1: Lambda-Calculuslcs.ios.ac.cn/~yzhang/fopl/01_lambda.pdf · contained in the pure λ-calculus, but can be represented byλ-terms. This is about encoding, just as we can represent data

THEORY AND PRACTICE OF FUNCTIONAL PROGRAMMING

Lambda-Calculus

Dr. ZHANG Yu

Institute of Software, Chinese Academy of Sciences

GUCAS, Beijing

Page 2: Lambda-Calculuslcs.ios.ac.cn/~yzhang/fopl/01_lambda.pdf · contained in the pure λ-calculus, but can be represented byλ-terms. This is about encoding, just as we can represent data

The untyped λ-calculus

Theorey and Practice of Functional Programming 2 / 21

λ-calculus is the fundamental model for programming languages. It itself isseen as a language that is built on very simple syntax, yet has powerfulcomputing capability.

In λ-calculus, the basic units are expressions or terms.

e, e′, . . . ::= x | λx . e | e e′

where x ranges over an infinite set of variables.

Page 3: Lambda-Calculuslcs.ios.ac.cn/~yzhang/fopl/01_lambda.pdf · contained in the pure λ-calculus, but can be represented byλ-terms. This is about encoding, just as we can represent data

The untyped λ-calculus

Theorey and Practice of Functional Programming 3 / 21

Abstraction: λx.e — a function where x is the argument and e is thefunction body.

− The variable x is bound in the expression.

− EXAMPLEEXAMPLEEXAMPLE (λ-abstractions)

λx . x+ 1 the increment functionλx.λy . x+ y the additionλf.λg.λx . f(g x) function composition

We shall see how to program “+” and numerals later.

− The scope of λ extends as far as possible to the right, e.g., λx . e e′

is actually λx . (e e′).

− We also write λx . λy . e as λxy . e.

Page 4: Lambda-Calculuslcs.ios.ac.cn/~yzhang/fopl/01_lambda.pdf · contained in the pure λ-calculus, but can be represented byλ-terms. This is about encoding, just as we can represent data

The untyped λ-calculus

Theorey and Practice of Functional Programming 4 / 21

Application: e e′ — compute function e with the (concrete) argument e′,or apply function e to the argument e′.

(λx . x+ 1) 2

(λx.λy . x+ y) 1

(λf.λg.λx . f(g x)) (λx . x+ 1) (λx . x ∗ 2)

− Applications are left-associative, i.e., e1 e2 e3 stands for (e1 e2) e3.

Page 5: Lambda-Calculuslcs.ios.ac.cn/~yzhang/fopl/01_lambda.pdf · contained in the pure λ-calculus, but can be represented byλ-terms. This is about encoding, just as we can represent data

Free and bound variables

Theorey and Practice of Functional Programming 5 / 21

Bound variables: variables that are attached to λ are bound variables —λ is a binder.Bound variables in other systems:

limx→∞ e−x

2

0x2 dx

∀x, y, z : x ≥ y ∧ y ≥ z ⇒ x ≥ z

int succ (x : int) return x+1 ;

Page 6: Lambda-Calculuslcs.ios.ac.cn/~yzhang/fopl/01_lambda.pdf · contained in the pure λ-calculus, but can be represented byλ-terms. This is about encoding, just as we can represent data

Free and bound variables

Theorey and Practice of Functional Programming 6 / 21

Free variables: all those are not bound. We write FV (e) for the set offree variables of expression e.

FV (x) = x

FV (e e′) = FV (e) ∪ FV (e′)

FV (λx . e) = FV (e) \ x

EXAMPLEEXAMPLEEXAMPLE (Free variables of λ-expressions)

FV (λx . x x) = ?FV ((λx . x y)(λy . y z)) = ?

A term without any free variable is a closed term; o.w. it’s open.

Page 7: Lambda-Calculuslcs.ios.ac.cn/~yzhang/fopl/01_lambda.pdf · contained in the pure λ-calculus, but can be represented byλ-terms. This is about encoding, just as we can represent data

Variables and α-equivalence

Theorey and Practice of Functional Programming 7 / 21

α-equivalence: the names of bound variables are irrelevant — they canbe renamed.

λx . e =α λy . ey/x, where y 6∈ FV (e)

EXAMPLEEXAMPLEEXAMPLE

λx . x =α λy . y

λz . x+ z 6=α λz . y + z

λz . x+ z 6=α λx . x+ x

Formal definition of renaming:

xy/xdef= y

zy/xdef= z, if z 6= x

(e e′)y/xdef= (ey/x)(e′y/x)

(λx . e)y/xdef= λx . e

(λz . e)y/xdef= λz . (ey/x)

Page 8: Lambda-Calculuslcs.ios.ac.cn/~yzhang/fopl/01_lambda.pdf · contained in the pure λ-calculus, but can be represented byλ-terms. This is about encoding, just as we can represent data

β-reduction

Theorey and Practice of Functional Programming 8 / 21

How do we compute in λ-calculus?

In models like λ-calculus, computations are done by reductions, whichbasically turns an expression into another “equivalent” form.

In λ-calculus, the essential reduction is called β-reduction:

(λx . e)e′ ; e[e′/x]

e[e′/x] denotes the expression obtained from e by substituting all freeoccurences of x with e′.

x[e/x]def= e

z[e/x]def= z, if z 6= x

(e1 e2)[e/x]def= (e1[e/x])(e2[e/x])

(λx . e′)[e/x]def= λx . e′

(λz . e′)[e/x]def= λz . (e′[e/x]) if z 6∈ FV (e)

(λz . e′)[e/x]def= λy . (e′y/z[e/x]) if z ∈ FV (e) and y is fresh

Page 9: Lambda-Calculuslcs.ios.ac.cn/~yzhang/fopl/01_lambda.pdf · contained in the pure λ-calculus, but can be represented byλ-terms. This is about encoding, just as we can represent data

β-reduction

Theorey and Practice of Functional Programming 9 / 21

A term of the form (λx . e)e′ is a β-redex.β-reduction examples:

(λx . x+ 1) 2 ; 2 + 1

(λf.λx . f (f x)) incrincrincr ; λx . incrincrincr(incrincrincr x)

(λf.λg.λx . f (g x)) incrincrincrdoubledoubledouble

; (λg.λx . incrincrincr(g x))doubledoubledouble ; λx . incrincrincr(doubledoubledouble x)

Single-step β-reduction can take place inside λ-expressions:

e1 ; e′1

e1e2 ; e′1e2

e2 ; e′2

e1e2 ; e1e′

2

e ; e′

λx . e ; λx . e′

EXAMPLEEXAMPLEEXAMPLE

(λf g x . f(g x)) (λx . (λy . x+ y)1) ((λx y . x ∗ y) 2)

; (λf g x . f(g x)) (λx . (λy . x+ y)1) (λy . 2 ∗ y)

; (λf g x . f(g x)) (λx . x+ 1) (λy . 2 ∗ y)

; . . . . . .

Page 10: Lambda-Calculuslcs.ios.ac.cn/~yzhang/fopl/01_lambda.pdf · contained in the pure λ-calculus, but can be represented byλ-terms. This is about encoding, just as we can represent data

β-reduction

Theorey and Practice of Functional Programming 10 / 21

Multi-step β-reduction β is the reflexive and transitive closure of ;β.

(λf g x . f (g x)) incrincrincr doubledoubledouble β λx . incrincrincr (doubledoubledouble x)

β-equivalence =β is the symmetric, reflexive and transitive closure.

(λf g x . f (g x)) incrincrincr doubledoubledouble =β (λf g x . g (f x)) doubledoubledouble incrincrincr

A term without any β-redexes is a β-normal form.

x y, λx y . y, y(λx . x)(λx . x)

β-reductions of untyped λ-terms can be infinite.

EXAMPLEEXAMPLEEXAMPLE : Ω def= (λx . xx)(λx . xx)

(λx . xx)(λx . xx) ; (λx . xx)(λx . xx) ; (λx . xx)(λx . xx) ; . . .

The reduction can produce larger terms:

(λx . x x x)(λx . x x x) ; (λx . x x x)(λx . x x x)(λx . x x x) ; · · ·

Page 11: Lambda-Calculuslcs.ios.ac.cn/~yzhang/fopl/01_lambda.pdf · contained in the pure λ-calculus, but can be represented byλ-terms. This is about encoding, just as we can represent data

Programming λ-calculus

Page 12: Lambda-Calculuslcs.ios.ac.cn/~yzhang/fopl/01_lambda.pdf · contained in the pure λ-calculus, but can be represented byλ-terms. This is about encoding, just as we can represent data

Programming untyped λ-calculus

Theorey and Practice of Functional Programming 12 / 21

In previous examples we have used + and numerals — they are notcontained in the pure λ-calculus, but can be represented by λ-terms.

This is about encoding, just as we can represent data and computationsin binary.

In terms of computability, λ-calculus is equivalent to the Turing machinemodel.

− More precisely, both λ-calculus and Turing machines define thesame class of computable (numeric) functions.

− We shall see how different types of data and related operations canbe programmed in λ-calculus.

Page 13: Lambda-Calculuslcs.ios.ac.cn/~yzhang/fopl/01_lambda.pdf · contained in the pure λ-calculus, but can be represented byλ-terms. This is about encoding, just as we can represent data

Booleans

Theorey and Practice of Functional Programming 13 / 21

Normally, we use only closed terms to encode data and operations.

TrueTrueTruedef= λx.λy . x

FalseFalseFalsedef= λx.λy . y

Boolean operations:

notnotnotdef= λu . λv . λw . uwv

andandanddef= λu . λv . uv(λx . λy . y)

. . . . . .

notnotnot TrueTrueTrue = (λu v w . u w v)(λx y . x) ; λv w . (λx y . x)w v λv w .w

If-Then-Else:

IFIFIFdef= λu.λv.λw . uvw

IFIFIF TrueTrueTrue e1 e2 = (λu v w . uvw)(λx y . x)e1e2 (λx y . x)e1e2 ;β e1

Page 14: Lambda-Calculuslcs.ios.ac.cn/~yzhang/fopl/01_lambda.pdf · contained in the pure λ-calculus, but can be represented byλ-terms. This is about encoding, just as we can represent data

Natural numbers

Theorey and Practice of Functional Programming 14 / 21

Natural numbers

0def= λfx . x,

1def= λfx . fx,

2def= λfx . f(fx),

3def= λfx . f(f(fx)),

. . . . . .

This encoding is called the Church numerals:

ndef= λf.λx . fnx

Natural number operations:

addaddadddef= λu1.λu2.λf.λx . u1f(u2fx)

multmultmultdef= λu1.λu2.λf.λx . u1(u2f)x

. . . . . .

Page 15: Lambda-Calculuslcs.ios.ac.cn/~yzhang/fopl/01_lambda.pdf · contained in the pure λ-calculus, but can be represented byλ-terms. This is about encoding, just as we can represent data

Pairs and tuples

Theorey and Practice of Functional Programming 15 / 21

Pairs〈e1, e2〉

def= λz . z e1 e2

Projections:firstfirstfirst

def= λu . u(λx y . x)

secondsecondseconddef= λu . u(λx y . y)

Tuples

〈e1, . . . , en〉def= λz . z e1 · · · en

i-th projection:

projprojprojidef= λu . u(λx1 · · ·xn . xi)

Page 16: Lambda-Calculuslcs.ios.ac.cn/~yzhang/fopl/01_lambda.pdf · contained in the pure λ-calculus, but can be represented byλ-terms. This is about encoding, just as we can represent data

Subtraction

Theorey and Practice of Functional Programming 16 / 21

We only concern unsigned arithmetic

subsubsub nm =

n−m if n > m0 otherwise

Question: what about signed arithmetic?

Define the predecessor:

predpredpred n =

n− 1 if n > 00 otherwise

Encoding in λ-calculus (using pair)

predpredpreddef= λn .firstfirstfirst (n P 〈0, 0〉)

where Pdef= λx . 〈secondsecondsecond x, (secondsecondsecond x) + 1〉.

Page 17: Lambda-Calculuslcs.ios.ac.cn/~yzhang/fopl/01_lambda.pdf · contained in the pure λ-calculus, but can be represented byλ-terms. This is about encoding, just as we can represent data

Subtraction

Theorey and Practice of Functional Programming 17 / 21

Programming subtraction

Define subsubsub with predpredpred:

subsubsubdef= λn m .m predpredpred n

An alternative definition of predpredpred (without using pair):

predpredpreddef= λn f x . n T (λu . x)(λv . v)

where Tdef= λg h . h (g f).

It can be checked that for n > 0,

Tn(λu . x) = λh . h (fn−1(x))

Page 18: Lambda-Calculuslcs.ios.ac.cn/~yzhang/fopl/01_lambda.pdf · contained in the pure λ-calculus, but can be represented byλ-terms. This is about encoding, just as we can represent data

Inductive data structure — list

Theorey and Practice of Functional Programming 18 / 21

Programming lists

List is the very basic data structure in functional programming.

− A list is just a sequence of elements, e.g., [3, 1, 2, 4].

− It has two constructs: nilnilnil — the empty list, and consconscons — theoperation that produces a list by adding an element to the head ofanother list, e.g.,

consconscons 2 [3, 1, 2, 4] = [2, 3, 1, 2, 4]

nilnilnil and consconscons in λ-calculus:

nilnilnildef= λx y . y

consconsconsdef= λh.λt.λx y . x h t

Exercise: program isnilisnilisnil, lengthlengthlength, . . .

Page 19: Lambda-Calculuslcs.ios.ac.cn/~yzhang/fopl/01_lambda.pdf · contained in the pure λ-calculus, but can be represented byλ-terms. This is about encoding, just as we can represent data

Recursion and fix-points

Theorey and Practice of Functional Programming 19 / 21

Fix-point and recursion:

Curry’s (a.k.a. YYY-combinator): YYY def= λf . (λx . f(xx))(λx . f(xx))

YYYF =β F (YYYF )

Turing’s fix-point: Θ def= (λx . λf . f(xxf))(λx . λf . f(xxf))

There exist other fix-point combinators.

Page 20: Lambda-Calculuslcs.ios.ac.cn/~yzhang/fopl/01_lambda.pdf · contained in the pure λ-calculus, but can be represented byλ-terms. This is about encoding, just as we can represent data

The Church-Rosser theorem

Theorey and Practice of Functional Programming 20 / 21

[Church & Rosser, 1936] If e e1 and e e2, then there exists e′ suchthat e1 e′ and e2 e′.

e

~~~~||||||||

BB

BBBB

BB

e1

@

@@

@e2

~~~~~~~~

e′

e

~~||||||||

BB

BBBB

BB

e1

@

@@

@e2

~~~~~~

e′

(CR) (Diamond)

Corollary

− If e1 =β e2 then there exists e′ with e1, e2 e.

− If e is in β-normal form and e =β e′, then e′ e.

Page 21: Lambda-Calculuslcs.ios.ac.cn/~yzhang/fopl/01_lambda.pdf · contained in the pure λ-calculus, but can be represented byλ-terms. This is about encoding, just as we can represent data

Proof of the CR theorem

Theorey and Practice of Functional Programming 21 / 21

Define a “parallel reduction” relation ≫:

(Refl)e ≫ e

e ≫ e′

(Abs)λx . e ≫ λx . e′

e1 ≫ e′1 e2 ≫ e′2(App)

e1e2 ≫ e′1e′

2

e1 ≫ e′1 e2 ≫ e′2(‖ −β)

(λx . e1)e2 ≫ e′1[e′

2/x]

The proof is supported by the following lemmas:

– Lemma 1: ≫ satisfies the Diamond-property, i.e., if e ≫ e1 and e ≫ e2,then there exists e′ with e1, e2 ≫ e′.

– Lemma 2: If e ≫ e′ and t ≫ t′, then e[t/x] ≫ e′[t′/x].

– Lemma 3: is the transitive closure of ≫.