-
Notifications
You must be signed in to change notification settings - Fork 8
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
How to use loop invariants #177
Comments
Moreover, according to @Ferinko, the invariant annotation and label don't do anything because there's no jumping. Take a look at this, which only works for small // @pre n >= 0
// @pre n <= 15
// @post 2 * $Return.sum == n * (n + 1)
func compute_sum(n: felt) -> (sum: felt) {
if (n == 0) {
// When 0 is reached, return 0.
return (sum=0);
}
let (sum) = compute_sum(n=n - 1);
let new_sum = sum + n;
return (sum=new_sum);
}
func main() {
let (res) = compute_sum(n=10);
return();
} (horus37) mal@computer:~/pkgs/horus-checker/demos$ ./display.sh invariant.cairo
~~~~~~~~~~~~~~~{SOURCE}~~~~~~~~~~~~~~
// @pre n >= 0
// @pre n <= 15
// @post 2 * $Return.sum == n * (n + 1)
func compute_sum(n: felt) -> (sum: felt) {
if (n == 0) {
// When 0 is reached, return 0.
return (sum=0);
}
let (sum) = compute_sum(n=n - 1);
let new_sum = sum + n;
return (sum=new_sum);
}
func main() {
let (res) = compute_sum(n=10);
return();
}
~~~~~~~~~~~~~~{RESULT}~~~~~~~~~~~~~~~
real 0m1.265s
user 0m1.198s
sys 0m0.068s
Warning: Horus is currently in the *alpha* stage. Please be aware of the
Warning: known issues: https://github.com/NethermindEth/horus-checker/issues
compute_sum
Verified
main [inlined]
Verified
real 0m1.640s
user 0m1.618s
sys 0m0.024s
~~~~~~~~~~~~~~{REVISION}~~~~~~~~~~~~~
3f2e0db (HEAD -> langfield/makerdao-frob, origin/langfield/makerdao-frob) dirty: Bump `horus-compile` version exclusive upper bound to 0.0.8
~~~~~~~~~~~~~~{FILENAME}~~~~~~~~~~~~~
invariant.cairo This at least works, although from the timeout behavior, I'm fairly sure it's checking via bit-blasting. Any thoughts on how to make this work for larger Edit: Okay it works the same even without the label and invariant. I had assumed this example complained about a loop without invariant before it was added. |
It should be noted that our upper bound needs to be sufficiently small so that the product does not wrap around, since we are working in a finite field. |
I am very confused by this example - what are we trying to show? There's no looping behaviour here, the And if we look at the original example by @Leonard-Pat that does use a Horus annotation of invariant, then Horus will split the reasoning into: Pre(compute_sum) -> Invariant(loop) and Invariant(loop -> Post(compute_sum); unfortunately, n > 0 is too weak to compute Post(compute_sum) from unless n is severerly bounded and Horus can bruteforce it. |
Okay that's a typo. I guess I didn't understand what is and is not considered a loop. |
@Leonard-Pat Can you show us the code example you were running where you did get the "loop with no invariant" error? |
The function i'm trying to test is as follows:
I can't seem to add an invariant check, or run horus-checker on it without it throwing an error |
Okay this was addressed on Discord: basically, the loopy behavior that requires an |
I was wondering what invariant condition (and where) I need to add to a recursive function
The text was updated successfully, but these errors were encountered: