Lab 10 Solutions
Transcription
Lab 10 Solutions
CS 235 Spring 2015 Lab 10 Key Rhys April 5, 2015 Introduction Decidability, huh? My partner Daphne Duck and I have decided we quite like this lab. 1 Exercise 1 (Compare to Sipser Exercise 4.8 (3rd) / 4.7 (2nd)). Convince me you can write a program that will print out the integer coordinates of every point in the all-nonnegative octant of 3-D space. It should print out every triple of the form (i,j,k) where each of i, j, k ≥ 0. One way you can convince me that you can write the program is, of course, to go ahead and write it in Scheme. (define points (lambda (n) ;; return a list of all triples (i j k) with 0 <= i,j,k <= n (reverse (map reverse (prod (upto n) (prod (upto n) (map list (upto n)))))))) (define upto (lambda (n) (cond ((< n 0) ’()) (else (cons n (upto (- n 1))))))) (define prod (lambda (x y) (cond ((null? x) ’()) (else (append (map (lambda (n) (cons (car x) n)) y) (prod (cdr x) y)))))) 1 (points n) will print out all the triples from (0 0 0) to (n n n). Just iterate from n=1 to infinity! 2 Exercise 2 The number of Scheme programs is countable. Prove it! One way to do this is to write (or convince me that it is possible to write) a program that will print out all possible Scheme programs. It’s ok if, in addition to proper Scheme programs, you also print out a lot of junk. Proof: Just as (points n) produced all the triples over the set 0..n I can write a program to generate all the strings over the alphabet of our keyboard of length up to n. This will include all the Scheme programs of length up to n. Now iterating for n from 1 to infinity, I would print out all the Scheme programs. If I filter the outputs through a Scheme compiler, I can even avoid printing garbage. All you will see is legitimate compilable Scheme programs. 3 Uncountable It turns out that the set of real numbers is uncountable. Read Sipser to see a diagonalization argument to prove this. If you’re not completely comfortable with the proof, ask me in class to do my version. 4 Exercise 3 The set of functions that map the natural numbers to boolean values is uncountable. To prove this, you will need to assume (to get a contradiction) that the set is countable. In this case you will have an ordering f1 , f2 , f3 ... of all the functions. Consider the boolean value fi (j) produced by the ith function when the number j is input. Now you can define a boolean function on the natural numbers that is not listed in your ordering f1 , f2 , f3 .... Please work on this with your partner. When you think you have it (or if you are completely lost) ask me or Olivia or Smaranda to come talk to you. This is a standard Diagonalization proof. We just need to check that everybody can write it convincingly. If we define g : N → {true, f alse} by g(i) = ¬fi (i) we see that g 6= fi for any i because g(i) 6= fi (i). So the boolean function g is not in the listing of – supposedly – all the functions. We must conclude that the number of functions is uncountable. 5 Exercise 4 (Problem 5.13 in Sipser). A state q in a Turing machine M is useless if it can never be entered no matter what the starting tape of M. HAS_USELESS_STATE 2 is the language of TM descriptors that describe TMs with at least one useless state. Prove that HAS_USELESS_STATE is undecidable. Suppose, to get a contradiction, we have HAS_USELESS_STATE-DEC. Given an instance M, w of the halting problem, let us build a TM that has a useless state precisely when M halts on w. (define impossible (lambda (M w) ; an instance of the halting problem (let ((M’ (lambda (x) simulate M running on w ; We can ensure that our simulation TM has no useless states if it halts, then M’ will enter a special state qSpecial and accept))) ; Notice that the state qSpecial of M’ can only be entered if M halts on w ; Otherwise, qSpecial is a state that M’ can never enter ; The rest of our machine M’ has no useless states (not (HAS_USELESS_STATE-DEC M’))))) Notice that impossible is a decider for the halting problem. Since it can not exist, we conclude that neither can HAS_USELESS_STATE-DEC. 6 Exercise 5 Consider the language LP5 consisting of those strings that encode Turing Machines that accept the five binary strings 10, 11, 101, 111, 1011. (If you’re interested, these are the first five prime numbers, expressed in binary – but that really has nothing to do with what I’m asking you to prove.) Prove that LP5 is Turing recognizable but not Turing decidable. To show that LP5 is Turing recognizable: (define LP5-recognizer (lambda (M) (return (and (M 10) (M 11) (M 101) (M 111) (M 1011))))) ; LP5-recognizer accepts M if and only if M accepts each of the first 5 primes To show that LP5 is not Turing decidable, suppose we have LP5-DEC. (define HALT-DEC ; which of course is impossible! (lambda (M w) (let ((M’ (lambda (x) simulate M on w 3 accept))) ; Notice that L(M’) is either empty or the whole of Sigma* ; depending on whether or not M halts on w. ; In particular, M’ accepts the first five primes iff M halts on w (LP5-DEC M’)))) would be a decider for the halting problem. Thus LP5-DEC does not exist. 7 Exercise 6 Suppose L1 , L2 and L3 are such that • each pair Li , Lj are disjoint (that is, have empty intersection), and • the union of all three languages is the set of all strings over Sigma, and • each of L1 , L2 , L3 is Turing recognizable Prove that each of L1 , L2 , L3 is Turing decidable. For definiteness let’s prove L1 is decidable (the others are similar). Because L1, L2 and L3 are Turing recognizable, we have recognizers L1-RECOG, L2-RECOG, L3-RECOG. boolean L1-DECIDER (String w) { for (int n=1; ; n++) { run L1-RECOG on w for n steps; if it accepts return true run L2-RECOG on w for n steps; if it accepts return false run L3-RECOG on w for n steps; if it accepts return false } } Since the union of the languages is the set of all strings over Sigma, we can conclude that for any w at least one of the recognizers will halt accepting in a finite number, say N, of steps. We can see that L1-DECIDER will halt when the value of n reaches N. Since L1, L2 and L3 are disjoint, we can see that L1-DECIDER will return the correct answer. We have built a decider for L1, proving that L1 is Turing-decidable. 4