1 (** Dijkstra's "Dutch national flag" *)
11 type color = Blue | White | Red
13 predicate monochrome (a:array color) (i:int) (j:int) (c:color) =
14 forall k:int. i <= k < j -> a[k]=c
16 (* We scan the array from left to right using [i] and we maintain
17 the following invariant, using indices [b] and [r]:
20 +---------+----------+-----------+-------+
21 | Blue | White | ? | Red |
22 +---------+----------+-----------+-------+
26 let dutch_flag (a:array color) : unit
27 ensures { exists b r: int.
28 monochrome a 0 b Blue /\
29 monochrome a b r White /\
30 monochrome a r (length a) Red }
31 ensures { permut_all (old a) a }
35 let r = ref (length a) in
37 invariant { 0 <= !b <= !i <= !r <= length a }
38 invariant { monochrome a 0 !b Blue }
39 invariant { monochrome a !b !i White }
40 invariant { monochrome a !r (length a) Red }
41 invariant { permut_all (old a) a }