merge: Lean proves the deductible share rounds once, at any size (branch t3/deductible-share-lean-proof, aa1331c8)
check / check (push) Canceled after 0s
portable-macos / portable (macos-latest) (push) Canceled after 0s
portable-windows / portable (windows-latest) (push) Canceled after 0s
two-user / two-user (push) Canceled after 0s

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Opus 5.5 committed 2026-11-08 08:19:17 -07:00
commit a2ad2ed35e
6 files changed
+388 -11

No files matched your search

+4 -1
View File
@@ -2272,7 +2272,10 @@ their first `settings.set` naming the key, B-27); `ledger-mcp` `books.add_accoun
`tests/tax.rs` (the rounding: `a_category_rounds_its_deductible_amount_once_not_per_account`,
`a_category_rounds_once_across_different_percents`); Kani
`deductible_amount_rounds_once`, `deductible_amount_refuses_overflow`;
bridge `deductible_amount_agrees`; `ledger-store` `tests/store.rs`
bridge `deductible_amount_agrees`; Lean `verify/lean/Wanderbooks/Deductible.lean`
(`Deductible.near`, `tie_away_pos`, `tie_away_neg`, `sign`, `neg`, `of_sum`,
`per_account_differs`, `scale_books`, `scale_percents`: the one rounding, at any
number of accounts and any sizes); `ledger-store` `tests/store.rs`
(`prop_tax_policy_refuses_exactly`, `a_journaled_clear_replays_under_refuse`);
`ledger-cli` `tests/spec_tax_policy.rs`; `ledger-gui` `tests/view_model.rs`
(`needs_attention_flags_every_uncategorized_pl_account_defect_8`);
+31 -8
View File
@@ -679,14 +679,37 @@ new command, so a new Lean step, and no kernel function.
the same function; the bridge properties above carry it to
Decimal, the exact-reference one over the decimal shapes Kani's
integer model does not have.
- No Lean model: this is arithmetic on a report figure, like
`discount_max` (law 33) and `unrecovered_cost` (law 32), not a
`Ledger` command or a law about posted entries. The model's `Books`
has no tax categories; adding them would model presentation (a
category is a label on an account, and the summary is read from the
P&L), not a change to the books, and the law it would restate is the
kernel function's own definition. Kani proves that function, and the
bridge carries it to `Decimal`.
- Lean `Deductible.near`, `Deductible.tie_away_pos`,
`Deductible.tie_away_neg`, `Deductible.sign`, `Deductible.neg`,
`Deductible.of_sum`, `Deductible.of_perm`, `Deductible.append`,
`Deductible.split_percent`, `Deductible.per_account_differs` (with
`_mixed` and `Deductible.tie_example`, the spec's three scenarios by
`decide`), `Deductible.scale_books`, `Deductible.scale_percents`
(and their rounding lemmas `Deductible.roundHA_*`, all in
`Audit.lean`), for any number of accounts and any sizes: the result
is within half a cent of the exact sum (`|2(D·c − N)| ≤ D`); at
exactly k + ½ cents it is k + 1 (and −(k + 1)), the cent farther
from zero; it never has the sum's opposite sign and is 0 for a zero
sum; negating every book negates it; it depends on the shares only
through their exact sum (reordering, regrouping, or splitting an
account's percent changes nothing), while the sum of per-account
roundings does differ (1.01 + 1.01 at 50%: 1.01 once, 1.02 by
account); and writing books or percents at a finer common
denominator (more decimal places) changes nothing.
- What the model is, and what it is not: no `Books` model is needed,
the way `Prorate.lean` needs none for the proration kernel. A share
is a pair of integers over one positive common denominator `D`
(`10^(p+s)` for books at p and percents at s decimal places, the
mantissas the `Decimal` path of `checked_div_round_cent` works on);
the category in cents is `N / D`, `N = Σ bookᵢ × percentᵢ` (the
`÷ 100` and the dollars-to-cents `× 100` cancel); one rounding,
`roundHA`, is `Prorate.roundUnit` (reused, unit one) on the magnitude,
signed after, as the kernel's `a_mag`/`b_mag`/`negative` do. Lean
proves the **law** of that arithmetic at any size; it does not prove
the Rust. Kani proves the code on `i64` (bounded, above); the bridge
and exact-reference properties carry it to `Decimal`; overflow
(`None`) is outside the model, Kani's `deductible_amount_refuses_overflow`
and the tests cover it.
## What the proofs found
+2 -2
View File
@@ -29,8 +29,8 @@ both were fixed before merging.
- **2026-11-08:** the deductible share is `kernel::deductible_amount`
with Kani harnesses (`deductible_amount_rounds_once`, in `kani_slow`,
~17 min; `deductible_amount_refuses_overflow`), bridge and exact-reference
properties, and docs/verification.md law 39 (no Lean theorem: report
arithmetic, reasoning there). `just prove` green; the slow harness
properties, and docs/verification.md law 39 (Lean `Deductible.lean`: the
law at any size, no Books model needed). `just prove` green; the slow harness
verified. Review caught a false overflow on huge Decimal results, fixed.
**Left:** as before: P12 step 2 (the owner's overnight run, not started);
+20
View File
@@ -104,3 +104,23 @@ open Wanderbooks
#print axioms Step.discard_keeps_books
#print axioms Step.discard_drafts
#print axioms Step.discard_unblocks_close
#print axioms Deductible.roundHA_near
#print axioms Deductible.roundHA_tie_pos
#print axioms Deductible.roundHA_tie_neg
#print axioms Deductible.roundHA_neg
#print axioms Deductible.roundHA_sign
#print axioms Deductible.roundHA_scale
#print axioms Deductible.near
#print axioms Deductible.tie_away_pos
#print axioms Deductible.tie_away_neg
#print axioms Deductible.sign
#print axioms Deductible.neg
#print axioms Deductible.of_sum
#print axioms Deductible.of_perm
#print axioms Deductible.append
#print axioms Deductible.split_percent
#print axioms Deductible.per_account_differs
#print axioms Deductible.per_account_differs_mixed
#print axioms Deductible.tie_example
#print axioms Deductible.scale_books
#print axioms Deductible.scale_percents
+1
View File
@@ -6,4 +6,5 @@ import Wanderbooks.Ledger
import Wanderbooks.Apportion
import Wanderbooks.Parties
import Wanderbooks.Prorate
import Wanderbooks.Deductible
import Wanderbooks.Documents
+330
View File
@@ -0,0 +1,330 @@
import Wanderbooks.Prorate
/-!
# A category's deductible amount: the shares added exactly, rounded once,
half away from zero, at any size
Mirrors `crates/ledger-core/src/kernel.rs` `deductible_amount` and
`Amount::checked_div_round_cent` (rules.md B-32, B-21; owner decision D-67).
The kernel adds each account's exact share `book × percent ÷ 100`, then
divides by 100 once and rounds the total to the cent, half away from zero.
On `Decimal` it does this on integer mantissas over powers of ten: a book
is `b × 10^-p`, a percent `q × 10^-s`, so a share is `b·q ÷ 10^(p+s)` of a
percent-point, and the category is `(Σ bᵢ·qᵢ) ÷ (100 · 10^(p+s))` dollars,
found by exact integer division with the remainder deciding the tie, never
a float and never a per-share rounding.
The model is that, with the power of ten left to be any positive `D`:
books and percents are integers (`Share`), `D` the common denominator they
share (`10^(p+s)` above), and the exact category in dollars is `N ÷ (100·D)`
with `N = Σ bᵢ·qᵢ` (`sumProd`). In cents that is `N ÷ D`: the `÷ 100` of the
percent and the `× 100` of dollars-to-cents cancel, so the one rounding is
`roundHA N D`, the integer nearest `N / D`, ties away from zero. It is
built on `Prorate.roundUnit` (the Nat rounding of the proration kernel, with
the cent as the unit), applied to the magnitude and signed after, which is
what the kernel's `a_mag`, `b_mag` and `negative` do.
This proves the law, not the Rust code. Kani proves the code on `i64` for up
to three accounts and bounded books (`deductible_amount_rounds_once`,
`deductible_amount_refuses_overflow`); `tests/kernel_bridge.rs` and the exact
reference property carry it to `Decimal`; here the law holds for any number
of accounts and any sizes: within half a cent of the exact sum (`near`), a
tie to the cent farther from zero (`tie_away_pos`, `tie_away_neg`), never the
opposite sign (`sign`), odd (`neg`), a function of the exact sum alone
(`of_sum`, `of_perm`, `append`: rounded once, on the sum, and so not the
sum of the rounded shares, which `per_account_differs` shows can differ), and independent of how many decimal places
the books and percents carry (`scale_books`, `scale_percents`). Overflow
(the kernel returns None) is outside the model; integers do not overflow.
-/
namespace Wanderbooks.Deductible
open Wanderbooks.Prorate (roundUnit roundUnit_near roundUnit_scale)
/-- One account's share: its book and its deductible percent, as integers
over the category's common denominator. -/
abbrev Share := Int × Int
/-- The exact sum `Σ bookᵢ × percentᵢ` over the common denominator. -/
def sumProd : List Share → Int
| [] => 0
| (b, q) :: t => b * q + sumProd t
/-- `n / d` to the nearest whole cent, half away from zero: the magnitude
rounded half up (`Prorate.roundUnit`, unit one), then signed. -/
def roundHA (n : Int) (d : Nat) : Int :=
if 0 ≤ n then (roundUnit n.natAbs d 1 : Int) else -(roundUnit n.natAbs d 1 : Int)
/-- The category's deductible amount, in cents: the exact sum `N`, over the
common denominator `D`, rounded once. -/
def deductible (D : Nat) (l : List Share) : Int := roundHA (sumProd l) D
/-! ## The rounding -/
theorem roundUnit_zero (d : Nat) (hd : 0 < d) : roundUnit 0 d 1 = 0 := by
unfold roundUnit
have : 2 * 0 + d * 1 < 2 * d * 1 := by omega
rw [Nat.div_eq_of_lt this, Nat.mul_zero]
/-- Natural-number rounding, in integers: `2·d·r` within `d` of `2·m`. -/
private theorem unit_near_int (m d : Nat) (hd : 0 < d) :
-(d : Int) ≤ 2 * (((d * roundUnit m d 1 : Nat) : Int) - m) ∧
2 * (((d * roundUnit m d 1 : Nat) : Int) - m) ≤ d := by
obtain ⟨h1, h2⟩ := roundUnit_near m d 1 hd Nat.one_pos
have e : 2 * d * roundUnit m d 1 = 2 * (d * roundUnit m d 1) := Nat.mul_assoc 2 d _
rw [e] at h1 h2
generalize d * roundUnit m d 1 = P at h1 h2 ⊢
omega
/-- **Near**: the result is within half a cent of the exact quotient, at any
size: `|2·(d·c − n)| ≤ d`, written without division. (Any other whole cent
is at least as far, so it is a nearest one.) -/
theorem roundHA_near (n : Int) (d : Nat) (hd : 0 < d) :
-(d : Int) ≤ 2 * ((d : Int) * roundHA n d - n) ∧
2 * ((d : Int) * roundHA n d - n) ≤ d := by
have h := unit_near_int n.natAbs d hd
have e : (d : Int) * (roundUnit n.natAbs d 1 : Nat) = ((d * roundUnit n.natAbs d 1 : Nat) : Int) := by
push_cast; rfl
unfold roundHA
generalize d * roundUnit n.natAbs d 1 = P at h e
split
· rename_i hn
have : (n.natAbs : Int) = n := Int.natAbs_of_nonneg hn
rw [e]; omega
· rename_i hn
have : (n.natAbs : Int) = -n := by omega
rw [Int.mul_neg, e]; omega
/-- **Odd symmetry**: rounding half away from zero treats the two signs
alike, so negating the amount negates the result. -/
theorem roundHA_neg (n : Int) (d : Nat) (hd : 0 < d) : roundHA (-n) d = -roundHA n d := by
unfold roundHA
rw [Int.natAbs_neg]
by_cases h0 : n = 0
· subst h0
simp [roundUnit_zero d hd]
· by_cases h : 0 ≤ n
· have h' : ¬ 0 ≤ -n := by omega
simp only [h, h', ↓reduceIte]
· have h' : 0 ≤ -n := by omega
simp only [h, h', ↓reduceIte, Int.neg_neg]
/-- **Sign**: the result is never of the opposite sign to the exact
quotient, and is zero when it is zero. (A small positive amount may round
to zero; none ever rounds across it. An `Int` has no negative zero.) -/
theorem roundHA_sign (n : Int) (d : Nat) (hd : 0 < d) :
(0 ≤ n → 0 ≤ roundHA n d) ∧ (n ≤ 0 → roundHA n d ≤ 0) ∧ (n = 0 → roundHA n d = 0) := by
refine ⟨?_, ?_, ?_⟩
· intro h
unfold roundHA
simp [h]
· intro h
unfold roundHA
by_cases h0 : n = 0
· subst h0; simp [roundUnit_zero d hd]
· have : ¬ 0 ≤ n := by omega
simp [this]
· intro h
subst h
unfold roundHA
simp [roundUnit_zero d hd]
/-- **A tie at exactly a half cent goes away from zero** (positive side):
`n / d = k + 1/2` rounds to `k + 1`, the cent farther from zero. -/
theorem roundHA_tie_pos (n : Int) (d k : Nat) (hd : 0 < d)
(h : 2 * n = (d : Int) * (2 * k + 1)) : roundHA n d = k + 1 ∧ n < (d : Int) * roundHA n d := by
have hQ : (d : Int) * (2 * k + 1) = ((d * (2 * k + 1) : Nat) : Int) := by push_cast; rfl
have hpos : 0 < d * (2 * k + 1) := Nat.mul_pos hd (by omega)
have hn : 0 ≤ n := by rw [hQ] at h; omega
have hm : 2 * n.natAbs = d * (2 * k + 1) := by
have : (n.natAbs : Int) = n := Int.natAbs_of_nonneg hn
rw [hQ] at h; omega
have hr : roundUnit n.natAbs d 1 = k + 1 := by
unfold roundUnit
have k1 : d * (2 * k + 1) = 2 * (d * k) + d := by
rw [Nat.mul_add, Nat.mul_left_comm, Nat.mul_one]
have k2 : 2 * d * 1 * (k + 1) = 2 * (d * k) + 2 * d := by
rw [Nat.mul_one, Nat.mul_add, Nat.mul_one, Nat.mul_assoc]
have e : 2 * n.natAbs + d * 1 = 2 * d * 1 * (k + 1) := by
rw [k2]; omega
rw [e, Nat.mul_div_cancel_left _ (by omega)]
omega
have hc : roundHA n d = k + 1 := by
unfold roundHA
simp [hn, hr]
refine ⟨hc, ?_⟩
rw [hc, Int.mul_add, Int.mul_one]
have k1 : (d : Int) * (2 * k + 1) = 2 * ((d : Int) * k) + d := by
rw [Int.mul_add, Int.mul_left_comm, Int.mul_one]
omega
/-- **A tie goes away from zero** (negative side): `n / d = −(k + 1/2)`
rounds to `−(k + 1)`. -/
theorem roundHA_tie_neg (n : Int) (d k : Nat) (hd : 0 < d)
(h : 2 * n = -((d : Int) * (2 * k + 1))) :
roundHA n d = -((k : Int) + 1) ∧ (d : Int) * roundHA n d < n := by
have h' : 2 * (-n) = (d : Int) * (2 * k + 1) := by omega
obtain ⟨h1, h2⟩ := roundHA_tie_pos (-n) d k hd h'
rw [roundHA_neg n d hd] at h1 h2
rw [Int.mul_neg] at h2
constructor <;> omega
/-- Rounding is unchanged when the amount and its denominator are scaled
together by a positive factor: the same quotient at a finer denominator
rounds the same. -/
theorem roundHA_scale (n : Int) (d c : Nat) (hc : 0 < c) :
roundHA ((c : Int) * n) (c * d) = roundHA n d := by
unfold roundHA
have hna : ((c : Int) * n).natAbs = c * n.natAbs := by
rw [Int.natAbs_mul, Int.natAbs_natCast]
have hcz : (0 : Int) < c := by omega
rw [hna, roundUnit_scale _ _ _ _ hc]
by_cases hn : 0 ≤ n
· have : 0 ≤ (c : Int) * n := Int.mul_nonneg (by omega) hn
simp only [hn, this, ↓reduceIte]
· have : ¬ 0 ≤ (c : Int) * n := by
have : (c : Int) * n < 0 := Int.mul_neg_of_pos_of_neg hcz (by omega)
omega
simp only [hn, this, ↓reduceIte]
/-! ## The category
The headline theorems: the rounding laws above, on the exact sum of a
category's shares. -/
theorem sumProd_append (l l' : List Share) : sumProd (l ++ l') = sumProd l + sumProd l' := by
induction l with
| nil => simp [sumProd]
| cons s t ih => obtain ⟨b, q⟩ := s; simp only [List.cons_append, sumProd, ih]; omega
theorem sumProd_perm {l l' : List Share} (h : l.Perm l') : sumProd l = sumProd l' := by
induction h with
| nil => rfl
| cons s _ ih => obtain ⟨b, q⟩ := s; simp only [sumProd, ih]
| swap s t l =>
obtain ⟨b, q⟩ := s; obtain ⟨b', q'⟩ := t
simp only [sumProd]; omega
| trans _ _ ih ih' => exact ih.trans ih'
theorem sumProd_neg_books (l : List Share) :
sumProd (l.map fun s => (-s.1, s.2)) = -sumProd l := by
induction l with
| nil => simp [sumProd]
| cons s t ih =>
obtain ⟨b, q⟩ := s
simp only [List.map_cons, sumProd, ih, Int.neg_mul]
omega
theorem sumProd_scale_books (c : Int) (l : List Share) :
sumProd (l.map fun s => (c * s.1, s.2)) = c * sumProd l := by
induction l with
| nil => simp [sumProd]
| cons s t ih =>
obtain ⟨b, q⟩ := s
simp only [List.map_cons, sumProd, ih, Int.mul_add, Int.mul_assoc]
theorem sumProd_scale_percents (c : Int) (l : List Share) :
sumProd (l.map fun s => (s.1, c * s.2)) = c * sumProd l := by
induction l with
| nil => simp [sumProd]
| cons s t ih =>
obtain ⟨b, q⟩ := s
simp only [List.map_cons, sumProd, ih, Int.mul_add, Int.mul_left_comm b c q]
/-- **Near**: the category's deductible amount `c` (in cents) is within
half a cent of the exact sum `N / D`, for any number of accounts and any
sizes: `|2·(D·c − N)| ≤ D`, written without division. -/
theorem near (D : Nat) (l : List Share) (hD : 0 < D) :
-(D : Int) ≤ 2 * ((D : Int) * deductible D l - sumProd l) ∧
2 * ((D : Int) * deductible D l - sumProd l) ≤ D :=
roundHA_near (sumProd l) D hD
/-- **Ties go away from zero** (positive): an exact sum of `k + 1/2` cents
gives `k + 1`, the cent farther from zero. -/
theorem tie_away_pos (D : Nat) (l : List Share) (k : Nat) (hD : 0 < D)
(h : 2 * sumProd l = (D : Int) * (2 * k + 1)) :
deductible D l = k + 1 ∧ sumProd l < (D : Int) * deductible D l :=
roundHA_tie_pos (sumProd l) D k hD h
/-- **Ties go away from zero** (negative): an exact sum of `−(k + 1/2)`
cents gives `−(k + 1)`. -/
theorem tie_away_neg (D : Nat) (l : List Share) (k : Nat) (hD : 0 < D)
(h : 2 * sumProd l = -((D : Int) * (2 * k + 1))) :
deductible D l = -((k : Int) + 1) ∧ (D : Int) * deductible D l < sumProd l :=
roundHA_tie_neg (sumProd l) D k hD h
/-- **Sign**: never the opposite sign to the exact sum, and zero when the
sum is zero. -/
theorem sign (D : Nat) (l : List Share) (hD : 0 < D) :
(0 ≤ sumProd l → 0 ≤ deductible D l) ∧ (sumProd l ≤ 0 → deductible D l ≤ 0) ∧
(sumProd l = 0 → deductible D l = 0) :=
roundHA_sign (sumProd l) D hD
/-- **Odd symmetry**: negating every book negates the result. -/
theorem neg (D : Nat) (l : List Share) (hD : 0 < D) :
deductible D (l.map fun s => (-s.1, s.2)) = -deductible D l := by
unfold deductible
rw [sumProd_neg_books, roundHA_neg _ _ hD]
/-- **Round once, (a)**: the result depends on the shares only through
their exact sum. -/
theorem of_sum (D : Nat) {l l' : List Share} (h : sumProd l = sumProd l') :
deductible D l = deductible D l' := by
unfold deductible; rw [h]
/-- Reordering the accounts does not change the result. -/
theorem of_perm (D : Nat) {l l' : List Share} (h : l.Perm l') :
deductible D l = deductible D l' :=
of_sum D (sumProd_perm h)
/-- Two groups of accounts are rounded together, on their exact sum, not
each alone. -/
theorem append (D : Nat) (l l' : List Share) :
deductible D (l ++ l') = roundHA (sumProd l + sumProd l') D := by
unfold deductible; rw [sumProd_append]
/-- An account's share split in two (its percent as the sum of two) gives
the same result. -/
theorem split_percent (D : Nat) (b q q' : Int) (t : List Share) :
deductible D ((b, q + q') :: t) = deductible D ((b, q) :: (b, q') :: t) := by
apply of_sum
simp only [sumProd, Int.mul_add]
omega
/-- **Round once, (b)**: summing per-account roundings can differ from the
category's one rounding. Two accounts of 1.01 (101 cents) at 50% (shares
over `D = 100`, the books' two places): 1.01 once, 1.02 by account. -/
theorem per_account_differs :
deductible 100 [(101, 50), (101, 50)] = 101 ∧
deductible 100 [(101, 50)] + deductible 100 [(101, 50)] = 102 ∧
deductible 100 [(101, 50), (101, 50)] ≠
deductible 100 [(101, 50)] + deductible 100 [(101, 50)] := by
decide
/-- The kernel's second scenario: 1.05 at 50% and 1.05 at 30% is 0.84
once (0.525 + 0.315 = 0.84), not 0.85 by account (0.53 + 0.32). -/
theorem per_account_differs_mixed :
deductible 100 [(105, 50), (105, 30)] = 84 ∧
deductible 100 [(105, 50)] + deductible 100 [(105, 30)] = 85 := by
decide
/-- The kernel's third scenario: 333.33 at 50% is 166.665, a tie, so
166.67. -/
theorem tie_example : deductible 100 [(33333, 50)] = 16667 := by
decide
/-- **Scaling, books**: writing the books at a finer common denominator
(each book times `c`, `D` times `c`) does not change the result: it is
independent of how many decimal places the books carry. -/
theorem scale_books (D c : Nat) (l : List Share) (hc : 0 < c) :
deductible (c * D) (l.map fun s => ((c : Int) * s.1, s.2)) = deductible D l := by
unfold deductible
rw [sumProd_scale_books, roundHA_scale _ _ _ hc]
/-- **Scaling, percents**: likewise for the percents' decimal places. -/
theorem scale_percents (D c : Nat) (l : List Share) (hc : 0 < c) :
deductible (c * D) (l.map fun s => (s.1, (c : Int) * s.2)) = deductible D l := by
unfold deductible
rw [sumProd_scale_percents, roundHA_scale _ _ _ hc]
end Wanderbooks.Deductible