10 Turing
10 Turing
COMP1600 / COMP6260
Semester 2, 2023
Alan Turing (1912–1954)
2 / 45
Turing’s Scientific contributions
1936
introduces Turing machines and the study of computability
1950.
introduces the Turing test that turns AI into a concrete research
problem
current “today”: Mitsuku, a computer programme has convinced
judges (for the last 5 competitions – from 2013 to 2019) that it was a
18-year-old female from Leeds, England. The competition ceased to
exist from 2020 onwards.
1952.
Pioneering work on computation in nature; (This refers to his 1952
work that describes how patterns in nature, such as stripes and
spirals, can arise naturally from a homogeneous, uniform state. It
doesn’t refer to the journal “Nature”)
The U.K.
code breaking effort assembled at Bletchley park
Turing chief scientific genius
Achievements
cracked secret Enigma code
estimated to have shortened world war II by 2 years
Fallout
many ideas and technologies discovered in Bletchley park fed directly
into modern computers
4 / 45
Death and Legacy
1952
less than 10 years after heroic war efforts for the UK
Turing prosecuted for ‘crime’ of homosexuality
sentenced to chemical castration
1954
death by suicide
Legacy Now
widely recognised as the father of computing
Turing award equivalent of Nobel prize
UK government apologised for persecution in 2009
2012 officially named the Turing year
Royal Pardon in 2013
Appears on current Bank of England £50 note (released 2021)
5 / 45
Turing Machines – Introduction
Modern Computers
didn’t exist in 1936?
ENIAC in 1946 generally considered to be the first
6 / 45
Computing as a profession
a0 a1 a2 ... .... an
Finite
State
zk
Control
stack
memory
z2
z1
9 / 45
Turing Machines
a0 a1 a2 ... .... an
Finite
State zk
Control read
write tape
head memory
z2
z1
10 / 45
Single tape Turing Machines
a0 a1 an z0 z1 zk
read / write
head
Finite
State
Control
Simplification.
have the same tape both for input and for storage
tape is assumed to be infinite in both directions
analogy: “get more paper if you run out”
11 / 45
Turing Machines as language recognisers
12 / 45
Output
13 / 45
Turing Machine – formal definition
δ : S ×Γ →
7 S × Γ × {L, R, S}
(state, tape symbol) 7→ (new state, new tape symbol, direction)
The direction tells the read/write head which way to go next: Left,
Right, or Stay.
14 / 45
Running a TM
Initialisation.
some input (a finite string over Σ) is written on the tape;
every other tape cell is a blank – Λ;
the read/write head sits over some cell of the input (or over any Λ if
the input is );
the state is the start state s0 .
Running.
in a cycle: read symbol and execute action (state dependent):
move/write/change state
until a final state is reached (or the machine gets stuck)
15 / 45
Graphical Representation of the Transition Function
Λ Λ
1,L 1,S
- S0 - S1 - H
6
1
1,L
(Like FSA, annotate transition edges with commands for accessing tape.)
Convention.
Numerator: symbol read from tape.
I Λ means the tape is blank at that position.
Denominator: symbol written / direction of head movement.
I direction one of L, R, S for Left, Right, Stay.
16 / 45
What does it do?
1 1 1 1
@
head
@
Λ Λ
1,L 1,S
- S0 - S1 - H
6
1
1,L
17 / 45
What does it do?
1 1 1 1 1 1
@
head
@
Λ Λ
1,L 1,S
- S0 - S1 - H
6
1
1,L
Adds two to a unary number!
Assume the head starts over the input data.
First phase scans left.
Second phase writes two extra 1s.
17 / 45
The Convention for Errors
Λ Λ
1,L 1,S
- S0 - S1 - H
6
1
1,L
0,R
0 1,L
0
? Λ
? Λ
Λ,L Λ,R
- S0 - S1 - H
6 6
1 1
1,R 0,L
Questions.
Do you see two phases?
What does each phase accomplish?
19 / 45
What does this one do?
0,R
0 1,L
0
? Λ
? Λ
Λ,L Λ,R
- S0 - S1 - H
6 6
1 1
1,R 0,L
Answer.
Phase 1: initialisation.
Phase 2: computation, in this case, complement a binary number.
20 / 45
Harder Problems?
21 / 45
Incrementing a binary number
Solution:
0
0,R
? Λ 0
- S0 Λ,L - S1 1,S - H
Λ
6 6 1,S
1 1
1,R 0,L
22 / 45
Decrement
? Λ 1
- S0 Λ,L - S1 0,S - H
6 6
1 0
1,R 1,L
23 / 45
How to add two numbers?
Operation.
Go back and forth between m and n, decrementing one (until this
fails) and incrementing the other.
decrement m, and increment n, because n will expand leftwards.
m gets changed to 00 . . . 0, n is replaced by the sum.
Finally, delete the #00 · · · 0 on the right.
24 / 45
How to add two numbers? ctd.
1
Λ,R
? Λ
Λ,S
1 S4 - H
0 1,R #
0,R #,R 6
#
Λ,R
? Λ 1
- S0 Λ,L - S1 0,S - S2
}
Z 6 6
Z
Z
0 ΛZ 0
# 0 1
1,S 1,S Z 1,L #,L 0,L 1,L
Z
Z =
S3
6
1
0,L
25 / 45
How to multiply two numbers?
Input. as for addition
|{z} # |10100101
{z } # |100101010
{z }
p n m
Operation.
Repeatedly decrement m (until this fails) and add n to p (p is initially
blank)
Must modify the addition routine to not erase the number n being
added.
Modification of addition routine
Two new tape symbols, 00 and 10 .
Before each addition stage, change all the 1s in n to 10 .
When decrementing n, swap 0s and 1s as usual, but keep the primes.
When finished adding n to p, go back and use the primes to restore n.
Observation.
this is very tedious – but the model is simple and easy to analyse
tricks that you see here are typical
26 / 45
Programming Issues – Data
Numbers.
Usually use unary or binary for integers.
Vocabulary.
Can be arbitrary, and {0, 1} suffice. Characters are represented as
strings of bits.
27 / 45
Programming Issues – Control
Common Idioms.
Scan to blank, or to find, insert, delete a symbol.
Use control states to remember information
In particular, we often need to “remember” a symbol, to write it
elsewhere: this typically requires a set of states, one per symbol
Composition
If you have a TM to multiply by 3 and one to multiply by 5, put them
together to multiply by 15.
Decisions (conditional computation)
As we have seen, we can branch on 0 or 1 (or T or F).
Loops — of course.
28 / 45
Using States to Remember a Tape Symbol
Given a string of 0 or 1
0 1
surrounded by blanks, this
0 ,R U0 1 ,R machine repeatedly forever
Λ erases the leftmost bit, and
0 ,L 0
1 Λ,R writes it on the right hand end.
1 ,L
Λ (Not so useful, but illustrates
S
Λ,R
T the point)
0
0 ,L
Λ
1 We use the choice of states
Λ,R
1 ,L U0 or U1 to remember which
0 1 symbol has been erased and is
0 ,R U1 1 ,R
to be written
29 / 45
Universal Turing Machines and Turing Completeness
30 / 45
Church-Turing Thesis
Church-Turing Thesis.
If a function is computable, then it can be calculated by a
Turing machine.
Equivalent Formulation.
if a problem can be solved by an algorithm, then it can be solved by a
Turing machine.
This is a Thesis.
more a definition than a theorem
can never be proved: what does algorithmic mean?
however, there’s lots of evidence
Evidence.
all other definitions of the term computable give the same class of
computable functions
there are many: λ-calculus, register machines, while programs, etc.
31 / 45
Church and λ-calculus
32 / 45
Can real computers simulate Turing Machines?
Differences between computers and Turing machines
A Turing Machine has an infinite tape.
“real” computers have finite memory (sometimes a lot, but nowhere
near infinite)
physical devices necessarily have finite memory. They’re more like
finite automata.
. . . but the number of states can be very, very large.
How big is 24294967296 ?
physical computers are an approximation of TMs.
Intuitive Understanding
Turing machine model feels closer to what we can program
e.g. we can recognise the language {an b n | n ≥ 0}
if the real machines runs out of memory . . . we can always buy more
this is about computation in principle
33 / 45
Church-Turing Thesis: Argument 1
Taken together
convincing (in the 1930s) that Turing’s model is correct
but no mathematical evidence (yet)
34 / 45
Church-Turing Thesis: Argument 2
Stability of the TM Model.
adding ‘features’ doesn’t make more functions computable
Multi-tape.
have extra tapes to store data
easier to program, but no extra “power”
(single-tape can simulate multi-tape)
Multi-head.
have more than one head, heads can move independently
heads can access multiple symbols at once
again, no extra power
Main Insight.
any (reasonable) model of computation can compute precisely the
same functions as the TM model.
“reasonable” in the sense of modelled on mechanical computation
Examples.
Lambda-Calculus (Church, 1932)
Post-Systems (Post, 1939)
Register Machines
...
Doesn’t include
models based on physical phenomena
. . . or biology, or . . .
36 / 45
Argument 3B: Grammars
Proof (Sketch)
write the start symbol S onto the tape (say, right of our input)
search through all possible derivations from S
each time we reach a word, check whether it matches the input
Acceptance.
if the grammar finds the input, we will find a derivation
if the grammar does not generate the input, we may loop forever (and
not accept)
37 / 45
Argument 3B – grammars ctd
Unrestricted Grammars. Have productions of the form
α→β
where β is arbitrary, and α contains at least one non-terminal.
Theorem. For any TM, there exists a grammar that generates precisely the
words that the TM accepts.
Proof (Sketch)
non-terminals (of the grammar) are states of the TM
run TM “backwards” (we are interested in inputs, not outputs)
0
1,R
T - U
TM Transition.
Grammar Production. 1U → T 0.
Observation.
no computer ever invented could do things that a TM can’t do
no programming language can do more than a TM
back-and-forth translation TM ↔ PL
Common Terminology.
A programming language that can compute every function that can be
computed by a Turing machine is called Turing complete.
Examples.
The languages that you know: Haskell, Java, Python, . . .
even the simple while language that we used for Hoare logic
implement TM simulator in your favourite programming language
39 / 45
Argument 3D – John Conway’s Game of Life
Game of Life.
infinite 2d grid
finitely many cells marked alive, all others are dead
Emergent Behaviour.
visualised by GoL, many interesting forms
analogy of complex behaviour emerging from simple rules
40 / 45
Argument 3D – John Conway’s Game of Life ctd.
41 / 45
Metaprograms
Examples
Lexical analysers and parser generators SPARK Examiner, which
automatically proves correctness properties of programs written in
SPARK Ada;
Code generation which automatically produces codes from e.g. a Z
specification or a formal proof
42 / 45
Coding a TM onto tape
Coding of a TM as binary strings
can be written onto a tape
just code the transition function
0i 1 0 j 1 0 k 1 0 l 1 0 m
C1 11 C2 11 · · · 11 Cn
Additional Input.
code for a TM, and additional string
use 111 to separate TM and string input
44 / 45
Coding a TM onto tape – example
Λ Λ
1,R 1,S
- S1 - S3 - S2
6
1
1,R
45 / 45