160 likes | 239 Views
GC16/3011 Functional Programming Lecture 6 Recursion and the Lambda Calculus. Contents. Introduction Recursive functions in Miranda Recursive functions in the lambda calculus Binding for function name Fixed point Fixed point operator self-application. Introduction. Recursive function f.
E N D
GC16/3011 Functional ProgrammingLecture 6Recursion and the Lambda Calculus
Contents • Introduction • Recursive functions in Miranda • Recursive functions in the lambda calculus • Binding for function name • Fixed point • Fixed point operator • self-application
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
Recursion and the lambda calculus • Consider: • In the lambda calculus this is: f x = 3, if (x = 0) = 11, otherwise l x.(if (x = 0) 3 11)
Recursion and the lambda calculus • However: • What’s wrong with this?: f x = 3, if (x = 0) = 1 + f (x - 1), otherwise l x.(if (x = 0) 3 (1 + (f (x - 1))))
Recursion and the lambda calculus • Whole program: • Whole program?: f x = 3, if (x = 0) = 1 + f (x - 1), otherwise main = f 7 l x.(if (x = 0) 3 (1 + (f (x - 1)))) 7
Recursion and the lambda calculus • 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
Recursion and the lambda calculus • Now the following lambda expression for “h” is fine, because “f” is bound: BUT “h” is not “f”, so we haven’t solved the problem yet! • However, notice that (h f) gives the same expression as f, so (h f) = f (this is an equality, not a definition) l f. (l x.(if (x = 0) 3 (1 + (f (x - 1)))))
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 x = x • id is called the “identity function” • Every value in the input domain of id is a fixed point of id • Example: 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 !!!!
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 = Y h
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 because f = Y h = (h (Y h)) 3 because Y g = g (Y g) = (l x.(if (x = 0) 3 (1 + ((Y h) (x - 1))))) 3 = 1 + ((Y h) 2) = 1 + ((h (Y h)) 2) loop!
Recursion and the lambda calculus • Now we have a lambda expression that defines “f” • But haven’t we really just shifted the problem - how do we define “Y” in the lambda calculus? Y (l f. (l x.(if (x = 0) 3 (1 + (f (x - 1))))))
Recursion and the lambda calculus The real magic: self-application lq.( (lx.(q (x x))) (lx.(q (x x))) )
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)
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)))))))
Summary • Recursive functions in Miranda • Recursive functions in the lambda calculus • Binding for function name • Fixed point • Fixed point operator • Self-application