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?
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"; exit(1); } if (selfhalt(argv[1]) == "halt") while(1); exit(0); } |
Now, we compile and run hproblem on itself, and we have problems:
UNIX> hproblem hproblem.cppIf 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.