vstte10_aqueue.mlw 1.53 KB
 Jean-Christophe Filliatre committed Jun 22, 2011 1 2 3 4 ``````(* VSTTE'10 competition http://www.macs.hw.ac.uk/vstte10/Competition.html Problem 5: amortized queue `````` Jean-Christophe Filliatre committed Jul 05, 2011 5 6 `````` Author: Jean-Christophe Filliatre (CNRS) Tool: Why3 (see http://why3.lri.fr/) `````` Jean-Christophe Filliatre committed Jun 22, 2011 7 ``````*) `````` Jean-Christophe Filliatre committed Nov 10, 2010 8 `````` `````` Jean-Christophe Filliatre committed Jun 22, 2011 9 10 ``````module AmortizedQueue `````` Jean-Christophe Filliatre committed Dec 30, 2010 11 `````` use import int.Int `````` Guillaume Melquiond committed Aug 21, 2014 12 13 `````` use import option.Option use import list.ListRich `````` Jean-Christophe Filliatre committed Nov 10, 2010 14 `````` `````` Andrei Paskevich committed Oct 13, 2012 15 16 `````` type queue 'a = { front: list 'a; lenf: int; rear : list 'a; lenr: int; } `````` Andrei Paskevich committed Oct 30, 2012 17 `````` invariant { length self.front = self.lenf >= length self.rear = self.lenr } `````` Jean-Christophe Filliatre committed Jun 22, 2011 18 `````` `````` Andrei Paskevich committed Jun 29, 2011 19 `````` function sequence (q: queue 'a) : list 'a = `````` Jean-Christophe Filliatre committed Jun 22, 2011 20 21 `````` q.front ++ reverse q.rear `````` Andrei Paskevich committed Oct 13, 2012 22 23 `````` let empty () ensures { sequence result = Nil } = { front = Nil; lenf = 0; rear = Nil; lenr = 0 } : queue 'a `````` Jean-Christophe Filliatre committed Jun 22, 2011 24 `````` `````` Andrei Paskevich committed Oct 13, 2012 25 26 27 28 `````` let head (q: queue 'a) requires { sequence q <> Nil } ensures { hd (sequence q) = Some result } = match q.front with `````` Jean-Christophe Filliatre committed Jun 22, 2011 29 30 31 32 `````` | Nil -> absurd | Cons x _ -> x end `````` Andrei Paskevich committed Oct 13, 2012 33 34 35 36 37 `````` let create (f: list 'a) (lf: int) (r: list 'a) (lr: int) requires { lf = length f /\ lr = length r } ensures { sequence result = f ++ reverse r } = if lf >= lr then { front = f; lenf = lf; rear = r; lenr = lr } `````` Jean-Christophe Filliatre committed Jun 22, 2011 38 39 `````` else let f = f ++ reverse r in `````` Andrei Paskevich committed Oct 13, 2012 40 `````` { front = f; lenf = lf + lr; rear = Nil; lenr = 0 } `````` Jean-Christophe Filliatre committed Jun 22, 2011 41 `````` `````` Andrei Paskevich committed Oct 13, 2012 42 43 44 45 `````` let tail (q: queue 'a) requires { sequence q <> Nil } ensures { tl (sequence q) = Some (sequence result) } = match q.front with `````` Jean-Christophe Filliatre committed Jun 22, 2011 46 47 48 49 `````` | Nil -> absurd | Cons _ r -> create r (q.lenf - 1) q.rear q.lenr end `````` Andrei Paskevich committed Oct 13, 2012 50 51 52 `````` let enqueue (x: 'a) (q: queue 'a) ensures { sequence result = sequence q ++ Cons x Nil } = create q.front q.lenf (Cons x q.rear) (q.lenr + 1) `````` Jean-Christophe Filliatre committed Nov 10, 2010 53 `````` `````` Jean-Christophe Filliatre committed Dec 29, 2010 54 ``end``