You are on page 1of 14

Mathematics for Computer Science

Todays lecture

Lecture 3: Turing Machines

Areces, Blackburn and Figueira


TALARIS team
INRIA Nancy Grand Est
Contact: patrick.blackburn@loria.fr

Course website:
http://www.loria.fr/~blackbur/courses/math

The main purpose of todays lecture is to introduce Turing


machines, a simple, abstract and (as we shall see) general
model of computation.

But although Turing machines are abstract, they reflect


certain very concrete ideas about what it is to compute.
Indeed, such ideas have long inspired others to create
machines of various kinds to compute, as we shall now see.

One of the most important skills you will learn today is


how to write simple Turing machines. This skill is
important for at least two reasons. First, it will teach you
some basic programming skills. Second, it will help you to
understand and appreciate just how simple and general
Turings model of computation is.

Areces, Blackburn, Figueira: Mathematics for Computer Science

Roman abacus

Chinese Abacus above (Japanese below)

It is unclear whether the Roman abacus influenced the Chinese


abacus or vice-versa (Rome and China were in contact via trade
at the time). What is clear is that the Roman abacus was far
slower than the Chinese one and much faster than hand
calculation with Roman numerals.
Areces, Blackburn, Figueira: Mathematics for Computer Science

c
INRIA

The Chinese abacus was first mentioned around 190 AD. They
became widely used around 1200 AD. Note the two rows of
count 5 beads.

c
INRIA

Areces, Blackburn, Figueira: Mathematics for Computer Science

Japanese Abacus (Soroban)

c
INRIA

Effectiveness of the abacus


On November 12, 1946, a contest was held in Tokyo to compare
the Japanese soroban with an electric calculator. The soroban
was operated by Kiyoshi Matsuzaki, the calculator by US Army
Private Thomas Nathan Wood. The contest compared speed
and accuracy for all four basic arithmetic operations (that is,
addition, subtraction, division and multiplication) and one
problem combined all four operations. The soroban won 4 to 1
(the electric calculator won the multiplication context).
This contest formed the basis for a science fiction short story by
Arthur C. Clarke in which teams of abacus operators
successfully take over the functions of a broken computer on a
spacecraft!

The Japanese adopted the abacus from the Chinese around the
fifteenth or sixteenth century. They dropped one of the row of
the count 5 beads, yielding the modern Soroban abacus, still
used in modern Japan.
Areces, Blackburn, Figueira: Mathematics for Computer Science

c
INRIA

Areces, Blackburn, Figueira: Mathematics for Computer Science

Inca abacus

c
INRIA

Gottfried Leibniz

Gottfried Leibniz (16461716), philosopher, inventor of calculus,


pioneer of symbolic logic, and general all-round genius, was also
interested in the idea of mechanical calculation.
As he put it: It is unworthy of excellent men to lose hours like
slaves in the labor of calculation, which could be safely
relegated to anyone else if machines were used.

Inca (12001532) calculator. There are several ideas about how


it worked, but no real certainty.
Areces, Blackburn, Figueira: Mathematics for Computer Science

c
INRIA

Areces, Blackburn, Figueira: Mathematics for Computer Science

c
INRIA

Stepped Reckoner (I)

Stepped Reckoner (II)

In 1671, Leibniz started working on a machine, the Stepped


Reckoner, that could perform the four basic arithmetical
operations. He gradually improved over the years. The idea
attracted a lot of interest, and led to Leibniz being elected a
member of the Royal Society in 1673.
Areces, Blackburn, Figueira: Mathematics for Computer Science

A number of versions of the machine were constructed in


Leibnizs lifetime by a Hanover-based craftsman. However the
Leibnizs ideas pushed the technology of the time to its limits:
the machine was prone to jamming and malfunctioning.
c
INRIA

Areces, Blackburn, Figueira: Mathematics for Computer Science

The Turk

Charles Babbage

The idea of computing over other domains than numbers also


seems to have been in the air. Chess was an obvious choice.
Between 17701854, the Turk, a mechanical chess player, took
on all comers. Famous victims include Napoleon Bonaparte and
Benjamin Franklin. In later incarnations it had a voice box, and
uttered the word Check!. The Turk was destroyed by fire in
1854.

English pioneer of computer science and sadly, too far in


advance of his time to realise his ideas,

Sadly, three years later, it was revealed to have been a fake; a


human being concealed inside operated it.
Areces, Blackburn, Figueira: Mathematics for Computer Science

c
INRIA

c
INRIA

Areces, Blackburn, Figueira: Mathematics for Computer Science

A forerunner of more than mere computer science . . . !

c
INRIA

The Difference Engine (I)

Heres what he wrote to Tennyson (the English poet) about


Tennysons poem The vision of sin poem:
In your otherwise beautiful poem, one verse reads,
Every moment dies a man, Every moment one is
born . . . If this was true, the population of the world
would be at a standstill. In truth, the rate of birth is
slightly in excess of that of death. I would suggest:
Every moment dies a man, Every moment 1 1/16 is
born. Strictly speaking, the actual figure is so long I
cannot get it into a line, but I believe the figure 1 1/16
will be sufficiently accurate for poetry.

