Download presentation
Presentation is loading. Please wait.
Published byHope Paul Modified over 9 years ago
1
CSE 341 -- S. Tanimoto Lambda Calculus 1 Lambda Calculus What is the simplest functional language that is still Turing complete? Where do functional languages really come from? Historical Context Syntax of the Lambda Calculus Examples Alpha reduction, Beta reduction Simulating multiple arguments: “Currying” Combinators True, False, and the IF-THEN-ELSE construct Church numerals Church’s Y combinator; recursion; Factorial Church’s Thesis
2
CSE 341 -- S. Tanimoto Lambda Calculus 2 Historical Context Like Alan Turing, another mathematician, Alonzo Church, was very interested, during the 1930s, in the question “What is a computable function?” He developed a formal system known as the pure lambda calculus, in order to describe programs in a simple and precise way. Today the Lambda Calculus serves as a mathematical foundation for the study of functional programming languages, and especially for the study of “denotational semantics.” Reference: http://en.wikipedia.org/wiki/Lambda_calculus
3
CSE 341 -- S. Tanimoto Lambda Calculus 3 Syntax of the Lambda Calculus expr ::= var expr ::= var. expr expr ::= expr expr expr ::= ( expr ) var ::= x | y | z | f | n var ::= var
4
CSE 341 -- S. Tanimoto Lambda Calculus 4 Examples xA variable by itself x.xAn identity function x.x yThe identity function applied to y x. y.yA function that returns the identify function
5
CSE 341 -- S. Tanimoto Lambda Calculus 5 Constants in Lambda Calculus Constants can be added to the syntax of the Lambda Calculus: e.g., 0, 1, 2, … However, in principle they are not necessary, because certain expressions can be used to represent them. 0 = x. y.yA fn that returns an ident. fun. 1 = x. y. (x y)A fn that applies a given fn. 1 time. 2 = x. y. (x (x y))A fn that applies a given fn. twice. Etc. These are called Church numerals. Other, symbolic, constants can also be added with the understanding that they, too, can be represented by some arbitrary L.C. expressions. E.g., Apple = x. y.y ( x. y.y)
6
CSE 341 -- S. Tanimoto Lambda Calculus 6 The Successor Function Arithmetic operations can be defined using Lambda Calculus primitives. As an example, here is the function to return one plus a given number. SUCCESSOR(x) = x. y. z. (y (x y z))
7
CSE 341 -- S. Tanimoto Lambda Calculus 7 Booleans in Lambda Calculus Boolean constants can be added to the syntax of the Lambda Calculus: i.e., true, false However, in principle they are not necessary, because certain expressions can be used to represent them. False = 0 = x. y.yA fn that returns an ident. fun. True = x. y.xA fn that returns its “first arg.”
8
CSE 341 -- S. Tanimoto Lambda Calculus 8 Evaluation of Expressions Alpha conversion: Renaming of a bound variable and its bound occurrences. x. y.y x. z.z
9
CSE 341 -- S. Tanimoto Lambda Calculus 9 Evaluation of Expressions Beta reduction: Application of a function to an expression by means of a substitution of the expression for all bound occurrences of the argument variable in the body of the function. x.x y y Beta reduction must not be permitted to do variable capture. If capture would occur, use alpha conversion first to rename variables. When as many beta reductions as possible have been applied, the resulting expression is in normal form.
10
CSE 341 -- S. Tanimoto Lambda Calculus 10 Simulating multiple arguments As defined, a Lambda Calculus function can only take one argument. However, the effect of allowing multiple arguments to a function can be obtained using “currying”. ( x. y. (+ x y)) 3 5 A 2-parameter function is simulated with two 1- parameter functions. The result of applying the first function is another function. This new function accepts the second argument and produces the result of the simulated 2-parameter function. More parameters can be handled similarly.
11
CSE 341 -- S. Tanimoto Lambda Calculus 11 Combinators A combinator is a function in the Lambda Calculus having no free variables. x. y. (x y) is a combinator x. y. (x z)is not a combinator Combinators can serve nicely as modular building blocks for more complex expressions. The Church numerals and simulated booleans are examples of useful combinators.
12
CSE 341 -- S. Tanimoto Lambda Calculus 12 Conditionals in Lambda Calculus IF-THEN-ELSE: x. y. z. ((x y) z) Example evaluation of a conditional (a case where the condition is true). if true then apple else pear x. y. z. ((x y) z) true apple pear x. y. z. ((x y) z) x. y.x apple pear y. z. (( x. y.x y) z) apple pear z. (( x. y.x apple ) z) pear (( x. y.x apple ) pear) ( y. apple ) pear) apple
13
CSE 341 -- S. Tanimoto Lambda Calculus 13 Conditionals in Lambda Calculus Example evaluation of a conditional (a case where the condition is false). if false then apple else pear x. y. z. ((x y) z) false apple pear x. y. z. ((x y) z) x. y.y apple pear y. z. (( x. y.y y) z) apple pear z. (( x. y.y apple ) z) pear (( x. y.y apple ) pear) ( y.y pear) pear
14
CSE 341 -- S. Tanimoto Lambda Calculus 14 Recursion in Lambda Calculus Recursion is not directly supported in the Lambda Calculus. However, it can be simulated using Church’s Y combinator: Y = ( x. ( y. x (y y)) ( y. x (y y))) This has the unusual property of being a “fixed-point” combinator: Y f f (Y f) Here means reduces via some number of beta reduction steps.
15
CSE 341 -- S. Tanimoto Lambda Calculus 15 Factorial in the Lambda Calculus Define H as follows, to represent 1 step of recursion. Note that ISZERO, MULT, and PRED represent particular combinators that accomplish these functions. H = ( f. n.(ISZERO n) 1 (MULT n (f (PRED n)))) Then we can create FACTORIAL = Y H = ( x. ( y. x (y y)) ( y. x (y y))) ( f. n.(ISZERO n) 1 (MULT n (f (PRED n)))) Reference: http://en.wikipedia.org/wiki/Y_combinator
16
CSE 341 -- S. Tanimoto Lambda Calculus 16 Comment The use of the Lambda Calculus to express recursion in this manner is more a theoretical exercise to show the power of combinators. Few programmers would want to write software directly in the pure Lambda Calculus!
17
CSE 341 -- S. Tanimoto Lambda Calculus 17 Church’s Thesis If a function can be computed, then it can be expressed in the pure lambda calculus. Also, Turing Machines and the Lambda Calculus have equivalent expressive power, in terms of which functions can or cannot be expressed.
18
CSE 341 -- S. Tanimoto Lambda Calculus 18 Concluding Remarks It is easy to implement a lambda calculus system in Lisp (esp. Scheme) or ML. The LET construct in these languages provides a binding method similar to that of LAMBDA, except for a change in order. Both Lisp and ML themselves can be viewed as extensions of the Lambda Calculus. Functional abstraction is an important programming concept not only in theory, but also in practice.
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.