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

survey of computer science
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Kevin Walsh kwalsh@holycross.edu

Survey of Computer Science

Computability & The Halting Problem

slide-2
SLIDE 2

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

slide-3
SLIDE 3

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

  • rder of cities will give the shortest trip?

A B C D E F 1 2 30 5 3 7 8 2 10 2 1 4

slide-4
SLIDE 4

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

slide-5
SLIDE 5

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).

slide-6
SLIDE 6

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

slide-7
SLIDE 7

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 ?

slide-8
SLIDE 8

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.

slide-9
SLIDE 9

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!")

slide-10
SLIDE 10

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!")

slide-11
SLIDE 11

11

The Halting Problem

Solving the halting problem means writing a (correct) body for this function: def halts(script, x): # check script then return # either "safe" or "danger" ...

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.

slide-12
SLIDE 12

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)

slide-13
SLIDE 13

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!)
slide-14
SLIDE 14

14

Examples

More examples: does halts return "safe" or "danger"?

while (True) { print x x = x - 1 } while (True) { print 'La' } while (x > 0) { print x x = x - 1 }

slide-15
SLIDE 15

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"

slide-16
SLIDE 16

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 ...

slide-17
SLIDE 17

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

slide-18
SLIDE 18

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 }

slide-19
SLIDE 19

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 }

slide-20
SLIDE 20

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!")

slide-21
SLIDE 21

21

Problem Statement

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

slide-22
SLIDE 22

22

Problem Statement

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

slide-23
SLIDE 23

23

Problem Statement

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

slide-24
SLIDE 24

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

slide-25
SLIDE 25

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.

slide-26
SLIDE 26

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.

slide-27
SLIDE 27

27

The Halting Problem

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. 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.

slide-28
SLIDE 28

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".

slide-29
SLIDE 29

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."