1 / 29

Lambda calculus

Lambda calculus. What is λ-calculus ?. “Lambda calculus is a formal system  in  mathematical logic  and computer science for expressing computation based on function abstraction and application using variable  binding and  substitution ” --Wikipedia.

chione
Download Presentation

Lambda calculus

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. Lambda calculus

  2. What is λ-calculus ? “Lambda calculusis a formal system in mathematical logic and computer science for expressing computation based on function abstraction and application using variable binding and substitution” --Wikipedia

  3. Lambda calculus in history of mathematics • The lambda calculus was introduced by mathematician Alonzo Church in the 1930s . •  The original system was shown to be logically inconsistent in 1935 when Stephen Kleene and J. B. Rosser developed the Kleene–Rosser paradox. • Subsequently, in 1936 Church isolated and published just the portion relevant to computation, what is now called the untyped lambda calculus. • The λ-calculus was re-discovered as a versatile tool in Computer Science by people like McCarthy, Landin, and Scott in the 1960s. Mc-Carthy came out with his list processing language Lisp in 1960. McCarthy knew of the λ-calculus, and his language closely resembles it. • Now used as • Tool for investigating computability • Basis of functional programming languages Lisp, Scheme, Haskell,ML…

  4. Expressions in the λ-calculus • Expressions in the λ-calculus are written in strict prefix form • Function and argument are simply written next to each other. If a function takes more than one argument, then these are simply lined up after the function. Thus: • Brackets are employed only to enforce a special grouping *The -calculus is a purely syntactic device, it does not make any distinctions between simple entities

  5. Functions in the λ-calculus • Function formation is sometimes written as an equation, , sometimes as a mapping . • In the λ-calculus a special notation is available - The Greek letter λ. • The λalerts that the variable is not part of an expression but the formal parameter of the function . The dot after the formal parameter introduces the function body. • A function which has been written in λ-notation can itself be used in an expression. • why the brackets around the function? They are there to make clear where the definition of the function ends. (assuming that is interpreted as a 3-ary function)

  6. Functions in the λ-calculus • Although it is not strictly necessary, it will be convenient to introduce abbreviations for-terms. • If body of a function consists of another function, as here we could also consider it as a function of two arguments. • If we want to see it as a function of two arguments then we can leave out the brackets:

  7. The official definition • Function formation and function application are all that there is. • λ-terms are constructed according to the grammar: • The placeholder c represents any constant, such as numbers 1, 2, 3,... or arithmetic operators etc. • the letter x represents any of infinitely many possible variables. The given grammar is ambiguous; the term could be parsed as *(we use “app” to indicate use of the clause in the derivation)

  8. - Reduction • There is only one rule of computation, called reduction , it concerns the replacement of a formal parameter by an actual one. 5 * 3 5 • When no further reductions are possible, we say that the term has been reduced to normal form. • Is every term has a normal form ? NO ! The term Ω always reduces to itself!

  9. Confluence • It may be that a λ-term offers many opportunities for reduction at the same time. • it is necessary that the result of a computation is independent from the order of reduction Theorem 1 (Church-Rosser) If a term M can be reduced (in several steps) to terms N and P, then there exists a term Q to which both N and P can be reduced (in several steps). As a picture:

  10. Confluence Theorem 1 (Church-Rosser) If a term M can be reduced (in several steps) to terms N and P, then there exists a term Q to which both N and P can be reduced (in several steps). Intuition: Lets look at a specific case where every variable can appear 0/1 time in a term. Base case - divided into 3 cases: 2.

  11. Confluence 3. And if you believed me up until now the rest is very simple…  . . . . . . . . .

  12. Confluence Corollary 2 Every λ-term has at most one normal form. Proof. For the sake of contradiction, assume that there are normal forms N and P to which a certain term M reduces: By the theorem of Church and Rosser there is a term Q to which both N and P can be reduced. However, N and P are assumed to be in normal form, so they don’t allow for any further reductions. The only possible interpretation is that N = P = Q.

  13. Free and bound variables • The operator is a binding operator. Variables that fall within the scope of an abstraction are said to be bound. All other variables are called free.  • Note: variable is bound by its "nearest" abstraction • (the single occurrence of x in the expression is bound by the second lambda) Formal Definition: • The set of free variables of a lambda expression, M, is denoted as FV(M) and is defined by recursion on the structure of the terms, as follows: • FV(x) = {x}, where x is a variable • FV(λx.M) = FV(M) \ {x} • FV(M N) = FV(M) ∪ FV(N)

  14. conversion • Alpha - conversion, allows bound variable names to be changed • The only variable occurrences that are renamed are those that are bound to the same abstraction • Alpha-conversion is not possible if it would result in a variable getting captured by a different abstraction ( ( (

  15. conversion • Two functions are the same if and only if they give the same result for all arguments. • Eta - conversion converts between and whenever does not appear free in

  16. Higher-order functions • Let us look at an example: A -term for squaring integers is given by • If we want to compute • In -calculus notation, we would write for the “power-8”-function: • It is now a simple step to write out a -term which applies any function three times: • Operators such as T are called higher order because they operate on functions rather than numbers.

  17. Iteration and recursion • How can we generalize what we saw, in the high order function, to get the behavior of a for-loop? • First of all let us define some helpful new “constants”: 1 - Its behavior is like an if-then-else clause: (In , we would write this as 2. “pred” for predecessor function on natural numbers.

  18. Iteration and recursion • Now we construct a term (for “Iteration”) which takes as arguments a number, a function , and a value , and computes the application of to : (If then should simply return ) Example:

  19. Well-typed - terms • there is nothing in the grammar which stops us from forming awful terms, such as “sin log”. • Such terms do not make any sense at all, and any sensible programming language compiler would reject them. • What is missing in the calculus is a notion of type. For example, the type of the sin function should be “accepts real numbers and produces real numbers”. • A language for expressing these properties (i.e., types) is easily defined- We start with some base types such as “int” and “real”, and then form function types on top of them. • The grammar: c represents all the base types

  20. Well-typed - terms • On the basis of a type system, we can formulate restrictions on what kind of terms are valid (or well-typed). We do so by employing an inductive definition: Definition (Well-typed -terms): • Base case. For every type and every variable , the term is well-typed and has type. • Function formation. For every term of type , every variable , and every type , the termis well-typed and has type . • Application. If is well-typed of type and is well-typed of type then is well-typed and has type • O

  21. Well-typed - terms Calculating simple types: • It is quite easy to find out whether a term can be typed or not by following the steps in which the term was constructed. • What we do is to annotate subterms with type expressions which still contain type variables A,B,C, . . . and which we refine as we go along. • Consider for example, the term )). *At this stage the type variables can be instantiated with something more concrete (such as “int” or “real”) but we only wanted to establish typability and so we can stop here.

  22. Well-typed - terms Calculating simple types: • Further refinement is required if we extend the term to • Finally, if we spell out the types in the term we get: Theorem Every well-typed -term has a normal form.

  23. The -calculus as a model of computation Turing-complete A computational system that can compute every Turing-computable function is called Turing complete  We call a calculus Turing-Complete if it allows one to define all computable function from N to N . The -calculus is Turing-complete ! Church encoding: Terms that are usually considered primitive in other notations (such as integers, Boolean) are mapped to higher-order functionsunder Church encoding. Church numerals- a representation of the natural numbers using lambda notation

  24. The -calculus as a model of computation … Computation with Church numerals: For example: Addition – uses the identity But programs would be : • Pretty slow • Pretty large • Pretty hard to understand.

  25. From Theory to Programming Language • Although the lambda-calculus is powerful enough to express any program, this doesn't mean that you'd actually want to do so. After all, the Turing Machine offers an equally powerful computational basis. Which lead us to Functional Programming… • Functional programming has its roots in lambda calculus– lambda calculus provides a theoretical framework for describing functions and their evaluation. Although it is a mathematical abstraction rather than a programming language, it forms the basis of almost all functional programming languages today. • Many functional programming languages can be viewed as elaborations on the lambda calculus.

  26. From Theory to Programming Language Functional Programming: “ functional programming is a programming paradigm, a style of building the structure and elements of computer programs, that treats computation as the evaluation of mathematical functions and avoids state and mutable data. “ • Functional programming emphasizes functions that produce results that depend only on their inputs and not on the program state - i.e. pure mathematical functions. • In functional code, the output value of a function depends only on the arguments that are input to the function. • So calling a function  twice with the same value for an argument x will produce the same result  both times. --Wikipedia

  27. Similarity to Functional Programming Pascal: …<statements >… end; ML: Scheme:

More Related