I don’t understand the Halting Problem

theory

I just ran across an answer to another question that references the Halting Problem. He starts with this snippet:

def halts( code_block ):
    # Some magical code

def whistler():
    while halts(whistler): 
        sys.whistle( 1 )

and then explains how the routine halts cannot be defined correctly because it essentially ends up being logically equivalent to "this sentence is false". Aside from the Python-style syntax, this is basically the standard explanation of the halting problem, and I've never understood the concept. Every time I look at that type of example, I think "but why in the world would anyone approach the problem in that way in the first place?"

If I wanted to determine a difficult property of a piece of code, such as answering the question of whether or not it halts, there's no way I would put the analysis inside the code to be analyzed! First off, that leads to contradictions like this example, and second, adding analysis code changes the nature of the thing being analyzed. I would almost certainly go about looking for the answer to the question with external tools, not internal ones.

So yes, I understand that there's no way to write a correct implementation of halts in the example above. But let's restrict ourselves solely to the realm of code that actually can be written correctly, instead of hypotheticals. What's an example of code that can actually be written and executed, for which it is impossible to determine by external analysis whether the code will end up in an infinite loop or whether it will terminate?

Best Answer

Every time I look at that type of example, I think "but why in the world would anyone approach the problem in that way in the first place?"

If I wanted to determine a difficult property of a piece of code, such as answering the question of whether or not it halts, there's no way I would put the analysis inside the code to be analyzed!

The trick here is that this is a proof by contradiction : assume A, demonstrate it would imply a paradox, conclude assumption was wrong.

So the idea is not that we "would approach the problem in that way" or that we "would put the analysis inside the code to be analyzed". The idea is rather: suppose someone came up with a program and said "Look, I wrote a program that tell if any given program will halt or not". "Oh really? Lemme see what happens if I plug it into itself?" KABOOM!

There's no need to actually take an existing example: this already proves there's no way such a program could exist.

On a side note, it does not mean we cannot try to write such a halts function. It just means it is impossible to write a halts function that will work for any program.

Related Topic