Cite

This course has no released version yet: the text at this address may change. Cite a released version when one exists.

/courses/models-of-computation/turing-machines
@misc{models-of-computation-turing-machines,
  title = {{Turing machines}},
  howpublished = {Lesson 2 of \emph{Models of computation}, Computability Theory course notes},
  url = {/courses/models-of-computation/turing-machines},
  note = {Unreleased working version}
}

To cite a single statement, use the # link that appears next to its heading.

Lesson 2 of 13

Turing machines

Formal definition of Turing machines, configurations and computations, input and output conventions, and two machines verified in detail.

35 min3 questions
Models of computation · lesson 2 of 13
  1. Algorithms and partial functions
  2. Turing machines
  3. Programming Turing machines
  4. Register machines
  5. Primitive recursive functions
  6. Coding finite sequences
  7. The Ackermann function
  8. Minimization and partial recursive functions
  9. LOOP and WHILE programs
  10. The λ-calculus
  11. Computing with λ-terms
  12. Equivalence of the models
  13. The Church–Turing thesis

Turing’s 1936 model arises from an analysis of a human computing with pencil and paper. The paper is replaced by a one-dimensional tape divided into cells, each holding one symbol from a finite alphabet. The person’s attention is a head scanning one cell at a time. Their state of mind, which can only take finitely many values, is the state of the machine. At each step, the action depends only on the current state and on the scanned symbol, and consists in writing a symbol, moving one cell left or right, and changing state.

⋯⊔⊔111⊔11⊔⊔⊔⋯headtapefinite controlq3δ(q, a) = (q′, b, D)read a, write b,move one cell in direction D,enter the state q′

The components of a Turing machine. The head scans one cell; the finite control holds the current state; each step is determined by the state and the scanned symbol.

2.1 Definition

Definition 2.1.#

A Turing machine is a tuple M=(Q,Γ,δ,q0,qh)M = (Q, \Gamma, \delta, q_0, q_h) where

  • QQ is a finite set of states, with an initial state q0∈Qq_0 \in Q and a halting state qh∈Qq_h \in Q, qh≠q0q_h \neq q_0;
  • Γ\Gamma is a finite alphabet containing at least the blank symbol ⊔\sqcup and the symbol 11;
  • δ:(Q∖{qh})×Γ→Q×Γ×{L,R}\delta : (Q \setminus \{q_h\}) \times \Gamma \to Q \times \Gamma \times \{L, R\} is the transition function.

