CORNELL CS 611 - Lecture 2 Lambda Calculus

Unformatted text preview:

CS611 Lecture 2 Lambda Calculus 28 August, 2006Lecturer: Michael Clarkson1 The Lambda Calculus1.1 Recap—λ-termsLambda calculus is a notation for describing mathematical functions and programs. A λ-term is:1. a variable x ∈ Var, where Var is a countably infinite set of variables;2. a function e0applied to an argument e1, usually written e0e1or e0(e1); or3. an expression λx. e denoting a function with input parameter x and body e.In BNF notation,e ::= x | λx. e | e0e1Parentheses are used just for grouping; they have no meaning on their own. Lambdas are greedy,extending as far to the right as they can. For simplicity, multiple variables may be placed after the lambda,and this is considered shorthand for having a lambda in front of each variable. For example, we write λxy. eas shorthand for λx. λ y. e. This shorthand is an example of syntactic sugar. The process of removing it inthis instance is called currying.Where a mathematician might write x 7→ x2, in the λ-calculus we would write λx.x2. This suggests thatfunctions are just ordinary values, and can be passed as arguments to functions (even to themselves!).The λ-calculus is a mathematical system for studying the interaction of functional abstraction and func-tional application. In the pure λ-calculus, λ-terms serve as both functions and data.1.2 Recap—BNF NotationThe grammare ::= x | λx. e | e0e1describing the syntax of the pure λ-calculus, the e is not a variable in the language, but a metavariablerepresenting a syntactic class (in this case λ-terms) in the language. It is not a variable at the level ofthe programming language. We use subscripts to differentiate syntactic metavariables of the same syntacticclass. For example, e0, e1and e all represent λ-terms.1.3 Recap—Variable BindingOccurrences of variables in a λ-term can be bound or free. In the λ-term λx. e, the lambda abstractionoperator λx binds all the free occurrences of x in e. The scope of λx in λx. e is e. This is called lexicalscoping; the variable’s scope is defined by the text of the program. It is “lexical” because it is p oss ible todetermine its scope before the program runs by inspecting the program text. A term is closed if all variablesare bound. A term is open if it is not closed.1.4 Digression—Terms and TypesThere are different kinds of expressions in a typical programming language: terms and types. We have nottalked about types yet, but we will soon. A term represents a value that exists only at run time; a type is acompile-time expression used by the compiler to rule out ill-formed programs. For now there are no types.12 Substitution and β-reductionNow we get to the question: How do we run a λ-calculus program? The main computational rule is called β-reduction. This rule applies whenever there is a subterm of the form (λx. e1) e2representing the applicationof a function λx. e1to an argument e2.Intuitively, to perform the β-reduction, we substitute the argument e2for all free occurrences of theformal parameter x in the body e1, then evaluate the resulting expression (which may involve further suchsteps).We have to be a little careful though. We cannot just substitute e2blindly for x in e1, because badthings could happen which could alter the semantics of expressions in undesirable ways. For example, if e2contained a free occurrence of a variable y, and there were a free occurrence of x in the scope of a λy in e1,then the free occurrence of y in e2would be “captured” by that λy and would end up bound to it after thesubstitution. This would not be good. However, we can avoid the problem by renaming the bound variabley.2.1 Safe SubstitutionWe wish to define a notion of safe substitution in which undesired capture of variables is avoided by judiciousrenaming of bound variables. We write e1{e2/x} to denote the result of substituting e2for all free occurrencesof x in e1according to the following rules:x{e/x} = ey{e/x} = y where y 6= x(e1e2){e/x} = e1{e/x} · e2{e/x}(λx. e0){e1/x} = λx. e0(λy. e0){e1/x} = λy. e0{e1/x} where y 6= x and y 6∈ FV(e1)(λy. e0){e1/x} = λz. e0{z/y}{e1/x} where y 6= x, z 6= x, z 6∈ FV(e0), and z 6∈ FV(e1).(There are many notations for substitution. Pierce writes [x 7→ e2]e1. Because we will be using similarnotation for some thing else, we will use the notation e1{e2/x}.)Note that the rules are applied inductively. That is, the result of a substitution in a compound term isdefined in terms of substitutions on its subterms. T he very last of the six rules applies when y ∈ FV(e1). Inthis case we can rename the bound variable y to z to avoid capture of the free occurence of y. One mightwell ask: But what if y occurs free in the scope of a λz in e0? Wouldn’t the z then be captured? The answeris that it will be taken care of in the same way, but inductively on a smaller term.Rewriting (λx. e1) e2to e1{e2/x} is the basic computational step of the λ-calculus and is called β-reduction. In the pure λ-calculus, we can start w ith a λ-term and perform β-reductions on subterms in anyorder.3 Call-by-Name and Call-by-ValueNow we have another issue. In general there may be many possible β-reductions that can b e performed ona given λ-term. How do we choose which beta reductions to perform next? Does it matter?A specification of which β-reduction to perform next is called a reduction strategy. Let us define a value tobe a closed λ-term to which no β-reductions are possible, given our chosen reduction strategy. For example,λx.x would always be value, whereas (λx.x)1 would most likely not be.Most real programming languages based on the λ-calculus use a reduction strategy known as call Byvalue (CBV). In other words, they may only call functions on values. Thus (λx.e1)e2only reduces if e2is avalue. Here is an example of a CBV evaluation s equence, assuming 3, 4, and s (the successor function) areappropriately defined.((λx.λy.y x) 3) s −→ (λy.y 3) s −→ s 3 −→ 4.Another strategy is call by name (CBN). We defer e valuation of arguments until as late as possible,applying reductions from left to right within the expression.24 Structured Operational Semantics (SOS)Let’s formalize CBV with a few inference rules.(λx. e) v −→ e{v/x}[β-reduction]e1−→ e01e1e2−→ e01e2e −→ e0v e −→ v e0This is a simple operational semantics for a programming language based on the lambda calculus. Anoperational semantics is a language semantics that describes how


View Full Document

CORNELL CS 611 - Lecture 2 Lambda Calculus

Download Lecture 2 Lambda Calculus
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 2 Lambda Calculus and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 2 Lambda Calculus 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?