-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFloydTortoiseHare2.dfy
50 lines (41 loc) · 989 Bytes
/
FloydTortoiseHare2.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
41
42
43
44
45
46
47
48
49
50
class Node {
var next: Node
ghost var reachable: set<Node>
predicate Valid(n: nat)
reads this, reachable
{
&& this in reachable
&& (n != 0 && next != null ==>
&& next in reachable
&& next.reachable <= reachable
&& next.Valid(n - 1))
}
}
function Hop(n: Node, i: nat): Node
requires n != null ==> n.Valid(i)
reads if n != null then n.reachable else {}
{
if i == 0 then
n
else if n == null then
null
else
Hop(n.next, i - 1)
}
method TortoiseHare(n: Node) returns (cycle: bool)
decreases *
{
if n == null { return false; }
var tortoise := n;
var hare := n.next;
while hare != null && hare != tortoise
decreases *
invariant tortoise != null
{
if hare.next == null { return false; }
hare := hare.next.next;
tortoise := tortoise.next;
}
if hare == null { return false; }
return true;
}