---
title: The Halting Problem
lang: en-GB
author: Maggie Harley
date: 09/03/2025
---
There are things computers can't do. More pertinently: there are questions computers cannot answer.

It's easy to think of questions that don't have correct answers. For example:

> Is the answer to this question "no"?

> What did I have for dinner on the 1st of January, 1029?

These questions _can_ be answered, but anything that attempts to answer them correctly will run into problems. Computers cannot accurately answer these questions for us, because nothing can. These are not the kinds of questions we're interested in today; there are questions that computers cannot accurately answer, but that - unlike the ones above - _do_ have answers.

## Glossary of Terms

### Turing Machine

A Turing Machine (abbreviated as TM) is a hypothetical kind of computer, made up by Turing to be used in thought experiments about computation.

![Diagram of a simple turing machine.](./assets/tm-light.svg)

We'll be using TMs in the proof that computers can't solve some problems. What's important to understand about them for this purpose is that they accept input on their _tape_: an infinitely-long series of cells, each of which can store a single value. While operating, the TM moves a _head_ along the tape, reading the value contained within and reacting to the value. The head can also write values to the tape, for the purpose of writing down working.

TMs are capable of solving every problem that a modern-day computer is able to. They're actually able to solve any problem any computer ever made will be able to. The proof of this is outside the scope of what we're looking at here, but it's important to note that if a TM can't solve a problem, no real-world computer can.

### Halting

After operating on an input for a while, a TM may eventually _halt_. When it does, it either _accepts_ or _rejects_ the input. This can be used as a way of answering yes-or-no questions: you input the question on the input tape, and after a while the TM either accepts (replies "yes" to the question) or rejects (replies "no"). You can also look at what a TM writes to its tape as a kind of output, but for the Halting Problem we're only concerned with accepting/rejecting as a TM's way of responding to a question.

TMs _may_ halt, but there is every possibility that it never does - it could run forever! In this case, the TM will never accept or reject its input; in other words, it will never answer the question it was asked.

Non-halting is quite a big problem, for two reasons. The first is that questions asked to TMs actually have three responses (accept, reject, run forever), which doesn't map neatly to the yes/no questions we'd like answers to. The second is that it's hard to tell the difference between a TM that won't ever halt, and a TM that hasn't halted _yet_.

Ideally, we'd like to design TMs so that they can accept or reject, but never run forever. The real-world equivalent here is that we'd like to be able to write programs that we're sure will _eventually_ complete. That way, if a program is taking a long time to run, we can be confident that leaving it running isn't a waste of resources, because it will, at some point, finish, and must therefore be making progress towards that finish.

### Words and Languages

Questions can be grouped into sets of questions that are very similar. For example, "is 3 the square root of 9?" is a question very similar to "is 2 the square root of 9?". When we think of TMs, we tend to think of them as being able to solve a whole class of questions. So, you could have a TM that answers questions in the form of "is X the square root of 9?", and that takes X as its input.

When we ask a TM a question, we aren't really giving it a _question_. We have to encode the question in some series of characters that we can put on its tape. In theory, there are a lot of things we could ask questions about that we could convert into characters this way. For example, colours are often represented as characters in hex code. Being able to encode things into characters makes it possible to input them into TMs, and therefore makes it possible to ask questions (and get answers) about them.

In computational theory jargon, the series of characters that represents a question (the input to a TM) is referred to as a _word_, and a collection of words is called a _language_. When we create a TM that answers a certain class of questions, what that means is that it accepts all of the words that represent questions with the answer "yes". There is a _language_ that the TM _recognises_, and the TM accepting a word means that the word is part of the language.

#### Descriptions of Turing Machines

If we want to ask questions about things, we need to be able to encode them as words. If we want to ask questions about TMs, we need to be able to _describe_ TMs on a TM input tape. There are methods for describing TMs in this way, though the specifics are outside the scope of this article. What's important is that it's _possible_ to make a method for encoding a TM as a word, so that it can be input as part of a question for another TM. We'll represent the description of a TM A in this text as ⟨A⟩.

### Recognisability and Decidability

A TM _recognises_ a language if it accepts all words in that language, and does not accept other words. A TM _decides_ a language if it accepts all words in that language, and rejects all other words.

A TM that recognises a language is sometimes useful, but ultimately its utility has limitations. Consider the situation where a TM is processing a word, and hasn't halted: the situation is ambiguous, as it could be one of:

