The Y Combinator (Slight Return)

Aug 14, 2008 08:44


or:

How to Succeed at Recursion Without Really Recursing

Tiger got to hunt,
Bird got to fly;
Lisper got to sit and wonder, (Y (Y Y))?

Tiger got to sleep,
Bird got to land;
Lisper got to tell himself he understand.

- Kurt Vonnegut, modified by Darius Bacon Introduction

I recently wrote a blog post about the Y ( Read more... )

Leave a comment

Comments 34

rus_kz August 14 2008, 09:40:56 UTC
could you insert lj cut please? :)

Reply

mvanier August 14 2008, 09:55:51 UTC
What's LJ cut?

Reply

rus_kz August 14 2008, 10:03:10 UTC
http://www.livejournal.com/support/faqbrowse.bml?faqid=75

it allows to hide most of the message under "read more...". Otherwise friendlist is completely occupied by your long post...

Reply

mvanier August 14 2008, 10:17:25 UTC
Done. Thanks! I didn't know about that feature.

Reply


Yawn anonymous August 14 2008, 11:26:46 UTC
Why waste your time blogging when you should be coding?

Reply


The joy of parens anonymous August 14 2008, 11:39:27 UTC
I'm not at the repl at the moment, but shouldn't that be

(define Y
(lambda (f)
((lambda (x) (f (lambda (y) ((x x) y))))
(lambda (x) (f (lambda (y) ((x x) y)))))))
^ ^

Reply

Re: The joy of parens mvanier August 14 2008, 12:14:48 UTC
Right you are -- fixed. Thanks for catching this.

Reply


Excellent article. ext_117279 August 14 2008, 21:03:50 UTC
I stopped at the part "I realize that the preceding discussion and derivation is nontrivial, so don't be discouraged if you don't get it right away. Just sleep on it, play with it in your mind and with your trusty DrScheme interpreter, and you'll eventually get it."

I'm going to sleep on what I've read so far. I'll continue reading once I feel comfortable with what you presented up to that point. Thank you for writing about the Y combinator.

You explain abstract topics in a way that is easy to understand. When are you going to write a post explaining Monads?

Reply

Re: Excellent article. ext_117279 August 14 2008, 21:07:41 UTC
I reckon you have.

Reply

Re: Excellent article. mvanier August 14 2008, 22:21:19 UTC
Thanks for the compliment! I still have to finish the state monad article. My new policy is to not post until I have the article finished, but I _will_ finish that article, promise!

I also have a nearly-finished article on lambda calculus, and I'd love to write something on continuations and continuation-passing style.

Reply

Re: Excellent article. mvanier August 14 2008, 22:37:39 UTC
One thing you might try is to manually evaluate (factorial 3) using the definition that uses the strict version of Y. That will be very illuminating if you can tough it out.

Reply


Y doesn't have a straightforward static type? ryani August 28 2008, 02:13:35 UTC
y :: forall a. (a -> a) -> a
y f = let x = f x in x

Now, it's true that you cannot define "y" without the use of recursion in many statically typed languages; this is a property of System F and most other typed lambda-calculus... it follows directly from the fact that these systems are strongly normalizing. That is to say, there are no infinite loops in typed lambda-calculi like System F; any program will always complete successfully. On the other hand, System F + Y-combinator is Turing complete; just one single recursive let in the definition of "y" is all that is needed.

Reply

Re: Y doesn't have a straightforward static type? mvanier August 28 2008, 23:55:46 UTC

Good point -- I didn't know about this definition of Y. I tried it in Haskell, and it works fine. However, note that Haskell's let is what is known as letrec in Scheme (a let where all the bindings are in the scope of all the other ones), so there is explicit recursion built in to this definition even though it's not obvious. If this was a Scheme-equivalent let then this definition would be equivalent to:

(define Y
(lambda (f)
(let ((x (f x)))
x)))

which by the let/lambda equivalence rule would be the same as:

(define Y
(lambda (f)
((lambda (x) x)
(f x))))

which is meaningless since the x in (f x) is unbound. What it really is is this:

(define Y
(lambda (f)
(letrec ((x (f x)))
x)))

which works in lazy Scheme but not in strict (standard) Scheme.

I definitely need to learn more about System F.

Reply


Leave a comment

Up