Discrete MathematicsLanguages, Automata and Computability5 of 6
Languages, Automata and Computability · 5
Turing Machines and the Halting Problem
Finite automata can only read an input string once from left to right using a fixed number of states, so we need a machine that can write down scratch work and move back and forth to follow any step-by-step mathematical recipe.
1
Tape, Head, and States
A Turing machine strips computation down to three mechanical parts: an infinite one-dimensional tape of boxes that each hold one symbol, a head that reads, writes, and shifts one box left (L), right (R), or stays put (S), and a finite rulebook of states that tracks which step of the recipe the machine is on.
To add 1 to a binary number like 1011 when the head starts on the leftmost digit, the machine first walks to the right end of the number in WALK_RIGHT and then carries leftward in ADD_ONE.
Tape, Head, and States
Text solution
Walk to the ones placeStarting on the leftmost 1 of 1011 in WALK_RIGHT, the head leaves 0s and 1s unchanged and steps right until it hits the blank _ past the right end, then steps left onto the last digit and switches to ADD_ONE:
Write the carry and haltWhen the head reads 0 (or a left blank _), it overwrites it with 1, stays put (S), and switches to HALT:
⟶_ 1↑ADD_ONE000 __ 1↑HALT100 _
So 10112(11) becomes 11002(12), and the machine stops without ever walking back over the leftmost digit.
2
Five-Tuples and Transition Tables
Instead of writing rules in full sentences, we write each instruction as a five-tuple or organize the rules into a transition table with current states down the rows and the symbol read across the columns:
(where I amCurrent State,box symbolRead,new symbolWrite,L, R, or SMove,where I goNext State)
You might also see Next State listed right after Read, and you can always spot which order is being used because the L,R,S move direction is unmistakable. Let's trace the Add-One transition table on the binary input 11, where the carry travels all the way to the blank at the left end.
Five-Tuples and Transition Tables
Text solution
Reach the right endStarting on the first 1 of _ 11 _, row WALK_RIGHT steps right across both 1s onto the right blank _, then steps left onto the second 1 in ADD_ONE:
Carry onto the left blankRow ADD_ONE turns both 1s into 0s as the head steps left onto the blank _, where rule (ADD_ONE,_,1,S,HALT) writes the leading 1 and halts:
So 112(3) becomes 1002(4) in six steps. When you are given a transition table, don't stare at the whole grid to guess what it does. Trace three or four steps on a tiny example tape and the pattern will jump right out.
In this machine, a run of trailing 1s keeps ADD_ONE stepping left and turning each 1 into 0 until the very first 0 or the left blank _ becomes 1 and halts.
After `WALK_RIGHT` bounces off the right blank, `ADD_ONE` steps left through each trailing `1`, turning it into `0`, until the first `0` or the left blank turns into `1` and the machine halts.
3
Deciding 0n1n on a Tape
A Turing machine decides a language when it is guaranteed to eventually stop in ACCEPT (yes) or REJECT (no) on every possible input without ever getting stuck in an infinite loop. Even though a Turing machine has only a fixed, finite number of states just like a finite automaton, it can decide L={0n1n∣n≥0} by zig-zagging across the tape and crossing off one 0 with an X for each 1 it crosses off with a Y.
Deciding 0ⁿ1ⁿ on a Tape
Text solution
Cross off matching pairs on 0011Starting in START, each round overwrites the leftmost remaining 0 with X, walks right in FIND_1 to overwrite the first unmatched 1 with Y, and walks left in RETURN_LEFT back to X:
Verify no unmatched symbols remain on 0011Seeing Y in START tells the head all 0s are matched, so it switches to CHECK_REST, scans past every Y to the right blank _, and halts in ACCEPT:
Catch an unmatched 1 on 011After pairing the single 0 with the first 1,START sees Y and enters CHECK_REST, which immediately hits the leftover 1 and halts in REJECT:
So 0011 is accepted and 011 is rejected using only four working states, no matter how large n grows.
Any mismatch, like hitting the blank in FIND_1 or seeing an unmatched 1 or out-of-order 0 in CHECK_REST, immediately sends the machine to REJECT.
The four states stay fixed while X and Y on the tape keep the count, and any mismatch lands directly on a REJECT cell.
4
The Church-Turing Thesis and Universal Machines
The Church-Turing Thesis states that every step-by-step algorithm that can be carried out by a human or physical computer can be carried out by a Turing machine. Because any rulebook of five-tuples is just text that can be encoded as a string of 0s and 1s (a machine's blueprint), one master Universal Turing Machine can read the blueprint of any machine M and an input I on its tape and simulate M running on I.
The Church-Turing Thesis and Universal Machines
5
The Halting Problem Is Undecidable
The Halting Problem asks whether we can build a single decider, HaltChecker, that takes any blueprint M and any input I and always finishes in finite time with HALTS or LOOPS. To prove that no such machine can exist, we assume HaltChecker exists and wire it inside a new machine called Rebel that feeds a blueprint into itself and does the opposite of whatever HaltChecker predicts.
The Halting Problem Is Undecidable
Text solution
Wire the opposite-day machineOn any input blueprint X,Rebel copies X into both slots of HaltChecker and flips the outcome:
Since both cases contradict HaltChecker's prediction and the copier and two-step loop are easy to build, HaltChecker itself cannot exist.
Now let's test this diagonal contradiction by placing Rebel on each row of the table.
Because Rebel is wired to do the opposite of every square on the diagonal, placing it on any row of HaltChecker's table always clashes at that row's diagonal square.
6
Undecidability by Reduction
A reduction proves that a new analyzer Tool X cannot exist by showing that if Tool X worked, we could wire it up with a simple text-editing adapter to build a working HaltChecker. Let's use a reduction to prove that BlankHaltChecker, a machine that takes only a single blueprint M′ and decides whether M′ halts when started on a blank tape, is also impossible.
Undecidability by Reduction
Text solution
Edit the rulebook without running MGiven any blueprint M and input I, the Blueprint Builder pastes two stages of five-tuples together to write the blueprint of a new machine M′:
M′ on a blank tape _:always takes finite stepsStage 1: write Iand return head to start⟶runs M on IStage 2: run rules of M
Match both outcomesStarted on a blank tape, M′ writes I and then does on that tape whatever M does on I:
Mhalts on I⟺M′halts on _⟺BlankHaltChecker(M′)=HALTS!Mloops on I⟺M′loops on _⟺BlankHaltChecker(M′)=LOOPS!
Because pasting Stage 1 onto M's rulebook is pure text editing that always finishes, a working BlankHaltChecker would give a working HaltChecker, so BlankHaltChecker cannot exist.
Key concepts
A Turing machine adds an infinite tape to a finite state table, where the head can read, write, and move L,R, or S according to five-tuples (Current State,Read,Write,Move,Next State). When you build or trace a machine, let the tape do the counting while the states handle the small repeatable jobs: in the Add-One machine, ADD_ONE steps left turning 1s into 0s until a 0 or _ becomes 1; in the 0n1n decider, four states zig-zag between X and Y tally marks on the tape. To decide a language, a machine must halt in ACCEPT or REJECT on every input. If it gets stuck in an infinite loop like (GO,0,0,R,GO) and (GO,1,1,L,GO) on even a single input, it is not a decider.
Because a rulebook is just text, any machine M can be encoded as a binary blueprint [M] and simulated by a Universal Turing Machine, which also means a machine can be fed its own blueprint. That self-reference powers our two ways of proving a problem is undecidable:
Diagonalization (Rebel)
To prove HaltChecker cannot exist, copy an input blueprint X into both slots of HaltChecker and flip its prediction (HALTS→loop,LOOPS→halt), so Rebel disagrees with every row k of the machine-versus-blueprint table at box (k,k), and feeding [Rebel] into Rebel contradicts HaltChecker's prediction. Note that undecidability only rules out a single universal algorithm for all programs; you can still decide halting for specific machines like Add-One or GO by inspecting their rules directly.
Reduction (Blueprint Builder)
When a problem asks you to prove a new analyzer Tool X is impossible, don't build a new diagonal table and never try to build Tool X out of HaltChecker (wiring it backwards proves the vacuous statement "if HaltChecker exists, then Tool X exists"). Instead, assume Tool X exists and build HaltChecker out of it by having a Blueprint Builder write a modified rulebook M′ (such as Stage 1 writing I on a blank tape and Stage 2 running M) so that Tool X(M′) answers whether M halts on I.
7
Practice
1
A Turing machine that adds 1 to a binary number is given by the six five-tuples (Current State,Read,Write,Move,Next State):
Trace this machine step-by-step on the input tape _ 101 _, starting in state WALK_RIGHT with the head on the leftmost 1. For each step, give the tape contents, the head position, and the current state until the machine halts.
Watch out: a common slip is switching to ADD_ONE on Step 3 as soon as the head reaches the rightmost 1, which gives a 4-step trace; the machine doesn't know it has reached the end until it steps onto _.
Keep the head in WALK_RIGHT until it actually steps onto the right blank _; only then does it step left and switch into ADD_ONE.
Using brackets [] to mark the square under the head:
Start: _ [1]01 _, state WALK_RIGHT.
Step 1 (read 1→write 1,R,WALK_RIGHT): _ 1[0]1 _, state WALK_RIGHT.
Step 2 (read 0→write 0,R,WALK_RIGHT): _ 10[1] _, state WALK_RIGHT.
Step 3 (read 1→write 1,R,WALK_RIGHT): _ 101[_], state WALK_RIGHT. (Watch out: a common slip is switching to ADD_ONE on Step 3 as soon as the head reaches the rightmost 1, which gives a 4-step trace; the machine doesn't know it has reached the end until it steps onto _.)
Step 4 (read _→write _,L,ADD_ONE): _ 10[1] _, state ADD_ONE.
Step 5 (read 1→write 0,L,ADD_ONE): _ 1[0]0 _, state ADD_ONE.
Step 6 (read 0→write 1,S,HALT): _ 1[1]0 _, state HALT.
The machine halts after 6 steps with _ 1[1]0 _ in state HALT.
2
Trace the 0n1n decider on the two rejected inputs (a) 001 and (b) 010, starting in state START on the leftmost symbol. For each input, find the step number, the state, the symbol read, and the tape contents at the exact moment the machine enters REJECT.
After RETURN_LEFT hits an X and steps right into START, check carefully whether the symbol under the head is 0 (starting another FIND_1 round) or Y (switching into CHECK_REST).
(a) On input 001:
Start: _ [0]01 _, state START.
Step 1: _ X[0]1 _, state FIND_1.
Step 2: _ X0[1] _, state FIND_1.
Step 3: _ X[0]Y _, state RETURN_LEFT.
Step 4: _ [X]0Y _, state RETURN_LEFT.
Step 5: _ X[0]Y _, state START.
Step 6: _ XX[Y] _, state FIND_1. (Don't confuse reading Y in FIND_1 with reading Y in START: in FIND_1 the head is still looking for a 1 to match the second X, so it steps right over Y and stays in FIND_1 instead of switching to CHECK_REST.)
Step 7: _ XXY[_], state FIND_1.
Step 8: In state FIND_1, the head reads _ and transitions via (_,S,REJECT) to state REJECT with tape _ XXY[_].
(b) On input 010:
Start: _ [0]10 _, state START.
Step 1: _ X[1]0 _, state FIND_1.
Step 2: _ [X]Y0 _, state RETURN_LEFT.
Step 3: _ X[Y]0 _, state START.
Step 4: _ XY[0] _, state CHECK_REST. (Because START saw Y, it switched to CHECK_REST rather than staying in START to cross off the second 0.)
Step 5: In state CHECK_REST, the head reads 0 and transitions via (0,S,REJECT) to state REJECT with tape _ XY[0] _.
3
Write the rulebook as a list of five-tuples (Current State,Read,Write,Move,Next State) for a Turing machine that starts on the leftmost digit of a non-empty binary string (surrounded by blanks _), flips every bit (0 becomes 1 and 1 becomes 0), and returns the head to the leftmost digit of the string before halting in state HALT.
Use one state FLIP to walk right while flipping bits until it hits the right blank _, and a second state RETURN to walk left past the flipped bits until it hits the left blank _.
(Be careful on rule 6: writing (RETURN,_,_,S,HALT) leaves the head stopped on the blank _ to the left of the string instead of stepping R back onto the leftmost digit.)
4
Consider the Turing machine with start state A, working state B, halting states ACCEPT and REJECT, and the following transition table:
Determine what this machine does when started on the leftmost symbol of (a) 11, (b) 01, and (c) 00. Does this machine decide a language? Explain why or why not.
After tracing all three two-bit strings, check the definition of a decider: is halting on some inputs enough?
(a) On 11: Starts in A on the first 1; rule (A,1)→(1,S,ACCEPT) immediately halts in ACCEPT after 1 step.
(b) On 01: Starts in A on 0→ moves right onto 1 in state B→ rule (B,1) moves right onto _ in state A→ rule (A,_) halts in REJECT after 3 steps.
(c) On 00: Starts in A on the first 0→ moves right onto the second 0 in state B→ rule (B,0) moves left back onto the first 0 in state A→ repeats forever in an infinite loop.
It is tempting to say yes because the machine accepts strings like 11 and rejects strings like 01, but to decide a language a Turing machine must halt in ACCEPT or REJECT on every possible input; because it loops forever on 00, this machine does not decide a language.
5
In the proof that HaltChecker cannot exist, the Rebel machine wires an opposite-day attachment onto HaltChecker. Suppose instead we build a machine called Copycat that wires a matching attachment onto HaltChecker: on input Blueprint X,Copycat copies X into both slots of HaltChecker, and if HaltChecker outputs HALTS,Copycat immediately halts; if HaltChecker outputs LOOPS,Copycat jumps into an infinite loop. (a) What happens if we feed the Blueprint of Copycat into Copycat? Why doesn't this prove HaltChecker is impossible? (b) Explain the difference between Copycat and Rebel in terms of Cantor's diagonal table of machines M1,M2,M3,… and inputs [M1],[M2],[M3],….
Check whether HaltChecker's prediction matches or clashes with what Copycat([Copycat]) actually does in box (k,k).
(a) When we feed [Copycat] into Copycat, both cases are completely consistent: if HaltChecker predicts HALTS,Copycat halts, matching the prediction; if HaltChecker predicts LOOPS,Copycat enters the infinite loop and loops forever, also matching the prediction. (Don't assume self-reference alone creates a paradox. Without flipping the output, neither case produces a contradiction, so Copycat cannot rule out HaltChecker.) (b) On the infinite table where row k is Copycat, the diagonal box (k,k) represents Copycat running on [Copycat]. Because Copycat matches whatever HaltChecker puts in box (k,k), it can sit on row k without contradiction whether box (k,k) says HALTS or LOOPS, whereas Rebel flips every diagonal entry so its behavior at (k,k) is the exact opposite of box (k,k), making it impossible for Rebel to exist on any row k of the table.
6
Suppose someone claims to have built a Turing machine called Print$Checker that takes a single blueprint M′ as input and always halts with YES if M′ ever writes the symbol $ when started on a blank tape, and NO if M′ never writes $ when started on a blank tape. (a) Prove that Print$Checker cannot exist by reducing HaltChecker to it. Specify how the Blueprint Builder constructs M′ from a blueprint M and input I, and verify both cases. (b) A classmate attempts the reduction by saying: "Assume HaltChecker exists. Given M′, run HaltChecker on M′ and a blank tape, and see if $ is on the tape when it finishes." Explain why this reduction is wired backwards and proves nothing about Print$Checker.
For (a), make sure M cannot accidentally print $ on its own while running on I, and wire M′ so that $ is written if and only if M reaches a halting state on I.
(a) Assume for contradiction that a working Print$Checker exists. We build HaltChecker on any input blueprint M and input I as follows:
First, if M or I uses the symbol $, rename $ everywhere in M's rulebook and in I to a fresh symbol so M never writes $ on its own. (Skipping this renaming step is a classic slip: if M printed $ partway through an infinite loop, Print$Checker would output YES even though M never halted.)
Pass M and I into a Blueprint Builder that writes the five-tuples of a new machine M′ with three stages on a blank tape:
Stage 1: Write I onto the blank tape and return the head to the leftmost symbol of I.
Stage 2: Run the rules of M.
Stage 3: Replace every halting transition of M with a transition to a new state WRITE_$ that writes $ on the tape and then halts.
Feed M′ into Print$Checker and output HALTS! if it says YES, or LOOPS! if it says NO.
Verify both cases:
If M halts on I, then M′ started on a blank tape writes I, runs M until M finishes, enters WRITE_$, and writes $, so Print$Checker(M′)=YES and our outer box outputs HALTS!.
If M loops forever on I, then M′ writes I in finite time and loops forever inside Stage 2 without ever reaching WRITE_$ or writing $, so Print$Checker(M′)=NO and our outer box outputs LOOPS!.
Since the Blueprint Builder only edits text and always finishes in finite time, a working Print$Checker would give a working HaltChecker; because HaltChecker cannot exist, Print$Checker cannot exist.
(b) A reduction must prove the conditional Print$Checker exists⟹HaltChecker exists, whose contrapositive proves Print$Checker cannot exist. Assuming HaltChecker exists and trying to build Print$Checker out of it argues the converse (HaltChecker exists⟹Print$Checker exists), which is vacuous because HaltChecker does not exist (and fails anyway because a machine M′ that loops forever could still write $ before entering its loop).