merge: a tie between shares goes to the earlier part, exactly (K-U10b, D-76)

The rule (BS-partner-1, bookspec's Apportion) is that leftover cents go
to the largest remainders, ties to the earlier part. The engine ranked
the difference of quotients rounded at Decimal's 28th digit, so an exact
tie went where that digit sent it: 20.00 by 4/1/1 came out 13.33 / 3.34 /
3.33. It now ranks the exact remainder, whole x weight - part x total,
and gives 13.34 / 3.33 / 3.33. The owner decided for the exact ranking on
2026-11-09 (D-76, recorded here).

What moves: no golden, nothing between two parts or by equal weights, at
most a cent a part elsewhere (tests/kernel_bridge/ties.rs measures it).
Of the engine's splits only the K-1 worksheet's share of contributed
property divides by a total that is not one; a convention's split and the
frozen close rule already ranked exactly. Posted splits are not
recomputed.

Changed in the merge:
- The ranking's two products are the rounding ones (checked_mul_rounded).
  The kernel's checked_mul is exact or refused since K-U10a, and would
  refuse a split by weights of 27 places; the split is made, ranked by
  the nearest figures Decimal holds. A test splits by thirds, sevenths,
  ninths and elevenths at 27 and 28 places, both signs.
- Kani failed the unit as it came: with the exact ranking a part whose
  share is exact never takes a leftover unit (5 by 2/3/5 is 1, 2, 2, where
  i64's truncated quotients gave 2, 1, 2), so the cover "a part exactly
  one step above its truncated share" could not be reached. The harness
  split_by_weights_within_one_step now asserts the strict bound and that
  an exact share takes no unit; both covers are reached.
- docs/verification.md also takes K-U10a's account (sums and products
  exact or refused), which closes the edges it listed as pinned.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude committed 2026-11-09 22:39:13 -07:00
commit 5e9591725a
9 files changed
+613 -149

No files matched your search

+38 -27
View File
@@ -73,12 +73,14 @@ split when the whole divides), the step is 1 (or 10 for the floor
harness), and the bounds are `whole` 0 to 1,000 (100 for the floor
harness). The laws are about the shape of the split, not the magnitude;
Lean proves them of the exact-share model at any size, and the bridge
checks the `Decimal` engine, whose ranking uses the rounded quotient.
checks the `Decimal` engine, whose ranking is the exact remainder (K-U10:
`whole × weight − part × total`, ties to the earlier part, the same
quantity `share_floor_with` returns).
| Harness | Law | Verification |
|---|---|---|
| `split_by_weights_sums` | 18 | the parts sum to the whole exactly; a shortfall is handed out; the largest weight takes more. 23.9 s (2 covers) |
| `split_by_weights_within_one_step` | 42 | each part is above `-total` and at most `total` from its share, scaled by the total; a part off its share and a part exactly one step above are reached. 14.4 s (2 covers) |
| `split_by_weights_within_one_step` | 42 | each part is above `-total` and below `total` from its share, scaled by the total, and a part whose share is exact takes no unit; a part off its share, and a unit handed out while the first part's share is exact, are reached. 22 s (2 covers) |
| `split_by_weights_no_part_exceeds` | 18 | no part is negative or above the whole. 12.4 s (1 cover) |
| `split_by_weights_first_absorbs` | 18 | the first part is its rounded share plus the shortfall; the sum is the whole; no part exceeds it. 2.3 s (1 cover) |
| `split_by_weights_last_absorbs` | 18 | the last part is the whole less the others; the sum is the whole; no part exceeds it. 4.3 s (1 cover) |
@@ -86,13 +88,16 @@ checks the `Decimal` engine, whose ranking uses the rounded quotient.
| `share_of_refuses_overflow` | 42 | `share_of(whole, 2, 1) = whole × 2` exactly when the product fits; a zero total and an overflowing product are `None`, never a panic. 0.47 s (2 covers) |
| `share_floor_is_the_exact_floor` | 42 | the part is a multiple of the step, one more step passes the share, and the remainder is exact below `step × total`; the correction down is reached. 5.1 s (2 covers) |
The upper bound of `split_by_weights_within_one_step` is inclusive on
the `i64` model: its division truncates, so a part that divides exactly
can still take a unit when another part's truncation left one over
(`whole = 5`, weights 2/3/5: the first's 1 becomes 2, exactly one step
above its truncated share). The `Decimal` quotient keeps the fraction, a
part with no remainder never takes a unit, and the bridge checks the
strict bound there.
The upper bound of `split_by_weights_within_one_step` was inclusive on
the `i64` model until K-U10: `i64`'s division truncates, the ranking read
the quotient, and a part that divided exactly could take the unit another
part's truncation left over (`whole = 5`, weights 2/3/5: the first's 1
became 2). The ranking is now the exact remainder, which no division
touches, so the split is 1, 2, 2 and the bound is strict on `i64` as on
`Decimal`. Kani said so itself: with the exact ranking the old cover "a
part exactly one step above its truncated share" could not be reached,
and `just prove` fails on a cover that cannot. The harness now asserts
the strict bound and that an exact share takes no unit.
`share_floor_is_the_exact_floor` needed `#[kani::unwind(4)]`: without a
bound on the correction loops, CBMC tried to unwind them without end and
@@ -143,10 +148,9 @@ harness 500.8 s, the run inside its 3600-second cap). Lean:
no-rounding split, at the scales the call sites produce.
- `decimal_edges_are_none_or_exact`: `Decimal::MAX`, a product past it, a
zero total, and scale 28: each `None` or exact, never a panic.
- `largest_remainder_ranks_like_the_exact_reference`: random distinct
weights over a non-terminating total (a factor of 3 or 7), against the
exact `i128` reference's ranking; exact ties are pinned below, not left
to fail this property.
- `largest_remainder_ranks_like_the_exact_reference`: random weights (ties
included, since K-U10) at whole units and at the cent, against the exact
`i128` reference's ranking.
- `the_absorbing_parts_keep_the_old_scale`: the strings, scale included,
for 4860 by 6/0.7/0.5 (first `4050`, as the old `split_first` gave) and
902 by 1/0.000001/0.000001 (first `902.00`); last absorbs against the
@@ -154,9 +158,10 @@ harness 500.8 s, the run inside its 3600-second cap). Lean:
- `split_into_matches_the_allocating_split`: the fixed-array
`split_by_weights_into` and `split_by_weights_with` agree, scale
included (the gate requires a bridge that calls the fixed-array form).
- `finding_ties_follow_the_rounded_quotient`: 20.00 split 4/1/1 over 6 is
13.33/3.34/3.33, and 20 whole is 13/4/3, not the exact tie's earlier
part (K-U10 decides).
- `ties_go_to_the_earlier_part`: 20.00 split 4/1/1 over 6 is
13.34/3.33/3.33, and 20 whole is 14/3/3 (K-U10: the ranking is exact;
the rounded quotient gave 13.33/3.34/3.33 and 13/4/3). A negative total
keeps the share's own order.
- `share_floor_refuses_past_the_correction_cap`: the review's
0.1113 × 5909471247797156928749568911.8 ÷ 0.11134381908633486049673
returns `None` promptly instead of hanging.
@@ -208,9 +213,9 @@ the `allocW_*` theorems were reused, not restated.
`split` at ~684 is not arithmetic (it reads `k1_detail` rows) and
needed no routing.
2. **The `i64` model's division truncates.** The Kani within-one-step
bound is therefore inclusive on `i64`; the `Decimal` bridge checks the
strict bound for the engine. This is a model statement, not an engine
change.
bound was therefore inclusive on `i64` while the ranking read the
quotient; since K-U10 ranks the exact remainder it is strict there
too (above).
3. **K-U10, Decimal's edges.** At `Decimal`'s 96-bit mantissa edge,
`checked_div`'s rounding can move a share across a step, and
`checked_mul`'s rounding hides it from `share_floor_with`'s correction.
@@ -249,11 +254,17 @@ the `allocW_*` theorems were reused, not restated.
checked arithmetic), and `the_absorbing_parts_keep_the_old_scale`
compares strings. `LastAbsorbs` was already the old `plug_last` and the
no-places conventions split, and the same property checks it.
7. **Ties follow the rounded quotient.** `split_by_weights_into` ranks the
rounded quotients' remainders, not exact rationals, so an exact tie goes
where the 28th digit sends it, not to the earlier part:
20.00 split 4/1/1 over 6 is `13.33, 3.34, 3.33` (exact ranking would be
`13.34, 3.33, 3.33`), and 20 whole is `13, 4, 3` (exact would be
`14, 3, 3`). The pre-U3 sites did the same. Pinned by
`finding_ties_follow_the_rounded_quotient`; the owner decides the exact
ranking in K-U10, and the ranking is unchanged here.
7. **Ties go to the earlier part, exactly (K-U10).** The first version of
`split_by_weights_into` (and the pre-U3 call sites) ranked the leftover
units by the rounded quotients' remainders, so an exact tie went where
the quotient's 28th digit sent it: 20.00 split 4/1/1 over 6 was
`13.33, 3.34, 3.33` and 20 whole was `13, 4, 3`. It now ranks the exact
integer remainder `whole × weight − part × total` (the quantity
`share_floor_with` returns; the sign flipped for a negative total), so
a tie goes to the earlier part, as `apportion`'s docs and bookspec's
`Apportion.lean` (`remW`, `allocNat`: ties to the earlier member) say:
`13.34, 3.33, 3.33` and `14, 3, 3`. Pinned by
`ties_go_to_the_earlier_part`; `tests/kernel_bridge/ties.rs` keeps the
old ranking as a reference and measures what the change moves. A
recomputed past report can differ from what it showed before by a cent
between parties (a posted entry never moves).
+58 -28
View File
@@ -892,7 +892,7 @@ replay rules.
`largest_remainder_ranks_like_the_exact_reference`,
`the_absorbing_parts_keep_the_old_scale`,
`split_into_matches_the_allocating_split`,
`finding_ties_follow_the_rounded_quotient`,
`ties_go_to_the_earlier_part`,
`share_floor_refuses_past_the_correction_cap`,
`finding_the_quotients_rounding_can_cross_a_step`
- `split_by_weights_with` is an `EXCEPTIONS` wrapper (it allocates two
@@ -988,20 +988,22 @@ kernel. What the proofs and their reviews found, beyond the tidying:
decimal places, before computing and never on replay (D-75,
`ValueOutOfRange`); `past_ten_places_the_reference_and_decimal_part_and_the_engine_refuses`
pins it.
- **The 96-bit half of that precondition is not enforced.** Ten places
- **The 96-bit half of that precondition was not enforced.** Ten places
each still allow a product of twenty places past about 7.9e8, which
needs more than 96 bits: `1050000010.0000000001 × 9.9999999999` is
`10500000099.89499999999999999999` exactly (10500000099.89 to the cent)
but `Decimal` holds 10500000099.895 and the engine books
10500000099.90. `the_mantissa_edge_is_outside_the_precondition_and_pinned`
pins it; refusing it is the owner's decision (K-U10).
- **`Decimal`'s addition is not exact in the top unit of `MAX`.**
but `Decimal` held 10500000099.895 and the engine booked
10500000099.90. Closed by K-U10: the kernel holds the product in 256
bits and rounds it once, so the line books 10500000099.89
(`the_mantissa_edge_books_the_exact_products_cent`). A line already
posted is not recomputed: replay reads the booked amount.
- **`Decimal`'s addition was not exact in the top unit of `MAX`.**
`Decimal::MAX.checked_add(0.4)` is `Some(MAX)`: `rust_decimal` rounds
sub-unit dust when the sum needs a 97th bit (`MAX + 0.5` is `None`), so
the kernel's "exact or `None`" contract fails there for every function
the kernel's "exact or `None`" contract failed there for every function
that adds, not only `total`. No posted line reaches it (the line cap is
10^15); `finding_decimal_add_rounds_dust_at_the_edge` and
`decimal_edge_table` pin it, and it is K-U10's to settle.
10^15). Closed by K-U10: the kernel's sum is the exact one or `None`
(`a_sum_past_exact_arithmetic_is_refused`, `decimal_edge_table`).
- **A share's quotient can cross a step, and its product's rounding can
hide it.** At `Decimal`'s 96-bit mantissa edge, `checked_div`'s rounding
can move a share across a step and `checked_mul`'s rounding hides that
@@ -1014,7 +1016,12 @@ kernel. What the proofs and their reviews found, beyond the tidying:
division carries the same edge: `MAX / 2` rounds before the step, so the
part is not within half a step of the exact quotient (`the_edges` pins
it); the engine's callers refuse totals of `EXACT_LIMIT` (10^26) or
more, so no schedule reaches it. Both are K-U10 items.
more, so no schedule reaches it. K-U10 settled the products: a product
that is the amount itself (`checked_mul`) is exact or refused, so the
square above is `None`; a share's product keeps rounding, under its own
name (`checked_mul_rounded`), since a share by a percent of 27 places
is a rounded figure by construction and is divided next. The division
(`checked_div`) is the engine's rounding division and is unchanged.
- **One panic became a refusal.** `k1::share_property`'s old
`apportion(...).expect(...)` would panic at that edge; the kernel
returns `None` and the site reports `TooLargeToCompute`.
@@ -1031,13 +1038,33 @@ kernel. What the proofs and their reviews found, beyond the tidying:
337.50`, the kernel `4050.00, …`), and that scale reaches `Part.amount`.
The kernel computes the old expression, pinned by
`the_absorbing_parts_keep_the_old_scale`.
- **Ties follow the rounded quotient.** The split ranks the rounded
quotients' remainders, not exact rationals, so an exact tie goes where
the 28th digit sends it: 20.00 split 4/1/1 over 6 is `13.33, 3.34,
3.33` (exact ranking `13.34, 3.33, 3.33`), and 20 whole is `13, 4, 3`
(exact `14, 3, 3`). The pre-sweep sites did the same;
`finding_ties_follow_the_rounded_quotient` pins it, and the owner decides
the exact ranking in K-U10, unchanged here.
- **Ties go to the earlier part, exactly (K-U10).** The split ranked the
rounded quotients' remainders, so an exact tie went where the 28th digit
sent it: 20.00 split 4/1/1 over 6 was `13.33, 3.34, 3.33` and 20 whole
was `13, 4, 3`. The pre-sweep sites did the same, and K-U3 kept it and
pinned it. It now ranks the exact remainder `whole x weight - part x
total` (as `share_floor_with` does), so a tie goes to the earlier part:
`13.34, 3.33, 3.33` and `14, 3, 3`, which is what bookspec's
`Apportion.lean` models (`remW`, ties to the earlier member).
`ties_go_to_the_earlier_part` pins it, and `tests/kernel_bridge/ties.rs`
measures what it moves against the old ranking: nothing between two
parts or by equal weights, at most a cent a part elsewhere. The owner
decided for the exact ranking (D-76, 2026-11-09). Kani then found the
same on whole numbers: with the exact ranking a part whose share is
exact can no longer take a leftover unit (5 by 2/3/5 is 1, 2, 2, where
the truncated quotients gave 2, 1, 2), so a cover in
`split_by_weights_within_one_step` could not be reached; the harness
now asserts the strict bound and that an exact share takes no unit.
- **`Decimal`'s sums and products are exact or refused (K-U10).**
`rust_decimal` rounds a sum or product it cannot hold and returns
`Some`; the kernel's `Amount for Decimal` now checks each result in 256
bits and refuses a rounded one. `checked_div_exact` accepted a rounded
quotient that multiplied back (`10 / 3`), found in review of the unit;
it refuses them. An independent review then found the bridge
properties proved a returned figure exact, not that an exact figure is
never refused; they now prove both, from the rational alone. One
refusal remains by design: a product that is not exact, rounded to a
step of 27 or 28 places. `docs/proofs/k-u10.md` has the account.
- **The sweep changed no posted figure.** Every golden and existing test
passed unchanged. `round_places` reproduces `round_dp_with_strategy` to
the character except a negative-zero input (which no call site can
@@ -1092,16 +1119,19 @@ Be precise about the claims:
checked; the sweep's bridge properties compare the engine to an
independent exact `i128` reference for every step, count and amount the
`Decimal` type can hold.
- **The sweep's `Decimal` edges are pinned, not refused.** `checked_mul`
rounds at 96 bits (`fx`'s `the_mantissa_edge_is_outside_the_precondition_and_pinned`
and `123456789.0123456789²`), `checked_div` rounds at 28 significant
digits (K-U3's `finding_the_quotients_rounding_can_cross_a_step`, K-U5's
`the_edges` at `Decimal::MAX / 2`), and `checked_add` rounds sub-unit
dust in the top unit of `MAX` (K-U4's
`finding_decimal_add_rounds_dust_at_the_edge`). None is reachable by a
posted line (the line cap is 10^15, and schedules refuse totals of
`EXACT_LIMIT` or more), so the laws hold at the engine's inputs; whether
the kernel should refuse these shapes outright is K-U10's decision.
- **The sweep's `Decimal` sums and products are refused at their edges;
its divisions are pinned.** Since K-U10 the kernel's `checked_add`,
`checked_sub` and `checked_mul` for `Decimal` are exact or `None`
(`tests/kernel_bridge/decimal_exact.rs`: `Some` exactly when the exact
result is a `Decimal`), and a foreign amount at a rate is rounded once
from the exact product. Two operations round, each by name:
`checked_div` at 28 significant digits (K-U3's
`finding_the_quotients_rounding_can_cross_a_step`, K-U5's `the_edges`
at `Decimal::MAX / 2`) and a share's `checked_mul_rounded`. Neither
edge is reachable by a posted line (the line cap is 10^15, and
schedules refuse totals of `EXACT_LIMIT` or more), so the laws hold at
the engine's inputs. Not proved by Kani: the 256-bit helpers, which
rest on the bridge properties and unit tests against `num_bigint`.
- **The FX overflow harness proves two concrete cases, not every
overflow.** `fx_booked_refuses_overflow` proves `i64::MAX × 2` and
`100 × i64::MAX` (step 1) are `None`, and that a non-positive rate is
@@ -1399,7 +1429,7 @@ included):
| `fx_booked_positive_for_positive` | 41 | prove | 3.2 |
| `fx_booked_refuses_overflow` | 41 | prove | 2.6 |
| `split_by_weights_sums` | 18 | prove | 23.9 |
| `split_by_weights_within_one_step` | 42 | prove | 14.4 |
| `split_by_weights_within_one_step` | 42 | prove | 22 |
| `split_by_weights_no_part_exceeds` | 18 | prove | 12.4 |
| `split_by_weights_first_absorbs` | 18 | prove | 2.3 |
| `split_by_weights_last_absorbs` | 18 | prove | 4.3 |