Presentation is loading. Please wait.

Presentation is loading. Please wait.

Impossible problems.

Similar presentations


Presentation on theme: "Impossible problems."— Presentation transcript:

1 Impossible problems

2 Semidecidable problems
Question: Given this input data, will Program P ever halt, or will it run forever? Solution: Try running it If it halts, we know the answer If it hasn’t halted yet, we don’t know the answer How long do we have to wait? This problem is semidecidable—if it halts, we know the answer, but if it doesn’t, we don’t Can we do better? Program P Data for Program P

3 The Halting Problem Let’s write a program HaltChecker that accepts two inputs: A program, and The data for that program and determines whether the program (with that data) will halt This program will have one of two outputs: halts, and loops Program P Data for Program P HaltChecker halts loops (forever) HaltChecker reads another program as one of its inputs This is not unusual--compilers do it all the time

4 A modified HaltChecker
We’ll make a slight change to HaltChecker--we will replace the halts output with an infinite loop Program P Data for Program P HaltChecker2 halts loops (forever) We call this new program HaltChecker2 HaltChecker2 behaves like this: If Program P would halt on its input data, HaltChecker2 goes into an infinite loop If Program P would go into an infinite loop on its data, HaltChecker2 halts (and reports its result) Thus, HaltChecker2 does the opposite of what Program P does

5 A problem HaltChecker2 has as input both a program and the input to that program What happens if we apply HaltChecker2 to itself (with itself as input)? HaltChecker2 halts loops (forever) A little thought (or maybe a lot of thought) should convince you that HaltChecker2 halts when applied to itself if and only if it doesn’t halt when applied to itself This is a contradiction Therefore, a program such as HaltChecker2 cannot exist However, we can easily modify HaltChecker to get HaltChecker2 Therefore, HaltChecker cannot exist

6 What have we proved? Clearly, for most programs we can determine whether or not they will halt We can write a HaltChecker-type program that can determine, for most programs, whether or not they will halt What we have proved is that we cannot write such a program that always works, for every program, for every input More generally, we have proved that some problems cannot be solved There are only a few such problems known (the Halting Problem is the most famous), but they are important

7 Consequences Can we prove that a certain statement in a program will never be executed? Replace the statement with an infinite loop The problem is now a Halting Problem Conclusion: You can’t always prove that a certain statement will or will not be executed But as usual, you can sometimes prove that it will or will not be executed Familiar consequence: Java warns you if you use a variable before it has been initialized Its algorithm for doing this is not perfect Could the algorithm be made 100% effective?

8 Why you should care Someday you may be asked to solve a problem that is equivalent to the Halting Problem It’s not too likely, but... ...it has happened to me If it happens, you can write a program that mostly works, but you cannot write one that always works Before you spend a lot of company money on the project, you need to educate your manager about what can be expected--or you’ll get blamed when it doesn’t work In my case, my manager decided the task wasn’t worth the effort :)

9 The End


Download ppt "Impossible problems."

Similar presentations


Ads by Google