r/ProgrammingLanguages • u/Randozart • 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)
16
u/Ma4r Jun 01 '26 edited Jun 01 '26
Hoare triples themselves do not work as expected when your program possibly do not halt. They are only correct by vacous truth in those conditions. Proof verifiers are able to verify the correctness of proofs about non halting programs
5
u/curtisf Jun 01 '26
It's definitely a major part of formal verification. There are two main ways that formal verification can lead to speed ups:
I remember the second bullet being talked about concretely by... I believe someone working on Idris and talking about how they could implement more more complicated but more efficient variable lookup data-structures thanks to bootstrapping in a dependently typed language
The combination of these two things is the reason why things like Project Everest get made. Performance, complexity, and correctness are usually at odds, but robust formal verification is one way to better make a tradeoff between them
3
u/reini_urban Jun 01 '26
I've used in that in my compiler to optimize loops. And many runtime bounds checks went from run-time, to compile - time.
3
u/Pzzlrr Jun 02 '26
Meanwhile, prolog "Look what they need to mimic a fraction of our power."
2
u/TheChief275 Jun 02 '26
Do share the C code when making such claims
2
u/Tasty_Replacement_29 Bau Jun 02 '26
Trying to beat C is probably not what you want to try (I guess you already know this). Good, and achievable goals are to be as fast as C, but safe, more ergonomic, or both.
There are semantic invariants (pre/postconditions, Hoare triples, C++23 std::assume, __builtin_unreachable, __builtin_assume, __assume), and there are (probabilistic) hints like __builtin_expect. Both invariant and hints make sense. Adding proofs (as in Lean etc.) will help with speed and safety.
For the developer, proofs add some overhead, unless if they are fully automated. (And there's a bit of a compile-time slowdown, but not too much.) For a new language, what also might make sense is adding restrictions in the type system, eg. range-restricted types, or value-dependent types. In my language, I use those to avoid array bound checks. I think it works quite nicely.
2
u/david-1-1 Jun 02 '26
Are these Hoare triplets equivalent to what David Gries presents in his book "The Science of Programming"?
2
u/Sad-Grocery-1570 Jun 01 '26
I’m curious: how do you verify the correctness of user-written Hoare triples? What kind of specifications can users write in Hoare triples? And does your language support arbitrary memory operations? (That would probably require separation logic or something similar to describe.)
-1
u/FruitdealerF Jun 01 '26
The AI rule in this sub is stupid. Clearly you have something that is worth sharing.