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 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
use core::marker;
#[allow(unused_imports)]
use super::pervasive::*;
#[allow(unused_imports)]
use super::prelude::*;
verus! {
/// `Seq<A>` is a sequence type for specifications.
/// To use a "sequence" in compiled code, use an `exec` type like `vec::Vec`
/// that has `Seq<A>` as its specification type.
///
/// An object `seq: Seq<A>` has a length, given by [`seq.len()`](Seq::len),
/// and a value at each `i` for `0 <= i < seq.len()`, given by [`seq[i]`](Seq::index).
///
/// Sequences can be constructed in a few different ways:
/// * [`Seq::empty`] construct an empty sequence (`len() == 0`)
/// * [`Seq::new`] construct a sequence of a given length, initialized according
/// to a given function mapping indices `i` to values `A`.
/// * The [`seq!`] macro, to construct small sequences of a fixed size (analagous to the
/// [`std::vec!`] macro).
/// * By manipulating an existing sequence with [`Seq::push`], [`Seq::update`],
/// or [`Seq::add`].
///
/// To prove that two sequences are equal, it is usually easiest to use the
/// extensional equality operator `=~=`.
#[verifier::external_body]
#[verifier::ext_equal]
#[verifier::accept_recursive_types(A)]
pub struct Seq<A> {
dummy: marker::PhantomData<A>,
}
impl<A> Seq<A> {
/// An empty sequence (i.e., a sequence of length 0).
#[rustc_diagnostic_item = "verus::vstd::seq::Seq::empty"]
pub spec fn empty() -> Seq<A>;
/// Construct a sequence `s` of length `len` where entry `s[i]` is given by `f(i)`.
#[rustc_diagnostic_item = "verus::vstd::seq::Seq::new"]
pub spec fn new(len: nat, f: impl Fn(int) -> A) -> Seq<A>;
/// The length of a sequence.
#[rustc_diagnostic_item = "verus::vstd::seq::Seq::len"]
pub spec fn len(self) -> nat;
/// Gets the value at the given index `i`.
///
/// If `i` is not in the range `[0, self.len())`, then the resulting value
/// is meaningless and arbitrary.
#[rustc_diagnostic_item = "verus::vstd::seq::Seq::index"]
pub spec fn index(self, i: int) -> A
recommends
0 <= i < self.len(),
;
/// `[]` operator, synonymous with `index`
#[verifier::inline]
pub open spec fn spec_index(self, i: int) -> A
recommends
0 <= i < self.len(),
{
self.index(i)
}
/// Appends the value `a` to the end of the sequence.
/// This always increases the length of the sequence by 1.
/// This often requires annotating the type of the element literal in the sequence,
/// e.g., `10int`.
///
/// ## Example
///
/// ```rust
/// proof fn push_test() {
/// assert(seq![10int, 11, 12].push(13) =~= seq![10, 11, 12, 13]);
/// }
/// ```
#[rustc_diagnostic_item = "verus::vstd::seq::Seq::push"]
pub spec fn push(self, a: A) -> Seq<A>;
/// Updates the sequence at the given index, replacing the element with the given
/// value, and leaves all other entries unchanged.
///
/// ## Example
///
/// ```rust
/// proof fn update_test() {
/// let s = seq![10, 11, 12, 13, 14];
/// let t = s.update(2, -5);
/// assert(t =~= seq![10, 11, -5, 13, 14]);
/// }
/// ```
#[rustc_diagnostic_item = "verus::vstd::seq::Seq::update"]
pub spec fn update(self, i: int, a: A) -> Seq<A>
recommends
0 <= i < self.len(),
;
/// DEPRECATED: use =~= or =~~= instead.
/// Returns `true` if the two sequences are pointwise equal, i.e.,
/// they have the same length and the corresponding values are equal
/// at each index. This is equivalent to the sequences being actually equal
/// by [`axiom_seq_ext_equal`].
///
/// To prove that two sequences are equal via extensionality, it may be easier
/// to use the general-purpose `=~=` or `=~~=` or
/// to use the [`assert_seqs_equal!`](crate::seq_lib::assert_seqs_equal) macro,
/// rather than using `.ext_equal` directly.
#[cfg_attr(not(verus_verify_core), deprecated = "use =~= or =~~= instead")]
#[rustc_diagnostic_item = "verus::vstd::seq::Seq::ext_equal"]
pub open spec fn ext_equal(self, s2: Seq<A>) -> bool {
self =~= s2
}
/// Returns a sequence for the given subrange.
///
/// ## Example
///
/// ```rust
/// proof fn subrange_test() {
/// let s = seq![10int, 11, 12, 13, 14];
/// // ^-------^
/// // 0 1 2 3 4 5
/// let sub = s.subrange(2, 4);
/// assert(sub =~= seq![12, 13]);
/// }
/// ```
#[rustc_diagnostic_item = "verus::vstd::seq::Seq::subrange"]
pub spec fn subrange(self, start_inclusive: int, end_exclusive: int) -> Seq<A>
recommends
0 <= start_inclusive <= end_exclusive <= self.len(),
;
/// Returns a sequence containing only the first n elements of the original sequence
#[verifier::inline]
pub open spec fn take(self, n: int) -> Seq<A> {
self.subrange(0, n)
}
/// Returns a sequence without the first n elements of the original sequence
#[verifier::inline]
pub open spec fn skip(self, n: int) -> Seq<A> {
self.subrange(n, self.len() as int)
}
/// Concatenates the sequences.
///
/// ## Example
///
/// ```rust
/// proof fn add_test() {
/// assert(seq![10int, 11].add(seq![12, 13, 14])
/// =~= seq![10, 11, 12, 13, 14]);
/// }
/// ```
#[rustc_diagnostic_item = "verus::vstd::seq::Seq::add"]
pub spec fn add(self, rhs: Seq<A>) -> Seq<A>;
/// `+` operator, synonymous with `add`
#[verifier::inline]
pub open spec fn spec_add(self, rhs: Seq<A>) -> Seq<A> {
self.add(rhs)
}
/// Returns the last element of the sequence.
#[rustc_diagnostic_item = "verus::vstd::seq::Seq::last"]
pub open spec fn last(self) -> A
recommends
0 < self.len(),
{
self[self.len() as int - 1]
}
/// Returns the first element of the sequence.
#[rustc_diagnostic_item = "vstd::seq::Seq::first"]
pub open spec fn first(self) -> A
recommends
0 < self.len(),
{
self[0]
}
}
// Trusted axioms
pub broadcast proof fn axiom_seq_index_decreases<A>(s: Seq<A>, i: int)
requires
0 <= i < s.len(),
ensures
#[trigger] (decreases_to!(s => s[i])),
{
admit();
}
pub proof fn axiom_seq_len_decreases<A>(s1: Seq<A>, s2: Seq<A>)
requires
s2.len() < s1.len(),
forall|i2: int|
0 <= i2 < s2.len() && #[trigger] trigger(s2[i2]) ==> exists|i1: int|
0 <= i1 < s1.len() && s1[i1] == s2[i2],
ensures
decreases_to!(s1 => s2),
{
admit();
}
pub broadcast proof fn axiom_seq_subrange_decreases<A>(s: Seq<A>, i: int, j: int)
requires
0 <= i <= j <= s.len(),
s.subrange(i, j).len() < s.len(),
ensures
#[trigger] (decreases_to!(s => s.subrange(i, j))),
{
broadcast use axiom_seq_subrange_len, axiom_seq_subrange_index;
let s2 = s.subrange(i, j);
assert forall|i2: int| 0 <= i2 < s2.len() && #[trigger] trigger(s2[i2]) implies exists|i1: int|
0 <= i1 < s.len() && s[i1] == s2[i2] by {
assert(s[i + i2] == s2[i2]);
}
axiom_seq_len_decreases(s, s2);
}
pub broadcast proof fn axiom_seq_empty<A>()
ensures
#[trigger] Seq::<A>::empty().len() == 0,
{
admit();
}
pub broadcast proof fn axiom_seq_new_len<A>(len: nat, f: spec_fn(int) -> A)
ensures
#[trigger] Seq::new(len, f).len() == len,
{
admit();
}
pub broadcast proof fn axiom_seq_new_index<A>(len: nat, f: spec_fn(int) -> A, i: int)
requires
0 <= i < len,
ensures
#[trigger] Seq::new(len, f)[i] == f(i),
{
admit();
}
pub broadcast proof fn axiom_seq_push_len<A>(s: Seq<A>, a: A)
ensures
#[trigger] s.push(a).len() == s.len() + 1,
{
admit();
}
pub broadcast proof fn axiom_seq_push_index_same<A>(s: Seq<A>, a: A, i: int)
requires
i == s.len(),
ensures
#[trigger] s.push(a)[i] == a,
{
admit();
}
pub broadcast proof fn axiom_seq_push_index_different<A>(s: Seq<A>, a: A, i: int)
requires
0 <= i < s.len(),
ensures
#[trigger] s.push(a)[i] == s[i],
{
admit();
}
pub broadcast proof fn axiom_seq_update_len<A>(s: Seq<A>, i: int, a: A)
requires
0 <= i < s.len(),
ensures
#[trigger] s.update(i, a).len() == s.len(),
{
admit();
}
pub broadcast proof fn axiom_seq_update_same<A>(s: Seq<A>, i: int, a: A)
requires
0 <= i < s.len(),
ensures
#[trigger] s.update(i, a)[i] == a,
{
admit();
}
pub broadcast proof fn axiom_seq_update_different<A>(s: Seq<A>, i1: int, i2: int, a: A)
requires
0 <= i1 < s.len(),
0 <= i2 < s.len(),
i1 != i2,
ensures
#[trigger] s.update(i2, a)[i1] == s[i1],
{
admit();
}
pub broadcast proof fn axiom_seq_ext_equal<A>(s1: Seq<A>, s2: Seq<A>)
ensures
#[trigger] (s1 =~= s2) <==> {
&&& s1.len() == s2.len()
&&& forall|i: int| 0 <= i < s1.len() ==> s1[i] == s2[i]
},
{
admit();
}
pub broadcast proof fn axiom_seq_ext_equal_deep<A>(s1: Seq<A>, s2: Seq<A>)
ensures
#[trigger] (s1 =~~= s2) <==> {
&&& s1.len() == s2.len()
&&& forall|i: int| 0 <= i < s1.len() ==> s1[i] =~~= s2[i]
},
{
admit();
}
pub broadcast proof fn axiom_seq_subrange_len<A>(s: Seq<A>, j: int, k: int)
requires
0 <= j <= k <= s.len(),
ensures
#[trigger] s.subrange(j, k).len() == k - j,
{
admit();
}
pub broadcast proof fn axiom_seq_subrange_index<A>(s: Seq<A>, j: int, k: int, i: int)
requires
0 <= j <= k <= s.len(),
0 <= i < k - j,
ensures
#[trigger] s.subrange(j, k)[i] == s[i + j],
{
admit();
}
pub broadcast proof fn axiom_seq_add_len<A>(s1: Seq<A>, s2: Seq<A>)
ensures
#[trigger] s1.add(s2).len() == s1.len() + s2.len(),
{
admit();
}
pub broadcast proof fn axiom_seq_add_index1<A>(s1: Seq<A>, s2: Seq<A>, i: int)
requires
0 <= i < s1.len(),
ensures
#[trigger] s1.add(s2)[i] == s1[i],
{
admit();
}
pub broadcast proof fn axiom_seq_add_index2<A>(s1: Seq<A>, s2: Seq<A>, i: int)
requires
s1.len() <= i < s1.len() + s2.len(),
ensures
#[trigger] s1.add(s2)[i] == s2[i - s1.len()],
{
admit();
}
pub broadcast group group_seq_axioms {
axiom_seq_index_decreases,
axiom_seq_subrange_decreases,
axiom_seq_empty,
axiom_seq_new_len,
axiom_seq_new_index,
axiom_seq_push_len,
axiom_seq_push_index_same,
axiom_seq_push_index_different,
axiom_seq_update_len,
axiom_seq_update_same,
axiom_seq_update_different,
axiom_seq_ext_equal,
axiom_seq_ext_equal_deep,
axiom_seq_subrange_len,
axiom_seq_subrange_index,
axiom_seq_add_len,
axiom_seq_add_index1,
axiom_seq_add_index2,
}
// ------------- Macros ---------------- //
#[doc(hidden)]
#[macro_export]
macro_rules! seq_internal {
[] => {
$crate::vstd::seq::Seq::empty()
};
[$elem:expr] => {
$crate::vstd::seq::Seq::empty()
.push($elem)
};
[$($elem:expr),* $(,)?] => {
<_ as $crate::vstd::view::View>::view(&[$($elem),*])
};
[$elem:expr; $n:expr] => {
$crate::vstd::seq::Seq::new(
$n,
$crate::vstd::prelude::closure_to_fn_spec(
|_x: _| $elem
),
)
};
}
/// Creates a [`Seq`] containing the given elements.
///
/// ## Example
///
/// ```rust
/// let s = seq![11int, 12, 13];
///
/// assert(s.len() == 3);
/// assert(s[0] == 11);
/// assert(s[1] == 12);
/// assert(s[2] == 13);
/// ```
#[macro_export]
macro_rules! seq {
[$($tail:tt)*] => {
::builtin_macros::verus_proof_macro_exprs!($crate::vstd::seq::seq_internal!($($tail)*))
};
}
#[doc(hidden)]
pub use seq_internal;
pub use seq;
} // verus!