ages

Rice’s Theorem via the Fixed Point Theorem (and why static analyzers have to approximate)

When working with compilers, linters, or static analyzers, a recurring frustration is that they reject programs we know are safe. In Rust, for example, the borrow checker rejects sound functions like Problem Case 3 from the Non-Lexical Lifetimes RFC, and attributes like #[must_use] only warn on obvious unused expressions while letting let _ = f(); pass silently.

This is not just an engineering limitation waiting for a smarter compiler pass. In computability theory, Rice’s Theorem shows that deciding any non-trivial property about what an arbitrary program does is fundamentally undecidable.

What Rice’s Theorem actually states

Informally, Rice’s Theorem is usually stated as:

All non-trivial semantic properties of programs are undecidable.

To make this precise, let ϕ0,ϕ1,ϕ2,…\phi_0, \phi_1, \phi_2, \dots be an enumeration of all computable partial functions, and let FcompF_{\text{comp}} be the set of all computable functions. Suppose A⊆FcompA \subseteq F_{\text{comp}} is some property of functions, and let II be the corresponding set of program indices:

I={i∣ϕi∈A}I = \{ i \mid \phi_i \in A \}

Two definitions matter here:

  1. Extensional (semantic). A set of indices II is extensional if whenever two indices compute the exact same function, either both are in II or neither is: ∀i,j.  (i∈I∧ϕi≃ϕj)  ⟹  j∈I\forall i, j.\; (i \in I \land \phi_i \simeq \phi_j) \implies j \in I In other words, II only cares about the input-output behavior of the function, not about how its source code is written.1
  2. Non-trivial. Neither every function has the property nor none of them do, meaning A≠∅A \neq \emptyset and A≠FcompA \neq F_{\text{comp}}.

Formally, Rice’s Theorem then says: for an extensional index set I={i∣ϕi∈A}I = \{ i \mid \phi_i \in A \}, II is recursive (decidable) if and only if A=∅A = \emptyset or A=FcompA = F_{\text{comp}}.

The Fixed Point Theorem

Many textbooks prove Rice’s Theorem by reducing the Halting Problem to II. There is an even cleaner proof that uses the Fixed Point Theorem (Kleene’s Recursion Theorem) directly.2

The Fixed Point Theorem states that for every total computable function tt, there exists an index pp such that:

ϕt(p)≃ϕp\phi_{t(p)} \simeq \phi_p

Note that this does not say t(p)=pt(p) = p as numbers. It says that the program at index t(p)t(p) computes the same function as the program at index pp. Even if tt tries to transform a program into a different one, there is always some program whose behavior tt leaves unchanged.

Proof of Rice’s Theorem

Using the Fixed Point Theorem, the proof takes five short steps:

  1. Since AA is non-trivial (A≠∅A \neq \emptyset and A≠FcompA \neq F_{\text{comp}}), there exist at least two indices ii and jj such that i∈Ii \in I (so ϕi∈A\phi_i \in A) and j∉Ij \notin I (so ϕj∉A\phi_j \notin A).
  2. Assume for contradiction that II is recursive (decidable).
  3. Under that assumption, there is an algorithm that decides membership in II, which lets us define the following function: f(x)={iif x∉I  (ϕx∉A)jif x∈I  (ϕx∈A)f(x) = \begin{cases} i & \text{if } x \notin I \;(\phi_x \notin A) \\ j & \text{if } x \in I \;(\phi_x \in A) \end{cases} Because II is assumed to be recursive, ff is both total and computable. In essence, ff checks whether program xx has property AA and deliberately outputs an index from the opposite side.
  4. By the Fixed Point Theorem, there exists an index pp such that ϕf(p)≃ϕp\phi_{f(p)} \simeq \phi_p.
  5. Now we check the two possible values of f(p)f(p):
    • Case 1: f(p)=if(p) = i. Since i∈Ii \in I, we have ϕf(p)∈A\phi_{f(p)} \in A. However, by the definition of ff, f(p)=if(p) = i holds if and only if p∉Ip \notin I, meaning ϕp∉A\phi_p \notin A. Because ϕf(p)≃ϕp\phi_{f(p)} \simeq \phi_p and II is extensional, this is a contradiction.
    • Case 2: f(p)=jf(p) = j. Since j∉Ij \notin I, we have ϕf(p)∉A\phi_{f(p)} \notin A. By the definition of ff, f(p)=jf(p) = j holds if and only if p∈Ip \in I, meaning ϕp∈A\phi_p \in A. Again, ϕf(p)≃ϕp\phi_{f(p)} \simeq \phi_p gives a contradiction.

As the only assumption we made was that II is recursive, II cannot be recursive.

Why this matters for compilers and Rust

Rice’s Theorem draws a sharp line between two kinds of checks a compiler can run:

  • Syntactic properties look at the structure of the code itself, not at the function it computes. Checking whether a variable is used in an expression (like Rust’s #[must_use] lint) or whether every branch of a match is covered is syntactic, and can therefore be checked efficiently at compile time.
  • Semantic properties ask about runtime behavior across all executions: whether a function always terminates, whether a pointer ever dangles, or what the exact minimum lifetime of every reference is. Because these properties are non-trivial and extensional, no algorithm can decide them exactly for all programs.

This is why static type systems like Rust’s borrow checker have to approximate. To stay both sound (never accepting an unsafe program) and decidable at compile time, the compiler must reject some programs that are actually safe. Better analyses like Non-Lexical Lifetimes or Polonius can shrink that gap and accept more valid patterns, but by Rice’s Theorem, the gap itself can never disappear completely.