Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions src/two_k_11/challenge1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,44 @@ use vstd::prelude::*;

verus! {

//datastructures
// TO-DO: why do we specify these? For Tree-like problems?
// [i32]
//enddatastructures

//preconditions
// Encodes: A non-empty integer array a.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we have descriptions like these to show which preconditions link to what parts of the natural-language problem (or an assumption we infer is necessary to the solution)?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good Point! We should allow one line docstring to explain the assumption.

pub fn precondition1(a: &[i32]) -> bool {
0 < a.len() <= usize::MAX
}
//endpreconditions

//postconditions
// TO-DO: Wonder how we'll make the signatures compatible.
// Encodes: index re-turned by the method max() points to an element maximal in the array.
pub fn postcondition1(a: &[i32], max_idx: usize) {
forall|i: int| 0 <= i < a.len() ==> a[max_idx as int] >= a[i]
}
//endpostconditions

//specs
spec pub fn preconditions(a: &[i32]) -> bool {
precondtion1(a)
}

spec pub fn postconditions(a: &[i32], max_idx: usize) {
postcondition1(a, max_idx)
}
//endspecs

#[allow(unused)]
//signature
pub fn max(a: &[i32]) -> (max_idx: usize)
//endsignature
requires
0 < a.len() <= usize::MAX,
preconditions(a),
ensures
forall|i: int| 0 <= i < a.len() ==> a[max_idx as int] >= a[i],
postconditions(a, max_idx),
{
let mut x = 0usize;
let mut y = a.len() - 1;
Expand Down