r/computerscience • • Dec 31 '25

Halting problem (Can a program contain itself?)

Please correct me if I'm wrong here. The usual proof is about a program passing its own source code to the machine and then changing the result to be wrong... But what if the running program and the source code it passes are not the same program?

If a running program reads its source code from an external file after it already started running, how do you know that its the same exact code as what is already running? It could be a different program.

If the source code of the program contained a copy of its own source code, it wouldn't actually be the same source code as the original program unless infinitely recursive and therefore impossible.

Basically my thinking is that the whole thing requires a program to contain itself which is impossible.

Does this break the proof?

1 Upvotes

47 comments sorted by

View all comments

Show parent comment

2

u/blacksteel15 Dec 31 '25

When you say a program that passes in itself as input, how does it do it? It passes some data or source code? How do you know it is the same as the running program? What if it doesn't pass in itself at all?

It seems like you don't understand how proofs by contradiction work. A proof by contradiction follows the pattern:


-We want to prove statement S.

-Assume S is not true.

-Provide at least 1 example of a logical contradiction caused by this.

-Therefore the assumption that statement S is false must be false.


For example, here's a very simple one:

I want to prove that there is some number x such that x + 1 = 3.

-Assume there is no such number.

-Let x = 2. x + 1 = 2 + 1 = 3

-This contradicts our assumption, so the assumption must be false.

-Therefore there is at least 1 number x for which x + 1 = 3.


Your question is equivalent to, in the above proof, "What if we let x be something other than 2?" Well, in that case it wouldn't produce a contradiction, so we can't say our assumption must be wrong. But that's why we picked 2. It was a deliberate choice that we control. We're demonstrating that there is a case that leads to a contradiction, not assuming that we're in the case that creates a contradiction.

Going back to your original question, yeah, obviously you could pass source  code into the program other than its own. But we know that the source code it got is its own because that's what we chose to pass it.

0

u/Nytra Dec 31 '25

So what you're saying is that some other program calls P with its own code, that means that P is not the program that is running, so if the machine says P will halt, then some other program decides not to halt, that is still correct