Imagine a program, executed by an interpreter to be a Turing Machine. Consider this code:
x = read_input
print x
Does undecidability mean that there may possibly be an input to this program such that the program may never halt?
Short answer: No.
Undecidability is a property of problems, not of programs. What is undecidable is however to check if some given program ever halts on any input. This problem has the program as input.
If you fix a program, then it may be decidable to check if the program halts for some input. This problem has the program input as input. In particular, for programs such as yours that always terminate, the termination problem is decidable, meaning there exists an algorithm for it: it just outputs "yes".
Note that the term "undecidability" only has a meaning in environments that allow arbitrary memory size and arbitrary input size. The Python language may fit to this definition, but the interpreter your are using certainly doesn't.
P
and its input x
as input and decide whether P
halts on x
.
$\endgroup$
Commented
Oct 23, 2014 at 15:08
Undecidability tells us that, given an arbitrary program and some input, there is no general computational procedure which tells us whether that program halts on that input.
It says nothing about specific computational procedures which may be correct for specific (classes of) programs. That is, we can generate heuristics which correctly answer the question for some programs; the more complicated we're willing to make the heuristics, the more programs may be potentially covered. All that undecidability says is that no finite set of heuristics is going to correctly cover all cases.
To illustrate, we can start developing heuristics to decide whether a program $P$ halts on an input $x$. Let's narrow down our language first: we'll assume a procedural language, with no function calls, and the only control flow constructs are while
and if-else
. On the right machine, this system is Turing-equivalent.
If the program doesn't contain a
while
loop, then it halts.If every basic block in the program contains a
return
statement, then it halts.
You can take this as far as you want to go, defining a sort of "halting semantics" for your programming language. The semantics will never be correct for all possible programs - if your language/machine is Turing-equivalent - but you can do pretty good. The above (correct) heuristics are enough to definitively answer the example in your question.
x = raw_input() \n eval(x) \n print "input program terminates"
? This takes an input program and outputs "input program terminates" if and only if the input read fromraw_input()
is a valid Python program that terminates. $\endgroup$read_input
does more carefully (does it read a fixed amount of input, or read until end of file?). $\endgroup$