merge: payroll's net pay in the kernel (K-U7; reviewed, fixes applied)
Net pay is gross less the withholdings and deductions, now one kernel fold (net_pay) with the plan's totals through the kernel's checked totals; payroll_remit_plan refuses a total past exact arithmetic instead of panicking. Proved: Kani (net pay equals gross less the deductions, None exactly when a running difference overflows, the plan balances for an employee), bridge properties against an exact integer reference at every prefix, and law 45 in bookspec's Lean for any number of employees. No figure moves; every golden unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
a37612d07c
8 files changed
+767
-39
No files matched your search
@@ -1,4 +1,194 @@
|
||||
//! U7 bridge properties: payroll arithmetic (kernel sweep).
|
||||
//!
|
||||
//! Empty in U0: the unit adds its `Decimal`-vs-integer properties here,
|
||||
//! beside `main.rs` and the other units.
|
||||
//! `proofs/payroll.rs` proves `kernel::net_pay` on `i64` (five
|
||||
//! 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
|
||||
//! code with the kernel), and the edge table: a gross or a running
|
||||
//! difference past the type's range is `None`, never a panic.
|
||||
|
||||
use ledger_core::kernel;
|
||||
use proptest::prelude::*;
|
||||
use rust_decimal::Decimal;
|
||||
|
||||
fn dec(cents: i64) -> Decimal {
|
||||
Decimal::new(cents, 2)
|
||||
}
|
||||
|
||||
/// The exact net reference: gross less the deductions, from the
|
||||
/// 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()
|
||||
.map(|&(_, s)| s)
|
||||
.max()
|
||||
.unwrap_or(0)
|
||||
.max(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(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()
|
||||
}
|
||||
|
||||
proptest! {
|
||||
#![proptest_config(ProptestConfig::with_cases(2000))]
|
||||
|
||||
/// The `i64` kernel (cents) and the `Decimal` kernel (dollars) give
|
||||
/// the same net for the same gross and five deductions.
|
||||
#[test]
|
||||
fn i64_and_decimal_net_pay_agree(
|
||||
gross in -1_000_000_000i64..=1_000_000_000,
|
||||
deductions in prop::collection::vec(-100_000_000i64..=100_000_000, 5),
|
||||
) {
|
||||
let i = kernel::net_pay(gross, &deductions);
|
||||
let d = kernel::net_pay(
|
||||
dec(gross),
|
||||
&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
|
||||
/// deductions of every sign and scale.
|
||||
#[test]
|
||||
fn decimal_net_pay_matches_the_exact_reference(
|
||||
gross_mantissa in -1_000_000_000i128..=1_000_000_000i128,
|
||||
gross_scale in 0u32..=4,
|
||||
deductions in prop::collection::vec(
|
||||
(-1_000_000_000i128..=1_000_000_000i128, 0u32..=4),
|
||||
0..=5,
|
||||
),
|
||||
) {
|
||||
let gross = Decimal::try_from_i128_with_scale(gross_mantissa, gross_scale).unwrap();
|
||||
let ds: Vec<Decimal> = deductions
|
||||
.iter()
|
||||
.map(|&(m, s)| Decimal::try_from_i128_with_scale(m, s).unwrap())
|
||||
.collect();
|
||||
let want = net_reference((gross_mantissa, gross_scale), &deductions);
|
||||
// `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
|
||||
/// less their sum and is not negative, at both instantiations.
|
||||
#[test]
|
||||
fn decimal_net_pay_nonnegative_when(
|
||||
gross in 0i64..=1_000_000_000,
|
||||
deductions in prop::collection::vec(0i64..=100_000_000, 5),
|
||||
) {
|
||||
let deducted: i64 = deductions.iter().sum();
|
||||
prop_assume!(deducted <= gross);
|
||||
let net = kernel::net_pay(
|
||||
dec(gross),
|
||||
&deductions.iter().copied().map(dec).collect::<Vec<_>>(),
|
||||
)
|
||||
.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);
|
||||
}
|
||||
}
|
||||
|
||||
/// The edges: empty deductions, a zero net, and figures past the type's
|
||||
/// range. Each answer is exact or `None`, never a panic.
|
||||
#[test]
|
||||
fn decimal_net_pay_at_the_edges() {
|
||||
// No deductions: the net is the gross itself.
|
||||
assert_eq!(
|
||||
kernel::net_pay(Decimal::new(500_000, 2), &[]),
|
||||
Some(Decimal::new(500_000, 2))
|
||||
);
|
||||
// The payroll scenario: 5,000.00 less 750, 250, 310, 72.50, 100.
|
||||
assert_eq!(
|
||||
kernel::net_pay(
|
||||
Decimal::new(500_000, 2),
|
||||
&[
|
||||
Decimal::new(75_000, 2),
|
||||
Decimal::new(25_000, 2),
|
||||
Decimal::new(31_000, 2),
|
||||
Decimal::new(7_250, 2),
|
||||
Decimal::new(10_000, 2),
|
||||
],
|
||||
),
|
||||
Some(Decimal::new(351_750, 2))
|
||||
);
|
||||
// Withholdings taking the whole gross give exactly zero, and more
|
||||
// than the gross give the exact negative difference.
|
||||
assert_eq!(
|
||||
kernel::net_pay(Decimal::ONE, &[Decimal::ONE]),
|
||||
Some(Decimal::ZERO)
|
||||
);
|
||||
assert_eq!(
|
||||
kernel::net_pay(Decimal::ONE, &[Decimal::from(2)]),
|
||||
Some(-Decimal::ONE)
|
||||
);
|
||||
// Past the range, in either direction and mid-fold: None.
|
||||
assert_eq!(kernel::net_pay(Decimal::MAX, &[-Decimal::ONE]), None);
|
||||
assert_eq!(kernel::net_pay(Decimal::MIN, &[Decimal::ONE]), None);
|
||||
assert_eq!(
|
||||
kernel::net_pay(Decimal::ZERO, &[Decimal::MAX, Decimal::MAX]),
|
||||
None
|
||||
);
|
||||
// The same edges on the Kani instantiation.
|
||||
assert_eq!(kernel::net_pay(i64::MAX, &[-1]), None);
|
||||
assert_eq!(kernel::net_pay(i64::MIN, &[1]), None);
|
||||
assert_eq!(kernel::net_pay(0i64, &[i64::MAX, i64::MAX]), None);
|
||||
assert_eq!(
|
||||
kernel::net_pay(5_000i64, &[750, 250, 310, 72, 100]),
|
||||
Some(3_518)
|
||||
);
|
||||
}
|
||||
@@ -1029,7 +1029,8 @@ fn remit_plan_hand_worked() {
|
||||
&[(3, d("600.00")), (4, d("0")), (6, d("145.00"))],
|
||||
PLAN_CASH,
|
||||
Currency::USD,
|
||||
);
|
||||
)
|
||||
.unwrap();
|
||||
let got: Vec<(AccountId, Side, Decimal)> =
|
||||
plan.iter().map(|(a, s, m)| (*a, *s, m.amount)).collect();
|
||||
assert_eq!(
|
||||
@@ -1042,6 +1043,29 @@ fn remit_plan_hand_worked() {
|
||||
);
|
||||
}
|
||||
|
||||
/// Oracle: BS-entry-4 (every derived figure computed with checked
|
||||
/// arithmetic): payables whose total is past exact arithmetic are refused
|
||||
/// as TooLargeToCompute, never a panic, even when a caller did not
|
||||
/// pre-check.
|
||||
#[test]
|
||||
fn remit_plan_past_exact_arithmetic_is_refused() {
|
||||
let err = payroll_remit_plan(
|
||||
&[(3, Decimal::MAX), (6, Decimal::ONE)],
|
||||
PLAN_CASH,
|
||||
Currency::USD,
|
||||
)
|
||||
.unwrap_err();
|
||||
assert!(matches!(err, LedgerError::TooLargeToCompute(_)), "{err:?}");
|
||||
// Past the range downward, too.
|
||||
let err = payroll_remit_plan(
|
||||
&[(3, Decimal::MIN), (6, -Decimal::ONE)],
|
||||
PLAN_CASH,
|
||||
Currency::USD,
|
||||
)
|
||||
.unwrap_err();
|
||||
assert!(matches!(err, LedgerError::TooLargeToCompute(_)), "{err:?}");
|
||||
}
|
||||
|
||||
/// Oracle: run_payroll INVARIANTS 1–2 and doc (auto-create 6100/6110/
|
||||
/// 2110–2150), hand-worked Alice entry; balances after: cash 10000 − 3567.50
|
||||
/// = 6432.50; payables as in the plan.
|
||||
@@ -1478,7 +1502,7 @@ proptest! {
|
||||
.enumerate()
|
||||
.map(|(i, c)| (i as AccountId + 1, Decimal::new(*c, 2)))
|
||||
.collect();
|
||||
let plan = payroll_remit_plan(&payables, PLAN_CASH, Currency::USD);
|
||||
let plan = payroll_remit_plan(&payables, PLAN_CASH, Currency::USD).unwrap();
|
||||
let total: Decimal = payables.iter().map(|(_, b)| *b).sum();
|
||||
let (dr, cr) = plan_totals(&plan);
|
||||
prop_assert_eq!(dr, cr);
|
||||
|
||||
Reference in new issue
Block a user