1. Overview

The halting problem is a well-known fundamental theory in computer science. It highlights the boundary of computability and provides a way to identify the algorithmic problems that can’t be solved in a finite time.

In this tutorial, we’ll explore the halting theory in detail with examples and formal proof.

2. Halting Problem

2.1. Problem Statement

When we execute different programs on a computer, they can be categorized into three distinct classes based on respective halting behaviors:

types of halting

The first category of programs halts quickly for any input we provide. Furthermore, some programs might take considerably longer to finish execution but eventually halt. Moreover, some programs don’t halt at all and keep running indefinitely.

The halting problem facilitates understanding computation limits and establishes that we can’t solve all the programs in a finite time.

Now, we discuss the formal problem statement. Let’s say we have an arbitrary program, Prog. Additionally, the given input for this program is denoted as input_data. The halting problem asks whether there is a way to determine whether the program Prog halts for the given input, input_data.

Moreover, in 1936, Alan Turing proved that the halting problem isn’t solvable, which means no methods or algorithms can tell us whether a program for a given input would halt or not.

2.2. Examples

Now, we know the basic theory of the halting problem. Moreover, we explore some examples to get a practical understanding.

Let’s take a look at the first example:

sample_prog1(in_num):
    for i in range(in_num):
        print("Line {i + 1}")
    print("Program halts")

Here, in_num is a positive integer. In this example, for any input, the program will indeed halt.

Furthermore, let’s see another example:

sample_prog2(in_num):
    while True: 
        print("Hello\n")

In this case, the program doesn’t use the input provided by the user. Moreover, the while loop ensures the program runs indefinitely regardless of the input provided. Hence, this program will not halt for any given input.

3. Formal Proof

We already know it’s impossible to determine whether an arbitrary program will halt with any given input. We need to prove that no algorithm or program can solve the halting problem to prove the halting theory.

We’re going to prove it by contradiction. Therefore, we first assume that such an algorithm exists. However, we eventually prove by contradiction that writing such an algorithm is impossible.

Let’s assume that there exists an algorithm Halt_Testing for solving the halting problem. Additionally, the algorithm takes two inputs: a program Prog and an input in_num.

Moreover, we assume the algorithm can determine whether the program Prog will halt with input in_number or not. Hence, Halt_Testing(Prog, in_num) returns True if the program halts with the given input in_num and False otherwise:

diagram of a halting algorithm

Now, we define a new function that takes a single program as input:

Reverse_Halt(Prog)
    if (Halt_Testing(Prog, Prog) == True)
        loop forever
    else
        return 0

In this function, we provide a program as input. If the Halt_Testing algorithm returns True for the input program Prog, the function Reverse_Halt goes into an infinite loop and never halts. On the other hand, if the Halt_Testing algorithm returns False for the input program Prog, the function Reverse_Halt halts immediately.

Additionally, we define the function Reverse_Halt to take a program as its sole input. However, in this case, we use the program Prog as both the program and the input for the Halt_Testing algorithm to create a self-referential structure. Creating the self-referential structure is necessary to prove the halting problem by contradiction.

Moreover, let’s explore the case when we call the function Reverse_Halt on itself:

Reverse_Halt(Reverse_Halt) 
    if (Halt_Testing(Reverse_Halt, Reverse_Halt) == True) 
        loop forever 
    else 
        return 0

Thus, if the Halt_Testing algorithm returns True for the given input program, the function Reverse_Halt halts. However, by the definition of the function Reverse_Halt, it should enter an infinite loop without halting when the algorithm Halt_Testing returns True. This gives us a contradiction.

Similarly, the function Reverse_Halt runs forever when the Halt_Testing algorithm returns False. Again, by its definition, Reverse_Halt should halt when the Halt_Testing algorithm returns False. This gives us a contradiction.

Hence, based on the contradictions, we proved that our initial assumption of an algorithm that can determine whether a program with a given input halts is wrong. Therefore, no algorithm can decide whether a program will halt for the given input.

4. Conclusion

In this article, we discussed a fundamental problem in computer science: the halting problem. We explained the basic concept of the problem along with some examples.

Finally, we proved by contradiction that we can’t design an algorithm that can determine whether a program will halt for the given input.


原始标题:Introduction to the Halting Problem

« 上一篇: Event Storming