objectives the y combinator
play

Objectives The Y Combinator Understand how to allow functions to - PowerPoint PPT Presentation

f n = f (n+1) Objectives To Infjnity and Beyond Further Reading Objectives To Infjnity and Beyond Further Reading Objectives The Y Combinator Understand how to allow functions to call themselves even when they dont have names.


  1. f n = f (n+1) Objectives To Infjnity and Beyond Further Reading Objectives To Infjnity and Beyond Further Reading Objectives The Y Combinator ◮ Understand how to allow functions to call themselves — even when they don’t have names. Dr. Mattox Beckman ◮ Understand how to develop a general combinator Y to implement University of Illinois at Urbana-Champaign recursion. Department of Computer Science Objectives To Infjnity and Beyond Further Reading Objectives To Infjnity and Beyond Further Reading Recursion Step 1 The outline of the function would look like Suppose we want to implement λ n . ( f ( inc n )) But, how does f get to know itself?

  2. Objectives To Infjnity and Beyond Further Reading Objectives To Infjnity and Beyond Further Reading Step 2 Expanding a Church Numeral ◮ Consider how this is similar to the operation of Church numerals. Maybe we can tell f by having it take it’s own name as a parameter. (( f 5 f ) x ) λ f .λ n . ( f ( inc n )) → ( f (( f 4 f ) x )) So then we pass a copy of f to itself... → ( f ( f (( f 3 f ) x ))) → ( f ( f ( f (( f 2 f ) x )))) ( λ f .λ n . ( f ( inc n ))) ( λ f .λ n . ( f ( inc n ))) → ( f ( f ( f ( f ( f x ))))) But now f must pass itself into itself... so we have So... ( λ f .λ n . (( f f ) ( inc n ))) ( λ f .λ n . (( f f ) ( inc n ))) (( f n f ) x ) → ( f (( f n − 1 f ) x )) What would it look like to have an f ∞ ? Objectives To Infjnity and Beyond Further Reading Objectives To Infjnity and Beyond Further Reading The Y Combinator Coding the Y Combinator Consider this pattern: ( Y f ) → f ( Y f ) So... (( f ∞ f ) x ) → ( f (( f ∞ f ) x )) Y = λ f . ( λ y . f ( y y )) λ y . f ( y y )) ◮ What can you tell about f ? About f ∞ ? The function f must take ( Y f ) as an argument. ◮ Defjnition: combinator = higher order function that produces its result only though function application. ( Y F ) = ( λ f . ( λ y . f ( y y )) λ y . f ( y y )) F ◮ The problem with the above function is that there’s no way out. = ( λ y . F ( y y )) λ y . F ( y y ) How can we stop the function when we are done? = F (( λ y . F ( y y )) λ y . F ( y y )) = F ( Y F )

  3. if n < 1 then 1 else n * (fact (n-1)) Objectives To Infjnity and Beyond Further Reading Objectives To Infjnity and Beyond Further Reading Example Further Reading 1 fact n = 2 3 ◮ You can use λ -calculus to represent itself using these techniques. In λ -calculus: You already have everything you need to do it. You can see the details in Torben Æ. Mogensen’s paper Effjcient λ f .λ n . Self-Interpretations in lambda Calculus, in the Journal of Functional if n < 1 then 1 Programming v2 n3. else n ∗ ( f ( n − 1)) Then we have: λ n . Y fact → if n < 1 then 1 else n ∗ (( Y fact ) ( n − 1))

Recommend


More recommend