Transcript Module 7

Module 7
• Halting Problem
– Fundamental program behavior problem
– A specific unsolvable problem
– Diagonalization technique revisited
• Proof more complex
1
Definition
• Input
– Program P
• Assume the input to program P is a single unsigned int
– This assumption is not necessary, but it simplifies the following
unsolvability proof
– To see the full generality of the halting problem, remove this
assumption
– Nonnegative integer x, an input for program P
• Yes/No Question
– Does P halt when run on x?
• Notation
– Use H as shorthand for halting problem when space is a
constraint
2
Example Input *
• Program with one input of type unsigned int
bool main(unsigned int Q) {
int i=2;
if ((Q = = 0) || (Q= = 1)) return false;
while (i<Q) {
if (Q%i = = 0) return (false);
i++;
}
return (true);
}
• Input x
4
3
Three key definitions
4
Definition of list L *
SP* is countably infinite where SP = {characters, digits, white
space, punctuation}
• Type program will be type string with SP as the alphabet
• Define L to be the strings in SP* listed in enumeration order
•
– length 0 strings first
– length 1 strings next
– …
• Every program is a string in SP
– For simplicity, consider only programs that have
• one input
• the type of this input is an unsigned int
• Consider strings in SP* that are not legal programs to be
programs that always crash (and thus halt on all inputs)
5
Definition of PH *
• If H is solvable, some program must solve H
• Let PH be a procedure which solves H
– We declare it as a procedure because we will use PH as
a subroutine
• Declaration of PH
– bool PH(program P, unsigned int x)
• In general, the type of x should be the type of the input to P
• Comments
– We do not know how PH works
– However, if H is solvable, we can build programs
which call PH as a subroutine
6
Definition of program D
bool main(unsigned int y) /* main for program D */
{ program P = generate(y);
if (PH(P,y)) while (1>0); else return (yes); }
/* generate the yth string in SP* in enumeration order */
program generate(unsigned int y)
/* code for extra credit program of slide 21 from lecture
5 did this for {a,b}* */
bool PH(program P, unsigned int x)
/* how PH solves H is unknown */
7
Generating Py from y *
• We won’t go into this in detail here
– This was the basis of the question at the bottom of slide 21 of
lecture 5 (alphabet for that problem was {a,b} instead of SP).
– This is the main place where our assumption about the input type for
program P is important
• for other input types, how to do this would vary
• Specification
–
–
–
–
–
–
–
–
0 maps to program l
1 maps to program a
2 maps to program b
3 maps to program c
…
26 maps to program z
27 maps to program A
…
8
Proof that H is not solvable
9
Argument Overview *
H is solvable
Definition of
Solvability
PH exists
H is NOT solvable
PH does NOT exist
D’s code
D exists
L is list of
all programs
D is on list L
p
D does NOT exist
D is NOT on list L
q is logically equivalent to (not q)
(not p)
10
Proving D is not on list L
• Use list L to specify a program behavior B that is
distinct from all real program behaviors (for
programs with one input of type unsigned int)
– Diagonalization argument similar to the one for proving the
number of languages over {a} is uncountably infinite
– No program P exists that exhibits program behavior B
• Argue that D exhibits program behavior B
– Thus D cannot exist and thus is not on list L
11
Non-existent program behavior B
12
Visualizing List L *
P0
P1
P2
P3
P4
0
1
2
3
4
H
H
H
H
H
H NH
H
NH
H
NH NH NH NH NH
NH
H
H
H
H
H
NH NH
H
H
...
•#Rows is countably infinite
• Sp* is countably infinite
•#Cols is countably infinite
• Set of nonnegative integers
is countably infinite
...
• Consider each number to be a feature
– A program halts or doesn’t halt on each integer
– We have a fixed L this time
13
Diagonalization to specify B *
•We specify a non-existent program behavior B by using a unique feature
(number) to differentiate B from Pi
B
0
1
2
3
4
P0
NH
H
H
H
H
H
H NH
H
H
NH
H
P1
P2
P3
P4
...
NH NH NH
H NH NH
NH
H
H
H
H
H
NH
H NH
H
NH
H
...
14
Arguing D exhibits
program behavior B
15
Code for D
bool main(unsigned int y) /* main for program D */
{ program P = generate(y);
if (PH(P,y)) while (1>0); else return (yes); }
/* generate the yth string in SP* in enumeration order */
program generate(unsigned int y)
/* code for extra credit program of slide 21 from lecture
5 did this for {a,b}* */
bool PH(program P, unsigned int x)
/* how PH solves H is unknown */
16
Visualization of D in action on
input y
• Program D with input y
– (type for y: unsigned int)
0
1
2 ...
P0
P1
P2
H
H
H
H NH
H
NH NH
NH
y ...
...
Py
NH
H
...
Given input y, generate the
program (string) Py
Run PH on Py and y
• Guaranteed to halt since
PH solves H
IF (PH(Py,y)) while (1>0);
else return (yes);
D
17
Alternate Proof
18
Alternate Proof Overview
• For every program Py, there is a number y
that we associate with it
• The number we use to distinguish program
Py from D is this number y
• Using this idea, we can arrive at a
contradiction without explicitly using the
table L
– The diagonalization is hidden
19
H is not solvable, proof II
• Assume H is solvable
– Let PH be the program which solves H
– Use PH to construct program D which cannot exist
• Contradiction
– This means program PH cannot exist.
– This implies H is not solvable
• D is the same as before
20
Arguing D cannot exist
• If D is a program, it must have an associated number y
• What does D do on this number y?
• 2 cases
– D halts on y
• This means PH(D,y) = NO
– Definition of D
• This means D does not halt on y
– PH solves H
• Contradiction
• This case is not possible
21
Continued
– D does not halt on this number y
• This means PH(D,y) = YES
– Definition of D
• This means D halts on y
– PH solves H
• Contradiction
• This case is not possible
– Both cases are not possible, but one must be for D to exist
– Thus D cannot exist
22
Implications *
• The Halting Problem is one of the simplest problems
we can formulate about program behavior
• We can use the fact that it is unsolvable to show that
other problems about program behavior are also
unsolvable
• This has important implications restricting what we can
do in the field of software engineering
– In particular, “perfect” debuggers/testers do not exist
– We are forced to “test” programs for correctness even though
this approach has many flaws
23
Summary
• Halting Problem definition
– Basic problem about program behavior
• Halting Problem is unsolvable
– We have identified a specific unsolvable
problem
– Diagonalization technique
• Proof more complicated because we actually need to
construct D, not just give a specification B
24