p x i 1 i n x, y, z = 2 x 3 y 5 z

Similar documents
Recursively Enumerable Languages, Turing Machines, and Decidability

In this section we will study problems for which we can prove that there is no algorithm solving them.

COMPUTABILITY THEORY AND RECURSIVELY ENUMERABLE SETS

Lecture 5: The Halting Problem. Michael Beeson

Diagonalization. The cardinality of a finite set is easy to grasp: {1,3,4} = 3. But what about infinite sets?

14.1 Encoding for different models of computation

introduction to Programming in C Department of Computer Science and Engineering Lecture No. #40 Recursion Linear Recursion

To prove something about all Boolean expressions, we will need the following induction principle: Axiom 7.1 (Induction over Boolean expressions):

CS 125 Section #4 RAMs and TMs 9/27/16

English for Computer Science

Material from Recitation 1

1 Achieving IND-CPA security

CSCI 270: Introduction to Algorithms and Theory of Computing Fall 2017 Prof: Leonard Adleman Scribe: Joseph Bebel

Grade 6 Math Circles November 6 & Relations, Functions, and Morphisms

Lecture 7: Primitive Recursion is Turing Computable. Michael Beeson

CS 275 Automata and Formal Language Theory. First Problem of URMs. (a) Definition of the Turing Machine. III.3 (a) Definition of the Turing Machine

Lecture 1 Contracts. 1 A Mysterious Program : Principles of Imperative Computation (Spring 2018) Frank Pfenning

The Undecidable and the Unprovable

Lecture 3: Recursion; Structural Induction

Limitations of Algorithmic Solvability In this Chapter we investigate the power of algorithms to solve problems Some can be solved algorithmically and

An Interesting Way to Combine Numbers

University of Nevada, Las Vegas Computer Science 456/656 Fall 2016

Lecture 1 Contracts : Principles of Imperative Computation (Fall 2018) Frank Pfenning

1 Definition of Reduction

TAFL 1 (ECS-403) Unit- V. 5.1 Turing Machine. 5.2 TM as computer of Integer Function

Functional Programming in Haskell Prof. Madhavan Mukund and S. P. Suresh Chennai Mathematical Institute

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

Chapter 12. Computability Mechanizing Reasoning

Chapter 3. Set Theory. 3.1 What is a Set?

CS103 Spring 2018 Mathematical Vocabulary

Lecture 4: examples of topological spaces, coarser and finer topologies, bases and closed sets

(Refer Slide Time: 0:19)

Introduction to Computers & Programming

AXIOMS FOR THE INTEGERS

6.001 Notes: Section 6.1

6.001 Notes: Section 8.1

1 Counting triangles and cliques

Computable Sets. KR Chowdhary Professor & Head.

Lambda Calculus and Computation

Kuratowski Notes , Fall 2005, Prof. Peter Shor Revised Fall 2007

MITOCW watch?v=kz7jjltq9r4

Reducibilities relations with applications to symbolic dynamics

Problem One: A Quick Algebra Review

Week - 01 Lecture - 03 Euclid's Algorithm for gcd. Let us continue with our running example of gcd to explore more issues involved with program.

Typing Control. Chapter Conditionals

Propositional Logic. Part I

CS2 Algorithms and Data Structures Note 10. Depth-First Search and Topological Sorting

MITOCW watch?v=4dj1oguwtem

This is already grossly inconvenient in present formalisms. Why do we want to make this convenient? GENERAL GOALS

Does this program exist? Limits of computability. Does this program exist? A helper function. For example, if the string program is

05. Turing Machines and Spacetime. I. Turing Machines and Classical Computability.

Divisibility Rules and Their Explanations

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #29 Arrays in C

Lecture 6: Arithmetic and Threshold Circuits

To prove something about all Boolean expressions, we will need the following induction principle: Axiom 7.1 (Induction over Boolean expressions):

CS 531: Notes II. January 31, 2014

(Refer Slide Time: 02.06)

ISA 562: Information Security, Theory and Practice. Lecture 1

Proofwriting Checklist

Iteration. # a and b are now equal # a and b are no longer equal Multiple assignment

Math 126 Number Theory

Denotational semantics

Lecture Transcript While and Do While Statements in C++

Introduction. chapter Functions

Lecture 1: Overview

