1 / 17

Types and Programming Languages

Types and Programming Languages. Lecture 6. Simon Gay Department of Computing Science University of Glasgow. 2006/07. 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).

step
Download Presentation

Types and Programming Languages

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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 variablesx, y, z, … : e ::= x (variable) | x.e (abstraction) | e e (application) Convention:  has the lowest priority. Types and Programming Languages Lecture 6 - Simon Gay

  3. 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 Types and Programming Languages Lecture 6 - Simon Gay

  4. 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 free bound 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 Types and Programming Languages Lecture 6 - Simon Gay

  5. 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 Types and Programming Languages Lecture 6 - Simon Gay

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

  7. 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 Types and Programming Languages Lecture 6 - Simon Gay

  8. 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. Types and Programming Languages Lecture 6 - Simon Gay

  9. 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). Types and Programming Languages Lecture 6 - Simon Gay

  10. 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. Types and Programming Languages Lecture 6 - Simon Gay

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

  12. 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. Types and Programming Languages Lecture 6 - Simon Gay

  13. 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. Types and Programming Languages Lecture 6 - Simon Gay

  14. 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) Types and Programming Languages Lecture 6 - Simon Gay

  15. 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. Types and Programming Languages Lecture 6 - Simon Gay

  16. 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 Types and Programming Languages Lecture 6 - Simon Gay

  17. Reading Pierce: 5 Exercises Pierce: 5.2.1, 5.3.3, 5.3.7, 5.3.8 Types and Programming Languages Lecture 6 - Simon Gay

More Related