r/ProgrammingLanguages • • Jun 01 '26

Discussion Hoare Triples for improving performance

I have been working on a language (which I won't share here due to the heavy use of AI for prototyping) that makes use of Hoare triples as first class citizens, albeit in a weakened form. For many functions, you don't need to declare the pre AND postcondition, just one suffices.

This was initially a built in safety feature for the compiler's proof engine, but when assembling the LLVM backend I noticed something really neat: because the bounds of some functions are extremely predictable due to the Hoare triple, I could more aggressively fold the execution graph based on inferred values.

In a way, this means that the safety feature becomes an optimization feature at the same time. It's not something I've seen in other languages I've explored, not even SPARK or other formal proof based languages. Is there any research or literature on this?

Because I've noticed it has allowed me to more aggressively optimize the compilation than even C, and currently my benchmarks are actually pointing out I'm beating C in execution speed without compromising on safety (one implementation saw 0.15s vs. 0.24s for C on 50m iterations on a similarily written program, though I do need to figure out if I can optimize the C version more for a completely fair comparison)

24 Upvotes

66 comments sorted by

View all comments

Show parent comment

2

u/ccapitalK Jun 01 '26

To be fair to the author, an infinite loop without observable side effects is UB in C++ (and C if it's not a constant expression), it's a arguably a rough spot in modern production grade compilers as well.

3

u/flatfinger Jun 01 '26

If the authors of the C Standard had intended that the failure of a side-effect-free loop with a statically reachable exit to terminate be treated as "anything can happen UB", then saying that all such loops shall terminate would have been more concise and unambiguous than saying such loops "may be assumed to terminate". A vastly more useful thing for the Standard to have said from the standpoint of optimizing correct programs that might receive data from untrustworthy sources would be that compilers may assume that code as written does not rely upon seemingly-side-effect-free loops having side effects, and compilers may treat a side-effect-free loop with a single statically reachable exit as side-effect-free, without regard for whether it is.

Given a function like:

unsigned test(unsigned x)
{
  unsigned i=1;
  while((i & 0x7FFF) != x)
    i *= 3;
  return i;
}

such allowance would permit calling code to either omit the function call if the return value was ignored, execute the code as written and then treat the function as having established the post-condition that the value that was passed had been an odd number from 1 to 32767, or even rewrite the code as something like:

    if (x & -32769u) do { __dummy_side_effect(); } while(1);
    ... Code may now assume x was an odd number 1..32767

In many real-world scenarios, scenarios where it would be acceptable to allow some invalid inputs to block program execution for more than a billion years unless or until it is forcibly terminated via external means, but not to allow such inputs to trigger arbitrary-code-execution exploits. The effect of making the Standard more "permissive" would not be to increase the number of useful optimization that could be applied to correct programs, but rather to eliminate from the set of correct programs those for which treating seemingly-side-effect-free loops as being side-effect-free would be a useful optimization.

2

u/Ma4r Jun 01 '26

Well yeah, this is an answer to the question of why lean doesn't do what his language does