Great Ideas of CS with Java Part 1 WWW & Computer programming in the language Java Ch 1: The World Wide Web Ch 2: Watch out: Here comes Java Ch 3: Numerical computation & Function Ch 4: Subroutines & Databases Ch 5: Graphics Ch 6: Simulation Ch 7: Software engineering Part 2 Understanding what a computer is and how it works Ch 8: Machine architecture Ch 9: Language translation Ch 10: Virtual Environment for Computing Ch 11: Security, Privacy, and Wishful thinking Ch 12: Computer Communication Part 3 Advanced topics Ch 13: Program Execution Time Ch 14: Parallel Computation Ch 15: Noncomputability Ch 16: Artificial intelligence 1 SNU OOPSLA Lab. Ch 15. Noncomputability Copyright © SNU OOPSLA Lab. SNU OOPSLA Lab. Table of Contents Noncomputable Functions Programs that Read Programs Solving the Halting Problem Examples of non-computable problems Proof of noncomputability Summary 3 SNU OOPSLA Lab. Noncomputable Functions Certain Problems Not Amenable to Computer Solution Intuitively speaking Examples given here may seem strained and artificial However, computers have very real limitation A class of problems that are unsolvable by any computer within the current paradigm of modern programming A function is computable if a Java program exists that can compute it We want to prove the existence of noncomputable functions 4 SNU OOPSLA Lab. Existence of Noncomputable Functions (1/5) Approach: Matching up Java Programs and Functions E.g., assume 4 functions (f1, f2, f3, f4) and only 3 java programs (p1, p2, p3) Without details, conclude that one function has no program Figure 15.1 5 SNU OOPSLA Lab. Existence of Noncomputable Functions (4/5) Try to Draw Lines Between Functions and Programs Could draw lines from every program to every function (problem) 6 SNU Figure 15.2 OOPSLA Lab. Existence of Noncomputable Functions (2/5) Consider all the Java programs that input an integer and return an integer But as shown in chapter 3, the set of functions that accept integers as input and yield positive numbers as output is uncountable The set of such Java programs is a countable set See page 426 of textbook See page 125 of textbook Therefore Some functions (problems) cannot have the corresponding Java programs! SNU 7 OOPSLA Lab. Table of Contents Noncomputable Functions Programs that Read Programs Solving the Halting Problem Examples of non-computable problems Proof of noncomputability Summary 8 SNU OOPSLA Lab. Programs that Read Programs (1/3) Express programs as a single string We can easily write program to see if there is an IF statement in the program. How about, does the program halt? Figure 15.3 Program that input other programs 9 SNU OOPSLA Lab. Programs that Read Programs (2/3) “no while statement in its input program” halt, otherwise “Not known whether it halts” Figure 15.4 String E (String P) { if (p.indexOf(“while”) >= 0) {return “Not known whether it halts.”;} else {return “Halts on all inputs.”;} } 10 SNU OOPSLA Lab. Programs that Read Programs (3/3) However, what we want is the following function! Figure 15.5 We want F such that can halt after a finite time with the correct answer 11 SNU OOPSLA Lab. Table of Contents Noncomputable Functions Programs that Read Programs Solving the Halting Problem Examples of non-computable problems Proof of noncomputability Summary 12 SNU OOPSLA Lab. Solving the Halting Problem (1) Does it halt for all input? DoesItHalt.java, DoesItHalt.html // input an integer value for k while (k > 1) if ((k/2) * 2 == k) // is k even? k = k / 2; else k = 3 * k + 1; 13 SNU OOPSLA Lab. public boolean action(Event e, Object arg) { int yea; String mak, sty, col, own; import awb.*; import java.awt.*; if (e.target == gStart) { j = 0; mResults.setText("Trace of the integer value: \n"); k = gStart.getInt(); while (k > 1) { j = j + 1; if ((k/2) * 2 == k) // is k even? { k = k / 2; } else { k = 3 * k + 1; } public class DoesItHalt extends java.applet.Applet { TextField mInstruct; IntField gStart; TextArea mResults; int j, k; public void init() { mInstruct = new TextField(60); mInstruct.setText("Enter starting integer:"); gStart = new IntField(10); mResults = new TextArea(10, 50); } add(mInstruct); add(gStart); add(mResults); mResults.appendText("k = " + k + "\n"); } mResults.appendText(j + " steps before halting.\n"); return true; } } } return false; DoesItHalt.java 14 SNU OOPSLA Lab. Solving the Halting Problem (2) Try It! e.g. 17: 52 26 13, 40 20 10 5, 16 8 4 2 1 No one knows whether this quits for all inputs. Mathematicians have proven that no one, finite program can check this property for all possible programs 15 SNU OOPSLA Lab. Table of Contents Noncomputable Functions Programs that Read Programs Solving the Halting Problem Examples of non-computable problems Proof of noncompuatbility Summary 16 SNU OOPSLA Lab. Examples of non-computable problems (1/2) Equivalence: Define by same input > same output Use variation of above program; not sure it ends Cannot generally prove equivalence Figure 15.6 17 SNU OOPSLA Lab. Examples of non-computable problems (2/2) Figure 15.7 To check for property X in the behavior of all other programs is impossible 18 SNU OOPSLA Lab. Table of Contents Noncomputable Functions Existence of Noncomputable Functions Programs that Read Programs Solving the Halting Problem Examples of non-computable problems Proof of noncompuatbility Summary 19 SNU OOPSLA Lab. Proof (1/4) Proofs by Contradiction (Indirect Proof) Proving non-computability Sketch of proof Find more details in book 20 SNU OOPSLA Lab. Proof (2/4) Assume existence of function halt: String halt(string p, string x); Inputs: p = program, x = input data Returns: If halt "Halts“ or Unless halt "Does not halt" Now write: String selfhalt(string p) { String answer; answer = halt (p, p); if (answer.equals(“Halts.”)) {return (“Halts on self.”);} else {return (“Does not halt on self.”);} } 21 SNU OOPSLA Lab. Proof (3/4) String contrary (String p) { String answer; answer = selfhalt(p); if (answer.equals(“Halts on self.”)) {while (true) // go into an infinite loop {answer = “x”} } return “Halt.” // program halts; } Now run contrary on itself 22 SNU OOPSLA Lab. Proof (4/4) We are running contrary on itself. Paradox! Therefore the halt program cannot exist! If the halt program decides that it halts, it goes into infinite loop and goes on forever. If the halt program decides that it doesn't halt, it quits immediately. If the halting problem is computable, then the halt program exist. Since the halt program cannot exist, the halting problem is not computable In general, whole classes of problems on deciding program behavior are non-computable. 23 SNU OOPSLA Lab. What Does It All Mean? Programs cannot do everything 24 SNU OOPSLA Lab. Table of Contents Noncomputable Functions Solving the Halting Problem Examples of non-computable problems Proof of noncomputability Summary 25 SNU OOPSLA Lab. Summary Limit of contemporary programming languages based on the Von Neumann Computer Architecture There are uncomputable problems We proved that the halting problem is uncompuatble Also the cousins of the halting problem (regarding the behavioral property of programs) are all uncomputable 26 SNU OOPSLA Lab. 27 Ch15: Noncomputability Text Review Time SNU OOPSLA Lab.