Skip to content

Commit

Permalink
run verusfmt on all tasks/*.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
jaybosamiya committed Aug 22, 2024
1 parent 22abb7a commit 46e1a9c
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 77 deletions.
1 change: 0 additions & 1 deletion tasks/human_eval_001.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,4 +263,3 @@ def check(candidate):
assert candidate('( ) (( )) (( )( ))') == ['()', '(())', '(()())']
*/

43 changes: 23 additions & 20 deletions tasks/human_eval_003c.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@

/*
### ID
HumanEval/3
*/

/*
### VERUS BEGIN
*/
Expand All @@ -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()));
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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 {
Expand All @@ -98,7 +103,6 @@ fn below_zero(operations: Vec<i32>) -> (result: bool)
}

} // verus!

fn main() {}

/*
Expand Down Expand Up @@ -159,4 +163,3 @@ def check(candidate):
assert candidate([1, -2, 2, -2, 5, -5, 4, -4]) == True
*/

Loading

0 comments on commit 46e1a9c

Please sign in to comment.