# Rice's theorem

In computability theory, Rice's theorem states that all non-trivial, semantic properties of programs are undecidable. A semantic property is one about the programs behavior (for instance, does the program terminate for all inputs), unlike a syntactic property (for instance, does the program contain an if-then-else statement). A property is non-trivial if it is neither true for every program, nor for no program.

Rice's theorem can also be put in terms of functions: for any non-trivial property of partial functions, no general and effective method can decide whether an algorithm computes a partial function with that property. Here, a property of partial functions is called trivial if it holds for all partial computable functions or for none, and an effective decision method is called general if it decides correctly for every algorithm. The theorem is named after Henry Gordon Rice, and is also known as the Rice–Myhill–Shapiro theorem after Rice, John Myhill, and Norman Shapiro.

## Introduction

Another way of stating Rice's theorem that is more useful in computability theory follows.

Let S be a set of languages that is nontrivial, meaning

1. there exists a Turing machine that recognizes a language in S
2. there exists a Turing machine that recognizes a language not in S

Then, it is undecidable to determine whether the language recognized by an arbitrary Turing machine lies in S.

In practice, this means that there is no machine that can always decide whether the language of a given Turing machine has a particular nontrivial property. Special cases include the undecidability of whether a Turing machine accepts a particular string, whether a Turing machine recognizes a particular recognizable language, and whether the language recognized by a Turing machine could be recognized by a nontrivial simpler machine, such as a finite automaton.

It is important to note that Rice's theorem does not say anything about those properties of machines or programs that are not also properties of functions and languages. For example, whether a machine runs for more than 100 steps on some input is a decidable property, even though it is non-trivial. Implementing exactly the same language, two different machines might require a different number of steps to recognize the same input. Similarly, whether a machine has more than 5 states is a decidable property of the machine, as the number of states can simply be counted. Where a property is of the kind that either of the two machines may or may not have it, while still implementing exactly the same language, the property is of the machines and not of the language, and Rice's Theorem does not apply.

Using Rogers' characterization of acceptable programming systems, Rice's Theorem may essentially be generalized from Turing machines to most computer programming languages: there exists no automatic method that decides with generality non-trivial questions on the behavior of computer programs.

As an example, consider the following variant of the halting problem. Let P be the following property of partial functions F of one argument: P(F) means that F is defined for the argument '1'. It is obviously non-trivial, since there are partial functions that are defined at 1, and others that are undefined at 1. The 1-halting problem is the problem of deciding of any algorithm whether it defines a function with this property, i.e., whether the algorithm halts on input 1. By Rice's theorem, the 1-halting problem is undecidable. Similarly the question of whether a Turing machine T terminates on an initially empty tape (rather than with an initial word w given as second argument in addition to a description of T, as in the full halting problem) is still undecidable.

## Formal statement

Let ${\displaystyle \phi \colon \mathbb {N} \to \mathbf {P} ^{(1)}}$ be an admissible numbering of the computable functions; a map from the natural numbers to the class ${\displaystyle \mathbf {P} ^{(1)}}$ of unary (partial) computable functions. Denote by ${\displaystyle \phi _{e}:=\phi (e)}$ the eth (partial) computable function.

We identify each property that a computable function may have with the subset of ${\displaystyle \mathbf {P} ^{(1)}}$ consisting of the functions with that property. Thus given a set ${\displaystyle F\subseteq \mathbf {P} ^{(1)}}$, a computable function ${\displaystyle \phi _{e}}$ has property F if and only if ${\displaystyle \phi _{e}\in F}$. For each property ${\displaystyle F\subseteq \mathbf {P} ^{(1)}}$ there is an associated decision problem ${\displaystyle D_{F}}$ of determining, given e, whether ${\displaystyle \phi _{e}\in F}$.

Rice's theorem states that the decision problem ${\displaystyle D_{F}}$ is decidable (also called recursive or computable) if and only if ${\displaystyle F=\emptyset }$ or ${\displaystyle F=\mathbf {P} ^{(1)}}$.

## Examples

According to Rice's theorem, if there is at least one computable function in a particular class C of computable functions and another computable function not in C then the problem of deciding whether a particular program computes a function in C is undecidable. For example, Rice's theorem shows that each of the following sets of computable functions is undecidable:

• The class of computable functions that return 0 for every input, and its complement.
• The class of computable functions that return 0 for at least one input, and its complement.
• The class of computable functions that are constant, and its complement.
• The class of indices for computable functions that are total [1]
• The class of indices for recursively enumerable sets that are cofinite
• The class of indices for recursively enumerable sets that are recursive

## Proof by Kleene's recursion theorem

