Hello World! An Invitation to Computer Science
Lecture Slides
The Halting Problem
Thursday May 8, 2008
- state diagrams
- AND, OR, NOT as Turing machines
- making Turing machines more robust
- unary v. binary; representing non-numeric data
Can we compute "majority" on a Turing machine?
- break problem into subparts:
use stack to count
see a 1? reduce 0 count, or add to 1 count
see a 0? reduce 1 count, or add to 0 count
find stack on tape
return head to rest of data
- each part can be computed by a TM
- can glue TMs together (but have to rename states)
- upshot: yes, but easier said than done
Church-Turing Thesis
- weak: any problem solved via numerical algorithm
can be solved by a Turing machine
- strong: any algorithm can be represented by a Turing machine
- stronger: any problem that can be solved
can be solved by a Turing machine
- theses vs. theorems: (reverse of all these are theorems)
- only says what can be done; not how fast
When does an algorithm or TM halt?
- does it always halt? does it halt with input x?
- useful: we don't want to wait forever for an answer!
- time comes into play:
cannot distinguish between slow and forever
- want to know in advance if computation will ever complete
- example: check before trying to divide by zero
- goal: construct algorithm (or TM) that can,
decide, in finite number of steps,
whether another TM and its input will halt
Proof by contradiction
- contradiction: true and not true
- example: there are infinitely many primes!
suppose not
let N be the largest prime
let M = N! + 1
M is prime or must have prime factor bigger than N
contradiction - reductio ad absurdum!
Universal Turing machine
a simple Turing machine - encoded
(A,0,1,L,A) ==> 01 * 00 * 01 * 10 * 01 *
(A,1,0,L,A) ==> 01 * 01 * 00 * 10 * 01 *
(A,*,*,R,B) ==> 01 * 11 * 11 * 01 * 10 *
(B,0,0,R,B) ==> 10 * 00 * 00 * 01 * 10 *
(B,1,1,R,B) ==> 10 * 01 * 01 * 01 * 10 *
(B,*,*,N,H) ==> 10 * 11 * 11 * 00 * 11 *
glued together:
01*00*01*10*01*01*01*00*10*01*01*11*11*01*10*10*00*00*01*10*10*01*01*01*10*10*11*11*00*11**
Turing machine to solve the halting problem
- suppose such a "halt-tester" TM, called F, exists
- given inputs T#, x:
if T# encodes a TM called T and T halts with input x
then F outputs 1, otherwise outputs 0
- final stage of F look like (_,_,0,R,HALT) or (_,_,1,R,HALT)
Turing machine that partially decides halting
we can augment F to G by changing:
(_,_,1,R,HALT) ==> (_,_,1,R,NEW)
and adding:
(NEW,_,_,NOMOVE,NEW)
given input T# and x, G only halts if T does not halt on x
Testing if a Turing machine halts on itself
- augment G to H
- takes as input T#, copies it to make tape T#T#
- does exactly what G would do with input T#, T#
- H halts precisely when T does not halt on T#
- doesn't matter what it means for T to execute on itself
- algorithmically, a TM halts or not, for any input
The halting problem is unsolvable!
- thought experiment: run H on itself
- halts when H does not halt on H#
- does not halt when H halts on H#
- just like "this statement is false"
- contradiction!
Implications
- Halting Problem + CT-Thesis: limit applies to any model
- programs are and will always be difficult to debug
- there is no silver bullet
- BSOD: very difficult to remove all occurrences
of such problems in advance
- can solve specific instances:
for given program and given input, can detect infinite loops
but no general tool can exist
- even then, we can't always tell (because of efficiency issues)
(Turing's original paper)