Dafny Explained

Dafny
Logo Size:128px
Designer:K. Rustan M. Leino
Latest Release Version:3.7.2
File Ext:.dfy

Dafny is an imperative and functional compiled language that compiles to other programming languages, such as C#, Java, JavaScript, Go and Python. It supports formal specification through preconditions, postconditions, loop invariants, loop variants, termination specifications and read/write framing specifications. The language combines ideas from the functional and imperative paradigms; it includes support for object-oriented programming. Features include generic classes, dynamic allocation, inductive datatypes and a variation of separation logic known as implicit dynamic frames[1] for reasoning about side effects.[2] Dafny was created by Rustan Leino at Microsoft Research after his previous work on developing ESC/Modula-3, ESC/Java, and Spec#.

Dafny is widely used in teaching because it provides a simple, integrated introduction to formal specification and verification; it is regularly featured in software verification competitions (e.g. VSTTE'08,[3] VSCOMP'10,[4] COST'11,[5] and VerifyThis'12[6]).

Dafny was designed as a verification-aware programming language, requiring verification along with code development. It thus fits the "Correct by Construction" software development paradigm. Verification proofs are supported by a mathematical toolbox that includes mathematical integers and reals, bit-vectors, sequences, sets, multisets, infinite sequences and sets, induction, co-induction, and calculational proofs. Verification obligations are discharged automatically, given sufficient specification. Dafny uses some program analysis to infer many specification assertions, reducing the burden on the user of writing specifications. The general proof framework is that of Hoare logic.

Dafny builds on the Boogie intermediate language which uses the Z3 automated theorem prover for discharging proof obligations.[7] [8]

Data types

Dafny provides methods for implementation which may have side-effects and functions for use in specification which are pure.[9] Methods consist of sequences of statements following a familiar imperative style whilst, in contrast, the body of a function is simply an expression. Any side-effecting statements in a method (e.g. assigning an element of an array parameter) must be accounted for by noting which parameters can be mutated, using the modifies clause. Dafny also provides a range of immutable collection types including: sequences (e.g. seq<int>), sets (e.g. set<int>), maps (map<int,int>), tuples, inductive datatypes and mutable arrays (e.g. array<int>).

Imperative features

The following illustrates many of the features in Dafny, including the use of preconditions, postconditions, loop invariants and loop variants.

method max(arr:array) returns (max:int) // Array must have at least one element requires arr.Length > 0 // Return cannot be smaller than any element in array ensures forall j : int :: j >= 0 && j < arr.Length

> max >= arr[j] // Return must match some element in array ensures exists j : int :: j>=0 && j < arr.Length && max

arr[j]

This example computes the maximum element of an array. The method's precondition and postcondition are given with the requires and ensures clauses (respectively). Likewise, the loop invariant and loop variant are given through the invariant and decreases clauses (respectively).

Loop invariants

The treatment of loop invariants in Dafny differs from traditional Hoare logic. Variables mutated in a loop are treated such that (most) information known about them prior to the loop is discarded. Information required to prove properties of such variables must be expressed explicitly in the loop invariant. In contrast, variables not mutated in the loop retain all information known about them beforehand. The following example illustrates using loops:

method sumAndZero(arr: array) returns (sum: nat) requires forall i :: 0 <= i < arr.Length

> arr[i] >= 0 modifies arr

This fails verification because Dafny cannot establish that (sum + arr[i]) >= 0 holds at the assignment. From the precondition, intuitively, forall i :: 0 <= i < arr.Length ==> arr[i] >= 0 holds in the loop since arr[i] := arr[i]; is a NOP. However, this assignment causes Dafny to treat arr as a mutable variable and drop information known about it from before the loop. To verify this program in Dafny we can either (a) remove the redundant assignment arr[i] := arr[i];; or (b) add the loop invariant invariant forall i :: 0 <= i < arr.Length ==> arr[i] >= 0

Dafny additionally employs limited static analysis to infer simple loop invariants where possible. In the example above, it would seem that the loop invariant invariant i >= 0 is also required as variable i is mutated within the loop. Whilst the underlying logic does require such an invariant, Dafny automatically infers this and, hence, it can be omitted at the source level.

Proof features

Dafny includes features which further support its use as a proof assistant. Although proofs of simple properties within a function or method (as shown above) are not unusual for tools of this nature, Dafny also allows the proof of properties between one function and another. As is common for a proof assistant, such proofs are often inductive in nature. Dafny is perhaps unusual in employing method invocation as a mechanism for applying the inductive hypothesis. The following illustrates:

datatype List = Nil | Link(data: int, next: List)

function sum(l: List): int

predicate isNatList(l: List)

lemma NatSumLemma(l: List, n: int) requires isNatList(l) && n

sum(l) ensures n >= 0

Here, NatSumLemma proves a useful property between sum and isNatList (i.e. that isNatList(l) ==> (sum(l) >= 0)). The use of a ghost method for encoding lemmas and theorems is standard in Dafny with recursion employed for induction (typically, structural induction). Case analysis is performed using match statements and non-inductive cases are often discharged automatically. The verifier must also have complete access to the contents of a function or predicate in order to unroll them as necessary. This has implications when used in conjunction with access modifiers. Specifically, hiding the contents of a function using the protected modifier can limit what properties can be established about it.

See also

Further reading

External links

Notes and References

  1. Implicit Dynamic Frames: Combining Dynamic Frames and Separation Logic . 10.1007/978-3-642-03013-0_8 . Proceedings of the Conference on European Conference on Object Oriented Programming . 148–172 . 2009 . Jan . Smans . Bart . Jacobs . Frank . Piessens.
  2. Dafny: An Automatic Program Verifier for Functional Correctness . 10.1007/978-3-642-17511-4_20 . Proceedings of the Conference on Logic for Programming, Artificial Intelligence, and Reasoning . 348–370 . 2010 . Rustan . Leino.
  3. Dafny Meets the Verification Benchmarks Challenge . 10.1007/978-3-642-15057-9_8 . International Conference on Verified Software: Theories, Tools, and Experiments . 112–116 . 2010 . Rustan . Leino . Rosemary . Monahan.
  4. The 1st Verified Software Competition: Experience Report . 10.1007/978-3-642-21437-0_14 . Proceedings of the Conference on Formal Methods . 154–168 . 2011 . etal . Vladimir . Klebanov. 10.1.1.221.6890 .
  5. The COST IC0701 Verification Competition 2011 . 10.1007/978-3-642-31762-0_2 . Proceedings of the Conference on Formal Verification of Object-Oriented Software. 3–21 . 2011 . etal . Thorsten . Bormer . 10.1.1.396.6170 .
  6. VerifyThis 2012. 10.1007/s10009-015-0396-8 . International Journal on Software Tools for Technology Transfer. 17 . 6 . 647–657 . 2015. Huisman . Marieke . Klebanov . Vladimir . Monahan . Rosemary . 14301377 .
  7. Web site: Z3 Homepage. . 2019-02-07.
  8. Z3: An Efficient SMT Solver . 10.1007/978-3-540-78800-3_24 . Proceedings of the Conference on Tools and Algorithms for the Construction and Analysis . 337–340 . 2008 . Leonardo . de Moura . Nikolaj . Bjørner. free .
  9. Web site: Dafny Programming Language. 2022-07-14.