1 / 19

159.331 Programming Languages & Algorithms

159.331 Programming Languages & Algorithms. Lecture 24 - Functional Programming Languages - Part 6 Lambda Calculus. Lambda Calculus. Alonzo Church’s notation for studying types

mura
Télécharger la présentation

159.331 Programming Languages & Algorithms

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. 159.331 Programming Languages & Algorithms Lecture 24 - Functional Programming Languages - Part 6 Lambda Calculus Prog Lang & Alg

  2. Lambda Calculus • Alonzo Church’s notation for studying types • Supposedly inspired by Whitehead and Russell notation for the class of all x’s such that f(x) ; to wit ^xf(x) - moved the carat ^ down and it became a lambda  • x.M is used for a function with parameter x and body M • So x.x*x is a function that maps 5 to 5 * 5 • Functions are written next to their arguments so f a is the application of function f to argument a • (x.x*x) 5 applies our function to 5 yielding 25 Prog Lang & Alg

  3. We call formulas like (x.x*x) 5 terms • Church’s original formulation of -calculus was for general properties of functions not tied to any particular problem area. The integer 5 and the multiplication operator below to the problem area “arithmetic” and are not part of the pure calculus • A grammar for terms in the pure -calculus is: • M ::= x | (M1 M2 ) | (x.M) • We use letters f, x, y, z for variables and M, N,P, Q for terms • A term is a variable x, or an application (M N) of function M to N or an abstraction (x.M) Prog Lang & Alg

  4. We are allowed constants such as c which can represent values like integers and operations on data structures like lists • c can stand for basic constants like true and nil or constant functions like head and + • Pure -calculus is untyped - Functions can be applied freely; and it even makes sense to write (x,x) where x is applied to itself • A functional programming language is essentially a -calculus with appropriate constants Prog Lang & Alg

  5. Equality of Pure  Terms • Equality relation on terms - known historically as -equality. We write: • M = N if M and N are “beta-equal” • Informally if M = N, then M and N must have the same value • Beta-equality deals with idea of applying an abstraction (x.M) to an argument N - ie the notion of function call and parameter passing in programming languages. • An abstraction corresponds to a a function definition, an application to a function call. Prog Lang & Alg

  6. For function square defined by: fun square(x) = x * x; • The function call square(5) is evaluated by substituting 5 for x in the body x*x • We say that square(5) = 5 * 5. Prog Lang & Alg

  7. Syntactic Conventions • We drop parentheses from (M N) and (x.M) • In the absence of parentheses, function application groups from left to right, so that x y z abbreviates ((x y) z) , and the parentheses in x( y z) are necessary. • Function application has higher precedence than abstraction, so x. x z abbreviates (x. (x z)) . • We will use the following terms in examples: • I = x.x • K = xy.x • S = xyz. (x z) (y z) or in full: (x.(y.(z.((x z)(y z))))) • A pure  term without free variables is called a closed term or a combinator Prog Lang & Alg

  8. Free and Bound Variables • Abstractions of the form x.M are referred to as bindings because they constrain the role of x in x.M. Variable x is said to be bound in x.M • The set free(M) of free variables of M, the set of variables that appear unbound in M is given by the following rules: free(x) = {x} free(M N) = free(M)  free(N) free (x.M) = free(M) - {x} • Variable x is free in the term x • Variable is free in M N if it is either free in M or free in N • With exception of x all other free variables of M are free in x.M Prog Lang & Alg

  9. In (y.z) (z.z) z is a free variable because it is free in the term y.z (union rule above) • The occurrence of x to the right of  in x.M is called a binding occurrence or simply a binding of x. • All occurrences of x in x.M are bound within the scope of this binding. • All unbound occurrences of a variable in a term are free and each occurrence is either free or bound - it cannot be both. In: y .  z . x z ( y z ) • Lines go from a binding to bound occurrence(s) Prog Lang & Alg

  10. Substitution • Result of applying an abstraction (x.M) to an argument N will be formalized by “substituting” N for x in M. Informally N replaces all free occurrences of x in M • A simple definition for substitution of a term N for a variable x in M is written as {N/x}M where: • If the free variables of N have no bound occurrences in M then the term {N/x}M is formed by replacing all free occurrences of x in M by N • Otherwise, if the variable y is free in N and bound in M, consistently replace the binding and corresponding bound occurrences of y in M by some fresh variable z. Repeat the renaming of bound variables until case 1 applies, then proceed as in 1. Prog Lang & Alg

  11. In each of the following, M has no bound occurrences, so N replaces all occurrences of x in M to form {N/x}M {u/x} x = u {u/x} (x x) = (u u) {u/x} (x y) = (u y) {u/x} (x u) = (u u) {(x.x)/x} x = (x.x) • (read the “/” as “substitutes for” ) Prog Lang & Alg

  12. In the following cases, M has no free occurrences of x, so {N/x} M is M itself. {u/x} y = y {u/x} (y z) = (y z) {u/x} (y.y) = (y.y) {u/x} (x.x) = (x.x) {(x.x)/x} y = y Prog Lang & Alg

  13. In the following cases, free variable u in N has bound occurrences in M, so {N/x}M is formed by first renaming the bound occurrence of u in M: {u/x} (u.x) = {u/x} (z.y) = (z.u) {u/x} (u.u) = {u/x} (z.z) = (z.z) Prog Lang & Alg

  14. Beta-Equality • The key axiom (beta axiom) of beta equality is: • (x.M) N = {N/x} M • So that (x.x) u = u and (x.y) u = y • The following axiom allows bound variables to be systematically renamed: • ( x.M) = z. {z/x}M , provided that z is not free in M (known as the alpha axiom) • So that x.x = y.y and xy.x = uv.u • Remaining rules for beta-equality formalize general properties: • Idempotence. A term M equals itself • Commutativity. If M equals N then N must equal M • Transitivity. If M equals N and N equals P then M equals P Prog Lang & Alg

  15. Prog Lang & Alg

  16. The replacement of equals for equals is formalised by the two congruence rules (6 & 7) • The first rule can be read as: If M = M and N = N then M N = M N • Furthermore: • If M = M , then x.M = x.M • Think of the axiom rule format written as a list of conditions on the numerator with the conclusion on the denominator Prog Lang & Alg

  17. Prog Lang & Alg

  18. Summary Prog Lang & Alg

  19. Further Reading • See Bal & Grune Chapter 4 • See Sebesta Chapter 15 • See also Hudak’s ACM article on Conception, Evolution and Application of Functional Programming Languages Prog Lang & Alg

More Related