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 • 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] 2 York University ‐ CSE 3401 ‐ V. Movahedi 10_LambdaCalculus
Extensional view of Functions 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 f ti f X → Y i t f i f X Y h th t f each x ∈ X , there exists exactly one y ∈ Y such that (x , y) ∈ f . (x , y) ∈ f . • Equality of functions: – Two functions are equal if given the T f i l if i h same input they yield the same output → = ⇔ ∀ ∈ = , g : , , ( ) ( ) f X Y f g x X f x g x 3 York University ‐ CSE 3401 ‐ V. Movahedi 10_LambdaCalculus
Intensional view of Functions Intensional view of Functions • “Functions as rules”: – Functions defined as rules, e.g. f(x)= x 2 – Not always necessary to specify domain and co ‐ domain N t l t if d i d d i • 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) 4 York University ‐ CSE 3401 ‐ V. Movahedi 10_LambdaCalculus
3 observations about functions 3 observations about functions f(x)=x is the identity function f( ) i h id i f i 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 a x − ( , ) a x y x y they are the same − ( , ) a u v u v � The specific choice for argument names is irrelevant − ( ( , , ) ) a x y y x y y ( ) − a a x y x y � Functions can be re ‐ written in a way to accept only one y p y single input (called currying ) 5 York University ‐ CSE 3401 ‐ V. Movahedi 10_LambdaCalculus
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.x 2 2 : a f x x – f(5) is ( λ x.x 2 )(5) and evaluates to 25 (called β ‐ reduction) f(5) i ( λ ll d β 2 )(5) d l t t 25 ( d ti ) 6 York University ‐ CSE 3401 ‐ V. Movahedi 10_LambdaCalculus
More examples More examples • Evaluate ( ( ) ) ) λ λ + 2 3 . ( . )( 2 ) ( 3 x y x y ( ) ( ) 17 = λ + = + = 2 3 2 3 . 2 ( 3 ) 3 2 x x • Evaluate ( ( ) ) λ λ λ λ + 2 2 3 3 .( ( . ) ) ( ( 2 2 )( )( 3 3 ) ) x y x y ( ) ( ) = λ + = + = 2 3 2 3 . 2 ( 3 ) 2 3 31 y y 7 York University ‐ CSE 3401 ‐ V. Movahedi 10_LambdaCalculus
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)= x 3 and g(x)=(f o f)(x)= f (2) (x)=f(f(x))=f(x 3 )=(x 3 ) 3 =x 9 – f(x) is written as λ x.x 3 – g(x)=f(f(x)) is written as λ x.f(f(x)) a o – The function defined as f f f is denoted as λ f. λ x.f(f(x)) 8 York University ‐ CSE 3401 ‐ V. Movahedi 10_LambdaCalculus
Lambda terms Lambda terms λ λ ‐ term calculation: l l i • A variable is a λ ‐ term (for example x, y, ...) 1. If M is a λ ‐ term and x is a variable, then ( λ x.M) is a λ ‐ If M is a λ term and x is a variable then ( λ x M) is a λ 2. 2 term (called a lambda abstraction ) If M and N are λ ‐ terms , then (MN) is a λ ‐ term (called an 3. , ( ) ( 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 ( ( ( ( ( ( ( ( ) ) ) ) ) ) ) ) λ λ λ λ λ λ . . . ( ( )( )( ) ) x x y y z z xz xz yz yz ( ) ( ( ) ) , λ , , , ( ), ( ), ( )( ) , . ( )( ) x y z xz yz xz yz z xz yz ( ( ( ) ) ) ( ( ( ( ) ) ) ) λ λ λ λ λ . . ( )( ) , . . . ( )( ) y z xz yz x y z xz yz 9 York University ‐ CSE 3401 ‐ V. Movahedi 10_LambdaCalculus
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) Nested abstractions can be contracted, e.g. λ xy.M means λ 4. λ x. λ y.M Ex: Write the following with as few parentheses as possible: ( ( ( ( ( ( ( ( ) ) ) ) ) ) ) ) ⇒ ⇒ λ λ λ λ λ λ λ λ . ( ( yz ) ) . . . ( ( )( )( ) ) xyz xyz xz xz yz x x y y z z xz xz yz yz 10 York University ‐ CSE 3401 ‐ V. Movahedi 10_LambdaCalculus
Free and bound variables Free and bound variables • In the term λ x.M λ h – λ 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} 11 York University ‐ CSE 3401 ‐ V. Movahedi 10_LambdaCalculus
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 ( ( ( ( ) ) ) ) λ λ λ λ λ λ . . . ( ( ) ) ( ( )( )( ) ) xy xy z z v v z z zv zv xy xy zu zu ( ( ) ) = λ λ − ( ) . . ( ) ( )( ) { , } is : FV M FV z v z zv xy zu x y ( ( ( ( ) ) ) ) = λ λ − . . ( ) ( ) ( ) { , } U U FV z v z zv FV xy FV zu x y ( ( ) ) = − − { , } { , } { , } { , } { , } U U z v z v x y z u x y = { u , } z 12 York University ‐ CSE 3401 ‐ V. Movahedi 10_LambdaCalculus
α ‐ equivalence α equivalence • λ x.x is the same as λ y.y (both are identity function) λ λ i h (b h id i f i ) • λ x.x 2 is the same as λ z.z 2 • Renaming bound variables does not change the abstraction • This is called α ‐ equivalence of lambda terms and is denoted as λ = λ . .( { \ }) x M y M x y α • 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 13 York University ‐ CSE 3401 ‐ V. Movahedi 10_LambdaCalculus
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 yx) [y : ( λ z xz) ] ≠ λ x ( λ z xz)x λ x. ( λ z.xz)x – ( λ x.yx) [y := ( λ z.xz) ] = λ x’. ( λ z.xz)x’ 14 York University ‐ CSE 3401 ‐ V. Movahedi 10_LambdaCalculus
Substitution (cont.) Substitution (cont ) • Definition: = ≡ [ [ : ] ] x x N N = ≡ ≠ [ : ] if y x N y x y = ≡ = = ( )[ : ] ( [ : ])( [ : ]) MP x N M x N P x N λ = ≡ λ ( . )[ : ] . x M x N x M λ = ≡ λ = ≠ ∉ ( . )[ : ] .( [ : ]) ( ) if and y M x N y M x N x y y FV N λ = ≡ λ = ≠ ∈ ( . )[ : ] '.( { \ ' }[ : ]) , ( ), ' if and fresh y M x N y M y y x N x y y FV N y Capture case! Bound variable y is renamed to y’ to avoid capture of free variable y in N avoid capture of free variable y in N 15 York University ‐ CSE 3401 ‐ V. Movahedi 10_LambdaCalculus
Recommend
More recommend