mirror of
https://github.com/AdaCore/why3.git
synced 2026-02-12 12:34:55 -08:00
reorganize examples/
- all programs with sessions are in examples/ - all programs without sessions are in examples/in_progress/ (if you have private sessions for those, just move them there) - all pure logical problems are in logic/ (to simplify bench scripts and gallery building; they are few anyway) - all OCaml programs are in examples/use_api/ - all strange stuff is in examples/misc/ (most of it should probably go) - Claude's solutions for Foveoos 2011 are in examples/foveoos11-cm/ (why do we need two sets of solutions for quite simple problems?) - hoare_logic, bitvectors, vacid_0_binary_heaps are in examples/ Bench scripts and documentation are updated. Also, bench/bench is simplified a little bit.
This commit is contained in:
56
examples/decrease1.mlw
Normal file
56
examples/decrease1.mlw
Normal file
@@ -0,0 +1,56 @@
|
||||
|
||||
(*
|
||||
We look for the first occurrence of zero in an array of integers.
|
||||
The values have the following property: they never decrease by more than one.
|
||||
The code makes use of that property to speed up the search.
|
||||
*)
|
||||
|
||||
module Decrease1
|
||||
|
||||
use import int.Int
|
||||
use import ref.Ref
|
||||
use import array.Array
|
||||
|
||||
predicate decrease1 (a: array int) =
|
||||
forall i: int. 0 <= i < length a - 1 -> a[i+1] >= a[i] - 1
|
||||
|
||||
lemma decrease1_induction:
|
||||
forall a: array int. decrease1 a ->
|
||||
forall i j: int. 0 <= i <= j < length a -> a[j] >= a[i] + i - j
|
||||
|
||||
exception Found
|
||||
|
||||
let search (a: array int)
|
||||
requires { decrease1 a }
|
||||
ensures {
|
||||
(result = -1 /\ forall j: int. 0 <= j < length a -> a[j] <> 0)
|
||||
\/ (0 <= result < length a /\ a[result] = 0 /\
|
||||
forall j: int. 0 <= j < result -> a[j] <> 0) }
|
||||
= let i = ref 0 in
|
||||
try
|
||||
while !i < length a do
|
||||
invariant { 0 <= !i }
|
||||
invariant { forall j: int. 0 <= j < !i -> j < length a -> a[j] <> 0 }
|
||||
variant { length a - !i }
|
||||
if a[!i] = 0 then raise Found;
|
||||
if a[!i] > 0 then i := !i + a[!i] else i := !i + 1
|
||||
done;
|
||||
-1
|
||||
with Found ->
|
||||
!i
|
||||
end
|
||||
|
||||
let rec search_rec (a: array int) (i : int)
|
||||
requires { decrease1 a /\ 0 <= i }
|
||||
ensures {
|
||||
(result = -1 /\ forall j: int. i <= j < length a -> a[j] <> 0)
|
||||
\/ (i <= result < length a /\ a[result] = 0 /\
|
||||
forall j: int. i <= j < result -> a[j] <> 0) }
|
||||
= if i < length a then
|
||||
if a[i] = 0 then i
|
||||
else if a[i] > 0 then search_rec a (i + a[i])
|
||||
else search_rec a (i + 1)
|
||||
else
|
||||
-1
|
||||
|
||||
end
|
||||
Reference in New Issue
Block a user