Babbages first project was the Difference Engine. He conceived


of it as a special-purpose mechanical digital calculator, designed
to tabulate polynomial functions; he proposed it in an 1822
paper to the Royal Astronomical Society, entitled Note on the
application of machinery to the computation of very big
mathematical tables. Such tables were of immense important
at the time, for example for navigation and gunnery.

Geeks rule!
Areces, Blackburn, Figueira: Mathematics for Computer Science

c
INRIA

Areces, Blackburn, Figueira: Mathematics for Computer Science

The Difference Engine (II)

c
INRIA

The Analytical Engine

This Difference Engine was designed to use the ordinary base 10


number system (that is, ordinary decimal arithmetic) and to be
powered by turning a handle.

The analytical engine was to have been powered by a


steam engine; if it had been built, would have been over 30
metres long and 10 metres wide.

The British government initially financed the project, but


stopped when Babbage repeatedly asked for more money when
he had made no apparent progress. The Difference Engine was
never finished, but was later reconstructed by the Science
Museum in London (the picture on the previous slide is of this
reconstruction).

The input was to have been via punched cards (such cards
were already being used in mechanical looms).

There were to be a variety of possible outputs: a printer, a


curve plotter, a bell(!) and a card punch.

It was designed for ordinary base 10 arithmetic.

It could be programmed. Indeed, the Analytical Engine


seems to have been comparable in power with the
programmable calculators marketed by Texas Instruments
and Hewlett Packard in the 1980s.

The reason for Babbages failure to finish the machine was his
all-consuming enthusiasm for a more general project (closer to a
modern computer) that he called the Analytical Engine.
Areces, Blackburn, Figueira: Mathematics for Computer Science

c
INRIA

Areces, Blackburn, Figueira: Mathematics for Computer Science

c
INRIA

The Mill of the Analytical Engine

Ada Lovelace the first programmer

Ada Lovelace (18151857), daughter of Lord Byron, was a was


a passionate advocate for the analytical engine and the
potential of computing. A mathematician, in 1842 she
translated an Italian treatise on the Analytical Engine into
English, and added a program for calculating Bernoulli
numbers. Babbage called her The enchantress of numbers.
The programming language Ada is named after her.

This is the only part of the Analytical Engine which was ever
built. Called the mill, which did the four basic arithmetic
operations, comparisons, and square roots.
Areces, Blackburn, Figueira: Mathematics for Computer Science

c
INRIA

Areces, Blackburn, Figueira: Mathematics for Computer Science

Practical computing

Holleriths fundamental patent

Leibnizs Stepped Reckoner and Babbages Analytical


Engine were visionary and outstripped the technology of
their time.

But much existing technology (for example, the punched


card) certainly was ready to be exploited for more
down-to-earth computing tasks.

One such task was tabulation counting and classifying


large amounts of data for statistical purposes. The need for
speedy methods of doing this was clear by the end of the
19th century.

Hollerith designed and built a number of machines for this


purposes, building on and extending existing technology.

Areces, Blackburn, Figueira: Mathematics for Computer Science

The herein-described method of compiling statistics,


which consists in recording separate statistical items
pertaining to the individual by holes or combinations of
holes punched in sheets of electrically non-conducting
material, and bearing a specific relation to each other
and to a standard, and then counting or tallying such
statistical items separately or in combination by means
of mechanical counters operated by electro-magnets the
circuits through which are controlled by the perforated
sheets, substantially as and for the purpose set forth.

c
INRIA

Areces, Blackburn, Figueira: Mathematics for Computer Science

The Hollerith Machine

Such work led eventually to other impressive technical


developments. For example, the Columbia machine combined
fast tabulation with the ability to perform arithmetic
calculations.

c
INRIA

Areces, Blackburn, Figueira: Mathematics for Computer Science

Journalistic description of the Columbia Machine

c
INRIA

How we calculate

Indeed, the Columbia-machine seemed like science fiction to


some writers at the time. But behind the journalistic
exaggerations, it was clear that technology needed to exploit
the theoretical work of Turing and his colleagues was almost in
place.
Areces, Blackburn, Figueira: Mathematics for Computer Science

c
INRIA

Columbia Machine

Holleriths company, the Tabulating Machine Company, built


and leased a number of such machines to the US Government.
His company was one four that eventually merged to form IBM.
Areces, Blackburn, Figueira: Mathematics for Computer Science

c
INRIA

c
INRIA

So what is a Turing machine, and how did the concept


arise?

Turing thought hard about what was involved in carrying


out simple calculations, such as adding a column of
numbers, or multiplying two large numbers together.

He saw clearly that when performing such calculations, in


essence we are carrying out simple algorithms. That is, we
are following, step by step, a series of rules.

For example, when adding two columns of numbers we


learn to add them from the rightmost digit to leftmost, and
to carry numbers. When multiplying two numbers
greater than 10 we learn to do it digit by digit, to add in
extra zeroes, and to obtain the final result by addition.

Areces, Blackburn, Figueira: Mathematics for Computer Science

c
INRIA

Algorithms are everywhere


I
I

From algorithms to machines