A corollary to Kleene's recursion theorem states that for every Gödel numbering ${\displaystyle \phi \colon \mathbb {N} \to \mathbf {P} ^{(1)}}$ of the computable functions and every computable function ${\displaystyle Q(x,y)}$, there is an index ${\displaystyle e}$ such that ${\displaystyle \phi _{e}(y)}$ returns ${\displaystyle Q(e,y)}$. (In the following, we say that ${\displaystyle f(x)}$ "returns" ${\displaystyle g(x)}$ if either ${\displaystyle f(x)=g(x)}$, or both ${\displaystyle f(x)}$ and ${\displaystyle g(x)}$ are undefined.) Intuitively, ${\displaystyle \phi _{e}}$ is a quine, a function that returns its own source code (Gödel number), except that rather than returning it directly, ${\displaystyle \phi _{e}}$ passes its Gödel number to ${\displaystyle Q}$ and returns the result.

Let ${\displaystyle F}$ be a set of computable functions such that ${\displaystyle \emptyset \neq F\neq \mathbf {P} ^{(1)}}$. Then there are computable functions ${\displaystyle f\in F}$ and ${\displaystyle g\notin F}$. Suppose that the set of indices ${\displaystyle x}$ such that ${\displaystyle \phi _{x}\in F}$ is decidable; then, there exists a function ${\displaystyle Q(x,y)}$ that returns ${\displaystyle g(y)}$ if ${\displaystyle \phi _{x}\in F}$, and ${\displaystyle f(y)}$ otherwise. By the corollary to the recursion theorem, there is an index ${\displaystyle e}$ such that ${\displaystyle \phi _{e}(y)}$ returns ${\displaystyle Q(e,y)}$. But then, if ${\displaystyle \phi _{e}\in F}$, then ${\displaystyle \phi _{e}}$ is the same function as ${\displaystyle g}$, and therefore ${\displaystyle \phi _{e}\notin F}$; and if ${\displaystyle \phi _{e}\notin F}$, then ${\displaystyle \phi _{e}}$ is ${\displaystyle f}$, and therefore ${\displaystyle \phi _{e}\in F}$. In both cases, we have a contradiction.

## Proof by reduction to the halting problem

### Proof sketch

Suppose, for concreteness, that we have an algorithm for examining a program p and determining infallibly whether p is an implementation of the squaring function, which takes an integer d and returns d2. The proof works just as well if we have an algorithm for deciding any other nontrivial property of programs, and is given in general below.

The claim is that we can convert our algorithm for identifying squaring programs into one that identifies functions that halt. We will describe an algorithm that takes inputs a and i and determines whether program a halts when given input i.

The algorithm for deciding this is conceptually simple: it constructs (the description of) a new program t taking an argument n, which (1) first executes program a on input i (both a and i being hard-coded into the definition of t), and (2) then returns the square of n. If a(i) runs forever, then t never gets to step (2), regardless of n. Then clearly, t is a function for computing squares if and only if step (1) terminates. Since we've assumed that we can infallibly identify programs for computing squares, we can determine whether t, which depends on a and i, is such a program, and that for every a and i; thus we have obtained a program that decides whether program a halts on input i. Note that our halting-decision algorithm never executes t, but only passes its description to the squaring-identification program, which by assumption always terminates; since the construction of the description of t can also be done in a way that always terminates, the halting-decision cannot fail to halt either.

 halts (a,i) {
define t(n) {
a(i)
return n×n
}
return is_a_squaring_function(t)
}


This method doesn't depend specifically on being able to recognize functions that compute squares; as long as some program can do what we're trying to recognize, we can add a call to a to obtain our t. We could have had a method for recognizing programs for computing square roots, or programs for computing the monthly payroll, or programs that halt when given the input "Abraxas", or programs that commit array bounds errors; in each case, we would be able to solve the halting problem similarly.

### Formal proof

If we have an algorithm that decides a non-trivial property, we can construct a Turing machine that decides the halting problem.

For the formal proof, algorithms are presumed to define partial functions over strings and are themselves represented by strings. The partial function computed by the algorithm represented by a string a is denoted Fa. This proof proceeds by reductio ad absurdum: we assume that there is a non-trivial property that is decided by an algorithm, and then show that it follows that we can decide the halting problem, which is not possible, and therefore a contradiction.

Let us now assume that P(a) is an algorithm that decides some non-trivial property of Fa. Without loss of generality we may assume that P(no-halt) = "no", with no-halt being the representation of an algorithm that never halts. If this is not true, then this holds for the negation of the property. Since P decides a non-trivial property, it follows that there is a string b that represents an algorithm and P(b) = "yes". We can then define an algorithm H(a, i) as follows:

1. construct a string t that represents an algorithm T(j) such that
• T first simulates the computation of Fa(i)
• then T simulates the computation of Fb(j) and returns its result.
2. return P(t).

We can now show that H decides the halting problem:

• Assume that the algorithm represented by a halts on input i. In this case Ft = Fb and, because P(b) = "yes" and the output of P(x) depends only on Fx, it follows that P(t) = "yes" and, therefore H(a, i) = "yes".
• Assume that the algorithm represented by a does not halt on input i. In this case Ft = Fno-halt, i.e., the partial function that is never defined. Since P(no-halt) = "no" and the output of P(x) depends only on Fx, it follows that P(t) = "no" and, therefore H(a, i) = "no".

