2 (** Simple queue implemented using a ring buffer
5 Developing Verified Programs with Dafny.
6 K. Rustan M. Leino. Tutorial notes, ICSE 2013.
8 For a similar data structure, see vstte12_ring_buffer.mlw
16 mutable data: array 'a;
19 ghost mutable contents: S.seq 'a; (* = data[m..n[ *)
21 invariant { 0 < length data }
22 invariant { 0 <= m <= n <= length data }
23 invariant { S.length contents = n - m }
24 invariant { forall i. 0 <= i < m - n -> S.get contents i = data[m + i] }
25 by { data = Array.make 1 (any 'a); m = 0; n = 0; contents = S.empty }
27 let create (x: 'a) : t 'a
28 ensures { S.(result.contents == empty) }
30 { data = Array.make 10 x;
34 let dequeue (q: t 'a) : (_r: 'a)
35 requires { S.length q.contents > 0 }
36 writes { q.m, q.contents }
37 ensures { S.(q.contents == (old q.contents)[1..]) }
39 let r = q.data[q.m] in
41 ghost S.(q.contents <- q.contents[1..]);
44 let enqueue (q: t 'a) (x: 'a) : unit
45 requires { q.n < length q.data }
46 writes { q.data.elts, q.n, q.contents }
47 ensures { S.(q.contents == snoc (old q.contents) x) }
51 ghost S.(q.contents <- S.snoc q.contents x)