Introductory Course on Logic and Automata Theory Introduction to the lambda calculus Polyvios.Pratikakis@imag.fr Based on slides by Jeff Foster, UMD Introduction to lambda calculus – p. 1/33
History Formal mathematical system Simplest programming language Intended for studying functions, recursion Invented in 1936 by Alonzo Church (1903-1995) Church’s Thesis: “ Every effectively calculable function (effectively decidable predicate) is general recursive ” i.e. can be computed by lambda calculus Church’s Theorem: First order logic is undecidable Introduction to lambda calculus – p. 2/33
Syntax Simple syntax: e x :: = Variables λ x . e | Functions e e | Function applications Pure lambda calculus: only functions Arguments are functions Returned value is function A function on functions is higher-order Introduction to lambda calculus – p. 3/33
Semantics Evaluating function application: ( λ x . e 1 ) e 2 Replace every x in e 1 with e 2 Evaluate the resulting term Return the result of the evaluation Formally: “ β -reduction” ( λ x . e 1 ) e 2 → β e 1 [ e 2 / x ] A term that can be β -reduced is a redex We omit β when obvious Introduction to lambda calculus – p. 4/33
Convenient assumptions Syntactic sugar for declarations let x = e 1 in e 2 Scope of λ extends as far to the right as possible λ x . λ y . x y is λ x . ( λ y . ( x y )) Function application is left-associative x y z means ( x y ) z Introduction to lambda calculus – p. 5/33
Scoping and parameter passing β -reduction is not yet well-defined: ( λ x . e 1 ) e 2 → e 1 [ e 2 / x ] There might be many x defined in e 1 Example Consider the program let x = a in let y = λ z . x in let x = b in y x Which x is bound to a , and which to b ? Introduction to lambda calculus – p. 6/33
Lexical scoping Variable refers to closest definition We can rename variables to avoid confusion: let x = a in let y = λ z . x in let w = b in y w Introduction to lambda calculus – p. 7/33
Free/bound variables The set of free variables of a term is FV ( x ) x = FV ( λ x . e ) FV ( e ) \{ x } = FV ( e 1 e 2 ) FV ( e 1 ) ∪ FV ( e 2 ) = A term e is closed if FV ( e ) = / 0 A variable that is not free is bound Introduction to lambda calculus – p. 8/33
α -conversion Terms are equivalent up to renaming of bound variables λ x . e = λ y . e [ y / x ] if y / ∈ FV ( e ) Renaming of bound variables is called α -conversion Used to avoid having duplicate variables, capturing during substitution Introduction to lambda calculus – p. 9/33
Substitution Formal definition x [ e / x ] = e y [ e / x ] = y when x � = y ( e 1 e 2 )[ e / x ] = ( e 1 [ e / x ] e 2 [ e / x ]) ( λ y . e 1 )[ e / x ] = λ y . ( e 1 [ e / x ]) when y � = x and y / ∈ FV ( e ) Example ( λ x . y x ) x = α ( λ w . y w ) x → β y x We omit writing α -conversion Introduction to lambda calculus – p. 10/33
Functions with many arguments We can’t yet write functions with many arguments For example, two arguments: λ ( x , y ) . e Solution: take the arguments, one at a time λ x . λ y . e A function that takes x and returns another function that takes y and returns e ( λ x . λ y . e ) a b → ( λ y . e [ a / x ]) b → e [ a / x ][ b / y ] This is called Currying Can represent any number of arguments Introduction to lambda calculus – p. 11/33
Representing booleans true = λ x . λ y . x false = λ x . λ y . y if a then b else c = a b c For example: if true then b else c → ( λ x . λ y . x ) b c → ( λ y . b ) c → b if false then b else c → ( λ x . λ y . y ) b c → ( λ y . y ) c → c Introduction to lambda calculus – p. 12/33
Combinators Any closed term is also called a combinator true and false are combinators Other popular combinators: I = λ x . x K = λ x . λ y . x S = λ x . λ y . λ z . x z ( y z ) We can define calculi in terms of combinators The SKI-calculus SKI-calculus is also Turing-complete Introduction to lambda calculus – p. 13/33
Encoding pairs ( a , b ) = λ x . if x then a else b fst = λ p . p true snd = λ p . p false Then fst ( a , b ) → ... → a snd ( a , b ) → ... → b Introduction to lambda calculus – p. 14/33
Natural numbers (Church) 0 = λ x . λ y . y 1 = λ x . λ y . xy 2 = λ x . λ y . x ( x y ) i.e. n = λ x . λ y . � apply x n times to y � succ = λ z . λ x . λ y . x ( z x y ) iszero = λ z . z ( λ y . false ) true Introduction to lambda calculus – p. 15/33
Natural numbers (Scott) 0 = λ x . λ y . x 1 = λ x . λ y . y 0 2 = λ x . λ y . y 1 i.e. n = λ x . λ y . y ( n − 1 ) succ = λ z . λ x . λ y . yz pred = λ z . z 0 ( λ x . x ) iszero = λ z . z true ( λ x . false ) Introduction to lambda calculus – p. 16/33
Nondeterministic semantics e → e ′ ( λ x . e 1 ) e 2 → e 1 [ e 2 / x ] ( λ x . e ) → ( λ x . e ′ ) e 1 → e ′ e 2 → e ′ 1 2 e 1 e 2 → e ′ 1 e 2 e 1 e 2 → e 1 e ′ 2 Question: why is this semantics non-deterministic? Introduction to lambda calculus – p. 17/33
Example We can apply reduction anywhere in the term ( λ x . ( λ y . y ) x (( λ z . w ) x ) → λ x . ( x (( λ z . w ) x ) → λ x . x w ( λ x . ( λ y . y ) x (( λ z . w ) x ) → λ x . ( λ y . y ) x w → λ x . x w Does the order of evaluation matter? Introduction to lambda calculus – p. 18/33
The Church-Rosser Theorem Lemma (The Diamond Property): If a → b and a → c , then there exists d such that b → ∗ d and c → ∗ d Church-Rosser theorem: If a → ∗ b and a → ∗ c , then there exists d such that b → ∗ d and c → ∗ d Proof by diamond property Church-Rosser also called confluence Introduction to lambda calculus – p. 19/33
Normal form A term is in normal form if it cannot be reduced Examples: λ x . x , λ x . λ y . z By the Church-Rosser theorem, every term reduces to at most one normal form Only for pure lambda calculus with non-deterministic evaluation Notice that for function application, the argument need not be in normal form Introduction to lambda calculus – p. 20/33
β -equivalence Let = β be the reflexive, symmetric, transitive closure of → E.g., ( λ x . x ) y → y ← ( λ z . λ w . z ) y y so all three are β -equivalent If a = β b , then there exists c such that a → ∗ c and b → ∗ c Follows from Church-Rosser theorem In particular, if a = β b and both are normal forms, then they are equal Introduction to lambda calculus – p. 21/33
Not every term has a normal form Consider ∆ = λ x . x x Then ∆ ∆ → ∆ ∆ → ··· In general, self application leads to loops . . . which is good if we want recursion Introduction to lambda calculus – p. 22/33
Fixpoint combinator Also called a paradoxical combinator Y = λ f . ( λ x . f ( x x )) ( λ x . f ( x x )) There are many versions of the Y combinator Then, Y F = β F ( Y F ) Y F = ( λ f . ( λ x . f ( x x )) ( λ x . f ( x x ))) F → ( λ x . F ( x x )) ( λ x . F ( x x )) → F (( λ x . F ( x x )) ( λ x . F ( x x ))) ← F ( Y F ) Introduction to lambda calculus – p. 23/33
Example fact ( n ) = if ( n = 0 ) then 1 else n ∗ fact ( n − 1 ) Let G = λ f . λ n . if ( n = 0 ) then 1 else n ∗ f ( n − 1 ) Y G 1 = β G ( Y G ) 1 = β ( λ f . λ n . if ( n = 0 ) then 1 else n ∗ f ( n − 1 )) ( Y G ) 1 = β if ( 1 = 0 ) then 1 else 1 ∗ (( Y G ) 0 ) = β if ( 1 = 0 ) then 1 else 1 ∗ ( G ( Y G ) 0 ) = β if ( 1 = 0 ) then 1 else 1 ∗ ( λ f . λ n . if ( n = 0 ) then 1 else n ∗ f ( n − 1 ) ( Y G ) 0 ) = β if ( 1 = 0 ) then 1 else 1 ∗ ( if ( 0 = 0 ) then 1 else 0 ∗ (( Y G ) 0 )) = β 1 ∗ 1 = 1 Introduction to lambda calculus – p. 24/33
In other words The Y combinator “unrolls” or “unfolds” its argument an infinite number of times Y G = G ( Y G ) = G ( G ( Y G )) = G ( G ( G ( Y G ))) = ... G needs to have a “base case” to ensure termination But, only works because we follow call-by-name Different combinator(s) for call-by-value Z = λ f . ( λ x . f ( λ y . x x y )) ( λ x . f ( λ y . x x y )) Why is this a fixed-point combinator? How does its difference from Y work for call-by-value? Introduction to lambda calculus – p. 25/33
Why encodings It’s fun! Shows that the language is expressive In practice, we add constructs as languages primitives More efficient Much easier to analyze the program, avoid mistakes Our encodings of 0 and true are the same, we may want to avoid mixing them, for clarity Introduction to lambda calculus – p. 26/33
Lazy and eager evaluation Our non-deterministic reduction rule is fine for theory, but awkward to implement Two deterministic strategies: Lazy : Given ( λ x . e 1 ) e 2 , do not evaluate e 2 if e 1 does not need x anywhere Also called left-most, call-by-name, call-by-need, applicative, normal-order evaluation (with slightly different meanings) Eager : Given ( λ x . e 1 ) e 2 , always evaluate e 2 to a normal form, before applying the function Also called call-by-value Introduction to lambda calculus – p. 27/33
Lazy operational semantics ( λ x . e 1 ) → l ( λ x . e 1 ) e 1 → l λ x . e e [ e 2 / x ] → l e ′ e 1 e 2 → l e ′ The rules are deterministic, big-step The right-hand side is reduced “all the way” The rules do not reduce under λ The rules are normalizing: If a is closed and there is a normal form b such that a → ∗ b , then a → l d for some d Introduction to lambda calculus – p. 28/33
Recommend
More recommend