What exactly is the halting problem?

前端 未结 22 660
时光说笑
时光说笑 2020-11-29 00:01

Whenever people ask about the halting problem as it pertains to programming, people respond with \"If you just add one loop, you\'ve got the halting program and therefore yo

相关标签:
22条回答
  • 2020-11-29 00:27

    "If you just add one loop, you've got the halting program and therefore you can't automate task"

    Sounds like someone over generalizing the application of the halting problem. There are plenty of particular loops that you can prove terminate. There exists research that can perform termination checking for wide classes of programs. For instance in Coq you are limited to programs that you can prove terminate. Microsoft has a research project called Terminator that uses various approximations to prove that programs will terminate.

    But, remember, the halting problem isn't just about toy examples. Neither of those solves the general 'halting problem', because they don't work for every program.

    The problem is that the halting problem says that there exist programs that you have no way to know if they will terminate without running them, which means that you may never get done deciding if they halt.

    An example of a program that may or may not halt (in Haskell):

    collatz 1 = ()
    collatz !n | odd n     = collatz (3 * n + 1)
               | otherwise = collatz (n `div` 2)
    

    or in something more accessible:

    while (n != 1)
        if (n & 1 == 1) 
            n = 3 * n + 1;
        else 
            n /= 2;
    

    Given every integer >= 1, will this program halt? Well, it has worked so far, but there is no theorem that says it will halt for every integer. We have a conjecture due to Lothar Collatz that dates back to 1937 that it holds, but no proof.

    0 讨论(0)
  • 2020-11-29 00:28

    This has been beaten to death so well that there is actually a poetic proof, written in the style of Lewis Carroll Dr. Seuss by Geoffrey Pullum (he of Language Log fame).

    Funny stuff. Here's a taste:

    Here’s the trick that I’ll use – and it’s simple to do.
    I’ll define a procedure, which I will call Q,
    that will use P’s predictions of halting success
    to stir up a terrible logical mess.

    ...

    No matter how P might perform, Q will scoop it:
    Q uses P’s output to make P look stupid.
    Whatever P says, it cannot predict Q:
    P is right when it’s wrong, and is false when it’s true!

    0 讨论(0)
  • 2020-11-29 00:30

    The precise definition of the problem is that you need to write a program that does the following: - takes an arbitrary program - determines if the program halts given any arbitrary finite input into the program

    However, this is a really high bar. There are many partial solutions to the halting problem, but no general solution. Even worse, even finding programs that partially solve the halting problem is known to be difficult:

    BBC h2g2 article on the halting problem

    If you have truly solved the halting problem, there work on sites like rentacoder.com for you. A few months ago there was a post on one of them from a user named ATuring who offered a contract to solve the halting problem. :)

    0 讨论(0)
  • 2020-11-29 00:31

    There's an OK proof the Halting Problem on wikipedia.

    To illustrate, exactly, why just applying some technique to loops is insufficient, consider the following program (pseudocode):

    int main()
    {
      //Unbounded length integer
      Number i = 3;
    
      while(true)
      {
        //example: GetUniquePositiveDivisiors(6) = [1, 2, 3], ...(5) = 1, ...(10) = 1, 2, 5, etc.
        Number[] divisiors = GetUniquePositiveDivisiors(i);
        Number sum = 0;
        foreach(Number divisor in divisiors) sum += divisor;
    
        if(sum == i) break;
    
        i+=2;
      }
    }
    

    Can you think of an approach that will return true if this code halts, and false otherwise?

    Think Carefully.

    If by chance you're in serious contention for a Fields medal, imagine some code for these problems in place of the above.

    0 讨论(0)
提交回复
热议问题