-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e8cadcf
commit 8dfe019
Showing
3 changed files
with
101 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -263,4 +263,3 @@ def check(candidate): | |
assert candidate('( ) (( )) (( )( ))') == ['()', '(())', '(()())'] | ||
*/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,7 @@ | ||
|
||
/* | ||
### ID | ||
HumanEval/3 | ||
*/ | ||
|
||
/* | ||
### VERUS BEGIN | ||
*/ | ||
|
@@ -13,34 +11,40 @@ verus! { | |
|
||
// This function is part of the specification | ||
pub open spec fn sum(s: Seq<int>) -> int | ||
decreases s.len() | ||
decreases s.len(), | ||
{ | ||
if s.len() == 0 { 0 } else { s[0] + sum(s.skip(1)) } | ||
if s.len() == 0 { | ||
0 | ||
} else { | ||
s[0] + sum(s.skip(1)) | ||
} | ||
} | ||
|
||
// This function is also part of the specification | ||
pub open spec fn first_n(s: Seq<i32>, n: int) -> Seq<int> | ||
{ | ||
pub open spec fn first_n(s: Seq<i32>, n: int) -> Seq<int> { | ||
s.take(n).map(|_idx, j: i32| j as int) | ||
} | ||
|
||
// This function is used by the proof | ||
pub open spec fn sum_other_way(s: Seq<int>) -> int | ||
decreases s.len() | ||
decreases s.len(), | ||
{ | ||
if s.len() == 0 { 0 } else { s.last() + sum_other_way(s.drop_last()) } | ||
if s.len() == 0 { | ||
0 | ||
} else { | ||
s.last() + sum_other_way(s.drop_last()) | ||
} | ||
} | ||
|
||
proof fn lemma_sum_equals_sum_other_way(s: Seq<int>) | ||
ensures | ||
sum(s) == sum_other_way(s), | ||
decreases s.len() | ||
decreases s.len(), | ||
{ | ||
if s.len() == 1 { | ||
assert(sum(s.skip(1)) == 0); | ||
assert(sum_other_way(s.drop_last()) == 0); | ||
} | ||
else if s.len() > 1 { | ||
} else if s.len() > 1 { | ||
let ss = s.skip(1); | ||
lemma_sum_equals_sum_other_way(ss); | ||
assert(sum_other_way(ss) == ss.last() + sum_other_way(ss.drop_last())); | ||
|
@@ -52,7 +56,8 @@ proof fn lemma_sum_equals_sum_other_way(s: Seq<int>) | |
|
||
fn below_zero(operations: Vec<i32>) -> (result: bool) | ||
ensures | ||
result <==> exists |i: int| 0 <= i <= [email protected]() && #[trigger] sum(first_n(operations@, i)) < 0, | ||
result <==> exists|i: int| | ||
0 <= i <= [email protected]() && #[trigger] sum(first_n(operations@, i)) < 0, | ||
{ | ||
let mut s = 0i32; | ||
let mut num_overflows: usize = 0; | ||
|
@@ -63,9 +68,11 @@ fn below_zero(operations: Vec<i32>) -> (result: bool) | |
max_plus == i32::MAX + 1, | ||
s >= 0, | ||
s + num_overflows * max_plus == sum(first_n(operations@, k as int)), | ||
forall |i: int| 0 <= i <= k ==> #[trigger] sum(first_n(operations@, i)) >= 0, | ||
forall|i: int| 0 <= i <= k ==> #[trigger] sum(first_n(operations@, i)) >= 0, | ||
{ | ||
assert(sum(first_n(operations@, k as int)) + operations@[k as int] == sum(first_n(operations@, k + 1))) by { | ||
assert(sum(first_n(operations@, k as int)) + operations@[k as int] == sum( | ||
first_n(operations@, k + 1), | ||
)) by { | ||
let q1 = first_n(operations@, k as int); | ||
let q2 = first_n(operations@, k + 1); | ||
assert(q2.last() == operations@[k as int] as int); | ||
|
@@ -78,12 +85,10 @@ fn below_zero(operations: Vec<i32>) -> (result: bool) | |
if s > i32::MAX - op { | ||
s += op - i32::MAX - 1; | ||
num_overflows += 1; | ||
} | ||
else { | ||
} else { | ||
s += op; | ||
} | ||
} | ||
else { | ||
} else { | ||
s += op; | ||
if s < 0 { | ||
if num_overflows == 0 { | ||
|
@@ -98,7 +103,6 @@ fn below_zero(operations: Vec<i32>) -> (result: bool) | |
} | ||
|
||
} // verus! | ||
|
||
fn main() {} | ||
|
||
/* | ||
|
@@ -159,4 +163,3 @@ def check(candidate): | |
assert candidate([1, -2, 2, -2, 5, -5, 4, -4]) == True | ||
*/ | ||
|
Oops, something went wrong.