Last active Oct 30, 2018

The Y Combinator

The Y Combinator is a classic lambda calculus construct that many people find baffling. Here's my attempt to explain it as clearly as possible (no promises!). Familiarity with Haskell syntax is assumed.

The problem we're trying to solve is how to write an anonymous function (a "lambda") that is recursive. Normally, if you want to write a recursive function, it looks like this:

fac n =if n ==0then1else n * fac (n-1)

Note that fac appears in the function body. Therein lies the problem: if fac were a lambda, what name would you use to refer to it?
This may seem like a non-issue; why not just write the recursive function normally? Why use a lambda? Well, in the lambda calculus, there are no "normal functions," there are only lambdas. So the Y Combinator was invented as a means of writing recursive functions in pure lambda calculus.

Getting back to the problem: we need to rewrite fac so that it doesn't reference itself in its own function body. Here's the trick that forms the basis of the Y Combinator: fac can't refer to fac, but it can refer to a generic function supplied as an argument. In other words, we rewrite fac to take an extra argument, f, which is a function. Then, instead of fac recursively calling fac, it will just call f. It looks like this:

fac f n =if n ==0then1else n * f (n-1)

Now when we call fac, we'll call it as fac fac 5, so that when fac calls f, it'll really be calling itself. Recursion!

There's an error in the above code snippet though. Since f is fac, it needs to take another argument:

fac f n =if n ==0then1else n * f f (n-1)

Excellent. With all the references removed, we're now able to write fac as a lambda:

fac =\f n ->if n ==0then1else n * f f (n-1)

Note that fac itself takes no arguments. This is known as the "fixed-point" version of fac, also known as "points-free" style (or "pointless" by its detractors).

There's just one more problem. Recall that we are now calling fac fac instead of just fac. That won't do! Think of how we would map our fac lambda to a list:

This will actually work! (Okay, not quite; see Addendum.) But it goes without saying that this is rather ugly; any time we want to write a recursive lambda, we have to write it out twice! Plus, wherever we recurse, we have to call f f instead of just f. This is where the Y Combinator comes in: it abstracts away these annoyances.

Addendum

Haskell actually won't allow you to write f f, because it's strictly typed. To get around this, import Unsafe.Coerce and preface every occurance of f f with unsafeCoerce f f.

However, Haskell also has a much simpler way of writing a fixed-point combinator (of which the Y Combinator is only one example):

fix f = f (fix f)

This is possible because Haskell is "lazily evaluated," which means it only evaluates expressions when their value is needed. Otherwise, when it tried to evaluate the definition of fix, it would recurse forever and hang.

You can verify for yourself that fix works with the previous examples, no unsafeCoerce necessary. But why does it work? Let's expand it a bit: