Download presentation

Presentation is loading. Please wait.

1

6. Introduction to the Lambda Calculus

2

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.2 Roadmap What is Computability? — Church’s Thesis Lambda Calculus — operational semantics The Church-Rosser Property Modelling basic programming constructs

3

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.3 References Paul Hudak, “Conception, Evolution, and Application of Functional Programming Languages,” ACM Computing Surveys 21/3, Sept. 1989, pp 359-411. Kenneth C. Louden, Programming Languages: Principles and Practice, PWS Publishing (Boston), 1993. H.P. Barendregt, The Lambda Calculus — Its Syntax and Semantics, North-Holland, 1984, Revised edition.

4

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.4 Roadmap What is Computability? — Church’s Thesis Lambda Calculus — operational semantics The Church-Rosser Property Modelling basic programming constructs

5

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.5 What is Computable? Computation is usually modelled as a mapping from inputs to outputs, carried out by a formal “machine,” or program, which processes its input in a sequence of steps. An “effectively computable” function is one that can be computed in a finite amount of time using finite resources. ……… ……… ……… input yes no output Effectively computable function

6

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.6 Church’s Thesis Effectively computable functions [from positive integers to positive integers] are just those definable in the lambda calculus. Or, equivalently: It is not possible to build a machine that is more powerful than a Turing machine. Church’s thesis cannot be proven because “effectively computable” is an intuitive notion, not a mathematical one. It can only be refuted by giving a counter-example — a machine that can solve a problem not computable by a Turing machine. So far, all models of effectively computable functions have shown to be equivalent to Turing machines (or the lambda calculus).

7

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.7 Uncomputability A problem that cannot be solved by any Turing machine in finite time (or any equivalent formalism) is called uncomputable. Assuming Church’s thesis is true, an uncomputable problem cannot be solved by any real computer. The Halting Problem: Given an arbitrary Turing machine and its input tape, will the machine eventually halt? The Halting Problem is provably uncomputable — which means that it cannot be solved in practice.

8

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.8 What is a Function? (I) Extensional view: A (total) function f: A B is a subset of A B (i.e., a relation) such that: 1. for each a A, there exists some (a,b) f (i.e., f(a) is defined), and 2. if (a,b 1 ) f and (a, b 2 ) f, then b 1 = b 2 (i.e., f(a) is unique)

9

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.9 What is a Function? (II) Intensional view: A function f: A B is an abstraction x.e, where x is a variable name, and e is an expression, such that when a value a A is substituted for x in e, then this expression (i.e., f(a)) evaluates to some (unique) value b B.

10

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.10 Roadmap What is Computability? — Church’s Thesis Lambda Calculus — operational semantics The Church-Rosser Property Modelling basic programming constructs

11

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.11 What is the Lambda Calculus? The Lambda Calculus was invented by Alonzo Church [1932] as a mathematical formalism for expressing computation by functions. Syntax: e::=xa variable | x. ean abstraction (function) |e 1 e 2 a (function) application Examples: x. x — is a function taking an argument x, and returning x f x — is a function f applied to an argument x NB: same as f(x) !

12

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.12 Parsing Lambda Expressions Lambda extends as far as possible to the right f.x y f.(x y) Application is left-associative x y z (x y) z Multiple lambdas may be suppressed f g.x f. g.x

13

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.13 What is the Lambda Calculus?… (Operational) Semantics: The lambda calculus can be viewed as the simplest possible pure functional programming language. conversion (renaming): x. e y. [ y/x ] e where y is not free in e reduction (application): ( x. e 1 ) e 2 [ e 2 /x ] e 1 avoiding name capture reduction: x. e x eif x is not free in e

14

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.14 Beta Reduction Beta reduction is the computational engine of the lambda calculus: Define: I x. x Now consider: I I = ( x. x) ( x. x ) [ x. x / x] x reduction = x. xsubstitution = I

