Skip to content

Commit

Permalink
Strengthen spec for 60
Browse files Browse the repository at this point in the history
  • Loading branch information
parno committed Sep 28, 2024
1 parent f2cc80e commit 70e6560
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions tasks/human_eval_060.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,52 @@ spec fn spec_sum_to_n(n: nat) -> nat
}
}

proof fn lemma_sum_monotonic(i:nat, j:nat)
requires
i <= j
ensures
spec_sum_to_n(i) <= spec_sum_to_n(j),
decreases j - i,
{
if (i == 0 && j == 0) || i == j {
} else if i == j - 1 {
lemma_sum_monotonic(i, (j - 1) as nat);
} else {
lemma_sum_monotonic(i, (j - 1) as nat);
}
}

fn sum_to_n(n: u32) -> (sum: Option<u32>)
ensures
sum.is_some() ==> sum.unwrap() == spec_sum_to_n(n as nat),
match sum {
None => spec_sum_to_n(n as nat) > u32::MAX,
Some(f) => f == spec_sum_to_n(n as nat)
},
{
if n >= 92682 {
proof {
assert(spec_sum_to_n(92682) > u32::MAX) by (compute_only);
lemma_sum_monotonic(92682, n as nat);
}
return None;
}
let mut res: u32 = 0;
let mut sum: u32 = 0;
let mut i: u32 = 0;
while i < n
invariant
i <= n,
i <= n < 92682,
res == spec_sum_to_n(i as nat),
res <= u32::MAX,
{
i += 1;
res = i.checked_add(res)?;
proof {
// Prove that that n1 + n2 won't overflow
assert(spec_sum_to_n(92681) < u32::MAX) by (compute_only);
lemma_sum_monotonic(i as nat, 92681);
lemma_sum_monotonic((i - 1) as nat, 92681);
}
res = i + res;
}
Some(res)
}
Expand Down

0 comments on commit 70e6560

Please sign in to comment.