Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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. 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.

2 Slides 01 2 Notation 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.

3 Slides 01 3 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. uv for instance.

4 Slides 01 4 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.

5 Slides 01 5 λ-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.

6 Slides 01 6 Notation 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)

7 Slides 01 7 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".

8 Slides 01 8 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.

9 Slides 01 9 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.

10 Slides 01 10 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).

11 Slides 01 11 Warning 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.

12 Slides 01 12 Substitution 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,

13 Slides 01 13 iii.[N/x](PQ) ≡ (([N/x]P)([N/x]Q)), iv.[N/x]( λx.P) ≡ λx.P, v.[N/x](λy.P) ≡ λy.Pif 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.)

14 Slides 01 14 Example: [λx.xy/u](λy.uuy) ≡ λz.[λx.xy/u][z/y]uuy ≡ λz.[λx.xy/u]uuz ≡ λz.(λx.xy)(λx.xy)z

15 Slides 01 15  -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.

16 Slides 01 16 Simple properties of  -conversion a.P ≡  Q ⇒ |P| = |Q| b.P ≡  Q ⇒ FV(P) = FV(Q)

17 Slides 01 17 Bound-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.

18 Slides 01 18 Combinator 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))

19 Slides 01 19 β-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 a β-contraction of P.

20 Slides 01 20 Example: (λxy.x)xy ⊳ 1β (λy.x)y ⊳ 1β x is a β-contraction of (λxy.x)xy. Lemma (1B1.1) P ⊳ 1β Q ⇒ FV(P) ⊇ FV(Q).

21 Slides 01 21 Definition (1B2) A β-reduction of a term P is a finite or infinite sequence of β- contractions with the form,,... where P 1 ≡  P and Q i ≡  P i+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 Q n ≡  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.

22 Slides 01 22 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:,.

23 Slides 01 23 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.

24 Slides 01 24 Example: For any term F, YF = β F(YF)

25 Slides 01 25 Church-Rosser Theorem for β (1B5) i.If M ⊳ β P and M ⊳ β Q then there exists T such that P ⊳ β T, Q ⊳ β T. M PQ T

26 Slides 01 26 ii.If P = β Q then there exists T such that P ⊳ β T, Q ⊳ β T. Q P T

27 Slides 01 27 β-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.

28 Slides 01 28 Example: (λxy.x)xy ⊳ β x x is a β-nf of (λxy.x)xy

29 Slides 01 29 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.

30 Slides 01 30 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 *β.

31 Slides 01 31 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,,..., such that R i is the leftmost β-redex-occurrence in P i for all i ≥ 1 (and P 1  -converts to P and P i+1  -converts to Q i for all i ≥ 1).

32 Slides 01 32 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.

33 Slides 01 33 Lemma (1B10) Every β-nf N can be expressed uniquely in the form N ≡ λx 1...x m.yN 1...N n (m ≥ 0, n ≥ 0), where N 1,..., N n are β-nf's. And if N is closed then y ∈ {x 1,..., x m }. Special cases:  an atom (m=n=0): N ≡ y  an application (m=0,n≥1): N ≡ yN 1...N n  an abstraction (m≥1): N ≡ λx 1...x m.P  an abstracted atom (m≥1,n=0): N ≡ λx 1...x m.y


Download ppt "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."

Similar presentations


Ads by Google