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.
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.
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.
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) = = 6 As for the binding constructs of logic, the quantifiers, we have to be careful about how we define substitution.
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.
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)
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".
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.
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.
Slides 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).
Slides 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.
Slides 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,
Slides 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.)
Slides Example: [λx.xy/u](λy.uuy) ≡ λz.[λx.xy/u][z/y]uuy ≡ λz.[λx.xy/u]uuz ≡ λz.(λx.xy)(λx.xy)z
Slides -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.
Slides Simple properties of -conversion a.P ≡ Q ⇒ |P| = |Q| b.P ≡ Q ⇒ FV(P) = FV(Q)
Slides 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.
Slides 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))
Slides β-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.
Slides 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).
Slides 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.
Slides 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:,.
Slides 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.
Slides Example: For any term F, YF = β F(YF)
Slides 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
Slides ii.If P = β Q then there exists T such that P ⊳ β T, Q ⊳ β T. Q P T
Slides β-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.
Slides Example: (λxy.x)xy ⊳ β x x is a β-nf of (λxy.x)xy
Slides 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.
Slides 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 *β.
Slides 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).
Slides 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.
Slides 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