Class 27: Modeling Computation CS200: Computer Science University of Virginia Computer Science David Evans http://www.cs.virginia.edu/evans Halting Problem Define a procedure halts? that takes a procedure and an input.
Download ReportTranscript Class 27: Modeling Computation CS200: Computer Science University of Virginia Computer Science David Evans http://www.cs.virginia.edu/evans Halting Problem Define a procedure halts? that takes a procedure and an input.
Class 27: Modeling Computation CS200: Computer Science University of Virginia Computer Science David Evans http://www.cs.virginia.edu/evans Halting Problem Define a procedure halts? that takes a procedure and an input evaluates to #t if the procedure would terminate on that input, and to #f if would not terminate. (define (halts? procedure input) … ) 26 March 2004 CS 200 Spring 2004 2 Informal Proof (define (contradict-halts x) (if (halts? contradict-halts null) (loop-forever) #t)) If contradict-halts halts, the if test is true and it evaluates to (loop-forever) - it doesn’t halt! If contradict-halts doesn’t halt, the if test if false, and it evaluates to #t. It halts! 26 March 2004 CS 200 Spring 2004 3 Proof by Contradiction 1. Show X is nonsensical. 2. Show that if you have A and B you can make X. 3. Show that you can make A. 4. Therefore, B must not exist. X = contradict-halts A = a Scheme interpreter that follows the evaluation rules B = halts? 26 March 2004 CS 200 Spring 2004 4 Virus Detection Problem Problem 7. Melissa Problem Input: A Word macro (like a program, but embedded in an email message) Output: true if the macro will forward the message to people in your address book; false otherwise. How can we show it is undecidable? 26 March 2004 CS 200 Spring 2004 5 Proof by Contradiction 1. Show X is nonsensical. 2. Show that if you have A and B you can make X. 3. Show that you can make A. 4. Therefore, B must not exist. X = halts? A = a Scheme interpreter that follows the evaluation rules B = is-virus? 26 March 2004 CS 200 Spring 2004 6 Undecidability Proof Suppose we could define is-virus? that decides the Melissa problem. Then: (define (halts? P input) (if (is-virus? ‘(begin (P input) virus-code)) it is a virus, we know virus-code was #t Since evaluated, and P must halt (assuming P wasn’t a virus). #f)) 26 March 2004 Its not a virus, so the virus-code never executed. Hence, P must not halt. CS 200 Spring 2004 7 Undecidability Proof Suppose we could define is-virus? that decides the Melissa problem. Then: (define (halts? P input) (is-virus? ‘(begin ((vaccinate P) input) virus-code)) Where (vaccinate P) evaluates to P with all mail commands replaced with print commands (to make sure (is-virus? P input) is false. 26 March 2004 CS 200 Spring 2004 8 Proof • • • • If we had is-virus? we could define halts? We know halts? is undecidable Hence, we can’t have is-virus? Thus, we know is-virus? is undecidable 26 March 2004 CS 200 Spring 2004 9 How convincing is our Halting Problem proof? (define (contradict-halts x) (if (halts? contradict-halts null) (loop-forever) #t)) If contradict-halts halts, the if test is true and it evaluates to (loop-forever) - it doesn’t halt! If contradict-halts doesn’t halt, the if test if false, and it evaluates to #t. It halts! This “proof” assumes Scheme exists and is consistent! 26 March 2004 CS 200 Spring 2004 10 Modeling Computation • For a more convincing proof, we need a more precise (but simple) model of what a computer can do • Another reason we need a model: Does (n) really make sense without this? 26 March 2004 CS 200 Spring 2004 11 How should we model a Computer? Colossus (1944) Cray-1 (1976) Apollo Guidance Computer (1969) 26 March 2004 CS 200 Spring 2004 IBM 5100 (1975) 12 Modeling Computers • Input – Without it, we can’t describe a problem • Output – Without it, we can’t get an answer • Processing – Need some way of getting from the input to the output • Memory – Need to keep track of what we are doing 26 March 2004 CS 200 Spring 2004 13 Modeling Input Punch Cards Altair BASIC Paper Tape, 1976 26 March 2004 Engelbart’s mouse and keypad CS 200 Spring 2004 14 Simplest Input • Non-interactive: like punch cards and paper tape • One-dimensional: just a single tape of values, pointer to one square on tape 0 0 1 1 0 0 1 0 0 0 How long should the tape be? Infinitely long! We are modeling a computer, not building one. Our model should not have silly practical limitations (like a real computer does). 26 March 2004 CS 200 Spring 2004 15 Modeling Output • Blinking lights are cool, but hard to model • Output is what is written on the tape at the end of a computation Connection Machine CM-5, 1993 26 March 2004 CS 200 Spring 2004 16 Charge • PS6 due Monday • Friday: – Finite state machines with infinite tape 26 March 2004 CS 200 Spring 2004 17