Skip to main content

vstd_extra/external/
iter.rs

1// SPDX-License-Identifier: MPL-2.0
2//! Specifications for iterator operations not yet modeled by `vstd`.
3use core::cmp::Ordering;
4use vstd::{
5    laws_cmp::obeys_cmp,
6    prelude::*,
7    std_specs::{cmp::OrdSpec, iter::IteratorSpec},
8};
9
10verus! {
11
12/// Consumes the iterator, returning its first minimum or `None` if empty.
13///
14/// Audited against Rust 1.100.0 (0ef1bc386), `library/core/src/iter/traits/iterator.rs`:
15/// `min` calls `reduce(Ord::min)` and retains the first equal minimum for lawful `Ord`.
16/// See <https://doc.rust-lang.org/1.100.0/core/iter/trait.Iterator.html#method.min>.
17/// TODO: Upstream this model; remove it when equivalent vstd support is available.
18#[verifier::external_trait_specification]
19pub trait ExIteratorMin {
20    type ExternalTraitSpecificationFor: Iterator;
21
22    type Item;
23
24    fn min(self) -> (ret: Option<Self::Item>) where Self: Sized, Self::Item: Ord
25        ensures
26            iterator_min_post(self, ret),
27    ;
28}
29
30/// The postcondition is indirect so the external trait need not depend on its extension.
31pub uninterp spec fn iterator_min_post<I, A>(iter: I, ret: Option<A>) -> bool;
32
33/// Connects `Iterator::min` to the prophetic iterator model on successful return.
34/// Like vstd's provided-method specs in `std_specs/iter.rs`, this model trusts
35/// `obeys_prophetic_iter_laws()` to include consistency of consuming methods with
36/// `next`. Constraining the underlying implementations remains a vstd TODO.
37/// Membership and ordering guarantees additionally require vstd's comparison laws.
38pub broadcast axiom fn axiom_iterator_min_postcondition<
39    A: Ord,
40    I: Iterator<Item = A> + IteratorSpec,
41>(iter: I, ret: Option<A>)
42    requires
43        iter.obeys_prophetic_iter_laws(),
44        #[trigger] iterator_min_post(iter, ret),
45    ensures
46        iter.produces().all(|p: bool| p),
47        ret is None <==> iter.remaining().len() == 0,
48        obeys_cmp::<A>() ==> (ret matches Some(least) ==> {
49            &&& iter.remaining().contains(least)
50            &&& forall|k: int|
51                #![trigger iter.remaining()[k]]
52                0 <= k < iter.remaining().len() ==> least.cmp_spec(&iter.remaining()[k])
53                    != Ordering::Greater
54            &&& exists|j: int|
55                0 <= j < iter.remaining().len() && iter.remaining()[j] == least && forall|k: int|
56                    0 <= k < j ==> (#[trigger] iter.remaining()[k]).cmp_spec(&least)
57                        == Ordering::Greater
58        }),
59;
60
61/// Consumes the iterator, returning its last maximum or `None` if empty.
62///
63/// Audited against Rust 1.100.0 (0ef1bc386), `library/core/src/iter/traits/iterator.rs`:
64/// `max` calls `reduce(Ord::max)` and retains the last equal maximum for lawful `Ord`.
65/// See <https://doc.rust-lang.org/1.100.0/core/iter/trait.Iterator.html#method.max>.
66/// TODO: Upstream this model; remove it when equivalent vstd support is available.
67#[verifier::external_trait_specification]
68pub trait ExIteratorMax {
69    type ExternalTraitSpecificationFor: Iterator;
70
71    type Item;
72
73    fn max(self) -> (ret: Option<Self::Item>) where Self: Sized, Self::Item: Ord
74        ensures
75            iterator_max_post(self, ret),
76    ;
77}
78
79/// The postcondition is indirect so the external trait need not depend on its extension.
80pub uninterp spec fn iterator_max_post<I, A>(iter: I, ret: Option<A>) -> bool;
81
82/// Connects `Iterator::max` to the prophetic iterator model on successful return.
83/// Like vstd's provided-method specs in `std_specs/iter.rs`, this model trusts
84/// `obeys_prophetic_iter_laws()` to include consistency of consuming methods with
85/// `next`. Constraining the underlying implementations remains a vstd TODO.
86/// Membership and ordering guarantees additionally require vstd's comparison laws.
87pub broadcast axiom fn axiom_iterator_max_postcondition<
88    A: Ord,
89    I: Iterator<Item = A> + IteratorSpec,
90>(iter: I, ret: Option<A>)
91    requires
92        iter.obeys_prophetic_iter_laws(),
93        #[trigger] iterator_max_post(iter, ret),
94    ensures
95        iter.produces().all(|p: bool| p),
96        ret is None <==> iter.remaining().len() == 0,
97        obeys_cmp::<A>() ==> (ret matches Some(greatest) ==> {
98            &&& iter.remaining().contains(greatest)
99            &&& forall|k: int|
100                #![trigger iter.remaining()[k]]
101                0 <= k < iter.remaining().len() ==> greatest.cmp_spec(&iter.remaining()[k])
102                    != Ordering::Less
103            &&& exists|j: int|
104                0 <= j < iter.remaining().len() && iter.remaining()[j] == greatest && forall|k: int|
105
106                    j < k < iter.remaining().len() ==> (#[trigger] iter.remaining()[k]).cmp_spec(
107                        &greatest,
108                    ) == Ordering::Less
109        }),
110;
111
112/// Verus proxy for the standard library's array `IntoIter`.
113#[verifier::external_type_specification]
114#[verifier::external_body]
115#[verifier::reject_recursive_types(T)]
116pub struct ExArrayIntoIter<T, const N: usize>(core::array::IntoIter<T, N>);
117
118/// The array iterator yields the array view from left to right and terminates.
119/// See [`array::into_iter`](https://doc.rust-lang.org/std/primitive.array.html#method.into_iter).
120pub assume_specification<T, const N: usize>[ <[T; N] as IntoIterator>::into_iter ](
121    array: [T; N],
122) -> (iter: <[T; N] as IntoIterator>::IntoIter)
123    ensures
124        IteratorSpec::obeys_prophetic_iter_laws(&iter),
125        IteratorSpec::produces(&iter) == Seq::new(N as nat, |i: int| true),
126        IteratorSpec::remaining(&iter) == array@,
127        IteratorSpec::decrease(&iter) == Some(N as nat),
128;
129
130} // verus!