Following the steps in a recipe book, or following a knitting


pattern, are two good examples of everyday algorithms.
Indeed, much of the mathematics we learn in school isnt
what a mathematician would regard as mathematics at all;
rather, its learning certain algorithms for certain tasks.
Early in school we learn algorithms for carrying out
addition, multiplication, division, or finding greatest
common divisors (using the famous Euclidean algorithm);
only much later, probably at university, do we learn the
deeper concepts of number theory.
Later in school we may learn simple calculus. But at school
this is mostly about learning simple algorithms for
differentiating and integrating simple functions such as
polynomials; only later (at university) so we learn the
underlying ideas of continuity, differentiability, and so on.
c
INRIA

Areces, Blackburn, Figueira: Mathematics for Computer Science

But how are algorithms actually realised in the physical


world? How can we design machines to really carry them
out?

Turing answered this question by thinking carefully about


what is involved when a person carries out calculations.

c
INRIA

Turing machines

Turing saw that performing an algorithm required some


sort of workspace a place to write the input, the output,
and the intermediate results. (For a human, such a
workspace might be a sheet of paper.)

He also saw that there needed to be clearly-defined finite


number of simple operations that could be performed; for
example, how to add two digits, or how to multiply two
digits.

Last but not least, he abstracted the notion of state.


Internal states are the simple model of mind that lie
behind Turings design.

Putting all together, a Turing machine has three components:


I An unending tape divided into squares. The tape is the
workspace. Input, output, and intermediate calculations
are written on the tape. We think of the tape as infinite
both to the right and to the left (if you prefer: imagine
that we always have the possibility of adding more squares,
at either end, should we need them).
I There is a read/write head (or simple, head) which moves
along the tape. The head is restricted in its movement; it
can only move one square left or right at a time. The head
can read the symbol (if any) that is written on the tape,
and write one of a finite number of symbols on the tape.
I A Turing machine can be in one of a fixed finite number of
internal states. The ability of a Turing machine to enter
different internal states is the real key to its power.

c
INRIA

Areces, Blackburn, Figueira: Mathematics for Computer Science

Areces, Blackburn, Figueira: Mathematics for Computer Science

Writing Turing machines

A Turing machine starts by scanning the input symbols


which are written on the tape. We say a Turing machine is
in standard configuration if it is scanning the leftmost
symbol of all the input symbols.

We write a Turing machine by saying what action the


Turing machine is to perform when scanning a certain
symbol in a certain state, and by saying what state it goes
into after it has performed that action.

c
INRIA

Areces, Blackburn, Figueira: Mathematics for Computer Science

Reading
$
x
#
$
#

There are three basic things a Turing machine can do: it


can move one square to the left, move one square to the
right, or write a new symbol on the square it is reading
(overwriting what is already there; note that we regard the
blank square as a symbol).

We also need to say what state the machine moves into


after performing the action. This might be the state it was
already in, or a new state.

A Turing machine halts if no action is defined for the state


it is in. Such a state is called a final state, or a halting
state.

If a Turing machine halts scanning the leftmost symbol of


its answer, we say it halts in standard configuration.
c
INRIA

What does this machine do?


x

Write
$

Areces, Blackburn, Figueira: Mathematics for Computer Science

An example Turing machine


$

c
INRIA

What actions can Turing machines perform?

InState
1
1
1
2
2

The idea of an algorithm, the step by step following of a


sequence of precise rules, is one of the fundamental ideas of
computer science.

Areces, Blackburn, Figueira: Mathematics for Computer Science

Turings analysis

...

...
Move
R
L
L
R

NewState
1
1
2
2
3

Heres an example machine. We see a blank tape, the


read/write head (the red arrow), and the table which says what
the machine is to do when in a given state scanning a given
symbol. Note: to prevent ambiguity, in the table we represent
the symbol blank square by #.
Areces, Blackburn, Figueira: Mathematics for Computer Science

c
INRIA

This machine is designed to start in standard configuration


scanning a tape containing a mixture of the symbols $
and x.

The head moves along the tape from left to right, and
changes every x it finds to a $ (that is, it overwrites
every x with a $ symbol).

When it reaches the first blank square to the right of the


input, it moves left all the way back to the first blank to
the left of the input. It then moves one square to the right
(so it is scanning the left most dollar symbol) and halts in
standard configuration.

Fine but how exactly does it do this? Lets work


through very carefully and see. . .

Areces, Blackburn, Figueira: Mathematics for Computer Science

c
INRIA

Turn Xs to Dollars!
...

Example input

$ x

InState
1
1
1
2
2

Reading
$
x
#
$
#

Write
$

...

...
Move
R
L
L
R

InState
1
1
1
2
2

NewState
1
1
2
2
3

c
INRIA

Areces, Blackburn, Figueira: Mathematics for Computer Science

Reading
$
x
#
$
#

Write
$

...
Move
R
L
L
R

NewState
1
1
2
2
3

c
INRIA

Areces, Blackburn, Figueira: Mathematics for Computer Science

Configuration 1

What to do. . . ?
$ x

InState
1
1
1
2
2

And heres some typical input. What happens next? Well, lets
assume we start in State 1

