Lambda calculus

36
Lambda Calculus Mahsa Seifikar Winter 94

Transcript of Lambda calculus

Page 1: Lambda calculus

Lambda Calculus

Mahsa SeifikarWinter 94

Page 2: Lambda calculus

2

Contents

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

Page 3: Lambda calculus

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.

Page 4: Lambda calculus

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.

Page 5: Lambda calculus

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.

Page 6: Lambda calculus

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 .

Page 7: Lambda calculus

7

Syntax λ calculus

BN-form

λ |

Page 8: Lambda calculus

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.

Page 9: Lambda calculus

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.

Page 10: Lambda calculus

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:

Page 11: Lambda calculus

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.

Page 12: Lambda calculus

12

Substitution

Substituting N for the free occurrences of x in MDenoted by

Page 13: Lambda calculus

13

Lambda Reduction

allows bound variable names to be changed.

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

Example :

Page 14: Lambda calculus

14

Lambda Reduction

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

Example:

Page 15: Lambda calculus

15

Lambda Reduction

Reversing β-reductionproduce the β-abstraction.

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

Page 16: Lambda calculus

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:

Page 17: Lambda calculus

17

Lambda Reduction

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

When we use emphasize that ()

Page 18: Lambda calculus

18

Encoding data type in calculus

Booleans Boolean is a function that give two choices selects

one of them.

Page 19: Lambda calculus

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” .

Page 20: Lambda calculus

20

Encoding data type in calculus

Logic Operator For example :

Page 21: Lambda calculus

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.

Page 22: Lambda calculus

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.

Page 23: Lambda calculus

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

Page 24: Lambda calculus

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.

Page 25: Lambda calculus

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.

Page 26: Lambda calculus

26

Encoding data type in calculus

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

Page 27: Lambda calculus

27

Encoding data type in calculus

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

Page 28: Lambda calculus

28

Encoding data type in calculus

Recursive Function

Page 29: Lambda calculus

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.

Page 30: Lambda calculus

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.

Page 31: Lambda calculus

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.

Page 32: Lambda calculus

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.

Page 33: Lambda calculus

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

Page 34: Lambda calculus

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

Page 35: Lambda calculus

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.

Page 36: Lambda calculus

36

References

Matthias Felleisen, “Programming Language AND Lambda Calculus”.

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