Halting Problem
- Imagine there is a function
bool doesItHalt({function f, input i})that returns if the parameter functionf(i)halts or not. - Now consider the following functions:
cpp
pair duplicator(input i) { return {i, i}}
bool invertHalt(bool b) { if(b) { while(true); // hangs forever return 0; } else { return 0; }}- Essentially, if
f(i)halts, theinvertHaltwill hang (i.e., wouldn't halt), and iff(i)hangs, theinvertHaltwill halt. - Let us consider the composition of the two functions:
cpp
bool unknown(input i) { auto a = duplicator(i) // a = {i, i} auto b = doesItHalt(a) // does i(i) halt? auto c = invertHalt(b) // hangs if i(i) halts and vice versa.}- Will
unknown(unknown)halt? What shoulddoesItHalt({unknown, unknown})return? - Let us suppose it will return
true. Then, it means thatdoesItHalt({unknown, unknown})returnedfalsebecauseinvertHalt(b)would've hung otherwise. Therefore, this contradicts our supposition thatdoesItHalt({unknown, unknown})returnstrue. - Let us suppose it will return
false. Then, it means thatdoesItHalt({unknown, unknown})would returntruebecauseinvertHaltwouldn't hang otherwise. Therefore, this contradicts our supposition thatdoesItHalt({unknown, unknown})returnsfalse. - Therefore,
unknowncannot hang nor halt; therefore, no suchdoesItHaltcan exist.