Since the halting problem is known to be undecidable, this is a contradiction and the assumption that there is an algorithm P(a) that decides a non-trivial property for the function represented by a must be false.

## Rice's theorem and index sets

Rice's theorem can be succinctly stated in terms of index sets:

Let ${\displaystyle {\mathcal {C}}}$ be a class of partial recursive functions with index set ${\displaystyle C}$. Then ${\displaystyle C}$ is recursive if and only if ${\displaystyle C=\emptyset }$ or ${\displaystyle C=\mathbb {N} }$.

where ${\displaystyle \mathbb {N} }$ is the set of natural numbers, including zero.

## An analogue of Rice's theorem for recursive sets

One can regard Rice's theorem as asserting the impossibility of effectively deciding for any recursively enumerable set whether it has a certain nontrivial property.[2] In this section, we give an analogue of Rice's theorem for recursive sets, instead of recursively enumerable sets.[3] Roughly speaking, the analogue says that if one can effectively determine for any recursive set whether it has a certain property, then finitely many integers determine whether a recursive set has the property. This result is analogous to the original Rice's theorem because both assert that a property is "decidable" only if one can determine whether a set has that property by examining for at most finitely many ${\displaystyle i}$ (for no ${\displaystyle i}$, for the original theorem), if ${\displaystyle i}$ belongs to the set.

Let ${\displaystyle W}$ be a class (called a simple game and thought of as a property) of recursive sets. If ${\displaystyle S}$ is a recursive set, then for some ${\displaystyle e}$, computable function ${\displaystyle \phi _{e}}$ is the characteristic function of ${\displaystyle S}$. We call ${\displaystyle e}$ a characteristic index for ${\displaystyle S}$. (There are infinitely many such ${\displaystyle e}$.) Let's say the class ${\displaystyle W}$ is computable if there is an algorithm (computable function) that decides for any nonnegative integer ${\displaystyle e}$ (not necessarily a characteristic index),

• if ${\displaystyle e}$ is a characteristic index for a recursive set belonging to ${\displaystyle W}$, then the algorithm gives "yes";
• if ${\displaystyle e}$ is a characteristic index for a recursive set not belonging to ${\displaystyle W}$, then the algorithm gives "no".

A set ${\displaystyle S\subseteq \mathbb {N} }$ extends a string ${\displaystyle \tau }$ of 0's and 1's if for any ${\displaystyle k<|\tau |}$ (the length of ${\displaystyle \tau }$), the ${\displaystyle k}$th element of ${\displaystyle \tau }$ is 1 if ${\displaystyle k\in S}$; is 0 otherwise. For example, ${\displaystyle S=\{1,3,4,7,\ldots \}}$ extends string ${\displaystyle 01011001}$. A string ${\displaystyle \tau }$ is winning determining if any recursive set extending ${\displaystyle \tau }$ belongs to ${\displaystyle W}$. A string ${\displaystyle \tau }$ is losing determining if no recursive set extending ${\displaystyle \tau }$ belongs to ${\displaystyle W}$.

We can now state the following analogue of Rice's theorem (Kreisel, Lacombe, and Shoenﬁeld, 1959,[4] Kumabe and Mihara, 2008[5]):

A class ${\displaystyle W}$ of recursive sets is computable if and only if there are a recursively enumerable set ${\displaystyle T_{0}}$ of losing determining strings and a recursively enumerable set ${\displaystyle T_{1}}$ of winning determining strings such that any recursive set extends a string in ${\displaystyle T_{0}\cup T_{1}}$.

This result has been applied to foundational problems in computational social choice (more broadly, algorithmic game theory). For instance, Kumabe and Mihara (2008,[5] 2008[6]) apply this result to an investigation of the Nakamura numbers for simple games in cooperative game theory and social choice theory.

2. ^ A set ${\displaystyle S\subseteq \mathbb {N} }$ is recursively enumerable if ${\displaystyle S=W_{e}:={\textrm {dom}}\,\phi _{e}:=\{x:\phi _{e}(x)\downarrow \}}$ for some ${\displaystyle e}$, where ${\displaystyle W_{e}}$ is the domain ${\displaystyle {\textrm {dom}}\,\phi _{e}}$ (the set of inputs ${\displaystyle x}$ such that ${\displaystyle \phi _{e}(x)}$ is defined) of ${\displaystyle \phi _{e}}$. The result for recursively enumerable sets can be obtained from that for (partial) computable functions by considering the class ${\displaystyle \{\phi _{e}:{\textrm {dom}}\,\phi _{e}\in C\}}$, where ${\displaystyle C}$ is a class of recursively enumerable sets.
3. ^ A recursively enumerable set ${\displaystyle S\subseteq \mathbb {N} }$ is recursive if its complement is recursively enumerable. Equivalently, ${\displaystyle S}$ is recursive if its characteristic function is computable.