64 points · by MindGods · 6y ago · 26 comments
"Dafny is based on a Java or C# like imperative language with the ability to write Hoare logic style state invariants, this allows users of the languages to verify properties about methods, and objects that use mutable state, loops, arrays, and so on. Dafny's core theory is a custom program logic mostly designed by Rustan and a handful of collaborators. Dafny discharges the verification conditions it generates by compiling them to Boogie an intermediate verification language, which in turn compiles them into queries which are passed to an SMT solver such as Z3 or CVC4 to discharge." - jroesch
https://rise4fun.com/Dafny/tutorial
Dafny is a good example of what formal verification looks like for imperative languages (as opposed to e.g. dependent types for functional languages). Gotta love loop invariants!
But does Dafny have any library documentation? Is it .Net?
Dafny compiles to .NET indeed. What do you mean by library documentation? Dafny doesn't have many (any? I certainly haven't used any...) libraries to begin with.
At a glance it looks like Dafny has functional programming features, and looks quite approachable. Its compiler targets .Net, unsuitable for critical-systems work. Are there plans for it to get an industry-strength compiler, or is the emphasis on research rather than critical-systems software development?
SPARK examples for the curious:
* http://docs.adacore.com/spark2014-docs/html/ug/en/source/bas...
I doubt Dafny will have native targets (which I assume is a prerequisite for the "critical-systems" work you're talking about) any time soon if ever. There's just not enough resources behind it. AFAICT, basically all the development, Stack Overflow answering, etc. is all powered by a single dude (Rustan Leino). So I don't think it'll be competing in the same space as SPARK Ada any time soon simply due to resource availability.
Nonetheless Dafny as a language is, I think, strictly more expressive than SPARK. For example, Dafny's functions (vs methods) allow for the very nice expression of arithmetic invariants that I don't think SPARK supports (look at the Fibonacci example in the online tutorial here: https://rise4fun.com/Dafny/tutorial that has the standard mathematical definition of Fibonacci numbers as a function and then implements it as a method).
Fair enough. Still a really neat project.
I forgot to ask an obvious question: Dafny uses .Net's garbage collection, right? If so, that presumably rules out its use on hard real-time systems.
> the Fibonacci example in the online tutorial
For anyone struggling to find it, ctrl-f for completed Fibonacci
I agree the Dafny syntax is very tidy.
Surprisingly I wasn't able to find a directly comparable SPARK program. The closest I could manage was populating an array with the Fib sequence, by someone on StackOverflow (I haven't verified it) [0]
https://github.com/microsoft/Armada
which is built upon Dafny and targets a C-like language.
I was just about to point that out. Yes, Dafny uses garbage collection (which makes formal verification much easier). As far as I know in SPARK (and FRAMA-C), verification of memory allocation is still an open research question.
It also has a much nicer syntax, also compared to ATS, which is in the same league.
Also Dafny has pretty poor IDE support. I don't think it has Visual Studio support and the plugin for Visual Studio Code is not written by MS (and is pretty bare bones).
MS really only supports Dafny by employing its creator. It doesn't seem to devote any other resources.
Again I haven't programmed with SPARK before but my impression is that it's getting way more industry use and support.
FWIW you can already use Dafny to write individual modules in real-world programs (it compiles to .NET and therefore interoperates with e.g. C#), although there are still some rough edges given the tiny size of its community, and the documentation for doing this is rather fragmentary.
As for the difference between Dafny and Rust, Rust really has only one big unique trick up its sleeve which is its affine types. Otherwise its approach is entirely compatible with Dafny's (and indeed there are efforts to port over some the same ideas between Dafny to Rust, see e.g. https://alastairreid.github.io/rust-verification-tools/). Those will take a long time though to come to fruition because formal verification is still a hard problem.
That said, it looks like the good ideas from Rust may find their way into SPARK, a highly restrictive programming language for critical systems development with formal verification. [0][1]
[0] https://www.adacore.com/papers/safe-dynamic-memory-managemen...
Rust could definitely be augmented with formal verification; it's just that this requires more research into semantics for Rust itself and Rust-like languages - especially wrt. the 'unsafe' subset.