Heres the machine again. . .

...

$ x

Reading
$
x
#
$
#

Write
$

...
Move
R
L
L
R

...

NewState
1
1
2
2
3

$ x

InState
1
1
1
2
2

blah blah blah

Reading
$
x
#
$
#

Write
$

...
Move
R
L
L
R

NewState
1
1
2
2
3

blah blah blah

c
INRIA

Areces, Blackburn, Figueira: Mathematics for Computer Science

c
INRIA

Areces, Blackburn, Figueira: Mathematics for Computer Science

There!

Configuration 2

...

$
InState
1
1
1
2
2

Reading
$
x
#
$
#

Write
$

...
Move
R
L
L
R

...

NewState
1
1
2
2
3

InState
1
1
1
2
2

blah blah blah

c
INRIA

Reading
$
x
#
$
#

Write
$

...
Move
R
L
L
R

NewState
1
1
2
2
3

c
INRIA

Areces, Blackburn, Figueira: Mathematics for Computer Science

What to do. . . ?

There. . . !
$

InState
1
1
1
2
2

blah blah blah

Areces, Blackburn, Figueira: Mathematics for Computer Science

...

Reading
$
x
#
$
#

Write
$

...
Move
R
L
L
R

...

NewState
1
1
2
2
3

InState
1
1
1
2
2

blah blah blah

Areces, Blackburn, Figueira: Mathematics for Computer Science

Reading
$
x
#
$
#

Write
$

...
Move
R
L
L
R

NewState
1
1
2
2
3

blah blah blah

c
INRIA

Areces, Blackburn, Figueira: Mathematics for Computer Science

c
INRIA

Configuration 3
...

What to do. . . ?
$

InState
1
1
1
2
2

Reading
$
x
#
$
#

Write
$

...
Move
R
L
L
R

...

NewState
1
1
2
2
3

$
InState
1
1
1
2
2

blah blah blah

Reading
$
x
#
$
#

Write
$

...
Move
R
L
L
R

NewState
1
1
2
2
3

blah blah blah

c
INRIA

Areces, Blackburn, Figueira: Mathematics for Computer Science

c
INRIA

Areces, Blackburn, Figueira: Mathematics for Computer Science

There!

Configuration 4

...

$
InState
1
1
1
2
2

$ $

Reading
$
x
#
$
#

Write
$

...
Move
R
L
L
R

...

NewState
1
1
2
2
3

InState
1
1
1
2
2

blah blah blah

c
INRIA

$ $

Reading
$
x
#
$
#

Write
$

...
Move
R
L
L
R

...

NewState
1
1
2
2
3

$
InState
1
1
1
2
2

blah blah blah

Write
$

...
Move
R
L
L
R

NewState
1
1
2
2
3

c
INRIA

$ x

Reading
$
x
#
$
#

Write
$

...
Move
R
L
L
R

NewState
1
1
2
2
3

blah blah blah

c
INRIA

Areces, Blackburn, Figueira: Mathematics for Computer Science

c
INRIA

Areces, Blackburn, Figueira: Mathematics for Computer Science

Configuration 5

What to do. . . ?
$

InState
1
1
1
2
2

Reading
$
x
#
$
#

There!
$

...

$ $

Areces, Blackburn, Figueira: Mathematics for Computer Science

What to do. . . ?

InState
1
1
1
2
2

blah blah blah

Areces, Blackburn, Figueira: Mathematics for Computer Science

...

$ x

Reading
$
x
#
$
#

Write
$

...
Move
R
L
L
R

...

NewState
1
1
2
2
3

InState
1
1
1
2
2

blah blah blah

Areces, Blackburn, Figueira: Mathematics for Computer Science

$ x

Reading
$
x
#
$
#

Write
$

...
Move
R
L
L
R

NewState
1
1
2
2
3

blah blah blah

c
INRIA

Areces, Blackburn, Figueira: Mathematics for Computer Science

c
INRIA

There!

Configuration 6

...

$
InState
1
1
1
2
2

Reading
$
x
#
$
#

Write
$

...
Move
R
L
L
R

...

NewState
1
1
2
2
3

InState
1
1
1
2
2

blah blah blah

c
INRIA

Reading
$
x
#
$
#

Write
$

...
Move
R
L
L
R

...

NewState
1
1
2
2
3

$
InState
1
1
1
2
2

blah blah blah

Write
$

...
Move
R
L
L
R

NewState
1
1
2
2
3

c
INRIA

Reading
$
x
#
$
#

Write
$

...
Move
R
L
L
R

NewState
1
1
2
2
3

blah blah blah

c
INRIA

Areces, Blackburn, Figueira: Mathematics for Computer Science

c
INRIA

Areces, Blackburn, Figueira: Mathematics for Computer Science

Configuration 7

What to do. . . ?
$

InState
1
1
1
2
2

Reading
$
x
#
$
#

There
$

...

Areces, Blackburn, Figueira: Mathematics for Computer Science

What to do. . . ?

InState
1
1
1
2
2

blah blah blah

Areces, Blackburn, Figueira: Mathematics for Computer Science

...

Reading
$
x
#
$
#

