Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lambda Calculus1 The Untyped Lambda-Calculus Hossein Hojjat University of Tehran Formal Methods Laboratory.

Similar presentations


Presentation on theme: "Lambda Calculus1 The Untyped Lambda-Calculus Hossein Hojjat University of Tehran Formal Methods Laboratory."— Presentation transcript:

1 Lambda Calculus1 The Untyped Lambda-Calculus Hossein Hojjat University of Tehran Formal Methods Laboratory

2 Hossein HojjatLambda Calculus2 “Whatever the next 700 languages turn out to be, they will surely be variants of lambda calculus.” Landin, Peter J. The next 700 programming languages. Communications of the ACM, 9(3):157–166, March 1966.

3 Hossein HojjatLambda Calculus3 Part 1 : Introduction

4 Hossein HojjatLambda Calculus4 History  David Hilbert (a German mathematician) delivered a famous speech to the Second International Congress of Mathematicians in Paris (1900) –The Problems of Mathematics  Perhaps the most influential speech ever given to mathematicians, given by a mathematician, or given about mathematics

5 Hossein HojjatLambda Calculus5 History  Hilbert outlined 23 major mathematical problems to be studied in the coming century  David Hilbert's tenth problem was to devise an algorithm that tests whether a polynomial has an integral root –Entscheidungsproblem (English: decision problem)  Find a process which determines the result in a finite number of operations

6 Hossein HojjatLambda Calculus6 History  Church and Turing independently proved that the Entscheidungsproblem is unsolvable  Church proved that there is no algorithm (defined via recursive functions) which decides for two given lambda calculus expressions whether they are equivalent or not  Turing reduced the halting problem for Turing machines to the Entscheidungsproblem

7 Hossein HojjatLambda Calculus7 λ-calculus strength  Although both models appear very different from one another, Turing later showed that were equivalent –They each pick out the same set of mathematical functions  Church-Turing thesis: any problem that has an algorithmic solution can be solved on a Turing machine –It implies a universality among different models of computation.

8 Hossein HojjatLambda Calculus8 Suitability  Lambda-calculus is more suitable as an abstract model of programming language rather than as a practical programming language –It is used to study programming languages semantics from a mathematical perspective called Denotational Semantics

9 Hossein HojjatLambda Calculus9 Overview  In the mid 1960s, Peter Landin observed that a complex programming language can be understood by formulating it as –A tiny core calculus capturing the language's essential mechanisms –A collection of convenient derived forms whose behavior is understood by translating them into the core

10 Hossein HojjatLambda Calculus10 Overview  The core language used by Landin was the lambda-calculus  Following Landin's insight, the lambda- calculus has seen widespread use in the specification of programming language features –in language design and implementation –in the study of type systems

11 Hossein HojjatLambda Calculus11 Overview  The lambda-calculus is just one of a large number of core calculi that have been used for similar purposes –The pi-calculus has become a popular core language for defining the semantics of message- based concurrent languages –The object calculus distills the core features of object-oriented languages

12 Hossein HojjatLambda Calculus12 Overview  Most of the concepts and techniques of the lambda-calculus can be transferred quite directly to other calculi

13 Hossein HojjatLambda Calculus13 Enriching the calculus  The lambda-calculus can be enriched in a variety of ways  It is often convenient to add special concrete syntax for features like numbers, tuples, records, etc.  The behavior of these features can already be simulated in the core language

14 Hossein HojjatLambda Calculus14 Enriching the calculus  We can add more complex features such as mutable reference cells or nonlocal exception handling  These features can be modeled in the core language only by using rather heavy translations  Such extensions lead eventually to languages such as ML, Haskell or Scheme

15 Hossein HojjatLambda Calculus15 Basics  Procedural (or functional) abstraction is a key feature of essentially all programming languages  Instead of writing the same calculation over and over –Write a procedure or function that performs the calculation generically in terms of one or more named parameters –Instantiate this function as needed, providing values for the parameters in each case