An entry δ(q,a)=(q′,b,D)\delta(q, a) = (q', b, D) reads: in state qq, scanning aa, write bb, move in direction DD, and enter state q′q'. Since δ\delta is defined on all pairs (q,a)(q, a) with q≠qhq \neq q_h, the machine can always perform a step unless it is in the halting state. A machine is a finite object: it can be written as a finite table, and there are only countably many machines (up to renaming of states and symbols).

To describe the state of a computation, we index the cells of the tape by Z\mathbb{Z}.

Definition 2.2.#

A tape is a function τ:Z→Γ\tau : \mathbb{Z} \to \Gamma such that τ(i)=⊔\tau(i) = \sqcup for all but finitely many ii. A configuration of MM is a triple C=(q,τ,h)C = (q, \tau, h) where q∈Qq \in Q, τ\tau is a tape and h∈Zh \in \mathbb{Z} is the head position. The configuration is halting if q=qhq = q_h.

If C=(q,τ,h)C = (q, \tau, h) is not halting and δ(q,τ(h))=(q′,b,D)\delta(q, \tau(h)) = (q', b, D), the successor of CC is the configuration C′=(q′,τ′,h′)C' = (q', \tau', h') where

τ′(i)={bif i=h,τ(i)otherwise,h′={h−1if D=L,h+1if D=R.\tau'(i) = \begin{cases} b & \text{if } i = h, \\ \tau(i) & \text{otherwise,} \end{cases} \qquad h' = \begin{cases} h - 1 & \text{if } D = L, \\ h + 1 & \text{if } D = R. \end{cases}

We write C⊢MC′C \vdash_M C', or simply C⊢C′C \vdash C'.

Every non-halting configuration has exactly one successor, and a halting configuration has none: Turing machines are deterministic.

Definition 2.3.#

The computation of MM from a configuration C0C_0 is the sequence C0⊢C1⊢C2⊢⋯C_0 \vdash C_1 \vdash C_2 \vdash \cdots, which is either infinite, or finite and ends with the first halting configuration CtC_t. In the second case we say that MM halts from C0C_0 after tt steps, with final configuration CtC_t.

2.2 Computing functions

To compute functions on integers we fix conventions for representing inputs and outputs on the tape. We use unary notation, representing x∈Nx \in \nat by x+1x + 1 consecutive 11s:

⌜x⌝=1x+1.\ulcorner x \urcorner = 1^{x+1}.

Using x+1x+1 rather than xx symbols ensures that every number, including 00, occupies at least one cell, so that a tuple of numbers separated by single blanks can be read unambiguously.

Definition 2.4.#

Let MM be a Turing machine and k≥1k \geq 1.

  • The initial configuration on input x⃗=(x1,…,xk)\vec x = (x_1, \dots, x_k) is (q0,τx⃗,0)(q_0, \tau_{\vec x}, 0), where the tape τx⃗\tau_{\vec x} contains the word ⌜x1⌝⊔⌜x2⌝⊔⋯⊔⌜xk⌝\ulcorner x_1 \urcorner \sqcup \ulcorner x_2 \urcorner \sqcup \cdots \sqcup \ulcorner x_k \urcorner written from cell 00 onwards, and blanks elsewhere.
  • A configuration (qh,τ,h)(q_h, \tau, h) is a standard final configuration with output yy if τ(i)=1\tau(i) = 1 for h≤i≤h+yh \leq i \leq h + y and τ(i)=⊔\tau(i) = \sqcup for all other ii: the tape contains exactly ⌜y⌝\ulcorner y \urcorner, and the head scans its first cell.

The partial function φM(k):Nk⇀N\varphi^{(k)}_M : \nat^k \rightharpoonup \nat computed by MM is defined by: φM(k)(x⃗) ⁣ ⁣↓=y\varphi^{(k)}_M(\vec x)\converge = y if the computation of MM from the initial configuration on input x⃗\vec x halts in a standard final configuration with output yy, and φM(k)(x⃗) ⁣ ⁣↑\varphi^{(k)}_M(\vec x)\diverge otherwise.

A partial function f:Nk⇀Nf : \nat^k \rightharpoonup \nat is Turing-computable if f=φM(k)f = \varphi^{(k)}_M for some machine MM.

Note that φM(k)(x⃗)\varphi^{(k)}_M(\vec x) is undefined both when the computation does not halt and when it halts in a non-standard configuration. With this convention every machine computes exactly one kk-ary partial function, for each kk.

Remark 2.5.

The precise conventions — unary or binary notation, a tape infinite in one or both directions, a cleaned tape at the end or not — vary between textbooks. They change the machines, but not the class of Turing-computable functions. The robustness of the class under such changes is discussed in the next lesson.

2.3 First example: the successor function

Let MSM_S be the machine with states {q0,q1,qh}\{q_0, q_1, q_h\}, alphabet {⊔,1}\{\sqcup, 1\}, and transitions

11 ⊔\sqcup
q0q_0 (q0,1,R)(q_0, 1, R) (q1,1,L)(q_1, 1, L)
q1q_1 (q1,1,L)(q_1, 1, L) (qh,⊔,R)(q_h, \sqcup, R)

startq0q1qh⊔ / 1, L⊔ / ⊔, R1 / 1, R1 / 1, L

State diagram of MSM_S. An edge labeled a/b,Da / b, D from qq to q′q' means δ(q,a)=(q′,b,D)\delta(q, a) = (q', b, D); the halting state is circled twice.

In state q0q_0 the head runs right across the input. On the first blank it writes a 11 and turns back in state q1q_1, runs left to the blank preceding the block, and steps back onto the first 11.

Proposition 2.6.#

MSM_S computes the successor function: φMS(1)(x)=x+1\varphi^{(1)}_{M_S}(x) = x + 1 for every xx.

Proof of Proposition 2.6.

Write τn\tau_n for the tape containing 11s exactly on the cells 0,…,n0, \dots, n. The initial configuration on input xx is (q0,τx,0)(q_0, \tau_x, 0). For i≤xi \leq x, the cell ii contains 11, so by the first transition

(q0,τx,0)⊢(q0,τx,1)⊢⋯⊢(q0,τx,x+1).(q_0, \tau_x, 0) \vdash (q_0, \tau_x, 1) \vdash \cdots \vdash (q_0, \tau_x, x+1).

Cell x+1x+1 is blank, so the next configuration is (q1,τx+1,x)(q_1, \tau_{x+1}, x). For 0≤i≤x0 \leq i \leq x the cell ii contains 11, so

(q1,τx+1,x)⊢(q1,τx+1,x−1)⊢⋯⊢(q1,τx+1,−1).(q_1, \tau_{x+1}, x) \vdash (q_1, \tau_{x+1}, x - 1) \vdash \cdots \vdash (q_1, \tau_{x+1}, -1).

Cell −1-1 is blank, so the next configuration is (qh,τx+1,0)(q_h, \tau_{x+1}, 0). This is a standard final configuration with output x+1x + 1, reached after 2x+42x + 4 steps.

The proof follows a pattern used for all such verifications: describe the configurations at the beginning and at the end of each phase of the computation, and check each phase by induction on the number of steps.

2.4 Second example: addition

The machine M+M_+ below computes (x,y)↦x+y(x, y) \mapsto x + y. On input ⌜x⌝⊔⌜y⌝\ulcorner x \urcorner \sqcup \ulcorner y \urcorner, it replaces the separating blank by a 11, which produces a block of x+y+3x + y + 3 ones; it then erases the last two 11s and returns to the beginning of the block.

11 ⊔\sqcup
q0q_0 (q0,1,R)(q_0, 1, R) (q1,1,R)(q_1, 1, R)
q1q_1 (q1,1,R)(q_1, 1, R) (q2,⊔,L)(q_2, \sqcup, L)
q2q_2 (q3,⊔,L)(q_3, \sqcup, L) (qh,⊔,R)(q_h, \sqcup, R)
q3q_3 (q4,⊔,L)(q_4, \sqcup, L) (qh,⊔,R)(q_h, \sqcup, R)
q4q_4 (q4,1,L)(q_4, 1, L) (qh,⊔,R)(q_h, \sqcup, R)

The entries δ(q2,⊔)\delta(q_2, \sqcup) and δ(q3,⊔)\delta(q_3, \sqcup) are never used on inputs of the required form; they are only there to make δ\delta total.

Proposition 2.7.#

M+M_+ computes addition: φM+(2)(x,y)=x+y\varphi^{(2)}_{M_+}(x, y) = x + y for all x,yx, y.

Proof of Proposition 2.7.

The initial tape has 11s on the cells 0,…,x0, \dots, x and x+2,…,x+y+2x + 2, \dots, x + y + 2, and a blank on cell x+1x + 1. In state q0q_0 the head moves right to cell x+1x + 1, writes 11 there and moves to cell x+2x + 2 in state q1q_1. Now the tape contains 11s exactly on 0,…,x+y+20, \dots, x + y + 2. In state q1q_1 the head moves right to cell x+y+3x + y + 3, which is blank, and moves back to cell x+y+2x + y + 2 in state q2q_2. It erases this cell and moves to cell x+y+1x + y + 1 in state q3q_3. Since 0≤x+y+1<x+y+20 \leq x + y + 1 < x + y + 2, this cell belongs to the block written in the first phase and has not been erased, so it contains 11; it is erased and the head moves to cell x+yx + y in state q4q_4. The tape now contains 11s exactly on 0,…,x+y0, \dots, x + y. As in the previous proof, q4q_4 runs left to cell −1-1, and the machine halts with the head on cell 00. The final configuration is standard with output x+yx + y.

2.5 Exercises

Exercise 2.8 ★.

Give a machine computing the constant function x↦0x \mapsto 0, and prove that it is correct.

Solution

The machine must erase all 11s but the first and return to cell 00:

11 ⊔\sqcup
q0q_0 (q1,1,R)(q_1, 1, R) (qh,⊔,R)(q_h, \sqcup, R)
q1q_1 (q1,⊔,R)(q_1, \sqcup, R) (q2,⊔,L)(q_2, \sqcup, L)
q2q_2 (q3,1,L)(q_3, 1, L) (q2,⊔,L)(q_2, \sqcup, L)
q3q_3 (qh,1,R)(q_h, 1, R) (qh,⊔,R)(q_h, \sqcup, R)

On input xx the head steps from cell 00 to cell 11 in state q1q_1, erases cells 1,…,x1, \dots, x and reaches the blank cell x+1x + 1, moves back to cell xx in state q2q_2, moves left over the blanks x,…,1x, \dots, 1 to cell 00, which contains 11, moves to cell −1-1 in state q3q_3, and finally halts on cell 00. The tape contains a single 11 on cell 00: the output is 00. The entries δ(q0,⊔)\delta(q_0, \sqcup) and δ(q3,1)\delta(q_3, 1) are never used.

Exercise 2.9 ★.

Give a machine computing the projection (x,y)↦x(x, y) \mapsto x.

Solution

Run right to the separator, step over it, erase the block ⌜y⌝\ulcorner y \urcorner until reaching the blank after it, then run left over blanks until the last 11 of ⌜x⌝\ulcorner x \urcorner, then left over the 11s to cell −1-1, and step right:

11 ⊔\sqcup
q0q_0 (q0,1,R)(q_0, 1, R) (q1,⊔,R)(q_1, \sqcup, R)
q1q_1 (q1,⊔,R)(q_1, \sqcup, R) (q2,⊔,L)(q_2, \sqcup, L)
q2q_2 (q3,1,L)(q_3, 1, L) (q2,⊔,L)(q_2, \sqcup, L)
q3q_3 (q3,1,L)(q_3, 1, L) (qh,⊔,R)(q_h, \sqcup, R)

In state q2q_2, the head moves left over the erased cells and the separator until it reaches cell xx, the last 11 of the first block.

Exercise 2.10 ★.

Show that every Turing-computable partial function is computed by infinitely many machines. Show that there are only countably many Turing-computable partial functions N⇀N\nat \rightharpoonup \nat, and deduce that there is a total function N→N\nat \to \nat which is not Turing-computable.

Solution

Adding to a machine states that are never entered does not change its computations, so each computable function has infinitely many machines. Up to renaming, a machine is determined by finitely many data — the numbers of states and symbols, and the table of δ\delta — so it can be described by a finite string over a finite alphabet, and there are countably many machines; hence countably many computable functions. The set of total functions N→N\nat \to \nat is uncountable (by a diagonal argument, see The diagonal argument), so some total function is not computable.

2.6 Notes and references

Turing’s machines (1936) differ from ours in inessential details: they compute infinite binary sequences rather than functions, and halting plays no role. Post introduced an almost identical model independently in the same year. The representation of xx by x+1x + 1 strokes is that of Kleene (Introduction to Metamathematics, §67); the requirement of a standard final configuration, with the output alone on the tape and the head on its first cell, is close to the conventions of Boolos, Burgess and Jeffrey.

  • E. L. Post, Finite combinatory processes — formulation 1, Journal of Symbolic Logic 1 (1936), 103–105.
  • S. C. Kleene, Introduction to Metamathematics, North-Holland, 1952.
  • G. Boolos, J. Burgess, R. Jeffrey, Computability and Logic, 5th edition, Cambridge University Press, 2007.