Write
$

...
Move
R
L
L
R

...

NewState
1
1
2
2
3

$
InState
1
1
1
2
2

blah blah blah

Reading
$
x
#
$
#

Write
$

...
Move
R
L
L
R

NewState
1
1
2
2
3

blah blah blah

c
INRIA

Areces, Blackburn, Figueira: Mathematics for Computer Science

c
INRIA

Areces, Blackburn, Figueira: Mathematics for Computer Science

There!

Configuration 8

...

$
InState
1
1
1
2
2

Reading
$
x
#
$
#

Write
$

...

Move
R
L
L
R

...

NewState
1
1
2
2
3

InState
1
1
1
2
2

blah blah blah

Areces, Blackburn, Figueira: Mathematics for Computer Science

Reading
$
x
#
$
#

Write
$

...

Move
R
L
L
R

NewState
1
1
2
2
3

blah blah blah

c
INRIA

Areces, Blackburn, Figueira: Mathematics for Computer Science

c
INRIA

What to do. . . ?
...

There!
$

InState
1
1
1
2
2

Reading
$
x
#
$
#

Write
$

...

Move
R
L
L
R

...

NewState
1
1
2
2
3

InState
1
1
1
2
2

blah blah blah

c
INRIA

Reading
$
x
#
$
#

Write
$

...
Move
R
L
L
R

...

NewState
1
1
2
2
3

c
INRIA

Write
$

...
Move
R
L
L
R

NewState
1
1
2
2
3

c
INRIA

Reading
$
x
#
$
#

Write
$

...
Move
R
L
L
R

NewState
1
1
2
2
3

c
INRIA

Areces, Blackburn, Figueira: Mathematics for Computer Science

Configuration 11

Configuration 12
$

$ $

Reading
$
x
#
$
#

Write
$

...
Move
R
L
L
R

...

NewState
1
1
2
2
3

$
InState
1
1
1
2
2

blah blah blah

Reading
$
x
#
$
#

Write
$

...
Move
R
L
L
R

NewState
1
1
2
2
3

blah blah blah

c
INRIA

Areces, Blackburn, Figueira: Mathematics for Computer Science

c
INRIA

Areces, Blackburn, Figueira: Mathematics for Computer Science

Configuration 13

Configuration 14
$ $

InState
1
1
1
2
2

blah blah blah

Areces, Blackburn, Figueira: Mathematics for Computer Science

...

$
InState
1
1
1
2
2

blah blah blah

InState
1
1
1
2
2

Reading
$
x
#
$
#

Configuration 10
$

...

Areces, Blackburn, Figueira: Mathematics for Computer Science

Configuration 9

InState
1
1
1
2
2

blah blah blah

Areces, Blackburn, Figueira: Mathematics for Computer Science

...

Reading
$
x
#
$
#

Write
$

...
Move
R
L
L
R

...

NewState
1
1
2
2
3

InState
1
1
1
2
2

blah blah blah

Areces, Blackburn, Figueira: Mathematics for Computer Science

Reading
$
x
#
$
#

Write
$

...
Move
R
L
L
R

NewState
1
1
2
2
3

blah blah blah

c
INRIA

Areces, Blackburn, Figueira: Mathematics for Computer Science

c
INRIA

Configuration 13
...

Configuration 13
$ $

InState
1
1
1
2
2

Reading
$
x
#
$
#

Write
$

...
Move
R
L
L
R

...

NewState
1
1
2
2
3

InState
1
1
1
2
2

Halt! No Action defined for State 3!

Areces, Blackburn, Figueira: Mathematics for Computer Science

c
INRIA

A simple machine, but one which demonstrated the basic


symbol manipulation abilities of Turing machines. Study it
well.
But the best way to learn to write Turing machines is to
study as many of them as possible. So lets look at another
example.

c
INRIA

I
I
I
I
I
I

c
INRIA

L
L
R

NewState
1
1
2
2
3

c
INRIA

We have seen examples of the sort of devices (abacuses and


so on) that people have devised during history to calculate
arithmetic functions (that is, addition, multiplication,
division and so on).

Unsurprisingly, Turing machines can calculate such


functions too.

Lets look at an example: a Turing machine for addition.

c
INRIA

Were going to present a Turing machine that will take as


input two blocks of 1s (separated by a blank square). The
machine will start in standard configuration.

It will then stick the two blocks together (thus


performing the monadic addition).

Furthermore, we shall design the machine is such a way


that it halts in standard configuration.

Why bother starting and stopping in standard


configuration? It may not seem like a very important
concept. But is is. We shall see why in the following
lecture.

c
INRIA

Areces, Blackburn, Figueira: Mathematics for Computer Science

Monadic addition
...

Begin by scanning the leftmost 1. Move right, and keep


doing so until you get to the blank that divides the two
blocks on input ones.
When you find the blank separating the two blocks of
input, write 1 in the blank, and switch into a new state.
Keep moving right until you come to the blank at the
right-hand-end of the (now continuous) block of 1s.
Move one space left, switch state, and change the 1 to a
blank (now the correct number of 1s are on the tape).
Move left, change state and keep moving left until you
come to the blank at the left-hand-end of the block of 1s.
Move right one space and halt.
And heres a machine that realizes this algorithm. . .

