-
Notifications
You must be signed in to change notification settings - Fork 47
/
02_iter_mut.rs
100 lines (87 loc) · 2.67 KB
/
02_iter_mut.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
#![feature(slice_take)]
extern crate creusot_contracts;
use creusot_contracts::{
invariant::{inv, Invariant},
logic::{Int, Seq},
*,
};
mod common;
use common::Iterator;
struct IterMut<'a, T> {
inner: &'a mut [T],
}
impl<'a, T> Invariant for IterMut<'a, T> {
#[open]
#[predicate(prophetic)]
fn invariant(self) -> bool {
// Property that is always true but we must carry around..
pearlite! { (^self.inner)@.len() == (*self.inner)@.len() }
}
}
impl<'a, T> Iterator for IterMut<'a, T> {
type Item = &'a mut T;
#[open]
#[predicate(prophetic)]
fn completed(&mut self) -> bool {
pearlite! { self.inner.resolve() && self.inner@.ext_eq(Seq::EMPTY) }
}
#[open]
#[predicate(prophetic)]
fn produces(self, visited: Seq<Self::Item>, tl: Self) -> bool {
pearlite! {
self.inner@.len() == visited.len() + tl.inner@.len() &&
(forall<i:Int> 0 <= i && i < self.inner@.len() ==>
*self.inner.to_mut_seq()[i] == *visited.concat(tl.inner.to_mut_seq())[i] &&
^self.inner.to_mut_seq()[i] == ^visited.concat(tl.inner.to_mut_seq())[i]
)
}
}
#[law]
#[open]
#[ensures(self.produces(Seq::EMPTY, self))]
fn produces_refl(self) {}
#[law]
#[open]
#[requires(a.produces(ab, b))]
#[requires(b.produces(bc, c))]
#[ensures(a.produces(ab.concat(bc), c))]
fn produces_trans(a: Self, ab: Seq<Self::Item>, b: Self, bc: Seq<Self::Item>, c: Self) {}
#[ensures(match result {
None => self.completed(),
Some(v) => (*self).produces(Seq::singleton(v), ^self)
})]
fn next(&mut self) -> Option<Self::Item> {
(self.inner).take_first_mut()
}
}
impl<'a, T> IterMut<'a, T> {
#[ensures(result == self)]
fn into_iter(self) -> Self {
self
}
}
#[ensures(result.inner@ == v@)]
#[ensures((^result.inner)@ == (^v)@)]
#[ensures((^v)@.len() == v@.len())]
fn iter_mut<'a, T>(v: &'a mut Vec<T>) -> IterMut<'a, T> {
IterMut { inner: &mut v[..] }
}
#[ensures((^v)@.len() == v@.len())]
#[ensures(forall<i : _> 0 <= i && i < v@.len() ==> (^v)[i]@ == 0)]
pub fn all_zero(v: &mut Vec<usize>) {
let mut it = iter_mut(v).into_iter();
let iter_old = snapshot! { it };
let mut produced = snapshot! { Seq::EMPTY };
#[invariant(inv(it))]
#[invariant(iter_old.produces(produced.inner(), it))]
#[invariant(forall<i : Int> 0 <= i && i < produced.len() ==> (^produced[i])@ == 0)]
loop {
match it.next() {
Some(x) => {
produced = snapshot! { produced.concat(Seq::singleton(x)) };
*x = 0;
}
None => break,
}
}
}