On this page
Can a program read another program and tell you whether it ever outputs 42, or stops on every input? For every question of that kind the answer is no, and one short argument, Rice's theorem, explains why. You will then spot it behind many of the cautious warnings your linter gives.
What does Rice's theorem say?
Rice's theorem states that all non-trivial semantic properties of programs are undecidable (Wikipedia: Rice's theorem). Two words carry the weight.
Semantic means the property depends only on the function the program computes, its input-output behaviour, and not on how the code is written. If two programs compute the same function, they must get the same answer.
Non-trivial means at least one program has the property and at least one does not.
Undecidable means no algorithm answers correctly for every program in finite time. H. G. Rice published the result in 1953 as "Classes of recursively enumerable sets and their decision problems" in the Transactions of the American Mathematical Society (DOI 10.1090/S0002-9947-1953-0053041-6).
How is Rice's theorem proved from the halting problem?
The proof rests on the halting problem, whose undecidability goes back to Alan Turing's paper "On Computable Numbers" (DOI 10.1112/plms/s2-42.1.230): no program decides whether program a halts on input i. New to it? Read why the halting problem is undecidable first.
Suppose a checker D decides some non-trivial semantic property P.
If a program that never halts has P, swap P for its opposite (deciding one decides the other). So assume it lacks P.
Because P is non-trivial, some program b has P.
Given any a and i, build a new program t. On input n, t first simulates a on i silently, then runs b on n and returns what b returns.
If a halts on i, t computes exactly what b computes, so t has P. If a runs forever, t never halts on any input, so t lacks P.
So D(t) tells you whether a halts on i. D only reads t's code, so it would solve the halting problem, which is impossible.
Worked examples: does it ever print 42? Is it total?
Example 1: does p output 42 on some input? Semantic (it depends only on outputs) and non-trivial (`return 42` has it, `return 0` does not), so undecidable. The gadget is t(n) = simulate a on i, then output 42. This t outputs 42 exactly when a halts on i.
Example 2: is p total, meaning it halts on every input? Semantic and non-trivial again: `return n + 1` is total, an infinite loop is not. Take t(n) = simulate a on i, then return n. This t is total exactly when a halts on i, so totality is undecidable, as the list of examples in the Rice's theorem article also notes.
Example 3: does p compute the same function as a reference program q? Also undecidable. That is one reason online judges test your code on cases rather than try to prove it equals the model answer.
What does Rice's theorem not cover?
The theorem is silent whenever two programs that compute the same function could get different answers (statement and examples).
Syntax. "Does the code contain an if-then-else?" or "Is it under 50 lines?" are about the text, and you decide them by reading it.
Running time on a given input. "Does p halt on input 5 within a million steps?" is decidable: simulate a million steps and look. A fast and a slow sorting program compute the same function, so speed is not semantic.
Trivial properties. "Does p compute some partial function?" is always yes.
Before you call a question undecidable, run this check.
Is it about the function computed, not the code or a single bounded run?
Would two programs computing the same function always get the same answer?
Can you name one program with the property and one without?
Three yeses: Rice applies. Any no: you need another argument, or the question may be easy. The busy beaver function is a good place to practise telling the two apart.
What does Rice's theorem mean for static analysis?
A static analyser reads code without running it. By Rice's theorem, no tool can check whether every program is correct, or even runs without error (Rice's theorem and static analysis). Real tools pick a side.
Sound tools over-approximate: they never miss a bug of the kind they look for, but raise false alarms.
Unsound tools under-approximate: few false alarms, but some bugs slip through.
Type systems change the question: they accept only programs whose safety can be read from the syntax, and reject some correct ones.
For background, revisit how a Turing machine works and look up reductions in the Fermi Concept Library. If step 5 of the proof still feels slippery, Fermi's AI tutor notices where you are stuck and decides whether to explain, ask or show.