Areces, Blackburn, Figueira: Mathematics for Computer Science

Move
R

The basic idea

The algorithm in more detail


I

Write

...

Areces, Blackburn, Figueira: Mathematics for Computer Science

The first thing to decide is how to represent numbers.


Now we could use ordinary decimal notation, or we could
use binary notation, but it will make things simpler if we
start by using monadic notation.
What is monadic notation? Simple! the numeral 1 is
represented by 1, 2 is represented by 11, 3 is represented by
111, 4 by 1111, 5 by 11111, and so on (we wont bother to
introduce a convention for handling 0, though it is easy to
do so).
This notation is makes it easy to do addition. For example,
2 + 3 is 11 + 111 which is just 11111. That is, we
really just need to stick the two blocks of one representing
the input together!
How can we devise a Turing machine for doing this?

Areces, Blackburn, Figueira: Mathematics for Computer Science

Turing machines for arithmetic functions

Representing numbers
I

Reading
$
x
#
$
#

Areces, Blackburn, Figueira: Mathematics for Computer Science

As promised, the machine started in standard


configuration, converted all x symbols to $ symbols,
and halted in standard configuration.

Areces, Blackburn, Figueira: Mathematics for Computer Science

Halt! No Action defined for State 3!

Summing up
I

$ $

1
InState
1
1
2
2
3
3
4
4

Reading
1
#
1
#
1
#
1
#

Write
1

...

Move
R

R
L
L
L
R

NewState
1
2
2
3
3
4
4
5

blah blah blah


blah blah blah
blah blah blah
c
INRIA

Areces, Blackburn, Figueira: Mathematics for Computer Science

c
INRIA

Example input
...

Configuration 1
1

InState
1
1
2
2
3
3
4
4

Reading
1
#
1
#
1
#
1
#

Write
1

...

Move
R
R
L

L
L
R

...

NewState
1
2
2
3
3
4
4
5

InState
1
1
2
2
3
3
4
4

blah blah blah


blah blah blah
blah blah blah
c
INRIA

Reading
1
#
1
#
1
#
1
#

Write
1

...

Move
R
R
L

L
L
R

...

NewState
1
2
2
3
3
4
4
5

c
INRIA

...

Move
R
R
L

L
L
R

NewState
1
2
2
3
3
4
4
5

c
INRIA

Reading
1
#
1
#
1
#
1
#

Write
1

...

Move
R
R
L

L
L
R

NewState
1
2
2
3
3
4
4
5

c
INRIA

Areces, Blackburn, Figueira: Mathematics for Computer Science

Configuration 4

Configuration 5
1

Reading
1
#
1
#
1
#
1
#

Write
1

...

Move
R
R
L

L
L
R

...

NewState
1
2
2
3
3
4
4
5

1
InState
1
1
2
2
3
3
4
4

We stick the blocks together by changing the blank between


them to a 1. This means we have one 1 too many. So we will
need to remember to delete rightmost 1.

Reading
1
#
1
#
1
#
1
#

Write
1

...

Move
R
R
L

L
L
R

NewState
1
2
2
3
3
4
4
5

blah blah blah


blah blah blah
blah blah blah
c
INRIA

Areces, Blackburn, Figueira: Mathematics for Computer Science

c
INRIA

Areces, Blackburn, Figueira: Mathematics for Computer Science

Configuration 6

Configuration 7
1

InState
1
1
2
2
3
3
4
4

Write

blah blah blah


blah blah blah
blah blah blah

Areces, Blackburn, Figueira: Mathematics for Computer Science

...

1
InState
1
1
2
2
3
3
4
4

blah blah blah


blah blah blah
blah blah blah

InState
1
1
2
2
3
3
4
4

Reading
1
#
1
#
1
#
1
#

Configuration 3
1

...

Areces, Blackburn, Figueira: Mathematics for Computer Science

Configuration 2

InState
1
1
2
2
3
3
4
4

blah blah blah


blah blah blah
blah blah blah

Areces, Blackburn, Figueira: Mathematics for Computer Science

...

Reading
1
#
1
#
1
#
1
#

Write
1

...

Move
R

R
L
L
L
R

...

NewState
1
2
2
3
3
4
4
5

InState
1
1
2
2
3
3
4
4

blah blah blah


blah blah blah
blah blah blah
Areces, Blackburn, Figueira: Mathematics for Computer Science

Reading
1
#
1
#
1
#
1
#

Write
1

...

Move
R

R
L
L
L
R

NewState
1
2
2
3
3
4
4
5

blah blah blah


blah blah blah
blah blah blah
c
INRIA

Areces, Blackburn, Figueira: Mathematics for Computer Science

10

c
INRIA

Configuration 8
...

Configuration 9
1

InState
1
1
2
2
3
3
4
4

Reading
1
#
1
#
1
#
1
#

Write
1

Move
R
R
L

...

L
L
R

...

NewState
1
2
2
3
3
4
4
5

InState
1
1
2
2
3
3
4
4

blah blah blah


