5 Enumerability and Diagonalization

5.1 Introduction 5.2 Equinumerous Sets 5.3 Countable and Uncountable Sets 5.4 Enumerating Programs 5.5 Abstract Families of Functions 5.6 Exercises and Supplements 5.7 Bibliographical Comments

5.1 Introduction

One of the purposes of this chapter is to present a formal approach to the intuitive notion of the size of a set. The facts discussed here play a fundamental role in computer science; they serve to make precise the idea that there are more functions on the set of natural numbers than programs (in any programming language), and therefore, there exist functions that cannot be computed by any program in that programming language. A focal point of this chapter is a proof method originating in : the diagonalization method. Developments in theoretical computer science have made diagonalization an essential instrument for proving negative and limitative results in this field. We discuss diagonalization both in its original set-theoretical context and in the context of computability theory.

5.2 Equinumerous Sets

It follows from Corollary 4.2.7 that there is a f : {ao, ... ,am-I} --+ {bo, ... , bp-tl if and only if m = p, that is, if and only if the two sets involved have the same number of elements. In this section, our main objective is to extend this idea to arbitrary sets. Definition 5.2.1 Two sets M and P are called equinumerous, denoted by M", P, if there exists a bijection f : M --+ P.

It is easy to verify the following properties for any sets M, P, and Q:

339

P. A. Fejer et al., Mathematical Foundations of Computer Science © Springer-Verlag New York, Inc. 1991 340 5. Enumerability and Diagonalization

1. M,....M; 2. if M,.... P, then p,.... M; and 3. if M ,.... P and p,.... Q, then M ,.... Q. The first property follows immediately from the fact that the mapping 1M : M --+ M is a bijection. Further , if I : M --+ P is a bijection, then 1- 1 : P --+ M is also a bijection; therefore, if M ,.... P then, P ,.... M. Finally, if I : M --+ P and 9 : P --+ Q are two , then gl : M --+ Q is also a bijection. Example 5.2.2 Consider the set E of all even natural numbers. We have E C N. Nevertheless, we also have N ,.... E because of the fact that the mapping I : N --+ E defined by I(n) = 2n for every nE N is a bijection. Example 5.2.3 The set Z of is equinumerous with N. Consider the mapping 9 : N --+ Z defined by

{ n/2 if n is even g(n) = -(n + 1)/2 if n is odd.

It is not difficult to see that 9 is a bijection, which proves that Z ,.... N. Example 5.2.4 Any two open intervals of the set of real numbers are equinumerous. Indeed, consider two intervals (a, b) and (c, d), where a < b and e< d. Define g: (a,b) --+ (e,d) by

d - e be - ad g(x) -b-x b . = -a + -a It is easy to see that 9 is a bijection between (a,b) and (c,d). Of course, this is true even if (a, b) ~ (c, d) since we did not use the relative positions of the two intervals in our argument. Furthermore, we can prove that R,.... (a, b) for any open interval (a, b). It suffices to consider the mapping tan : (-t, t) --+ R, which is a bijection, to obtain (-t, t) ,.... R. On the other hand, any open interval (a,b) is equinumerous with (-t, t), which gives R,.... (a, b). The following theorem gives some evidence that two equinumerous sets do in fact have the same size. Theorem 5.2.5 Let M and P be sets such that M,.... P. Then, either M and P are both finite or they are both infinite.

Proof: If one of M and Pisfinite and M ,.... P, then it follows from Theorem 2.4.10 that the other of these two sets is also finite. I In our next definition, we try to make precise the ideas of one set being at least as large as or strictly larger than another. 5.2. Equinumerous Sets 341

Definition 5.2.6 Let M and P be two sets. M is dominated by P, written M ~ P, if there exists an injection f : M --+ P. M is strictly dominated by P, written M ~ P, if M ~ P and M is not equinumerous with P.

Example 5.2.7 For any finite set M, we have M ~ N since if M has n elements, M = {xQ, ... , xn-d, the mapping f : M --+ N defined by f(Xä) = i for 0 ~ i ~ n - 1 is injective.

Theorem 5.2.8 For every M, P, Q, R, Ml , ... , Mb Pl , ••• , Pk , we have

1. M ~ M;

2. if M ~ P and P ~ Q, then M ~ Q;

3. if M ~ P, then M ~ P;

4. if M '" P, then M ~ P;

6. if M ~ P, Q'" M, and R", P, then Q ~ R. Proof. The argument consists of direct applications of the definition of ~, and it is left to the reader. I The connection between finiteness and domination Is given in the next theorem.

Theorem 5.2.9 Let M and P be sets such that M ~ P. Then, if P is finite, so is M, and if M is infinite, so is P.

Proof: Since M ~ P, we have M '" K for some subset K of P. If P is finite, then by Theorem 4.2.11 so is K, but then by Theorem 5.2.5, M is finite. The second part of the theorem follows immediately from the first. I The next theorem shows the connection between and domi• nation for finite sets.

Theorem 5.2.10 Let M and P be finite sets. Then, M ~ P if and only if IMI ~ IPI and M ~ P if and only if IMI < IPI. Proof: The theorem follows immediately from Corollary 4.2.9. I Theorem 5.2.8 implies that if M '" P then M ~ P and P ~ M. By the previous theorem, the converse of this statement is true if M and P are finite sets. We now show that the converse is true in general. This supports our intuition that M ~ P me ans that P is at least as big as M and M '" P means that M and P have the same size.

Theorem 5.2.11 {Schröder-Bernstein Theorem} For all sets M and P, if M ~ P and P ~ M, then M '" P. 342 5. Enumerability and Diagonalization

Prool. According to the hypothesis of the theorem, we have two injective mappings f: M --+ P and g: P --+ M. Define ~ : 1'(M) --+ 1'(M) by ~(K) = M -g(P- f(K)). This mapping is monotonie since K 1 ~ K 2 implies f(Kt} ~ f(K2 ) (because of Theo• rem 2.5.6); hence, P - f(K2) ~ P - f(Kd, which gives g(P - f(K2)) ~ g(P- f(Kd). This, in turn, implies M -g(P- f(K1 )) ~ M -g(P- f(K2 )); hence, ~(Kt) ~ ~(K2). Let Ko be a subset of M such that ~(Ko) = Ko. Such a subset exists because of Theorem 3.9.10. For this set, we have

K o = ~(Ko) = M - g(P - f(Ko)). (5.1)

Define now a mapping h : M --+ P by

h(a) = { f(a) if a E Ko g-l(a) if a E M - K o.

Since 9 is an injection, g-l is a function and if a E M - Ko, then a E g(P - f(Ko)), because of (5.1), so g-l(a) is defined. Thus, h is well• defined. The mapping h is onto. In order to justify this claim, let b E P. If bE f(Ko), then there is a E Ko such that b = f(a). In this case, we have h(a) = f(a) = b. If b f/. f(Ko), then bE P - f(Ko) implies a = g(b) f/. Ko by (5.1), so b = h(a) and this proves that every element of P is the image (by h) of an element of M. The mapping h is injective. Suppose that h(a) = h(a') = b. If both a, a' E Ko, then we have f(a) = b = f(a') so a = a', because of the fact that fis injective. Ifneither a nor a' are in K o, then we have a = g(b) = a'. Finally, suppose that a E Ko and a' f/. Ko. Then, b = h(a) = f(a), and a' = g(h(a')) = g(b), so a' = g(f(a)) E g(f(Ko)). Together with a' E M - Ko = g(P - f(Ko)), this contradicts the injectivity of g. Thus, h is a bijection and M "" P. I

Theorem 5.2.12 If M, P, and Q are sets such that either M --( P:5 Q or M :5 P --( Q, then M --( Q. Prool: Suppose that M --( P :5 Q. Then, M :5 P :5 Q, so by Theo• rem 5.2.8, M :5 Q. Since P :5 Q, there must be an injection 9 : P --+ Q. If M "" Q, say, f : Q --+ M is a bijection, then fg is an injection from P to M, so P :5 M. In combination with M :5 P, we get from Theorem 5.2.11 that M "" P, contradicting M --( P. Thus, we do not have M "" Q, and hence, M --( Q. If M :5 P --( Q, then a similar proof shows that M --( Q. I It is interesting to note that, while there is always an injective mapping W : M --+ 1'( M) given by W(a) = {a}, there is no such thing as a bijection between a set M and its power set 1'(M). 5.2. Equinumerous Sets 343

Theorem 5.2.13 For every set M, M -< P(M).

Proof. Suppose that I: M --+ P(M) is a surjection. Define K = {a E MI a f/. I(a)}. K is a subset of M, and therefore, there exists ao E M such that I(ao) = K, since 1 is onto. We have either ao E K or ao f/. K. In the first case, ao E K implies (by the definition of K) that ao f/. I(ao) = K, which is a contradiction. In the second case, ao f/. K = f(ao) amounts to ao E K, which is, again, a contradiction, so 1 cannot exist. Since there is no surjection from M to P(M), we cannot have M "" P(M). By the remark just prior to the theorem, we do have M :; P(M). Thus, M -

It is easy to show, by induction on n, that for any n E N, h(n) is finite, and therefore, M - h(n) is nonempty for every n E N. We also note that if m < n then h(m) C h(n). Define 9 : N --+ M by g(n) = I(M - h(n)). Clearly, h(n + 1) = h(n) U {g(n)}, g(n) f/. h(n), and g(n) E h(p) for every p > n. In order to prove the theorem, we need to show that 9 is injective. Con• sider two numbers m, n E N, with m < n. Then, g(m) E h(n), while g(n) f/. h(n). This implies that g(m) i= g(n).1 Definition 5.2.15 A set M is countably infinite if M "" N. Examples 5.2.2 and 5.2.3 show that E and Z are both countably infinite. We have seen that N is an infinite set. Thus, by Theorem 5.2.5 a set that is countably infinite is, in fact, infinite. If M is countably infinite and M "" P, then it is immediate that P is also countably infinite. The following theorem could be obtained as a corollary of Theorem 5.2.14, but we give a slightly Ion ger proof in order to avoid use of the axiom of choice.

Theorem 5.2.16 If M is an infinite subset of N, then M is countably infinite. 344 5. Enumerability and Diagonalization

Proof. Sinee M ~ N, we ean define a selective function f for M with• out using the axiom of ehoiee by letting f(K) be the least element of K for every nonempty subset K of M. Continuing now, as in the proof of Theorem 5.2.14, we obtain N ~ M. Sinee M ~ N, We also have M ~ N, and Theorem 5.2.11 implies that M-N.I Theorem 5.2.17 If M is a set such that M --< N, then M is finite.

Proof. If M --< N, then M - K for some subset K ofN. If K is infinite, then by the previous theorem, K - N, so M - N, eontradicting M --< N. Thus, K is finite; henee, M is finite. I In Examples 5.2.2 and 5.2.3, we have shown that an infinite set ean be equinumerous with one of its proper subsets. This turns to be a eharacter• istie property of infinite sets. Theorem 5.2.18 A set is infinite if and only if it is equinumerous with one of its proper subsets.

Proof. Let M be an infinite set. By Theorem 5.2.14, N ~ M; henee, We have an injection f : N --+ M. Consider the function 9 : M --+ M defined by

if x ft. f(N) g(x) = { ;(n + 1) if x = f(n) for nE N.

Note that 9 is well-defined because of the fact that f is injective. The range of 9 is M - {f(0)}, and 9 is clearly injective, which makes it a bijection between M and M - {f(0)}. Conversely, if M is finite, then by Corollary 4.2.10, any injection from M to itself is a surjection, so there eannot be a bijeetion between M and one of its proper subsets. I

5.3 Countable and Uncountable Sets

The property of sets we are about to study plays an essential role in com• putability.

Definition 5.3.1 A set M is eountable if it is finite or countably infinite. A set that is not countable is said to be uncountable.

It follows immediately from this definition that the countably infinite sets are, indeed, the eountable sets whieh are infinite. We have already remarked that Z and E, the set of even integers, are countably infinite and henee, these are examples of eountable sets, as of course is N itself. On the other hand, by Theorem 5.2.13, N --< P(N) 5.3. Countable and Uncountable Sets 345 which implies that P(N) is neither finite nor countably infinite and hence, is uncountable. Further examples will be discussed later in this section, but first, we give some characterizations of countable sets.

Theorem 5.3.2 For every set M, M is countable il and only il M ~ N.

Proof. Let M be a . If M is finite, then by Example 5.2.7 M ~ N. If M is countably infinite, we have M '" N, so M :j N, as we observed before. Conversely, assurne that M :j N. Then, either M --< N, or M '" N. In the former case, M is finite by Theorem 5.2.17, while in the latter case, M is countably infinite by definition, so in either case M is countable .•

Theorem 5.3.3 11 M is a nonempty set, we have M :j N il and only il there exists a surjection I : N -+ M. Proof. Let M be a nonempty set such that M :j N. There exists a injective mapping 9 : M -+ N. Since M f= 0, Theorem 2.3.11 implies the existence of a left inverse I : N -+ M for g, which is necessarily a surjection (since 9 is a right inverse for f). Conversely, let I : N -+ M be a surjection. Theorem 2.3.11 implies the existence of a right inverse 9 : M -+ N for I, but this part of that theorem uses the axiom of choice. We can define a right inverse 9 for I explicitly and avoid the use of the axiom of choice by defining, for every a E M, g( a) to be the least element of 1-1 ( {a}). Then, 9 has I as left inverse, so is an injection, and M ~ N .• The previous theorem says that the countable sets are exactly those sets which can be enumerated as the range of a (not necessarily injective) infinite , together with 0. When we use a phrase such as "Let M = {80, 81, ... } be a nonempty countable set," we are implicitly using this fact to write an arbitrary nonempty countable set as the range of an infinite sequence. Of course, since M could be finite, we cannot assurne that the sequence is injective. However, when we use a phrase such as "Let M = {SO,Slo .•• } be a countably infinite set," we will assurne, unless the contrary is stated, that s is an injective sequence (just as when we write "Let M = {so, ... , sn-tl be a finite set," we implicitly assurne that M has n elements and hence, that the sequence s lists the elements of M without repetition) .

Corollary 5.3.4 Let M be a nonempty set. The lollowing three statements are equivalent:

1. M is countable;

2. M:j N;

3. there is a surjection I : N -+ M. 346 5. Enumerability and Diagonalization

Proof. The corollary follows immediately from Theorems 5.3.2 and 5.3.3. I Corollary 5.3.5 Let M be a countable set, and let P be any set. 11 any 01 the lollowing conditions ia met, then P ia countable.

1. There is an injection I: P --+ M (i.e., P ~ M).

2. There is a surjection 9 : M --+ P.

3. M '" P. Proof. Suppose that the first condition holds. Since M is countable, we have by Theorem 5.3.2 that M ~ N and hence, by Part 2 ofTheorem 5.2.8, P ~ N, so P is countable. Now, suppose that 9 : M --+ P is a surjection. If P is empty, then P is certainly countable. If P :I 0, then the existence of the surjection shows that M is not empty and hence, by Theorem 5.3.3, there is a surjection h : N --+ M. The composition gh : N --+ P is also a surjection and hence, P is countable. If the third condition holds, then so do both of the first two, so P is countable. I Definition 5.3.6 Let M be a set. A numbering lor M is an injection g: M --+ N. An enumeration 01 Mis a surjection I: N --+ M. Note that by Theorem 5.3.2, a set M is countable if and only if M has a numbering, and by definition M is countably infinite if and only if M has a bijective numbering. Also, by Theorem 5.3.3, a nonempty set is countable if and only if it has an enumeration. The proof of Theorem 5.3.3 gives an explicit method for converting an enumeration 1 : N --+ M into a numbering 9 : M --+ N of M; namely, for each a E M, g(a) is the least n such that I(n) = a. The resulting numbering will not be a bijection unless 1 was a bijection, and in this case 9 is r 1 . Sometimes one wants to carry the process out in reverse, Le., one has a numbering 9 : M --+ N of a nonempty set M and wants to produce an enumeration 1 : N --+ M of M. If 9 is bijective, then we can take 1 = g-1. Otherwise, the method from the proof of the second part of Theorem 2.3.11 can be used. An element mo of M is chosen. (Usually mo is in some sense one of the simplest elements of M.) Then, 1 is defined by letting I(n) = g-1(n) for nE g(M) and I(n) = mo for those n E N which are not in the range of g. If M is a countably infinite set then, in many situations, it is advantagous to have a bijective numbering of M instead of just an ordinary one. If 9 : M --+ N is a numbering of an infinite set M, then g(M) is an infinite subset of N and the proof of Theorem 5.2.16 provides a method to define 5.3. Countable and Uncountable Sets 347 an injection I: N --+ g(M); namely, 1(0) is the smallest element of g(M), 1(1) the second smallest element of g(M), and so on. (In other words, I enumerates the elements of g(M) in increasing order.) It is easy to see that I is in fact a bijection, and hence, we obtain a new bijective numbering g' for M by defining g' = I-lg. Explicitly, g'(a) = n if g(a) is the n + 1st largest element of g(M). This provides an automatie process for converting a numbering of an infinite set into a bijective one, but even if the original numbering is a natural one, the resulting bijective numbering will in general be very unnatural. We turn now to the quest ion of what operations can be performed on countable sets so that the resulting set is countable. Our first observation is quite easy to prove, yet yields many results. Theorem 5.3.7 Seq(N) is countable. Proof. Let (Po, PI, ... ) be the sequence of prime natural numbers listed in increasing order. Define a function 9 : Seq(N) --+ N by

_ ao+l a .. _l +1 9 « ao,··.,an-l )) -Po "'Pn-l for each sequence (ao, ... ,an-I) in Seq(N). (As usual, the empty product is 1.) If a and bare two different elements of Seq(N), then either there is an i with ai i= bi or else the two have different lengths. In either case, g(a) and g(b) will be numbers with different prime factorizations and hence, by the uniqueness of the prime factorization for each positive natural number, g(a) i= g(b). Thus, 9 is an injection, and hence, by Theorem 5.3.2, Seq(N) is countable. I Note that the numbering of Seq(N) defined in the previous theorem is not a bijection. Corollary 5.3.8 Let M be a set. 1. 11 M is countable, then Seq(M) is countable. 2. 11 M is countable, then P fin(M), the collection 01 all finite subsets 01 M, is countable. 3. I/ n E N and (Mo, ... , Mn-I) is a sequence 0/ countable sets, then the Cartesian product Mo X ... X M n - 1 is countable. 4. I/ M is countable and nE N, then Mn is countable. Proof. Let M be a countable set. Then, by Theorem 5.3.2 there is an injection 9 : M --+ N. To see (1), observe that in an obvious way, ginduces an injection I : Seq(M) --+ Seq(N) (namely, I«ao, ... ,an-t}) = (g(ao), ... ,g(an-t}) for each sequence (ao, ... , an-I) in Seq(M)). Thus, by Theorem 5.3.7 and Corollary 5.3.5, Seq(M) is countable. 348 5. Enumerability and Diagonalization

To show (2), we define an injection h : 'Pfin(M) ---+ Seq(M) as folIows. For each finite subset K of M, let h(K) be the sequence of length 1/(1 obtained by listing the elements of K in increasing order according to their numbering under g. Since we have just seen that Seq(M) is countable, it follows that 'Pfin(M) is countable. For part (3), let n E N and let (Mo, . .. ,Mn-d be a sequence of countable sets. Then, for each i, 0 ~ i ~ n - 1, Mi ~ N, so by Theorem 5.2.8, part (5), Mo x ... x Mn- l ~ Nn. Since Nn ~ Seq(N) and Seq(N) is countable, Nn is countable and hence, Mo x ... x Mn-l is countable. Part (4) follows immediately from part (3) .• In the previous corollary, we have shown that certain sets built up from countable ones are countable, but have not specified when the set con• structed is finite and when it is countably infinite. A finer analysis, which makes this distinction, can be obtained using the results in Exercise 5. Since the numbering of Seq(N) given in Theorem 5.3.7 is not bijective, none of the numberings implicit in the proof of the previous Corollary are bijective either. Example 5.3.9 We can prove now that the set Q of rational numbers is countable. If r E Q, we can determine uniquely p and q such that r = ~, where p, q are relatively prime integers and q > O. The mapping I : Q ~ z x Z given by I(r) = (p, q) is clearly an injection; hence, Q ~ Z x Z. Since Z is countable, so is Z x Z, because of Corollary 5.3.8. Consequently, Q is countable.

Our next observations are quite trivial. If C is any nonempty collection of sets, then nC is a subset of every element of C. Thus, if even one of the elements of Cis countable, nC will be countable. Similarly, M - P ~ M, so if M is countable and P is any set, M - P will be countable. A more interesting question is when unions of countable sets are count• able. We first show that the union of finitely many countable sets is count• able. Theorem 5.3.10 Let C = {Mo, ... , Mn-d be a finite collection 01 count• able sets. Then, the union UC = U{Mk 10 ~ k ~ n - I} is countable. Proof. For n = 0 or n = 1, the result is immediate. We proceed by induction on n ~ 2. For the basis step, n = 2, consider countable sets Mo and Ml . If either Mo or Ml is empty, then MoUMl is clearly countable. Therefore, we assurne that both Mo and Ml are nonempty. There are two surjections 10 : N ---+ Mo and !t : N ---+ Ml . This allows us to consider the mapping I : N ---+ Mo U Ml , defined by if n is odd if n is even. 5.3. Countable and Uncountable Sets 349

Note that Mo = I(N - E), while Mt = I(E). Therefore, Mo UMt = I(N); hence, 1 is onto, which means that Mo U Mt is countable. For the inductive step, assurne that n ~ 2 and the union of any collection of n countable sets is countable. Since

in view of the inductive hypothesis and of the basis step, we may conclude that U{Mk I 0 ~ k ~ n} is countable.1 The next theorem shows that the union of a countable collection of count• able sets is countable, which immediately implies the previous theorem. We proved the previous theorem separately because that proof did not involve the axiom of choice, while the proof of the following theorem necessarily does. Theorem 5.3.11 Let C be a countable collection 01 countable sets. The set M = UC, the union 01 all the sets in C, is countable. Proof. If C = 0 or C = {0}, the result is immediate. In any other case, we may assurne that C is a nonempty countable collection of nonempty count• able sets since, otherwise, we can replace C by C - {0} without modifying the set M. Let C = {Mo, Mt, ... , Mn, .. .}, and for each nE N, let Sn be the set of all surjective mappings from N to Mn. By Theorem 5.3.3, we have Sn :I 0 for every n E N and, by the axiom of choice, there is a choice function 1 for the collection {Snln E N}. For each n ~ 0, l~t hn = I(Sn). Define a mapping h : N x N --+ M by h(n, m) = hn(m). Clearly, h is surjective. By Corollary 5.3.5, M is countable. I We will now show how to give natural bijective numberings for some of the infinite sets which we have already shown to be countable, using numberings which are not bijections. First we look at Seq(M) where M is finite. If M = 0, then Seq(M) = {A} is finite. If M is nonempty, then it is an alphabet, and the following example gives a natural bijective numbering. Example 5.3.12 Let V = {at, ... ,ak} be an alphabet. We define a bijec• tion h : V" --+ N from V" = Seq(V), the set of all words over V, to N. If w = aioah ... ai ... _1 , where 1 ~ io,···, im - t ~ k, is a word of length m then h(w) = iokm- t + it km- 2 + ... + im-t. For instance, if V = {at, a2, a3}, then

We can think of h(w) as being the number which io ... im - t represents in base k. Note however that the "digits" i o, ... , i m - t vary between 1 and k, not 0 and k - 1 as they would in the usual base k representation. 350 5. Enumerability and Diagonalization

We leave to the reader the task of proving that h is a bijection. (See Exercise 28.) Now, we turn to bijective numberings for Seq(M), 'Pfin(M), and Mn when M is countably infinite and n > o. There is no loss in generality in taking M = N. First we consider Seq(N). We get a bijective numbering for this set by making only a small change to the numbering defined in Theorem 5.3.7.

Example 5.3.13 We define a bijection 9 : Seq(N) ---+ N. As before, let (Po, PI, ... ) be the sequence of prime positive integers listed in increasing order. Let g(,x) = 0, and define

_ 00 01 o .. _l 0 .. _1+1 1 9 « ao,···, an-l )) -Po PI ... Pn-2 Pn-l - for each nonnull sequence (ao, ... , an-d in Seq(N). It is dear that if ais a nonnull sequence, then g(a) i 0, so ,x is the only sequence mapped to 0 by g. If a = (ao, ... , an-d and b = (bo, ... , bm-d are two nonnull sequences both mapped to the same number x by g, then Pn-l is the largest prime wh ich divides x+ 1, as is Pm-I, (because of the plus one in the final exponent in the definition of g) and so n = m. Furthermore, an -l and bn - I both are one less than the highest power of Pn-l which divides x + 1, so are equal, while for 0:$ i < n - 1, ai and bi are both equal to the largest power of Pi which divides x + 1. Thus, a = band 9 is an injection. Clearly 0 is in the range of g. If x > 0 then x + 1 ~ 2, so we may write z + 1 = p~o ... P~"--l' for some n > 0 and bo, ... , bn- 1 with bn- 1 > o. Then, g«bo, ... ,bn - 2 ,bn - 1 -1» = x, so gis onto. Next we consider 'Pfin (N).

Example 5.3.14 We define a bijection 9 : 'Pfin (N) ---+ N by defining

g( {ao, ... , an-d) = 200 + 201 + ... + 20 "-1 for each finite subset {ao, ... , an - d of N, where (ao, ... , an - d is a listing of the elements of the set in increasing order. Supplement 30 shows that 9 is a bijection.

Now, we give bijective numberings for N k for k ~ 1. For k = 1, if we take our usual identification of NI with N, then 1N is the desired bijection. In the next two examples, we give the two standard bijective numberings for N x N and later we give a recursive process to obtain the remaining bijections.

Example 5.3.15 Define I : N x N ---+ N by I(m, n) = 2m (2n + 1) - 1 for every m,n E N. This mapping is injective because, if I(m,n) = I(p,q), we have 2m (2n + 1) = 2P(2q + 1). This implies immediately 2m = 2P and 2n + 1 = 2q + 1; hence, m = P and n = q. It is easy to see that I is 5.3. Countable and Uncountable Sets 351 surjective. If rEN, we can decompose r + 1 as a product of powers of prime numbers: r+ 1 = 2ml 3m2 •••• This allows us to write r = 2ml k -1, where k is an odd number. By writing k = 2h + 1, we obtain f(mi, h) = r, which shows that f is surjective. Therefore, f is a bijection. There are many possible bijections between N x N and N. A different bijection is introduced in the next example.

Example 5.3.16 Consider the function f: N x N --+- N defined by (m+n)(m+n+1) f()m,n = m+ 2 .

The motivation for this definition can be seen in Figure 5.1. Every diagonal line corresponds to a set of points

D" = {(p,q) E N x N I p+ q = k}. The set DI: contains k + 1 points. In order to find the place of a point (m, n) in the numbering suggested by Figure 5.1, observe that the diagonal Dm +n is preceded by the diagonals Do, . .. ,Dm+n - i , which contain a total of (m+n)(m+n+1) 2 points. In addition, (m, n) is the (m + 1)-st point on the diagonal D m +n , and since the numbering starts from 0, the place of (m, n) in the numbering is indeed f(m, n). The previous examples suggest the following.

Definition 5.3.17 A pairing function is a bijection f : N x N --+- N.

Ifwe have a pairing function f : N xN --+- N and f(p, q) = n, then, given n, we should be able to determine both components of the pair (p, q) that is mapped by f to n. The following definition introduces an appropriate device. Definition 5.3.18 A pairing tripie is a tripie offundions (f,Pi,p2) such that f: N x N --+- N, Pi: N --+- N, P2 : N --+- N and f(p,q) = n if and only ifpi(n) = p and P2(n) = q for every p, q, nE N. We refer to Pi, P2 as the projections of the pairing tripie. In any pairing tripie (f, Pi, P2), fis a bijection, that is, a pairing function. Indeed, since n = f(Pi(n),P2(n)) for every n E N, it follows that f is surjective. To show that f is injective, assume that f(p,q) = f(u,v) = n. We have p = Pi(n) = u and q = P2(n) = v; hence, (p, q) = (u, v), which shows that f is also one-to-one. 352 5. Enumerability and Diagonalization

FIGURE 5.1. Definition of the function f.

Conversely, Cor every pairing Cunction f, we can define Pl, P2 : N --+ N by Pl = pU- l and P2 = p~f-l so that (f,Pl,P2) is a pairing tripIe. Here, the projections p~, p~ : N X N --+ N are given by p~ (m, n) = m and p~(m,n) = n Cor every (m,n) E N x N. Example 5.3.19 Let us define the pairing tripIe involving the Cunction f introduced in Example 5.3.16. Let n E N. We need to determine initially the set DA: to which the n-th point belongs. In other words, we need to find the number k E ~ Cor which k(k + 1) < (k + 1)(k + 2) 2 _n< 2 .

The first inequality implies

k < v'f+'8ri - 1 - 2 ' while the second implies

k v'f+'8ri - 3 > 2 .

Since ..;r:+8n' - 1 _ v'f+8n" - 3 = 1 22' there is exactly one in the interval ( vT+Tn" - 3 vT+Tn" - 1] 2 ' 2 ' 5.3. Countable and Uncountable Sets 353 namely, (5.2)

It is easy to see then that

Pl(n) = n _ k(k;l), and P2(n) = k - Pl(n) = k(k;3) - n, where k has been determined from equation (5.2).

If the pairing function 1 : N X N --+ N is monotonie in both its argu• ments (with respect to the usual order), we shall refer to the pairing tripie (f,Pl,p2) as a monotonie pairing tripie. Note that, beeause ofthe faet that 1 is a bijection, 1 is actually a strietly monotonie function in eaeh variable. Example 5.3.20 The function 1 from Example 5.3.15 is monotonie in eaeh variable. Thus, the eorresponding pairing tripie (f, pl, P2) is mono• tonie. Monotonie pairing tripies have eertain useful properties mentioned in the next theorem. Theorem 5.3.21 Let (f, Pt. P2) be a monotonie pairing tripie. We have 1. 1(0,0) = 0; 2. Pl(n) $ n and P2(n) $ n lor every nE N. Proof. Sinee 1 is a bijeetion, there exists (p, q) E N x N such that I(p, q) = 0; on the other hand, the monotonicity of 1 implies 1(0,0) $ !(p, q) = 0, and therefore, we obtain 1(0,0) = 0. To prove the seeond part of the theorem, we shall prove that p $ I(p,q) for every p,q E N. For p = °this fact is clearly true. Suppose now that the statement is true for p. We have p < I(p,q) (the inductive hypothesis ), I(p,q) < l(p+1,q) (the strict monotonicity of f), p+ 1 < l(p+1,q) (from the previous two steps). A similar induetive argument shows that q $ I(p, q) for every p, q E N. I In our next example, we show how to start from an arbitrary pairing function 1 and eonstruct reeursively a family of functions {fk I k 2: I} such that eaeh fk is a bijeetion from N k to N. We shall refer to the functions {h:lk 2: I} as the lunc1ions associated with !. 354 5. Enumerability and Diagonalization

Example 5.3.22 Let I : N x N --+ N be a pairing function and define h =lN' Suppose now that k ~ 1 and we have a bijection hc : NI: --+ N. Define 1l:+1 : NI:+1 --+ N by

11:+1 (Xl, ... , XI:, XI:+t) = /I: (Xl , ... , XI:-1 '/(XI:, Xl:+t)), (5.3) for every xl,"" XI:, XI:+1 E N. (Note that h = I according to this definition.) We leave it to the reader to prove that 1l:+1 is also a bijection. A useful function wh ich can be derived from the functions 11: is given in the following Definition 5.3.23 Let I be a pairing lunction. The uniform projection function for I is the lunction 'TrI : N 3 --+ N, where

il k = 0, il II:(X1, ... , Xi, ... , XI:) = n and i < 1 ~ k, 1f'J(i,k,n) = { :: il II:(X1,'" ,Xi," .,XI:) = n and 1 ~ i ~ k, XI: il II:(X1, ... ,Xi, ... ,XI:) = n and 1 ~ k < i, lor every i,k,n E N. If I is understood from the context, we denote its uniform projection function 'TrI simply by 'Tr. Properties of uniform projection functions are further discussed in Supplements 38 and 39. We have already seen that Seq(N) is countable and have in factgiven two numberings for this set, one of which is a bijection. In the next example, we give yet another numbering for Seq(N) which we will take as our official numbering for finite sequences of integers. We use this numbering in the sequel because it is closely connected with the family of bijections {Ik I k ~ I} associated with a particular pairing function.

Example 5.3.24 Let I : N x N --+ N be the mapping introduced in Example 5.3.16 and let {JI: I k ~ I} be the family of functions associated with I. Consider the mapping gnseq : Seq(N) --+ N defined by

gnseq((no, ... , nk-t)) = I(k, Ik(no, ... , nk-d), for every k E P and (no, ... , nl:-t) E NI:; for the null sequence, we define gnseq by gnseq(A) = O. It is easy to see that gnseq is an injection. However, gnseq is not a surjection since no number 1(0, m) with m > 0 is in its range. We can define a slight variation 9 of gnseq which is a bijection by letting 9 ((no, ... , nl:_1)) be I(k - 1, Ik(nO,"" nl:-t)) + 1 if k ~ 1, and be 0 if k = O. We find it more convenient to use the slightly simpler function gnseq in the sequel, even though it is not a bijection. 5.3. Countable and Uncountable Sets 355

The number gnseq(s) will be referred to as the Gödel number1 of s. We now turn to abrief study of uncountable sets. We first give two easily derived methods for showing that sets are uncountable.

Corollary 5.3.25 If P is an uncountable set and P ~ M, then M is uncountable. Proof. This follows immediately from the first part of Corollary 5.3.5. I

Corollary 5.3.26 Let M and P be two sets such that M is uncountable and P is countable. Then, M - P is uncountable. Proof. Since M = PU(M -P), M -P must be uncountable; otherwise, M would be countable by Theorem 5.3.10. I Uncountable sets can be naturally constructed starting from countable ones. One such example, already mentioned, is the set P(N). A more gen• eral statement is given below. Theorem 5.3.27 If M is an infinite set, then P(M) is uncountable. Proof. Suppose that P(M) is countable. We have

M ~P(M) ~N because ofTheorems 5.2.13 and 5.3.2. By Theorem 5.2.12, this implies M ~ N; hence, M is finite because ofTheorem 5.2.17, which is a contradiction. I Example 5.3.28 The set oflanguages over an alphabet V, P(V*), is un• countable.

Theorem 5.3.29 Let P be a set that has more than one element. If M is an infinite set, then the set of functions M - P is uncountable. Proof. Let Po,Pt be be two fixed and otherwise arbitrary elements of P. Then, M - {Po,pd ~ M - P, so if we can show that the former set is uncountable, then it will follow from the third part of Theorem 5.2.8 and Corollary 5.3.25 that M - P is uncountable. Now, it is dear that M - {Po,pd ...... M - {O, I} and by Theorem 2.3.16 this latter set is

IGödel numberings are named after the logician Kurt Gödel in whose work they played an important part. Gödel was born on April 28, 1906 in Brno, Czechoslovakia, and died on January 14, 1978 in Princeton, New Jersey. He was associated with the University of Vienna, first as a student and then as a lecturer, from 1924 to 1939. From 1940 until his death he was affiliated with the Institute for Advanced Study in Princeton. Gödel was the most outstanding logician of the twentieth century. His work, including his results on the undecidability of formal systems, settled fundamental open questions, influenced greatly subsequent work, and has had a profound effect on our understanding of the foundations of math• ematics and the limitations of computation. 356 5. Enumerability and Diagonalization equinumerous with P(M). By Theorem 5.3.27, P(M) is uncountable, so M ---+ {po, Pl} is also uncountable. I An immediate consequence of this fact is that ISeq( {O, I}) = N ---+ {O, I}, the set of infinite sequences over {O, I}, is uncountable. We can also prove that ISeq( {O, I}) is uncountable using an instructive direct approach. Suppose that ISeq( {O, I}) is countable. In this case, we can enumerate its elements, and we have ISeq({O,l}) = {SO,Sl, ... ,Sn, ... }. Consider the following table listing the components of the infinite sequences SO,Sl,···'Sn'···· Sequence 1 .. . n ... So so(O)° so(l) .. . so(n) ... SI Sl(O) SI (1) ... Sl(n) ...... Sn Sn(O) sn(1) .. . Sn(n) ...... We now define a new sequence starting from the elements located on the diagonal of this table. Let S be given by if sk(k) = 1 s(k) = { ~ if sk(k) = 0.

Note that S is an infinite {O,l}-sequence, so sE ISeq({O,l}). On the other hand, S is different from any element of the list {so, SI, . .. , sn, ... } because of the fact that s(k) "I sk(k) for every k E N. This contradiction proves that ISeq( {O, I}) is not countable. The proof technique used here is referred to as diagona/ization and it appears frequently in mathematics and theoretical computer science. We use this method of proof even when there is no direct "graphical" connec• tion to a table. If we formulate the same argument using subsets of N, we assurne that we have a listing of all subsets of N: {[{o, [{I, ... , [{n, .. . }, and we construct a set [{ of natural numbers different from any set [{n on this list by defining [{ as

[{ = {n E NI n f/. [{n}. This is exactly the method we used in proving Theorem 5.2.13 (if we take M = N and let J : N ---+ P(N) be given by J(n) = [{n), which justifies calling the argument used in that proof diagonalization. The countability considerations developed in this section have an impor• tant consequence for computability theory. From Theorem 5.3.29, we can infer that the set N ---+ N is uncountable. On the other hand, the set P 5.4. Enumerating Programs 357 of all programs (written in some programming language L) that compute one-argument functions from N -- N is countable since any program of L is essentially a word over the basic alphabet of L. Therefore, P -< N -- N and this implies the existence in N -- N of functions that cannot be computed by any program of L.

5.4 Enumerating Programs

In this section, we present the syntax and semantics of a very simple pro• gramming language (VSPL) that will allow us to illustrate some of the mathematical techniques used in computability theory. As the reader will soon realize, VSPL is not areal programming language. It lacks construc• tions that implement data structures and input and output facilities, and its control structures are rather primitive. Yet, what makes this language interesting is precisely its simplicity, which makes it convenient to use as a mathematical tool. VSPL is built starting from the LOOP language introduced by Meyer and Ritchie [MR67b, MR67a]. Further references are [CB72, BL74]. The basic ingredients of this language are variables and instructions. We assurne that there are infinitely many variables each of which is a nonnull string of symbols over some fixed alphabet V. The set of variables is VAR. The variables of VAR are denoted by Wo, ... ,Wn , ... , where h(wo) < ... < h(wn ) < .... Here, h : V" -- N is the bijection introduced in Example 5.3.12. In addition, we use special symbols that do not belong to V: ;- + 1 0 goto loop endloop. The basic alphabet Vb is obtained by adding the special symbols to V. Variables of VSPL programs assurne their values in N. The instructions of the language are defined as folIows. 1. For every v E VAR, v ;- v + 1 is an instruction. 2. For every v E VAR, v ;- 0 is an instruction. 3. For every v E VAR, loop v is an instruction. 4. For every u E {I}", goto u is an instruction. 5. The symbol endloop is an instruction. An instruction goto u will be denoted by goto I, where I = lul; for instance, we denote goto 111 by goto 3. If an instruction contains the symbols ;- and +, it must be v ;- v + 1 with v uniquely determined (as consisting of all symbols to the left of ;-). On the other hand, if an instruction contains ;- but no +, then it must be v ;- 0 with v uniquely determined. 358 5. Enumerability and Diagonalization

Note that if an instruction contains loop or goto then it must be loop v and goto I, respectively, with v and 1 uniquely determined in each case. In any other case, the instruction is endloop. The above remarks give the unique readability property for instructions. As usual, we will denote the length of a finite sequence P of instructions by IPI. When we displaya program, we usually put one instruction on each line. Sometimes we will want to save space by putting several instructions on a single line of text. When we do this we will separate instructions with semicolons. This is just a notational device. The semicolon is not in the basic alphabet. Let P be a sequence of instructions satisfying the following conditions. Pi. P contains the same number of loop instructions as endloop in• structions. Pii. Every suffix of P contains at least as many endloop instructions as loop instructions. Note that every prefix of P contains at least as many loop instructions as endloop instructions. Consider the sets

Lp = {ilO ~ i ~ IPI- 1 and P(i) is a loop instruction} and Kp = {ilO ~ i ~ IPI- 1 and P(i) is an endloop instruction}. Because of (Pi), ILpl = IKpl. If P(i) is a loop instruction then, since the suffix of P beginning with that loop has at least as many endloops as loops, there must be a shortest segment of P beginning with P( i) that has at least as many endloops as loops. This segment will in fact have the same number of loops as endloops and will end with an endloop statement P(I). This endloop is said to be associated with the corresponding loop and we define ap : Lp ---+ Kp by ap(i) = 1 where P(I) is associated with P(i).

Theorem 5.4.1 The mapping ap : Lp ---+ Kp defined above is a bijection such that i < ap(i) and i < j < ap(i) implies ap(j) < ap(i) for every i,j E Lp. Proof. The mapping ap is injective, that is, an endloop cannot be associated with two distinct loop instructions. Indeed, ifthe same endloop corresponds to both loop Vl and loop V2, then we have the same number of loop instructions and endloop instructions in both segments SI and S2.

loop Vl; ..• ; loop v2; .•• ; endloop~ , ... # s~ 5.4. Enumerating Programs 359

This would imply that the segment S, defined by

contains the same number of loop instructions and endloop instructions, contradicting the fact that loop VI corresponds to the given endloop. This proves that op is injective. Since ILpl = I[{pl, we also have that Op is a bijection. It is immediate from the definition that i < op(i) for all i E Lp. Suppose that P(i) and P(j) are two loop instructions, i < j < op(i) = I, and op(j) = m. Assurne that I < m. If X, Y, Z are the sequences defined by P(i);~;P(j);~;P(/);~;P(m), x y z denote by Xloop, Yloop, Zloop and Xend, Yend, Zend the number of loop and endloop instructions that occur in X, Y, Z, respectively. Since ap(i) = I, Xloop + 1 > Xend and Xloop + Yloop + 2 = Xend + Yend + 1, from which it follows that Yend > Yloop' On the other hand, since op(j) = m, Yloop + 1 > Yend, so Yloop 2: Yend. This contradiction shows that op(j) < I. I Note that if Op satisfies the conditions of Theorem 5.4.1 then

i < j < op(i) implies i < j < op(j) < op(i) (5.4) for every i,j E Lp. Theorem 5.4.1 suggests the following. Definition 5.4.2 Let P be a sequence of instructions satisfging conditions (Pi) and (Pii). The body of a loop instruction that occurs in P is the sequence Z of instructions situated between the loop and its associated endloop:

loop ~endloop. Z Definition 5.4.3 A VSPL program is a sequence of instructions P satis• fging conditions (Pi)-{Piii), where (Pi), (Pii) are as defined before Theorem 5.4.1 and (Piii) is the following condition: Ifgoto I is an instruction of P, then P contains at least 1+1 instructions. In other words, a VSPL program is a sequence of instructions containing properly nested loops and endloops, where every goto instruction has a line of the program to which it can jump. The set of all programs is denoted by PROG. Now that we have given the syntax of our programming language, we give semantics for it. We will give an operational semantics for the languagej that 360 5. EnumerabiIity and Diagonalization is, we give a mathematical description of how the execution of a program is carried out in a step-by-step manner. We begin with a set of counter variables

COUNT = {cili E N}, with COUNT n V AR = 0. Each loop instruction of a program has an associated counter variable. Ifloop v is the i+ l-st instruction of a program, then the associated counter variable is Ci. We introduced the not ion of environment of a program in Exercise 30 of Chapter 2. Here, we redefine this not ion as needed for defining the semantics ofVSPL.

Definition 5.4.4 The set of states STATES is defined by

STATES= VAR -- N.

The set 01 environments is the set

ENV= (VARU COUN7) -- N.

If e is an environment and v E VAR U COUNT, then e( v) is the value of v in e.

Definition 5.4.5 The set of configurations of a program P is the set CONFIGp given by

CONFIGp = {O, ... , !PI} x ENV.

The set of terminal configurations 01 P is the set TCONFIGp given by

TCONFIGp = {IPI} x ENV.

The set of initial configurations 01 P is the set ICONFIGp, where

ICONFIGp = {(O, e)le(u) = 0 lor every u E COUNT}.

Our intention is to use configurations to represent the instruction of the program that is executed at a certain moment and the values of the variables at that moment. The function introduced below allows us to compute the configuration that succeeds a given configuration. We remind the reader of the notation [a -+ b]J introduced in Section 2.3, which refers to the function obtained from a function I by making its value on a be b. Let nextp : CONFIGp .."... CONFIGp be the partial function (where Dom(nextp) = CONFIGp - TCONFIGp ) defined as follows for (i, e) E CONFIGp - TCONFIGp : 5.4. Enumerating Programs 361

1. If P(i) is v +- v + 1, then nextp(i,e) = (i + 1,[v -+ e(v) + l]s). (5.5)

2. If P(i) is v +- 0, then nextp(i,e) = (i + 1, [v -+ O]e). (5.6)

3. If P(i) is goto I, then nextp( i, e) = (1, e'), (5.7)

where e' is obtained from e by setting to 0 all counter variables Cj such that P( i) is in the body of a loop instruction P(j) but the I + l-st instruction is not in this body and is not the associated endloop. 4. If P(i) is loop v, then

e if e(v) 0 t ( . )_{ (i+l,[cä-+ (v)]e) > (5.8) nex pi, e - (j + 1, e) otherwise,

where j is the line number of the endloop associated with P(i).

5. If P( i) is endloop, then

t (' ) _ { (k + 1, [CA: -+ e( CA:) - l]e) if e( CA:) > 1 neXpl,e - (i+l,[cA:-+O]e)otherwise. (5.9)

Here, ap(k) = i, that is, k is the line number of the loop associated with the endloop. Note that an endloop instruction resets to 0 the counter variable of the associated loop whenever it equals 1. The function nextp is not defined for terminal configurations. This me ans that P halts when ente ring such a configuration. Let s be astate of a VSPL program P. We define an environment e, determined by s as

if v E VAR if v E COUNT. Definition 5.4.6 The meaning of a program P is the partial mapping Mp: STATES,.",. STATES, where Mp(s) = s' ifthere is k E N such that nexti(O,e,) = (lPI,e') and s' = e'fVAR. Note that Mp is well-defined since there is at most one k E N such that nexti(O, e,) is a terminal configuration. This is true because no terminal configuration is in the domain of next p. If nexti (0, e,) is a terminal con• figuration, then we caU k the number of steps in the execution of P on input s. 362 5. Enumerability and Diagonalization

If s is astate that is not in Dom(Mp), then there is no k such that next1>(O, e,) is a terminal configuration. In other words, the program will cycle indefinitely. Having given a precise semantics, we can now prove precisely results about program behavior. Such proofs inevitably involve induction. In the proofofthe next lemma, we give all the details ofsuch an argument. Later, we leave such details for the reader to supply. Lemma 5.4.7 Let P be a VSPL program and let s be astate such that next1>(O, e,) is defined and equals (i, e). 11 P(j) is a loop statement such that P(i) is not in the body 01 the loop beginning at P(j), and P(i) is not the endloop corresponding to P(j), then e(cj) = O. Proof. The argument is by induction on k. For k = 0, the statement holds trivially because of the definition of initial configuration. Suppose that the result is true for k ~ 0 and that next~+1 (0, e.) = (i, e), P(j) is a loop statement, ap(j) = I, and either i $ j or i > I. Let next1> (0, e,) = (h, e'). We need to consider several cases, depending on the position and the nature of P(h).

1. If P(h) is not a goto, a loop, or an endloop, then the value of the counter variable Cj remains the same in the configuration (i, e) as it was in the previous configuration (h,e'), that is, e(cj) = e'(cj) = O. 2. Suppose now that P(h) is a goto i statement. If j < h < I, then (5.7) implies that e( Cj) = O. Otherwise, we have e( Cj) = e'( Cj) = 0, by the inductive hypothesis. 3. If P( h) is a loop statement and h :f; j, note that we cannot have j< h < I, since this would imply j < ap(h) < I. In view of the fact that the line executed after the loop statement P( h) is either h + 1 or ap(h) + 1, this would imply that line i is in the body of the loop P(j) or coincides with the endloop P(ap(j». Therefore, the loop P(h) is located outside the body of the loop P(j) and, by the induction hypothesis, e'( Cj) = 0, which gives e( Cj) = O. 4. If h = j, then we have either i = j + 1 or i = 1+ 1. The first case contradicts the hypothesis. In the second case e( Cj) = e' (Cj ) = o. . 5. Assurne now that P(h) is an endloop. Note that if h = ap(j), then we must have i = h + 1 and, by (5.9), we have e(cj) = O. If P(h) were located inside the body of the loop P(j), P(i) would also be included in this body or it would be the endloop P(ap(j», which contradicts the hypothesis made on P(i). The only case left is when P(h) is outside the body of P(j), h :f; I, and then we have e(cj) = e'(cj) = 0.1 5.4. Enumerating Programs 363

If we concatenate two VSPL programs P and Q, the gotos in the result• ing sequence that originate in Q will no longer redirect the execution of the new program to the proper instructions. Therefore, we need a special concatenation operation for programs called program concatenation. If S is a sequence of VSPL instructions, we denote by S[kj the sequence obtained by replacing every instruction goto I by goto I + k. Let P, Q be two VSPL programs. Consider the sequence of VSPL instruc• tions R obtained by concatenating the sequences P and Q[IPIl. We leave to the reader the task of verifying that R satisfies conditions (Pi)-(Piii), and hence, is a program.

Definition 5.4.8 The VSP L program obtained by program concatenation from VSPL programs P and Q is the VSPL program R introduced above. The program concatenation of P and Q will be denoted by

P • Q

It is easily seen that program concatenation is associative and hence, we may write concatenations of arbitrary finite sequences of programs unam• biguously.

Theorem 5.4.9 If R is the program concatenation of two VSPL progams P and Q, then MR = MQMp.

Proof. Consider astate s. If next~(O, e.) is defined, then

next~(O,e.) = next~(O,e.). Thus, if s ft Dom(Mp), then s ft Dom(MR), and if s E Dom(Mp) say, Mp(s) = s' and next~(O,e.) = (IPI,e'), where e'tVAR = s', then nextMO,e.) = (IPI,e'). By Lemma 5.4.7 we have e' = e.,. If (n,e) is a configuration for Q, let (n,e)+ = (IPI + n,e+), where

if xE VAR if x = Ci, i ~ IPI ifx=cj,i< IPI.

If next~(O, e.,) is defined, then next~+l (0, e.) is defined and

next~+I(O, e.) = (next~(O, e.,))+.

Thus, if s' ft Dom(MQ), then s ft Dom(MR) while if s' = Mp(s) E Dom(MQ) and next~(O,e.,) = (IQI,e") with MQ(s') = e"tVAR = s", then 364 5. Enumerability and Diagonalization

next~+1 (0, e,) = (next~(O, e,/»+ (IQI, e")+ (IPI + IQI, e"),

SO MR(S) = S" = MQ(Mp(s».1 Theorem 5.4.10 Let P be a VSPL program. Consider the sequence of instructions S defined by

loop v S { plI] endloop

The sequence S is a program and its meaning Ms is given by

Ms(s) = M;(v)(s). (5.10)

Proof. It is easy to see that S is a VSPL program. Let e be an environ• ment. For every rEN, define an environment wr(e) by

if z E VAR if z = Ci, i > 0 if z = Co.

We have two claims. Claim 1: If k E N, e E ENV and nexti(O, e) = (m,e'), then for an rEN, next~(1, wr(e» = (m + 1, wr(e'». This can be proven by induction on k, and the argument is left to the reader. Claim 2: If sESTATES, 0 $ I< s(v) and M~(s) = SI, then, for some t" we have next~(O,e,) = (1, W,(v)_I(e,,». The argument is by induction on I, using Claim 1, and it is also left to the reader. Now, suppose that M;(v)(s) is undefined. Then, there is some 1,0 $1 < s(v) such that M~(s) = S, and Mp(sl) is undefined. By Claim 2, for some t, E N, we have next~(O,e,) = (1, W,(v)_I(e,,». Since Mp(sl) is undefined, for an k, nexti(O, e,,) is defined. Hence, by Claim 1, for an k,

next~+k(O,e,) = next~(1, W,(v)_I(e,,» 5.4. Enumerating Programs 365 next~(O,e.) = (1, \)l(e •• (.)_.). Since Mp(s.(v)_d = s.(v) is defined, for some k, nexti(O,e',(.)_l) = (IPI,e.,(.». By Claim 1, we have

next~+l:(O,e.) = next~(1, \)l(e',(V)_l» = (!PI + 1, \)l(e.,(v»)' so next~+Hl(O,e,) = (!PI + 2, \)o(e,,(.») and Ms(s) = s.(v) = M;(v)(s). I Example 5.4.11 For the one-instruction program P,

v +- v + 1, we have Mp(s) = [v -+ s(v) + 1]s, for every state s. Consider the following program: loopu v+-v+1 endloop

We assurne that u f:. v. Suppose that initially we have s(v) = 0 and s(u) = b. By Theorem 5.4.10, the program ends in state z = M~(s). We have z(v) = b. This shows that the effect of the program is to "copy" the value of u into v, provided that the program starts from astate s in which the value of v is O. Note that we can always arrange this to happen if we consider a slightly larger program;

v+-O loop u v+-v+l endloop

We shall denote this program by v +- u. This is not hing but shorthand for the real thing, and it is a useful notational device. Example 5.4.12 A VSPL program such as

w+-O w+-w+l w+-w+l w+-w+l w+-w+l w+-w+l will simply be denoted by w +- 5. 366 5. Enumerability and Diagonalization

We use the programs introduced in this section to define a certain dass of functions. Let i = (Xl"" ,Xr) E N r, where rEN. Define astate Sif by

ifO ~ k ~ r-1 if k ~ r.

Definition 5.4.13 The r-argument lunction I);) computed by the VSPL program P (Ior rEN) is the partiallunction I : Nr ..... N, whose domain is {i E Nr I Sif E Dom(Mp)}, and lor each i E Dom(JJ;», IJ;)(i) = Mp(s,f)(wo). Note that the first r variables Wo, . .. ,Wr-l serve as input variables and that Wo also serves as output variable. When i f/. Dom(JJ;» , if P starts with the state S,f, then P will never halt. Observe that there is no require• ment that the variables Wo, . .. ,Wr-l occur in the program P. This allows us to use the same program to compute several functions (for different values for r). Let :FVSPL be the dass of partial functions computable by VSPL pro• grams. Definition 5.4.14 Let rEN and let I: Nr ..... N. We say that a VSPL program P strongly computes f illor all (Xl, . .. ,Xr) E Nr and all states S such that s(Wj) = Xi+l lor all i, 0 ~ i ~ r - 1, Mp(s) is defined il and only if (Xl> ... , x r ) E Dom(J) and if (Xl, ... , Zr) E Dom(J), then Mp(s)(wO) = f(X1, ... ,Zr). Note that if P strongly computes the r-argument function f, then f = fJ;), but more is true. Given r inputs in Wo, ... , Wr-l, P computes the correct answer no matter wh at values are assigned initially to the other variables.

Theorem 5.4.15 11 f : N r ..... N is in :FvSPL, then there is a VSPL program which strongly computes f.

Proof. Let I = IJ;) for P a VSPL program. Let Va,.·., Vm -l consist of all the variables in P wh ich are not among Wo, ... ,Wr-l, listed in order according to their code number. In addition, if r = 0 and Wo does not appear in P, indude Wo in this list. Let Q be the VSPL program

Va +- 0; ... ;Vm -1 +- 0, and let pi be the program concatenation of Q and P. Then by Theo• rem 5.4.9, Mp. = MpMQ. If i = (Xl. ... , xr) E Nr and s is astate such that s(wd = Xi+l for all i, 0 ~ i ~ r - 1, then MQ(s) and Sif agree on aB 5.4. Enumerating Programs 367 variables whieh oeeur in P. Thus, by Exercise 55, Mp,(s) = Mp(MQ(s» is defined if and only if Mp(s~) is defined, and, by assumption, this hap• pens if and only if f(i) is defined. Suppose that f(i) is defined. If Wo ap• pears in P then, by the same exereise, Mp,(s)(wo) = Mp(MQ(s»(wo) = Mp(s~)(wo) = f(i). If Wo does not appear in P, then Mp,(s)(wo) = Mp(MQ(s»(wo) = MQ(s)(wo) = s~(wo) = Mp(s~)(wo) = f(i) .• Example 5.4.16 The function pred : N -- N defined by 0 ifn=O pred( n) = { n _ 1 if n > 0, belongs to :FVSPL. Consider the following VSPL program: :2o~~l R Wl +- Wl + 1 !endloop Wo +- W2 Suppose that we have state s before exeeuting Q:

Q { W2 +- Wl Wl +- Wl + 1 If s( wd = a and s( W2) = b, then, after exeeuting the first instruction, we obtain state s', where s'(wd = a and S'(W2) = a; after the exeeution of the seeond instruetion, we have state s", where s"( wd = a + 1 and S"(W2) = a. The meaning of this fragment, MQ, transforms state sinto state s = MQ(s), where s(w) if W:F Wb W2 s(w) = { s(wd + 1 if W = Wl S(Wl) if W = W2. Note that the value assigned to W2 is one less than the value assigned to Wl· Using Theorem 5.4.10, the meaning of the fragment P, loop Wo P { W2 +- Wl Wl +- Wl + 1 endloop is Mp, where Mp(s) = MQ(wo)(s). In other words, if Mp(s) = s', then we have s(w) if W:F Wl,W2 { s'(w) = s(wd + s(wo) if W = Wl S(Wl) + s(wo) - 1 ifw = W2, 368 5. Enumerability and Diagonalization when s(WO) ~ 1. If s(WO) =0, then s' = s. Let P1 be the program

The meaning of P1 is given by

The meaning of the entire program is given by

Suppose that a computation of R begins in state So, where so(w) = n if W = Wo and 80(W) = 0, otherwise. Then, 81 = Mp(so) is given by ifw = Wo if W = W1 .,(w) = { ~red(n) ifw=W2 otherwise.

Therefore, the program will halt in state 82 = Mpl(8I), where 82(WO) = pred(n), wh ich shows that P computes the function pred. Note that the program

W1-0 W2 -0 loop Wo R' W2-W1 W1 - W1 + 1 endloop Wo -W2 is the program which is obtained from R using the method of the proof of Theorem 5.4.15 and hence, R' strongly computes pred. (In fact, the instruction W2 - 0 is not needed for g to strongly compute pred, but W1 - 0 is needed.)

Example 5.4.17 The function f : N2 -- N given by f( Z, y) = Z + Y for z,y E N belongs to :FvSPL. Indeed, consider the following program.

loop W1 wo-wo+l endloop

If 8 is the initial state of the program, then the variable Wo is incremented by 1 8(WI) times, which, of course, amounts to storing in Wo the sum of the initial values of Wo and W1. 5.4. Enumerating Programs 369

Example 5.4.18 Consider the following VSPL program:

loop Wo W1 -I endloop Wo -W1 This program computes the function sgn defined in Example 4.9.6.

Theorem 5.4.19 :FvsPL is c10sed under composition. In other words, il I: Nn ~ N and gi : Nm ~ N lor 1 ~ i ~ n belong to :FVSPL, then the function l(g1. ... ,gn) : Nm ~ N defined by

/(g1, . .. , gn)(X1. ... , x m ) = l(g1 (Xl. ... , x m ), . .. gn(X1, ... , x m ))

belongs to :FvSPL.

Proof. Suppose that P, P1, ... , Pn strongly compute I, g1, ... , gn, re• spectively, and let Vo, V1, ... , Vm -1, Z1, ... , Zn be distinct variables that do not appear in P1, ... ,Pn and are different from wo, ... ,wrnax(m,n)-l. Let Q be the following program:

Vo - Wo; .. ·; Vm -1 - Wm -1 • P1 • Z1 - Wo Wo - Vo;···; Wm -1 - Vm -1 • P2 • Z2 - Wo Wo - Vo;···; Wm -1 - Vm -1 • P3 • • Zn-1 - Wo Wo - Vo; ..• ; Wm -1 - Vm -1 • Pn • Zn - Wo Wo - Z1; ... ; Wn-1 - Zn • P 370 5. Enumerability and Diagonalization

It is not hard to show, using Theorem 5.4.9 and Exercise 55, that Q computes l(gl," ., gn). I

Example 5.4.20 We can show now that the test function lor : N 2 -- N defined by if Z > °or y > ° lor(z,y) = { ~ otherwise, belongs to :FYSPL. To this end, observe that we can write lor(z,y) sgn(z + y); since :FYSPL is closed with respect to composition, it follows that lor E :Fy SPL.

Theorem 5.4.21 :FYSPL is closed under primitive recursion. In other words, il 9 : Nm "'" N, and h : N m+2 "'" N belong to :FYSPL, then the function I : N m +1 "'" N defined by I(Zl, ... ,Zm,O) = g(Z1. ... , zm), I(Zl,'" ,zm,n + 1) h(Zl,'" ,zm,n,/(zl,'" ,Zm, n», belongs to :FYSPL.

Proof. Let P and Q be VSPL programs which strongly compute 9 and h respectively. Let Vo, ••• , Vm -1, v and W be distinct variables which do not appear in either P or Q and are different from Wo, ••• , Wm . Let R be the VSPL program:

Vo +- Wo; •• ·; Vm -1 +- Wm -1 W+-Wm • P R • v+-Q • loop W Tl1] endloop

Here, T is the program:

W m +1 +- Wo; Wm +- V Wo +- Vo; • •• ; Wm -1 +- Vm -1 T • Q

Using Theorem 5.4.9 and Exercise 55, it is not hard to show that if s is any state such that s(wo) = I(s(vo), ... ,s(vm-t},s(v», then MT(S) is 5.4. Enumerating Programs 371 defined if and only if f(s( vo), . . , ,s( vm-d, s(v) + 1) is defined and if s' = MT(S) is defined, then S'(Vä) = S(Vä) for all i, 0 $ i $ rn-I, s'(v) = s(v)+l and s'(wo) = f(s'( vo), ... , s'(vm-d, s'( v)). Using this remark, Theorems 5.4.9 and 5.4.10, and Exercise 55, it is now fairly easy to show that R computes f. I

Theorem 5.4.22 If f : Nn+l ""'" N belongs to :FVSPL, then so does the function JJf : N n ""'" N obtained from f by minimization.

Proof. We remind the reader that I'f(rl,'''' r n ) = q if q is the least natural number such that (rl,"" r n , q') E Dom(f) for all q' with 0 $ q' $ q and f(rl, ... ,rn,q) = 0, ifsuch a q exists; otherwise, I'f(rl, ... ,rn) is undefined. Suppose that P is a VSPL program which strongly computes fand that Vo, ... ,Vn-l, v are distinct variables which do not appear in P and are different from Wo, ... , Wn . Let Q be obtained by appending to P the lines

loop Wo Wo - vo; ... ; Wn-l - Vn-l V -v+ 1 W n -v gotoO endloop Wo -v

It is easy to show that Q is a VSPL program. Let R be the program concatenation of the initialization program

Vo - Wo;···; Vn-l - Wn-l with Q, so that we have

Vo - Wo;···; Vn-l - Wn-l • P loop Wo R Wo - Vo;· .. ; Wn-l - Vn-l V -v+ 1 W n -v goto 0 endloop Wo -v

The goto 0 in Q transfers control to the first line of P. The loop Wo never terminates normally; it serves as an if statement checking the value of Wo produced by P. Note that Q is not the program concatenation of P with the lines following it, nor is the loop just after P in the form of 372 5. Enumerability and Diagonalization

Theorem 5.4.10. Thus, neither of Theorems 5.4.9 and 5.4.10 is of much use in analyzing Q. It is not to hard to prove the following statement, however. Suppose that s is astate such that s( Vi) = s( Wi) for all i, 0 $ i $ n - 1, and s( v) = s( wn ). If f(s( wo), ... , s( wn-d, s( v)) is undefined, then MQ(s) is undefined. If f(s(wo), ... ,s(wn_d,s(v)) = 0, then MQ(s) is defined, and MQ(s)(wo) = s(v). If f(s(wo), ... ,s(wn_d,s(v)) > 0, then there is astate s' such that S'(Vi) = S'(Wi) = S(Wi) for all i, 0 $ i $ n - 1, s'(v) = s'(wn) = s(v) + 1, and MQ(s) = MQ(s'). We leave it to the reader to use this observation to show that R computes JJf •• Theorem 5.4.23 Every partial recursive function is VSPL-computable. Proof. The initial functions Z,pf and S are all VSPL-computable. In• deed, the VSPL programs that compute these functions are

Wo +- 0 for the zero function Z, Wo +- Wi-I for the projection pf with i > 1, the empty program (that is, the empty sequence of VSPL statements) for p?, and

Wo +- Wo + 1 for the suceessor function S. Since the set of partial recursive functions is defined as the closure of the set of initial functions under eomposition, primitive recursion and min• imization, the theorem follows immediately from Theorems 5.4.19, 5.4.21 and 5.4.22 .• We define a one-to-one mapping gn : P ROG ---+ N in such a manner that it will be easy to reconstitute the program P starting from gn(P). For the remainder of the ehapter, we denote by (I, PI, P2) a fixed, mono• tonie pairing tripie such that fand its projeetions PI, P2 are primitive reeursive functions (see Exereise 35).

Definition 5.4.24 The code d(S) of an instruction S is

f(l,p) if S is wp +- wp + 1 f(2,p) if S is wp +- 0 d(S) = f(3,p) if S is loop wp f( 4, I) if S is goto I f(5,0) if S is endloop. Note that any two distinct instructions have distinct codes. The mapping gn, defined on PROG, is given by

gn(P) = gnseq(d(So), ... ,d(Sn-I)), 5.4. Enumerating Programs 373 where P = So; ... ; Sn-I' In view of its definition, it is dear that gn is one-to-one. Moreover, start• ing from the code gn(P), we can easily reconstitute P. If m = gn(P), then PI(m) gives the length of P, and 1r(i,PI(m),P2(m)) will give us the code of the i-th instruction, for 1 ~ i ~ PI (m). We call gn(P) the Gödel number of P and we denote by GPROG the set gn( P ROG) of all Gödel numbers of programs from P ROG. VSPL programs can be indexed by their Gödel numbers. Namely, if x E gn(P ROG), then Pr is the VSPL program whose Gödel number is x. Not all natural numbers are Gödel numbers of programs, and this cre• ates certain technical problems. Therefore, we shall adopt the following convention: if x (j gn( P ROG), then Pr will be the ever cyding program

goto 0

This convention makes every natural number the Gödel number of a VSPL program. The partial function f~n) : N n --... N will be denoted by f~n). An example of a functi~n that does not belong to Fv SPL originates in the halting problem for VSPL programs.

Definition 5.4.25 Let P be a VSP L program. P halts on input m if m E Dom(f~l»). There is no VSPL program that is able to decide, starting from the Gödel number Xl of a program P and a number x2 E N, whether or not

Pr1 halts on X2. In order to make a formal statement of this property of VSPL programs, consider the function h : N x N --+ N defined by

if X2 E Dom(f~!») otherwise.

Theorem 5.4.26 The function h does not belong to FVSPL. Proof. We present an argument by diagonalization. Suppose that there is a VSPL program Q that computes h. This implies the existence of a VSPL program R that will compute the function 9 : N --+ N, defined by

( ) _ {I if Pz halts on input z 9 z - 0 otherwise.

Indeed, R can be obtained from Q by adding one extra instruction:

WI +- Wo R { • Q 374 5. Enumerability and Diagonalization

Starting from R, consider the program P: p{ ~opwo goto IRI + 1 endloop Let z = gn(P) and let us apply P to its own Gödel number. If P halts on input z, then upon exiting R, the value of the variable Wo would be 1. This, however, implies that P will cyde forever on the (IRI + l)-st instruction goto IRI + 1 and P never halts on z. Assurne now that P does not halt on z. This happens only if R assigns the value 0 in Wo. The definition of R teIls us that this means that the program P halts on its own Gödel number. Since both situations yield contradictions, we condude that R does not exist and, therefore, Q does not exist. I

5.5 Abstract Families of Functions

The purpose of this section is to present an abstract version of the families of functions that are computable by certain dasses of programs. We use this section to provide the reader with additional exposure to proofs by diagonalization. Definition 5.5.1 An abstract family of functions (AFF) is a family of partial functions F = Ufn)ln E N, i E N}, where If n ) : Nn ~ N for every n, i E N. The definition of AFF implies that every AFF is countable. Example 5.5.2 Let G be the abstract family consisting of the functions (n) Nn N h (n)() . r . N gi: ~ ,weregi Xl, ... ,Xn =xl+···+xn +tl0reveryn,tE . Note that this family contains such functions as g~2)(Xl' X2) = Xl + X2 and g~l)(xd = Xl for Xl, X2 E N. All members ofthis family are total functions.

Example 5.5.3 Consider the family of functions li(n) , where li(n) is the n-argument function computed by the VSPL program P with gn(P) = i, as defined in Section 5.4. This abstract family contains both total and nontotal functions.

In the sequel, we are going to use the constant functions I<;: : N n ----. N, with n, k E N, defined by

for every Xl, ... ,Xn E N. 5.5. Abstract Families of Functions 375

Example 5.5.4 Consider the AFF POL that consists of all multivariable polynomials with coefficients in N. Any polynomial 9 : N n --+ N can be written as

(a!, ... ,an)EA for every xl, ••. , Xn E N, where the sum extends over a finite subset A n of N , and ca!, ... ,a n =F 0 for every (al, ... , an) E A. Each such polynomial uniquely determines the sets A and {calo ... ,an!(a1, ... ,an) E A}. We index 9 using the codes for the (n + 1)- tuples (ca!, ... ,an' a1, ... , an). Consider the function In+1 : N n +1 --+ N introduced in Example 5.3.22. Suppose that 9 has r monomials and let

be the I-th monomial of 9 (where I is determined by the place ofthe n-tuple (al, ... , an) in the lexicographic order of the poset (Nn,~» for 1 ~ I ~ r. Let b, = In+1 (cau , ... ,an/' all, ... , an') be "the code" of m,. The index of 9 will be defined as the code of the sequence (b 1 , ••. ,br ) (as introduced in Example 5.3.24). If A = 0, then 9 = K[; , and the index of 9 is o. If i is not the code of a polynomial, we define gr = K[;. Now, we introduce or recall some special notations for functions that we need in this section. We use the notation p~ : Nn --+ N to designate the projection

for all Xl, ••• , X n E N and 1 ~ m ~ n. For each n E N, we have 0 : Nn "-+ N and Dom(0) = 0. When we want to emphasize that we are considering 0 as a partial function on N n , we will write it as 0n . The successor function S : N --+ N is given by S(n) = n + 1 for n E N and the discriminator function D : N 4 --+ N is defined by

ifp = q D(m,n,p,q) = { : otherwise.

The collection 01 basic lunctions consists of the projections, the constant functions, and the discriminator function. For an AFF F, the set of functions from F that have m arguments will be denoted by Fm. 376 5. Enumerability and Diagonalization

Definition 5.5.5 Let F be an AFF. The universal function lor Fm is the partial function Um: Nm+1 "" N such that (i,ZI, ... ,Zm) E Dom(um) il and only il(ZI, ... ,Zm) E Dom(flm» and

Um(i,ZI, ... ,Zm) = Ifm)(zl. ... 'zm) lor every (ZI, ... , zm) E Dom(fi(m». F is a universal family il the universal function Um lor Fm is in F m+1 lor every m E N. Example 5.5.6 The AFF introduced in Example 5.5.2 is universal. In• deed, note that g~n+1) is the universal function for G n since

(n+1)(. ) (n)( ) go Z, ZI, ... , Zn = gi Zb ... , Zn for every i,ZI"'" Zn E N. Furthermore, Exercise 67 shows that the collection of partial recursive functions (which is :Fv spL) is a universal AFF. Definition 5.5.7 An AFF F is closed under composition illrom h E Fm, with m ~ 1, and gl, ... , gm E F n, it 10 110 ws that h(gl, ... , gm) E F n. Using a diagonalization argument we can prove the following. Theorem 5.5.8 A universal AFF that is closed under composition and contains the projections and the successor lunction cannot consist entirely 01 total functions.

Proof. Let F be a universal AFF and let S E F 1 be the successor function. In view ofthe above hypothesis, we have I = S(u2(pLpt» E F 1 . We have I(n) = u2(n, n) + 1 = IA 1)(n) + 1 for n E Dom(fA1». Suppose that every member of F is a total function. Since I E F 1, there is k E N such that I = Ik 1), and since I is total, I( k) is defined. However, this generates a contradiction since we also have Ik 1)(k) = I(k) = Ik 1)(k) + 1, and therefore, such a family cannot consist entirely of total functions .• A consequence of Theorem 5.5.8 is given in the following. Corollary 5.5.9 Let F be a universal AFF that is closed under composi• tion and contains the constant lunctions, the projections, and the successor lunction. Then F contains the empty lunction. Proof. From the proof of Theorem 5.5.8, it follows that F contains a nontotal, one-argument function, I : N "" N. Let k be a number, k E N -Dom(f). Since F is closed under composition, we have h = loKZ E F. Note that, hO = I(KZm = I(k) and therefore, h = 00 is the empty function .• The halting problem discussed in Section 5.4 can be rephrased for AFFs. The proof is by diagonalization. 5.5. Abstract Families of Functions 377

Theorem 5.5.10 Let F be a universal AFF that is closed under compo• sition and contains the constant functions, the projections, the successor function and the discriminator function. There is no function h : N 2 -- N in F 2 such that

if y E Dom(f}I» h(z,y) = { ~ otherwise.

Proof. From Corollary 5.5.9, we have 01 E F 1 . Therefore, we have 01 = fP> for some i E N. Also, since KJ E Fl, we can write KJ = fP>. Suppose that hE F 2 and consider the function f : N"""" N defined by f(z) = u2(D(j,i,h(z,z),0),z) for every zEN. Note that we have f E F 1 since we can write f = u2(D(K] , Kl,h(pt,pt), KJ),pt).

From the definition of f we obtain

f(z) = u2(D(j, i, h(z, z), 0), z) _ ~ U2(j, z) if h(z, z) = ° - u2(i,z) if h(z,z) = 1 _ ° ifh(z,z)=O - undefined if h(z,z) = 1.

As customary in proofs by diagonalization, suppose that c is an index for !, that is, ! = !P>. The previous equalities give 0 if h(c,c) = 0 !(c) undefined if h(c,c) = 1 if c ft Dom(f~I» = °undefined if c E Dom(f~I» if c ft Dom(!) = °undefined if c E Dom(f) , which is a contradiction. We conclude that h ft F 1. I Corollary 5.5.11 Let F be a universal AFF that is closed under com• position and contains the constant functions, the projections, the successor junction and the discriminator function. The junction k : N -- N, defined by if z E Dom(f}I» k(z) = { ~ otherwise, does not belong to Fl' 378 5. Enumerability and Diagonalization

Proof. Let us remark that k(x) = h(x, x) for x E N and that the previ• ous proof shows that a contradiction can be derived from the assumption that k E F1.1 Let P be a program written in some programming language. Assurne that we use P for computing the values of some function f : Nm+n ~ N. Furthermore, suppose that f belongs to some AFF F = Uj')I/,j E N} and that i is a subscript for f, that is, f = fi(m+n). If we fix the first m arguments of f to be a1, . .. , am , we obtain a new function g : N n ~ N, where for Xl, ••• , x n E N. Note that if F contains the projections and the con• stant functions and it is closed with respect to composition, we have g E F n. In general, when an AFF family is defined in relation to a programming language, we use some code of the program as the subscript for the func• tion computed by that program (see Example 5.5.3). If this is the case for the AFF F, then, since g E Fm, we can write g = f;n), where j is the code of a program Q that computes g. We are interested in those AFFs that contain functions sm,n : N m +1 ~ N which allow us to write j = sm,n(i, a1,···, am ). Definition 5.5.12 An AFF has the smn property ij, for every m, n E N, there exists a total function sm,n : N m+1 ~ N such that for every (m+n) F h /,i E m+n we ave

for every Xl, •• ·, X m+n E N. Definition 5.5.13 A standard AFF is a universal AFF that has the smn property, is c10sed with respect to composition, and contains the basic func• tions. Note that, according to Corollary 5.5.9, any standard AFF contains the empty function.

Example 5.5.14 Exercise 70 implies that FVSPL has the smn property. This shows that the AFF of partial recursive functions is a standard AFF.

An application of the smn property is given below.

Theorem 5.5.15 Let F be a standard AFF. There exists a function cE F 2 such that «l? .) = /,~1) 0 f~l) f C I,) I ) for every i,j E N. 5.5. Abstract Families of Functions 379

Proof. Consider the function 9 : N3 "-+ N, defined by

9 = U2(p~, U2(p~,p~», which belongs to F 3 . We have 9 = lä(3) for some i E N and

for Xl, X2, X3 E N. Applying the definition of universal function, we obtain

On the other hand, an application of the smn property allows us to write

which shows that we can define c as

Definition 5.5.16 Let F be an AFF, and let n E N. A set A ~ N n is an n-ary F -set il its characteristic lunction XA belongs to F n. A set A is an F-set il it is an n-ary F-sei lor some n.

Theorem 5.5.17 Let F be a standard AFF, and let A, B ~ Nn be two n-ary F-sets. Then, AU B, An B, and Nn - A are also n-ary F-sets.

Proof. The characteristic function of the complement C = Nn - A can be expressed as Xc(X) = D(I, 0, XA(X), 0), which shows that C is an F-set. Consider the function 1 defined by I(x) = D(XA(X), O,XA(X), XB(X» -t XA(X) if XA(X) = XB(X) - 0 otherwise, _ 1 if XA(X) = XB(X) = 1 - 0 otherwise for every X E N n • This is precisely the characteristic function of the set An B, and this proves that An B is an F-set. AU B is an F-set because the intersection and the complement of two F-sets is an F-set. I Note that, for any n E N, 0 and Nn are n-ary F-sets for any standard AFF, since their characteristic functions are Kr; and K?, respectively. 380 5. Enumerability and Diagonalization

Theorem 5.5.18 Consider an AFF family F = {fi(n) In, i E N} and the set T(F), defined by

T(F) = {n E Nlf~1) is total}. For any standard AFF F, the set T(F) is not an F-set. Proof. We have to prove that the characteristic function t : N --- N of TF) is not a member of F 1 for any standard AFF F. Consider the function g : N 2 ~ N given by

For p,q E N, we have

if pE DomUP») g(p, q) KJ(U2(p,p)) 0 d fi d = = { un e ne otherwise.

Since F is a standard family, we have g E F 2 ; hence, g = f?) for some i E N. Using the smn property we have

for every p, q E N. Consider the total function l : N --- N given by l(p) = S1,1(i,p) for every p E N. We have

if E / (1) _ { KJ p DomU~1») l(p) - 01 otherwise.

Assurne that the function t is a member of the family, t E F1. Since F is closed under composition, the total function tl is also a member of F 1. We claim that tl = k, where k is the function introduced in Corol• lary 5.5.11. Indeed, if p E DomUP»), the function f;(~) is total (being equal to KJ), and therefore, t(l(p)) = 1 = k(p). On the other hand, if p ft DomU~1»), then t(l(p)) = 0 = k(p), since f;(~) = 01. This is a contra• diction since tl E F 1 and k ft F 1, according to Corollary 5.5.11.. For any standard AFF F, the set K(F), defined by

K(F) = {x E N I x E DomU~1»)} is not an F-set. This follows immediately from the fact that XK(F) coincides with the function k considered in Corollary 5.5.11. In general, a function f that belongs to an AFF F may have many subscripts; this corresponds to the fact that a function may be computed by several programs. From the point of view of a program P whose Gödel number is i, a function Iln) describes the input-output behavior of P. 5.5. Abstract Families of Functions 381

Therefore, it is interesting to examine those collections of programs whose input-output behaviors are described by functions that belong to certain families of functions. For any AFF Fand collection of functions C ~ F1, define the index set olC as P(C) = {i E NI/P) E Cl· Theorem 5.5.19 (Rice's Theorem) IIF is a standard AFF, and P(C) is an F-set, then we have either P(C) = N or P(C) = 0. Proof. Suppose that 0 C P(C) C N and that P(C) is an F-set. In this case, C is neither empty nor the entire set F 1. We may assurne that the empty function 01 is not a member of Cj if this is not the case, we can replace C in the subsequent argument by F 1 - C. Let IP) be a function from C. Consider the function 9 : N 2 ~ N defined by g(X1,X2) = D(U2(i,X2),O,U2(X1,Xt),U2(X1,Xt» for Xl, X2 E N. Since 9 is a member of F 2 , we have 9 = I?) for some JEN, and we can write

if Xl E Dom(f~!» otherwise.

Applying the smn property, we obtain

if Xl E Dom(f~!» otherwise, which, in turn, implies

if Xl E Dom(f~!» otherwise.

Consider the function h : N ~ N, defined by h(x) = Sl,l(j,X). In view of the choice made for the function IP), we have h( x) E P( C) if and only if xE K(F). In terms of characteristic functions this amounts to

XK(F) = XP(C) 0 h, which implies that K(F) is an F-set. Since we know that this is not the case, it follows that P(C) is not an F-set either. I Rice's theorem is a powerful tool for proving that certain sets are not F-sets. For instance, Theorem 5.5.18 follows as an immediate consequence (see part (1) of the next Corollary). Corollary 5.5.20 For every standard AFF F the lollowing sets are not F-sets. 382 5. Enumerability and Diagonalization

1. P1 = {i E NI/P) is total}.

2. P2 = {i E NIDomUI1» = 0}.

3. P3 = {i E NIDomUP» is infinite}.

4. P4 = {i E NI/P)(xo) = Yo} lor same fixed Xo,Yo in N. 5. Ps = {i E NI/P) = f} lor same fixed function I E F1.

6. P6 = {i E Nlyo E RanUP»)} lor same fixed Yo E N.

Let I E F1 be a total function. We shall prove that if F is a standard AFF then there exists a subscript i such that IP) = I;U). Such a subscript is called a fixed point of I. If i is a fixed point of the function I, this means that both i and I(i) denote the same function. This result has some interesting consequences. If F is an AFF then we can list all functions that belong to F 1 as 1~1),/~1), ... ,/~1),.... (5.11) Since F contains the successor function Sand it is closed with respect to composition, it also contains the function I : N --+ N, defined by I(x) = x + k. Since I has a fixed point, it follows that there is i E N such that .rl 1) = IjU) or, equivalently, that IP) = 1f!.1. This shows that for each k ~ 1 there are identical functions located k positions apart in list (5.11). Theorem 5.5.21 (Recursion Theorem) Let F be a standard AFF. 11 I E F 1 is a total function then there is i E N such that IP) = I;U).

Proof. Consider the function 9 E F2 defined by g(x,y) = U2(U2(X,X),y) for x,y E N. We have t<1) () if l~l)(x) is defined g(x, y) = { l~l)(Z) Y undefined otherwise.

If g(x,y) = 1$2)(x,y), then, applying the smn property, we have g(x,y) = t<1) ( _)(y). Let I E F be the total function determined by 81,1 r,,,,, 1 I(x) = sl,l(r, x) for every x E N. We have J,<1) = {/jl\z) if l~l)(x) is defin ed l(z) 01 otherwise. 5.6. Exercises and Supplements 383

Note that the function h = 101 is total since both 1 and I are total. We write h = Ig), since h E F1. Let n = I(m). We claim that n is a fixed point of I. Indeed, we have /,(1) _ ,(i) _ 1(1) n - I(m) - !!.:)(m) since Ig)(m) = h(m) is defined. Furthermore, in view of the definition of h, we have /,(1) - ,(i) - 1(1) - 1(1) n - h(m) - !(I(m» - J(n)' which proves that n is a fixed point for I. I

5.6 Exercises and Supplements

Equinumerous Sets

1. Let M and P be sets such that M j P. Prove that

(a) P(M) j P(P); (b) M(M) j M(P) (here M(M) is the collection of all multisets over M as introduced in Chapter 1); ( c) M x Q j P x Q for all sets Q; (d) M U Q j PU Q for all sets Q such that M n Q = P n Q = 0. Conclude that if M '" P, then P(M) '" P(P), M(M) '" M(P), Mx Q '" P X Q for all sets Q, and M U Q '" PU Q for all sets Q such that M n Q = P n Q = 0. 2. Using the axiom of choice, show that if p is an equivalence on a set M, then {[x]p I x E M} j M. 3. Without using the axiom of choice, show that if M and P are sets such that N j M U P, then either N j M or N j P. 4. Using the axiom of choice, show that if M is a nonempty set and Pis any set, then M j P if and only if there is a surjection 1 : P --+ M. (Theorem 5.3.3 is a special case of this exercise, wh ich can be proven without the axiom of choice.) Hint: Use Theorem 2.3.11. 5. Let M be a set, n a natural number and (Mo, ... , Mn-d be a se• quence of n sets. Show that (a) Seq(M) is finite if and only if M = 0. (b) Pfin (M) is finite if and only if M is finite. 384 5. Enumerability and Diagonalization

(c) Mo x ... x Mn- 1 is finite if and only if either i. there is an i, 0 :S i:S n - 1, such that Mi = 0, or ii. for every i, 0 :S i :S n - 1, Mi is finite. (d) Mn is finite if and only if either n = 0 or M is finite. Hint: You may find Theorem 5.2.9 useful for showing that certain sets are infinite. For example, to show that Seq(M) is infinite when M is nonempty, let a be some element of M, and define an injection J : N ---+ Seq(M) by defining J(n) to be the sequence of length n each of whose entries equals a. Since N is infinite, Theorem 5.2.9 shows that Seq(M) is infinite. 6. Using the axiom of choice, generalize the third part of the previous exercise by showing that for any family of sets {Mi I i E I} indexed by an arbitrary set I, TIiEI Mi is finite if and only if either (a) there is an i E I such that Mi = 0, or

(b) there is a finite subset J of I such that for every j E J, Mj IS finite, and for every i E 1- J, Mi is a singleton. 7. Show that if Cis a collection of sets, then UC is finite if and only if C is finite and every element of Cis finite.

8. Consider a sequence of sets Mo, ... , Mn-l such that MIc ~ MicH for 0 :S k :S n - 2 and Mn-l ~ Mo. Prove that Mp "'" Mg for o :S p, q :S n - 1.

9. Let M, P, J, 9 and ~ be as in Theorem 5.2.11 and its proof.

(a) Prove that we have ~(M) = M if and only if J is a surjection. Also, ~(0) = 0 if and only if 9 is a surjection. (b) Prove that if K and H are fixed points of ~, then so are KUH and KnH.

10. Let M and P be two sets, and let J : M ---+ P and 9 : P ---+ M be two injections. Show that for every subset K of M, M -K ~ Ran(g). Also, the mapping h : M ---+ P defined by

h a = {J(a) ~f a E K ( ) g-l(a) If a E M - K,

is a bijection if and only if K is a fixed point of the mapping ~ defined in the proof of Theorem 5.2.1l. Hint: One half of the argument is contained in the proof of Theo• rem 5.2.11. 5.6. Exercises and Supplements 385

11. Let M and P be two sets and let f : M ---+ P and 9 : P ---+ M be two injective mappings. For each a E M we define a sequence sa as folIows.

sao - a, g-l(s~) if s~ is defined, n is even and s~ E Ran(g) S~+l = rl(s~) if s~ is defined, n is odd, and s~ E Ran(f) 1undefined otherwise.

Similarly, for each b E P we define a sequence t b as folIows.

tob - b, rl(t~) if t~ is defined, n is even and t~ E Ran(f) t~+1 = g-l(t~) if t~ is defined, n is odd, and t~ E Ran(g) 1undefined otherwise.

(a) Prove that for all a E M, t!(a) = (a). Sa; similarly, for all b E P, sg(b) = (b) ·tb.

Let MM, Mp and Moo be defined as

MM {a E Misa is of even finite length }, Mp {a E Misa is of odd finite length }, Moo = {a E Misa is infinite }.

Similarly, Pp, PM, P00 are introduced as

Pp {b E Plt b is of even finite length }, PM = {b E Plt b is of odd finite length }, Poo = {b E Pltb is infinite }.

(b) Prove that ff M M is a bijection between M M and PM, ff M 00 is a bijection between Moo and Poo , 9 t Pp is a bijection between Pp and M p, and 9 t P00 is a bijection between P00 and M 00 • (Thus, if /{ = MM or K = MM U Moo and h : M ---+ P is defined as in Exercise 10, then h is a bijection. This gives another proof of the Schröder-Bernstein Theorem where we explicitly construct two fixed points of the mapping ~ as defined in our original proof.) 386 5. Enumerability and Diagonalization

12. (a) Let 1 : M --+ P and 9 : P --+ M be injections, and let (Qo, ... , Qn, ... ) be the infinite sequence of subsets of M defined recursively by Qo = M - Ran(g) and Qn+1 = g(f(Qn», for n E N. Prove that, in the notation of the previous exercise, MM = UneNQn' (b) Show directly (i.e., without referring to the previous exercise) that if K is defined to be the union of the Qn's and h is defined as in Exercise 10, then h is a bijection. (Motivation for this proof ofTheorem 5.2.11 can be given as fol• lows. As a first attempt to construct h, we might let it equal g-1 wherever we can (i.e., on g(P» and let it equal 1 elsewhere (i.e., on Qo = M - g(P». This suggested h will not be an injection because every element of I(Qo) will be the image under h of an element of Qo and also of an element of Q1 = g(f(Qo». We can remedy this problem by redefining h on Q1 to be the same as I, but now elements of I(Qt) are the image under h of an element of Q1 and also of Q2 = g(f(Ql»' We fix this by redefining h on Q2 and so on. We are led to let K be the union of the Qn's and define ht K = ff K and ht(M - K) = g-1 t(M - K).)

13. Let 1 : M --+ P and 9 : P --+ M be injections and let

where the sets Qi are as defined in the previous exercise. Conclude that if we take the least fixed point of

If Q ~ P ~ M and M '" Q then M", P. (5.12)

(a) Show how to derive (5.12) easily from Theorem 5.2.11. (b) Conversely, show how Theorem 5.2.11 can be derived easily from (5.12). Hint: If 1 : M --+ P and 9 : P --+ Mare injections, then g(f(M» ~ g(P) ~ M and M '" g(f(M» via gl. 5.6. Exercises and Supplements 387

(c) We can prove (5.12) as follows. If Q ~ P ~ M and k : M --+ Q is a bijection, define a sequence of sets recursively by Do = M - P and Dn+1 = k(Dn ) for nE N. Let K = UneN Dn , and define I by I(a) = {k(a) ~fa E K a IfaEM-K. Show that I is a bijection from M to P. (d) Let I: M --+ P and g : P --+ M be injections. Then, g(f(M» ~ g(P) ~ M and k : M --+ g(f(M» is a bijection when k = gl. Let I: M --+ g(P) be the bijection which results from the above proofof(5.12). Then, h = g-ll is a bijection from M to P. Show that h is the same bijection that was constructed in the last three exerClses.

15. (a) Without using the axiom of choke, show by induction that if M is an infinite set, then for an n E N, {O, ... , n - 1} ~ M (and hence, for all finite sets P, P ~ M). (b) Explain why the argument you gave in (a) cannot be used to prove Theorem 5.2.14 without the axiom of choice.

16. Show that in the proof ofTheorem 5.2.16, when the explicitly defined selective function I for M is used, the resulting injection g : N --+ M is in fact a bijection (and hence, the final paragraph of the proof is actually unneccessary). Countable and U ncountable Sets 17. Let M be a countable set.

(a) Let g : M --+ N be a numbering for M. Define a relation pg on M by pg = {(a, b) E M2 I g(a) :::; g(b)}. Prove that pg is a linear ordering on M such that every element of M has only finitely many predecessors under Pg. (b) Conversely, suppose that P is a linear ordering on M such that each element of M has only finitely many predecessors under p. Define gp : M --+ N by

gp(a) = I{b E MI bpa and b =f. a}1

for an a E M. Show that gp is a numbering of M.

Let NUMBM be the set of an numberings of M and let LM be the set of an linear orderings on M such that each element of M has only finitely many predecessors under the ordering. Define cI> : NUMBM --+ LM by cI>(g) = Pg for all g E NUMBM, and define W" : L M --+ NUMBM by W"(p) = gp for all p E L M • 388 5. Enumerability and Diagonalization

(c) Show that 4> is a left inverse for W (i.e., 4>(w(p)) = p for all p E L M ), butthat in general 4> is not a right inverse for W. (Thus, 4> is onto but not one-to-one and W is one-to-one but not onto.) (d) Show that the range ofw is the set ofnumberings 9 of M with the following property: IfO ~ n ~ m and m E g(M), then nE g(M) (i.e., there are no gaps in the range of g). In particular, if M is countably infinite, then the range of W is the set of bijective numberings of M. It follows that if we restrict our attention to numberings with the property just given, then the two processes of the first two parts of the exercise are inverse to each other. (e) Suppose that M is countably infinite. Let 9 be a numbering of M, and let g' be the bijective numbering of M obtained from 9 by using the method of page 347. Show that g' = w(4)(g)) . Thus, g' is the unique bijective numbering whose induced order• ing Pg' is the same as the ordering pg induced by the original numbering g.

18. Let 9 : Seq(N) ~ N be the function defined in Theorem 5.3.7. Give a simple characterization in terms of the prime factorization of a number wh ich determines whether or not the number is in the range of g.

19. Let 9 be as in the previous exercise, and let g' be the bijective num• bering which results from applying the process described on page 347. What is g' ((2, 1,0))?

20. Define a function f : Seq(N) ~ N by

ao a .. _l 1 f (( ao,···,an-l )) --Po ···Pn-l-

for each sequence (ao, ... ,an-t) in Seq(N) where (PO,Pl, ...) is the sequence of prime natural numbers listed in increasing order. (a) Show that f is a surjection, but not an injection. (b) Show that for each n E N, the restriction of f to Nn is one-to• one. (c) Let T be the subset of Seq(N) consisting of those sequences which do not end with a 0. Show that the restriction of f to T is a bijection from T to N. 21. Prove that the following sets are countable: (a) the set of one-variable polynomials with rational coefficients; 5.6. Exercises and Supplements 389

(b) the set of algebraic numbers;2 (c) the set of all formulas of propositionallogic; (d) the set of all partial recursive functions.

22. Let K be a subset of R that is well-ordered under the usual order relation. Prove that K is countable. Hint: We define a function f : K - Q. Let x E K, and consider the set Gz = {y E K I x < y}. If Gz 'f. 0, then Gz has aleast element Yz, and we can let f(x) be a rational number in (x,yz). (For definiteness, choose f(x) with smallest possible denominator, and if there are several candidates with the same denominator, choose the one with the smallest numerator, when written in lowest terms.) If Gz = 0, then x is the greatest element of K, and we can define f(x) as an arbitrary rational number greater than x. You need to show that f is one-to-one.

23. Let M be a set such that there exists a linear ordering on M. Without using the axiom of choice, show that the union of any countable collection of finite subsets of M is countable. Conclude without using the axiom of choice that the union of any countable collection of finite sets of real numbers is countable.

24. Consider a set of positive real numbers K. Suppose that there a number a ERsuch that for any finite subset J of K, the sum of the numbers in J is less than or equal to a. Prove that K is countable. Solution: Consider the countable collection of sets {Qn In E N}, where Qn = {x E R I 2"\1 < X ::; 21,,} for n ~ 0 and the set p = {x E R I x > I}. Note that none of these sets can contain more than a finite number of elements of K. Indeed, if Un = Qn n K would be an infinite set it would suffice to consider a set containing 2n +2 ra1 elements of Un. The sum of these elements would exceed

which would contradict our assumption about K. Consequently, K is a countable union of finite sets of reals and, therefore, it is countable.

25. Show that if M and P are countable sets, then so is MG:) P.

26. Let M and P be two sets such that M is countable and P is countably infinite. Prove that M U P '" P.

2 An algebraic nu mb er is areal number that is the root of some polynomial with integer coefficients. 390 5. Enumerability and Diagonalization

27. Prove that every countably infinite set can be obtained as the union of a countably infinite disjoint family of countably infinite sets. 28. Let V = {al' ... ' ak} be an alphabet, and let h : V· - N be the function defined in Example 5.3.12. For each m E N, define Im = {nEN I T-1$n<---r-r-.k"'-l k"'+l_l} (a) Show that {Im Im E N} is a partition of N. (b) Show that for each m E N, the restriction of h to V m is a bijection between Vm and Im. Hint: First argue that the restriction maps into Im and that it is one-to-one, then use a counting argument to show that it is onto. (c) Conclude from the first two parts that h is a bijection. 29. Let V = {ao, ... , ak-d be an alphabet. Define f: V· - N by

f( ajo···aj"'_l ) =%0. km - 1 +%1• k m-2+···+%m-1·.

(Note that here the indices ij vary from 0 to k - 1, not 1 to k.) Prove that f is a surjection but is not an injection. Show however that for all m E N, the restriction of f to Vm is an injection. 30. Let ASeq(N) and NDSeq(N) be the set of ascending sequences and the set of nondescending sequences over N, respectively. A sequence (ao, ... ,an-d is in ASeq(N) if ao < a2 < ... < an-l and it is in NDSeq(N) if ao $ a1 $ ... $ an-1. Prove by giving explicit bijections that (a) Seq(N)"" NDSeq(N), (b) NDSeq(N)"" ASeq(N), (c) ASeq(N),.., Pfin(N), (d) Pfin(N) '" N. Solution: (a) Consider the mapping f : Seq(N) - NDSeq(N) given by f(ao, ... , an-1) = (ao, ao + a1, ... , ao + a1 + ... + an-d for every sequence (ao, ... , an-d. Note that f(>') = >.. It is clear that (ao, ao + a1, ... ,ao + a1 + ... + an-1) is a nondescending sequence and that f is an injection. Morever, if (bo, ... , bn-t) E NDSeq(N), we have f(ao, ... , an-d = (bo, ... , bn-d, where ao = bo and aj = bj - bj-1 for 1 $ i $ n - 1, which shows that f is a bijection. (b) Let (ao, . .. , an-1) be a nondescending sequence. Define h( ao, a1 , ... ,an-d to be (bo,b1, ... ,bn-d, where bj = aj +j for 0 $ j $ n - 1. Since bj - bj- 1 = aj - aj-1 + 1 ~ 1, we have bj > bj _1 for 1 $ j $ n - 1. This shows that (bo, b1, ... , bn-d is indeed an 5.6. Exercises and Supplements 391

ascending sequence. Since aj = bj - j, it follows that h is a one• to-one mapping. Furthermore, observe that if (bo, bl, ... , bn- 1 ) is an ascending sequence, then bj ~ j for 0 $ j $ n - 1. This allows us to define a sequence of natural numbers (ao, ... , an-d by aj = bj - j for 0 $ j $ n - 1. Note that aj - aj_l = bj - bj _ 1 - 1 ~ 0 (because of the fact that bj > bj _ 1 ); hence, aj ~ aj_l and (ao, ... , an-d is a nondescending sequence with h(ao, ... ,an-d = (bo, ... ,bn-I). This proves that h is onto, and, therefore, it is the desired bijection. (c) Define k : ASeq(N) - Pfin(N) by letting k(ao, ... , an-I} = {ao, ... , an-d for every ascending sequence (ao, ... , an-I). Then, n = Ik(ao, ... , an-dl, and for each i, 0 $ i $ n - 1, ai is the i + 1st largest element of k( ao, ... , an-I) so the sequence (ao, ... , an-I) can be recovered from the value of k on that sequence, which shows that k is injective. If K E Pfin(N) then K = k(ao,· .. , an-I} where (ao, ... , an-I) is the sequence listing the elements of K in increasing order, so k is onto. (d) Define 1 : Pfin(N) - N by 1(8) = 2ao + ... + 2a .. - 1 where (ao, ... , an-I) is the sequence listing the elements of 8 in increasing order. We have 1(0) = 0, and ifwe have 1(8) = x for some 8 E Pfin(N) and x E N, then 2x = I( {a + 1 I a E 8}) and 2x + 1 = I( {a + 1 I a E 8} U {O}). It follows from this that 1 is onto. If 1(8) = 0, then 8 = 0. If 1(8) = x > 0, then the least element ao of 8 is the greatest number z such that 2Z divides x evenly. If 2ao = x, then 8 = {ao}. Otherwise, we recover al, the second largest element of 8, as being the largest number z such that 2Z divides x - 2ao evenly and so forth. These considerations (made precise by an induction proof) show that 1 is one-to-one.

31. Determine the pairing functions f : N 2 - N for the numbering mechanisms suggested by Figure 5.2. 32. Let k be a natural number which is greater than 1. We generalize the pairing function of Example 5.3.15 as folIows. Let Po, ... ,Pk-2 be the first k -1 primes listed in increasing order, and let (so, SI," .) be the sequence of positive integers which are relatively prime to Po ... Pk-2 listed in increasing order. Define gk : N k - N by

gk«ao, ... , ak-I}) = p~Op~l ... P:~-22 sak_l - 1

for all (ao, .. . ,ak-d E N k. (a) Show that g2 is the pairing function f defined in Example 5.3.15. (b) Show that each gk is a bijection. 33. In this exercise, we generalize the pairing function of Example 5.3.16. Let $L denote lexicographic order on Seq(N). For each k ~ 2, we 392 5. Enumerability and Diagonalization I , , , , ----- '9~'_'_'_'_ '4~ '5="6_'_'_ '1=.'2_ ~l_'_'_ 'O_'3l_ ~l_'_'_ FIGURE 5.2. Other definitions of pairing functions.

define arelation Pk on Nk.lfa = (ao, ... ,ak-d and b = (bo, ... ,bk- 1) are two elements of N k , then we define apkb to hold if and only if either k-1 k-1 La; < Lb; ;=0 ;=0 or k-1 k-1 La; = Lb; and a ~L b. ;=0 ;=0

(a) Show that for each k ~ 2, Pk is a linear order on N k such that each element of N k has only finitely many predecessors.

For each k ~ 2, let 9k be the bijective numbering of N k obtained from Pk as in Exercise 17.

(b) Show that 92 is the pairing function defined in Example 5.3.16.

34. Let S = Uk>l (Nk -- N) and let f be a pairing function. Define a function H /: S -- S such that if {/A: I k ~ 1} are the functions associated with f then we have

for all k ~ 1. 35. Prove the following properties ofpairing functions and pairing tripies: 5.6. Exercises and Supplements 393

(a) The pairing function introdueed in Example 5.3.16 is primitive reeurSlve. (b) If f : N2 ---+ N is primitive reeursive then so are eaeh of the functions fk introdueed in Example 5.3.22. (e) If (j,PbP2) is a monotonie pairing tripie and f is primitive reeursive then both PI and P2 are primitive reeursive. We refer to sueh a tripie as a monotonie primitive reeursive pairing tripie or, for short, as an mprp-triple. Hint: Observe that for a monotonie primitive reeursive tripie we ean write

for n E N, whieh shows that PI is primitive reeursive.

36. Let f : N 2 ---+ N be a pairing function and let {In I n ~ 1} be the family of functions generated by f. Prove that

for every k, i E P. Solution: The argument is by induetion on i. For i = 1 we retrieve equality (5.3) from Example 5.3.22. Let us assurne now that the equal• ity holds for i. We have

fk+i+l(Zl,"" zk+i+d fk+i(Zlt ... , ZkH-l, f(Zk+i, zk+i+t)) = fk(Zl"" ,Zk-l,Ji+l(Zk, ... ,f(Zk+i, zk+i+t})) = f,.,(Zl"" ,Zk-l.Ji+2(Zk, ... ,Zk+i,Zk+i+t}), whieh concludes the argument. 37. Let {Iklk E P} be the eolleetion offunctions associated with a pairing function f.

(a) Show that if f = h is monotonie then so is fk for any k ~ 3. (b) Show that if hismonotonie and 1 ::; m < k then

for every Zl,'" ,Zrn E N. Solution: We give a solution for the seeond part of the problem. The argument is by induetion on k. For k = 2, we have m = 1 and the equality is trivially true. Assurne that the equality holds for k ~ 2 and eonsider m E P sueh that 394 5. Enumerability and Diagonalization

m < k + 1. We distinguish two cases: m < k and m = k. In the first case, we have fk+l(Xl,"" Xm, 0, ... ,0) = h:(Xl,'" ,Xm, 0, .. ·,12(0,0» fk(Xl,'" ,xm,O, ... ,0) fm+l(Xl,'" ,Xm,O), where the second equality follows from the monotonicity of 12. In the second case, we have a trivial equality. 38. Give a recursive definition of the uniform projection function intro• duced in Definition 5.3.23. Prove that if (f, Pl, P2) is an mprp tripie then 11' is a primitive recursive function.

Hint: Prove that for k ~ 1: 1I'(i,k,n) if i < k, 1I'(i,k+ l,n) = { Pl(1I'(k,k,n» ifi = k P2(1I'(k,k,n» if i > k.

The primitive recursiveness of 11' follows from Supplement 71 of Chap• ter 4, Definition 5.3.23, and the above recursive definition.

39. Prove the following properties of the uniform projection function 11' corresponding to an mprp tripie (f, Pl , P2): (a) For any i, k, nE N, 1I'(i, k, n) ::; n. (b) Let f : N 2 ---+ N be the function defined by

f(k, n) = fk+l(n, ... , n),

for every k, n E N. Prove that l is primitive recursive and that 1I'(i, k + l,l(k, n» = n for every i, k, nE N. Hint: The primitive recursiveness of l follows from the existence of the following recursive definition:

l(O, n) n, l(k + 1, n) f(n,l(k,n».

40. Let L : N ---+ N be the function defined by L( n) = l( n, n) for every n E N, where l is the function defined in Exercise 39. If s,t,r E Seq(N) and r is the concatenation of sand t, prove that gnseq(r) ::; L(gnseq(s) + gnseq(t».

Solution: If both sand t are the null sequence then so is rand the inequality is trivially true. Therefore, we assurne that at least one of 5.6. Exercises and Supplements 395

sand t is not null. Let U = (uo, ... , un-t) E Seq(N). If P = gnseq(u), the properties of mprp tripies allow us to infer that P ~ n and also that P ~ Uj for every j, 0 :5 j :5 n - 1. This allows us to write for s = (so, ... , Sn-t) and t = (to, ... , tm-t), where P = gnseq(s) and q = gnseq(t): gnseq(r) = !(n+m,!n+m(SO, ... ,Sn-t,to, ... ,tm-t» = !n+m+t(n + m,so,··· ,Sn-t,to, ... , tm-t} :5 !n+m+1(p+q, ... ,p+q) :5 !P+9+t(P + q, .. . ,p + q) = L(p + q).

41. Let GSEQ be the set of all Gödel numbers for sequences of natural numbers:

GSEQ = {n E NI n = gnseq(s) for some s E Seq(N)}. Prove that GSEQ is a primitive recursive set. Solution: We have n E GSEQ if pt(n) = 0 implies n = O. Con• sequently, we can write XGSEQ(n) = !_(f=(pt(n), O).!=(n, 0» for every n E N, which shows that XGSEQ is primitive recursive. 42. Prove that there is a primitive recursive function catseq : N2 - N such that if s,t E Seq(N) and gnseq(s) = p, gnseq(t) = q then catseq(p, q) is the Gödel number of the concatenation of sand t. Hint: We can define catseq using bounded minimization:

catseq(p, q) = (I'r:5 L(p + q»[xGSEQ(r) = 1 and pt(r) = Pt(p) + Pl(q) and FORALLf!~)-1(1I"(j + l,pt(p),P2(p» = 1I"(j + l,pt(r),P2(r))) and FORALLf!~9)-t(1I"(j + I,Pl(q),P2(q» = 1I"(j + 1 + Pt(p),pt(r),P2(r)))].

43. Using Theorem 5.2.14 (and hence, the axiom of choice), prove that for any set M, M is uncountable if and only if N -< M. 44. Let M be a set. Prove that if P(M) is infinite then it is uncountable. 45. Prove that (0,1) is uncountable. Conclude that every open interval (c, d) of the set R of real numbers is uncountable and R is also un• countable. Solution: We know that the set ISeq( {O, I}) of infinite sequences over {O, I} is uncountable. Let us consider the set S that consists of the following infinite sequences: 396 5. Enumerability and Diagonalization

(a) the sequences SO,SI where so(n) = 0 and sl(n) = 1 for every nEN; (b) any infinite sequence s for which there is a number n, such that s(n,) = 1 and s(m) = 0 for every m > n,. S is clearly countable. Therefore, ISeq( {O, I}) - S is uncountable (be• cause of Corollary 5.3.26). We prove that (0,1) '" ISeq({O,I}) - S by giving a bijection 1 : (0,1) --+ ISeq({O, I}) - S. If z E (0,1) then z has a unique in• finite binary expansion z = O.ZOZI." such that for every k E N there is h > k with Zh = 1. For example, 0.5 can be written in bi• nary as 0.100 .... This, of course, does not qualify as an expansion in the sense introduced above. Rather, we adopt the equivalent rep• resentation 0.011 .... We define I(z) = (ZO,ZI, ... ,Zn, ... ) for every Z E (0,1). The reader will easily verify that 1 is a bijection. Since ISeq( {O, I}) - S is uncountable, it follows that so is (0,1).

The existence of the bijection 9 : (0, 1) --+ (c, d) given by g( z) = (d - c)z + c (see Example 5.2.4) proves that (c, d) is uncountable for c < d. The uncountability of R also follows from Example 5.2.4. 46. Using the axiom of choice, show that for any family of sets {Mi li E I} indexed by an arbitrary set I, OiEI Mi is countable if and only if either

(a) there is an i E I such that Mi = 0, or (b) there is a finite subset J of I such that for every j E J, Mj is countable and for every i E I - J, Mi is a singleton. (Compare this condition with the one given in Exercise 6.) 47. (a) Using the axiom of choice, show that if Cis a disjoint collection of sets then U C is countable if and only if C is countable and every element of C is countable. (Compare this condition with the one in Exercise 7 but note that here we assurne that C is a disjoint collection of sets.) (b) Give an example of an uncountable collection C such that UC is countable. 48. Let {co, ... , cn-d be the set of all n male inhabitants of a small town. Among them, there is one barber, and the town council orders that the barber will shave all male inhabitants who do not shave them• selves and will not shave any male inhabitant who shaves hirnself. Prove that the town council ordinance is unenforceable since it puts the barber in an impossible situation. 5.6. Exercises and Supplements 397

Solution: If the barber shaves hirnself, then the ordinance says that he should not shave hirnself, but if he does not shave hirnself then the ordinance says that he should. We can use diagonalization to help understand the situation. Consider an n x n table with 0, l-entries; each row and each column correspond to one of the male inhabitants of the town. The element Sij situated in row i and column j is 1 if and only if Ci shaves Cj. Note that the element located in the diagonal of the table and in row i, Sii, equals 1 if and only if Ci shaves hirnself.

Co Cl ... Cn-l Co 1 0 ... 1 Cl 0 0 ... 0 .. . Cn-l 0 0 ... 0

Consider now the sequence b = (bo, ... , bn - l ) where, for 0 $ i $ n-l, bi = 1 if Sii = 0 and bi = 0 if Sii = 1. According to the town council ordinance, the row of the table corresponding to the barber must be b, but, by diagonalization, b is different from every row of the table, so there can be no barber in the town obeying the ordinance. (If the barber did not hirnself live in the town, the ordinance could be enforced.)

49. Consider the poset (N, $) and the set ofmonotonic functions N -m N. Prove that N -m N. is uncountable. Hint: Use a diagonalization argument.

50. Let rand t be natural numbers such that t ~ 1. The notions of partial recursive and primitive recursive function can be extended to Nr "'" Nt and Nr _ Nt, respectively, as folIows. A function f : Nr "'" Nt is partial recursive (primitive recursive) if pj f is partial recursive (primitive recursive) for every j, 1 $ j $ t.

(a) Suppose that f : NP"", Nq and 9 : Nq "'" Nr with p,q,r E N and q, r ~ 1 are partial recursive (primitive recursive). Show that 9 f : NP "'" Nr is partial recursive (primitive recursive). Definition by primitive recursion can also be extended. Namely, let r, t E N and suppose that 9 : Nr "'" Nt and h : N r +1+t "'" Nt. The function k : Nr+l "'" Nt is defined by primitive recursion from 9 and h if k(Xl,""Xr,O) g(Xl,""Xr ), k(Xl,""Xr,x+ 1) = h(Xl, ... ,Xr,x,pik(Xl,""Xr,x), ... ,P~k(Xl"" ,Xr , x)), 398 5. Enumerability and Diagonalization

for all Xl, ... ,Xm,X in N.

(b) Prove that if 9 : N r "-+ Nt and h : N r+1+t "-+ Nt with t ~ 1 are partial recursive (primitive recursive) functions and k is obtained from g, h by primitive recursion, then k is also partial recursive (primitive recursive).

If p, q, rEN and I : NP "-+ Nt, 9 : NP "-+ N r, we define (J, g) NP "-+ Nt+r by Dom( (J, g}) = Dom(J) n Dom(g) and

(J, g}(X1, ... , xp ) = I(xl. ... , xp ) • g(Xl. ... , xp ),

for (Xl. ... , xp ) E Dom(f x g).

(c) Show that if I : NP "-+ Nt, 9 : NP "-+ Nr , and h : NP "-+ Nt then ((J,g},h) = (J,(g,h}). (Thus, the operation taking land 9 to (J, g) is associative and we may use notations such as (Jo, ... , In-1) unambiguously as long as the functions all take the same number of arguments.)

(d) Let I : NP "-+ Nt and 9 : NP "-+ Nr be two partial recursive (primitive recursive) functions, where q, r ~ 1. Prove that (J, g) : NP "-+ Nt+r is partial recursive (primitive recursive).

Let I : NP "-+ Nt and 9 : Nr "-+ Nt be two partial functions. Define the function I x 9 : NP+r "-+ Nt+t by Dom(f x g) = {(Xl. ... , XP+r) I (Xl, . .. , Xp ) E Dom(f) and (X p+1, .. . , Xp +r ) E Dom(g)} and

for every (Xl, . .. , XP+r) E Dom(f x g).

(e) Prove that if I : NP "-+ Nt, 9 : N r "-+ Nt, and h : NU "-+ Nt! then I x (g x h) = (f x g) x h. (Thus, x is also associative.)

(f) Prove that if I : NP "-+ Nt and 9 : N r "-+ Nt with q, t ~ 1 are partial (primitive) recursive, then so is I x g.

Suppose that k : N r "-+ Nr . Define a function k# : Nr+1 "-+ N r by

for Xl. ... , Xr, xE N. (We use here the notation of Example 4.5.24.)

(g) Prove that if k : Nr "-+ N r with r ~ 1 is partial (primitive) recursive then so is k# . 5.6. Exercises and Supplements 399

Solution: To see (a), note that for all i with 1 ~ i ~ r we have

pi (g f) = (pi g )(P!f, ... ,p~f).

Since each projection of f and g is partial recursive (primitive re• cursive), it follows that each projection of gf is also partial recursive (primitive recursive).

In order to prove (b), consider the function K : Nr+l "-+ N defined by K = ftk, where ft : Nt --+ N is a primitive recursive bijection (as shown to exist by Exercise 35). Define the functions G : Nr "-+ N and H : N r+2 "-+ N by

G(Xl,""Xr) = ft(g(xt, ... ,xr)), H(xt, ... ,xr,y,z) = ft(h(Xl, ... ,Xr,y,1I'(1,t,z), ... ,1I'(t,t,z))), for Xl,."'Xr'y,z E N. It is easy to see that both G and H are partial recursive (primitive recursive) if g and h are. For instance, in the case of G we can write

and, since all functions p}g are partial (primitive) recursive, then so is G. The function K : Nr+l "-+ N can be defined from G and H by primitive recursion and, therefore, it is partial (primitive) recursive. Note that we have

p}k(Xl'''' ,xr,x) = 1I'(j,t,K(Xl,'" ,xr,x)), which shows (using Exercise 38) that p}k is partial (primitive) recur• sive for 1 ~ j ~ t. This implies that k is partial (primitive) recursive. Parts (c) through (f) are easy and are left to the reader.

In order to prove (g), let ir : Nr --+ Nr and h : Nr+l+r "-+ N r be given by ir(Xl,"" xr) = (Xl, ... ,Xr) and

for every Xl, ... ,Xr,X,Zl, ... ,Zr E N. Then, for each i, 1 ~ i ~ r, r' r d rh rk( 2r+l 2r+l) ., . 't' we h ave Pi Zr = Pi an Pi = Pi Pr+2"" 'P2r+l ,so Zr IS prImi Ive recursive and h is partial (primitive) recursive. Note that

k#(Xl,''''Xr,O) = ir(XI,""Xr) and k#(xt, ... ,xr,x+ 1) h(Xl" .. ,Xr, X, pi k#(Xl" .. ,Xr, x), ... ,p~k#(Xl,'" ,xr,x)), which is adefinition by primitive recursion of k# starting from ir and h. This shows that k# is partial (primitive) recursive. 400 5. Enumerability and Diagonalization

51. Given a function l : {l, ... ,n} - {l, ... ,m} define the function f.O : Nm _ Nn by

f.O(Zl, ... ,Zm) = (Zl(I)"",Zl(n»), for every (ZI,"" zm) E Nm. Prove that for every l, f.O is primitive recurSlve. Solution: Observe that for every j, 1 ::; j ::; n, the function pjf.O equals the projection Pl(j)' 52. Another way to extend the not ions of primitive recursive and partial recursive function to functions with range in Nm with m > 1 would be to give an inductive definition similar to the ones given in Defini• tions 4.9.3 and 4.9.10 for the primitive and partial recursive functions with range in N. We show here how this can be done. Let S = U{Nm - Nq Im E N and q E P}. We define a subset I of S by the following inductive definition. (1) Every initial function (as in Definition 4.9.2) is in I. (2) If 1 : NP - Nq and 9 : NP _ N r are both in I then so is (f,g) (as defined in Exercise 50). (3) If I: NP - Nq and 9 : Nq - N r are both in I then so is gl. (4) If 1 and gare both in I and h is obtained from 1 and 9 by primitive recursion (in the sense of Exercise 50) then h is in I.

Similarly, let T = U{Nm ~ Nq I m E N and q E P} and define a subset :7 ofT inductively using the same four rules as in the definition of I (but with "-" replaced by "~") plus this additional rule:

(5) If 1 : Nm+! ~ N is in 3 and 9 : Nm ~ N is obtained from 1 by minimization (as in Definition 4.9.9) then 9 is in :7. Prove that I is the set of primitive recursive functions and 3 is the set of partial recursive functions as defined in Exercise 50. Hint: First show by structural induction that every element of I is primitive recursive and every element of :7 is partial recursive. (Most of the work for this part has been done in Exercise 50.) For the converse, show by structural induction on Definitions 4.9.3 and 4.9.10 that if 1 : Nm - N is primitive recursive (g : Nm ~ N is partial recursive) then 1 E I (g E 3). Then, note that for any 1 : Nm ~ Nq with q > 0, we have 1 = (pU,··· ,p:f). 53. Using an inductive definition, it is possible to extend the not ions of primitive recursive and partial recursive function one step further to include functions in Nm ~ NO. 5.6. Exercises and Supplements 401

Let S' = U{Nm ---+ Nq I m, q E N} and let T' = U{Nm """ Nq I m, q E N}. Define subsets X' and 3' of S' and T' respectively by taking the definitions of X and 3 from the last exercise and adding to each definition the following rule.

For each n E N, p~, the (unique) function in N ß ---+ NO, is in X' (3'). Prove that X' n S = X and 3' nT = 3 (where Sand T are as in the previous exercise). (Thus, X' and 3' provide generalizations of the definitions of primitive recursive and partial recursive function given earlier.) Hint: It is immediate that X ~ X' n Sand 3 ~ 3' n T. It is also not hard to show by structural induction on the definition of 3' that every member of 3'nS is primitive recursive. Showing the remaining inclusion is trickier. To this end, if mE N and f : Nm """ NO, define t/J J : Nm """ N by

./. ( ) {1 if (Xl,"" Xm ) E Dom(f) o/J Xl,"" X m = undefined otherwise.

Show by structural induction on the definition of 3' that if f : Nm """ Nq is in 3' then f is partial recursive if q > 0 and t/J J is partial recursive if q = O.

Enumerating Programs

54. Show that the function computed by the program

loop Wo R { Wo +- W1 W1 +- W1 + I endloop

is the same as the function computed by the program R given in Example 5.4.16.

55. Prove the following assertions.

(a) Let X be a variable that does not occur in the VSPL program P. For all states s, if Mp(s) is defined then Mp(s)(x) = s(x). (b) If s,s' are two states of a VSPL program P such that s(w) = s'(w) for every w that occurs in P then Mp(s) is defined if and only if Mp(s') is defined, and if both are defined, Mp(s)(w) = Mp(s')(w) for every variable w that occurs in P. 402 5. Enumerability and Diagonalization

56. Prove that the set

INSTR = {n E NI n = d(S) for some VSPL instruction}

is primitive recursive. Hint: Note that INSTR = {n E N 11 :5 Pi(n) :5 5 and (pi(n) = 5 -+ P2(n) = On.

57. Let "I : N 2 -- N be defined by "I(i, k) = gn(Wi - k), where Wi - k was introduced in Example 5.4.12. Prove that "I is primitive recursive. Solution: The code of the statement Wi - °is 1(2, i). Therefore, for k = °we have "I(i, 0) = 1(1,/(2,i». Note that "I(i, k) = I(k + I'/k+i(/(2, i),/(I, i), ... , 1(1, i))), = I(k + 1,/(/(2, i), "c(/(I, i), ... , 1(1, i)))). Hence

This allows us to write

"I ( i, k + 1) = I(k + 2'/(1(2, i)'/(I(l, i), P2(P2("t( i, k)))))), which shows that "I is, indeed, primitive recursive. 58. Consider the functions length : N -- N, instr : N 2 -- N, var : N -- N, type: N -- N, varprog : N -- N,

defined by

length(p) = Pi(P), instr(p,i) = 1r(i,length(p),P2(p», var(m) = {P2(m) if 1 :5 ~i(m) :5 3 o otherwlse, type(m) = Pi(m) varprog(p) = max19:5length(p) var(instr(p, i»

for every p, i, mE N. Prove that: 5.6. Exercises and Supplements 403

(a) If pis the Gödel number of a VSPL program P, length(p) gives the number of statements in P. (b) If pis the code of a VSPL program P and 1 $ i $ length(p), then instr(p, i) gives the code d(8i-d ofthe i-th instruction 8i-1 of P. (c) var( m) gives the subscript of the variable v used by the instruc• tion whose code is m, where 1 $ P1(m) $ 3. (d) type(m) can be used to determine the type of the instruction having code m, as introduced in Definition 5.4.24. (e) If gn(P) = p for a nonempty VSPL program P then varprog(p) gives the largest subscript of a variable used by P. Prove that the functions length, instr, var, type, varprog are all prim• itive recursive. 59. States, environments, and configurations of VSPL programs can be encoded as numbers. The interest of such encodings resides in the pos• sibility of expressing the effect of VSPL instructions and, eventually, of VSPL programs as partial functions on N. We remind the reader that the pairing function / is assumed to be primitive recursive and, therefore, so are its associated projections P1 ,P2 and all functions / n, for n ~ 1 (see Exercise 35). To encode states and environments of VSPL programs, we need to encode both the values of the variables and the values of the counter variables. Let P be a VSPL program, where gn(P) = p. The code 0/ astate s of P is given by

gnsp(s) = fm+1 (s( wo), . .. ,s(wm », where m = varprog(p). The set of counter variables available to P is given by

{Ci 10$ i $length(p) -I}.

In order to define the code of an environment e of P, consider the sequence of values of counter variables:

ce = (e(co), ... , e(cIPI_d).

The code of e is

gnep(e) =!3(p,gnsp(s),fIength(p)(c e ),

where s is the state that corresponds to e. 404 5. Enumerability and Diagonalization

The code of a configuration C = (k, e) of P is gncp(C) = fa(gn(P), k,gnep(e».

Prove the existence of the primitive recursive functions

valvar : N 3 --+ N and valcount : N 3 --+ N

such that if p = gn(P) and n = gn(e) (where Pisa VSPL program and e is an environment of P), then valvar(p, n,i) gives the value of the variable Wj in e for °~ i ~ varprog(p), while valcount(p, n, i) will return the value of the counter variable Ci corresponding to line i, where °~ i ~ length(p) - 1. Solution: We have valvar(p, n,i) = 1r(j + 1, varprog(p) + 1, 1r(2, 3, n» and valcount(p, n, i) = 1r(i + 1,length(p), 1r(3,3, n» for every p, n, i, k that can be interpreted as above.

60. Show that for every m E N there is a primitive recursive function ugns(m) : Nm+l --+ N such that if P is a VSPL program with gn(P) = p then for all Xo, • .. ,Xm-l E N we have

ugns(m)(p, Xo, ... , Xm-l) = gnsp(s),

where S(Wi) = Xi for °~ i ~ m - 1 and S(Wi) = °when i ~ m. Solution: Let q(p) = varprog(p) + 1. We have

ugns(m)(p, Xo, ••• , xm-d = { fq(p)(xo, ... ,xq(p)-d if q(p) - 1 < m fq(p)(xo, ... ,Xm-l'O, ... ,O) ifq(p)-1 ~ m.

Let M(m) : N m+1 --+ N be the primitive recursive function given by

M(m)(p, Xo, ••• ,xm-t} = i(varprog(p), Xo + ... + xm-t}.

Using this function we then have

ugns(m)(p, Xo, ... , Xm-l) = (J.ly ~ M(m)(p, Xo, ... , xm_d)[FORALL;~~prog(p) (j < m -+ 1r(j + 1, q(p), y) = Xj) snd (j ~ m -+ 1r(j + 1, q(p), y) = 0)]

which shows that ugns(m) is primitive recursive. 5.6. Exercises and Supplements 405

61. Prove the existence ofthe following four primitive recursive functions:

varincr, varzero, countdecr : N 3 --+ N

and countset : N4 --+ N, where varincr(p, n, i) and varzero(p, n, i) give the Gödel number of the environment e' of the VSPL program P (where gn(P) = p) obtained from e (where gnep(e) = n) by applying the monus to the value of the variable Wi or after setting the value of Wi to 0, respectively when 0 :$ i :$ varprog(p). Similarly, countdecr(p, n, k) and countset(p, n, k, I) give the Gödel number of the erivironment e' obtained from e after applying monus to the value of the counter C/c or resetting C/c to I, respectively. 62. Let P be a VSPL program with gn(P) = p and e is an environment with gnep(e) = n. Prove that there is a primitive recursive function

countinstr : N 5 --+ N, such that if p = fn(rl,'" ,rn) and r/c E INSTR for 1 :$ k :$ n then countinstr(p, n, t, i, j) gives the number of instruction codes of type t (as introduced in Definition 5.4.24) that occur in the interval ri,"" rj when 1:$ i :$ j :$ n and 1:$ t :$ 5. Solution: A recursive definition of this function is given by

countinstr(p, n, t, i, 0) = 0, countinstr(p, n, t, i,j) + 1 if type( 1I"{j + 1, n,p» = t countinst,(p, n,t,;,j + 1) and i:$ j + 1:$ n !countinstr(p, n, t, i, j) otherwise which shows that countinstr is primitive recursive.

63. Prove that there are two primitive recursive functions assoe: N 2 --+ N and invassoc : N 2 --+ N such that, if p is the code of a VSPL program P, where P = So; ... ; Sm, Si. is a loop instruction and Sj is its associated endloop instruction, then assoc(p, i) = j and invassoc(p, j) = i. Solution: We have: assoc(p, i) = (IlY < length(p) + l)[type(instr(p, i» = 3, Y > i and countinstr(p2(p), length(p), 3, i + 1, y) = countinstr(p2(p), length(p), 5, i + 1, y)] ..:. 1]. 406 5. Enumerability and Diagonalization

and invassoc(p, j) = (J-lY < j)[assoc(p, y) = j], for every p, i, JEN. Here, " ..:. " is the notation introduced in Exer• eise 61 of Chapter 4 for the monus function. 64. Prove that the set GPROG of those numbers that are Gödel numbers for VSPL programs is primitive recursive. Solution: We have rE GPROG if the following conditions are sat• isfied:

PO. rE GSEQ and

FORALLf~~)-l[XINSTR(1r(j + 1,pl(r),p2(r))) = 1].

P 1. rencodes a sequence of instruction codes that contains the same number of loop instructions as endloop instructions:

countinstr(p2(r),pl(r),3, 1,pl(r» = countinstr(p2( r), Pl (r), 5,1, Pl (r».

P2. Every suffix of the sequence encoded by r contains at least as many endloop instructions as loop instructions:

FORALLf~~)-lcountinstr(p2(r),Pl(r), 3,j + 1, Pl(r» ::5 countinstr(p2( r), PI (r), 5, j + 1, PI (r».

P3. Every goto instruction that occurs in the sequence points to• wards an instruction that occurs in the sequence:

FORALLf~~)-lpl(1r(j + 1,pl(r),p2(r))) = 4 -+ (1:5 P2(1r(j + 1,pl(r),p2(r))):5 Pl(r».

GPROG is a primitive recursive set as an intersection of the four primitive recursive sets whose characteristic functions are defined by (PO)-(P3). 65. Prove that there are three primitive recursive functions

nextline : N 3 -- N nextenv : N 3 -- N follconf : N 3 -- N

such that if pis the Gödel number of a VSPL program P, n is the Gödel number of an environment e and i is the current statement of P being executed then we have: 5.6. Exercises and Supplements 407

(a) nextline(p, i, n) = j if P will continue its execution with the j-th statement. (b) nextenv(p, i, n) = m if P, after being in configuration (i,e), enters environment e' where gnep(e') = m. (c) If Pis in configuration C with gncp(C) = k, then, after m steps, the program P will be in configuration C', where gncp(C') = follconf(p, k, m). Solution: The function nextline can be defined as follows: i) nextline(p, i, n) = i + 1 if type(instr(p, i» E {l, 2}. ii) If type(instr(p, i» = 4, then nextline(p, i, n) = p2(instr(p, i».

iii) Ir type(instr(p, i» = 3, then nextline(p, i, n) = if valvar(p, n, var(instr(p, i))) > 0 { i + 1 assoc(p, i) + 1 otherwise.

iv) If type(instr(p, i» = 5, then if invassoc(p, i) = j we have r ( . ) {j + 1 if valcount(p, n,j) > 1 next me p, ',n = i + 10therwise.

v) In any other case, nextline(p, i, n) = p. The existence of this definition proves that nextline is primitive re• cursive. A primitive recursive definition of follconf can be given as follows: follconf(p, k, 0) = k, and follconf(p, k, m + 1) = !a(p, nextline(p, 11'(2, 3, k'), 11'(3, 3, k'», nextenv(p, 11'(2,3, k'), 11'(3, 3, k'))), where k' = follconf(p,k,m).

66. Prove that there exists a partial recursive function time : N2 ~ N such that time(p, m) is defined if and only if there is a VSPL program P with gn(P) = p and astate s with gnsp(s) = m such that s E Dom(Mp), and iftime(p,m) is defined, then time(p,m) = k, where k is the number of steps in the execution of P on s. Solution: Note that we can write time(p, m) = 1.m[xGPROG(p) = 1 and 11'(2,3, follconf(p, 1, n» = length(p)], 408 5. Enumerability and Diagonalization

where I = h(p,O,gnep(e,» is the code of the initial configuration that corresponds to the state s,

gnep(e,) = h(p, m, 0).

67. For every m ENdefine Um : Nm+1 ~ N such that

um(p, Z1, ... ,zm) = f~m)(z1' . .. ,zm),

if(Z1' ... ,zm) E Dom(J~m» and is undefined, otherwise. The func• tion Um is ealled the VSPL m-universal function. Show that (a) For each mE N, Um is partial reeursive. (b) For each m E N, Um ean be obtained from primitive reeursive functions by applying minimization only onee.

Solution: (a) Let (Z1' ... ' zm) E Nm. Define the state So by

( ) _ {Zi+1 if °::; i ::; m - 1 So Wi - ° 1·f m <._ z.

If gn(P) = p then ugns(m)(p, Z1, ... , Zm) = gnsp(so) and, ifwe define I: Nm+! --+ N by'

I(P,Zl, ... ,zm) = h(p,O, h(p, ugns(m)(p,Z1, ... , zm), 0» then I(p, Z1, ... , zm) gives the Gödel number of of the initial configu• ration that eorresponds to So. If f~m)(zl' . .. ,zm) is defined then the Gödel number of the final eonfiguration entered by P is given by fe(p,z1, ... ,zm) = folleonf(p, I(p, Z1, ... ,zm), time(p, ugns(m)(p, Z1, ... ,zm))).

If f~m)(z1' .. . , zm) is undefined then, fe(p, Z1, ... ,zm) is undefined. If p is not the Gödel number of a program, then fe(p, Z1, ... ,zm) is undefined. This allows us to write

Um(p,Z1, ... ,Zm) valvar(p, 11'(3, 3, fc(p, Z1, ... ,zm», 0),

for p, Z1, ... ,Zm E N whieh proves that Um is partial reeursive. (b) Note that minimization is used only onee, in the definition of time. 5.6. Exercises and Supplements 409

68. Prove that the dass of VSPL-computable functions equals the dass of partial recursive functions. Solution: Theorem 5.4.23 states that the dass of partial recursive functions is induded in the dass of VSPL-computable functions. Ex• ercise 67 implies that that every VSPL-computable function is partial recurSlve. 69. Prove the existence of a primitive recursive function

catprogs : N 2 --+ N

such that if P, Q are two VSPL prograrns with gn(P) = p and gn( Q) = q then catprogs(p, q) gives the Gödel number of the pro• gram concatenation of P and Q. Solution: Note that we cannot directly use the function catseq since, when two VSPL prograrns P and Q are concatenated, we need to modify the goto instructions of Q, according to Definition 5.4.8. Therefore, we need to define a function 1 : N2 --+ N, such that if q = gn(Q), and k E N then r = I(q, k) gives the Gödel nutnber of Q[kl. It is easy to see that if q E GPROG and if r = I(q, k) then r < l(q,J(5,q + k)). This allows us to define the function 1 by bounded minimization: I(q, k) = (J-lr $ l(q, 1(5, q + k)))[Pl(r) = length(q) and FORALL!~gth(q)-l(type (instr (q,j + 1)) = 4 ...... P2(1r(j + I,Pl(r),P2(r))) = P2(instr(q,j + 1)) + k and Pl(1r(j + I,Pl(r),P2(r))) = 4)) and (type (instr (q,j + 1)) ::p 4 ...... 1r(j + I,Pl(r),P2(r)) = instr(q,j + 1))), for q, k E N which proves that 1 is primitive recursive. We define now the function catprogs as

catprogs(p, q) = catseq(p,J(q, length(p))),

for every p, q E N. It is eIear that this function is primitive recursive. 70. Prove that for every m E P and n E N there exists a primitive recursive function 410 5. EnumerabiIity and Diagonalization

such that for every f;m+n) E FVSPL we have F(m+n)( ) Jp %1,'" ,%m, %m+1,··· ,%m+n = f!:~"(P'~l, ... ,zm)(%m+1"'" %m+n) for every %1, ... ,%m+n E N. Solution: If P f/. GPROG we define sm,n(P, %1, ... , %m) = P for every mE P amd ",%1, ... ,%m E N. The argument is by induction on m ~ 1. For m = 1 we fix the first argument %1 of the function f;1+ m). Let r = varprog(p), where p = gn(P). Consider the program R defined by

W n - W n -1 Wn -1 - Wn -2

R W2 -W1 W1 -wo Wo - %1 p[4n+zl+1]

Prior to executing R, we place the values %2, ... , %n+1 in the variables Wo, •.• , W n -1, respectively. The first " - 1 instructions shift these values into W1, ••• , W n , and eventually, Wo - %1 fixes the value of Wo to %1. Thus, we have (n)( ) f(1+ n)( ) f R %2,···,%1+n = P %1,···,%1+n, for every %2, ... , %1+n E N. If r = gn(R) we can easily express r as a function of p = gn(P) using a primitive recursive function. Let rk = gnseq(f(2,k+ 1),f(3,k),f(1,k+ 1), f(5, 0» be the Gödel number of the program WH1 - Wk for °:$ k :$ "- 1 and let ")'(0, %I) be the Gödel number of Wo - %1 (see Exercise 57). The Gödel number of R is r = gn(R) = catprogs(rn_1,(catprogs(rn_2,'" (catprogs(ro, catprogs(")'(0, %1), p))) ...))).

This proves the existence of the primitive recursive function S1,n, given by sl,n(P,%d = catprogs(rn_l, (catprogs(rn_2, ... { (catprogs(ro, catprogs(")'(O, %d,p))) ...» ifpE GPROG, p,otherwise 5.6. Exercises and Supplements 411

for every p, xl E N. Suppose now that for m ~ 1 there exist primitive recursive functions sm,n for every n E N. Since

(m+1+n )( ) f p Xl."" Zm, Zm+1, ... , Zm+l+n (1+n ) ( ) = 1I,..,1+"(P,2:1, ... ,2:,,,) Xm +1,··· ,Zm+1+n , for every Zl, ... , Zm+1+n E N, using again the existence of Sl,n we can write

which allows us to define Sm+1,n as

for every n,p, Zl, ... , Zm+1 E N. 71. The notion of function computable by a VSPL program can be ex• tended as follows. Let r, t E N, where t ~ 1. The r-argument, t• component partial function computed by a VSPL program P is the partial function I)!",t) : Nr --... Nt, whose domain is {i E Nr Isx E Dom(Mp)}, and for each i E Dom(f)!",t»), I)!",t)(i) = (Mp(sx)(wo), ... , Mp(sx)(wt-d). If gn(P) =p we denote I)!",t) by I~r,t). If pis not the Gödel number of a program, then I~r,t) is the empty function.

(a) Let r, u, v E N be such that 1 :s; u :s; v. If pr : N tl --+ N are the projection functions, 1 :s; i :s; v, prove that (r,u)(_) (ti tI}/(r,tI)(-) I p Z = P1,"" Pu p Z,

for every i E N r . (b) Let P be a VSPL program such that gn(P) = p and let j, mE N be such that m ~ max{j, varprog(p) + I}. If R is the program

loop Wj p[l] endloop prove that Ikm,m) = (f~m,m»)#t>, where l: {l, ... ,m+ I} --+ {l, ... ,m} is given by l(i) = i for 1 :s; i :s; m and l( m + 1) = j. 412 5. Enumerability and Diagonalization

72. Let VSPL - {goto} be the set of VSPL programs that do not use goto. Similarly, VSPL - {goto, loop} is the set of VSPL programs that do not use goto, loop and endloop.

(a) Let P be a sequence of VSPL instructions that does not contain any gotos and let

Lp {iIO::; i< IPI and P(i) is a loop instruction}, Kp = {iIO::; i< IPI and P(i) is an endloop instruction}.

P is a VSPL - {goto} program if and only if there exists a bijection a : Lp --+ Kp such that i::; a(i) for every i E Lp. (b) Let P be a VSPL - {goto} program. If P( i) is a loop in• struction and ap(i) = j then the body Q of the loop P(i): P( i + 1); ... ; PU - 1) is a VSPL - {goto} program. (c) Let P be a VSPL - {goto} program. If P contains a loop in• struction then P can be written as

~op Wie P= Q !endloop T,

where R is a VSPL-{goto, loop} program and Q, T are VSPL• {goto} programs.

Solution: (a) Let a be a bijection between Lp and Kp. It is dear that condition (Pi) is satisfied. Let U = P( I); ... ; P( n -1) be a suffix of P. If Lu = {i E Lpl/::; i::; n-l} and Ku = {i E Kpl/::; i::; n-l} note that ß = ar Lp is an injective mapping between Lu and Ku because of the fact that i < a( i) for every i E Lu. Therefore, ILu I ::; IK u I, which shows that P satisfies also condition (Pii). Conversely, Theorem 5.4.1 gives the existence of the desired bijection, if Pisa VSPL - {goto} program.

(b) Theorem 5.4.1 implies that the mapping a : LQ --+ KQ and given by a(l) = ap(l + i + 1) is a bijection between LQ and KQ. By part a), Q is a VSPL - {goto} program. (c) Let P(i) be the first loop that occurs in P. Define R, Q, T as

R = P(O); ... ; P(i - 1) Q = P(i + 1); ... ; PU - 1) T = PU + 1); ... ; P(n - 1), 5.6. Exercises and Supplements 413

where n = IPI and j = op(i). Note that no loop or endloop occurs in R. Therefore, R satisfies vacuously conditions (Pi) and (Pii) and, thus is a VSPL - {goto, loop} program. From b) we obtain that Q is a VSPL - {goto} program. Finally, the mapping 0' : LT ---+ ]{T, given by 0'(/) = op(1 + j + 1) for every 1 E LT is a bijection between LT and ]{T such that 1 < 0'(1) for every 1 E LT. So, by a), T is a VSPL - goto program. 73. Let P,Q be two VSPL - {goto} programs, where gn(P) = p and gn(Q) = q. Prove that 11m ,m) = I~m,m) I~m,m), where

m ~ max{varprog(p), varprog(q)} + 1

and R is the concatenation of P and Q. 74. Let P be a VSPL program, where gn(P) = p and varprog(p)+ 1 = m. Prove that if I~m,m) is partial recursive or primitive recursive then so is l~u,tJ) for every u, v E N, with v ~ 1. Solution: There are six cases to consider depending on the relative positions of u, v and m. If u ~ v ~ m we have

tJI(u,tJ)( ) m/(m,m)( 0 0) P, P Xl, ... , X u = P, P Xl, ... , X U , , ••• , ,

for every (Xl! ... , xu) E NU and for every I, 1 ~ I ~ u, which shows that l~u,tJ) is indeed primitive recursive. We leave the remaining cases to the reader. 75. Prove that the set of primitive recursive functions equals the set of functions computable by VSPL - {goto} programs. Solution: We prove initially that any function f};,m) computed by a VSPL - {goto} program P is primitive recursive. The argument is by induction on IPI. For IPI = 0, Pis the empty sequence and we have 1};,m)(X1, ... , xr) = (Xl, ... , Xm), if m ~ r or 1);,m)(X1, ... , Xr) = (Xl, ... , Xr, 0, ... ,0), if m> r, which shows that f);,m) is primitive recursive. When IPI = 1, P can be either Wj - 0 or Wj - Wj + 1. There are six possible cases to consider depending on the relative positions of the numbers r, m and j. For example, if Pis Wj - Wj + 1 and j ~ r ~ m we have

for every (Xl, ... , Xr ) E N r . Again, the verification of the primitive recursiveness is immediate. We leave the other five cases as exercises for the reader. 414 5. Enumerability and Diagonalization

Suppose now that n > 1 and the functions computed by any VSPL• {goto} program of length less than n is primitive recursive. Let P be a VSPL - {goto} program with IPI = n, gn(P) = p and m = varprog(p) + 1. If P does not contain any loop instruction, P is obtained by concatenating a one-line program Pl (which consists of its first instruction) to a program Q with IQI = n - 1. From Exercise 73 we have I(m,m) - F(m,m)/(m,m). hence I(q,q) is primitive recursive P - JQ Pi' 'P and so is I;:,t) for any r, t E N, where t ~ 1. If P contains loop instructions then, by Exercise 72 P can be written as R loop Wj P= Q !endloop T,

where R, Q, T are VSPL - {goto} programs of length less than n. By Exercises 73 and 71, if l : {I, ... , m + I} ---+ {I, ... , m} is the function given by l( i) = i for i ~ m and l( m + 1) = j we have

I~m,m) = tf!"m)(f~m,m»#r 11m ,m),

which shows, by inductive hypothesis, that I~m,m) is indeed, primitive recursive. Therefore, for all r, t E N (with t ~ 1) the functions I;:,t) are primitive recursive. In order to prove that every primitive recursive function is computed by a VSPL - {goto} program observe that the initial functions have this property, as shown in the proof of Theorem 5.4.23. Further , if we start from VSPL-{goto} programs that compute primitive recursive functions I : N n ---+ N and gi : Nm ---+ N for 1 ~ i :5 n then the program that computes their composition I(gl,'" ,gm) is, again, a VSPL - {goto} program as shown by the proof of Theorem 5.4.19. If the construction of Theorem 5.4.21 is applied to VSPL - {goto} programs that compute the functions g : Nm ---+ N and h : Nm+2 ---+ N the resulting program which computes the function obtained from g and h by primitive recursion is also a VSPL-{goto} program. This concludes our argument. Abstract Families of Functions 76. Let F be a universal AFF that contains the base functions and is closed under composition. Prove that F contains a partial function I : N "'" N such that oC Dom(f) C N. 5.6. Exercises and Supplements 415

77. Give a direct proof of the fact that for no n E P is the universal function for POLn a member of POLn +1, where POL is the AFF introduced in Example 5.5.4. 78. Derive Rice's theorem from the recursion theorem. Solution: Let F be a standard AFF and let C be a collection of functions. Assume that 0 c P(C) C N and that P(C) is an F-set. There are i E P( C) and j ~ P( C) and this allows us to consider the function h : N ~ N defined by h(x) = DU, i, XP(C)(x), 1) for every x E N. Note that h is a total function that belongs to Fl since P(C) was assumed to be an F-set. The recursion Theorem implies that h has a fixed point n, that is, lAi) = I~~~). Suppose that n E P(C), that is, lAi) E C. This gives h(n) = DU, i, XP(c)(n), 1) = DU, i, 1, 1) = j, so I~~~) ~ C, wh ich is contradictory in view of the fact that n is a fixed point for h. Similarly, the assumption n ~ P(C) also yields a contradiction, and this shows that P(C) is not a F-set. 79. Prove that for any standard AFF there is a total function h such that I~~~)(n) = IA1)(m) for every m, n E N. Show that this implies the existence of p, q E N such that ti(q) = I~l)(p).

80. Let F be an AFF and let A, B be two sets, where A, B ~ N. A is F-reducible to B if there is a function I in F l such that x E A if and only if I(z) E B for all zEN. Prove the following. (a) Ir Fis closed under composition, Ais F-reducible to B, and B is an F-set, then Aisalso an F-set. (b) The set K(F) is F-reducible to the set T(F), for any standard AFF F. (c) Ir C is a collection of functions from F 1, where F is a standard AFF, such that P(C) =F 0 and P(C) =F N, then K(F) is F• reducible to P(C).

81. Define g}n) : Nn --+ N as the function Ij(n) if Pis a VSPL - {goto} program, with gn(P) =i, and as the constant function K~, otherwise. This allows us to consider the AFF PRF = {g~n)ln E N,i E N}. It is clear, because of Exercise 75 that PRF coincides with the class of primitive recursive functions. Prove that PRF is not a universal AFF. 416 5. Enumerability and Diagonalization 5.7 Bibliographical Cornments

The main topics of this chapter, enumerability and diagonalization, are treated in references on set theory ([End77, Ham82, HJ84, Sup60]). Proofs by diagonalization in computability theory are frequently used in standard references such as [Hen77, BL74, MY79]j Section 5.5 is based on [Hen77] and [MY79].