Greedy Algorithms 1. For large values of d, brute force search is not feasible because there are 2 d

Scribe: Virginia Williams, Sam Kim (2016), Mary Wootters (2017) Date: May 22, 2017

Computational Complexity and Implications for Security DRAFT Notes on Infeasible Computation for MA/CS 109 Leo Reyzin with the help of Nick Benes

Computer Architecture Prof. Mainak Chaudhuri Department of Computer Science & Engineering Indian Institute of Technology, Kanpur

CS21 Decidability and Tractability

Greedy Algorithms 1 {K(S) K(S) C} For large values of d, brute force search is not feasible because there are 2 d {1,..., d}.

6.080 / Great Ideas in Theoretical Computer Science Spring 2008

6.001 Notes: Section 31.1

1. [5 points each] True or False. If the question is currently open, write O or Open.

Introduction to Denotational Semantics. Class Likes/Dislikes Survey. Dueling Semantics. Denotational Semantics Learning Goals. You re On Jeopardy!

6.001 Notes: Section 15.1

Combinatorics Prof. Dr. L. Sunil Chandran Department of Computer Science and Automation Indian Institute of Science, Bangalore

Treewidth and graph minors

6.001 Notes: Section 4.1

6. Advanced Topics in Computability

γ(ɛ) (a, b) (a, d) (d, a) (a, b) (c, d) (d, d) (e, e) (e, a) (e, e) (a) Draw a picture of G.

Lecture 19. Lecturer: Aleksander Mądry Scribes: Chidambaram Annamalai and Carsten Moldenhauer

CS 3512, Spring Instructor: Doug Dunham. Textbook: James L. Hein, Discrete Structures, Logic, and Computability, 3rd Ed. Jones and Barlett, 2010

Cantor s Diagonal Argument for Different Levels of Infinity

Starting Boolean Algebra

9. MATHEMATICIANS ARE FOND OF COLLECTIONS

Week - 04 Lecture - 01 Merge Sort. (Refer Slide Time: 00:02)

Comparing sizes of sets

Notes on Turing s Theorem and Computability

Algorithms (III) Yu Yu. Shanghai Jiaotong University

Reflection in the Chomsky Hierarchy

Lecture Notes on Contracts

Binary, Hexadecimal and Octal number system

CSCI 3130: Formal Languages and Automata Theory Lecture 16 The Chinese University of Hong Kong, Fall 2010

MATHEMATICS 191, FALL 2004 MATHEMATICAL PROBABILITY Outline #1 (Countability and Uncountability)

The Turing Machine. Unsolvable Problems. Undecidability. The Church-Turing Thesis (1936) Decision Problem. Decision Problems

Algorithms (III) Yijia Chen Shanghai Jiaotong University

Recitation 4: Elimination algorithm, reconstituted graph, triangulation

From the λ-calculus to Functional Programming Drew McDermott Posted

We show that the composite function h, h(x) = g(f(x)) is a reduction h: A m C.

Transcription:

3 Pairing and encoding functions Our aim in this part of the course is to show that register machines can compute everything that can be computed, and to show that there are things that can t be computed. To do this, we will need to express everything including register machines themselves as numbers, so that we can compute with everything. We will use the term encoding function to refer to a function that takes an object of some kind and represents it as a natural number. First, we ll consider functions for encoding pairs, triples, etc. of numbers into a single number. Definition 5 A pairing function is an injective function N N N. Given a pairing function f, write x, y 2 for f(x, y). If z = x, y 2, let z 0 = x and z 1 = y. An easy example is (x, y) 2 x 3 y. Note that this function is easy to invert: if I give you z = 2 x 3 y, you can just repeatedly divide by two and count to get x, and then repeatedly by three and count to get y. We will use this shortly. We can generalize to functions that encode n-tuples of integers, which we write... n : N n N. One way to do this is to generalize the above example: x 1,..., x n n = 1 i n p x i i where p i is the i th prime. We ll use that below. Another way uses repeated pairing. Even more generally, we can write functions... : N N to encode an arbitrary length sequence of integers into an integer, as we ll see below. 4 Three into two does go We ll now show that we can easily encode a three-register machine program into a tworegister machine program, given the three instructions inc, decjz, goto. For this, we will use the obvious tripling function: x, y, z = 2 x 3 y 5 z The reason for this choice is that it is particularly easy to manipulate the three components by manipulating the code directly. The target machine has three registers, which we call T 0, T 1, T 2 ; our interpreting machine has two, R 0, R 1. The principle of the encoding is that R 0 always contains T 0, T 1, T 2, while R 1 is our work register. Each instruction of the target machine is translated to a macro of the interpreting machine; we will now define these macros. First, consider the instruction inc T 0. Incrementing T 0 corresponds to multiplying T 0, T 1, T 2 by 2: 4

# r1 is assumed zero # move double r0 into r1 start: decjz r0 next inc r1 inc r1 goto start # move r1 to r0 next: decjz r1 end goto next # note this leaves r1 zero Equally easily, we can translate inc T 1 and inc T 2. The decrement instructions are a little trickier to encode decjz T 0, dest, we need to divide T 0, T 1, T 2 by 2, but back out and jump if it turns out not to be a multiple of 2. Note, by the way, that R 0 can never contain zero on entry, as this is not a valid triple code. # r1 assumed zero # by repeated subtraction, move half r0 into r1 # if we hit zero at this point, that s good - # it means we ve halved r0 start: decjz r0 next # if we hit zero here, r0 wasn t a multiple # of two, so we need to recover decjz r0 not2 inc r1 goto start # move r1 back to r0 next: decjz r1 end goto next # recovery: need to restore r0 # first undo the dec at start before we jumped here not2: # now add double r1 on to r0; # when finished, jump to branch target loop: decjz r1 dest goto loop # note that either way, r1 is zero when we # leave this macro Similarly, though rather more tediously, particularly with the recovery, we can write macros to decrement T 1 and T 2. The target goto is translated directly. Modulo correctness of the above macros, we have now encoded a 3-register machine into a 2-register machine. It is obvious that for any given value of n, this encoding can be extended to encode 5

n-register machines into 2-register machines, and so in some sense we re done: we ve shown two registers are enough to encode any given machine, and so any RM-computable function can be simulated by a 2-register RM. It is an interesting quirk that while a 2-register RM can simulate a general RM, and can therefore compute a suitable encoding of any computing function, it is not true that a 2-register RM can compute any computable function, in the sense of taking the input in R 0 and leaving the answer in R 0. Such simple functions as n 2, 2 n cannot be computed (in this sense) by a 2-register machine. 5 Simulating unbounded register numbers However, it would be more satisfactory if we had a single encoding that would translate machines with arbitrary numbers of registers into 2-register machines. This is rather harder, because it means we have to treat the target register index (i in T i ) as a variable quantity, instead of hard-wiring each different instance. Moreover, using the arbitrary extension of the prime-powers pairing function would involve us generating the ith prime on the fly every time we touched T i. While this is possible, it s pretty hideous (and certainly not obviously doable with two registers). So in order to sketch the proof, I ll assume some pairing function, 2 : N N N\{0} (note that 2 x 3 y is such a function), and then define the following coding of sequences: = 0 x, s = x, s 2 where s is a sequence Hence, extracting the i element of a coded sequence is simply a matter of pulling out the first component of a pair, i times. Assuming the unpairing function is reasonable, this can be done with a couple of auxiliary registers. Similarly, updating the ith element is routine, though a little more tedious. Try writing the pseudo-code for it, and see how many auxiliary registers you need. Given that, then the rest of the translation process is very simple: if interpreting machine R 0 holds the coded registers, then target machine instruction inc(i) turns into load R 1 with i extract T i into R 2, saving T 0,..., T i 1 in R 3 and leaving T i+1,... in R 0 inc (R 2 ) pack T 0,..., T i 1, R 2, T i+1,... into R 0 The macros are a bit fiddly, especially the re-packing (if you are a Lisp programmer, you may remember basic exercises that involved a lot of list reversing), but not difficult. Similarly for decjz and goto. While I haven t counted, I d guess that ten registers are enough to do all that conveniently possibly fewer. But we wanted just two registers. No problem! If we can produce an arbitary-register-rm interpreter using ten registers, we can encode it into a two-register machine using the technique of the previous section. Note, however, that if we do all this with the primes coding function, the machine has zilch chance of doing anything useful in the lifetime of the universe. What is the code of the sequence (2, 2, 2, 2, 2)? For the purpose of this section, any coding funtion will do, so we can code more efficiently using the diagonal pairing function (or to be precise, 1 + the diagonal pairing function). 6