15

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.15 Lambda expressions in Haskell We can implement most lambda expressions directly in Haskell: i = \x -> x ? i 5 5 (2 reductions, 6 cells) ? i i 5 5 (3 reductions, 7 cells)

16

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.16 Lambdas are anonymous functions A lambda abstraction is just an anonymous function. Consider the Haskell function: The value of compose is the anonymous lambda abstraction: f g x. f (g x) NB: This is the same as: f. g. x. f (g x) compose f g x = f(g(x))

17

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.17 A Few Examples 1. ( x.x) y 2. ( x.f x) 3. x y 4. ( x.x) ( x.x) 5. ( x.x y) z 6. ( x y.x) t f 7. ( x y z.z x y) a b ( x y.x) 8. ( f g.f g) ( x.x) ( x.x) z 9. ( x y.x y) y 10. ( x y.x y) ( x.x) ( x.x) 11. ( x y.x y) (( x.x) ( x.x))

18

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.18 Free and Bound Variables The variable x is bound by in the expression: x.e A variable that is not bound, is free : An expression with no free variables is closed. (AKA a combinator.) Otherwise it is open. For example, y is bound and x is free in the (open) expression: y. x y fv(x)={ x } fv(e 1 e 2 )= fv(e 1 ) fv(e 2 ) fv( x. e) =fv(e) – { x }

19

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.19 “Hello World” in the Lambda Calculus hello world Is this expression open? Closed?

20

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.20 Roadmap What is Computability? — Church’s Thesis Lambda Calculus — operational semantics The Church-Rosser Property Modelling basic programming constructs

21

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.21 Why macro expansion is wrong Syntactic substitution will not work: ( x y. x y ) y [ y / x] ( y. x y) reduction ≠ ( y. y y )incorrect substitution! Since y is already bound in ( y. x y), we cannot directly substitute y for x.

22

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.22 Substitution We must define substitution carefully to avoid name capture: [e/x] x=e [e/x] y=yif x ≠ y [e/x] (e 1 e 2 )=([e/x] e 1 ) ([e/x] e 2 ) [e/x] ( x. e 1 ) = ( x. e 1 ) [e/x] ( y. e 1 ) = ( y. [e/x] e 1 )if x ≠ y and y fv(e) [e/x] ( y. e 1 ) = ( z. [e/x] [z/y] e 1 ) if x ≠ y and z fv(e) fv(e 1 ) ( x. (( y. x) ( x. x)) x ) y [y / x] (( y. x) ( x. x)) x = (( z. y ) ( x. x)) y Consider:

23

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.23 Alpha Conversion Alpha conversions allow us to rename bound variables. A bound name x in the lambda abstraction ( x.e) may be substituted by any other name y, as long as there are no free occurrences of y in e: Consider: ( x y. x y ) y ( x z. x z) y conversion [ y / x] ( z. x z) reduction ( z. y z) = y reduction

24

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.24 Eta Reduction Eta reductions allow one to remove “redundant lambdas”. Suppose that f is a closed expression (i.e., there are no free variables in f). Then: ( x. f x ) y f y reduction So, ( x. f x ) behaves the same as f ! Eta reduction says, whenever x does not occur free in f, we can rewrite ( x. f x ) as f.

25

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.25 ( x y. x y) ( x. x y) ( a b. a b)NB: left assoc. ( x z. x z) ( x. x y) ( a b. a b) conversion ( z. ( x. x y) z) ( a b. a b) reduction ( x. x y) ( a b. a b) reduction ( a b. a b) y reduction ( b. y b) reduction y reduction

26

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.26 Normal Forms A lambda expression is in normal form if it can no longer be reduced by beta or eta reduction rules. Not all lambda expressions have normal forms! = ( x. x x) ( x. x x) [ ( x. x x) / x ] ( x x ) =( x. x x) ( x. x x) reduction ( x. x x) ( x. x x) reduction … Reduction of a lambda expression to a normal form is analogous to a Turing machine halting or a program terminating.

