Dafny


Dafny is an imperative compiled language that targets C# and supports formal specification through preconditions, postconditions, loop invariants and loop variants. The language combines ideas primarily from the functional and imperative paradigms, and includes limited support for object-oriented programming. Features include generic classes, dynamic allocation, inductive datatypes and a variation of separation logic known as implicit dynamic frames for reasoning about side effects. Dafny was created by Rustan Leino at Microsoft Research after his previous work on developing ESC/Modula-3, ESC/Java, and Spec#. Dafny is been used widely in teaching and features regularly in software verification competitions.
Dafny was designed to provide a simple introduction to formal specification and verification and has been used widely in teaching. Dafny builds on the Boogie intermediate language which uses the Z3 automated theorem prover for discharging proof obligations.

Data types

Dafny provides methods for implementation which may have side-effects and functions for use in specification which are pure. 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 must be accounted for by noting which parameters can be mutated in the modifies clause. Dafny also provides a range of immutable collection types including: sequences, sets and maps. In addition, mutable arrays are provided.

Imperative features

Dafny supports proofs of imperative programs based on the ideas of Hoare logic. The following illustrates many such features in Dafny, including the use of preconditions, postconditions, loop invariants and loop variants.

method max returns
// Array must have at least one element
requires arr!=null && arr.Length > 0;
// Return cannot be smaller than any element in array
ensures ;
// Return must match some element in array
ensures ;

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

Loop invariants

The treatment of loop invariants in Dafny differs from traditional Hoare logic. Variables mutated in a loop are treated such that 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 provides illustrates:

method sumAndZero returns
requires ns != null
requires forall i :: 0 <= i < ns.Length > ns >= 0
modifies ns

This fails verification because Dafny cannot establish that >= 0 holds at the assignment. From the precondition, intuitively, forall i :: 0 <= i < arr.Length > arr >= 0 holds in the loop since arr := arr; 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: remove the redundant assignment arr := arr;; or, add the loop invariant invariant forall i :: 0 <= i < arr.Length > arr >= 0
Dafny additionally employs limited static analysis to infer simple loop invariants where possible. In the example above, it would seem 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. Whilst proofs of simple properties within a function or method 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
function sum: int
predicate isNatList
ghost method NatSumLemma
requires isNatList && n sum
ensures n >= 0

Here, NatSumLemma proves a useful property between sum and isNatList. The use of a ghost method for encoding lemmas and theorems is standard in Dafny with recursion employed for 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.