Just read the docs™
Just read the docs™
For the curious, here's an official example in the SPARK language, that gives a proven-correct sort function. (A complete proof of correct program behaviour, not just absence of undefined behaviour). As you can see, proving correctness is a challenge that permeates every inch of the code. [2][3]
Microsoft's Dafny language is similar. [4]
[0] https://yoric.github.io/post/rust-typestate/
[1] https://news.ycombinator.com/item?id=21413174
[2] https://github.com/AdaCore/spark2014/blob/76e08d279c/docs/ug...
[3] https://github.com/AdaCore/spark2014/blob/76e08d279c/docs/ug...
I don't think it's really even possible for a language to force the programmer to give a proper formal specification.
We may want the formal spec to grant some leeway, i.e. not to specify the exact output/behaviour, but instead to express certain important constraints. For instance, we may not care which traffic-light turns green first, so long as the system preserves the important properties about safety and liveness.
If we wish to support such flexibility, we can't stop the programmer from expressing all output states are valid.
If we don't wish to support such flexibility, what we're really doing is comparing against a reference implementation.
The language, probably not. The standard library can certainly require detailed-enough formal preconditions to make its code defect-free, meaning that any remaining bugs can then be directly traced to the programmer's code.
That's an interesting idea. Like Java interfaces, but with formal contracts.
I doubt that putting these contracts into the standard-library would give you good proof-coverage though.
for example check out the err::checked() theory which enforces that a function call must be followed by err::check.
similarly, i expect a community will come up with other standard contracts such as must_not_copy() for secret values that may not be copied.
[1] https://en.wikipedia.org/wiki/Typed_lambda_calculus
[2] https://en.wikipedia.org/wiki/Type_theory
[3] https://en.wikipedia.org/wiki/Foundations_of_mathematics