Let's assume that it is possible to write a Python program that
correctly tells whether other Python programs will eventually halt
when given particular inputs. Let's call our hypothetical program
** halts**. Here's a rough sketch of what it would
look like:

def halts(program, data): ...lots of complicated code... if ...some condition...: return True else: return False

** Halts** takes two inputs, called

def progA(input): print 'done' def progB(input): if input == 1: while True: pass # this causes an infinite loop else: print 'done'

Now we can ask ** halts** what these programs do when given
various inputs.

would return`halts(progA, 1)``True`would return`halts(progA, 2)``True`

** ProgB** loops forever if we happen to feed it the value 1.
Any other value will cause it to halt:

would return`halts(progB, 1)``False`would return`halts(progB, 2)``True`

So far, we have every reason to believe that ** halts** could
exist, at least in principle, even though it might be a rather complicated
program to write. At this point, Turing says "OH YEAH? If

def turing(program): if halts(program, program): while True: pass else: print 'done'

At this point, we say "Yes, so what?" Turing laughs and says "Well, what
happens when I feed the ** turing** program to itself?"

`turing(turing)`

What happens indeed? Let's analyze the situation:

The

program uses`turing`to analyze the Python program given to it as input:`halts`- If the Python program halts when fed itself as input, then
never halts.`turing` - If the Python program never halts when fed itself as input, then
prints 'done' and halts.`turing`

- If the Python program halts when fed itself as input, then
But if the Python program happens to be

, then:*the turing program itself*- If
halts when fed itself as input, then`turing`never halts.`turing` - If
never halts when fed itself as input, then`turing`prints 'done' and halts.`turing`

- If
This is a blatant logical contradiction!

can neither halt nor loop forever; it doesn't make sense either way.`turing(turing)`

Thus our original assumption about the existence of ** halts**
must have been invalid, since as we just saw, it's easy to define the
logically-impossible

*Q.E.D.*

ConclusionThe task of deciding if an arbitrary computation will ever terminate cannot be described computationally |