1 Dafny Cheatsheet
1.1 Statements
Declaration:
var i: int; var i: int := 5 var j: real;- Assignment:
i := 5; i :| i > 0; //Choose some value for i such that the condition i > 0 holds. f :- Find(5); //assign to f some result value that satisfies the // postconditions of Find with the given arguments. i, j, k := k, j, i; i, j, k := m(); i := *; // i is now some arbitrary intDafny
method Find(x: int) returns (y: int) ensures y > x { y :| y > x; } method Test() { var i := Find(5); assert i > 5; } Method Call:
m(5,6,7); i, j, k := p(5,6,7); i, j, k :- p(5,6,7);Conditional:
if β¦ { β¦ } else β¦ if x: int | P(x) { β¦ } else β¦ if case β¦ => β¦ case β¦ => β¦ match s case p => β¦ case q => β¦Loop:
while β¦ { β¦ } while case β¦ => β¦ case β¦ => β¦ for i: int := β¦ to β¦ { β¦ } for i: int := β¦ downto β¦ { β¦ } break; continue;Labeled:
label L: β¦Forall:
forall i | 0 <= i < j { β¦ } forall e <- s { β¦ }Others:
{ β¦ } return ; return β¦, β¦; yield ; yield β¦, β¦; assert β¦ ; assume β¦ ; expect β¦, msg ; print β¦, β¦, β¦; reveal β¦, β¦, β¦ ; modify β¦, β¦, β¦ ; calc <= { β¦ ; β¦ ; β¦ ; }
1.2 Expressions
Logical Operators:
<==> ==> <== && || !Comparison operators:
== != < <= > >= !! in !inInfix and Unary operators:
+ - * / % | & ^ ! << >>Conditional:
if β¦ then β¦ else β¦ match β¦ case β¦ => β¦ case β¦ => β¦Tests and Conversions:
e is Type e as TypeLambda expression:
i => i*i (i, j) => i+j (i: int) requires β¦ => β¦ (i: int, r: real) => β¦Allocations:
new MyClass new MyClass(4,5,6) new MyClass.Init(5,6,7) new int[10] new int[][5,6,7,8,9] new int[5](_ => 42) new int[10,10]((i,j)=>i+j)Collections:
[ e1, e2, e3 ] seq(n, i requires 0<=i<n => f(i)) { e1, e2, e3 } iset{ e1, e2, e3 } set x: nat | x < 10 :: x*x multiset{ e1, e2, e3 } multiset(s) map[ 1:= βaβ, 2 := βbβ ] map x: int | 0<=x<10 :: -x := x m.Keys m.Values m.ItemsTwo-state:
old(o) old@L(o) allocated(o) allocated@L(o) unchanged(o) unchanged@L(o) fresh(o) fresh@L(o)Primaries:
this null true false 5 0.0 0xABCD βcβ "asd" @"asd" ( e ) | e | e.f e.fn(3,4,5) e.fn(3,4,option:=5)
Arrays & sequences:
a[6] a[j..k] a[j..] a[..k] a[..] s[ 2 : 2 : 2 : ]Updates:
d.(f := x) s.[ 2 := 6, 3 := 7] mp.[ 2 := "Two", 3 := "Three"]Quantifiers, Let expressions:
forall x: int :: x > 0 exists x: int :: x > 0 var k := j*j; k*k var k :| k > 0; k + 1 var k :- f(); k + 1 var R(x,y) := T(); x+yStatements in expressions:
assert P(x); x > 0 assume P(x); x > 0 expect P(x); x > 0 reveal β¦ ; x > 0 calc { β¦ } x > 0 L(x); f(x) (lemma call)Types
int bool real nat char string bv16 array<int> array3<int> set iset multiset seq map imapFunction types:
int->int intβ->int int~>int (int, real, nat) tuple type newtype datatype class trait iterator
1.3 Declarations & Specifications
module { … }
const c: int := 6
var f: T in types only
method m(i: int)
returns (r: real) requires β¦ ensures β¦ modifies β¦ decreases β¦ { β¦ }function f(i: int): int
requires β¦ ensures β¦ reads β¦ decreases β¦ { expr }class A<T> extends I { … }
trait I<T,U> extends J, K { … }
datatype D = A(val: int) | B | C { … }
type T
type Tuple3 = (int, real, nat)
type T = x: int | x > 0
newtype T = x: int | x > 0
while …
invariant β¦ modifies β¦ decreases β¦ { β¦ }for i: int … to …
invariant β¦ modifies β¦ decreases β¦ { β¦ }
Keyword(s) |
| What it does |
| Example |
|
| precondition |
| |
|
| postcondition |
| |
|
| inline propositions |
| |
|
| logical connectives |
| |
|
| logical quantifiers |
| |
|
| pure definitions |
| |
|
| framing (for methods) |
| |
|
| framing (for functions) |
| |
|
| loop invariants |
| |
|
| collections |
| |
1.4 Imperative
Keyword(s) |
| What it does |
| Example |
|
| declares variables |
| |
:= |
| assignment |
| |
|
| conditional statement |
| |
|
| conditional expression |
| |
|
| loops |
| |
|
| subroutines |
| |
|
| object classes |
| |
|
| typed arrays |
| |