Dafny
Dafny is an imperative and functional compiled language that compiles to other programming languages, such as C#, Java, JavaScript, Go, and Python.[src] It supports formal specification through preconditions, postconditions, loop invariants, loop variants, termination specifications and read/write framing specifications.[src] The language combines ideas from the functional programming and imperative programming paradigms; it includes support for object-oriented programming.[src] Features include generic classes, dynamic allocation, inductive datatypes and a variation of separation logic known as implicit dynamic frames for reasoning about side effects.[src] Dafny was created by Rustan Leino at Microsoft Research after his prior work on developing ESC/Modula-3, ESC/Java, and Spec#.[src] Dafny is regularly featured in software verification competitions (e.g.[src] VSTTE'08, VSCOMP'10, COST'11, and VerifyThis'12 ).[src] Dafny was designed as a verification-aware programming language, requiring verification along with code development.[src] It thus fits the Correct by Construction software development paradigm.[src] 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.[src] Verification obligations are discharged automatically, given sufficient specification.…[src]
Dafny is an imperative and functional compiled language that compiles to other programming languages, such as C#, Java, JavaScript, Go, and Python.[src] It supports formal specification through preconditions, postconditions, loop invariants, loop variants, termination specifications and read/write framing specifications.[src] The language combines ideas from the functional programming and imperative programming paradigms; it includes support for object-oriented programming.[src] Features include generic classes, dynamic allocation, inductive datatypes and a variation of separation logic known as implicit dynamic frames for reasoning about side effects.[src] Dafny was created by Rustan Leino at Microsoft Research after his prior work on developing ESC/Modula-3, ESC/Java, and Spec#.[src] Dafny is regularly featured in software verification competitions (e.g.[src] VSTTE'08, VSCOMP'10, COST'11, and VerifyThis'12 ).[src] Dafny was designed as a verification-aware programming language, requiring verification along with code development.[src] It thus fits the Correct by Construction software development paradigm.[src] 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.[src] Verification obligations are discharged automatically, given sufficient specification.[src] Dafny uses some program analysis to infer many specification assertions, reducing the burden on the user of writing specifications.[src] The general proof framework is that of Hoare logic.[src] Dafny builds on the Boogie intermediate language which uses the Z3 automated theorem prover for discharging proof obligations.[src] ==Data types== Dafny provides methods for implementation which may have side-effects and functions for use in specification which are pure.[src] Methods consist of sequences of statements following a familiar imperative style whilst, in contrast, the body of a function is simply an expression.[src] Any side-effecting statements in a method (e.g.[src] assigning an element of an array parameter) must be accounted for by noting which parameters can be mutated, using the modifies clause.[src] Dafny also provides a range of immutable collection types including: sequences (e.g.[src] seq<int> ), sets (e.g.[src] set<int> ), maps ( map<int,int> ), tuples, inductive datatypes and mutable arrays (e.g.[src] array<int> ).[src] ==Imperative features== The following illustrates many of the features in Dafny, including the use of preconditions, postconditions, loop invariants and loop variants.[src] 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 max >= arr[j] // Return must match some element in array ensures exists j : int :: j>=0 && j = 0 && j max >= arr[j] // Some element seen so far matches max invariant exists j:int :: j >= 0 && j max) { max := arr[i]; } // Continue through array i := i + 1; } } This example computes the maximum element of an array.[src] The method's precondition and postcondition are given with the requires and ensures clauses (respectively).[src] Likewise, the loop invariant and loop variant are given through the invariant and decreases clauses (respectively).[src] ==Loop invariants== The treatment of loop invariants in Dafny differs from traditional Hoare logic.[src] Variables mutated in a loop are treated such that (most) information known about them before the loop is discarded.[src] Information required to prove properties of such variables must be expressed explicitly in the loop invariant.[src] In contrast, variables not mutated in the loop retain all information known about them beforehand.[src] The following example illustrates using loops: method sumAndZero(arr: array ) returns (sum: nat) requires forall i :: 0 arr[i] >= 0 modifies arr { var i: int := 0; sum := 0; // while(i This fails verification because Dafny cannot establish that (sum + arr[i]) >= 0 holds at the assignment.[src] From the precondition, intuitively, forall i :: 0 arr[i] >= 0 holds in the loop since arr[i] := arr[i]; is a NOP.[src] However, this assignment causes Dafny to treat arr as a mutable variable and drop information known about it from before the loop.[src] 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 arr[i] >= 0 Dafny also employs limited static program analysis to infer simple loop invariants where possible.[src] 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.[src] Whilst the underlying logic requires such an invariant, Dafny infers this automatically, and hence, it can be omitted at the source level.[src] ==Proof features== Dafny includes features which further support its use as a proof assistant.[src] 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.[src] As is common for a proof assistant, such proofs are often inductive in nature.[src] Dafny may be unusual in employing method invocation as a mechanism for applying the inductive hypothesis.[src] The following illustrates: datatype List = Nil | Link(data: int, next: List) function sum(l: List): int { match l case Nil => 0 case Link(d, n) => d + sum(n) } predicate isNatList(l: List) { match l case Nil => true case Link(d, n) => d >= 0 && isNatList(n) } lemma NatSumLemma(l: List, n: int) requires isNatList(l) && n == sum(l) ensures n >= 0 { match l case Nil => // Discharged Automatically case Link(data, next) => { // Apply Inductive Hypothesis NatSumLemma(next, sum(next)); // Check what known by Dafny assert data >= 0; } } Here, NatSumLemma proves a useful property between sum() and isNatList() (i.e.[src] that isNatList(l) ==> (sum(l) >= 0) ).[src] The use of a ghost method for encoding lemmas and theorems is standard in Dafny with recursion employed for induction (typically, structural induction).[src] Case analysis is performed using match statements and non-inductive cases are often discharged automatically.[src] The verifier must also have complete access to the contents of a function or predicate to unroll them as necessary.[src] This has implications when used in conjunction with access modifiers.[src] Specifically, hiding the contents of a function using the protected modifier can limit what properties can be established about it.[src] ==See also== Design by contract ==References== ==Further reading== ==External links== Dafny: A Language and Program Verifier for Functional Correctness - Microsoft Research , verification-aware programming language[src]
59 sentences · 59 cited inline · 0 references stored
References
No reference rows published for this article.
Cite this article
MLA
"Dafny." encyclopegia.org, 2026, https://encyclopegia.org/wiki/dafny. Accessed 27 November 2026.
Chicago
"Dafny." encyclopegia.org. Accessed November 27, 2026. https://encyclopegia.org/wiki/dafny.
APA
encyclopegia.org. (2026). Dafny. Retrieved November 27, 2026, from https://encyclopegia.org/wiki/dafny
Accessed November 27, 2026 · CC BY-SA 4.0 · citation page · JSON