What exactly is the halting problem?

前端 未结 22 718
时光说笑
时光说笑 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.

提交回复
热议问题