Introduction to Introduction to Lambda Calculus York University CSE - - PowerPoint PPT Presentation

introduction to introduction to lambda calculus
SMART_READER_LITE
LIVE PREVIEW

Introduction to Introduction to Lambda Calculus York University CSE - - PowerPoint PPT Presentation

Introduction to Introduction to Lambda Calculus York University CSE 3401 Vida Movahedi 1 York University CSE 3401 V. Movahedi 10_LambdaCalculus Overview Overview Functions F i calculus : notation for functions


slide-1
SLIDE 1

Introduction to Introduction to Lambda Calculus

York University CSE 3401 Vida Movahedi

York University‐ CSE 3401‐ V. Movahedi

1

10_LambdaCalculus

slide-2
SLIDE 2

Overview Overview

F i

  • Functions
  • λ‐calculus : λ‐notation for functions
  • Free and bound variables
  • α‐ equivalence and β‐reduction
  • Connection to LISP

[ref : Chap 1 & 2 of Selinger’s lecture notes on Lambda Calculus: [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

slide-3
SLIDE 3

Extensional view of Functions Extensional view of Functions

  • “Functions as graphs”:

– each function f has a fixed domain X and co‐domain Y f ti f X→Y i t f i f X Y h th t f – 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. (x , y) ∈ f.

  • Equality of functions:

T f i l if i h – Two functions are equal if given the same input they yield the same output

) ( ) ( , , : g , x g x f X x g f Y X f = ∈ ∀ ⇔ = →

York University‐ CSE 3401‐ V. Movahedi

3

10_LambdaCalculus

slide-4
SLIDE 4

Intensional view of Functions Intensional view of Functions

  • “Functions as rules”:

– Functions defined as rules, e.g. f(x)= x2 N t l t if d i d d i – Not always necessary to specify domain and co‐domain

  • Equality of functions:

q y

– 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 p g , – 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

slide-5
SLIDE 5

3 observations about functions 3 observations about functions

f( ) i h id i f i f(x)=x is the identity function g(x)=x is also the identity function Functions do not need to be explicitly named Functions do not need to be explicitly named Can be expressed as

x x a

they are the same

y x y x − a ) , (

v u v u − a ) , ( The specific choice for argument names is irrelevant

y x y x − a ) , (

Functions can be re‐written in a way to accept only one

( )

y x y x y y − a a ) , (

y p y single input (called currying)

York University‐ CSE 3401‐ V. Movahedi

5

10_LambdaCalculus

slide-6
SLIDE 6

Lambda Calculus 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:
  • Examples of λ‐notation:

– The identity function f(x)=x is denoted as λx.x λx x is the same as λy y (called α equivalence) – λx.x is the same as λy.y (called α‐equivalence) – Function f defined as is written as λx.x2 f(5) i (λ

2)(5)

d l t t 25 ( ll d β d ti )

2

: x x f a

– f(5) is (λx.x2)(5) and evaluates to 25 (called β‐reduction)

York University‐ CSE 3401‐ V. Movahedi

6

10_LambdaCalculus

slide-7
SLIDE 7

More examples More examples

  • Evaluate

( ) ( ) )

3 ( ) 2 )( . ( .

3 2

y x y x + λ λ

( ) ( ) 17

2 3 ) 3 ( 2 .

3 2 3 2

= + = + = x x λ

  • Evaluate

( )

) 3 )( 2 ( ) (

3 2

λ λ

( )

) 3 )( 2 ( ) . .(

3 2

y x y x + λ λ

( ) ( )

31 3 2 ) 3 ( 2 .

3 2 3 2

= + = + = y y λ

York University‐ CSE 3401‐ V. Movahedi

7

10_LambdaCalculus

slide-8
SLIDE 8

Higher order functions Higher order functions

  • Higher‐order functions are functions whose input

and/or output are functions

  • They can also be expressed in λ‐notation
  • Example:

– f(x)= x3 and g(x)=(f o f)(x)= f(2)(x)=f(f(x))=f(x3)=(x3)3=x9 – f(x) is written as λx.x3 – g(x)=f(f(x)) is written as λx.f(f(x)) – The function defined as is denoted as λf.λx.f(f(x))

f f f

  • a

York University‐ CSE 3401‐ V. Movahedi

8

10_LambdaCalculus

slide-9
SLIDE 9

Lambda terms Lambda terms

λ l l i

  • λ‐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 λ 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) Note in λ notation we write (fx) instead of f(x) Example: Write the steps in λ‐term calculation of

( ) ( ) ( ) ( )

) )( ( yz xz z y x λ λ λ

( ) ( ) ( ) ( )

) )( ( . . . yz xz z y x λ λ λ

( ) ( ) ( ),

) )( ( . , ) )( ( ), ( ), ( , , , yz xz z yz xz yz xz z y x λ

York University‐ CSE 3401‐ V. Movahedi

9

( ) ( ) ( ) ( ) ( ) ( ) ( )

) )( ( . . . , ) )( ( . . yz xz z y x yz xz z y λ λ λ λ λ

10_LambdaCalculus

slide-10
SLIDE 10

Conventions 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:

( ) ( ) ( ) ( )

) )( ( yz xz z y x λ λ λ ) (yz xz xyz λ ⇒

York University‐ CSE 3401‐ V. Movahedi

10

( ) ( ) ( ) ( )

) )( ( . . . yz xz z y x λ λ λ ) ( . yz xz xyz λ ⇒

10_LambdaCalculus

slide-11
SLIDE 11

Free and bound variables Free and bound variables

h λ

  • In the term λx.M

– λ is said to bind x in M – λx is called a binder λx is called a binder – x is a bound variable

  • In the term λx xy
  • 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 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} – Set of free variables FV={y,z}

York University‐ CSE 3401‐ V. Movahedi

11

10_LambdaCalculus

slide-12
SLIDE 12

Set of free variables 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

( ) ( )

) )( ( ) ( zu xy zv z v z xy λ λ λ is :

( ) ( )

) )( ( ) ( . . . zu xy zv z v z xy λ λ λ ( ) ( ) ( ) ( )

} , { ) )( ( ) ( . . ) ( y x zu xy zv z v z FV M FV − = λ λ

( ) ( ) ( ) ( )

} , { } , { } , { } , { } , { } , { ) ( ) ( ) ( . . y x u z y x v z v z y x zu FV xy FV zv z v z FV − − = − = U U U U λ λ

York University‐ CSE 3401‐ V. Movahedi

12

} , { u z =

10_LambdaCalculus

slide-13
SLIDE 13

α equivalence α‐ equivalence

λ i h λ (b h id i f i )

  • λ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 α‐equivalence of lambda terms and is

denoted as

}) \ { .( . y x M y M x λ λ

α

=

  • 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 – Note x is a bound variable in this definition

York University‐ CSE 3401‐ V. Movahedi

13

10_LambdaCalculus

slide-14
SLIDE 14

Substitution 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 – (λx.yx)[y := (λz.xz)] = λx’.(λz.xz)x’

York University‐ CSE 3401‐ V. Movahedi

14

10_LambdaCalculus

slide-15
SLIDE 15

Substitution (cont ) Substitution (cont.)

  • Definition:

] : [ N N x x ≡ = if ]) : [ ])( : [ ( ] : )[ ( ] : [ ] [ N x P N x M N x MP y x y N x y = = ≡ = ≠ ≡ = and if ) ( ]) : [ .( ] : )[ . ( . ] : )[ . ( N FV y y x N x M y N x M y M x N x M x ∉ ≠ = ≡ = ≡ = λ λ λ λ fresh and if ' ), ( , ]) : }[ ' \ { '.( ] : )[ . ( y N FV y y x N x y y M y N x M y ∈ ≠ = ≡ = λ λ

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

York University‐ CSE 3401‐ V. Movahedi

15

avoid capture of free variable y in N

10_LambdaCalculus

slide-16
SLIDE 16

β reduction β‐reduction

β d ti th f l ti l bd t b

  • β‐reduction: the process of evaluating a lambda term by

giving value to arguments

For example: (λ

2)( )

2 – (λx.x2)(5) β 25 – (λx.y)(z) β y

  • Definition
  • Definition

– β‐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 – β‐reduction is applied recursively until there is no more – β‐reduction is applied recursively until there is no more redexes left to reduce – A lambda term without any β‐redexes is said to be in β‐ normal form normal form

York University‐ CSE 3401‐ V. Movahedi

16

10_LambdaCalculus

slide-17
SLIDE 17

β reduction – more examples β‐reduction – more examples

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

β y[x:=(λz.zz)] = y β y[x:=(λz.zz)] = y

( y)( )

  • (λw.w)(λw.w)

β

y[ ( )] y β w[w:=(λw.w)] = (λw.w)

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

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

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

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

York University‐ CSE 3401‐ V. Movahedi

17

10_LambdaCalculus

slide-18
SLIDE 18

Why Lambda Calculus?! Why Lambda Calculus?!

P l i i 1930’ Popular question in 1930’s: “What does it mean for a function f to be computable?”

I i i bili A il d h d ll – 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 g p y computed by the Turing machine. 2. Gödel: A function is computable if and only if it is general recursive 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

18

10_LambdaCalculus

slide-19
SLIDE 19

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 p p p g g g g Turing complete – ‘Pure LISP’ is equivalent to Lambda Calculus q – ‘LISP’ is Lambda calculus, plus some additional features such as data types, input/output, etc such as data types, input/output, etc

York University‐ CSE 3401‐ V. Movahedi

19

10_LambdaCalculus