GC16/3011 Functional Programming Lecture 6 Recursion and the Lambda Calculus 4/7/2015 1 Contents • • • Introduction Recursive functions in Miranda Recursive functions in the lambda calculus • • • • Binding for function name Fixed point Fixed point operator self-application 4/7/2015 2 Introduction Recursive function f Intermediate function h Fixed points and the fixpoint operator Y Definition of f in terms of Y How to define Y in the lambda calculus Final lambda-calculus definition of f 4/7/2015 3 Recursion and the lambda calculus • Consider: f x = 3, if (x = 0) = 11, otherwise • In the lambda calculus this is: l x.(if (x = 0) 3 11) 4/7/2015 4 Recursion and the lambda calculus • However: f x = 3, if (x = 0) = 1 + f (x - 1), otherwise • What’s wrong with this?: l x.(if (x = 0) 3 (1 + (f (x - 1)))) 4/7/2015 5 Recursion and the lambda calculus • Whole program: f x = 3, if (x = 0) = 1 + f (x - 1), otherwise main = f 7 • Whole program?: l x.(if (x = 0) 3 (1 + (f (x - 1)))) 7 4/7/2015 6 Recursion and the lambda calculus • 1 SO how can we represent a recursive function in the lambda calculus? First define a new function (e.g. call it “h”) as follows: h f x = 3, if (x = 0) = 1 + f (x-1), otherwise 4/7/2015 7 Recursion and the lambda calculus 2 Now the following lambda expression for “h” is fine, because “f” is bound: l f. (l x.(if (x = 0) 3 (1 + (f (x - 1))))) BUT “h” is not “f”, so we haven’t solved the problem yet! 3 However, notice that (h f) gives the same expression as f, so (h f) = f (this is an equality, not a definition) 4/7/2015 8 Recursion and the lambda calculus • A “fixed point” (or “fixpoint”) of a function f is a value x from the input domain of f such that (f x) returns the same value x • Example: • • • • id is called the “identity function” Every value in the input domain of id is a fixed point of id Example: • id x = x three x = if (x=3) 3 else (x+1) The input value 3 is the only fixed point of three Note that f is a fixpoint of h !!!! 4/7/2015 9 Recursion and the lambda calculus • There is a special function that we can use (called the “fixpoint operator”) which will return the fixed-point of any function. • The fixpoint operator is often denoted “Y” • It takes a function as its argument and returns the fixed point of that function • If the fixed-point is a function, it returns the “least” (i.e. most general) function that is a fixed-point • So now (Y h) gives the least fixpoint of h, which we know is f f=Yh 4/7/2015 10 Recursion and the lambda calculus • Y can easily be expressed operationally as: Y g = g (Y g) • || not valid Miranda! Now, for example (f is fixpoint of h): f 3 = (Y h) 3 = (h (Y h)) 3 = (l x.(if (x = 0) 3 (1 + ((Y h) (x - 1))))) 3 = 1 + ((Y h) 2) = 1 + ((h (Y h)) 2) 4/7/2015 because f = Y h because Y g = g (Y g) loop! 11 Recursion and the lambda calculus • Now we have a lambda expression that defines “f” Y (l f. (l x.(if (x = 0) 3 (1 + (f (x - 1)))))) • But haven’t we really just shifted the problem - how do we define “Y” in the lambda calculus? 4/7/2015 12 Recursion and the lambda calculus The real magic: self-application lq.( (lx.(q (x x))) (lx.(q (x x))) ) 4/7/2015 13 Recursion and the lambda calculus Example: Y h = lq.( (lx.(q (x x))) (lx.(q (x x))) ) h = (lx.(h (x x))) (lx.(h (x x))) = h ((lx.(h (x x))) (lx.(h (x x)))) = h (Y h) 4/7/2015 14 Recursion and the lambda calculus f = (Y h) = (Y (l f. (l x.(if (x = 0) 3 (1 + (f (x - 1))))))) = ((lq.((lx.(q (x x)))(lx.(q (x x))))) (l f. (l x.(if (x = 0) 3 (1 + (f (x - 1))))))) 4/7/2015 15 Summary • • Recursive functions in Miranda Recursive functions in the lambda calculus • • • • Binding for function name Fixed point Fixed point operator Self-application 4/7/2015 16