CS302 Lecture Notes - The Halting Problem

This is by no means meant to be a complete treatment of the halting problem. However, the halting problem is an important problem for you to see, and this introduction will let you either:

  1. Have an easier time with it when you get the full treatment later in life.
  2. Pretend to friends, family and potential employers that you actually understand the halting problem.

Let's couch the halting problem as a cruel lab:

Write the procedure:

string halt(char *program, char *input);

Its first parameter, program is the name of a C++ program which, when compiled, will execute, reading from standard input.

The program halt must return in a finite amount of time, and what it returns depends on what happens when program is compiled and executes on the contents of the file input as standard input. If program finishes in a finite amount of time, then halt should return "halt". Otherwise, halt should return "infinite loop" and exit.

halt() is not allowed to ever go into an infinite loop. In other words, it must eventually finish.

If you cannot write halt(), prove to me why?

This is the halting problem

The proof is a bit hinky, but it is sound. Suppose you can write halt(). Then you can write the following procedure called selfhalt:

string selfhalt(char *f)
  return halt(f, f);

Next, you can write the program hproblem.cpp, which takes just one command line argument, the name of a C++ program:

#include <iostream>
#include <string>
using namespace std;

int main(int argc, char **argv)
  if (argc != 1) {
    cerr << "usage: hproblem program\n";

  if (selfhalt(argv[1]) == "halt") while(1);

Now, we compile and run hproblem on itself, and we have problems:

UNIX> hproblem hproblem.cpp
If this program were to exit in a finite period of time, then selfhalt("hproblem.cpp") would have returned "infinite loop", which means that "hproblem hproblem.cpp" should never return.

On the other hand, if this program were to loop forever, then selfhalt("hproblem.cpp") would have returned "halt", which means that hproblem hproblem.cpp" should return in a finite period of time.

This contradiction is why you cannot write halt() -- if this seems like the stuff of puzzle books for precocious 11-year olds, you're right, but the logic is tight. If you are interested in further theory along these lines, try Wikipedia's Halting Problem Page, not to mention Wikipedia's page on Godel's incompleteness theorem.