survey of computer science
play

Survey of Computer Science Computability & The Halting Problem - PowerPoint PPT Presentation

Survey of Computer Science Computability & The Halting Problem Kevin Walsh kwalsh@holycross.edu Example List all possible orderings (permutations) of n letters Example: n = 3: A, B, C ABC, ACB, etc. 6 permutations n n! permutations


  1. Survey of Computer Science Computability & The Halting Problem Kevin Walsh kwalsh@holycross.edu

  2. Example List all possible orderings (permutations) of n letters Example: n = 3: A, B, C ABC, ACB, etc. 6 permutations n  n! permutations This is intractable 2

  3. The Traveling Salesman problem Traveling Salesman Problem A salesperson wants to visit n cities, beginning and ending with the home city. She wants to visit each city (other than home) only once. What order of cities will give the shortest trip? F 2 E 1 30 3 7 A 1 D 4 10 2 5 8 2 B C 3

  4. Solving Traveling Salesman problem Naïve approach: compute distance for every possible path: ABCDEFA  cost is 48 ACBDEFA  cost is 47 ADBCEFA  cost is infinite (no link between A and D) ... How many possibilities are there? Largest instance so far: 85,900 cities, solved in 2005 4

  5. Alternative solutions for intractable problems Many intractable problems involve finding the best solution among many possibilities.  But if we are willing to settle for good enough? A greedy approach to traveling salesman: 1) Find the shortest path to any unvisited city. 2) Repeat until all cities are visited. Running time of this algorithm? Usually, this gives a good solution (a short path), but not necessarily the best solution (shortest path). 5

  6. Recap The story so far: Tractable problem = there exist programs that solve the problem with execution time polynomial in n Intractable problem = any program that solves the problem has worse-than-polynomial execution time 6

  7. Limits of Computation Limitations of computation: 1) We know how to solve it... but execution time is too high. (Try an approximation instead...) 2) We don't know how to solve it (yet)  Security, Computer Vision, Artificial Intelligence, ... (Keep trying...) 3) The problem can't be solved ? 7

  8. Recall Church-Markov-Turing Recall the Church-Markov-Turing Thesis: Any non-trivial computer language is apparently capable of computing no more and no fewer functions than all other non-trivial programming languages. So, all languages are equally powerful. Python is at least as powerful as any other programming language.  Therefore any solvable problem is solvable in Python.  And a problem not solvable in Python isn't solvable in any other language either. 8

  9. Problem Statement Recall dangers of client-side scripting: Browser should only run a script if it doesn't contain an infinite loop. if doesntContainInfiniteLoop(script): execute(script) else: popup("This script is dangerous!") 9

  10. Problem Statement Recall dangers of client-side scripting: Browser should only run a script if it doesn't contain an infinite loop. ans = halts(script, x) if ans == "safe": execute(script, x) else: popup("This script is dangerous!") 10

  11. The Halting Problem The Halting Problem: Write an infinite-loop- detector program, which reads in another program and outputs a message saying whether that other program halts on all inputs. Solving the halting problem means writing a (correct) body for this function: def halts(script, x): # check script then return # either "safe" or "danger" ... 11

  12. A program to read other programs Goal is to write a function halts(script, x) that... • takes two parameters, script , a string, and x , any value • returns "safe" if script would, when executed with input x , eventually halt • returns "danger" if script would loop forever when executed with input x Examples: halts("if (x == "Yes") { print 'Oui' } else { print 'Non' }","No") halts("if (x < 0) { return 5*x; } else { return x/2; }", -3) halts("while (True) { print 'La'; print x; }", 42) 12

  13. A simple case Writing the halts function, first attempt: def halts(script, x): if "while" not in script: else: Assumptions for today: • Javascript doesn't have "for loops" (but it does!) • Javascript can't do recursion (but it can!) 13

  14. Examples More examples: does halts return "safe" or "danger"? while (True) { while (x > 0) { print 'La' print x } x = x - 1 } while (True) { print x x = x - 1 } 14

  15. A simple case Writing the halts function, second attempt: def halts(script, x): if not "while" in script: return "safe" elif script.startswith("while(True)"): return "danger" else: return "unknown" 15

  16. A simple case Writing the halts function, general pattern to check all possible cases... def halts(script, x): if not "while" in script: return "safe" elif script.startswith("while(True)"): return "danger" elif another condition : return some answer elif yet another condition : return answer for that case ... 16

  17. Some cases to check Some other cases to check... "while ( 1 > 0 ) ..." Condition has the form "1 > 0"? Or better yet... Does the condition always evaluate to True? "while ( x > 0 ) { ... x = x - 1 ... }" Does the while condition eventually become False?  Condition has the form "x > 0" ... ... and the body contains "x = x -1" ... and the body doesn't otherwise change x 17

  18. Example: Does this halt? Lots of other cases (nested loops, etc.), and simple pattern matching won't get us very far. y = 2 * x while (y > x) { x = x + 2 y = y + 1 } 18

  19. Another Example: Does this loop always halt? while (x > 1) { if (x % 2 == 0) # x is even? x = x / 2 else # x is odd? x = 3 * x + 1 } 19

  20. Problem Statement Assume we are given a halts function that works. What can we do with it? def executeIfSafe(script, x): ans = halts(script, x) if ans == "safe": execute(script, x) else: popup("Nope! Too dangerous!") 20

  21. Problem Statement Assume we are given a halts function that works. What can we do with it? def haltsOnZero(script): 21

  22. Problem Statement Assume we are given a halts function that works. What can we do with it? def haltsOnSelf(script): 22

  23. Problem Statement What does this function do? def contrary(script): ans = haltsOnSelf(script) if ans == "danger": print "done" else: while true: print "La" 23

  24. contrary function The contrary function: 1) If the input is dangerous to execute on itself, contrary prints "Done" then stops 2) If the input is safe to execute on itself, contrary prints "La La La ..." forever What happens if we run contrary on itself ? contrary( contrary ) # contrary with itself as input 24

  25. A contradiction What happens if we run this program? contrary( contrary ) # contrary with itself as input Line 1: ans = haltsOnSelf(contrary) Will haltsOnSelf(contrary) return"safe" or "danger"? I don't know... If haltsOnSelf(contrary) returns "safe", what happens? Then contrary( contrary ) loops forever If haltsOnSelf(contrary) returns "danger", what happens? Then contrary( contrary ) prints "Done" then stops  Either way, this is a contradiction. 25

  26. What went wrong? Recap of what we did: 1) Assumed there exists a halts( ) function that works. 2) We wrote a haltsOnSelf( ) function. 3) We wrote a contrary( ) function. 4) We derived a logical contradiction. Our assumption leads to a logical contradiction. Conclusion: Our assumption is wrong. 26

  27. The Halting Problem The Halting Problem: Write an infinite-loop- detector program, which reads in another program and outputs a message saying whether that other program halts on all inputs. This function can't exist.  The halting problem is non-computable: no program can check, in a finite time, whether some other program will halt. Proof: by contradiction. 27

  28. Other non-computable problems Surprisingly, almost every problem related to program behavior is non-computable. 1) Showing that two programs are equivalent (for each input you always get the same output). 2) Determining whether a given output will occur for some input. 3) Determining whether a program is "correct", for some definition of "correct". 28

  29. TL;DR "If a legit psychic predicted that you would drown in a pool tomorrow, you would not go anywhere near one tomorrow, thus proving the psychic to be a charlatan. Programs can behave the same way too." 29

Recommend


More recommend