# Counting contract

## Weighted weak rankings

Candidates are unique nonempty IDs, canonicalized by lexical ordering. A ballot has a positive integer weight and rank groups. IDs in one group are tied; all unranked candidates tie below every ranked candidate. A candidate cannot occur twice. An empty ranking is blank, but an empty inner rank group is invalid. Pairwise D[a,b] sums weights strictly preferring a to b; indifferent ballots contribute to neither direction. Pairwise diagonal is zero. Weighted identical ballots may be aggregated without changing results.

At most 32 candidates, 10,000 weighted ballot types, total weight <=1,000,000,000. All sums and margins fit signed Int32 on every backend. Public getters copy nested arrays; external mutation cannot change an election.

## Pairwise methods

Condorcet requires strict victory over every other candidate. Schulze uses WINNING VOTES: direct strength is D[a,b] when D[a,b]>D[b,a], otherwise zero. Widest paths use max/min Floyd-Warshall. All unbeaten candidates are returned; no single-winner tie-break is silently applied. Witness paths use only strict winning edges and have bottleneck equal to their reported strength.

Ranked Pairs sorts strict defeats by margin descending, then winning votes descending, then lexical winner ID, then lexical loser ID. Each edge is locked unless the current graph already contains a reverse path; rejected edges include that existing reverse path. All source vertices are returned. Equal-strength priorities are a PROJECT VARIANT, not every possible resolution of Ranked Pairs. The fixed lexical priority is transparent and can affect outcomes. A rejection path excludes the proposed edge; add that edge to close the cycle.

## IRV

IRV requires strict ranks and <=16 candidates. Exhausted ballots are excluded from the continuing majority denominator, but reported each round. A majority means count > floor(continuing/2). Without tie_order an elimination tie stops with needs_tiebreak=true and no asserted winner. A supplied tie_order must be a full candidate permutation, earliest elimination preference first. Only one lowest candidate is eliminated per round.

irv_possibilities explores every legal choice among tied LOWEST candidates, memoizing active subsets; every returned winner has one replayable elimination sequence. This is parallel-universe tie resolution for this IRV rule. State budget 1..65,535; exceeding it raises Limit, never a partial winner set. A round with zero continuing votes produces no winner, even if candidates remain. Blank elections produce no winners for all methods. No random ballot tie-break, STV, fractional transfers or jurisdiction-specific rule set is implied.

## Sources and ecosystem distinction

- Markus Schulze, *The Schulze Method of Voting*: https://arxiv.org/abs/1804.02973 . Reference for winning-vote strongest paths. This is original MoonBit implementation; no third-party code was copied.
- Algorithms are classical, not claimed as newly invented. Exact implemented variants are specified above, so users need not assume an unspecified jurisdiction or rulebook.
- GitHub metadata searches on 2026-11-01 for MoonBit voting/ranked choice/apportionment found no direct counterpart. `hiroyannnn/yuru-poll` describes AI interpretation of free-text polling; MoonTally instead takes explicit ranked ballots and deterministically returns mathematically checkable counting evidence, with no model/API dependency. Search coverage is not a uniqueness guarantee.
