33
Slides 01 Slides 01 1 Type-Free Type-Free λ λ -Calculus -Calculus The λ-calculus is a family of prototype programming languages invented by the logician, Alonzo Church, in the 1930's. Main features: functional : they are based on the notion of function and include notation for function-application and abstraction. higher-order : they give a systematic notation for functions whose input and output values may be other functions.

Slides 01 1 Type-Free λ-Calculus The λ-calculus is a family of prototype programming languages invented by the logician, Alonzo Church, in the 1930's

  • View
    222

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Slides 01 1 Type-Free λ-Calculus The λ-calculus is a family of prototype programming languages invented by the logician, Alonzo Church, in the 1930's

Slides 01Slides 0111

Type-Free Type-Free λλ-Calculus-Calculus

• The λ-calculus is a family of prototype programming languages invented by the logician, Alonzo Church, in the 1930's.

• Main features: functional: they are based on the notion of

function and include notation for function-application and abstraction.

higher-order: they give a systematic notation for functions whose input and output values may be other functions.

Page 2: Slides 01 1 Type-Free λ-Calculus The λ-calculus is a family of prototype programming languages invented by the logician, Alonzo Church, in the 1930's

Slides 01Slides 0122

Notation for FunctionsNotation for Functions

• The crux of the λ-calculus is the mechanism of λ-abstraction.

• The expression

λx . M

is the general form that functions take in the system. To specify a function we say what is its formal parameter, here x, and what is its result, here M.

Page 3: Slides 01 1 Type-Free λ-Calculus The λ-calculus is a family of prototype programming languages invented by the logician, Alonzo Church, in the 1930's

Slides 01Slides 0133

• In mathematical texts we might talk about the function f given by

f(x) = M• In the λ-calculus we have an anonymous

notation for the function which introduces the function without giving it a name (like f).

• The parameter x is a formal parameter and so we would expect that the function

λx . λy . xy• would be indistinguishable from

λu . λv . uvfor instance.

Page 4: Slides 01 1 Type-Free λ-Calculus The λ-calculus is a family of prototype programming languages invented by the logician, Alonzo Church, in the 1930's

Slides 01Slides 0144

• How do we associate actual parameters with the formals? We form applications

(λx . M) N• To evaluate these applications, we pass the

parameter: we substitute the actual parameter for the formal, which we denote

[N/x]M• Here is an example in a familiar setting:

(λx . x + 1)5 → [5/x](x+1) = 5 + 1 = 6• As for the binding constructs of logic, the

quantifiers, we have to be careful about how we define substitution.

Page 5: Slides 01 1 Type-Free λ-Calculus The λ-calculus is a family of prototype programming languages invented by the logician, Alonzo Church, in the 1930's

Slides 01Slides 0155

λλ-terms-terms• Definition (1A1) An infinite sequence of term-

variables is assumed to be given. Then linguistic expression called λ-terms are defined as follows:

i. each term-variable is a λ-term, called an atomic term or atom;

ii. if M and N are λ-terms then (MN) is a λ-term called an application;

iii. if x is a term-variable and M is a λ-term then (λx.M) is a λ-term called a λ-abstract or abstraction or abstract.

A composite λ-term is a λ-term that is not an atom.

Page 6: Slides 01 1 Type-Free λ-Calculus The λ-calculus is a family of prototype programming languages invented by the logician, Alonzo Church, in the 1930's

Slides 01Slides 0166

NotationNotation• Term-variables are denoted by lower-case

letters u, v, w, x, y, z, with or without subscripts.

• Arbitrary λ-terms are denoted by upper-case letters L, M, N, P, Q, R, S, T, with or without subscripts.

• Parentheses and repeated λ's will often be omitted, for example:λxyz.M ≡ (λx.(λy.(λz.M))) MNPQ ≡ (((MN)P)Q)

Page 7: Slides 01 1 Type-Free λ-Calculus The λ-calculus is a family of prototype programming languages invented by the logician, Alonzo Church, in the 1930's

Slides 01Slides 0177

• Definition (1A2) The length, |M|, of a λ-term M is the number of occurrences of variables in M:|x| = 1, |MN| = |M| + |N|, |λx.M| = 1 + |M|.

• Example: |(λxyz.x(yz))(λx.x)| = 8

• Note: We shall usually call "λ-term" just "term".

Page 8: Slides 01 1 Type-Free λ-Calculus The λ-calculus is a family of prototype programming languages invented by the logician, Alonzo Church, in the 1930's

