Getting Stuck: Writing a Turing Machine Simulator
This entry in my Getting Stuck series started with what I thought was a fun challenge: write a Turing machine simulator from scratch.
It sounded clean and manageable. After all, how hard could it be? A tape, a head, some states, some rules. By the end, I had written code that ran… and then refused to stop. I had also stumbled into undecidability theorems I’d only read about in textbooks.
This is the story of how I got stuck — and what climbing out looked like.
Step 1: The Innocent Beginning
A Turing machine is described in almost childlike terms:
-
An infinite tape with symbols on it.
-
A head that can move left or right.
-
A finite set of states.
-
A transition function telling the machine what to do next.
So I wrote down the core class in Python:
class TuringMachine:
def __init__(self, tape, transitions, start_state, accept_state, reject_state):
self.tape = list(tape)
self.head = 0
self.state = start_state
self.transitions = transitions
self.accept_state = accept_state
self.reject_state = reject_state
def step(self):
symbol = self.tape[self.head]
key = (self.state, symbol)
if key not in self.transitions:
self.state = self.reject_state
return
new_state, write_symbol, move = self.transitions[key]
self.tape[self.head] = write_symbol
self.state = new_state
self.head += -1 if move == "L" else 1
I was optimistic. Next up was testing it on the simplest machine I could think of: recognizing strings of as with even length.
Step 2: First Crash
Transitions for the “even length” checker:
transitions = {
("q0", "a"): ("q1", "a", "R"),
("q1", "a"): ("q0", "a", "R"),
("q0", "_"): ("ACCEPT", "_", "R"),
("q1", "_"): ("REJECT", "_", "R"),
}
Running it on "aaaa_":
tm = TuringMachine("aaaa_", transitions, "q0", "ACCEPT", "REJECT")
while tm.state not in ("ACCEPT", "REJECT"):
tm.step()
print("Result:", tm.state)
Boom.
IndexError: list index out of range.
The head had marched off the right edge of my tape. My machine had fallen into the void.
Step 3: Mistaken Fixes
The obvious fix? Just make the tape longer.
self.tape = list(tape) + ["_"] * 1000
This worked — until I gave it a longer input, and it ran out of blanks again. My tape wasn’t infinite; it was just really wide.
Then I got clever. What if the tape “wrapped around”?
self.head = (self.head + 1) % len(self.tape)
No more index errors! But the logic was broken. The head that was supposed to drift forever to the right instead looped back and scribbled on the start of the tape. Suddenly, my machine was “accepting” inputs it should’ve rejected.
I had “fixed” the bug in a way that destroyed the whole model.
Step 4: The Real Fix
The right way to simulate infinity is… not to. It’s to cheat intelligently.
If the head walks off the left end, insert a blank at the start.
If it walks off the right end, append a blank at the end.
def step(self):
if self.head < 0:
self.tape.insert(0, "_")
self.head = 0
elif self.head >= len(self.tape):
self.tape.append("_")
symbol = self.tape[self.head]
key = (self.state, symbol)
if key not in self.transitions:
self.state = self.reject_state
return
new_state, write_symbol, move = self.transitions[key]
self.tape[self.head] = write_symbol
self.state = new_state
self.head += -1 if move == "L" else 1
With this change, the machine finally behaved properly:
-
"aa"→ accepted. -
"aaa"→ rejected. -
"aaaa"→ accepted.
I had successfully built an actual Turing machine simulator.
Step 5: Running Into Theoretical Walls
At this point, I was feeling confident. Time to test something bigger: a palindrome recognizer.
But then the simulator… just kept running. And running.
# Contrived infinite loop
transitions = {
("q0", "a"): ("q0", "a", "R"),
}
My program hung forever. I was convinced I had a bug — until I remembered a lecture from my theory course.
The halting problem.
It’s not just a story in textbooks. You literally cannot, in general, decide whether a Turing machine will halt on a given input. My simulator wasn’t broken. It was faithfully reproducing one of the deepest truths of CS: sometimes, computation just doesn’t end.
Step 6: Climbing Out
At first, I was frustrated. My “palindrome machine” was hopeless. I considered adding an artificial timeout: “if it runs 10,000 steps, just stop.” That let me keep experimenting, but it wasn’t a real solution.
Eventually, I accepted that this wasn’t a programming bug. It was a fundamental feature of the model. A simulator of Turing machines will always face inputs that lead to infinite loops.
And that’s when it hit me: I hadn’t just built a quirky toy. I had experienced the line between “things we can compute” and “things we cannot.”
Reflection
This round of Getting Stuck was unlike my earlier ones. It wasn’t about fixing a messy bug or optimizing away inefficiency. It was about realizing that getting stuck can sometimes mean reaching the limits of computability itself.
-
I started with the naive assumption that “infinite” just meant “big enough.”
-
I tried wrong fixes that worked mechanically but broke the math.
-
I finally built a working simulator, only to slam into undecidability.
In the end, my code works — but it also reminds me that not every problem is solvable. Sometimes getting stuck is not an accident; it’s a fundamental property of the landscape.
Comments
Post a Comment