Lambda Calculus (PDCS 2) combinators, higher-order programming, recursion combinator, numbers, Church numerals Carlos Varela Rennselaer Polytechnic Institute September 8, 2017 C. Varela 1
Lambda Calculus Syntax and Semantics The syntax of a λ -calculus expression is as follows: e :: = v variable | λ v . e functional abstraction | ( e e ) function application The semantics of a λ -calculus expression is called beta-reduction: ( λ x . E M ) ⇒ E{M/x} where we alpha-rename the lambda abstraction E if necessary to avoid capturing free variables in M . C. Varela 2
α -renaming Alpha renaming is used to prevent capturing free occurrences of variables when beta-reducing a lambda calculus expression. In the following, we rename x to z , (or any other fresh variable): ( λ x.(y x) x) α → ( λ z.(y z) x) Only bound variables can be renamed. No free variables can be captured (become bound) in the process. For example, we cannot alpha-rename x to y . C. Varela 3
β -reduction β ( λ x . E M ) E{M/x} → Beta-reduction may require alpha renaming to prevent capturing free variable occurrences. For example: ( λ x. λ y.(x y) (y w)) α → ( λ x. λ z.(x z) (y w)) β λ z.((y w) z) → Where the free y remains free. C. Varela 4
Booleans and Branching ( if ) in λ Calculus |true| : λ x. λ y.x (True) |false| : λ x. λ y.y (False) |if| : λ b. λ t. λ e.((b t) e) (If ) Recall semantics rule: (((if true) a) b) ( λ x . E M ) ⇒ E{M/x} ((( λ b. λ t. λ e.((b t) e) λ x. λ y.x) a) b) ⇒ (( λ t. λ e.(( λ x. λ y.x t) e) a) b) ⇒ ( λ e.(( λ x. λ y.x a) e) b) ⇒ (( λ x. λ y.x a) b) ⇒ ( λ y.a b) ⇒ a C. Varela 5
η -conversion η λ x .( E x ) E → if x is not free in E . For example: ( λ x. λ y.(x y) (y w)) α → ( λ x. λ z.(x z) (y w)) β λ z.((y w) z) → η → (y w) C. Varela 6
Combinators A lambda calculus expression with no free variables is called a combinator . For example: I: λ x.x (Identity) App: λ f. λ x.(f x) (Application) C: λ f. λ g. λ x.(f (g x)) (Composition) L: ( λ x.(x x) λ x.(x x)) (Loop) Cur: λ f. λ x. λ y.((f x) y) (Currying) Seq: λ x. λ y.( λ z.y x) (Sequencing--normal order) ASeq: λ x. λ y.(y x) (Sequencing--applicative order) where y denotes a thunk, i.e. , a lambda abstraction wrapping the second expression to evaluate. The meaning of a combinator is always the same independently of its context. C. Varela 7
Combinators in Functional Programming Languages Functional programming languages have a syntactic form for lambda abstractions. For example the identity combinator: λ x.x can be written in Oz as follows: fun {$ X} X end \x -> x in Haskell as follows: (lambda(x) x) and in Scheme as follows: C. Varela 8
Currying Combinator in Oz The currying combinator can be written in Oz as follows: fun {$ F} fun {$ X} fun {$ Y} {F X Y} end end end It takes a function of two arguments, F, and returns its curried version, e.g., {{{Curry Plus} 2} 3} ⇒ 5 C. Varela 9
Recursion Combinator ( Y or rec ) Suppose we want to express a factorial function in the λ calculus. 1 n=0 f(n) = n! = n*(n-1)! n>0 We may try to write it as: f : λ n.(if (= n 0) 1 (* n (f (- n 1)))) But f is a free variable that should represent our factorial function. C. Varela 10
Recursion Combinator ( Y or rec ) We may try to pass f as an argument ( g ) as follows: f : λ g. λ n.(if (= n 0) 1 (* n (g (- n 1)))) The type of f is: f: (Z → Z) → (Z → Z) So, what argument g can we pass to f to get the factorial function? C. Varela 11
Recursion Combinator ( Y or rec ) f: (Z → Z) → (Z → Z) (f f) is not well-typed . (f I) corresponds to: 1 n=0 f(n) = n*(n-1) n>0 We need to solve the fixpoint equation: (f X) = X C. Varela 12
Recursion Combinator ( Y or rec ) (f X) = X The X that solves this equation is the following: X: ( λ x.( λ g. λ n.(if (= n 0) 1 (* n (g (- n 1)))) λ y.((x x) y)) λ x.( λ g. λ n.(if (= n 0) 1 (* n (g (- n 1)))) λ y.((x x) y))) C. Varela 13
Recursion Combinator ( Y or rec ) X can be defined as (Y f) , where Y is the recursion combinator . Y: λ f.( λ x.(f λ y.((x x) y)) Applicative Applicative λ x.(f λ y.((x x) y))) Order Order Y: λ f.( λ x.(f (x x)) Nor orma mal Order λ x.(f (x x))) You get from the normal order to the applicative order recursion combinator by η -expansion ( η -conversion from right to left). C. Varela 14
Natural Numbers in Lambda Calculus |0| : λ x.x (Zero) |1| : λ x. λ x.x (One) … |n+1| : λ x.|n| (N+1) s : λ n. λ x.n (Successor ) Recall semantics rule: (s 0) ( λ x . E M ) ⇒ E{M/x} ( λ n. λ x.n λ x.x) ⇒ λ x. λ x.x C. Varela 15
Church Numerals |0| : λ f. λ x.x (Zero) |1| : λ f. λ x.(f x) (One) … |n| : λ f. λ x.(f … (f x)…) (N applications of f to x) s : λ n. λ f. λ x.(f ((n f) x)) (Successor ) Recall semantics rule: (s 0) ( λ x . E M ) ⇒ E{M/x} ( λ n. λ f. λ x.(f ((n f) x)) λ f. λ x.x) ⇒ λ f. λ x.(f (( λ f. λ x.x f) x)) ⇒ λ f. λ x.(f ( λ x.x x)) ⇒ λ f. λ x.(f x) C. Varela 16
Church Numerals: isZero? Recall semantics rule: ( λ x . E M ) ⇒ E{M/x} isZero? : λ n.((n λ x.false) true) (Is n=0? ) (isZero? 0) ( λ n.((n λ x.false) true) λ f. λ x.x) ⇒ (( λ f. λ x.x λ x.false) true) ⇒ ( λ x.x true) ⇒ true (isZero? 1) ( λ n.((n λ x.false) true) λ f. λ x.(f x)) ⇒ (( λ f. λ x.(f x) λ x.false) true) ⇒ ( λ x.( λ x.false x) true) ⇒ ( λ x.false true) ⇒ false C. Varela 17
Exercises 9. PDCS Exercise 2.11.10 (page 31). Test your representation of numbers in Haskell. 10. PDCS Exercise 2.11.11 (page 31). 11. Prove that your addition operation is correct using induction. C. Varela 18
Recommend
More recommend