Slides 01Slides 0188

• Definition (1A3) The subterms of a term M are defined by induction on |M| as follows:

i. an atom is a subterm of itself;

ii. if M ≡ λx.P, its subterms are M and all subterms of P;

iii. if M ≡ PQ, its subterms are all the subterms of P, all those of Q, and M itself.

• Example:

The subterms of M ≡ (λx.yx)(λz.x(yx)) are x, y, yx, λx.yx, x(yx), λz.x(yx) and M itself.

Page 9: Slides 01 1 Type-Free λ-Calculus The λ-calculus is a family of prototype programming languages invented by the logician, Alonzo Church, in the 1930's

Slides 01Slides 0199

• Definition (1A5) An occurrence of λx is called an abstractor, and the occurrence of x in it is called a binding occurrence of x. All the occurrences of terms in M, other than binding occurrences of variables, are called components of M.Let λx.P be a component of a term M. The displayed component P is called the body of λx.P or the scope of the abstractor λx. The covering abstractors of a component R of M are the abstractors in M whose scopes contain R.

Page 10: Slides 01 1 Type-Free λ-Calculus The λ-calculus is a family of prototype programming languages invented by the logician, Alonzo Church, in the 1930's

Slides 01Slides 011010

• Definition (1A6) A non-binding variable-occurrence x in a term M is said to be bound in M iff it is in the scope of an occurrence of λx in M, otherwise it is free in M.

A variable x is said to be bound in M iff M contains an occurrence of λx; and x is said to be free in M iff M contains a free occurrence of x. The set of all variables free in M is denoted by FV(M).

Page 11: Slides 01 1 Type-Free λ-Calculus The λ-calculus is a family of prototype programming languages invented by the logician, Alonzo Church, in the 1930's

Slides 01Slides 011111

WarningWarning

• Two distinct concepts have been defined: free/bound occurrences and free/bound variables. A variable x may be both free and bound in M, for example, if M ≡ x(λx.x), but a particular occurrence of x in M cannot be both free and bound.

• x is said to be bound in λx.y even though its only occurrence in the term is a binding occurrence.

Page 12: Slides 01 1 Type-Free λ-Calculus The λ-calculus is a family of prototype programming languages invented by the logician, Alonzo Church, in the 1930's

Slides 01Slides 011212

SubstitutionSubstitution

• Definition (1A7) [N/x]M is the result of substituting N for each free occurrence of x in M and making any changes of bound variables needed to prevent variables free in N from becoming bound in [N/x]M. More precisely, we define for all N, x, P, Q and all y ≢ x

i. [N/x]x ≡ N,ii. [N/x]y ≡ y,

Page 13: Slides 01 1 Type-Free λ-Calculus The λ-calculus is a family of prototype programming languages invented by the logician, Alonzo Church, in the 1930's

Slides 01Slides 011313

iii. [N/x](PQ) ≡ (([N/x]P)([N/x]Q)),iv. [N/x](λx.P) ≡ λx.P,v. [N/x](λy.P) ≡ λy.P if x ∉ FV(P),vi. [N/x](λy.P) ≡ λy.[N/x]P

if x ∈ FV(P) and y ∉ FV(N),vii. [N/x](λy.P) ≡ λz.[N/x][z/y]P

if x ∈ FV(P) and y ∈ FV(N).(In (vii) z is the first variable in the

sequence given in (1A1) which does not occur free in NP.)

Page 14: Slides 01 1 Type-Free λ-Calculus The λ-calculus is a family of prototype programming languages invented by the logician, Alonzo Church, in the 1930's

Slides 01Slides 011414

• Example:

[λx.xy/u](λy.uuy) ≡ λz.[λx.xy/u][z/y]uuy

≡ λz.[λx.xy/u]uuz

≡ λz.(λx.xy)(λx.xy)z

Page 15: Slides 01 1 Type-Free λ-Calculus The λ-calculus is a family of prototype programming languages invented by the logician, Alonzo Church, in the 1930's

Slides 01Slides 011515

-conversion -conversion

• Definition (1A8) Let y ∉ FV(M); then we say

λx.M ≡ λy.[y/x]M,

and the act of replacing an occurrence of λx.M in a term by λy.[y/x]M is called a change of bound variables. If P changes to Q by a finite (perhaps empty) series of changes of bound variables we say P -converts to Q or P ≡ Q.

