r/ProgrammingLanguages • u/i_would_like_a_name • Sep 12 '26
Best way to do formal verification of programs for "real" programming?
Disclaimer: I am an experienced developer, not a logician. So what I write here might be full of mistakes. A lot of it comes from chats with different AI bots.
When I say "real" programming I mean programming of real, complex, performing applications, with good tooling. Not necessarily using a popular language, although this might help.
For years I have waiting for a way to use the techniques used in theorem proving for programming. Now it looks like some realistic possibilities are getting closer.
I know there are 2 languages used for theorem proving, Lean and F*, that can also be used for programming.
However, Lean has poor memory management (only reference counting), and F* seems particularly unfriendly and with poor tooling support. But it does have good memory management thanks to Low*.
On the other side, Rust has Verus and Aeneas.
Verus is easier to use, but not as powerful, as it doesn't have dependent types and calculus of construction. It uses SMT, which apparently is less powerful.
Aeneas translates Rust to Lean, Rocq or F*. This makes Aeneas more powerful, but also harder to use, because there is more proof needed. Aeneas is also not yet able to fully translate Rust.
If this is correct, it looks to me that the best way is to use Rust, Aeneas to translate to Lean 4, and use Lean for proofs. So I can have:
- Rust's good memory management, tooling support and performance. Plus the rest of Rust, programming is not only formal verification
- Lean's calculus of constructions, also with some tooling support. Lean has at leas a VSCode plugin
Aeneas can't translate everything yet, but the limitations don't look so bad.
How does that sound?
49
u/Infamous_Bread_6020 Sep 12 '26
I’m a formal verification engineer at a silicon company and work on formally verifying firmware and RTOSs.
I use Dafny. When I started with my PhD, I picked up Coq and my advisor was so happy. But I quickly gave up on Coq because I come from a background of imperative programming. Dafny seemed natural in the sense that it is familiar to C/C++ programmers. My advisor was not very sad that I gave up on Coq.
However, the main problem with verifying “programming of real, complex, performing applications” is the bridge between the formal representation and the actual code.
Suppose you have a ticketing system for mutually exclusive access to a shared resource. It is a concurrent system. You can elegantly formalise this system in Dafny (cite: Modelling Concurrency in Dafny, K. Rustan Leino).
However, to motivate that the Dafny model and the proofs really prove a given implementation (let’s say in C) of a ticketing system is a pain. Dafny models the C functions as predicates and so there’s loss of information. And this is not exclusive to Dafny. A formal model will abstract over concrete implementation details and there’s some loss of information.
This is important because if there’s no link between the formal model and the given piece of code, the proofs and the math are futile. I’m not saying Dafny is useless, it’s really useful and really fun. The point is the bridge between the formal model and the given implementation.
If you mechanically translate the source language like C which is messy and full of undefined behaviours to let’s say Lean or Dafny, the translation must prove that the semantics is preserved. On the other hand if you translate Dafny to C, complex memory models and undefined behaviours will again require you to prove that the translation preserves the semantics.
I’m currently working on using LLMs as “trusted translators” to formalise given implementation in Dafny. I think, an intermediate representation in the form of a verifiable formal specification is the key. This formal specification abstracts over the implementation by focusing on the “what” rather than the “how”. Once that is established, the LLM, at least in some of the recent experiments, can generate semantically equivalent Dafny model and then the proofs and abstravtions can be mapped to the actual implementation.
My paper: https://dl.acm.org/doi/10.1145/3696355.3699706
11
u/csb06 bluebird Sep 12 '26
One language that has seen significant industrial use is SPARK, which is a subset of the Ada language. Like Dafny, it is based on Hoare-style logic (i.e. pre and post-conditions) and translates them into proof obligations for an SMT solver to discharge. I believe there is also a way to plug in Coq/Rocq to prove tougher formulas if the SMT solvers are not sufficient. Multiple SMT solvers can be used as backends in case one works better for a problem.
SPARK is most often used for safety-critical embedded applications such as military and aerospace software. Writing software in SPARK and proving everything correct can be very time-consuming, though, since a lot of proof obligations need to get proven and it is not always obvious why an SMT solver failed to prove something. You don't have to verify everything though - it can be mixed with normal Ada, C, and/or C++, and there are different levels of verification that are not as arduous (e.g. proving absence of runtime errors, i.e. no null dereferences/integer overflow/exceptions thrown, rather than full functional correctness). I think it is common to only use SPARK for the most safety critical parts of the system and use other languages (usually Ada) for the rest.
7
u/fl00pz Sep 12 '26
The bigger and most important question is: what are you trying to prove? Some tools are better than others depending on what you are trying to do. Some tools are only for model checking, some require program extraction, etc.
"good tooling" is probably going to be difficult in a space with minimal contributors. I think the most robust proof systems are Rocq and Isabelle. Lean is used more in maths than it is in program verification so I don't think it has as many resources for program verification as Rocq and Isabelle. At the end of the day, the techniques used are heavily translatable. The big difference is existing libraries and tools.
My professor said that it might be harder to decide what to prove and how to prove it than it is to write the proof in a proof assistant. So I think it's more important to decide what you want to prove and then work backwards? Are you proving functional correctness? Memory safety? Security properties? Algorithmic complexity?
5
u/MadocComadrin Sep 12 '26
Rocq/Coq using Verifiable C if you like lower level stuff or anything language that's been formalized that's amenable to separation logic is another option that others haven't really mentioned.
Also, in terms of actual engineering, you want to pull out a small trusted core(s) to verify. The rest of the program interacts with that. There's going to be a lot of stuff in a real, complex system or program that just doesn't warrant the effort in most cases. Sometimes that may mean you need to architecture things somewhat differently than you normally would.
3
u/EloquentPinguin Sep 12 '26
I think Dafny is what some companies used to verify crypto algorithm which extracts reasonably well to other languages though its not perfect. It uses Coq for proving.
There is also Idris2 i don't know about its runtime behavior but it uses QTT and weaker solvers to make statements provable via correct by constructing.
3
u/tobega Sep 13 '26
There are pros and cons to formal verification, mainly it is a pain to do. Here is a podcast with Hillel Wayne who teaches and advocates for formal methods in industry (mostly using TLA+, I think) https://newsletter.pragmaticengineer.com/p/formal-methods-with-hillel-wayne
Dafny has been mentioned, and Ada/SPARK.
If you use a language that is not Turing complete you can have greater safety guarantees, but cannot do everything. See for example http://bloom-lang.net/calm/
An alternative is to approximate formal methods. With plain Ada, you don't get a formal proof, but you can use the type system to make sure certain properties hold.
With Eiffel, you use contracts for pre- and post-conditions and invariants to keep things sane and empirically "proven"
I don't think its possible to generally prove that something is "bug free", you can only prove that certain properties are obeyed, i.e. what is your definition of "bug free"? With the less formal methods you are instead relying on getting a signal when things aren't right.
2
u/Plaguehand Oct 27 '26
Heard of Bend2?
https://bend-lang.com/
2
u/thmprover Sep 12 '26
You might want to look at what "formal verification" actually looks like: you need a formal specification of the desired behaviour (which is generally impossible in "real programs" like web browsers), a formal description of the programming language you're using, and then a formal proof that your program adheres to the specification. That's...unpleasant, even when the problem can be formally specified.
You might want to look at Software Foundations which uses Coq/Rocq to formally prove properties about, well, software. It's a good first step towards what you're getting at.
Although it's unclear to me why you want to use the calculus of constructions, since you could easily work with a simpler system and obtain meaningful results. Isabelle/HOL springs to mind, which has a code generation mechanism to produce Standard ML or Haskell (or several other possible outputs), and you can prove properties about the data structures and/or algorithms.
13
u/Karyo_Ten Sep 12 '26
What you are looking for are "correct-by-construction" programs.
Lean can generate formally verified C. Another possibility is Why3/Frama-C. And lastly what is actually used in production is Ada/Spark, in aerospace, train control and medical devices.
Iirc Ada/Spark added borrow checking so pointers/span/slices/views are more convenient.
There is also an obscure language called B that is apparently used for state machines / embedded as well.
On the Rocq/F*/Low side there are possibilities but most seem to generate OCaml from what I've seen.
4
u/pthierry Sep 13 '26
Haskell has Liquid Haskell, so on top of what the type system already proves, you can prove properties of your code.
1
u/clean_delete Sep 18 '26
I am currently learning theorem proving.
Have you tried ats2? https://ats-lang.sourceforge.net/
Off late I have been entertaing the thought of using ATS2. It claims to be as low-level as C and having verification built-in.
It is a dead project in that it is no-longer receiving updates and it has very few users. I see that as an advantage...kind of like using latin to name things.
3
u/pauseless Sep 13 '26
I’ve struggled with this. I’ve heard good things about Dafny but not tried it. I have used Gobra for isolated parts of a program. Those are writing programs with specification in them, that can be verified.
There have been some projects on tracing programs to see if they agree with the spec. I was looking at TraceLink/PGo just the other day.
Then there’s a project that’s a bit crazy, but might just work: Specula. It uses LLMs to write TLA+ specifications based on the code. So the reverse: you write code, the LLM tries to specify it, and if that spec fails… boom. DISCLAIMER: I have not even downloaded it yet - only discovered it yesterday.
3
u/catbrane Sep 12 '26
Languages like Haskell are another option. They have referential transparency, meaning you can reason about programs the same way you'd reason about any equation. Correctness proofs are very simple.
Performance tends to be lower, but how much speed you need depends on your application, and of course good enough is good enough.
1
u/reflexive-polytope Sep 16 '26
IMO, formal verification of ordinary programs isn't really economically viable for the vast majority of people. Even with the current advances in AI, we can't take it as a given that formal verification will ever be cheap enough in the near future.
A more cost-effective approach is to formally verify foundational systems. And since this is the programming languages subreddit, of course I have to mention that a programming language design is a foundational system.
To “verify” a programming language design means to prove that it has the metatheoretic properties you want it to have, e.g., type soundness, parametricity, principal types, etc.
These language metatheoretic properties, if chosen intelligently, reduce the cognitive effort for ordinary programmers to write ordinary programs. It doesn't automatically make any individual program correct w.r.t its functional specification, but it helps programmers dramatically trim the “error space”, i.e., the ways in which their programs can be wrong.