-
Notifications
You must be signed in to change notification settings - Fork 1
/
000-has_close_elements.dfy
40 lines (39 loc) · 1.15 KB
/
000-has_close_elements.dfy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
function abs(val : real): real
{
if (val < 0.0) then
-val
else
val
}
method has_close_elements(numbers: seq<real>, threshold: real) returns (flag : bool)
// pre-conditions-start
requires threshold > 0.0
// pre-conditions-end
// post-conditions-start
ensures flag == (exists i: int, j: int :: i >= 0 && j >= 0 && i < |numbers| && j < |numbers| && i != j && abs(numbers[i] - numbers[j]) < threshold)
// post-conditions-end
{
// impl-start
flag := false;
for i := 0 to |numbers|
// invariants-start
invariant flag == (exists i1: int, j1: int :: i1 >= 0 && j1 >= 0 && i1 < i && j1 < |numbers| && i1 != j1 && abs(numbers[i1] - numbers[j1]) < threshold)
// invariants-end
{
for j := 0 to |numbers|
// invariants-start
invariant flag == (exists i1: int, j1: int :: i1 >= 0 && j1 >= 0 && ((i1 < i && j1 < |numbers|) || (i1 == i && j1 < j)) && i1 != j1 && abs(numbers[i1] - numbers[j1]) < threshold)
// invariants-end
{
if (i != j)
{
var distance: real := abs(numbers[i] - numbers[j]);
if (distance < threshold)
{
flag := true;
}
}
}
}
// impl-end
}