-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathdoubly_linked_xor.rs
518 lines (494 loc) · 18.8 KB
/
doubly_linked_xor.rs
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
use vstd::prelude::*;
use vstd::simple_pptr::*;
use vstd::*;
// "XOR Linked List". This is a sorta-cute (if not usually practical) folk data structure:
// A doubly-linked list which saves memory by having each node store the XOR of the two
// pointers to its two neighbors.
//
// This example uses the XOR Linked List to build a deque.
//
// TODO should really use usize, but bit-vector operations on usize aren't supported right now,
// so we use u64 and assume it's equivalent to usize.
verus! {
#[verifier::external_body]
proof fn lemma_usize_u64(x: u64)
ensures
x as usize as u64 == x,
{
unimplemented!();
}
// Single node in the list
struct Node<V> {
xored: u64,
v: V,
}
// Doubly-linked list
// Contains head pointer, tail pointer
// and in ghost code, tracks all the pointers and all the permissions to access the nodes
type MemPerms<V> = PointsTo<Node<V>>;
// The xor-doubly-linked list requires us to treat pointers as integer addresses.
// This is somewhat tricky because it forces us to think a bit about pointer provenance.
// The simple_pptr library abstracts this away (and verifies it) and thus lets us treat pointers
// as isomoprhic to usize values.
struct DListXor<V> {
ptrs: Ghost<Seq<PPtr<Node<V>>>>,
perms: Tracked<Map<nat, MemPerms<V>>>,
head: u64,
tail: u64,
}
impl<V> DListXor<V> {
spec fn wf_perms(&self) -> bool {
forall|i: nat| 0 <= i < [email protected]() ==> self.wf_perm(i)
}
spec fn prev_of(&self, i: nat) -> u64
recommends
i < [email protected](),
{
if i == 0 {
0
} else {
self.ptrs@[i - 1].addr() as u64
}
}
spec fn next_of(&self, i: nat) -> u64
recommends
i < [email protected](),
{
if i + 1 == [email protected]() {
0
} else {
self.ptrs@[i + 1int].addr() as u64
}
}
spec fn wf_perm(&self, i: nat) -> bool
recommends
i < [email protected](),
{
&&& [email protected]().contains(i)
&&& self.perms@[i].pptr() == self.ptrs@[i as int]
&&& self.perms@[i].pptr() == self.ptrs@[i as int]
&&& 0 < self.ptrs@[i as int].addr()
&&& self.ptrs@[i as int].addr() < 0x10000000000000000
&&& self.perms@[i].is_init()
&&& self.perms@[i].value().xored == (self.prev_of(i) ^ self.next_of(i))
}
spec fn wf_head(&self) -> bool {
if [email protected]() == 0 {
self.head == 0
} else {
self.head == self.ptrs@[0].addr()
}
}
spec fn wf_tail(&self) -> bool {
if [email protected]() == 0 {
self.tail == 0
} else {
self.tail == self.ptrs@[[email protected]() - 1].addr()
}
}
spec fn wf(&self) -> bool {
self.wf_perms() && self.wf_head() && self.wf_tail()
}
spec fn view(&self) -> Seq<V>
recommends
self.wf(),
{
Seq::<V>::new([email protected](), |i: int| { self.perms@[i as nat].value().v })
}
fn new() -> (s: Self)
ensures
s.wf(),
[email protected]() == 0,
{
DListXor {
ptrs: Ghost(Seq::empty()),
perms: Tracked(Map::tracked_empty()),
head: 0,
tail: 0,
}
}
fn push_empty_case(&mut self, v: V)
requires
old(self).wf(),
old(self)[email protected]() == 0,
ensures
self.wf(),
self@ == old(self)@.push(v),
{
let (ptr, Tracked(perm)) = PPtr::new(Node::<V> { xored: 0, v });
proof {
self.ptrs@ = [email protected](ptr);
(&perm).is_nonnull();
self.perms.borrow_mut().tracked_insert(([email protected]() - 1) as nat, perm);
}
self.tail = ptr.addr() as u64;
self.head = self.tail;
assert(0u64 ^ 0u64 == 0u64) by (bit_vector);
assert(self@ =~= old(self)@.push(v));
}
fn push_back(&mut self, v: V)
requires
old(self).wf(),
ensures
self.wf(),
self@ == old(self)@.push(v),
{
if self.tail == 0 {
// Special case: list is empty
proof {
assert_by_contradiction!([email protected]() == 0, {
assert(self.wf_perm(([email protected]() - 1) as nat));
});
}
self.push_empty_case(v);
} else {
assert([email protected]() > 0);
assert(self.wf_perm(([email protected]() - 1) as nat));
let tail_ptr_u64 = self.tail;
proof {
lemma_usize_u64(tail_ptr_u64);
}
let tail_ptr = PPtr::<Node<V>>::from_usize(tail_ptr_u64 as usize);
let tracked mut tail_perm: MemPerms<V> = self.perms.borrow_mut().tracked_remove(
([email protected]() - 1) as nat,
);
let mut tail_node = tail_ptr.take(Tracked(&mut tail_perm));
let second_to_last_ptr = tail_node.xored;
let (ptr, Tracked(perm)) = PPtr::new(
Node::<V> { xored: tail_ptr_u64, v },
);
proof {
perm.is_nonnull();
}
let new_ptr_u64 = ptr.addr() as u64;
tail_node.xored = second_to_last_ptr ^ new_ptr_u64;
tail_ptr.put(Tracked(&mut tail_perm), tail_node);
proof {
self.perms.borrow_mut().tracked_insert(
([email protected]() - 1) as nat,
tail_perm,
);
self.perms.borrow_mut().tracked_insert([email protected](), perm);
self.ptrs@ = [email protected](ptr);
}
self.tail = new_ptr_u64;
proof {
assert(tail_ptr_u64 ^ 0 == tail_ptr_u64) by (bit_vector);
let i = ([email protected]() - 2) as nat;
//assert([email protected]().contains(i));
//assert(self.perms@[i]@.pptr == self.ptrs@[i]@);
//assert(self.perms@[i].value.is_Some());
let prev_of_i = self.prev_of(i);
assert(prev_of_i ^ 0 == prev_of_i) by (bit_vector);
//assert(self.prev_of(i) == second_to_last_ptr);
//assert(self.next_of(i) == new_ptr_int);
//assert(self.perms@[i].value.get_Some_0().xored == (
// self.prev_of(i) ^ self.next_of(i)
//));
assert(self.wf_perm(([email protected]() - 2) as nat));
assert(self.wf_perm(([email protected]() - 1) as nat));
assert(forall|i: nat|
i < [email protected]() ==> old(self).wf_perm(i) ==> self.wf_perm(i));
assert(self.wf_perms());
assert(self.wf_tail());
assert(self@[[email protected]() - 1] == v);
assert forall|i: int| 0 <= i < [email protected]() - 1 implies old(self)@[i]
== self@[i] by {
assert(old(self).wf_perm(i as nat)); // trigger
};
assert(self@ =~= old(self)@.push(v));
}
}
}
fn pop_back(&mut self) -> (v: V)
requires
old(self).wf(),
old(self)@.len() > 0,
ensures
self.wf(),
self@ == old(self)@.drop_last(),
v == old(self)@[old(self)@.len() - 1],
{
assert(self.wf_perm(([email protected]() - 1) as nat));
let last_u64 = self.tail;
proof {
lemma_usize_u64(last_u64);
}
let last_ptr = PPtr::<Node<V>>::from_usize(last_u64 as usize);
let tracked last_perm: MemPerms<V> = self.perms.borrow_mut().tracked_remove(
([email protected]() - 1) as nat,
);
let last_node = last_ptr.into_inner(Tracked(last_perm));
let penult_u64 = last_node.xored;
let v = last_node.v;
proof {
let self_head = self.head;
assert(self_head ^ 0 == self_head) by (bit_vector);
assert(0u64 ^ 0 == 0) by (bit_vector);
}
if penult_u64 == 0 {
self.tail = 0;
self.head = 0;
proof {
assert_by_contradiction!([email protected]() == 1, {
assert(old(self).wf_perm(([email protected]() - 2) as nat));
#[verifier::spec] let actual_penult_u64 = self.prev_of(([email protected]() - 1) as nat);
assert(actual_penult_u64 ^ 0 == actual_penult_u64) by(bit_vector);
});
}
} else {
self.tail = penult_u64;
assert(old(self)@.len() != 1);
assert(old(self)@.len() >= 2);
assert(old(self).wf_perm(([email protected]() - 2) as nat));
proof {
let actual_penult_u64 = self.prev_of(([email protected]() - 1) as nat);
assert(actual_penult_u64 ^ 0 == actual_penult_u64) by (bit_vector);
lemma_usize_u64(penult_u64);
}
let penult_ptr = PPtr::<Node<V>>::from_usize(penult_u64 as usize);
let tracked mut penult_perm = self.perms.borrow_mut().tracked_remove(
([email protected]() - 2) as nat,
);
let mut penult_node = penult_ptr.take(Tracked(&mut penult_perm));
let t: Ghost<u64> = Ghost(self.prev_of(([email protected]() - 2) as nat));
assert((t@ ^ last_u64) ^ last_u64 == t@ ^ 0) by (bit_vector);
penult_node.xored = penult_node.xored ^ last_u64;
assert(penult_node.xored == t@ ^ 0);
penult_ptr.put(Tracked(&mut penult_perm), penult_node);
proof {
self.perms.borrow_mut().tracked_insert(
([email protected]() - 2) as nat,
penult_perm,
);
}
}
proof {
self.ptrs@ = [email protected]_last();
}
proof {
assert(self.wf_head());
assert(self.wf_tail());
if [email protected]() > 0 {
/*#[verifier::spec] let i = [email protected]() - 1;
assert([email protected]() == old(self)[email protected]() - 1);
assert([email protected]().contains(i));
assert(self.perms@[i]@.pptr == self.ptrs@[i]@);
assert(0 < self.ptrs@[i]@);
assert(self.ptrs@[i]@ < 0x10000000000000000);
assert(self.perms@[i].value.is_Some());
assert(self.perms@[i].value.get_Some_0().xored == (
self.prev_of(i) ^ self.next_of(i)
));*/
assert(self.wf_perm(([email protected]() - 1) as nat));
}
assert(forall|i: nat| i < [email protected]() ==> old(self).wf_perm(i) ==> self.wf_perm(i));
assert(self.wf_perms());
assert forall|i: int| 0 <= i < [email protected]() implies #[trigger] self@[i] == old(
self,
)@.drop_last()[i] by {
assert(old(self).wf_perm(i as nat)); // trigger
}
assert(self@ =~= old(self)@.drop_last());
}
v
}
fn pop_front(&mut self) -> (v: V)
requires
old(self).wf(),
old(self)@.len() > 0,
ensures
self.wf(),
self@ == old(self)@.subrange(1, old(self)@.len() as int),
v == old(self)@[0],
{
assert(self.wf_perm(0));
let first_u64 = self.head;
proof {
lemma_usize_u64(first_u64);
}
let first_ptr = PPtr::<Node<V>>::from_usize(first_u64 as usize);
let tracked first_perm: MemPerms<V> = self.perms.borrow_mut().tracked_remove(0);
let first_node = first_ptr.into_inner(Tracked(first_perm));
let second_u64 = first_node.xored;
let v = first_node.v;
proof {
let self_tail = self.tail;
assert(self_tail ^ 0 == self_tail) by (bit_vector);
assert(0u64 ^ 0 == 0) by (bit_vector);
}
if second_u64 == 0 {
self.tail = 0;
self.head = 0;
proof {
assert_by_contradiction!([email protected]() == 1, {
assert(old(self).wf_perm(1));
#[verifier::spec] let actual_second_u64 = self.next_of(0);
assert(0 ^ actual_second_u64 == actual_second_u64) by(bit_vector);
});
}
} else {
self.head = second_u64;
assert(old(self)@.len() != 1);
assert(old(self)@.len() >= 2);
assert(old(self).wf_perm(1));
proof {
let actual_second_u64 = self.next_of(0);
assert(0 ^ actual_second_u64 == actual_second_u64) by (bit_vector);
lemma_usize_u64(second_u64);
}
let second_ptr = PPtr::<Node<V>>::from_usize(second_u64 as usize);
let tracked mut second_perm = (self.perms.borrow_mut()).tracked_remove(1);
let mut second_node = second_ptr.take(Tracked(&mut second_perm));
let t: Ghost<u64> = Ghost(self.next_of(1));
assert((first_u64 ^ t@) ^ first_u64 == 0 ^ t@) by (bit_vector);
second_node.xored = second_node.xored ^ first_u64;
assert(second_node.xored == 0 ^ t@);
second_ptr.put(Tracked(&mut second_perm), second_node);
proof {
self.perms.borrow_mut().tracked_insert(1, second_perm);
assert forall|j: nat| 1 <= j < old(self)@.len() implies [email protected]().contains(
j,
) by {
assert(old(self).wf_perm(j));
}
(self.perms.borrow_mut()).tracked_map_keys_in_place(
Map::<nat, nat>::new(
|j: nat| 0 <= j < old(self)@.len() - 1,
|j: nat| (j + 1) as nat,
),
);
}
}
proof {
self.ptrs@ = [email protected](1, [email protected]() as int);
}
proof {
assert(self.wf_tail());
assert(self.wf_head());
if [email protected]() > 0 {
assert(self.wf_perm(0));
}
assert(forall|i: nat| i < [email protected]() ==> old(self).wf_perm(i + 1) ==> self.wf_perm(i));
assert(self.wf_perms());
assert forall|i: int| 0 <= i < [email protected]() implies #[trigger] self@[i] == old(
self,
)@.subrange(1, old(self)@.len() as int)[i] by {
assert(old(self).wf_perm(i as nat + 1)); // trigger
}
assert(self@ =~= old(self)@.subrange(1, old(self)@.len() as int));
}
v
}
fn push_front(&mut self, v: V)
requires
old(self).wf(),
ensures
self.wf(),
self@ == seq![v].add(old(self)@),
{
if self.tail == 0 {
// Special case: list is empty
proof {
assert_by_contradiction!([email protected]() == 0, {
assert(self.wf_perm(([email protected]() - 1) as nat));
});
}
self.push_empty_case(v);
assert(self@ =~= seq![v].add(old(self)@));
} else {
assert([email protected]() > 0);
assert(self.wf_perm(0));
let head_ptr_u64 = self.head;
proof {
lemma_usize_u64(head_ptr_u64);
}
let head_ptr = PPtr::<Node<V>>::from_usize(head_ptr_u64 as usize);
let tracked mut head_perm: MemPerms<V> = (self.perms.borrow_mut()).tracked_remove(
0,
);
let mut head_node = head_ptr.take(Tracked(&mut head_perm));
let second_ptr = head_node.xored;
let (ptr, Tracked(perm)) = PPtr::new(
Node::<V> { xored: head_ptr_u64, v },
);
proof {
perm.is_nonnull();
}
let new_ptr_u64 = ptr.addr() as u64;
head_node.xored = new_ptr_u64 ^ second_ptr;
head_ptr.put(Tracked(&mut head_perm), head_node);
proof {
self.perms.borrow_mut().tracked_insert(0, head_perm);
assert forall|j: nat| 0 <= j < old(self)@.len() implies [email protected]().contains(
j,
) by {
assert(old(self).wf_perm(j));
}
self.perms.borrow_mut().tracked_map_keys_in_place(
Map::<nat, nat>::new(
|j: nat| 1 <= j <= old(self)@.len(),
|j: nat| (j - 1) as nat,
),
);
self.perms.borrow_mut().tracked_insert(0, perm);
self.ptrs@ = seq![ptr].add(self.ptrs@);
}
self.head = new_ptr_u64;
proof {
assert(0 ^ head_ptr_u64 == head_ptr_u64) by (bit_vector);
let i = 1;
//assert([email protected]().contains(i));
//assert(self.perms@[i]@.pptr == self.ptrs@[i]@);
//assert(self.perms@[i].value.is_Some());
let next_of_i = self.next_of(i);
assert(0 ^ next_of_i == next_of_i) by (bit_vector);
//assert(self.prev_of(i) == second_to_last_ptr);
//assert(self.next_of(i) == new_ptr_int);
//assert(self.perms@[i].value.get_Some_0().xored == (
// self.prev_of(i) ^ self.next_of(i)
//));
assert([email protected](1).value().xored == new_ptr_u64
^ second_ptr);
assert([email protected](0).value().xored == head_ptr_u64);
assert([email protected](1).pptr().addr() == head_ptr_u64);
assert(self.wf_perm(1));
assert(self.wf_perm(0));
assert(forall|i: nat|
1 <= i <= old(self)[email protected]() ==> old(self).wf_perm((i - 1) as nat)
==> #[trigger] self.wf_perm(i));
assert(self.wf_perms());
assert(self.wf_tail());
assert(self@[0] == v);
assert forall|i: int| 1 <= i <= [email protected]() - 1 implies old(self)@[i - 1]
== self@[i] by {
assert(old(self).wf_perm((i - 1) as nat)); // trigger
};
assert(self@ =~= seq![v].add(old(self)@));
}
}
}
}
#[verifier::external_body]
fn print_result(msg: &'static str, value: u32) {
println!("{}: {value}", msg);
}
fn main() {
let mut t = DListXor::<u32>::new();
t.push_back(2);
t.push_back(3);
t.push_front(1); // 1, 2, 3
print_result("pushed", 2);
print_result("pushed", 3);
print_result("pushed", 1);
let x = t.pop_back(); // 3
let y = t.pop_front(); // 1
let z = t.pop_front(); // 2
assert(x == 3);
assert(y == 1);
assert(z == 2);
print_result("popped", x);
print_result("popped", y);
print_result("popped", z);
}
} // verus!