Lambda calculus

Post on 17-Feb-2017

216 views 7 download

Transcript of Lambda calculus

Lambda Calculus

Mahsa SeifikarWinter 94

2

Contents

• History• Definition• Syntax λ calculus • Function in Lambda Calculus• Reduction• Encoding data type in calculus• Order of Evaluation• Functional Programming

3

History

It was introduced in 1930s by Alonzo Church as a way of formalizing the concept of effective computability.

λ calculus is a universal model of computation equivalent to a Turing machine.

It also can be called the smallest universal programming language of the world.

4

History

All function programming language can be viewed as syncretic variation of λ calculus.

Functional languages such as Lisp, Scheme, FP, ML, Miranda, and Haskell are an attempt to realize Church's lambda calculus in practical form as a programming language.

5

Definition

Lambda expressions are composed of:

Variables The abstraction symbols lambda 'λ' and dot '.' parentheses ‘(‘ , ‘)'

Note : variable is an identifier which can be any of the letters a, b, c.

6

Syntax λ calculus

The set of λ-terms (notation Λ) is built up from an infantine set of variables

rule 2 are known as application and rule 3 are known as abstraction .

7

Syntax λ calculus

BN-form

λ |

8

Syntax λ calculus

Application associate from the left.– x y z : (x y) z

Abstraction associate from the right.– lx. x ly. x y z : l x. (x (ly. ((x y) z)))

Outermost parentheses are dropped.

9

Function in Lambda Calculus

Function in the calculus focus on the rule for going from an argument to

result. Ignore the issues of naming the function and its

domain and range. Mathematical function : In the calculus : Argument is called x, the output of the function is x.

10

Function in Lambda Calculus The application of the identity function to a :

The argument for the identify itself:

Function take an argument, ignore it and return the identity function:

Function take an argument, return a function that ignore its own argument and return the argument original function:

11

Free Variables

The set of free variables M denoted by

Note : Variables that fall within the scope of an abstraction are said to be bound. All other variables are called free.

12

Substitution

Substituting N for the free occurrences of x in MDenoted by

13

Lambda Reduction

allows bound variable names to be changed.

if x and y is variable and M is a expression:

Example :

14

Lambda Reduction

applying functions to their arguments. If x is variable and M and N are expression:

Example:

15

Lambda Reduction

Reversing β-reductionproduce the β-abstraction.

β-abstraction and β-reduction taken together give β-conversion

16

Lambda Reduction

which captures a notion of extensionality. If x is variables and M is a expression , and x has

no free occurrence in M :

Example:

17

Lambda Reduction

The general reduction relation n is the union of , and ɳ :

When we use emphasize that ()

18

Encoding data type in calculus

Booleans Boolean is a function that give two choices selects

one of them.

19

Encoding data type in calculus

Logic Operator with two false term and true term, we can

define some logic operator:

Symbol f and t correspond with to “false” and “true” .

20

Encoding data type in calculus

Logic Operator For example :

21

Encoding data type in calculus

Natural Number A natural number n is encoded by a function of

two arguments, f and x, where the function applies f to x, n times.

22

Encoding data type in calculus

Natural Number the function add1 represent a number n and

produce a number n+1.

The function applies first argument to second argument n+1.

23

Encoding data type in calculus

Natural Number To add to numbers n and m, we apply add1 to n m

time

Multiplication can be defined as

Iszero also

24

Encoding data type in calculus Pair

A linked list can be defined as either NIL for the empty list, or the pair of an element and a smaller list.

The predicate NULL tests for the value NIL.

25

Encoding data type in calculus

Recursive Function Is the definition of a function using the function itself.

Can be defined in the calculus using a function which calls a function y and generates itself.

26

Encoding data type in calculus

Recursive Function Y f is the fixed point of f expand to :

27

Encoding data type in calculus

Recursive Function For example, given n=4 to factorial function:

28

Encoding data type in calculus

Recursive Function

29

Normal Form

An expression is a normal form if it can’t be reduced by or .

M has normal form and N is a normal form.If , , and both M and N are normal form, then .If an expression has a normal form, then may

be an infinite reduction sequence for the expression that never reaches normal form.

30

Church-Rosser Theorem

Reduction in any way can eventually produce the same result.

If , then there exist an E such that and .If , and is a normal form, then there is a

normal-order reduction of to .Normal-order reduction will always produce a

normal form, if one exists.

31

Order of Evaluation In programming languages, we do not reduce the

bodies of functions (under a l).Functions are considered values.Call by Name

No reduction are performed inside abstraction . Also call left-most, lazy evaluation.

Call by Value Reduced only when its right hand side has reduced to a value

(variable or lambda abstraction). Also call Eager evaluation.

32

Order of EvaluationCall by Name– Difficult to implement.– Order of side effects not predictable.

Call by Value– Easy to implement efficiently.– Might not terminate even if CBN might terminate.– Example: (lx. l z.z) ((ly. yy) (lu. uu))

Outside the functional programming language community, only CBV is used.

33

Functional Programming

• The lambda calculus is a prototypical functional programming language.– Higher order function– No side-effect

• In practice, many functional programming language are not “pure”, they permit.– Supposed to avoid them

34

Functional Programming

Two main camps– Haskell - Pure, lazy function language , no side-

effects– ml (SML-OCaml) call- by- value with side-effects

Old still around : lisp scheme– Disadvantage : no static typing

35

Functional Programming

• Functional ideas move to other languages.• Garbage collection was designed for lisp ,now no mast

new languages us GC.• Generics in C++/Java com from ML polymorphism ,or

Haskell type classes• Higher-order function and closures (used in Ruby, exist

in C# proposed to be in java soon) are everywhere in function languages.

• Many object-oriented abstraction principles come from ML’s module system.

36

References

Matthias Felleisen, “Programming Language AND Lambda Calculus”.

Raul Rojas, “A Tutorial Introduction to the Lambda Calculus”.