forked from creusot-rs/creusot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
02_gnome.rs
38 lines (34 loc) · 881 Bytes
/
02_gnome.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
extern crate creusot_contracts;
use creusot_contracts::{
logic::{Int, OrdLogic, Seq},
*,
};
#[predicate]
fn sorted_range<T: OrdLogic>(s: Seq<T>, l: Int, u: Int) -> bool {
pearlite! {
forall<i : Int, j : Int> l <= i && i < j && j < u ==> s[i] <= s[j]
}
}
#[predicate]
fn sorted<T: OrdLogic>(s: Seq<T>) -> bool {
sorted_range(s, 0, s.len())
}
#[ensures(sorted((^v).deep_model()))]
#[ensures((@^v).permutation_of(@v))]
pub fn gnome_sort<T: Ord + DeepModel>(v: &mut Vec<T>)
where
T::DeepModelTy: OrdLogic,
{
let old_v = ghost! { v };
let mut i = 0;
#[invariant(sorted, sorted_range(v.deep_model(), 0, @i))]
#[invariant(permutation, (@v).permutation_of(@old_v))]
while i < v.len() {
if i == 0 || v[i - 1].le(&v[i]) {
i += 1;
} else {
v.swap(i - 1, i);
i -= 1;
}
}
}