Merge branch 'fmap_additional_operators' into 'master'
[why3.git] / examples_in_progress / wcet_hull.mlw
blobac13d0d40ac4ea84ef8805fdd658e52e28fe63c1
1 module M
3 (* WCET example
5    This double loop program is actually O(n).
6    We show it by introducing a variable t which counts the number of times
7    the body of the inner loop is executed.
8 *)
10   use int.Int
11   use ref.Ref
12   use array.Array
14   val constant n : int
15     ensures { 0 <= result }
17   let hull (a: array int) (b: array int) =
18     let j = ref 0 in
19     let t = ref 0 in (* WCET *)
20     for i = 0 to n-1 do
21       invariant { 0 <= !j <= i <= n /\ i = !j + !t }
22       while !j > 0 && b[!j-1] > a[i] do
23         invariant { 0 <= !j <= i /\ i = !j + !t } variant { !j }
24         j := !j - 1;
25         t := !t + 1
26       done;
27       b[!j] <- a[i];
28       j := !j + 1
29     done;
30     assert { 0 <= !t <= n }
32 end