Lambda Calculus Adrian Groza Department of Computer Science Technical University of Cluj-Napoca
Outline 1 λ-calculus 2 Operational Semantics Syntax Conversions Normal Form 3 Lambda Calculus as a Functional Language Aritmethic Operations Boolean Expressions Tuples 4 Food for Tought
What is Computable? Computation is usually modelled as a mapping from inputs to outputs, carried out by a formal machine or program, which processes its input in a sequence of steps. An effectively computable function is one that can be computed in a finite amount of time using finite resources.
Computation Models Turing Machine (Turing, 1936) modifies the content of the memory and of an input tape whose movement is controllable corresponds to imperative languages where the state of the computation process is represented and explicitly processed Lambda Calculus (Church, 1932) works on unary functions relies on function application the substitution in the function body of the formal parameter with the effective parameter processing without state (1912-1954) (1903-1995) Church-Turing thesis If an algorithm (a procedure that terminates) exists then there is an equivalent Turing Machine or applicable λ-function for that algorithm.
Lambda Calculus 1 universal: any computable function can be expressed and evaluated using this formalism 2 to directly express higher-order functions 3 to study evaluation orders, termination, uniqueness of answers 4 to serve as a kernel language for functional languages 5 the smallest universal programming language of the world
Outline 1 λ-calculus 2 Operational Semantics Syntax Conversions Normal Form 3 Lambda Calculus as a Functional Language Aritmethic Operations Boolean Expressions Tuples 4 Food for Tought
Syntax Syntax E = x λx. E E 1 E 2 x - variable λx.e - functional abstraction (x - bound variable, E - body) E 1 E 2 - function application (E 1 function, E 2 argument) <expression> := <name> <function> <application> <function> := λ <name>.<expression> <application> := <expression><expression> Examples: x λ-expression reduced to a variable λx.x is a function taking an argument x, and returning x (identity fun) λx. λy.x function which selects the first value from a pair of values f x is a function f applied to an argument x (same as f(x)) (λx.x) y applying the function λx.x on the argument y
Syntax Examples (λx. x x) (λy. y) = (λy. y) (λx.(x + 5)) 3 = 3+5 = 8 ((λx.(λy.x + y)) 3) 5 = (λy.3 + y)) 5 = 3+5=8 (λf.λx.f(f x))(λy.y+1) = λx.(λy.y+1)((λy.y+1) x) = λx.(λy.y+1)(x+1) = λx.x+1+1
Syntax Parsing Lambda Expressions 1 Lambda extends as far as possible to the right λf.x y λ f.(x y) λx.e1 E2 E3 λ x.(e1 E2 E3) λx.x λz.x z x λx.(x λz.(x z x)) 2 Application is left-associative x y z (x y) z 3 Parantheses are needed for (λx.e1 E2) E3 where E3 is an argument to the function λx.e1 E2, not part of the body of the function 4 Multiple lambdas may be suppressed λ f g. x λ f. λ g. x
Syntax 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
Syntax Free and Bound Variables In function λx.t, the variable x is bound; t is the body of the function A variable which is not bound is free. Examples: (λx. x y) (λx. x)(λy. y x ) (λz. (λx. (y x))) Observations: x is bound, y is free x is bound in the first expression, x is independen x is bound, y is free Name of bound var does not matter: λx.(3+x) = λy.(3+y) Name of free var does matter: λx.(x+y) λx.(x+z) Same identifier can occur free and bound in the same expression: (λx.xy)(λy.y)
Conversions α-conversion Conversion = rule for transforming a λ-expression into a another one with the same meaning. α-conversion: renaming bound variables (λx.t) α (λy.t[y/x]) Example (λx.ax) α (λy.ay) (λx.(x(λy.(yx)))) α (λz.(z(λy.(yz))))[z/x] (λx.x(λx.ax))b α (λy.y(λx.ax))b[y/x]
Conversions β-conversion β-conversion: applying abstractions to arguments ((λx.t)u) β (t[u/x]) Example ((λx.((fx)x)(ga) β ((f (ga))(ga)) ((λz.(za)(λx.x) β (λx.x)a β a ((λx.λy.y)a)b) β ((λy.y))b (λy.y)b β b β-conversion represents the computational engine of the lambda calculus.
Conversions η-conversion η-conversion: eliminates redundant lambda-expressions (λx.fx)y η fy Every time when x is not free in f, we can write: (λ x. f x ) = f Example (λ b. y b) y (λ z. (λ x. x y) z) λ x. x y
Conversions Applying Conversions Conversions 1 (λx.t) α (λy.t[y/x]) 2 ((λx.t)u) β (t[u/x]) 3 (λx.fx)y η fy (λ x y. x y) (λ x. x y) (λ a b. a b) NB: left assoc α (λ x z. x z) (λ x. x y) (λ a b. a b) α-conversion [z/x] β (λ z. (λ x. x y) z) (λ a b. a b) β-reduction [(λ x. x y)/x] η (λ x. x y) (λ a b. a b) η-reduction (λ z. f z )= f β (λ a b. a b) y β-reduction [(λ a b. a b)/x] β (λ b. y b) β-reduction [(y/a] η y η-reduction(λ z. f z)= f
Conversions Nested expressions Syntax simplification: (λx 1. (λx 2....(λx n. t)...)) (λx 1 x 2...x n. t) (λx y.y x) y b (λx y.y x) y b (λx. (λy. y x)) y b α (λx. (λz. z x)) y b β (λz. z y) b β b y (λx. (λy. y x)) y b β (λy. y y) b β b b Don t accidentally bind a free variable!
Normal Form Normal Form A λ-expression is said to be in the normal form if no further reduction steps could be applied to it. Example (λ x. a x)((λ y. b y) c) α (λ z. a z)((λ y. b y) c) β a ((λ y. b y) c) a (b c) Not all expressions have a normal form (λ x. x x) (λ x. x x) β (x x)[(λ x. x x)/x] (λ x. x x) (λ x. x x)... the program never ends.
Normal Form Church-Rosser Thesis Thesis Two sequences of reductions applied to a λ-expressions will always result in equivalent normal forms. Thus the normal form does not depend on the order the reductions are performed. (λ x. a x)((λ y. b y) c) α (λ z. a z)((λ y. b y) c) β a ((λ y. b y) c) a (b c) (λ x. a x)((λ y. b y) c) β (λx. a x)(b c) β a (b c)
Normal Form Call by name and call by value in λ-calculus Call by name (λ y. a) ((λ x. x x) (λ x. x x)) β a argument is not evaluated and it is substitued in the body of the function. Call by value (λ y. a) ((λ x. x x) (λ x. x x)) β (λ y. a) ((λ x. x x) (λ x. x x)) This type of substitution always finds the normal form, provided that such a form actually exists. (\x -> \y -> x) 1 (5/0) = 1
Outline 1 λ-calculus 2 Operational Semantics Syntax Conversions Normal Form 3 Lambda Calculus as a Functional Language Aritmethic Operations Boolean Expressions Tuples 4 Food for Tought
Aritmethic Operations λ-calculus Expressivity In the lambda calculus we can only define new functions. How can we program with only functions? Idea Encode the behavior of values and not their structure. Introducing variables let x = e1 in e2 (λx.e1) e2
Aritmethic Operations Aritmethic Can we represent numbers in λ-calculus? Numbers will be defined as... functions: 0 := λsz.z 1 := λsz.s(z) 2 := λsz.s(s(z))... Definition A natural number is a function that given an operation s and a starting value z, applies s a number of times to z.
Aritmethic Operations Successor Function Our first interesting function... S λwyx.y(wyx) S 0 λw y x. y(wyx) (λsz. z) β λy x. y ((λsz. z) y x) β λy x. y ((λz. z) x) β λy x. y (x) α λs x. s(x) 1
Aritmethic Operations Summing numbers Our second interesting function... PLUS λ mnfx. nf (mfx) PLUS 1 2 (λ mnfx.nf (mfx))1 2 ββ λ fx.2f (1fx) λ fx.2f ((λ fx.f (x)) fx) η λ fx.2f (f (x)) λ fx.(λ fx.f (f (x)))f (f (x)) λ fx.f (f (f (x))) α 3
Boolean Expressions Boolean Expressions What is true? True is something that used as the first parameter of if makes the result of if to be its second parameter if T M N M if λ p. λthen.λelse. p then else if λp. (λ c. (λ a. pca))) λpca. pca A boolean is a function that given two choices selects one of them T λx. (λy. x) λx y. x F λx. (λy. y) λx y. y if T M N λp c a. p c a (λx y. x) M N β (λc a. (λx y. x) c a) M N β (λa. (λx y. x) a) M N β (λx y. x) M N β (λy. M) N β M
Boolean Expressions Logical Operators And, Or, Not and λx (λy if x y F) λxy. x y F or λx (λy if x T y) λxy. x T y not λx. x F T and T F (λx y. x y F) T F β (λy. T y F) F β T F F β (λx y. x) F F β (λy. F) F β F 1 (λx.t) α (λy.t[y/x]) 2 ((λx.t)u) β (t[u/x]) 3 (λx.fx)y η fy
Boolean Expressions Logical Operators And, Or, Not and λx (λy if x y F)... λxy. x y F or λx (λy if x T y) λxy. x T y not λx. x F T 1 (λx.t) α (λy.t[y/x]) 2 ((λx.t)u) β (t[u/x]) 3 (λx.fx)y η fy and T F (λx y. x y F) T F β (λy. T y F) F β T F F β (λx y. x) F F β (λy. F) F β F not T (λ b. b F T ) (λ x y. x ) β (λ x y. x) F T β F
Boolean Expressions Testing if a number is zero Another interesting function... Z λx.xf F Z 0 λx.xf F 0 0F F (λs z. z) F F F T
Boolean Expressions Testing if a number is zero Z 0 λx.xf F 0 0F F (λs z. z) F F F T Z 1 λx.xf F 1 1F F (λsz.s(z))f F F( )F (λxy.y)( )F F Z λx.xf F
Tuples Defining pairs PAIR λxyf.fxy FIRST λ p. p T SECOND λ p. p F First (Pair (a b)) (λp.p T) (PAIR a b) (PAIR a b) T (λxyf.fxy a b) T (λf. f a b) T T a b a
Tuples Recursion A self-regenerating function... Y λy.(λx.y(xx))(λx.y(xx)) YR λy.(λx.y(xx))(λx.y(xx))r (λx.r(xx))(λx.r(xx)) R((λx.R(xx))(λx.R(xx))) R(YR) When Y is applied on an arbitrary function R, R will call itself on (YR). Subsequent equivalent forms appear when R gets expanded according to its definition. It is R s responsibility to break the recursive loop at some moment by providing a condition for stopping.
Tuples Recursion - Example fact = λ f. λ n.if n = 0 then 1 else n * (f (n-1)) (Y fact) 1 = (fact (Y fact)) 1 if 1 = 0 then 1 else 1 * ((Y fact) 0) 1 * ((Y fact) 0) 1 * (fact (Y fact) 0) 1 * (if 0 = 0 then 1 else 0 * ((Y fact) (-1)) 1 * 1 1
Outline 1 λ-calculus 2 Operational Semantics Syntax Conversions Normal Form 3 Lambda Calculus as a Functional Language Aritmethic Operations Boolean Expressions Tuples 4 Food for Tought
Readings 1 Mandatory reading: Chapter 8: pages 107-119 2 Optional reading: A Tutorial Introduction to the Lambda Calculus, Raul Rojas, FU Berlin, Germany, WS-97/98