Introduction to Lambda Calculus - York University · Lambda Calculus •These 3 observations are...

18
Introduction to Lambda Calculus York University Department of Computer Science and Engineering York University- CSE 3401- V. Movahedi 1 10_LambdaCalculus

Transcript of Introduction to Lambda Calculus - York University · Lambda Calculus •These 3 observations are...

Page 1: Introduction to Lambda Calculus - York University · Lambda Calculus •These 3 observations are motivations for a new notation for functions: Lambda notation • -calculus: theory

Introduction to Lambda Calculus

York University

Department of Computer Science and Engineering

York University- CSE 3401- V. Movahedi 1 10_LambdaCalculus

Page 2: Introduction to Lambda Calculus - York University · Lambda Calculus •These 3 observations are motivations for a new notation for functions: Lambda notation • -calculus: theory

Overview

• Functions

• -calculus : -notation for functions

• Free and bound variables

• a- equivalence and b-reduction

• Connection to LISP

[ref.: Chap. 1 & 2 of Selinger’s lecture notes on Lambda Calculus:

http://www.mathstat.dal.ca/~selinger/papers/lambdanotes.pdf ] [also Wikipedia on Lambda calculus]

[I am using George Tourlakis’ notations for renaming and substitution]

York University- CSE 3401- V. Movahedi 2 10_LambdaCalculus

Page 3: Introduction to Lambda Calculus - York University · Lambda Calculus •These 3 observations are motivations for a new notation for functions: Lambda notation • -calculus: theory

Extensional view of Functions

• “Functions as graphs”: – each function f has a fixed domain X and co-domain Y – a function f : X→Y is a set of pairs f X × Y such that for

each x X, there exists exactly one y Y such that (x , y) f.

• Equality of functions: – Two functions are equal if given the same input they yield the same output

York University- CSE 3401- V. Movahedi 3

)()(,,:g, xgxfXxgfYXf

10_LambdaCalculus

Page 4: Introduction to Lambda Calculus - York University · Lambda Calculus •These 3 observations are motivations for a new notation for functions: Lambda notation • -calculus: theory

Intensional view of Functions

• “Functions as rules”: – Functions defined as rules, e.g. f(x)= x2

– Not always necessary to specify domain and co-domain

• Equality of functions: – Two functions are equal if they are defined by (essentially)

the same formula

• Comparing the two views – Graph model is more general, does not need a formula – Rule model is more interesting for computer scientists

(How can it be calculated? What is the time/memory complexity? etc)

York University- CSE 3401- V. Movahedi 4 10_LambdaCalculus

Page 5: Introduction to Lambda Calculus - York University · Lambda Calculus •These 3 observations are motivations for a new notation for functions: Lambda notation • -calculus: theory

3 observations about functions

f(x)=x is the identity function g(x)=x is also the identity function

Functions do not need to be explicitly named

Can be expressed as they are the same

The specific choice for argument names is irrelevant

Functions can be re-written in a way to accept only one single input (called currying)

York University- CSE 3401- V. Movahedi 5

xx

yxyx ),(

vuvu ),(

yxyx

yxyx

),(

10_LambdaCalculus

Page 6: Introduction to Lambda Calculus - York University · Lambda Calculus •These 3 observations are motivations for a new notation for functions: Lambda notation • -calculus: theory

Lambda Calculus

• These 3 observations are motivations for a new notation for functions: Lambda notation

• -calculus: theory of functions as formulas

• Easier manipulation of functions using expressions

• Examples of -notation:

– The identity function f(x)=x is denoted as x.x

– x.x is the same as y.y (called a-equivalence)

– Function f defined as is written as x.x2

– f(5) is (x.x2)(5) and evaluates to 25 (called b-reduction)

York University- CSE 3401- V. Movahedi 6

2: xxf

10_LambdaCalculus

Page 7: Introduction to Lambda Calculus - York University · Lambda Calculus •These 3 observations are motivations for a new notation for functions: Lambda notation • -calculus: theory

More examples

• Evaluate

• Evaluate

York University- CSE 3401- V. Movahedi 7

)3()2)(.(.32

yxyx

1723)3(2.3232 xx

)3)(2()..(32

yxyx

3132)3(2.3232 yy

10_LambdaCalculus

Page 8: Introduction to Lambda Calculus - York University · Lambda Calculus •These 3 observations are motivations for a new notation for functions: Lambda notation • -calculus: theory

Lambda terms

• -term calculation: 1. A variable is a -term (for example x, y, ...) 2. If M is a -term and x is a variable, then (x.M) is a -

term (called a lambda abstraction) 3. If M and N are -terms, then (MN) is a -term (called an

application)

– Note in -notation we write (fx) instead of f(x)

Example: Write the steps in -term calculation of

York University- CSE 3401- V. Movahedi 8

))((... yzxzzyx

))((...,))((..

,))((.,))((),(),(,,,

yzxzzyxyzxzzy

yzxzzyzxzyzxzzyx

10_LambdaCalculus

Page 9: Introduction to Lambda Calculus - York University · Lambda Calculus •These 3 observations are motivations for a new notation for functions: Lambda notation • -calculus: theory

Conventions

• Conventions for removing parentheses:

1. Omit outermost parentheses, e.g. MN instead of (MN)

2. Applications are left-associative, omit parentheses when not necessary, e.g. MNP means (MN)P

3. Body of abstraction extends to right as far as possible, e.g. x.MN means x.(MN)

4. Nested abstractions can be contracted, e.g. xy.M means x.y.M

Ex: Write the following with as few parentheses as possible:

York University- CSE 3401- V. Movahedi 9

))((... yzxzzyx )(. yzxzxyz

10_LambdaCalculus

Page 10: Introduction to Lambda Calculus - York University · Lambda Calculus •These 3 observations are motivations for a new notation for functions: Lambda notation • -calculus: theory

Free and bound variables

• In the term x.M – is said to bind x in M – x is called a binder – x is a bound variable

• In the term x.xy – x is a bound variable – y is a free variable

• In the term (x.xy)(y.yz) – x is a bound variable – z is a free variable – y has a free and a bound occurrence – Set of free variables FV={y,z}

York University- CSE 3401- V. Movahedi 10 10_LambdaCalculus

Page 11: Introduction to Lambda Calculus - York University · Lambda Calculus •These 3 observations are motivations for a new notation for functions: Lambda notation • -calculus: theory

Set of free variables

• FV(M): the set of free variables of a term M

– FV(x) = {x},

– FV(x.M) = FV(M) - {x}

– FV(MN) = FV(M) FV(N),

• Set of free variables in term M defined as

is :

York University- CSE 3401- V. Movahedi 11

))(()(... zuxyzvzvzxy

},{

},{},{},{},{},{

},{)()()(..

},{))(()(..)(

uz

yxuzyxvzvz

yxzuFVxyFVzvzvzFV

yxzuxyzvzvzFVMFV

10_LambdaCalculus

Page 12: Introduction to Lambda Calculus - York University · Lambda Calculus •These 3 observations are motivations for a new notation for functions: Lambda notation • -calculus: theory

a- equivalence

• x.x is the same as y.y (both are identity function)

• x.x2 is the same as z.z2

• Renaming bound variables does not change the abstraction

• This is called a-equivalence of lambda terms and is denoted as

• Where M{x\y} denotes renaming every occurrence of x in M to y (assuming y does not already occur in M) – Note x is a bound variable in this definition

York University- CSE 3401- V. Movahedi 12

})\{.(. yxMyMx a

10_LambdaCalculus

Page 13: Introduction to Lambda Calculus - York University · Lambda Calculus •These 3 observations are motivations for a new notation for functions: Lambda notation • -calculus: theory

Substitution

• Substitution is defined for free variables, substituting a variable with a term. – (x.xy)[y := M] = x.xM – (x.xy)[y := (uv)] = x.x(uv)

• Substitution must be defined to avoid capture – (x.xy)[y := x] ≠ x.xx – (x.xy)[y := x] = (x’.x’y)[y := x] = x’.x’x

– (x.yx)[y := (z.xz)] ≠ x.(z.xz)x – (x.yx)[y := (z.xz)] = x’.(z.xz)x’

York University- CSE 3401- V. Movahedi 13 10_LambdaCalculus

Page 14: Introduction to Lambda Calculus - York University · Lambda Calculus •These 3 observations are motivations for a new notation for functions: Lambda notation • -calculus: theory

Substitution (cont.)

• Definition:

York University- CSE 3401- V. Movahedi 14

fresh and if

and if

if

'),(,]):}['\{'.(]:)[.(

)(]):[.(]:)[.(

.]:)[.(

]):[])(:[(]:)[(

]:[

]:[

yNFVyyxNxyyMyNxMy

NFVyyxNxMyNxMy

MxNxMx

NxPNxMNxMP

yxyNxy

NNxx

Capture case! Bound variable y is renamed to y’ to avoid capture of free variable y in N

10_LambdaCalculus

Page 15: Introduction to Lambda Calculus - York University · Lambda Calculus •These 3 observations are motivations for a new notation for functions: Lambda notation • -calculus: theory

b-reduction

• b-reduction: the process of evaluating a lambda term by giving value to arguments For example: – (x.x2)(5) b 25 – (x.y)(z) b y

• Definition – b-redex: A term of the form (x.M)N (a lamda abstraction

applied to another term) – It reduces to M[x:=N] – The result is called a reduct – b-reduction is applied recursively until there is no more

redexes left to reduce – A lambda term without any b-redexes is said to be in b-

normal form

York University- CSE 3401- V. Movahedi 15 10_LambdaCalculus

Page 16: Introduction to Lambda Calculus - York University · Lambda Calculus •These 3 observations are motivations for a new notation for functions: Lambda notation • -calculus: theory

b-reduction – more examples

• (x.y)(z.zz)

• (x.y)(w.w)

• (w.w)(w.w)

• (x.y)((z.zz)(w.w))

b (x.y) (zz [z:=(w.w)] ) b (x.y) ((w.w) (w.w) )

b (x.y) (w.w) b (y [x:= (w.w)] ) b y

• Or (x.y)((z.zz)(w.w))

b y [x:= ((z.zz)(w.w)) ] b y

York University- CSE 3401- V. Movahedi 16 10_LambdaCalculus

b y[x:=(z.zz)] = y

b y[x:=(w.w)] = y

b w[w:=(w.w)] = (w.w)

Page 17: Introduction to Lambda Calculus - York University · Lambda Calculus •These 3 observations are motivations for a new notation for functions: Lambda notation • -calculus: theory

Why Lambda Calculus?!

Popular question in 1930’s:

“What does it mean for a function f to be computable?” – Intuitive computability: A pencil-and-paper method to allow a

trained person to calculate f(n) for any given n?

1. Turing: A function is computable if and only if it can be computed by the Turing machine.

2. Gödel: A function is computable if and only if it is general recursive.

3. Church: A function is computable if it can be written as a lambda term.

• It has been proven that all three models are equivalent.

• Are they equivalent to ‘intuitive computability’? Cannot be answered!

York University- CSE 3401- V. Movahedi 17 10_LambdaCalculus

Page 18: Introduction to Lambda Calculus - York University · Lambda Calculus •These 3 observations are motivations for a new notation for functions: Lambda notation • -calculus: theory

Lambda Calculus as a Programming Language

• Lambda calculus

– It can be used to encode programs AND data, such as Booleans and natural numbers

– It is the simplest possible programming language that is Turing complete

– ‘Pure LISP’ is equivalent to Lambda Calculus

– ‘LISP’ is Lambda calculus, plus some additional features such as data types, input/output, etc

York University- CSE 3401- V. Movahedi 18 10_LambdaCalculus