16 Hossein HojjatLambda Calculus16 Example  Consider (5*4*3*2*1) + (7*6*5*4*3*2*1) - (3*2*1)  Normally rewrite it as factorial(5) + factorial(7) - factorial(3), where factorial(n) = if n=0 then 1 else n * factorial(n-1)  Instantiating the function factorial (for n≥0) with the argument n yields the factorial of n as result

17 Hossein HojjatLambda Calculus17 The λ Notation  We write "λn...." as a shorthand for “The function that, for each n, yields...,”  factorial = λn. if n=0 then 1 else n * factorial(n-1)  The lambda-calculus (or λ-calculus) embodies this kind of function definition and application in the purest possible form  Everything is a function!

18 Hossein HojjatLambda Calculus18 Why lambda?  Principia Mathematica : a three-volume work on the foundations of mathematics –written by Alfred North Whitehead and Bertrand Russell in 1910-1913  In this book the notation for the function f with is  Church originally intended to use the notation

19 Hossein HojjatLambda Calculus19 Why lambda?  The typesetter could not position the hat on the top of the x, and placed it in front of it  Another typesetter, changed it to lambda!

20 Hossein HojjatLambda Calculus20 Syntax  A term is defined as follows: e::=x(identifier) |e 0 e 1 (application) |λx. e(abstraction)  In an abstraction: –x is the argument –e is the body of the function

21 Hossein HojjatLambda Calculus21 λ Expression  The only keywords used in the language are λ and dot  An expression can be surrounded with parenthesis for clarity

22 Hossein HojjatLambda Calculus22 Example  The identity function : λx. x  The name after the λ is the identifier of the argument of this function  The expression after the point is the “body” of the definition

23 Hossein HojjatLambda Calculus23 Application  Functions can be applied to expressions (λx.x) y  This is the identity function applied to y  Parenthesis are used for clarity in order to avoid ambiguity –Function application is left associative  The result is : y

24 Hossein HojjatLambda Calculus24 Scope  In λ calculus all names are local to definitions  λx.t : The scope of x is the term t  An occurrence of the variable x is said to be bound when it occurs in the body t of an abstraction λx.t (λx. y x)(λy. x y) bound free bound free

25 Hossein HojjatLambda Calculus25 Free variables  We can define formally the free variables of an expression E recursively as follows: Free(x) = {x} Free(E 1 E 2 ) = Free(E 1 )  Free(E 2 ) Free(λx. E) = Free(E) – {x}  Example: Free(λx. x (λy.x y z)) = {z}

26 Hossein HojjatLambda Calculus26 Computation  In its pure form, the lambda-calculus has no built-in constants or primitive operators –no numbers, arithmetic operations, conditionals, records, loops, sequencing, I/O, etc.  Computation is achieved by application of functions to arguments (which themselves are functions)  Each step in the computation consists of abstraction the right-hand component for the bound variable in the abstraction's body

27 Hossein HojjatLambda Calculus27 Computation  Graphically, we write: – means: “the term obtained by replacing all free occurrences of x in M by N”

28 Hossein HojjatLambda Calculus28 Example  What does λs.(s s) mean? –Self application function  (λs.(s s) λx.x) = (λx.x λx.x) = λx.x  What does this lambda expression mean? (λs. (s s) λs. (s s))

29 Hossein HojjatLambda Calculus29 Example  λfunc. λarg. (func arg)  This is the function application function  It returns a second function which then applies the first function's argument to the second function’s argument

30 Hossein HojjatLambda Calculus30 Example  λfirst. λsecond. first –Selecting the first of two arguments –(λx. λy. x) a b = (λy. a) b = a  λfirst. λsecond. second –Selecting the second of two arguments –(λx. λy. y) a b = (λy. y) b = b

31 Hossein HojjatLambda Calculus31 Naming Conventions  A term of the form (λx. M) N is called a redex ("reducible expression")  The operation of rewriting a redex according to the above rule is called beta-reduction