blah blah blah
blah blah blah
c
INRIA

Reading
1
#
1
#
1
#
1
#

Write
1

...

Move
R
R
L

L
L
R

...

NewState
1
2
2
3
3
4
4
5

Write
1

...

Move
R
R
L

L
L
R

NewState
1
2
2
3
3
4
4
5

c
INRIA

Reading
1
#
1
#
1
#
1
#

Write
1

...
Move
R
R
L

L
L
R

NewState
1
2
2
3
3
4
4
5

c
INRIA

Areces, Blackburn, Figueira: Mathematics for Computer Science

Configuration 12

Configuration 14
1

Reading
1
#
1
#
1
#
1
#

Write
1

...
Move
R
R
L

L
L
R

...

NewState
1
2
2
3
3
4
4
5

1
InState
1
1
2
2
3
3
4
4

blah blah blah


blah blah blah
blah blah blah

1 1

Reading
1
#
1
#
1
#
1
#

Write
1

...
Move
R
R
L

L
L
R

NewState
1
2
2
3
3
4
4
5

blah blah blah


blah blah blah
blah blah blah
c
INRIA

Areces, Blackburn, Figueira: Mathematics for Computer Science

c
INRIA

Areces, Blackburn, Figueira: Mathematics for Computer Science

Configuration 15

Configuration 16
1

InState
1
1
2
2
3
3
4
4

blah blah blah


blah blah blah
blah blah blah

c
INRIA

Areces, Blackburn, Figueira: Mathematics for Computer Science

...

1
InState
1
1
2
2
3
3
4
4

There! We changed the rightmost 1 to a blank. So we have the


right number of symbols on the tape. The only thing left to do
is get the head back to the leftmost 1 symbol.

InState
1
1
2
2
3
3
4
4

Reading
1
#
1
#
1
#
1
#

Configuration 11
1

...

Areces, Blackburn, Figueira: Mathematics for Computer Science

Configuration 10

InState
1
1
2
2
3
3
4
4

blah blah blah


blah blah blah
blah blah blah

Areces, Blackburn, Figueira: Mathematics for Computer Science

...

1 1

Reading
1
#
1
#
1
#
1
#

Write
1

...
Move
R

R
L
L
L
R

...

NewState
1
2
2
3
3
4
4
5

InState
1
1
2
2
3
3
4
4

blah blah blah


blah blah blah
blah blah blah
Areces, Blackburn, Figueira: Mathematics for Computer Science

1 1

Reading
1
#
1
#
1
#
1
#

Write
1

...
Move
R

R
L
L
L
R

NewState
1
2
2
3
3
4
4
5

blah blah blah


blah blah blah
blah blah blah
c
INRIA

Areces, Blackburn, Figueira: Mathematics for Computer Science

11

c
INRIA

Configuration 17
...

InState
1
1
2
2
3
3
4
4

Configuration 18
1

Reading
1
#
1
#
1
#
1
#

Write
1

...
Move
R
R
L

L
L
R

...

NewState
1
2
2
3
3
4
4
5

InState
1
1
2
2
3
3
4
4

blah blah blah


blah blah blah
blah blah blah
c
INRIA

1 1

Reading
1
#
1
#
1
#
1
#

Write
1

...
Move
R
R
L

L
L
R

NewState
1
2
2
3
3
4
4
5

c
INRIA

Areces, Blackburn, Figueira: Mathematics for Computer Science

Our second Turing machine, the monadic addition


machine, carried out a typical mathematical task.

Our third machine is going to carry out a typical logical


task it is going to provide a yes/no answer to a question.
That is, it is going to implement what logicians call a
decision procedure.
In particular, the machine is going to decide whether the
input number (written in monadic) is odd or even.

c
INRIA

Areces, Blackburn, Figueira: Mathematics for Computer Science

R
L

L
L
R

NewState
1
2
2
3
3
4
4
5

c
INRIA

This machine clearly does perform monadic addition. Thus


we have seen our first example of a Turing machine which
calculates an arithmetic function.
Our use of monadic notation for number made addition
easy but it makes other operations quite involved. For
example, monadic multiplication takes a little more
thought. Writing a Turing machine for monadic
multiplication is one of the problems on Worksheet 3.
It is perhaps worth emphasizing that we used monadic
addition because it gave us a simple way to show how
arithmetic functions could be implemented. But we werent
forced to do so! We could have defined addition in decimal
or binary. And indeed, youll find such exercises on
Worksheet 3.
c
INRIA

We start in the initial state reading a 1. Assume that there


are two other states: an even state and an odd state.
Move right, go into the odd state, and then . . .

Odd state: if you read a blank, write odd and halt. If you
read a 1, move right, and go into the even state.

Even state: if you read a blank, write even and halt. If


you read a 1, move right, and go into the odd state.

Just keep flipping between the previous two moves!

c
INRIA

Areces, Blackburn, Figueira: Mathematics for Computer Science

Odd or even?

Example input
1

InState
1
2
2
3
3

Move
R

The Algorithm

Our first Turing machine (the x to $ converter)


performed a typical computer science problem, namely
converting one kind of symbol to another.

...

Write

...

