test(core): K-U7 review: None-iff-overflow proof, covers that can fail, a sharper bridge
- Kani net_pay_none_iff_a_prefix_overflows proves what the doc claimed: None exactly when some running difference leaves the i64 range, else the exact i128 result. It found that i64's Amount::checked_sub negates first, so a deduction of i64::MIN (no negation) is refused where the difference would fit (-1 - i64::MIN). Conservative, and Decimal's range is symmetric, so the engine is unaffected; the harness assumes it away and asserts the refusal at the fixed case; the kernel doc says so. - payroll_plan_balances: covers that the assumes did not already force (net of one unit, payables holding employer shares alone, federal or state posted); the harness doc says it rebuilds the plan on i64 rather than calling payroll_entry_plan, and what that does and does not prove. - Docs name the Lean theorems that carry any number of employees (payroll_run_net_eq, payroll_run_plan_balances) where they had claimed it. - Bridge: compare to_string as well as value (Decimal equality ignores scale); a strategy near Decimal::MAX/MIN where the reference also returns None, which required the reference to check every running difference as the kernel's fold does, not only the last. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
46dccd5d76
commit
b31824b2ca
4 files changed
+210
-45
No files matched your search
@@ -36,9 +36,13 @@ use super::Amount;
|
||||
/// payroll line whose net is negative, so a posted run's net pay
|
||||
/// is never below zero. A net below zero is still a value here:
|
||||
/// the refusal is the caller's rule, not arithmetic's.
|
||||
/// 4. None on overflow; NEVER a rounded or wrapped result, never a
|
||||
/// panic. The fold is left to right, so [MAX, MAX, -MAX] from a
|
||||
/// zero gross is None while the same figures summed first would
|
||||
/// 4. None on overflow (proved: `net_pay_none_iff_a_prefix_overflows`
|
||||
/// says None exactly when some running difference leaves the
|
||||
/// range; on `i64` a deduction of `i64::MIN`, which has no
|
||||
/// negation, is refused too); NEVER a rounded or wrapped
|
||||
/// result, never a panic.
|
||||
/// The fold is left to right, so [MAX, MAX, -MAX] from a zero
|
||||
/// gross is None while the same figures summed first would
|
||||
/// cancel: each step must be exact, as `total`'s fold is.
|
||||
/// SCENARIOS:
|
||||
/// 1. IF gross 5,000.00 and withholdings 750.00, 250.00, 310.00,
|
||||
|
||||
@@ -12,7 +12,27 @@
|
||||
//! lines. Amounts run to a few thousand units: the law is about the
|
||||
//! shape of the subtraction and the balance, not the magnitude, and
|
||||
//! every total stays far inside the `i64` line range (2^40) that
|
||||
//! `check_lines` requires. Lean proves the same laws at any size.
|
||||
//! `check_lines` requires.
|
||||
//!
|
||||
//! What carries "any number of employees": Kani covers one employee
|
||||
//! (and `net_pay` for any five deductions). Lean carries the rest, in
|
||||
//! bookspec's `Payroll.lean`: `foldl_sub_eq` and `payroll_net_eq` (the
|
||||
//! left-to-right fold is gross less the sum), `payroll_run_net_eq` (the
|
||||
//! sum of each employee's net is the total gross less every deduction,
|
||||
//! for a list of any length) and `payroll_run_plan_balances` (the plan
|
||||
//! built from per-employee sums balances). The edge where a running
|
||||
//! difference leaves the range is proved here by
|
||||
//! `net_pay_none_iff_a_prefix_overflows`; the bridge's edge table
|
||||
//! checks the same on `Decimal`.
|
||||
//!
|
||||
//! The plan harness rebuilds the eight totals on `i64` rather than
|
||||
//! calling `payroll_entry_plan`: that function works in `Decimal` and
|
||||
//! `Money` and maps the totals to accounts, neither of which is generic
|
||||
//! over `Amount`. So the harness proves the arithmetic the plan is made
|
||||
//! of (the kernel totals and `net_pay`, the shape of the eight lines,
|
||||
//! `check_lines` accepting them); that `payroll_entry_plan` assembles
|
||||
//! those same totals in that shape is checked by its tests, the goldens
|
||||
//! and `check_lines`, which every posted entry passes.
|
||||
//!
|
||||
//! ```text
|
||||
//! GOAL: net pay is gross less the withholdings exactly; it is not
|
||||
@@ -24,7 +44,9 @@
|
||||
//! through check_lines).
|
||||
//! INVARIANTS:
|
||||
//! 1. net_pay(gross, ds) = gross - sum(ds) whenever it is Some, and
|
||||
//! None exactly when a running difference leaves the range.
|
||||
//! (net_pay_none_iff_a_prefix_overflows) it is None exactly when
|
||||
//! some running difference (gross less the first k deductions)
|
||||
//! leaves the range.
|
||||
//! 2. sum(ds) <= gross (ds non-negative) gives a net that is Some and
|
||||
//! not negative.
|
||||
//! 3. The eight plan lines pass check_lines: two or more lines, every
|
||||
@@ -73,6 +95,57 @@ fn any_employee() -> Employee {
|
||||
}
|
||||
}
|
||||
|
||||
/// `net_pay` is `None` exactly when some running difference leaves the
|
||||
/// `i64` range, and otherwise the exact result: gross and five
|
||||
/// deductions over the whole `i64` range, checked against an `i128`
|
||||
/// running difference (which cannot overflow here) at every prefix.
|
||||
///
|
||||
/// The one exception is the `i64` instantiation's own: `Amount`'s
|
||||
/// `checked_sub` negates the subtrahend first, and `i64::MIN` has no
|
||||
/// negation, so a deduction of exactly `i64::MIN` is refused even where
|
||||
/// the difference would fit (`-1 - i64::MIN`). That is conservative (a
|
||||
/// refusal, never a wrong figure), and `Decimal`'s range is symmetric,
|
||||
/// so it does not arise in the engine; the harness assumes no deduction
|
||||
/// is `i64::MIN` and asserts the refusal at the fixed case.
|
||||
#[kani::proof]
|
||||
fn net_pay_none_iff_a_prefix_overflows() {
|
||||
let gross: i64 = kani::any();
|
||||
let mut deductions = [0i64; DEDUCTIONS];
|
||||
for deduction in &mut deductions {
|
||||
let d: i64 = kani::any();
|
||||
kani::assume(d != i64::MIN);
|
||||
*deduction = d;
|
||||
}
|
||||
assert!(
|
||||
net_pay(-1i64, &[i64::MIN]).is_none(),
|
||||
"i64::MIN has no negation: refused, conservatively"
|
||||
);
|
||||
let mut running = i128::from(gross);
|
||||
let mut overflowed = false;
|
||||
for deduction in deductions {
|
||||
running -= i128::from(deduction);
|
||||
if i64::try_from(running).is_err() {
|
||||
overflowed = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
let net = net_pay(gross, &deductions);
|
||||
if overflowed {
|
||||
assert!(net.is_none(), "a prefix left the range: refused");
|
||||
} else {
|
||||
assert_eq!(
|
||||
i128::from(net.expect("every prefix in range")),
|
||||
running,
|
||||
"no prefix left the range: the exact result"
|
||||
);
|
||||
}
|
||||
kani::cover!(overflowed, "some running difference overflows");
|
||||
kani::cover!(
|
||||
!overflowed && running != i128::from(gross),
|
||||
"an exact, changed net"
|
||||
);
|
||||
}
|
||||
|
||||
/// Net pay is gross less the deductions: the exact difference of the
|
||||
/// gross and the deductions' total, for deductions of either sign.
|
||||
#[kani::proof]
|
||||
@@ -123,9 +196,10 @@ fn net_pay_nonnegative_when() {
|
||||
/// Two employees timed out in CBMC (over 80 minutes, 2026-11-09:
|
||||
/// sixteen symbolic figures into eight totals and `check_lines`); one
|
||||
/// employee reaches every line, both FICA shares on both sides, and
|
||||
/// the balance, and Lean's `payroll_plan_balances` carries the plan at
|
||||
/// its totals for any number of employees (a run's totals are sums of
|
||||
/// the same eight figures).
|
||||
/// the balance, and Lean's `payroll_run_plan_balances` carries the plan
|
||||
/// built from per-employee sums for any number of employees. The plan is
|
||||
/// rebuilt here on `i64` (see the module doc), not taken from
|
||||
/// `payroll_entry_plan`.
|
||||
#[kani::proof]
|
||||
fn payroll_plan_balances() {
|
||||
let employees = [any_employee()];
|
||||
@@ -170,6 +244,14 @@ fn payroll_plan_balances() {
|
||||
check_lines(&plan).is_ok(),
|
||||
"the payroll plan is an acceptable entry"
|
||||
);
|
||||
kani::cover!(net < gross, "withholdings leave a smaller net");
|
||||
kani::cover!(employer > 0, "the employer share is posted");
|
||||
// Covers the assumptions do not already force.
|
||||
kani::cover!(net == 1, "net pay of exactly one unit is posted");
|
||||
kani::cover!(
|
||||
employees[0].deductions[2] == 0 && employees[0].deductions[3] == 0,
|
||||
"the payables hold the employer shares alone"
|
||||
);
|
||||
kani::cover!(
|
||||
employees[0].deductions[0] + employees[0].deductions[1] > 0,
|
||||
"federal and state withholdings are posted"
|
||||
);
|
||||
}
|
||||
@@ -1,7 +1,8 @@
|
||||
//! U7 bridge properties: payroll arithmetic (kernel sweep).
|
||||
//!
|
||||
//! `proofs/payroll.rs` proves `kernel::net_pay` on `i64` (five
|
||||
//! deductions, a one-employee plan through `check_lines`); the engine
|
||||
//! deductions, a one-employee plan through `check_lines`, and `None`
|
||||
//! exactly when a prefix overflows); the engine
|
||||
//! runs the same code on `Decimal`. These properties check that the two
|
||||
//! instantiations agree, that `Decimal` agrees with an independent exact
|
||||
//! `i128` reference (mantissas over a common power of ten, sharing no
|
||||
@@ -17,7 +18,11 @@ fn dec(cents: i64) -> Decimal {
|
||||
}
|
||||
|
||||
/// The exact net reference: gross less the deductions, from the
|
||||
/// mantissas over their common power of ten, in `i128` throughout.
|
||||
/// mantissas over their common power of ten, in `i128` throughout. Like
|
||||
/// the kernel's fold it requires every running difference to be
|
||||
/// representable, each at the scale it has so far (the largest scale of
|
||||
/// the gross and the deductions folded in): a prefix past `Decimal`'s
|
||||
/// range is `None` even if later deductions would bring it back.
|
||||
fn net_reference(gross: (i128, u32), deductions: &[(i128, u32)]) -> Option<Decimal> {
|
||||
let scale = deductions
|
||||
.iter()
|
||||
@@ -25,9 +30,22 @@ fn net_reference(gross: (i128, u32), deductions: &[(i128, u32)]) -> Option<Decim
|
||||
.max()
|
||||
.unwrap_or(0)
|
||||
.max(gross.1);
|
||||
let mut net: i128 = gross.0.checked_mul(10i128.checked_pow(scale - gross.1)?)?;
|
||||
let at_common = |(m, s): (i128, u32)| m.checked_mul(10i128.checked_pow(scale - s)?);
|
||||
let mut net: i128 = at_common(gross)?;
|
||||
let mut so_far = gross.1;
|
||||
let representable = |net: i128, so_far: u32| {
|
||||
let own = net / 10i128.pow(scale - so_far);
|
||||
Decimal::try_from_i128_with_scale(own, so_far).is_ok()
|
||||
};
|
||||
if !representable(net, so_far) {
|
||||
return None;
|
||||
}
|
||||
for &(mantissa, s) in deductions {
|
||||
net = net.checked_sub(mantissa.checked_mul(10i128.checked_pow(scale - s)?)?)?;
|
||||
net = net.checked_sub(at_common((mantissa, s))?)?;
|
||||
so_far = so_far.max(s);
|
||||
if !representable(net, so_far) {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
Decimal::try_from_i128_with_scale(net, scale).ok()
|
||||
}
|
||||
@@ -48,6 +66,7 @@ proptest! {
|
||||
&deductions.iter().copied().map(dec).collect::<Vec<_>>(),
|
||||
);
|
||||
prop_assert_eq!(i.map(dec), d);
|
||||
prop_assert_eq!(i.map(|n| dec(n).to_string()), d.map(|n| n.to_string()));
|
||||
}
|
||||
|
||||
/// `Decimal`'s net is the exact difference, for a gross and
|
||||
@@ -67,7 +86,41 @@ proptest! {
|
||||
.map(|&(m, s)| Decimal::try_from_i128_with_scale(m, s).unwrap())
|
||||
.collect();
|
||||
let want = net_reference((gross_mantissa, gross_scale), &deductions);
|
||||
prop_assert_eq!(kernel::net_pay(gross, &ds), want);
|
||||
// `Decimal` equality ignores scale: compare the rendering too, so
|
||||
// the reference's scale (the common one) is the kernel's.
|
||||
let got = kernel::net_pay(gross, &ds);
|
||||
prop_assert_eq!(got.map(|n| n.to_string()), want.map(|n| n.to_string()));
|
||||
prop_assert_eq!(got, want);
|
||||
}
|
||||
|
||||
/// Near `Decimal`'s range the reference also overflows, so the
|
||||
/// property meets `None` (and the in-range answers beside it), not
|
||||
/// only the edge table: the kernel and the reference agree on which
|
||||
/// is which.
|
||||
#[test]
|
||||
fn decimal_net_pay_near_the_range_agrees_with_the_reference(
|
||||
gross_offset in 0i128..=3,
|
||||
gross_negative in any::<bool>(),
|
||||
deductions in prop::collection::vec((0i128..=3, any::<bool>()), 0..=5),
|
||||
) {
|
||||
let max = Decimal::MAX.mantissa();
|
||||
let with_sign = |offset: i128, negative: bool| {
|
||||
if negative { -(max - offset) } else { max - offset }
|
||||
};
|
||||
let g = with_sign(gross_offset, gross_negative);
|
||||
let ds_m: Vec<(i128, u32)> = deductions
|
||||
.iter()
|
||||
.map(|&(o, n)| (with_sign(o, n), 0u32))
|
||||
.collect();
|
||||
let gross = Decimal::try_from_i128_with_scale(g, 0).unwrap();
|
||||
let ds: Vec<Decimal> = ds_m
|
||||
.iter()
|
||||
.map(|&(m, s)| Decimal::try_from_i128_with_scale(m, s).unwrap())
|
||||
.collect();
|
||||
let want = net_reference((g, 0), &ds_m);
|
||||
let got = kernel::net_pay(gross, &ds);
|
||||
prop_assert_eq!(got.map(|n| n.to_string()), want.map(|n| n.to_string()));
|
||||
prop_assert_eq!(got, want);
|
||||
}
|
||||
|
||||
/// When the deductions do not exceed gross, the net is the gross
|
||||
@@ -85,6 +138,7 @@ proptest! {
|
||||
)
|
||||
.expect("in range");
|
||||
prop_assert_eq!(net, dec(gross - deducted));
|
||||
prop_assert_eq!(net.to_string(), dec(gross - deducted).to_string());
|
||||
prop_assert!(net >= Decimal::ZERO);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user