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

Add fibonacci example to the Guide to illustrate how spec, proof, and exec code work together #1354

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

parno
Copy link
Collaborator

@parno parno commented Nov 20, 2024

No description provided.

@parno
Copy link
Collaborator Author

parno commented Nov 20, 2024

When talking with some folks learning Verus, they said that Section 3 of the guide gave them some understanding of the mode system, but they were still confused about how you were supposed to use the different modes, and (despite the table in opening section), which modes call which other modes. I thought this might help with some of that confusion. I'm open to suggestions for other examples, however.

Comment on lines +404 to +412
if i < 2 && j < 2 {
} else if i == j {
} else if i == j - 1 {
assert(fib(j) == fib((j - 2) as nat) + fib((j - 1) as nat));
lemma_fib_is_monotonic(i, (j - 1) as nat);
} else {
lemma_fib_is_monotonic(i, (j - 1) as nat);
lemma_fib_is_monotonic(i, (j - 2) as nat);
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

There's a simpler proof for this, that we found during the tutorial proof debugging session:

Suggested change
if i < 2 && j < 2 {
} else if i == j {
} else if i == j - 1 {
assert(fib(j) == fib((j - 2) as nat) + fib((j - 1) as nat));
lemma_fib_is_monotonic(i, (j - 1) as nat);
} else {
lemma_fib_is_monotonic(i, (j - 1) as nat);
lemma_fib_is_monotonic(i, (j - 2) as nat);
}
if i < 2 && j < 2 {
} else if i == j {
} else if i == j - 1 {
reveal_with_fuel(fibo_spec, 2);
} else {
reveal_with_fuel(fibo_spec, 2);
lemma_fib_is_monotonic(i, (j - 1) as nat);
}

Comment on lines +419 to +423
exec fn fib_impl(n: u64) -> (result: u64)
requires
fib_fits_u64(n as nat),
ensures
result == fib(n as nat),
Copy link
Collaborator

Choose a reason for hiding this comment

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

This version of the fibonacci demo is a bit more compact, but sort-of unrealistic, due to the fact that the caller needs to prove that the result will fit in a u64. I have a more realistic version that returns an Option that's None if the result doesn't fit: what do you think, shall I replace it on this branch?

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.

2 participants