CMU 15-112: Fundamentals of Programming and Computer Science
Class Notes: The Halting Problem
Question: can we automatically verify any arbitrary Python program P works properly?
- This is really important! Programs control our cars and planes and banks and nuclear weapons and most everything else. It would be just lovely if we could be sure they actually worked right.
- When these programs fail, the stock market flash-crashes, planes crash, and people die. This is very serious business.
- Restated: given a program P and an arbitrary input W, does P(W) eventually halt (and not run forever), and then does it return the correct result?
- Let's just focus on the first part: does P(W) even halt at all (ignoring whether or not it returns the correct result)? This is the Halting Problem.
- Aside: we know Python programs can take other Python programs as input. Consider our linter, our autograder, and Python itself.
Assume we have a function Halts(P, W) that halts if program P halts on input W.
Then we write this perfectly-valid Python function:
def K(P): if (Halts(P, P)): while True: pass # run forever else: return 42 # halt!
Now, what happens when we call K(K)?
- If Halts(K,K) is True, then K(K) hangs (runs forever)
- If Halts(K,K) is False, then K(K) halts
- So either way, Halts(K,K) is wrong.
- Thus, the Halts function cannot exist!
- So: the Halting Problem is uncomputable
- So: we cannot tell if arbitrary programs work.
- Though we can tell if some programs work. See here.
- You can even win a Turing Award for this! See here.
- The race is on to prove more and more programs work, but the Halting Problem guarantees we will never prove that they all work.