1 / 16

GC16/3011 Functional Programming Lecture 6 Recursion and the Lambda Calculus

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.

ursala
Download Presentation

GC16/3011 Functional Programming Lecture 6 Recursion and the Lambda Calculus

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. GC16/3011 Functional ProgrammingLecture 6Recursion and the Lambda Calculus

  2. Contents • Introduction • Recursive functions in Miranda • Recursive functions in the lambda calculus • Binding for function name • Fixed point • Fixed point operator • self-application

  3. 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. 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)

  5. 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))))

  6. 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

  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

  8. 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)))))

  9. 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 !!!!

  10. 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

  11. 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!

  12. 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))))))

  13. Recursion and the lambda calculus The real magic: self-application lq.( (lx.(q (x x))) (lx.(q (x x))) )

  14. 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)

  15. 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)))))))

  16. Summary • Recursive functions in Miranda • Recursive functions in the lambda calculus • Binding for function name • Fixed point • Fixed point operator • Self-application

More Related