-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathdoubly_linked.rs
606 lines (541 loc) · 24.7 KB
/
doubly_linked.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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
use vstd::prelude::*;
verus! {
mod doubly_linked_list {
use vstd::prelude::*;
use vstd::simple_pptr::*;
use vstd::raw_ptr::MemContents;
use vstd::assert_by_contradiction;
// Single node in the list
struct Node<V> {
prev: Option<PPtr<Node<V>>>,
next: Option<PPtr<Node<V>>>,
payload: V,
}
// Doubly-linked list
// Contains head pointer, tail pointer
// and in ghost code, tracks all the pointers and all the PointsTo permissions
// to access the nodes
pub struct DoublyLinkedList<V> {
// physical data:
head: Option<PPtr<Node<V>>>,
tail: Option<PPtr<Node<V>>>,
// ghost and tracked data:
ghost_state: Tracked<GhostState<V>>,
}
pub tracked struct GhostState<V> {
ghost ptrs: Seq<PPtr<Node<V>>>,
tracked points_to_map: Map<nat, PointsTo<Node<V>>>,
}
impl<V> DoublyLinkedList<V> {
/// Pointer to the node of index (i-1), or None if i is 0.
spec fn prev_of(&self, i: nat) -> Option<PPtr<Node<V>>> {
if i == 0 {
None
} else {
Some([email protected][i as int - 1])
}
}
/// Pointer to the node of index (i+1), or None if i is the last index.
spec fn next_of(&self, i: nat) -> Option<PPtr<Node<V>>> {
if i + 1 == [email protected]() {
None
} else {
Some([email protected][i as int + 1])
}
}
/// Node at index `i` is well-formed
spec fn well_formed_node(&self, i: nat) -> bool {
&&& [email protected]_to_map.dom().contains(i)
&&& [email protected]_to_map[i].pptr() == [email protected][i as int]
&&& [email protected]_to_map[i].mem_contents() matches MemContents::Init(node)
&& node.prev == self.prev_of(i) && node.next == self.next_of(i)
}
/// Linked list is well-formed
pub closed spec fn well_formed(&self) -> bool {
// Every node from 0 .. len - 1 is well-formed
&&& forall|i: nat| 0 <= i && i < [email protected]() ==> self.well_formed_node(i)
&&& if [email protected]() == 0 {
// If the list is empty, then the `head` and `tail` pointers are both None
self.head.is_none() && self.tail.is_none()
} else {
// If the list is non-empty, then `head` and `tail` pointers point to the
// the first and last nodes.
&&& self.head == Some([email protected][0])
&&& self.tail == Some([email protected][[email protected]() as int - 1])
}
}
/// Representation of this list as a sequence
pub closed spec fn view(&self) -> Seq<V> {
Seq::<V>::new(
|i: int| { [email protected]_to_map[i as nat].value().payload },
)
}
//// Interface of executable functions
/// Construct a new, empty, doubly-linked list.
pub fn new() -> (s: Self)
ensures
s.well_formed(),
[email protected]() == 0,
{
DoublyLinkedList {
ghost_state: Tracked(GhostState {
ptrs: Seq::empty(),
points_to_map: Map::tracked_empty(),
}),
head: None,
tail: None,
}
}
/// Insert one node, assuming the linked list is empty.
fn push_empty_case(&mut self, v: V)
requires
old(self).well_formed(),
old(self)[email protected]() == 0,
ensures
self.well_formed(),
self@ =~= old(self)@.push(v),
{
// Allocate a node to contain the payload
let (ptr, Tracked(points_to)) = PPtr::<Node<V>>::new(
Node::<V> { prev: None, next: None, payload: v },
);
// Update head and tail pointers
self.tail = Some(ptr);
self.head = Some(ptr);
// Update proof state
proof {
self.ghost_state.borrow_mut().ptrs = [email protected](ptr);
self.ghost_state.borrow_mut().points_to_map.tracked_insert(
([email protected]() - 1) as nat,
points_to,
);
}
}
/// Insert a value to the end of the list
pub fn push_back(&mut self, v: V)
requires
old(self).well_formed(),
ensures
self.well_formed(),
self@ == old(self)@.push(v),
{
match self.tail {
None => {
// Special case: list is empty
proof {
// Show that the `self.tail == None` implies the list is empty
assert_by_contradiction!([email protected]() == 0,
{
assert(self.well_formed_node(([email protected]() - 1) as nat)); // trigger
});
}
self.push_empty_case(v);
}
Some(old_tail_ptr) => {
proof {
assert(self.well_formed_node(([email protected]() - 1) as nat)); // trigger
}
// Allocate a new node to go on the end. It's 'prev' field points
// to the old tail pointer.
let (new_tail_ptr, Tracked(new_tail_pointsto)) = PPtr::<Node<V>>::new(
Node::<V> { prev: Some(old_tail_ptr), next: None, payload: v },
);
// Update the 'next' pointer of the previous tail node
// This is all equivalent to `(*old_tail_ptr).next = new_tail_ptr;`
let tracked mut old_tail_pointsto: PointsTo<Node<V>> =
self.ghost_state.borrow_mut().points_to_map.tracked_remove(([email protected]() - 1) as nat);
let mut old_tail_node = old_tail_ptr.take(Tracked(&mut old_tail_pointsto));
old_tail_node.next = Some(new_tail_ptr);
old_tail_ptr.put(Tracked(&mut old_tail_pointsto), old_tail_node);
proof {
self.ghost_state.borrow_mut().points_to_map.tracked_insert(
([email protected]() - 1) as nat,
old_tail_pointsto,
);
}
// Update `self.tail`
self.tail = Some(new_tail_ptr);
proof {
// Put the new tail's PointsTo into the map
self.ghost_state.borrow_mut().points_to_map.tracked_insert([email protected](), new_tail_pointsto);
[email protected] = [email protected](new_tail_ptr);
// Additional proof work to help the solver show that
// `self.well_formed()` has been restored.
assert(self.well_formed_node(([email protected]() - 2) as nat));
assert(self.well_formed_node(([email protected]() - 1) as nat));
assert(forall|i: nat| i < [email protected]() && old(self).well_formed_node(i)
==> self.well_formed_node(i));
assert forall|i: int| 0 <= i && i < [email protected]() as int - 1
implies old(self)@[i] == self@[i]
by {
assert(old(self).well_formed_node(i as nat)); // trigger
}
assert(self@ =~= old(self)@.push(v));
assert(self.well_formed());
}
}
}
}
/// Take a value from the end of the list. Requires the list to be non-empty.
pub fn pop_back(&mut self) -> (v: V)
requires
old(self).well_formed(),
old(self)@.len() > 0,
ensures
self.well_formed(),
self@ == old(self)@.drop_last(),
v == old(self)@[old(self)@.len() as int - 1],
{
assert(self.well_formed_node(([email protected]() - 1) as nat));
// Deallocate the last node in the list and get the payload.
// Note self.tail.unwrap() will always succeed because of the precondition `len > 0`
let last_ptr = self.tail.unwrap();
let tracked last_pointsto = self.ghost_state.borrow_mut().points_to_map.tracked_remove(
([email protected]() - 1) as nat,
);
let last_node = last_ptr.into_inner(Tracked(last_pointsto));
let v = last_node.payload;
match last_node.prev {
None => {
// If this was the *only* node in the list,
// we set both `head` and `tail` to None
self.tail = None;
self.head = None;
proof {
assert_by_contradiction!([email protected]() == 1,
{
assert(old(self).well_formed_node(([email protected]() - 2) as nat)); // trigger
});
}
},
Some(penultimate_ptr) => {
assert(old(self)@.len() >= 2);
assert(old(self).well_formed_node(([email protected]() - 2) as nat));
// Otherwise, we need to set the 'tail' pointer to the (new) tail pointer,
// i.e., the pointer that was previously the second-to-last pointer.
self.tail = Some(penultimate_ptr);
// And we need to set the 'next' pointer of the new tail node to None.
let tracked mut penultimate_pointsto =
self.ghost_state.borrow_mut().points_to_map.tracked_remove(([email protected]() - 2) as nat);
let mut penultimate_node = penultimate_ptr.take(Tracked(&mut penultimate_pointsto));
penultimate_node.next = None;
penultimate_ptr.put(Tracked(&mut penultimate_pointsto), penultimate_node);
proof {
self.ghost_state.borrow_mut().points_to_map.tracked_insert(
([email protected]() - 2) as nat,
penultimate_pointsto,
);
}
},
}
// Additional proof work to help the solver show that
// `self.well_formed()` has been restored.
proof {
[email protected] = [email protected]_last();
if [email protected]() > 0 {
assert(self.well_formed_node(([email protected]() - 1) as nat));
}
assert(forall|i: nat| i < [email protected]() && old(self).well_formed_node(i) ==> self.well_formed_node(i));
assert forall|i: int| 0 <= i && i < [email protected]() implies #[trigger] self@[i] == old(
self,
)@.drop_last()[i] by {
assert(old(self).well_formed_node(i as nat)); // trigger
}
assert(self@ =~= old(self)@.drop_last());
assert(self.well_formed());
}
return v;
}
/// Insert a value to the front of the list
pub fn push_front(&mut self, v: V)
requires
old(self).well_formed(),
ensures
self.well_formed(),
self@ == seq![v].add(old(self)@),
{
match self.head {
None => {
// Special case: list is empty
proof {
// Show that the `self.head == None` implies the list is empty
assert_by_contradiction!([email protected]() == 0, {
assert(self.well_formed_node(([email protected]() - 1) as nat));
});
}
self.push_empty_case(v);
assert(self@ =~= seq![v].add(old(self)@));
}
Some(old_head_ptr) => {
proof {
assert([email protected]() > 0);
assert(self.well_formed_node(0));
}
// Allocate a new node to go at the front. It's 'next' field points
// to the old head pointer.
let (new_head_ptr, Tracked(new_head_pointsto)) = PPtr::new(
Node::<V> { prev: None, next: Some(old_head_ptr), payload: v },
);
// Update the 'tail' pointer of the previous head node
// This is all equivalent to `(*old_head_ptr).next = new_head_ptr;`
let tracked mut old_head_pointsto =
self.ghost_state.borrow_mut().points_to_map.tracked_remove(0);
let mut old_head_node = old_head_ptr.take(Tracked(&mut old_head_pointsto));
old_head_node.prev = Some(new_head_ptr);
old_head_ptr.put(Tracked(&mut old_head_pointsto), old_head_node);
proof {
self.ghost_state.borrow_mut().points_to_map.tracked_insert(0, old_head_pointsto);
}
// Update `self.head`
self.head = Some(new_head_ptr);
proof {
// Put the new head's PointsTo into the map.
// This goes in at index 0, so we have to shift all the keys up by 1.
assert forall|j: nat|
0 <= j && j < old(self)@.len() implies [email protected]_to_map.dom().contains(
j,
) by {
assert(old(self).well_formed_node(j));
}
self.ghost_state.borrow_mut().points_to_map.tracked_map_keys_in_place(
Map::<nat, nat>::new(
|j: nat| 1 <= j && j <= old(self).view().len(),
|j: nat| (j - 1) as nat,
),
);
self.ghost_state.borrow_mut().points_to_map.tracked_insert(0, new_head_pointsto);
[email protected] = seq![new_head_ptr].add([email protected]);
// Additional proof work to help the solver show that
// `self.well_formed()` has been restored.
assert(self.well_formed_node(0));
assert(self.well_formed_node(1));
assert(forall|i: nat|
1 <= i && i <= old(self)[email protected]() && old(self).well_formed_node((i - 1) as nat)
==> #[trigger] self.well_formed_node(i));
assert forall|i: int| 1 <= i && i <= [email protected]() as int - 1
implies old(self)@[i - 1] == self@[i]
by {
assert(old(self).well_formed_node((i - 1) as nat)); // trigger
}
assert(self@ =~= seq![v].add(old(self)@));
assert(self.well_formed());
}
}
}
}
/// Take a value from the front of the list. Requires the list to be non-empty.
pub fn pop_front(&mut self) -> (v: V)
requires
old(self).well_formed(),
old(self).view().len() > 0,
ensures
self.well_formed(),
self@ == old(self)@.subrange(1, old(self)@.len() as int),
v == old(self)@[0],
{
assert(self.well_formed_node(0));
// Deallocate the first node in the list and get the payload.
// Note self.head.unwrap() will always succeed because of the precondition `len > 0`
let first_ptr = self.head.unwrap();
let tracked first_pointsto = self.ghost_state.borrow_mut().points_to_map.tracked_remove(0);
let first_node = first_ptr.into_inner(Tracked(first_pointsto));
let v = first_node.payload;
match first_node.next {
None => {
// If this was the *only* node in the list,
// we set both `head` and `tail` to None
self.tail = None;
self.head = None;
proof {
assert_by_contradiction!([email protected]() == 1,
{
assert(old(self).well_formed_node(1)); // trigger
});
}
}
Some(second_ptr) => {
assert(old(self)@.len() >= 2);
assert(old(self).well_formed_node(1));
// Otherwise, we need to set the 'head' pointer to the (new) head pointer,
// i.e., the pointer that was previously the second pointer.
self.head = Some(second_ptr);
// And we need to set the 'tail' pointer of the new head node to None
let tracked mut second_pointsto = self.ghost_state.borrow_mut().points_to_map.tracked_remove(1);
let mut second_node = second_ptr.take(Tracked(&mut second_pointsto));
second_node.prev = None;
second_ptr.put(Tracked(&mut second_pointsto), second_node);
proof {
self.ghost_state.borrow_mut().points_to_map.tracked_insert(1, second_pointsto);
// Since we removed index 0, we need to shift all the keys down,
// 1 -> 0, 2 -> 1, etc.
assert forall|j: nat|
1 <= j && j < old(self)@.len() implies [email protected]_to_map.dom().contains(
j,
) by {
assert(old(self).well_formed_node(j));
};
self.ghost_state.borrow_mut().points_to_map.tracked_map_keys_in_place(
Map::<nat, nat>::new(
|j: nat| 0 <= j && j < old(self).view().len() - 1,
|j: nat| (j + 1) as nat,
),
);
}
}
}
// Additional proof work to help the solver show that
// `self.well_formed()` has been restored.
proof {
if [email protected]() > 0 {
assert(self.well_formed_node(0));
}
assert(forall|i: nat|
i < self.view().len() && old(self).well_formed_node(i + 1) ==> self.well_formed_node(i));
assert forall|i: int| 0 <= i && i < [email protected]() implies #[trigger] self@[i] == old(
self,
)@.subrange(1, old(self)@.len() as int)[i] by {
assert(old(self).well_formed_node(i as nat + 1)); // trigger
}
assert(self@ =~= old(self)@.subrange(1, old(self)@.len() as int));
assert(self.well_formed());
}
return v;
}
/// Get a reference to the i^th value in the list
fn get<'a>(&'a self, i: usize) -> (v: &'a V)
requires
self.well_formed(),
0 <= i < [email protected](),
ensures
*v == self@[i as int]
{
// Iterate the nodes from 0 to j, starting at the head node
let mut j = 0;
let mut ptr = self.head.unwrap();
while j < i
invariant
self.well_formed(),
0 <= j <= i < [email protected](),
ptr == [email protected][j as int],
{
proof {
assert(self.well_formed_node(j as nat)); // trigger
}
// Get the next node from the 'next' field
let tracked pointsto_ref: &PointsTo<Node<V>> =
self.ghost_state.borrow().points_to_map.tracked_borrow(j as nat);
let node_ref: &Node<V> = ptr.borrow(Tracked(pointsto_ref));
let next_ptr = node_ref.next.unwrap();
j += 1;
ptr = next_ptr;
}
proof {
assert(self.well_formed_node(j as nat)); // trigger
}
// Get a reference to this node's payload and return it
let tracked pointsto_ref: &PointsTo<Node<V>> =
self.ghost_state.borrow().points_to_map.tracked_borrow(j as nat);
let node_ref: &Node<V> = ptr.borrow(Tracked(pointsto_ref));
return &node_ref.payload;
}
}
pub struct Iterator<'a, V> {
l: &'a DoublyLinkedList<V>,
cur: Option<PPtr<Node<V>>>,
index: Ghost<nat>,
}
impl<'a, V> Iterator<'a, V> {
pub closed spec fn list(&self) -> &'a DoublyLinkedList<V> {
self.l
}
pub closed spec fn index(&self) -> nat {
self.index@
}
pub closed spec fn valid(&self) -> bool {
&&& self.list().well_formed()
&&& self.index@ < self.list()@.len()
&&& self.cur.is_some() && self.cur.unwrap() =~= [email protected][self.index@ as int]
}
pub fn new(l: &'a DoublyLinkedList<V>) -> (it: Self)
requires
l.well_formed(),
[email protected]() > 0,
ensures
it.valid(),
it.index() == 0,
it.list() == l,
{
Iterator { l, cur: l.head, index: Ghost(0) }
}
pub fn value(&self) -> (v: &V)
requires
self.valid(),
ensures
v == self.list()@[self.index() as int],
{
let cur = self.cur.unwrap();
assert(self.l.well_formed_node(self.index()));
let tracked pointsto = self.l.ghost_state.borrow().points_to_map.tracked_borrow(self.index());
let node = cur.borrow(Tracked(pointsto));
&node.payload
}
pub fn move_next(&mut self) -> (good: bool)
requires
old(self).valid(),
ensures
old(self).list() == self.list(),
good == (old(self).index() < old(self).list()@.len() - 1),
good ==> (self.valid() && self.index() == old(self).index() + 1),
{
assert(self.l.well_formed_node(self.index()));
let cur = self.cur.unwrap();
let tracked pointsto = self.l.ghost_state.borrow().points_to_map.tracked_borrow(self.index());
let node = cur.borrow(Tracked(pointsto));
proof {
self.index@ = self.index@ + 1;
}
match node.next {
None => {
self.cur = None;
false
},
Some(next_ptr) => {
self.cur = Some(next_ptr);
true
},
}
}
}
}
mod main {
use super::doubly_linked_list::{DoublyLinkedList, Iterator};
pub fn run() {
let mut t = DoublyLinkedList::<u32>::new();
t.push_back(2);
t.push_back(3);
t.push_front(1); // 1, 2, 3
let mut it = Iterator::new(&t);
let v1 = it.value();
assert(*v1 == 1);
let g = it.move_next();
let v2 = it.value();
assert(*v2 == 2);
let _ = it.move_next();
let v3 = it.value();
assert(*v3 == 3);
let g = it.move_next();
assert(!g);
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);
}
}
fn main() {
main::run();
}
} // verus!