Search code examples
infinite-loophalting-problem

Detecting if a Program is in an Infinite Loop (Read: Solving the Halting Problem)


Is detecting whether a deterministic program (i.e. state machine) is in an infinite loop equivalent to solving the halting problem?

I came up with a solution, and I'm not sure why it shouldn't work:

  • Let the program run
  • When you think it's in an infinite loop, take a snapshot of its memory regular intervals
  • If you ever detect the same snapshot, the program is in an infinite loop
  • As long as you don't get the same snapshot twice, it's either (1) not in an infinite loop, or (2) you need to take snapshots more quickly (perhaps once on every memory access?)

I'm assuming this doesn't work... but why?

It seems like a perfectly reasonable way to detect if a program is in an infinite loop (e.g. especially if you store hashes rather than the memory itself, although that will not be 100% accurate)... what's wrong with it, if anything?


Solution

  • The solution wouldn't work even in principle. A Turing machine doesn't have to ever be in precisely the same state (with the tape in the same configuration) to get into an infinite loop.

    Your algorithm might work for context-sensitive languages and linear-bounded automata, but if you can't know how much tape a TM is going to need, you'd never know if you had an infinite loop or were about to hit the top. Note that your method would clearly work for real computers for a variety of reasons... chief among them being that your computer is less powerful than a (big) finite automaton.