Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Lecture 7 Topics –Problems about program behavior At least problem input is a program/algorithm –Halting Problem Fundamental problem about program behavior.

Similar presentations


Presentation on theme: "1 Lecture 7 Topics –Problems about program behavior At least problem input is a program/algorithm –Halting Problem Fundamental problem about program behavior."— Presentation transcript:

1 1 Lecture 7 Topics –Problems about program behavior At least problem input is a program/algorithm –Halting Problem Fundamental problem about program behavior A specific unsolvable problem Diagonalization technique revisited –Proof more complex

2 2 Problems about Programs What are some problems you might define where at least one input is a program?

3 3 Program correctness How do we determine whether or not a program P we have written is correct? –Testing Run program P on some inputs x 1, x 2, … What are some weaknesses to this method? What might be a better approach?

4 4 Halting Problem (H) definition Input –Program P –Input string x for program P Yes/No Question –Does P halt when run on x? Question –How does H compare to other problems about program behavior?

5 5 Example Input Program P –Variables Integer array A[10] Integer max,i; –Instructions read input string into array A; max = A[0]; for (i=1;i<10;i++) IF A[i] > max THEN max = A[i]; return max; Input x –10;5;700;3;-10;13;75;82;91;100

6 6 H is not solvable Assume H is solvable –Let P H be the program which decides H [P H used as a subroutine] Let L be a listing of all programs –L exists because the set of programs is countably infinite –Furthermore, we can specify what L is this time Construct a program D which is not on list L –Use P H to do so [Subroutine theme] Contradiction –This implies H is not recursive –This means program P H does not exist.

7 7 Representing L P0P0 P1P1 P2P2 P3P3 P4P4... 010001... HHHHH NHHHH HH H H H HH #Rows is countably infinite  * is countably infinite #Cols is countably infinite  * is countably infinite Consider each string to be a feature –A program halts or doesn’t halt on each string –Several programs may be identical now (e.g. several always halt)

8 8 Specifying D P0P0 P1P1 P2P2 P3P3 P4P4... 010001... HHHHH NHHHH HH H H H HH We specify D by using a unique feature (string) to differentiate D from P i NH H H H D

9 9 Specification Insufficient Task not complete –The previous slide only provides a specification for program D That is, it describes how program D should behave –We still need to construct an actual program D –Previous number of languages proof The specification of language D was sufficient We were done at this point

10 10 Constructing D Program D with input y Figure out which program P i should be different from D with respect to input y Run P H on P i and y [subroutine] Guaranteed to halt since P H decides H IF (P H (P i,y) = yes) THEN loop ELSE halt P0P0 P1P1 P2P2 PiPi... 01... HH HH NH H D...y HNH

11 11 Constructing D continued How do we compute P i from y? –Expand the rows to include all of  * including nonprograms –Now P i is simply y assuming y is a program –If y is not a program, it doesn’t matter what D does on input y 0 1 y... 01... HH HH NH H D...y Not a program P0P0... PiPi

12 12 Example Input Again Input y –Integer array A[10] Integer max,i; read input string... Program P –Variables Integer array A[10] Integer max,i; –Instructions read input string into array A; max = A[0]; for (i=1;i<10;i++) IF A[i] > max THEN max = A[i]; return max; Key Point: –string y used to differentiate D from program P

13 13 Program D with input string y “Double” y to produce string y:y Run P H on y:y [Subroutine Theme] If P H (y:y) = yes THEN loop ELSE halt –If y denotes a program, D is different than that program –If y is an illegal string, D can do anything –Conclusion: D is different from all programs Constructing D continued

14 14 Alternate Proof We think of y as a string which encodes program P y The string we use to distinguish program P y from D is the encoding of P y, namely y Using this idea, we can arrive at a contradiction without explicitly using the table L –The diagonalization is hidden

15 15 H is not solvable, proof II Assume H is solvable –Let P H be the program which decides H [Subroutine Theme] Use P H to construct a program D which cannot exist Contradiction –This means program P H cannot exist. –This implies H is not recursive D is the same as before

16 16 Arguing D cannot exist If D is a program, its encoding must be some string y What does D do on its own encoding y? 2 cases –D halts on its own encoding y This means P H (D:y) = NO –Definition of D This means D does not halt on y –P H solves H Contradiction This case is not possible

17 17 Continued –D does not halt on its own encoding y This means P H (D:y) = YES –Definition of D This means D halts on y –P H 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

18 18 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 We shall see next time that this also forces us to consider a new language class

19 19 Summary Halting Problem definition –Basic problem about program behavior Halting Problem is unsolvable (not in REC) –We have identified a specific unsolvable problem –Diagonalization technique Proof more complicated because we actually need to construct D, not just give a specification


Download ppt "1 Lecture 7 Topics –Problems about program behavior At least problem input is a program/algorithm –Halting Problem Fundamental problem about program behavior."

Similar presentations


Ads by Google