Page 16: Slides 01 1 Type-Free λ-Calculus The λ-calculus is a family of prototype programming languages invented by the logician, Alonzo Church, in the 1930's

Slides 01Slides 011616

Simple properties of Simple properties of -conversion-conversion

a. P ≡ Q ⇒ |P| = |Q|

b. P ≡ Q ⇒ FV(P) = FV(Q)

Page 17: Slides 01 1 Type-Free λ-Calculus The λ-calculus is a family of prototype programming languages invented by the logician, Alonzo Church, in the 1930's

Slides 01Slides 011717

Bound-variable clashBound-variable clash

• Definition (1A9) A term M has a bound-variable clash iff M contains an abstractor λx and a (free, bound or binding) occurrence of x that is not in its scope.

• Examples of terms with bound-variable clashes: x(λx.N), λx.λy.λx.N, (λx.P)(λx.Q).

• Lemma Every term can be -converted to a term without bound-variable clashes.

Page 18: Slides 01 1 Type-Free λ-Calculus The λ-calculus is a family of prototype programming languages invented by the logician, Alonzo Church, in the 1930's

Slides 01Slides 011818

CombinatorCombinator• Definition (1A10) A closed term or

combinator is a term in which no variable occurs free.

• Example:

Curry's fixed-point combinator

Y ≡ λx.(λy.x(yy))(λy.x(yy))

Page 19: Slides 01 1 Type-Free λ-Calculus The λ-calculus is a family of prototype programming languages invented by the logician, Alonzo Church, in the 1930's

Slides 01Slides 011919

ββ-reduction-reduction

• Definition (1B1) A β-redex is any term (λx.M)N; its contractum is [N/x]M and its rewrite rule is

(λx.M)N ⊳1β [N/x]M.If P contains a β-redex-occurrence R ≡ (λx.M)N and Q is the result of replacing this by [N/x]M, we say P β-contracts to Q, denoted as P ⊳1β Q, and we call the triple <P, R, Q> a β-contraction of P.

Page 20: Slides 01 1 Type-Free λ-Calculus The λ-calculus is a family of prototype programming languages invented by the logician, Alonzo Church, in the 1930's

Slides 01Slides 012020

• Example:

(λxy.x)xy ⊳1β (λy.x)y ⊳1β x

<(λxy.x)xy, (λxy.x)x, (λy.x)y> is a β-contraction of (λxy.x)xy.

• Lemma (1B1.1)

P ⊳1β Q ⇒ FV(P) ⊇ FV(Q).

Page 21: Slides 01 1 Type-Free λ-Calculus The λ-calculus is a family of prototype programming languages invented by the logician, Alonzo Church, in the 1930's

Slides 01Slides 012121

• Definition (1B2) A β-reduction of a term P is a finite or infinite sequence of β-contractions with the form <P1,R1,Q1>, <P2,R2,Q2>, ...

where P1 ≡ P and Qi ≡ Pi+1 for i = 1,2,.... (The empty sequence is allowed.) We say a finite reduction is from P to Q iff either it has n ≥ 1 contractions and Qn

≡ Q or it is empty and P ≡ Q. A reduction from P to Q is said to terminate or end at Q. If there is a reduction from P to Q we say P β-reduces to Q, or P ⊳β Q.

Page 22: Slides 01 1 Type-Free λ-Calculus The λ-calculus is a family of prototype programming languages invented by the logician, Alonzo Church, in the 1930's

Slides 01Slides 012222

• Definition (1B3) The length of a β-reduction is the number of its β-contractions (finite or ∞). A reduction with maximal length is one that continues as long as there are redexes to contract (i.e. one that either is infinite or ends at a term containing no redexes).

• Example:A β-reduction with maximal length:<(λxy.x)xy, (λxy.x)x, (λy.x)y>,

<(λy.x)y, (λy.x)y, x>.

Page 23: Slides 01 1 Type-Free λ-Calculus The λ-calculus is a family of prototype programming languages invented by the logician, Alonzo Church, in the 1930's

Slides 01Slides 012323

• Definition (1B4) If we can change P to Q by a finite sequence of β-reductions and reversed β-reductions, we say P β-converts to Q, or P is β-equal to Q, or

P =β Q.

A reversed β-reduction is also called a β-expansion.

Page 24: Slides 01 1 Type-Free λ-Calculus The λ-calculus is a family of prototype programming languages invented by the logician, Alonzo Church, in the 1930's

