Equationpedia

Y Combinator

The Y combinator lets a function call itself without using its own name. Give it a rule that expects a callback for the recursive call, and it supplies a callback that follows that same rule. The trick is to apply a function to a copy of itself.

Fixed point of the rule
One unfolding
Y f=βf(Y f)\htmlData{region=fixed-point}{\htmlData{term=y}{Y}\,\htmlData{term=rule}{f}} \htmlData{term=beta-equivalence}{=_{\beta}} \htmlData{region=one-unfolding}{\htmlData{term=rule}{f}(\htmlData{term=y}{Y}\,\htmlData{term=rule}{f})}

Notes

Read the notation

In untyped lambda calculus, λx. M\lambda x.\,M means “take an input xx and return the body MM.” Writing two terms next to each other means application, so x xx\,x applies xx to itself. Applying a lambda function substitutes the argument for every occurrence bound by that parameter, renaming bound variables as needed to preserve their meaning. This is called beta reduction.

Follow one unfolding

Fix a rule ff and call either parenthesized block Af=λx. f(x x)A_f=\lambda x.\,f(x\,x). Open Function position and Argument position in the explorer. The two blocks have the same form. Predict what goes into both places marked xx when the left block receives the right block as its argument.

After substituting ff into the outer function, the expression is Af AfA_f\,A_f. Applying the left block replaces both uses of xx in its body with the right block, so the result is f(Af Af)f(A_f\,A_f). The whole application has reappeared as the argument to ff. Call that application gg. One unfolding yields f(g)f(g), so gg is a fixed point of ff under beta equivalence. The Fixed-point identity form shows this as Y f=βf(Y f)Y\,f =_{\beta} f(Y\,f).

Calculate factorial

Let ff take a callback rr and return a function of a nonnegative integer nn.

f(r)(n)={1n=0,n⋅r(n−1)n>0.f(r)(n)= \begin{cases} 1 & n=0,\\ n\cdot r(n-1) & n>0. \end{cases}

Set g=Y fg=Y\,f. The fixed-point identity says gg follows this rule with r=gr=g. At n=3n=3, the recursive branch gives g(3)=3g(2)=3⋅2g(1)=3⋅2⋅1g(0)=6g(3)=3g(2)=3\cdot2g(1)=3\cdot2\cdot1g(0)=6. At zero, the base case returns 11 without calling the callback.

When evaluation order matters

With the classic YY, evaluating function arguments eagerly unfolds the recursive argument before ff can use it. A direct port to JavaScript therefore keeps unfolding. The related ZZ combinator puts the self-application inside another function so it waits until the recursive call receives an argument.

Sources

Y Combinator (Fixed-point identity) merch