lambda calculus

29
Lambda calculus λ

Upload: chione

Post on 07-Jan-2016

109 views

Category:

Documents


3 download

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Lambda  calculus

Lambda calculus

λ

Page 2: 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

Page 3: Lambda  calculus

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…

Page 4: Lambda  calculus

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

Page 5: Lambda  calculus

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)

Page 6: Lambda  calculus

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:

Page 7: Lambda  calculus

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)

Page 8: Lambda  calculus

- 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!

Page 9: Lambda  calculus

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:

Page 10: Lambda  calculus

ConfluenceTheorem 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:1.

2.

𝛽1

𝛽1 𝛽2

𝛽2

𝛽1

𝛽1 𝛽2

𝛽2

Page 11: Lambda  calculus

Confluence3.

And if you believed me up until now the rest is very simple…

𝛽1

𝛽1 𝛽2

𝛽2

..

.

.

.

.

..

.

Page 12: Lambda  calculus

ConfluenceCorollary 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 whicha 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.

Page 13: Lambda  calculus

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)∪

Page 14: Lambda  calculus

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

( ( (

Page 15: Lambda  calculus

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

Page 16: Lambda  calculus

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.

Page 17: Lambda  calculus

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.

Page 18: Lambda  calculus

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: 𝐼=𝜆𝑛 𝑓 𝑥 . 𝑧𝑒𝑟𝑜?𝑛𝑥 ( 𝐼 (𝑝𝑟𝑒𝑑𝑛) 𝑓 ( 𝑓 𝑥 ) )

𝟑

I (3 , f ,0 )⟹ zero? 30 ( I (2 , λx . x+1 ,1 ) )I (3 , f ,0 )⟹ zero? 30 ( I (2 , λx . x+1 ,1 ) )I (2 , f ,1 )⟹ zero? 21 ( I (1 , λx . x+1 ,2))I (2 , f ,1 )⟹ zero? 21 ( I (1 , λx . x+1 ,2))I (1 , f ,2 )⟹ zero?12 ( I (0 , λx . x+1 ,3) )I (1 , f ,2 )⟹ zero?12 ( I (0 , λx . x+1 ,3) )

Page 19: Lambda  calculus

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

Page 20: Lambda  calculus

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

Page 21: Lambda  calculus

Well-typed - termsCalculating 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.

Page 22: Lambda  calculus

Well-typed - termsCalculating 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.

Page 23: Lambda  calculus
Page 24: Lambda  calculus

The -calculus as a model of computation

Turing-completeA 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 functions under Church encoding.

Church numerals- a representation of the natural numbers using lambda notation

Page 25: Lambda  calculus

The -calculus as a model of computation

Computation with Church numerals:

For example: Addition – uses the identity

But programs would be :Pretty slowPretty largePretty hard to understand.

Page 26: Lambda  calculus

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.

Page 27: Lambda  calculus

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

Page 28: Lambda  calculus

Similarity to Functional Programming

Pascal:…<statements >… end;ML:

Scheme:

Page 29: Lambda  calculus