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)
View all comments
Show parent comment
1
u/Randozart Jun 01 '26
Thank you for the insights. Regarding the complex algorithms, I've been experimenting with aggressively folding them wherever possible.
If a precondition is always going to be between X and Y, and this triggers a predictable line of logic leading to Z, we could hypothetically either fold the entire calculation into a LUT, or find a formula that accomplishes the same as the actual code.
Which is neat, because it means the verification gate isn't just a reason for the compiler to look over your shoulder, but also helps with optimization passes at compile time.
But originally, I just wanted to have the compiler shout at me more if I made wrong assumptions, which really helps with time writing the language and I don't need to do safety checks at runtime.