esolang
Learn · Ideas9 languages show it

Fixed points

Something that a transformation leaves unchanged. Behind every quine, every recursive function without names, and every evolving program that stops changing.

●●● hard · where to see it

The idea

A fixed point of a function f is an x with f(x) = x. Squaring has fixed points 0 and 1. "Sort this list" has every sorted list as a fixed point.

Read the story

In computing, fixed points explain two things that look like magic.

  1. how a program can talk about itself (quines, self-reference);
  2. how a function can call itself when the language has no names (recursion in pure lambda calculus or combinators).

How it works

Stephen Kleene proved this in 1938.

Kleene's recursion theorem, in plain words

Stephen Kleene proved this in 1938. Take any computable way of transforming programs (add a line, swap two instructions, rename every variable, anything). Then there is a program P such that P and the transformed P compute the same thing.

No transformation can change every program's behaviour. Some program always survives it.

Two consequences.

  • Quines exist in every universal language. Let the transformation be "turn P into a program that ignores its input and prints P". Its fixed point is a program that behaves like "print P", which is to say a program that prints itself.
  • Programs may use their own source as if it were handed to them. This is why the no-peeking rule for quines can always be met; the data-half/code-half trick is the proof, made concrete.

Kleene's fixed point is about behaviour. P and its transform compute the same function, but their text may differ.

The Y combinator

In lambda calculus a function has no name, so how does factorial call itself? Haskell Curry's Y combinator finds the fixed point of a function.

Y = λf. (λx. f (x x)) (λx. f (x x))

Apply it to some g and unfold once.

Y g → (λx. g (x x)) (λx. g (x x))
    → g ((λx. g (x x)) (λx. g (x x)))
    = g (Y g)

So Y g is a fixed point of g; g applied to it gives it back. If g is "given a way to recurse, here is factorial", then Y g is factorial.

The core is self-application, x x, the same trick as S I I in combinators and :aSS in Underload's quine.

In a language that evaluates arguments first, Y loops forever before doing anything. The fix is to delay the self-application. That variant is called Z.

const Z = f => (x => f(v => x(x)(v)))(x => f(v => x(x)(v)));
const fact = Z(self => n => n === 0 ? 1 : n * self(n - 1));
console.log(fact(5));   // 120

No function here refers to itself by name. In Node it prints 120.

Seen in

  • Unlambda and Lazy K. All recursion goes through a fixed-point combinator built from S, K and I.
  • Binary Lambda Calculus. The same, in bits.
  • Underload. Loops are made by duplicating a program and running a copy of it, which is self-application.

Try this

  1. Find the fixed points of "reverse the string". Of "sort the letters".
  2. Using Z, write a recursive Fibonacci in JavaScript without naming the function inside itself.
  3. Hard: find a string function with no fixed point that still gives every string back when applied twice (a cycle of length 2). What must change and change back?

Further

Where to see it