- the word isn't part of the language, and the TM is running forever;
- the word isn't part of the language, but the TM is taking a long time to reject it;
- the word is part of the language, but the TM is taking a long time to accept it.

Because of the first option, it's impossible to tell if you should sit and wait for the TM to halt, or accept that the TM _won't_ halt and mark this word down as not in the language. TMs that decide a language don't have this problem, and as a result are much preferred. Bringing this back to the real world, it's very handy to know that your program won't get stuck - you can just set it up to process a list of inputs you need computed, and leave it alone knowing that it's making progress until it's done. A program that sometimes sticks needs supervision so that someone can unstick it, and also means that you won't get solutions to some of your problems.

## The Halting Problem

It's annoying when we want to automate a process, but our program has the potential to get stuck running forever. Surely, by inspecting a program, we can tell if it has the possibility of getting stuck for a given input? And, if it's possible to do so, surely it's possible to automate this process, by creating a program that checks other programs?

Well, it turns out that while it is possible to do this kind of check on a case-by-case basis, creating a _general_ mechanical method for determining this is impossible. What this means is that you can't invent a program that can check other programs to make sure they won't get stuck forever, without it also having the chance of getting stuck forever. This problem ("will program X ever halt when given input Y") is called the _Halting Problem_, and it is _undecidable_, meaning that no TM decides it.

### The Halting Problem Is Recognisable

There are TMs that recognise the Halting Problem - that is to say, there are TMs that accept all TM-input pairs that do eventually halt, and don't accept any other pairs. For example, a TM that pretends to be the TM it receives as an input, and accepts once the TM it's pretending to be halts, is a TM that recognises the Halting Problem. Being able to recognise the Halting Problem isn't useful in this case though, since that means the TM can still loop forever on some inputs, which means that we can't tell if it will accept the input if given more time. We really need a TM that can _decide_ the Halting Problem.

### The Halting Problem Is Undecidable

It's impossible to invent a TM that decides the Halting Problem. No such TM can ever possibly exist.

We're going to prove this using a method called _proof by contradiction_:

1. we assume the _opposite_ of what we want to prove;
2. we follow some reasonable steps and end up at a contradiction;
3. contradictions are impossible, so something we've assumed must be wrong;
4. we only assumed one thing, so its opposite (that is the opposite of the opposite of what we want to prove, or simply what we want to prove) must be true.

In this case, we'll assume the Halting Problem is decidable, show that from that assumption a contradiction occurs, and so prove that its opposite (the Halting Problem is _not_ decidable) is true.

#### Proof of Undecidability

If the halting problem is decidable, that means we can make a TM that decides it. Let's call this TM _Decider_. _Decider_ takes as input, two things combined together:

1. a description of a TM;
2. any other input.

It's possible to get a description of a TM that can be encoded in a TM's tape as input. It's also possible to combine two things together to create one input.

_Decider_ decides the Halting Problem: when given these two things as input, it accepts if the TM described in input 1 would halt when given input 2 as its own input (and rejects if it would run forever). _Decider_ never loops forever, since TMs that decide a language never do.

Let's think up another TM, and let's call it _Rascal_. _Rascal_ takes the same kind of input as _Decider,_ and does the following:

- if _Decider_ would accept the input I've just been given, then loop forever;
- if _Decider_ would reject the input I've just been given, then accept.

The result is that _Rascal_ behaves in an opposite way to the TM it's been passed as part of its input: if that TM would halt, _Rascal_ doesn't, and vice versa.

What happens if we pass _Rascal_ itself as input? _Rascal_ depends on what _Decider_ would say, but no matter what _Decider_ says, a contradiction occurs:

- If _Decider_ accepts when passed _Rascal_, then it's claiming that _Rascal_ will halt, but _Rascal_ sees that _Decider_ would accept and so loops forever. Therefore, _Decider_ was wrong, which is a contradiction.
- If _Decider_ rejects when passed _Rascal_, then it's claiming that _Rascal_ will loop forever, but _Rascal_ sees that _Decider_ would reject and so halts. Therefore, _Decider_ was wrong, which is a contradiction.

_Decider_ must either accept or reject; it can't loop forever since it decides a language. Therefore, a contradiction is unavoidable. Since we've encountered a contradiction, the one thing we assumed - that the Halting Problem is decidable - must be incorrect.
