forked from viperproject/prusti-dev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Heapsort.rs
167 lines (151 loc) · 4.57 KB
/
Heapsort.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
//! An adaptation of the example from
//! https://rosettacode.org/wiki/Sorting_algorithms/Heapsort#Rust
//!
//! Changes:
//!
//! + Monomorphised types.
//! + Wrapped built-in types and functions.
//! + Replaced closure with a function.
//! + Rewrote loops into supported shape (while bool with no break, continue, or return).
//!
//! Verified properties:
//!
//! + Absence of panics.
extern crate prusti_contracts;
pub struct VecWrapperI32{
v: Vec<i32>
}
impl VecWrapperI32 {
#[trusted]
#[pure]
#[ensures="result >= 0"]
pub fn len(&self) -> usize {
self.v.len()
}
#[trusted]
#[ensures="result.len() == 0"]
pub fn new() -> Self {
VecWrapperI32{ v: Vec::new() }
}
#[trusted]
#[pure]
#[requires="0 <= index && index < self.len()"]
pub fn lookup(&self, index: usize) -> i32 {
self.v[index]
}
#[trusted]
#[requires="0 <= index && index < self.len()"]
#[ensures="after_expiry(
self.len() == old(self.len()) &&
self.lookup(index) == before_expiry(*result) &&
(
forall i: usize :: (0 <= i && i < self.len() && i != index) ==>
self.lookup(i) == old(self.lookup(i))
)
)"]
pub fn borrow(&mut self, index: usize) -> &mut i32 {
self.v.get_mut(index).unwrap()
}
#[trusted]
#[requires="0 <= index && index < self.len()"]
#[ensures="self.len() == old(self.len())"]
#[ensures="self.lookup(index) == value"]
#[ensures="forall i: usize :: (0 <= i && i < self.len() && i != index) ==>
self.lookup(i) == old(self.lookup(i))"]
pub fn store(&mut self, index: usize, value: i32) {
self.v[index] = value;
}
#[trusted]
#[ensures="self.len() == old(self.len()) + 1"]
#[ensures="self.lookup(old(self.len())) == value"]
#[ensures="forall i: usize :: (0 <= i && i < old(self.len())) ==>
self.lookup(i) == old(self.lookup(i))"]
pub fn push(&mut self, value: i32) {
self.v.push(value);
}
#[trusted]
#[requires="0 <= index_a && index_a < self.len()"]
#[requires="0 <= index_b && index_b < self.len()"]
#[ensures="self.len() == old(self.len())"]
#[ensures="self.lookup(index_a) == old(self.lookup(index_b))"]
#[ensures="self.lookup(index_b) == old(self.lookup(index_a))"]
#[ensures="forall i: usize :: (0 <= i && i < self.len() && i != index_a && i != index_b) ==>
self.lookup(i) == old(self.lookup(i))"]
pub fn swap(&mut self, index_a: usize, index_b: usize) {
self.v.swap(index_a, index_b);
}
}
#[pure]
fn order(x: i32, y: i32) -> bool {
x < y
}
fn main() {
/*let mut v = VecWrapperI32::new();
v.push(4);
v.push(6);
v.push(8);
v.push(1);
v.push(0);
v.push(3);
v.push(2);
v.push(2);
v.push(9);
v.push(5);
heap_sort(&mut v);*/
}
#[ensures="array.len() == old(array.len())"]
fn heap_sort(array: &mut VecWrapperI32)
{
let len = array.len();
let mut start = len/2;
let mut continue_loop = start > 0;
// Create heap
#[invariant="len == array.len()"]
#[invariant="start <= len/2"]
#[invariant="continue_loop ==> start > 0"]
while continue_loop {
start -= 1;
shift_down(array, start, len - 1);
continue_loop = start > 0;
}
let mut end = len;
let mut continue_loop = end > 1;
#[invariant="len == array.len()"]
#[invariant="end <= len"]
#[invariant="continue_loop ==> end > 1"]
while continue_loop {
end -= 1;
let start = 0;
array.swap(start, end);
shift_down(array, start, end - 1);
continue_loop = end > 1;
}
}
#[requires="end < array.len()"]
#[requires="0 <= start && start < array.len()"]
#[requires="0 <= end && end < array.len()"]
#[ensures="array.len() == old(array.len())"]
fn shift_down(array: &mut VecWrapperI32, start: usize, end: usize)
{
let mut root = start;
let mut continue_loop = true;
#[invariant="0 <= root && root < array.len()"]
#[invariant="0 <= end && end < array.len()"]
#[invariant="array.len() == old(array.len())"]
while continue_loop {
let mut child = root * 2 + 1;
if child > end {
continue_loop = false;
} else {
if child + 1 <= end && order(*array.borrow(child), *array.borrow(child + 1)) {
child += 1;
}
if order(*array.borrow(root), *array.borrow(child)) {
array.swap(root, child);
root = child
} else {
continue_loop = false;
}
}
}
}