forked from creusot-rs/creusot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathite_normalize.rs
231 lines (206 loc) · 6.41 KB
/
ite_normalize.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
#![feature(allocator_api)]
extern crate creusot_contracts;
use creusot_contracts::{std::clone::Clone, *};
#[trusted]
struct BTreeMap<K, V>(std::collections::BTreeMap<K, V>);
impl<K: DeepModel, V> BTreeMap<K, V> {
#[trusted]
fn new() -> Self {
Self(std::collections::BTreeMap::new())
}
#[trusted]
#[ensures(result == None ==> [email protected](key.deep_model()) == None)]
#[ensures(forall<v: &V> result == Some(v) ==> [email protected](key.deep_model()) == Some(*v))]
fn get<'a>(&'a self, key: &'a K) -> Option<&'a V>
where
K: Ord,
{
self.0.get(key)
}
#[trusted]
#[ensures(forall<i: K::DeepModelTy> (^self)@.get(i) == (if i == key.deep_model() { Some(value) } else { [email protected](i) }))]
fn insert(&mut self, key: K, value: V) -> Option<V>
where
K: Ord,
{
self.0.insert(key, value)
}
}
impl<K: Clone, V: Clone> Clone for BTreeMap<K, V> {
#[trusted]
#[ensures(*self == result)]
fn clone(&self) -> Self {
Self(self.0.clone())
}
}
impl<K: DeepModel, V> ShallowModel for BTreeMap<K, V> {
type ShallowModelTy = creusot_contracts::logic::Mapping<K::DeepModelTy, Option<V>>;
#[logic]
#[trusted]
fn shallow_model(self) -> Self::ShallowModelTy {
pearlite! { absurd }
}
}
#[derive(Clone)]
pub enum Expr {
IfThenElse { c: Box<Expr>, t: Box<Expr>, e: Box<Expr> },
Var { v: usize },
True,
False,
}
// FIXME: this should go away, we have not defined any order relation on Expr
#[trusted]
impl WellFounded for Expr {}
use std::alloc::Allocator;
extern_spec! {
mod std {
mod boxed {
impl<T : Clone, A : Allocator + Clone> Clone for Box<T, A> {
#[ensures(result == *self)]
fn clone(&self) -> Self;
}
}
}
}
impl From<usize> for Expr {
fn from(a: usize) -> Self {
Self::variable(a)
}
}
impl From<bool> for Expr {
fn from(b: bool) -> Self {
if b {
Self::True
} else {
Self::False
}
}
}
impl Expr {
#[ensures(result == Expr::IfThenElse { c: Box::new(c), t: Box::new(t), e: Box::new(e) })]
pub fn ite(c: Expr, t: Expr, e: Expr) -> Self {
Self::IfThenElse { c: Box::new(c), t: Box::new(t), e: Box::new(e) }
}
pub fn variable(v: usize) -> Self {
Self::Var { v }
}
#[requires(self.is_normalized())]
#[requires(a.is_normalized())]
#[requires(b.is_normalized())]
#[ensures(result.is_normalized())]
#[variant(self)]
pub fn transpose(self, a: Self, b: Self) -> Self {
match self {
Self::IfThenElse { c, t, e } => Self::IfThenElse {
c,
t: Box::new(t.transpose(a.clone(), b.clone())),
e: Box::new(e.transpose(a, b)),
},
Self::Var { .. } => {
Self::IfThenElse { c: Box::new(self), t: Box::new(a), e: Box::new(b) }
}
Self::True => a,
Self::False => b,
}
}
#[predicate]
fn is_normalized(self) -> bool {
match self {
Expr::IfThenElse { c, t, e } => {
c.is_normalized()
&& t.is_normalized()
&& e.is_normalized()
&& match *c {
Expr::IfThenElse { .. } => false,
_ => true,
}
}
Expr::Var { .. } => true,
Expr::True => true,
Expr::False => true,
}
}
#[ensures(result.is_normalized())]
#[variant(self)]
pub fn normalize(&self) -> Self {
match self {
Expr::IfThenElse { c, t, e } => {
let cp = c.normalize();
let tp = t.normalize();
let ep = e.normalize();
cp.transpose(tp, ep)
}
e => e.clone(),
}
}
#[predicate]
fn is_simplified(self) -> bool {
match self {
Expr::IfThenElse { c, t, e } => match *c {
Expr::Var { v } => t.does_not_contain(v) && e.does_not_contain(v),
c => c.is_simplified() && t.is_simplified() && e.is_simplified(),
},
_ => true,
}
}
#[predicate]
fn does_not_contain(self, vp: usize) -> bool {
match self {
Expr::IfThenElse { c, t, e } => {
c.does_not_contain(vp) && t.does_not_contain(vp) && e.does_not_contain(vp)
}
Expr::Var { v } => v != vp,
_ => true,
}
}
#[requires(self.is_normalized())]
#[ensures(result.is_simplified())]
pub fn simplify(self) -> Self {
self.simplify_helper(BTreeMap::new())
}
#[requires(self.is_normalized())]
#[ensures(forall<i: usize> (exists<v: bool> [email protected](i@) == Some(v)) ==> result.does_not_contain(i))]
#[ensures(result.is_simplified())]
#[variant(self)]
fn simplify_helper(self, state: BTreeMap<usize, bool>) -> Self {
match self {
Expr::IfThenElse { c, t, e } => {
match *c {
Expr::Var { v } => {
if let Some(b) = state.get(&v) {
if *b {
t.simplify_helper(state)
} else {
e.simplify_helper(state)
}
} else {
// Then
let mut state_t = state.clone();
state_t.insert(v, true);
let tp = t.simplify_helper(state_t);
// Else
let mut state_e = state.clone();
state_e.insert(v, false);
let ep = e.simplify_helper(state_e);
// Composite
Self::IfThenElse { c, t: Box::new(tp), e: Box::new(ep) }
}
}
c => c.simplify_helper(state),
}
}
Expr::Var { v } => {
if let Some(b) = state.get(&v) {
if *b {
Self::True
} else {
Self::False
}
} else {
Expr::Var { v }
}
}
c => c,
}
}
}