Areces, Blackburn, Figueira: Mathematics for Computer Science

A decision problem

Remarks

Halt! State 5 is a final state!

Reading
1
#
1
#
1
#
1
#

Areces, Blackburn, Figueira: Mathematics for Computer Science

Configuration 18

InState
1
1
2
2
3
3
4
4

blah blah blah


blah blah blah
blah blah blah

Areces, Blackburn, Figueira: Mathematics for Computer Science

...

1 1

Reading
1
#
1
#
1

1
Write
even

...

...
Move
R

odd

R
R

InState
1
2
2
3
3

NewState
3
4
3
5
2

Heres a machine for the algorithm just described. State 2 is the


even state and State 3 is the oddstate. blah blah blah
blah blah blah
Areces, Blackburn, Figueira: Mathematics for Computer Science

Reading
1
#
1
#
1

1
Write
even

...
Move
R

odd

R
R

NewState
3
4
3
5
2

And heres some typical (even) input. Ready to go. . . blah blah
blah
blah blah blah

c
INRIA

Areces, Blackburn, Figueira: Mathematics for Computer Science

12

c
INRIA

Configuration 1
...

Configuration 2
1 1

InState
1
2
2
3
3

Reading
1
#
1
#
1

...

Write
even

Move
R
R

odd

...

NewState
3
4
3
5
2

InState
1
2
2
3
3

We start in State 1. Were reading the first 1, so we have so far


seen one 1, an odd number. So we go into the odd state
(State 3). blah blah blah

Reading
1
#
1
#
1

...

Write
even

Move
R
R

odd

...

NewState
3
4
3
5
2

1
InState
1
2
2
3
3

Were in the even state. But were reading a 1. So now we have


seen an odd number of 1s. So we go into the odd state
(State 3). blah blah blah

...

Write
even

Move
R
R

odd

NewState
3
4
3
5
2

c
INRIA

Reading
1
#
1
#
1

...

Write
even

Move
R
R

odd

NewState
3
4
3
5
2

Were in the odd state. But were reading a 1. So now we have


seen an even number of 1s. So we go into the even state
(State 2). blah blah blah

c
INRIA

Areces, Blackburn, Figueira: Mathematics for Computer Science

c
INRIA

Areces, Blackburn, Figueira: Mathematics for Computer Science

Configuration 5

Configuration 6
1

InState
1
2
2
3
3

Reading
1
#
1
#
1

Configuration 4
1

...

Areces, Blackburn, Figueira: Mathematics for Computer Science

Configuration 3

InState
1
2
2
3
3

Were in the odd state. But were reading a 1. So now we have


seen an even number of 1s. So we go into the even state
(State 2). blah blah blah

c
INRIA

Areces, Blackburn, Figueira: Mathematics for Computer Science

...

Reading
1
#
1
#
1

...

Write
even

Move
R

odd

R
R

...

NewState
3
4
3
5
2

InState
1
2
2
3
3

Were in the even state. But now we are seeing a blank square.
So we know that the input was an even number. So we write
even on the tape and go into State 4 blah blah blah
Areces, Blackburn, Figueira: Mathematics for Computer Science

Reading
1
#
1
#
1

...

even

Write
even

Move
R

odd

R
R

NewState
3
4
3
5
2

And this is where we halt, because State 4 is a final state. blah


blah blah
blah blah blah

c
INRIA

Areces, Blackburn, Figueira: Mathematics for Computer Science

Remarks

c
INRIA

Summary of Lecture 3
Key concepts:
I

So the machine really does implement the odd/even


decision problem.

We introduced the key concept underlying this course,


namely the concept of a Turing machine.

Study this machine carefully, and then do the exercise on


Worksheet 3 which asks you to modify it in a certain way.

Turing machines are abstract machines, nonetheless they


are based on an analysis of concrete calculations.

Indeed Turing machines are essentially a simple mechanism


for realizing algorithms using three fundamental concepts:
the tape, some basic operations operations (moving right,
moving left, and writing symbols), and internal states.

Areces, Blackburn, Figueira: Mathematics for Computer Science

c
INRIA

Areces, Blackburn, Figueira: Mathematics for Computer Science

13

c
INRIA

Summary of Lecture 3 continued

Summary of Lecture 3 continued

Key concepts:
I
I

We examined three Turing machines in detail.


The first performed a symbol conversion task: it took in a
string of mixed xs and $s, changed all the xs to $s, and
halted leaving a string of dollars on the tape.
The second performed a typical mathematical task: it
calculated values of the addition function. For this task we
represented numbers using monadic notation, as this
greatly simplified the task.
Finally, we introduced the notion of a decision problem,
and illustrated this with a machine that could decide
whether an input number was odd or even.
Note: all three machines are calculating functions think
about it.

Areces, Blackburn, Figueira: Mathematics for Computer Science

Key concepts:

c
INRIA

We also mentioned several times the concept of standard


configuration for input and output, though as yet we have
not seen why this idea is so important.

We will see why next week when we start to talk about to


build Turing machines to perform more interesting tasks.

Areces, Blackburn, Figueira: Mathematics for Computer Science

14

c
INRIA

You might also like