Slides 01Slides 012424

• Example:

For any term F,

YF =β F(YF)

Page 25: Slides 01 1 Type-Free λ-Calculus The λ-calculus is a family of prototype programming languages invented by the logician, Alonzo Church, in the 1930's

Slides 01Slides 012525

Church-Rosser Theorem for Church-Rosser Theorem for ββ(1B5)(1B5)

i. If M ⊳β P and M ⊳β Q then there exists T such that P ⊳β T, Q ⊳β T.

M

P Q

T

Page 26: Slides 01 1 Type-Free λ-Calculus The λ-calculus is a family of prototype programming languages invented by the logician, Alonzo Church, in the 1930's

Slides 01Slides 012626

ii. If P =β Q then there exists T such that

P ⊳β T, Q ⊳β T.

Q

P

T

Page 27: Slides 01 1 Type-Free λ-Calculus The λ-calculus is a family of prototype programming languages invented by the logician, Alonzo Church, in the 1930's

Slides 01Slides 012727

ββ-normal forms-normal forms

• Definition (1B6) A β-normal form is a term that contains no β-redexes. The class of all β-normal forms is called β-nf. We say a term M has β-normal form N iff M ⊳β N and N ∈ β-nf.

Page 28: Slides 01 1 Type-Free λ-Calculus The λ-calculus is a family of prototype programming languages invented by the logician, Alonzo Church, in the 1930's

Slides 01Slides 012828

• Example:

(λxy.x)xy ⊳β x

x is a β-nf of (λxy.x)xy

Page 29: Slides 01 1 Type-Free λ-Calculus The λ-calculus is a family of prototype programming languages invented by the logician, Alonzo Church, in the 1930's

Slides 01Slides 012929

• A reduction can be thought of as a computation and a β-normal form (β-nf) as its result.

• One main aim when designing a type-theory is to give it the property that every computation can be pursued to a result, i.e. that every term with a type has a β-nf.

• In general, terms do not necessarily have β-nf. For example, (λx.xx)(λx.xx) does not have a β-nf. Neither has Y.

Page 30: Slides 01 1 Type-Free λ-Calculus The λ-calculus is a family of prototype programming languages invented by the logician, Alonzo Church, in the 1930's

Slides 01Slides 013030

• By the Church-Rosser Theorem (1B5), we have

NF-Uniqueness Lemma (1B7)

Modulo -conversion, a term M has at most

one β-nf.

Notation: If M has a β-nf it will be called M*β.

Page 31: Slides 01 1 Type-Free λ-Calculus The λ-calculus is a family of prototype programming languages invented by the logician, Alonzo Church, in the 1930's

Slides 01Slides 013131

• Definition (1B8)

The leftmost β-redex-occurrence in a term P is the β-redex-occurrence whose leftmost parenthesis is to the left of all the parentheses in all the other β-redex-occurrences in P.The leftmost β-reduction of a term P is a β-reduction of P with maximal length, say<P1,R1,Q1>, <P2,R2,Q2>, ..., such that Ri is the leftmost β-redex-occurrence in Pi for all i ≥ 1 (and P1 -converts to P and Pi+1 -converts to Qi for all i ≥ 1).

Page 32: Slides 01 1 Type-Free λ-Calculus The λ-calculus is a family of prototype programming languages invented by the logician, Alonzo Church, in the 1930's

Slides 01Slides 013232

• Leftmost-reduction Theorem (1B9)

A term M has a β-nf M*β iff the leftmost β-reduction of M is finite and ends at M*β.

• Example:The leftmost β-reduction of the fixed-point combinator Y is infinite, so Y has no β-nf.

Page 33: Slides 01 1 Type-Free λ-Calculus The λ-calculus is a family of prototype programming languages invented by the logician, Alonzo Church, in the 1930's

Slides 01Slides 013333

• Lemma (1B10)Every β-nf N can be expressed uniquely in the form

N ≡ λx1...xm.yN1...Nn (m ≥ 0, n ≥ 0), where N1, ..., Nn are β-nf's. And if N is closed then y ∈ {x1, ..., xm}.

• Special cases: an atom (m=n=0): N ≡ y an application (m=0,n≥1): N ≡ yN1...Nn

an abstraction (m≥1): N ≡ λx1...xm.P an abstracted atom (m≥1,n=0): N ≡ λx1...xm.y