Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update exercises #1024

Merged
merged 1 commit into from
Jun 21, 2024
Merged

Update exercises #1024

merged 1 commit into from
Jun 21, 2024

Conversation

nhusung
Copy link
Contributor

@nhusung nhusung commented Jun 21, 2024

Before turning to a larger project, I thought it might be better to solve a few small exercises. Unfortunately, two of the provided ones have little bugs such that the compilation/translation to Coma already fails. I am not confident about the DeepModel trait bound (still learning Creusot …) but it seemed to be required.

@@ -4,11 +4,11 @@ use creusot_contracts::*;
// Prove the following:
// 1. If we return Some(i) it is the first index containing `tgt`
// 2. If we return None, then there are no indices containing `tgt`
fn search<T: Ord>(v: &Vec<T>, tgt: &T) -> Option<usize> {
fn search<T: Ord + DeepModel>(v: &[T], tgt: &T) -> Option<usize> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Are you saying compilation/translation to Coma fails without this? Or did you have to add this to prove whichever spec you wanted to prove?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, this happened after adding the specification.

use creusot_contracts::*;

#[ensures(forall<i: usize> result == Some(i) ==> i@ < v@.len() && forall<j: Int> 0 <= j && j < i@ ==> v@[j] != *tgt)]
#[ensures(result == None ==> forall<i: Int> 0 <= i && i < v@.len() ==> v@[i] != *tgt)]
pub fn search<T: Ord>(v: &[T], tgt: &T) -> Option<usize> {
    let mut i = 0;

    #[invariant(forall<j: Int> 0 <= j && j < i@ ==> v@[j] != *tgt)]
    while i < v.len() {
        if v[i] == *tgt {
            return Some(i);
        }

        i += 1
    }

    return None;
}

For the code above, I get the following error message:

error[E0277]: the trait bound `T: creusot_contracts::DeepModel` is not satisfied
  --> src/lib.rs:10:12
   |
10 |         if v[i] == *tgt {
   |            ^^^^^^^^^^^^ the trait `creusot_contracts::DeepModel` is not implemented for `T`

I can also use tgt@ instead of *tgt in the specifications, but then I get an additional message about ShallowModel not being implemented by T.

I’m on commit 917bcee1, if this is of interest.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Indeed, you need to add this bound. I think my original intent was to have that be part of the exercise, but its probably best to provide that for the user.

Copy link
Contributor Author

@nhusung nhusung Jun 21, 2024

Choose a reason for hiding this comment

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

In fact, I started to wonder whether adding the DeepModel trait bound was intended to be part of the exercise once I read @sarsko’s comment 😅 I guess the actual issue is that DeepModel is not mentioned in the guide. Otherwise I would have felt more comfortable with the exercise. And if you have the page anyway, you could use #[diagnostic::on_unimplemented] and link the respective page, which would IMO be the best experience for newcomers 😉

@@ -6,9 +6,7 @@ use creusot_contracts::*;
pub fn all_zero(v: &mut Vec<u32>) {
let mut i = 0;
let old_v = snapshot! { v };
// Until https://gitlab.inria.fr/why3/why3/-/merge_requests/667 is merged
// the following invariant is needed to allow Why3 to remember prophecies dont change
#[invariant(proph_const, ^v == ^old_v.inner())]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Keep in mind, this is actually now needed again by the change to coma. At least until 0.2 comes out (soon ish).

Copy link
Contributor Author

@nhusung nhusung Jun 21, 2024

Choose a reason for hiding this comment

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

Interesting, so that’s why I needed #[invariant(^*old_v == ^v)]? The old syntax with proph_const didn’t seem to work …

error: unexpected token
   --> src/lib.rs:119:28
    |
119 |     #[invariant(proph_const, ^old_v.inner() == ^v)]
    |                            ^

error: internal error: Cannot fetch THIR body

@xldenis
Copy link
Collaborator

xldenis commented Jun 21, 2024

Hi Nils,

Thanks for the PR, I think we'll merge it but there are a few remarks / questions I have for you:

Unfortunately, two of the provided ones have little bugs such that the compilation/translation to Coma already fails.

Do you mean that Creusot panics? Returns an error? Or that it generates ill-formed Coma code?

More generally, I would strongly recommend sticking to v0.1, which is our first "stable" version and still uses MLCFG. The current Coma branch on master has some regressions compared to 0.1 though by 0.2 it will supersede it.

@nhusung
Copy link
Contributor Author

nhusung commented Jun 21, 2024

Do you mean that Creusot panics? Returns an error? Or that it generates ill-formed Coma code?

I just noticed ordinary error messages. So everything is fine 🙂

@xldenis xldenis merged commit 5b61435 into creusot-rs:master Jun 21, 2024
4 checks passed
@nhusung nhusung deleted the update-exercises branch June 25, 2024 05:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants