Readings: You are expected to read and study sections 3.1 through 3.4. You do not
need to learn the definitions on page 59 and 60.
HW: due Sept 18. Write GOTO programs for the functions of problems 3,7 and 11
on page 65.
Notes on Grammars and Turing Machine Languages
Definition : A TM, M, accepts a string, w, if when started with w on the tape and the read-head on the leftmost character of w, the machine eventually halts, in a predetermined halt state, h, with nothing on the tape but Y (for yes). and the read- head resting on this Y
The language of M is the set of all strings accepted by M.
If a string is not accepted, then either it halts in some state other than H , or else halts in H with something other than Y on the tape , or else does not halt at all.
Definition( alternative defintion of accepts) A TM , M, accepts string w if it halts on input w.
If rejects w, if it never halts when started on input w.
Definition: If a language is the language of some TM, it is called a Turing machine language.
Note: It will be shown later that either definition of "accept" defines the same class of languages.
If some machine accepts some language according to the first definition of "accepts", another TM
can be found that accepts the same language in the alternative sense, and vice-versa.
Exercise. Find a TM whose language is { an b nc n | n > 0 } .
Hint: modify the machine you found for problem 3 in the first HW.
Notation; If S is a set of symbols, then S * means the set of all strings that can be made from S , including the empty string, denoted by l .
We now present an entirely different way to defineTuring machine languages.
A (context sensitive) grammar consists of the following
T : a finite set of symbols known as the terminal alphabet
We will tend to use small letters and numerals for member of T`
N : a finite set of symbols as the non-terminal alphabet.
Members of N will typically be indicated b Capital letters.
One special member of N, called the start symbol, usually denoted as S.
R: a finite set of replacement rules of the form
a ® b where a = sAt , where s ,t e T* ; A e N and b e (NÈ T)*.
That is, the right side of each rule contains exactly one non-terminal, together with
an arbitrary number (possibly zero) of terminal symbols. The right side can be any string
made up of terminals and non-terminals, including the null string.
Note: we usually specify a grammar simply by listing the rules -- One can guess the sets
N and T from the rules.
Strings are derived from a grammar by starting with the start symbol , S, and applying the
rules, replacing , in the string, right side by the left side.
The set of terminal strings that can derived from a grammar, is called its language.
A language that is the language of some context sensitive grammar is called a context sensitive language.
Theorem: Any language accepted by some TM, M, is generated by some grammar, G (That is, All TM languages are context sensitive languages)
The proof is constructive. The necessary grammar can be formed by looking at the machine.
Here is how to make the grammar:
Assume the states are indicated by capital letters ( They become the non-terminals of the grammar).
S is the start state for the TM; S is the start symbol for the grammar.
The first rule is S -> [ HY], ( assume H is the halt state.)
The other rules are determined by the Turing machine; each entry in the table determines a rule.
P Q a b R gives the rule : b Q -> Pa
P Q a b L gives all rules of the form Qxb -> xPa , one rule for each x in the tape alphabet.
P Q a b S gives Qb -> Pa
Also to simulate the potentially infinite number of blanks on a TM, the following rules are needed, where b stands for blank. ] -> b
] , b ] -> ], [b -> [ , [ -> [b , [ -> l , ] -> l ,[S ->l
The brackets simulate the ends of the active part of the tape. It is assumed that [ and ] are chosen so as not to be tape symbols.
Example showing how to find a grammar from a Turing machine

Trace of test of aaaa The Rules of the Grammar:
[bSaaaab] S ® [HY]
[bxAaaab] [b ® [
[bxxSaab] b] ® ]
[bxxxAab] [® [b
[bxxxxSb] ] ® b]
[bxxxCxb] [S ® l ; [ ® l
[bxxCxbb] xA® Sa
[bxCxbbb] xS ® Aa
[bCxbbbb] Cxb ® Sxb ; Cbb® Sbb
[Cbbbbbb] Cxb ® xCx
[HYbbbbb] Cbb ® bCx
HY ® Cb
derivation of aaaa from these rules . (I have skipped some steps.)
S
Þ [HY] Þ [HYb] Þ [HYbbbbb] Þ [Cbbbbbb] Þ [bCxbbbb] Þ [bxCxbbb] Þ [bxxCxbb] Þ [bxxxCxb] Þ [bxxxxSb] Þ [bxxxAab] Þ [bxxSaab] Þ [bxAaaab]Þ [bSaaaab] Þ [Saaaa] Þ aaaa
The language is the machine is all strings with an even number of a's (including zero)
The claim is that the grammar will generate exactly these strings and nothing else.
We next turn to proving the assertion that we made earlier when we gave two definitions of what
it means for a TM to accept a string. We asserted that either definition implies the same concept of
Turing machine language.
Theorem: Given any Turing machine, M, there is a TM, M' such that M' accepts in the first definition of "accept" exactly those string which M accepts in the alternative sense of "accepts". That is, the
the language of M' is the set of strings on which M halts.
Proof
. M' is built up from M. In order for it to be able to clean up the tape, it uses special symbols as end markers. These are chosen so that they are not in the tape alphabet
We use [ and ] to indicate these markers.
M' places these markers at each end of the tape, as follows.
L [ RB ] L [ R ----- M
All the printing on the tape is done between the end markers. To accomplish this, the machine
simply moves the markers farther out whenever they are found and adds a blank where the
marker was.
The following loops would have to be added to every state in M.
[
-----------------> BL[ R
]
-----------------> BR]L
So, after executing this extra code, M' will do exactly the same thing when it sees an endmarker that M does when it sees a blank.
Finally, M' will have its own special pre-halt state, not found in M, say h. Every missing entry in the table for M would be replaced by h ( do nothing but change to state h.)
h then cleans up by going to one marker, erasing it an coming back to the other marker , erasing every thing between, then prints Y and goes to the final, new official, halt state.
Conversely we have:
Theorem: Given TM , M, there exists a TM, M' which halts on input x iff M accepts x.
To construct M', we must make it fail to halt on any string that M rejects. We first add code to
add the brackets, [ and ] to tape and make the read head stay between the brackets.
Recall that besides failing to halt, M can reject by either halting in the some state other than the special halt state H, or else halts with something under the read head besides Y or something on the tape besides Y. We must fix M' so that is fails to halt in either of these 2 cases.
The first case can be handled by running all missing arcs from states other than H to a special state that jumps into an infinite loop. The second case is handled by making the machine go from H to code that
first checks the read head for Y and jumps into an infinite loop on every character but Y, and then checks the tape between the [ and ] , looking for something other than Y and blank. If such is found, it jumps into the infinite loop machine.
Note: This theorem, proves the assertion that we made earlier when we gave two definitions of what
it means for a TM to accept a string. We asserted that either definition implies the same concept of
Turing machine language.
A Famous Example of a set which is not accepted by any Turing machine
Notation: recall the Universal Turing machine encoding (see page 37.)
Every Turing machine can be represented by a positive integer ( but not every number is a Turing machine.)
Let Wj be defined as the language accepted by TM with code j , if j is a valid TM code.
otherwise, Wj = {0,1,2,3,4,5,....}
DEF. of the set K: K º { x || x Î Wx } .
Theorem: K is a Turing machine language.
proof:
To build a Turing machine that accepts K, first note that a Turing machine can determine whether
a code is valid . So, build such a machine and make it halt if its input string is not a valid Turing machine code., thereby accepting such input string. If the input is a valid code , the machine then copies *x behind
the input x, leaving x*x on the tape and branches to the Universal Turing machine, which then
simulates the running of machine x on input x. If this simulation halts, x is accepted . If not, x is
not accepted.
The machine accepts K, because in the case that x is not a valid code, is accepted and
xÎ K because in this case, Wx = {0,1,2,3,4..} by definition. On the other hand, if x is a valid code,
the because of the way the universal machine works, x will be accepted iff x Î Wx , which is true
iff xÎ K.
Theorem:
~K º {x | x Ï Wx } is not a Turing machine language.
proof
Either p Î Wp or p Ï Wp .
Suppose p Î Wp . Then since ~K = Wp , p Î ~K.
But then, by definition of ~K, p Ï Wp , so ~K ¹ Wp .
since p is a member of the one, but not the other.
On the other hand, if p Ï Wp, then by definition of ~K,
p Î ~K , again ~K ¹ Wp .
Therefore, in either case, ~K ¹ Wp , and so no Turing machine
accepts ~K.
The result above is referred to as "The undecidablitiy or unsolvability of the Halting Problem".
The intuitive notion of decidable appeared before the notion of Turing machines. In fact, Turing introduced the notion of Turing machine in order to give a precise mathematical characterization of decidability,
definition: A set, S, of strings from some symbol alphabet is (intuitively) decidable ( ( or recursively decidable or recursive) if there is some computable process, or program,. which , given any string in the alphabet, always halts , after returning Y if the string is in s and halts after returning N , if not.
If one believes Church's Thesis, the two definitions of decidable are equivalent.
Because of the wide acceptance of Church's Thesis, one can prove something is computable merely by exhibiting a program or intuitively computable process for it, instead of producing an actual Turing machine.
Church's Thesis also provides a
new intuitive definition of Turing machine language: A set L is a Turing machine language iff
there is some program p which given input x, halts if x Î L and which loops forever if x Ï L.
Turing machine machine language might well be called semi-decidable, because there is a program that
halts when the answer is "yes", but unlike a decision program, fails to halt when the answer is "no".
The famous halting problem is: Is the set K, just defined, decidable?
Or to put it another way: Is there a computable process for determining whether a Turing machine will halt , given itself as input?
The answer is no, and the result follows from what we already know about K together with the following: theorem.
Theorem: A set S is decidable iff both S and ~S are Turing languages.
Proof ( if) Assume S is decidable. Prove that both S and ~S are Turing machine languages.
Let M be the decision machine for S. We first modify M to build a TM that accepts S.
To do this, add code to M so that instead on halting after it prints N, it jumps into an infinite loop.
To find a machine that accepts ~M, modify it so that after printing Y , it goes into an infinite loop , instead of halting, and also change it so that it prints Y when it used to print N.
Part 2 (if) This part is hard to do on a Turing machine ,so let us give an intuitive approach first.
Assume S and ~S are both TM languages. ( We must prove that S is decidable.)
Therefore, there are Turing machines, say M and ~M that accept S and ~S respectively.
Given some string , w, the idea is to run M and ~M concurrently on w. Since w has to be in either S or ~S,
one of the 2 machines must halt. If M halts, output Y; if ~M halts, output N. This is a decision process.
To carry this process out on a Turing machine, one must combine the two machines M and ~M into on machine that alternates moves between the 2 machines. They both work on the same tape, but a marker can separate the parts of the tape used by the same machines, so that they do not interfere with each other.
The read head can alternately move between the two areas. The current states and the current positions of the read-head can be written on the tapes in the same way that this is done with the universal machine. The location of a special encoding of the current state is placed on each tape at the left of the symbol the
original machine would be on.
The undecidability of K now follows from our theorem since ~K is not a Turing machine language.
The result on the previous page can also be stated as : A set S is decidable iff both S and ~S are r.e.
Consequently, all decidable sets are r.e.
But K is an example of an r.e. set that is not decidable.
All of the sets that you are likely to think of, are decidable:
Try to find decision processes for these sets of natural numbers:
Any given finite set(evens}
{odds}
( multiples of any given m}
{primes}
{squares}
{cubes}
{all p powers for any fixed p}
{perfect numbers} ( A perfect number is one that is equal to the sum of its proper divisors, such as 6 = 1+2+3 and 28 = 1 + 2 + 4 + 7 + 14 = 28.
{abundant numbers} sum of the proper divisors exceed the number.
{deficient numbers}
{numbers that can be expressed as the sum of two primes}
Goldbach's famous unproven conjecture is that the set above is the set of even numbers.
{x | x is the (code number) of a person who will die today}.
Note: this result shows the undecidability of the halting problem
definition: A set, S, of strings from some symbol alphabet is decidable ( or recursively decidable or recursive) if there is Turing machine , M. which , given any string in the alphabet, always halts in some special state,H,after printing Y (for yes) if the string is in S and N (for no) if the string is not in S.
M is called the decision machine for S. So, one says that a set if decidable iff it has a decision machine.
Church's Thesis: Any intuitively computable process can be simulated by some Turing machine( or any of several models of computation that have been shown to be equivalent to Turing machines. This statement is a thesis ( not a theorem) because it can never be proved. It is a statement of belief about what it means to be intuitively computable. However, there is strong evidence that it is true. The evidence is that all known models of computation either compute the same functions and accept the same languages as do Turing machines or else a subset thereof., being less powerful than Turing machines. No one has every conceived of any computable process that can not be done on a Turing machine, subject to suitable encoding.
The intuitive notion of decidable appeared before the notion of Turing machines. In fact, Turing introduced the notion of Turing machine in order to give a precise mathematical characterization of decidability,
definition: A set, S, of strings from some symbol alphabet is (intuitively) decidable ( ( or recursively decidable or recursive) if there is some computable process, or program,. which , given any string in the alphabet, always halts , after returning Y if the string is in s and halts after returning N , if not.
If one believes Church's Thesis, the two definitions of decidable are equivalent.
Because of the wide acceptance of Church's Thesis, one can prove something is computable merely by exhibiting a program or intuitively computable process for it, instead of producing an actual Turing machine.
The famous halting problem is: Is the set K, just defined, decidable?
Or to put it another way: Is there a computable process for determining whether a Turing machine will halt , given itself as input?
The answer is no, and the result follows from what we already know about K and the following:
Theorem: A set S is decidable iff both S and ~S are Turing languages.
Proof ( if) Assume S is decidable. Prove that both S and ~S are Turing machine languages.
Let M be the decision machine for S. We first modify M to build a TM that accepts S.
To do this, add code to M so that instead on halting after it prints N, it jumps into an infinite loop.
To find a machine that accepts ~M, modify it so that after printing Y , it goes into an infinite loop , instead of halting, and also change it so that it prints Y when it used to print N.
Part 2 (if) This part is hard to do on a Turing machine ,so let us give an intuitive approach first.
Assume S and ~S are both TM languages. ( We must prove that S is decidable.)
Therefore, there are Turing machines, say M and ~M that accept S and ~S respectively.
Given some string , w, the idea is to run M and ~M concurrently on w. Since w has to be in either S or ~S,
one of the 2 machines must halt. If M halts, output Y; if ~M halts, output N. This is a decision process.
To carry this process out on a Turing machine, one must combine the two machines M and ~M into on machine that alternates moves between the 2 machines. They both work on the same tape, but a marker can separate the parts of the tape used by the same machines, so that they do not interfere with each other.
The read head can alternately move between the two areas. The current states and the current positions of the read-head can be written on the tapes in the same way that this is done with the universal machine. The location of a special encoding of the current state is placed on each tape at the left of the symbol the
original machine would be on.
The undecidabiliy of K now follows from our theorem since ~K is not a Turing machine language.
Enumeration machines and recursively enumerable sets
DEF.An enumeration Turing machine for a set of strings, S, is a machine, such that if started on a blank string, in state 1, it prints the members of S, one after the other on the tape, separated by some special symbol such as *.
Obviously if S infinite, an enumeration machine for it would never halt. However, even if S is infinite, the machine need not halt -- it just stops printing anything new after awhile. Indeed, it can proven that there is no
computable procedure that given any enumeration program, can determine it
it will halt. The set of programs that produce finite output is undecidable.
Def. A set of strings , S, is called recursively enumerable (r.e.) [ or computably enumerable] iff there is some enumeration machine for it.
Intuitively, if we accept Church's thesis, a set is r.e. iff there is some
program or computable process that prints all the member of S, one at a time. (i.e., there is some computable enumeration procedure for S.)
One rarely bothers with constructing an actual Turing machine in order to show that some set is r.e. -- it normally suffices to find some intuitively computable method for enumerating the set.
Theorem: A set is r.e. iff it is a Turing machine acceptable language
Proof:
(if) assume that S a Turing machine language, accepted by machiine, M.
We need to describe an enumeration procedure for S. We do it informally and appeal to Church's thesis. The idea is to test all the strings in the alphabet of the machine and print those that are accepted. So first develop a program would eventually enumerate all the strings in the given alphabet. The natural way to do this is in numerical order, as if the symbols were all digits. The base is the number of symbols in the alphabet. Example ,f the alphabet is {a,b,c}, the order would be a,b,c,aa,ab,ac,ba,bb,bc,ca,cb,cc,aaa,aab,aac ,...
To find the next string, add 1, in the given base.
You can not simply test each string in order -- Why not?
What is needed is a time slice so that each pass takes only finite time.
algorithm for enumerating S:
N := 1
LOOP
Test each of the first n strings for n steps each.
If M halts in n steps when started on any string w ,then output w
N := 2*N
END LOOP
This works because if any string w is accepted by M, M will halt in some number of steps, say m. w will be the kth string for some k, so when N > max(k,m) , w will be printed out.
It takes a lot of work to build a TM that does this algorithm, but it has been proven possible. The tape would have to be divided into sections by markers. N would be stored in one section. Another would contain a step counter and the machine would be modified to advance the step counter. Any section would contain the string to be tested. etc.
(only if) assume S is r.e. and build a TM that accepts it. Again, we appeal to Church's thesis and merely sketch how the machine would work,
Given an input string, w, to test, the machine begins enumerating S, to the right of S and each time a new string is produced, it tested to see if it matches w. If it does, the machine halts. Otherwise, it keep running forever.
Thus the concepts of Turing machine language and r.e. sets are the same.
A Turing machine language is also just the domain of some Turing computable function in the following sense. To use a TM to compute a function, load the input on the tape and run it. If it halts, the answer is on the tape. If it does not halt, the function is not defined for that input. The set of strings for which it is defined is called the domain of a function. Thus every TM computes a function and the domain of this function is a TM language, that is , the language accepted by the machine.
If you accept Church's thesis, one concludes that an r.e. set or Turing machine accepted language is just the domain of some computable function and conversely. Since ~K is not r.e., we conclude that no computable function has ~K for its domain.
About Godel numberings
The listing W0 ,W1, W2, ..... is a called a Godel numbering , this time of the the r.e. sets.
j is called the index of Wj .
Earlier, we defined the subscripts to be (encoded) Turing machine and made a special definition:
in the case that v is not the number for any Turing machine , namely , Wv = { 0,1,2,3,...} ( one can think of the numbers are being codes for string.). To avoid this, one can remove the invalid Turing machine coded
from the list and renumber. Thus Wj now means the language of the jth Turing machine code, in
numerical order. Most importantly, since a Turing machine can determine whether a number is a valid TM code, a program can still find the actual TM from the renumbered index. Further, since one can uniformly
(computably) find , given any Turing machine, an enumeration machine, for the language of the Turing machine, one can consider j to be a program for enumerating the member of Wj . There are many ways
to number the r.e. sets: the idea of a Godel numbering is that the index is a program for enumerating the set Wj . Further this program can be translated uniformly into an executable program: there is a computable function, U(j,x) which returns the output of program j, running on input x, if there is output .
Also worth mentioning: we can define a Godel numbering of function by letting, f j º the function computed by Turing machine , number j. The listing f 0, f 2, f 3, ... is thus a Godel numbering of all the computable functions. The index, j, is a program for the function f j .
Until otherwise stated, we will think of strings as being natural numbers and think about r.e. and decidable sets of natural numbers.`
Prop: All finite sets are decidable.
Proof: ?
Prop: If D is decidable, ~D is decidable.
Prop: If C and D are both decidable, then C È D is decidable
Prop If C and D are both decidable, then C.Ç D is decidable.
Prop. The union and interesection of r.e. sets is r.e. but the complement need not be.
K is r.e., but ~K is not r.e.
All finite r.e. sets are decidable since all finite sets are decidable.Prop: An infinite set S, is decidable iff it can be enumerated in natural order.
proof (if) A decision process for S is:
given x, enumerate S in order until either x is found or something bigger than x is found.
Output "yes" in the first case and "no" in the second case.
Q: why must the enumeration be in order for this to work?
(only if) given a decision process for S, test the numbers 0,1,2,3,... in order. Whenever the process returns "yes", output the number. The result is an enumeration in order.
Corollary: Every infinite r.e. set contains an infinite decidable subset.
Corollay: Every infinitie decidable set can be split into 2 disjoint infinitie decidable subsets.
Cor.: Every infinitie decidable set can be split into any given number of infinite, pairwise disjoint decidable subsets.
The next result shows that there are infinitely many r.e. sets that are not decidable.
Prop: Every infinite r,e, set ,S, contains a subset which is r.e. but not decidable.
Choose an enumeratiion method for S and let a0, a1, a2, a3, ...
be this enumeration.
Define B = { aj | j Î K } where K is any undecidable, r.e. set.
B is r.e. since K and S are both r.e.
Enumeration method for B.
Loop
Enumerate next member of K, say j
Enumerate S until aj appears, the print aj
End Loop
B is not decidable.
proof :
suppose we did have a decision procedure, d(x) for B,
We could then use it to create a decision procedure, k(x) for K as follows:
Given x, to compute k(x),
enumerate S to find ax .
call d(ax )
If "yes" is returned then output "yes"
If "no" is returned then output "no"
Therefore, since no decision procedure for K can exist, we conclude that d(x) can not exist.