27

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.27 Evaluation Order Most programming languages are strict, that is, all expressions passed to a function call are evaluated before control is passed to the function. Most modern functional languages, on the other hand, use lazy evaluation, that is, expressions are only evaluated when they are needed. Consider: Applicative-order reduction: Normal-order reduction: sqr n = n * n sqr (2+5) sqr 7 7*7 49 sqr (2+5) (2+5) * (2+5) 7 * (2+5) 7 * 7 49

28

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.28 The Church-Rosser Property “If an expression can be evaluated at all, it can be evaluated by consistently using normal-order evaluation. If an expression can be evaluated in several different orders (mixing normal-order and applicative order reduction), then all of these evaluation orders yield the same result.” So, evaluation order “does not matter” in the lambda calculus.

29

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.29 Roadmap What is Computability? — Church’s Thesis Lambda Calculus — operational semantics The Church-Rosser Property Modelling basic programming constructs

30

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.30 Non-termination However, applicative order reduction may not terminate, even if a normal form exists! Compare to the Haskell expression: ( x. y) ( ( x. x x) ( x. x x) ) Applicative order reductionNormal order reduction ( x. y) ( ( x. x x) ( x. x x) ) y ( x. y) ( ( x. x x) ( x. x x) ) … (\x -> \y -> x) 1 (5/0) 1

31

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.31 Currying Since a lambda abstraction only binds a single variable, functions with multiple parameters must be modelled as Curried higher-order functions. As we have seen, to improve readability, multiple lambdas are suppressed, so: x y. x= x. y. x b x y. b x y= b. x. y. ( b x ) y

32

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.32 Representing Booleans Many programming concepts can be directly expressed in the lambda calculus. Let us define: True x y. x False x y. y not b. b False True if b then x else y b x y. b x y then: not True= ( b. b False True ) ( x y. x ) ( x y. x ) False True False if True then x else y= ( b x y. b x y ) ( x y. x) x y ( x y. x) x y x

33

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.33 Representing Tuples Although tuples are not supported by the lambda calculus, they can easily be modelled as higher-order functions that “wrap” pairs of values. n-tuples can be modelled by composing pairs… Define:pair ( x y z. z x y) first ( p. p True ) second ( p. p False ) then:(1, 2)=pair 1 2 ( z. z 1 2) first (pair 1 2) (pair 1 2) True True 1 2 1

34

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.34 Tuples as functions In Haskell: t= \x -> \y -> x f= \x -> \y -> y pair= \x -> \y -> \z -> z x y first= \p -> p t second= \p -> p f ? first (pair 1 2) 1 ? first (second (pair 1 (pair 2 3))) 2

35

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.35 What you should know! Is it possible to write a Pascal compiler that will generate code just for programs that terminate? What are the alpha, beta and eta conversion rules? What is name capture? How does the lambda calculus avoid it? What is a normal form? How does one reach it? What are normal and applicative order evaluation? Why is normal order evaluation called lazy? How can Booleans and tuples be represented in the lambda calculus?

36

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.36 Can you answer these questions? How can name capture occur in a programming language? What happens if you try to program in Haskell? Why? What do you get when you try to evaluate (pred 0)? What does this mean? How would you model numbers in the lambda calculus? Fractions?

37

© O. Nierstrasz PS — Introduction to the Lambda Calculus 6.37 License > http://creativecommons.org/licenses/by-sa/2.5/ Attribution-ShareAlike 2.5 You are free: to copy, distribute, display, and perform the work to make derivative works to make commercial use of the work Under the following conditions: Attribution. You must attribute the work in the manner specified by the author or licensor. Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under a license identical to this one. For any reuse or distribution, you must make clear to others the license terms of this work. Any of these conditions can be waived if you get permission from the copyright holder. Your fair use and other rights are in no way affected by the above.

Similar presentations

© 2023 SlidePlayer.com Inc.

All rights reserved.