-
Notifications
You must be signed in to change notification settings - Fork 1
/
AVL.dfy
219 lines (198 loc) · 6.13 KB
/
AVL.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// RUN: /rlimit:500000
// /noCheating:1
module AVL {
// newtype balance = x: int | -1 <= x <= 1
class Node {
var left: Node
var data: int
var right: Node
// var bal: balance
ghost var repr: set<object>
ghost var contents: set<int>
constructor(x: int)
ensures Valid() && contents == {x} && fresh(repr)
{
left := null;
data := x;
right := null;
repr := {this};
contents := {x};
new;
}
predicate Valid()
reads this, repr
{
&& this in repr
&& null !in repr
&& (left != null ==>
&& left in repr
&& left.repr <= repr
&& this !in left.repr
&& (forall x | x in left.contents :: x < data)
&& left.Valid())
&& (right != null ==>
&& right in repr
&& right.repr <= repr
&& this !in right.repr
&& (forall x | x in right.contents :: x > data)
&& right.Valid())
&& GetRepr(left) !! GetRepr(right)
&& contents == GetContents(left) + GetContents(right) + {data}
}
static function GetRepr(n: Node): set<object>
reads n
{
if n == null then {} else n.repr
}
static function GetContents(n: Node): set<int>
reads n
{
if n == null then {} else n.contents
}
method {:rlimit 5000000} Insert(x: int)
requires Valid()
decreases repr
modifies repr
ensures Valid() && contents == old(contents) + {x} && fresh(repr - old(repr))
{
if x == data { return; }
else if x < data {
if left == null {
left := new Node(x);
} else {
left.Insert(x);
}
repr := repr + left.repr;
} else {
if right == null {
right := new Node(x);
} else {
right.Insert(x);
}
repr := repr + right.repr;
}
contents := contents + {x};
}
method Contains(x: int) returns (b: bool)
requires Valid()
decreases repr
ensures b <==> x in contents
{
if x == data {
return true;
} else if x < data {
if left == null {
return false;
}
b := left.Contains(x);
} else {
if right == null {
return false;
}
b := right.Contains(x);
}
}
method {:rlimit 10000000} PopFront() returns (x: int, node: Node)
requires Valid() && contents != {}
modifies repr
decreases repr
ensures x in old(contents) && (forall y | y in old(contents) :: x <= y)
ensures node != null ==> node.Valid() && fresh(node.repr - old(repr))
ensures old(contents) - {x} == GetContents(node)
{
if left == null {
x := data;
node := right;
return;
}
x, left := left.PopFront();
repr := repr + GetRepr(left);
contents := contents - {x};
node := this;
}
method {:rlimit 10000000} Remove(x: int) returns (node: Node)
requires Valid()
modifies repr
decreases repr
ensures node != null ==> node.Valid() && fresh(node.repr - old(repr))
ensures old(contents) - {x} == GetContents(node)
{
if x == data {
if left == null {
return right;
}
if right == null {
return left;
}
data, right := right.PopFront();
repr := repr + GetRepr(right);
} else if x < data {
if left != null {
left := left.Remove(x);
repr := repr + GetRepr(left);
}
} else {
if right != null {
right := right.Remove(x);
repr := repr + GetRepr(right);
}
}
contents := contents - {x};
return this;
}
}
class BST {
var root: Node
ghost var repr: set<object>
ghost var contents: set<int>
constructor BST()
{
root := null;
repr := {};
contents := {};
}
predicate Valid()
reads this, repr
{
&& null !in repr
&& this in repr
&& (root != null ==>
&& root in repr
&& this !in root.repr
&& root.repr <= repr
&& root.Valid()
&& contents == root.contents)
&& (root == null ==> contents == {})
}
method Contains(x: int) returns (b: bool)
requires Valid()
ensures b <==> x in contents
{
if root == null { return false; }
b := root.Contains(x);
}
method Insert(x: int)
requires Valid()
modifies repr
ensures Valid() && fresh(repr - old(repr)) && contents == old(contents) + {x}
{
if root == null {
root := new Node(x);
} else {
root.Insert(x);
}
repr := repr + root.repr;
contents := root.contents;
}
method Remove(x: int)
requires Valid()
modifies repr
ensures Valid() && fresh(repr - old(repr)) && contents == old(contents) - {x}
{
if root == null { return; }
root := root.Remove(x);
repr := repr + Node.GetRepr(root);
contents := Node.GetContents(root);
}
}
}