What keeps me from lying and annotating a functions a âAlways Returnsâ?
A termination checker.
How would the compiler know (for sure) that I was right (halting problem)?
A termination checker, ideally, would be both sound (never allows a non-terminating function) and complete (never forbids a terminating function). This is impossible, however - this is, in fact, the core distinction between the complexity classes R and RE being unequal.
However, you can choose which to give up. Modern termination checkers are sufficiently powerful that they will permit pretty much any deterministically-terminating function you care to come up with. They are sound, and they are close enough to completeness that it is not much of an issue in practice.
For example: If I had a function that generated a random u128 and then went into a loop and kept generating random u128âs until there was a match and then broke out of the loop, that function would technically âalways returnâ.
No, it would not. The probability of returning would approach unity, but itâd never reach it - for example, your RNG might enter a cycle, and never return to that first u128.
âArbitrarily small possibility of not returningâ is still different from âzero possibility of not returningâ.