230 likes | 499 Views
Dynamic-frame specifications in Dafny. K. Rustan M. Leino RiSE , Microsoft Research. 1 Dec 2008 Invited talk, working group meeting COST Action IC0701, Formal Verification of Object-Oriented Software Madrid, Spain. Dafny. experimental language sequential, object based (no subclassing )
E N D
Dynamic-frame specifications in Dafny K. Rustan M. Leino RiSE, Microsoft Research 1 Dec 2008Invited talk, working group meetingCOST Action IC0701, Formal Verification of Object-Oriented SoftwareMadrid, Spain
Dafny • experimental language • sequential, object based (no subclassing) • specifications in the style of dynamic frames • coarse-grained frames (at the level of whole objects, not individual memory locations)
Functional-correctness verifications • queue • linked list with head/tail pointers • in-situ list reversal • integer set • binary tree • Schorr-Waite marking algorithm
Dafny grammar • Program ::= Class* • Class ::= class C<TypeParam*> { Member* } • Member ::= • Field • Method • Function
Fields • var x : T;
Types • T ::= • bool • int • set<T> • seq<T> • C<T*> • object
Methods • method M<TypeParam*> (Param*) returns (Param*) Spec*{ Stmt*}
Statements • Stmt ::= • var x: T; • x := E; • E.f := E’; • x := new C<T*>; • call x* := E.M(E*); • if (E) { Stmt* } else { Stmt* } • while (E) invariant J; decreases F; { Stmt* } • foreach (x in S) { x.f := E; }
Specifications • Spec ::= • requires E; • modifies S; • ensures E; • where “modifies S” means • modifies Heap • ensures (o,f Heap[o,f] = old(Heap)[o,f] o old(S) ¬old(Heap)[o,alloc]) • modifies clauses are enforced atevery update
Functions • function F<TypeParam*> (Param*): Treads Rd;{Expr} • produces definitional axiom: (Heap,this,x F(Heap,this,x) = Expr)
What is the reads clause good for? • ensures definitional axioms are consistent • reading o.f requires o Rd • calling a function G requires RdG Rd • produces frame axiom: • ( h0,h1,this,x ( o,f o Rd h0[o,f] = h1[o,f]) F(h0,this,x) = F(h1,this,x))
* That’s all! * *) well, pretty much…
Specification idiom: footprints and validity • class C {var footprint: set<object>;function Valid(): boolreads {this},footprint; {this footprint … } …
Specification idiom: initializer • method Init()modifies {this};ensures Valid() fresh(footprint – {this});
Specification idiom: mutating method • method M()requires Valid();modifies footprint;ensures Valid() fresh(footprint – old(footprint));
Demo: Queue :Queue tail head :Node :Node :Node :Node
Demo: Schorr-Waite • Specification (excerpt): • ensuresroot.marked; • ensures( n, i n.marked 0 ≤ i < |n.children|n.children[i] = null n.children[i].marked); • Loop invariant (excerpt): • invariantt.marked; • invariant( n, i n.marked 0 ≤ i < |n.children| nnodeStackn.children[i] = null n.children[i].marked);
Schorr-Waite termination • decreases { n | ¬ n.marked }, |nodeStack|, |t.children| – t.childrenVisited;
Schorr-Waite: garbage unmarked • ensuresroot.marked; • ensures( n, i n.marked 0 ≤ i < |n.children|n.children[i] = null n.children[i].marked); • ensures ( n Reach(root,n) ¬n.marked);
Lessons, 0 • Dynamic-frame specifications are useful and flexible • A language design around dynamic frames can be simple • Thus good in teaching? • Specifications are verbose, but perhaps simplification techniques can be applied (like in Spec# or Chalice) • Currently missing in Dafny: scopes for axioms
Lessons, 1 • Pure methods are hard, functions are easy • SMT solvers work better with ghost fields than with functions • Reachability is not always necessary in specifications • Sets and sequences are nice as value types • Generics are a cinch • Decreases bound checks can be more liberal than naïve translation
Lessons, 2 • SMT solvers can be used for functional-correctness verification • Inductive predicates seem useful • cases fit nicely with matching triggers • take us in the direction of the input languages of interactive theorem provers • Need: better views/visualizations of program states to clarify error messages and, generally, what’s going on
Parting note • Try it for yourself:http://research.microsoft.com/boogie/dafny