For the construction of section 4, I don t know any tupling function that can be calculated with only one scratch register, and is not exponential in the inputs. I also don t know that no such function exists. I believe that the goto is necessary, in that without it we cannot write a general RM simulator using just two registers. So far, the proof escapes me. 6 Universal machines In the previous sections, we showed that a particular three-register or n-register machine could be simulated by a particular two-register machine. Now we will show (without details) that we can build a single machine that can simulate any register machine. To do this, we need to encode RMs into numbers. Following a common convention in logic, we shall use to denote a coding function into the integers. It is important to remember that... is just an integer, whatever the... are. The coding function is as on the slides: Definition 6 Let M be a Register Machine, and let R be the contents of registers R 0,..., R m 1, P = I 0... I n 1 be the program itself, and let C be the program counter, as in definition 2. Let be a sequence coding function. We define the RM coding function... by: inc(i) = 0, i decjz(i, j) = 1, i, j P = I 0,..., I n 1 R = R 0,..., R m 1 M = P, R, C We now build a universal machine in a very similar way to the previous simulation of n-register machines by 2-register machines. The main difference is that instead of hardwiring the translation of each program instruction, we extract the code of the instruction from the coded program, and translate it in software by examining the code. As remarked on the slides, this is not hard, but it is a substantial programming task. If you want to see a fairly detailed description of the construction, my colleague Philippa Gardner at Imperial College did a lecture about it. There are some small differences in notation, and her gadgets are my macros, but it should be easy to follow. Her lecture is here: https://www.doc.ic.ac.uk/~pg/computation/lecture6.pdf If we do this work, we obtain Theorem 7 There is a Register Machine U which takes as input the code M of the initial state of an arbitrary RM M, and gives as output the code of the final state, if there is one. If M does not halt, U does not halt on input M. 7

