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
7
u/Ma4r Jun 01 '26
I gave an example on this comment chain with the OP with collatz as an example. Basically if you put the post condition of a possibly non halting program as pre condition to the next, the postcondition is only valid by the condition of vacous truth. From the deecription of the program, it should not have compiled in the first place. Sure enough the language actually doesn't support unbounded recursion where you are able to define the collatz, so it's really no stronger than the typical languages with ADT and graded loops.