-
Notifications
You must be signed in to change notification settings - Fork 1
/
057-monotonic.dfy
36 lines (33 loc) · 962 Bytes
/
057-monotonic.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
method monotonic(xs: seq<int>) returns (result: bool)
// pre-conditions-start
requires |xs| > 0
// pre-conditions-end
// post-conditions-start
ensures result <==> (forall i, j :: 0 <= i < j < |xs| ==> xs[i] < xs[j]) || (forall i, j :: 0 <= i < j < |xs| ==> xs[i] > xs[j])
// post-conditions-end
{
// impl-start
if |xs| == 1 {
return true;
}
var increasing := true;
var decreasing := true;
var i := 1;
while i < |xs|
// invariants-start
invariant 1 <= i <= |xs|
invariant increasing <==> (forall j, k :: 0 <= j < k < i ==> xs[j] < xs[k])
invariant decreasing <==> (forall j, k :: 0 <= j < k < i ==> xs[j] > xs[k])
// invariants-end
{
if xs[i - 1] >= xs[i] {
increasing := false;
}
if xs[i - 1] <= xs[i] {
decreasing := false;
}
i := i + 1;
}
result := increasing || decreasing;
// impl-end
}