1 Dafny Cheatsheet
1.1 Statements
Declaration:
var i: int; var i: int := 5 var j: real;
- Assignment:
5; i := 0; //Choose some value for i such that the condition i > 0 holds. i :| i > 5); //assign to f some result value that satisfies the f :- Find(with the given arguments. // postconditions of Find i, j, k := k, j, i; i, j, k := m();int i := *; // i is now some arbitrary
Dafny
method Find(x: int) returns (y: int) ensures y > x { y :| y > x; } method Test() { var i := Find(5); assert i > 5; }
Method Call:
5,6,7); m(5,6,7); i, j, k := p(5,6,7); i, j, k :- p(
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 !in == != < <= > >= !!
Infix and Unary operators:
<< >> + - * / % | & ^ !
Conditional:
if β¦ then β¦ else β¦ match β¦ case β¦ => β¦ case β¦ => β¦
Tests and Conversions:
e is Typeas Type e
Lambda expression:
i => i*i (i, j) => i+jint) requires β¦ => β¦ (i: int, r: real) => β¦ (i:
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.Items
Two-state:
old(o) old@L(o) allocated(o) allocated@L(o) unchanged(o) unchanged@L(o) fresh(o) fresh@L(o)
Primaries:
true false this null 5 0.0 0xABCD βcβ "asd" @"asd" ( e ) | e | e.f3,4,5) e.fn(3,4,option:=5) e.fn(
Arrays & sequences:
6] a[ a[j..k] a[j..] a[..k] a[..]2 : 2 : 2 : ] s[
Updates:
d.(f := x)2 := 6, 3 := 7] s.[ 2 := "Two", 3 := "Three"] mp.[
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+y
Statements in expressions:
assert P(x); x > 0 assume P(x); x > 0 0 expect P(x); x > 0 reveal β¦ ; x > calc { β¦ } x > 0 lemma call) L(x); f(x) (
Types
int bool real nat char string array<int> array3<int> bv16 set iset multiset seq map imap
Function types:
int->int intβ->int int~>int int, real, nat) tuple type ( newtypedatatype 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 |
|
|