7 The halting theorem The main result of this part of the course is the Theorem 8 There is no Register Machine H such that H takes as input an RM state encoding M and halts with output 1 if M halts or output 0 if M does not halt. We expand a little on the proof from the slides. The proof runs by contradiction: we suppose that such an H does exist, and show that this cannot be, by building another machine from H which says both yes and no to a particular question. So, let H be an RM (P H, R 0,... ) which takes a machine coding M in R 0, and terminates with 1 in R 0 if M halts, and 0 in R 0 if M runs forever. We now construct a machine L = (P L, R 0,... ), which takes a program code P and terminates with 1 if H returns 0 on input (P, R 0 = P ), and or goes into an infinite loop if H returns 1 on (P, R 0 = P ). To construct L, we just have to write code that, given P in R 0, computes (P, R 0 = P ), which by definition 6 is P, P, copies that into R 0, and transfers control to the code of the machine H. Then the halting state of H (which, recall, is the empty instruction at the end) is replaced by code which changes R 0 (the output of H) to 1 if it is 0, and goes into a tight loop if it is 1. (You may find it helpful to draw a diagram for this construction.) Thus, L takes a program, and runs the halting test on the program with itself as input this is called (diagonalization and loops iff the program halts, thereby reversing the halting behaviour of the program: L halts iff its input loops. The combination of diagonalization (or self-reference) and negation is the core of most theorems of this kind. Now consider the result of running L with input P L. If L halts on P L, that means that H says that (P L, P L ) loops; and if L loops on P L, that means that H says that (P L, P L ) halts. So either way, contradiction. Thus we conclude no such machine as H can exist, and the theorem is proved. 8 Turing Machines The Halting Theorem was originally proved by Turing for his model of computation, now called Turing Machines. In this course, we are using Register Machines, as a more intuitive model of computation. Therefore I shall not provide here any further technical details of Turing Machines. The resources listed on the course webpage provide details if you want them. It is not hard that is, it is only a matter of writing large programs to show that TMs and RMs are equivalent, in that we can write an RM program to simulate TMs, and a TM program to simulate RMs. In fact, all plausible models of computation are equivalent in this sense. This is the Church Turing Thesis. It s not a theorem, because plausible model of computation is not a formal notion, but nobody has ever come up with a counter-example. There are models of computation which can compute more than TMs can (and in particular can solve the Halting Problem of TMs), but they involve rather implausible tricks such as orbiting a suitably rotating black hole. See the Wikipedia article on Hypercomputation for a summary. 8

9 Generalizing computability We have now produced something that (we claim) is a universal model of computation, and also shown that there are things it cannot compute. Several questions now arise. Is the Halting Problem the only uncomputable problem? Are there different levels of uncomputability? Suppose we re given a real problem (not one about RMs!) how can we tell whether it is computable or not? If we have a real problem, then one way to show that it is computable is to write a program that solves it, and then prove (using suitable mathematical techniques specific to the problem, such as induction) that indeed the program always terminates. Another way is to translate it into another problem we already know about. For example, any problem about Turing Machines can be turned into a problem about Register Machines, so we don t need to think independently about TM problems. How do we show that a real problem is uncomputable? It s not usually obvious how to carry out a diagonalization and negation argument on a real problem. However, it is often not too hard to see how to encode the Halting Problem (about RMs) into a particular example of our real problem. If we can do that, then we know the real problem is uncomputable in general because if we could always compute the answer, then we could compute the answer to the encode Halting Problem. To study these ideas, we need to introduce some more formal apparatus. So far, we have thought about the computability of a problem with a yes/no answer. Because we want to think about translating or encoding, it will be useful to think more generally about computable functions. Ultimately, we are always talking about integers, because that s what RMs compute with, but since most mathematical or engineering concepts can easily be coded up as integers, we shall quite freely talk about computable functions on, for example, finite graphs, or rational numbers, or finite trees, and assume that the work of encoding and decoding happens behind the scenes. For example, many problems concern finite graphs. A question we shall see later is does G have a k-clique? that is, are there k vertices of G such that every one is connected to every other? In modern programming languages, we would define a suitable datatype for graphs. In RM programming, we have to code graphs as numbers. An obvious (but not necessarily the best) way to code a graph is as G = (V = {v 1,..., v n }, E = {e 1,..., e m }) n, src(e 1 ), dst(e 1 ) 2,... src(e m ), dst(e m ) 2 2 where src, dst indicate the source and target vertices of an edge. Definition 9 A (total) function f : N N is computable if there is an RM/TM which computes f (e.g. by taking x in R 0 and leaving f(x) in R 0 ) and always terminates. Historically, computable functions were often called recursive functions. This comes from another approach to computability theory in which we look at the ways of defining mathematical functions. With the advent of computers, programming languages, and the widespread use of recursion in programming, the term has become unhelpful: for example you can perfectly well write both computable and uncomputable functions in a language without recursion, and if you write a recursive (in the programming sense) function, it may or may not be computable. Most people now use computable, but when reading older literature, be aware that the term is used. 9

A problem is just a special case of a function, as it can be viewed as a function where the result is either 0 for no or 1 for yes. Set membership is, of course, a predicate. We ll flip freely between notions. We ll have a formal definition for this: Definition 10 A decision problem is a set D (the domain) and a subset Q (the query) of D. The problem is is d Q?, or is Q(d) 0 or 1?. The problem is computable or decidable iff the predicate Q is computable. Some example decision problems are: the domain N and the subset Primes of prime numbers; the domain N N and the set { (p, p + 2) : p and p + 2 are both prime }; the domain RM of register machine (encodings), and the subset H that halt; the domain of ASCII strings and the subset that are English sentences. 10 Oracles Sometimes it is interesting (or technically useful) to be able to think about the question what else could we compute if we could solve the Halting Problem?, or more generally, given some hard problem Q, what could we compute if we could solve Q?. Oracles are a device to let us reason about such questions. An oracle is imagined to be a black box that we can plug into our RM, which gives the answer to the hard problem Q in a single computation step. Definition 11 Given a decision problem (D, Q), an oracle for Q is an additional RM instruction oracle Q (i) which assumes that R i contains (an encoding of) some d D, and sets R i to contain Q(d). Note that if Q is itself decidable, then oracle Q (i) can be replaced by a call to a sub- RM which computes Q thus a decidable oracle adds nothing. However, in the second part of the course, when we consider computing with restricted resources, we shall again use oracles for problems with more resource than normally allowed, and in that context oracles for decidable problems make sense. 10