32 Hossein HojjatLambda Calculus32 Evaluation Strategies  Several different evaluation strategies for the lambda-calculus have been studied over the years by programming language designers and theorists  Each strategy defines which redex or redexes in a term can fire on the next step of evaluation

33 Hossein HojjatLambda Calculus33 Evaluation Strategies  For example, consider: (λx.x) ((λx.x) (λz. (λx.x) z))  We can write it more readable as: id (id (λz. id z))  This term contains three redexes: id (id (λz. id z))

34 Hossein HojjatLambda Calculus34 Full beta-reduction  Any redex may be reduced at any time  At each step we pick some redex, anywhere inside the term we are evaluating, and reduce it  For example, we can begin with the innermost redex, then do the one in the middle, then the outermost id (id (λz. id z)) → id (id (λz.z)) → id (λz.z) → λz.z ↛

35 Hossein HojjatLambda Calculus35 Normal Order  The leftmost, outermost redex is always reduced first  id (id (λz. id z)) → id (z. id z) → λz. id z → λz.z ↛

36 Hossein HojjatLambda Calculus36 Why Not Normal Order?  In most (all?) programming languages, functions are considered values (fully evaluated)  Thus, no reduction is done inside of functions (under the lambda)  No popular programming language uses normal order

37 Hossein HojjatLambda Calculus37 Call by Name-Call by Value  Consider the application e 0 e 1  Call by value: evaluate the argument e 1 to a value before β reduction  Call by name: reduce the application, without evaluating e 1  In both cases: A value is an abstraction: λx. e

38 Hossein HojjatLambda Calculus38 Call by Name-Call by Value  Call by name: id (id (λz. id z)) → id (λz. id z) → λz. id z ↛  Call by value: id (id (λz. id z)) → id (λz. id z) → λz. id z ↛

39 Hossein HojjatLambda Calculus39 Comparison  The call-by-value strategy is strict  The arguments to functions are always evaluated, whether or not they are used by the body of the function  Non-strict (or lazy) strategies evaluate only the arguments that are actually used –call-by-name –call-by-need

40 Hossein HojjatLambda Calculus40 Call by Value vs. Name  The debate about whether languages should be by value or by name is nearly 20 years old  This debate is confined to the functional programming community  Outside the functional community call by name is rarely considered

41 Hossein HojjatLambda Calculus41 Languages Hierarchy  Call-by-name lambda calculus –Haskell –Miranda  Call-by-value lambda calculus –Lisp –Scheme –ML

42 Hossein HojjatLambda Calculus42 Name Clashes  Consider the function application function –def apply = λfunc. λarg. (func arg)  Consider: ((apply arg) salam) = (( (λfunc. λarg. (func arg)) arg) salam)  arg is used both as a –function bound variable –free variable

43 Hossein HojjatLambda Calculus43 Name Clashes  If we carry out β reduction: (( (λfunc. λarg. (func arg)) arg) salam) → (λarg. (arg arg) salam) → (salam salam)  This was not intended at all!  The argument arg has been substituted in the scope of the bound variable arg,and appears to create a new occurrence of that variable

44 Hossein HojjatLambda Calculus44 Renaming  We can avoid this problem by using consistent renaming  For example, we can replace the bound variable arg with arg1 (( (λfunc. λarg1. (func arg1)) arg) salam) → (λarg1. (arg arg1) salam) → (arg salam)

45 Hossein HojjatLambda Calculus45 α-Conversion  A name clash arises when –A β-reduction places an expression with a free variable in the scope of a bound variable with the same name as the free variable  The names of bound variables are unimportant (as far as the meaning of the computation goes)  We will treat λx. x and λy. y as if they are (absolutely and totally) indistinguishable –we can always use one in place of the other

46 Hossein HojjatLambda Calculus46 Equivalence Relation  An equivalence relation is defined that captures the intuition that two expressions denote the same function  This equivalence relation is defined by the so- called alpha-conversion rule and the beta- reduction rule  There is a third rule, η-conversion, which may be added to these two to form a new equivalence relation

47 Hossein HojjatLambda Calculus47 η-Conversion  η-Conversion is useful to eliminate redundant lambda abstractions  If the sole purpose of a lambda abstraction is to pass its argument to another function, then the lambda abstraction is redundant  λx. (E x)  E, if x  Free(E)

48 Hossein HojjatLambda Calculus48 η-Conversion Usage  η-Conversion is similar to the proxy pattern in OO programming  In “eager evaluation” languages is used (like Scheme)  It creates a wrapper around a lambda expression to prevent immediate evaluation

49 Hossein HojjatLambda Calculus49 Examples  Valid conversion λx. add x → η add λy. add x y → η add x  Invalid conversion λx. add x x → η add x  Valid conversion λx. (λy. y) x → η λy. y  What about λx. λy. y x

50 Hossein HojjatLambda Calculus50 Part 2 : Programming in the Lambda-Calculus

51 Hossein HojjatLambda Calculus51  The lambda-calculus is much more powerful than its tiny definition might suggest  In this part, we develop a number of standard examples of programming in the lambda- calculus  How to make –Booleans and conditional functions –Numerals and arithmetic functions

52 Hossein HojjatLambda Calculus52 Church Booleans  We represent TRUE by select_first function def true = λt. λf. t  We represent FALSE by select_second function def false = λt. λf. f  As a motivation for this representation, consider the C conditional expression: ? :

53 Hossein HojjatLambda Calculus53 Conditional Expression  The definition of TRUE and FALSE suggest the definition for a conditional construct def cond = λe1. λe2. λc. ((c e1) e2)  We will use the conditional definition with true and false to model some logical operators

54 Hossein HojjatLambda Calculus54 Not  The Not operator can be written using a conditional expression: x ? FALSE : TRUE  def not = λx. ( ( (cond false) true) x)  This expression can be written as follows: λx. ((x false) true)  Exercise: Use the above result to calculate NOT FALSE

55 Hossein HojjatLambda Calculus55 And  x  y  ( x ? y) : FALSE  def and = λx. λy. ( ( (cond y) false) x)  This expression can be written as follows: λx. λy. ( (x y) false)  Exercise: Use the above result to calculate AND TRUE FALSE  Exercise: Find an expression for OR

56 Hossein HojjatLambda Calculus56 Church Numerals  There are several possible ways to define the natural numbers in lambda calculus  The most common are the Church numerals  First we define f n x which means n application of f to x f 5 x = f ( f ( f ( f ( f x) ) ) )

57 Hossein HojjatLambda Calculus57 Church Numerals  The intuition behind Church numerals is to represent a number n as a function that transforms any function f into the function f n 0 := λ f. λ x. x 1 := λ f. λ x. f x 2 := λ f. λ x. f( f x) … n := λ f. λ x. f n x

58 Hossein HojjatLambda Calculus58 Successor Function  Given this definition of the Church numerals, we can define a successor function  This implementation uses the argument number n to construct the function f n, then applies f one more time to get f n+1  def succ = λ n. λ f. λ x. f (n f x)  Exercise: Try succ 1 == 2

59 Hossein HojjatLambda Calculus59 Addition  Adding m to n is simply taking the successor of n, m times  add m n = m succ n = λm. λn. λf. λx. m f ( n f x)  Let’s check it: add m n → λf. λx. m f ( n f x) → λf. λx. f m ( f n x) → λf. λx. f m+n x  m + n

60 Hossein HojjatLambda Calculus60 Multiplication  Since add takes its arguments one at a time, applying it to just one argument n yields the function that adds n to whatever argument it is given  Passing this function as the first argument to m and 0 as the second argument means: –apply the function that adds n to its argument, iterated m times, to zero, i.e., add together m copies of n.  def mult = λm. λn. m (add n) 0

61 Hossein HojjatLambda Calculus61 Exercise  Define a term for raising one number to the power of another.


Download ppt "Lambda Calculus1 The Untyped Lambda-Calculus Hossein Hojjat University of Tehran Formal Methods Laboratory."

Similar presentations


Ads by Google