Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Why3
why3
Commits
39d2ec48
Commit
39d2ec48
authored
Nov 16, 2010
by
Jean-Christophe Filliâtre
Browse files
new example: binary search (alt-ergo rules)
parent
ab24f0dc
Changes
2
Hide whitespace changes
Inline
Side-by-side
examples/programs/.gitignore
View file @
39d2ec48
...
...
@@ -4,3 +4,4 @@ vstte10_search_list
vstte10_aqueue
insertion_sort_list
mergesort_list
binary_search
examples/programs/binary_search.mlw
0 → 100644
View file @
39d2ec48
(* Binary search *)
{
use array.ArrayLength as A
use import int.ComputerDivision
type array = A.t int int
logic (#) (a : array) (i : int) : int = A.get a i
}
let array_get (a : ref array) i =
{ 0 <= i < A.length !a } A.get !a i { result = A.get !a i }
let array_set (a : ref array) i v =
{ 0 <= i < A.length !a } a := A.set !a i v { !a = A.set (old !a) i v }
let length (a : ref array) =
{ } A.length !a { result = A.length !a }
exception Break of int
exception Not_found
let binary_search (a : ref array) (v : int) =
{ A.length !a >= 1 and
forall i1 i2 : int. 0 <= i1 <= i2 < A.length !a -> !a#i1 <= !a#i2 }
try
let l = ref 0 in
let u = ref (length a - 1) in
while !l <= !u do
invariant {
0 <= !l and !u < A.length !a and
forall i : int. 0 <= i < A.length !a -> !a#i = v -> !l <= i <= !u }
variant { !u - !l }
let m = !l + div (!u - !l) 2 in
assert { !l <= m <= !u };
if array_get a m < v then
l := m + 1
else if array_get a m > v then
u := m - 1
else
raise (Break m)
done;
raise Not_found
with Break i ->
i
end
{ 0 <= result < A.length !a and !a#result = v }
| Not_found -> { forall i:int. 0 <= i < A.length !a -> !a#i <> v }
(*
Local Variables:
compile-command: "unset LANG; make -C ../.. examples/programs/binary_search.gui"
End:
*)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment