Skip to main content

vstd/
laws_cmp.rs

1#![feature(rustc_attrs)]
2#![allow(unused_imports)]
3
4use super::laws_eq::*;
5use super::prelude::*;
6use super::std_specs::cmp::{OrdSpec, PartialEqSpec, PartialOrdSpec};
7#[cfg(feature = "alloc")]
8use alloc::{boxed::Box, rc::Rc, sync::Arc};
9#[cfg(feature = "alloc")]
10use core::alloc::Allocator;
11use core::cmp::Ordering;
12
13verus! {
14
15#[verifier::opaque]
16pub open spec fn obeys_partial_cmp_spec_properties<T: PartialOrd>() -> bool {
17    &&& forall|x: T, y: T| #[trigger]
18        x.partial_cmp_spec(&y) == Some(Ordering::Equal) <==> x.eq_spec(&y)
19    &&& forall|x: T, y: T| #[trigger]
20        x.partial_cmp_spec(&y) == Some(Ordering::Less) <==> y.partial_cmp_spec(&x) == Some(
21            Ordering::Greater,
22        )
23    &&& forall|x: T, y: T, z: T|
24        x.partial_cmp_spec(&y) == Some(Ordering::Less) && #[trigger] y.partial_cmp_spec(&z) == Some(
25            Ordering::Less,
26        ) ==> #[trigger] x.partial_cmp_spec(&z) == Some(Ordering::Less)
27    &&& forall|x: T, y: T, z: T|
28        x.partial_cmp_spec(&y) == Some(Ordering::Greater) && #[trigger] y.partial_cmp_spec(&z)
29            == Some(Ordering::Greater) ==> #[trigger] x.partial_cmp_spec(&z) == Some(
30            Ordering::Greater,
31        )
32    &&& obeys_eq_spec_properties::<T>()
33}
34
35#[verifier::opaque]
36pub open spec fn obeys_cmp_partial_ord<T: PartialOrd>() -> bool {
37    &&& T::obeys_eq_spec()
38    &&& T::obeys_partial_cmp_spec()
39    &&& forall|x: T, y: T| x.eq_spec(&y) <==> x.partial_cmp_spec(&y) == Some(Ordering::Equal)
40}
41
42#[verifier::opaque]
43pub open spec fn obeys_cmp_ord<T: Ord>() -> bool {
44    &&& T::obeys_cmp_spec()
45    &&& forall|x: T, y: T|
46        #![trigger x.partial_cmp_spec(&y)]
47        #![trigger x.cmp_spec(&y)]
48        x.partial_cmp_spec(&y) == Some(x.cmp_spec(&y))
49}
50
51pub open spec fn obeys_cmp<T: Ord>() -> bool {
52    &&& obeys_eq::<T>()
53    &&& obeys_cmp_partial_ord::<T>()
54    &&& obeys_cmp_ord::<T>()
55    &&& obeys_partial_cmp_spec_properties::<T>()
56}
57
58#[deprecated(note = "`laws_cmp::obeys_cmp_spec` has been renamed to `laws_cmp::obeys_cmp`")]
59#[verifier::inline]
60pub open spec fn obeys_cmp_spec<T: Ord>() -> bool {
61    obeys_cmp::<T>()
62}
63
64/// Whether the type's cmp spec matches structural equality, i.e.,
65/// whether `v1.cmp(&v2) is Equal <==> v1 == v2`.
66pub open spec fn obeys_cmp_concrete_eq<T: Ord>() -> bool {
67    forall|v1: T, v2: T| v1.cmp_spec(&v2) is Equal <==> v1 == v2
68}
69
70/// Whether the type's cmp spec matches deep-view equality, i.e.,
71/// whether `v1.cmp(&v2) is Equal <==> v1.deep_view() == v2.deep_view()`.
72pub open spec fn obeys_cmp_deep_eq<T: Ord + DeepView>() -> bool {
73    forall|v1: T, v2: T| v1.cmp_spec(&v2) is Equal <==> v1.deep_view() == v2.deep_view()
74}
75
76macro_rules! num_laws_cmp {
77    ($n: ty, $modname:ident) => {
78        verus! {
79        mod $modname {
80            use super::*;
81
82            pub broadcast proof fn lemma_obeys_cmp_spec()
83                ensures
84                    #[trigger] obeys_cmp::<$n>(),
85            {
86                broadcast use group_laws_eq;
87                reveal(obeys_eq_spec_properties);
88                reveal(obeys_partial_cmp_spec_properties);
89                reveal(obeys_cmp_partial_ord);
90                reveal(obeys_cmp_ord);
91                assert(obeys_eq::<$n>());
92            }
93        }
94        }
95    }
96}
97
98num_laws_cmp!(u8, u8_laws);
99
100num_laws_cmp!(i8, i8_laws);
101
102num_laws_cmp!(u16, u16_laws);
103
104num_laws_cmp!(i16, i16_laws);
105
106num_laws_cmp!(u32, u32_laws);
107
108num_laws_cmp!(i32, i32_laws);
109
110num_laws_cmp!(u64, u64_laws);
111
112num_laws_cmp!(i64, i64_laws);
113
114num_laws_cmp!(u128, u128_laws);
115
116num_laws_cmp!(i128, i128_laws);
117
118num_laws_cmp!(usize, usize_laws);
119
120num_laws_cmp!(isize, isize_laws);
121
122macro_rules! tuple_cmp_impl {
123    ($modname:ident, $($T:ident )+) => {
124        verus! {
125        mod $modname {
126            use super::*;
127
128            pub broadcast proof fn lemma_obeys_cmp_spec<$($T,)+>()
129                where
130                    $($T: Ord + PartialEq,)+
131                requires
132                    $(obeys_cmp::<$T>(),)+
133                ensures
134                    #[trigger] obeys_cmp::<($($T,)+)>(),
135            {
136                broadcast use group_laws_eq;
137                reveal(obeys_eq_spec_properties);
138                reveal(obeys_partial_cmp_spec_properties);
139                reveal(obeys_cmp_partial_ord);
140                reveal(obeys_cmp_ord);
141                assert(obeys_cmp::<($($T,)+)>());
142            }
143        }
144        }
145    }
146}
147
148tuple_cmp_impl!(tuple_1_laws, T);
149
150tuple_cmp_impl!(tuple_2_laws, U T);
151
152proof fn lemma_isomorphic_obeys_cmp<T: PartialEq + Ord, U: PartialEq + Ord>(f: spec_fn(U) -> T)
153    requires
154        obeys_cmp::<T>(),
155        obeys_eq::<U>(),
156        U::obeys_partial_cmp_spec(),
157        U::obeys_cmp_spec(),
158        forall|x: U, y: U| x.eq_spec(&y) == f(x).eq_spec(&f(y)),
159        forall|x: U, y: U| x.partial_cmp_spec(&y) == f(x).partial_cmp_spec(&f(y)),
160        forall|x: U, y: U| x.cmp_spec(&y) == f(x).cmp_spec(&f(y)),
161    ensures
162        obeys_cmp::<U>(),
163{
164    reveal(obeys_eq_spec_properties);
165    reveal(obeys_partial_cmp_spec_properties);
166    reveal(obeys_cmp_partial_ord);
167    reveal(obeys_cmp_ord);
168}
169
170proof fn lemma_obeys_cmp_obeys_traits<T: Ord>()
171    requires
172        obeys_cmp::<T>(),
173    ensures
174        T::obeys_eq_spec(),
175        T::obeys_partial_cmp_spec(),
176        T::obeys_cmp_spec(),
177{
178    reveal(obeys_cmp_partial_ord);
179    reveal(obeys_cmp_ord);
180}
181
182macro_rules! tuple_cmp_induct_impl {
183    ($modname:ident, $inductmod:ident, 0 $U:ident, $($idx:tt $T:ident, )+) => {
184        verus! {
185        mod $modname {
186            use super::*;
187
188            pub broadcast proof fn lemma_obeys_cmp_spec<$U, $($T,)+>()
189                where
190                    $U: Ord + PartialEq,
191                    $($T: Ord + PartialEq,)+
192                requires
193                    obeys_cmp::<$U>(),
194                    $(obeys_cmp::<$T>(),)+
195                ensures
196                    #[trigger] obeys_cmp::<($U, $($T,)+)>(),
197            {
198                broadcast use super::tuple_2_laws::lemma_obeys_cmp_spec;
199                broadcast use super::$inductmod::lemma_obeys_cmp_spec;
200                broadcast use group_laws_eq;
201                lemma_obeys_cmp_obeys_traits::<$U>();
202                $(lemma_obeys_cmp_obeys_traits::<$T>();)+
203                lemma_isomorphic_obeys_cmp(|x: ($U, $($T,)+)| (x.0, ($(x.$idx,)+)));
204                assert(obeys_cmp::<($U, $($T,)+)>());
205            }
206        }
207        }
208    };
209}
210
211tuple_cmp_induct_impl!(tuple_3_laws, tuple_2_laws, 0 V, 1 U, 2 T,);
212
213tuple_cmp_induct_impl!(tuple_4_laws, tuple_3_laws, 0 X, 1 W, 2 V, 3 U, );
214
215tuple_cmp_induct_impl!(tuple_5_laws, tuple_4_laws, 0 X, 1 W, 2 V, 3 U, 4 T, );
216
217tuple_cmp_induct_impl!(tuple_6_laws, tuple_5_laws, 0 Y, 1 X, 2 W, 3 V, 4 U, 5 T, );
218
219tuple_cmp_induct_impl!(tuple_7_laws, tuple_6_laws, 0 Z, 1 Y, 2 X, 3 W, 4 V, 5 U, 6 T, );
220
221tuple_cmp_induct_impl!(tuple_8_laws, tuple_7_laws, 0 A, 1 Z, 2 Y, 3 X, 4 W, 5 V, 6 U, 7 T, );
222
223tuple_cmp_induct_impl!(tuple_9_laws, tuple_8_laws, 0 B, 1 A, 2 Z, 3 Y, 4 X, 5 W, 6 V, 7 U, 8 T, );
224
225tuple_cmp_induct_impl!(tuple_10_laws, tuple_9_laws, 0 C, 1 B, 2 A, 3 Z, 4 Y, 5 X, 6 W, 7 V, 8 U, 9 T, );
226
227tuple_cmp_induct_impl!(tuple_11_laws, tuple_10_laws, 0 D, 1 C, 2 B, 3 A, 4 Z, 5 Y, 6 X, 7 W, 8 V, 9 U, 10 T, );
228
229tuple_cmp_induct_impl!(tuple_12_laws, tuple_11_laws, 0 E, 1 D, 2 C, 3 B, 4 A, 5 Z, 6 Y, 7 X, 8 W, 9 V, 10 U, 11 T, );
230
231pub broadcast proof fn lemma_ref_obeys_cmp_spec<T: Ord>()
232    requires
233        obeys_cmp::<T>(),
234    ensures
235        #[trigger] obeys_cmp::<&T>(),
236{
237    broadcast use lemma_ref_obeys_eq_spec;
238
239    assert(obeys_eq::<&T>());
240
241    assert(obeys_cmp_partial_ord::<&T>() && obeys_cmp_ord::<&T>()) by {
242        reveal(obeys_cmp_partial_ord);
243        reveal(obeys_cmp_ord);
244    }
245
246    assert(obeys_partial_cmp_spec_properties::<&T>()) by {
247        reveal(obeys_cmp_partial_ord);
248        reveal(obeys_partial_cmp_spec_properties);
249    }
250}
251
252pub broadcast proof fn lemma_option_obeys_cmp_spec<T: Ord>()
253    requires
254        obeys_cmp::<T>(),
255    ensures
256        #[trigger] obeys_cmp::<Option<T>>(),
257{
258    broadcast use lemma_option_obeys_eq_spec;
259
260    assert(obeys_eq::<Option<T>>());
261
262    assert(obeys_cmp_partial_ord::<Option<T>>() && obeys_cmp_ord::<Option<T>>()) by {
263        reveal(obeys_cmp_partial_ord);
264        reveal(obeys_cmp_ord);
265    }
266
267    assert(obeys_partial_cmp_spec_properties::<Option<T>>()) by {
268        reveal(obeys_cmp_partial_ord);
269        reveal(obeys_partial_cmp_spec_properties);
270    }
271}
272
273/// `obeys_cmp` makes `eq_spec` reflexive: `x.partial_cmp_spec(&x)` is
274/// `Some(x.cmp_spec(&x))`, and `Less` or `Greater` would contradict antisymmetry
275/// with `y = x`.
276proof fn lemma_obeys_cmp_eq_spec_reflexive<T: Ord>()
277    requires
278        obeys_cmp::<T>(),
279    ensures
280        forall|x: T| #[trigger] x.eq_spec(&x),
281{
282    reveal(obeys_cmp_ord);
283    reveal(obeys_partial_cmp_spec_properties);
284    assert forall|x: T| #[trigger] x.eq_spec(&x) by {
285        assert(x.partial_cmp_spec(&x) == Some(x.cmp_spec(&x)));
286    }
287}
288
289/* Box, Rc, Arc */
290
291#[cfg(feature = "alloc")]
292macro_rules! smart_ptr_laws_cmp {
293    ($modname:ident, $ptr:ident) => {
294        verus! {
295        mod $modname {
296            use super::*;
297
298            pub broadcast proof fn lemma_obeys_cmp_spec<T: Ord, A: Allocator>()
299                requires
300                    obeys_cmp::<T>(),
301                ensures
302                    #[trigger] obeys_cmp::<$ptr<T, A>>(),
303            {
304                broadcast use group_laws_eq;
305                // Rc and Arc's eq laws need reflexivity.
306                lemma_obeys_cmp_eq_spec_reflexive::<T>();
307                assert(obeys_eq::<$ptr<T, A>>());
308
309                assert(obeys_cmp_partial_ord::<$ptr<T, A>>() && obeys_cmp_ord::<$ptr<T, A>>()) by {
310                    reveal(obeys_cmp_partial_ord);
311                    reveal(obeys_cmp_ord);
312                }
313
314                assert(obeys_partial_cmp_spec_properties::<$ptr<T, A>>()) by {
315                    reveal(obeys_cmp_partial_ord);
316                    reveal(obeys_partial_cmp_spec_properties);
317                }
318            }
319        }
320        }
321    };
322}
323
324#[cfg(feature = "alloc")]
325smart_ptr_laws_cmp!(box_laws, Box);
326
327#[cfg(feature = "alloc")]
328smart_ptr_laws_cmp!(rc_laws, Rc);
329
330#[cfg(feature = "alloc")]
331smart_ptr_laws_cmp!(arc_laws, Arc);
332
333pub broadcast group group_laws_cmp {
334    u8_laws::lemma_obeys_cmp_spec,
335    i8_laws::lemma_obeys_cmp_spec,
336    u16_laws::lemma_obeys_cmp_spec,
337    i16_laws::lemma_obeys_cmp_spec,
338    u32_laws::lemma_obeys_cmp_spec,
339    i32_laws::lemma_obeys_cmp_spec,
340    u64_laws::lemma_obeys_cmp_spec,
341    i64_laws::lemma_obeys_cmp_spec,
342    u128_laws::lemma_obeys_cmp_spec,
343    i128_laws::lemma_obeys_cmp_spec,
344    usize_laws::lemma_obeys_cmp_spec,
345    isize_laws::lemma_obeys_cmp_spec,
346    lemma_ref_obeys_cmp_spec,
347    lemma_option_obeys_cmp_spec,
348    tuple_1_laws::lemma_obeys_cmp_spec,
349    tuple_2_laws::lemma_obeys_cmp_spec,
350    tuple_3_laws::lemma_obeys_cmp_spec,
351    tuple_4_laws::lemma_obeys_cmp_spec,
352    tuple_5_laws::lemma_obeys_cmp_spec,
353    tuple_6_laws::lemma_obeys_cmp_spec,
354    tuple_7_laws::lemma_obeys_cmp_spec,
355    tuple_8_laws::lemma_obeys_cmp_spec,
356    tuple_9_laws::lemma_obeys_cmp_spec,
357    tuple_10_laws::lemma_obeys_cmp_spec,
358    tuple_11_laws::lemma_obeys_cmp_spec,
359    tuple_12_laws::lemma_obeys_cmp_spec,
360    #[cfg(feature = "alloc")]
361    box_laws::lemma_obeys_cmp_spec,
362    #[cfg(feature = "alloc")]
363    rc_laws::lemma_obeys_cmp_spec,
364    #[cfg(feature = "alloc")]
365    arc_laws::lemma_obeys_cmp_spec,
366}
367
368} // verus!