Presentation is loading. Please wait.

Presentation is loading. Please wait.

Types and Programming Languages Lecture 6 Simon Gay Department of Computing Science University of Glasgow 2006/07.

Similar presentations


Presentation on theme: "Types and Programming Languages Lecture 6 Simon Gay Department of Computing Science University of Glasgow 2006/07."— Presentation transcript:

1 Types and Programming Languages Lecture 6 Simon Gay Department of Computing Science University of Glasgow 2006/07

2 Types and Programming Languages Lecture 6 - Simon Gay2 Lambda Calculus Lambda calculus is a notation for functions, and can be viewed as a core functional programming language, either untyped (initially) or typed (later). Syntax Terms e are defined by this grammar, assuming a supply of variables x, y, z, … : e ::= x (variable) | x.e (abstraction) | e e (application) Convention: has the lowest priority.

3 2006/07Types and Programming Languages Lecture 6 - Simon Gay3 Lambda Calculus Examples x.x is a function which returns its parameter x. y.xy is a function which, given a parameter x, returns a function which, given a parameter y, returns the result of applying x to y. To obtain more concrete examples, we can combine -notation with the syntax of SEL: x.x+1 x. y.x+y x. y.if x==1 then y else y+1

4 2006/07Types and Programming Languages Lecture 6 - Simon Gay4 Scope and Variable Binding x declares a new variable x with its own scope. Occurrences of x within this scope are said to be bound. Variables which are not bound are said to be free. x.y+x boundfree The name of a bound variable is not important as long as it is the same as the variable attached to the corresponding. x.y+x is equivalent to z.y+z Bound variables can be renamed as long as free variables are not captured. x.y+x is different from y.y+y

5 2006/07Types and Programming Languages Lecture 6 - Simon Gay5 Scope and Variable Binding Renaming bound variables is called alpha-conversion and terms which differ only in the names of bound variables are said to be alpha-equivalent. It is often convenient to assume that a term has been alpha-converted so that the names of all bound variables are different from each other and different from the names of all free variables. Variables are bound by the structurally nearest matching. x.( y. x.xy)x is alpha-equivalent to x.( y. z.zy)x

6 2006/07Types and Programming Languages Lecture 6 - Simon Gay6 Variable Binding Variable binding is a fundamental concept which occurs in many places: and of course in any programming language: int f(int x) { return x+1; } { int x = 1; { int y; y = x + 1; }

7 2006/07Types and Programming Languages Lecture 6 - Simon Gay7 Substitution To define the operational semantics of the lambda calculus we need to define substitution, in a similar way to SFL. if x and z are different variables if y is not free in e In general it may be necessary to rename bound variables to avoid variable capture during substitution: WRONG! CORRECT

8 2006/07Types and Programming Languages Lecture 6 - Simon Gay8 Lambda Calculus: Operational Semantics The reduction rule for function application: is similar to the rule in SFL. It is known as beta-reduction. Example ( x.x+1)2  (x+1)[2/x] = 2+1  3 if we include the syntax and reduction rules of SEL. ( x. y.x+y)2  ( y.x+y)[2/x] = y.2+y an example of a function which returns a function.

9 2006/07Types and Programming Languages Lecture 6 - Simon Gay9 Lambda Calculus: Reduction Strategies Different reduction strategies are based on where in a term it is possible to apply beta reduction. We will concentrate on call by value reduction, which means adding the following rules: Values v are -abstractions, and any other values which we might incorporate from SEL. Other reduction strategies are possible (e.g. call by name), but this has little effect on typing (which is what we are interested in).

10 2006/07Types and Programming Languages Lecture 6 - Simon Gay10 Lambda Calculus: what I’m not telling you Lambda calculus is powerful enough to be a general-purpose programming language: any computation can be expressed. Data can be encoded as functions, for example: true x. y.x false x. y.y if c then t else e (c t) e Non-terminating computation is possible, for example: ( x.xx)( x.xx)  ( x.xx)( x.xx) and recursive functions can be encoded.

11 2006/07Types and Programming Languages Lecture 6 - Simon Gay11 Lambda Calculus + Expressions As a step towards a better functional language (BFL) we can combine -calculus and SEL. v ::= integer literal | true | false | x.e e ::= v | x | e + e | e == e | e & e | if e then e else e | ee Syntax “x” stands for any variable

12 2006/07Types and Programming Languages Lecture 6 - Simon Gay12 Lambda Calculus + Expressions Reductions The rules for SEL, and the following extra rules: (R-AppAbs) note: applied to a value (R-AppL)(R-AppR) Remember that a -abstraction is a value.

13 2006/07Types and Programming Languages Lecture 6 - Simon Gay13 Do we have a Better Functional Language? We have functions, e.g. x.x+2 is a function, but can we really do functional programming? It’s essential to be able to define recursive functions, but our language has no direct support for recursive definitions. We have no way of associating a name with a function. It is possible to express nameless recursive functions in -calculus, and it’s interesting to see how this works.

14 2006/07Types and Programming Languages Lecture 6 - Simon Gay14 Recursion in -calculus Fixed points If f is a function then a fixed point of f is a value v such that fv = v. Fixed point combinators It is possible to find a -term Y with the curious property that for any term f, Yf is a fixed point of f. In other words, f(Yf) = Yf. For example, define Y = y.( x.y(xx))( x.y(xx)) Yf = ( y.( x.y(xx))( x.y(xx)))f  ( x.f(xx))( x.f(xx))  f(( x.f(xx))( x.f(xx))) = f(Yf) (we haven’t said what ‘=’ means, but it should include reduction)

15 2006/07Types and Programming Languages Lecture 6 - Simon Gay15 Recursion in -calculus Recursive definitions and fixed points Here is a recursive function which we would like to define, but can’t because we have no mechanism for naming functions: f = x.if x==0 then 1 else x*f(x-1) (1) If we abstract on f we get the following, which we call F just for convenience: F = f. x.if x==0 then 1 else x*f(x-1) Now imagine that g is a fixed point of F: g = Fg  x.if x==0 then 1 else x*g(x-1)) which strongly suggests that the meaning of the recursive definition (1) is given by a fixed point of F.

16 2006/07Types and Programming Languages Lecture 6 - Simon Gay16 Recursion in -calculus Conclusion Given a recursive function definition such as f = x.if x==0 then 1 else x*f(x-1) we can construct the corresponding functional: F = f. x.if x==0 then 1 else x*f(x-1) whose fixed point is the function we want. This fixed point can be expressed as YF and written as a -term without naming. Example YF = ( y.( x.y(xx))( x.y(xx)))( f. x.if x==0 then 1 else x*f(x-1)) (YF)3  * 6

17 2006/07Types and Programming Languages Lecture 6 - Simon Gay17 Reading Pierce: 5 Exercises Pierce: 5.2.1, 5.3.3, 5.3.7, 5.3.8


Download ppt "Types and Programming Languages Lecture 6 Simon Gay Department of Computing Science University of Glasgow 2006/07."

Similar presentations


Ads by Google