Elamur GroupantsโŒ•
โ˜ฐ

algorithm-inversion-count

The groupant beside algorithms/src/inversion_count/mod.rs in the Mova repository: what the code computes, what it never does, how to go back from its outputs, its invariants, cost and checks.

Facts

Implementation material. A Mova code model describes this code at one source revision: partial models, finite checks and their stated scope. It is not a reviewed general exposition, and its labels are shown as written, never raised.

Components

componentlabelwhere
approximantpartialForward
astpartialsyntax.json
costconditionalCost
descriptantpresentdescriptant
invariantpartialInvariant
testsbounded executedevidence

Obernants

The typed relations around the forward model: the behaviour complement (what the code never does), the reverse reconstruction (every input compatible with an output) and specialised views.

idtypelabelwhere
behavior_complementbehavior_complementpartial relational specificationbehavior_complement
reverse_reconstructionreverse_reconstructionpartial relational specificationreverse_reconstruction
merge_cross_pairsmerge_cross_pairspartial relational specificationmerge_cross_pairs
input_preservationinput_preservationpartial relational specificationinput_preservation
large_pair_boundlarge_pair_boundpartial relational specificationlarge_pair_bound

Model text

descriptant

inversion_count mod.rs

Epoch 2026-10-30: prospective model selected bounded exact computation before implementation. O(n log n) comparisons, two O(n) usize buffers and O(log n) recursion depth. Word arithmetic exact under cap; generic Ord comparison cost is not assumed constant in wall time.

Retained observation: Tie orientation, residue normalization and first-error order are part of the public behavior, not incidental implementation choices.

The reference attribution is in production module documentation. No copied solution text or implementation is used. No approximate surrogate is useful for the exact API here, and no performance gain is claimed. Partial syntax is generated twice identically; macro bodies remain opaque and semantic AST/proofs are absent. These small files have no nested fragments.

model

inversion_count mod.rs model

Forward

Return I(x)=sum over i<j of indicator(x[i]>x[j]). Recursively sort index halves, merge stably by <=; when the right index wins add the number of unconsumed left indices. Original values remain borrowed and never cloned or written.

Invariant

Each merged index interval is a permutation of its original indices and nondecreasing by value. Each counted cross pair is strict and counted exactly once. 0<=I<=n(n-1)/2, fitting u128 at n<=2^20.

Cost

O(n log n) comparisons, two O(n) usize buffers and O(log n) recursion depth. Word arithmetic exact under cap; generic Ord comparison cost is not assumed constant in wall time.

behavior_complement

Universe: lawful-Ord sequence and candidate count. Counts differing from the strict pair sum are absent; equality contributes zero. Input-changing transitions are absent because only index buffers are written. Over-cap Ok is absent; allocation refusal is a separate result.

reverse_reconstruction

For count k retain every admitted sequence with exactly k strict inversions. [1,2], [7,7], and empty all produce zero; [2,1] and [9,3] both produce one. Length, magnitudes and ties cannot be recovered from count alone. With fixed n fibers remain ambiguous.

merge_cross_pairs

When right[j]<left[i], sortedness implies it is smaller than all remaining left values, adding left.len-i. Equal values choose left and add zero. Quadratic direct pair enumeration independently observes this relation.

input_preservation

Read-only &[T] plus index copies require neither Clone nor mutations of T; a lawful non-Clone Key fixture compiles and a saved vector equals input after counting. Interior mutability or unlawful comparison effects are outside the premise.

large_pair_bound

Descending n distinct values attain n(n-1)/2. At n=2^20 the exact result is 549755289600, beyond u32. A full-cap descending fixture checks this bound and a cap+1 fixture checks rejection.

evidence

Evidence, 2026-10-30

Source SHA-256: 3dfd310fccfd46f5646eed66f903cabe87604ada69ddda02c0e36a0fad985aa2. Twelve focused named tests (three per module) pass twice in debug and twice in release with identical normalized outcomes. Strict all-target Clippy and narrow rustfmt pass. Each production module has a compiled negative control rejected by its exhaustive assertion; test false-oracle controls are separately rejected. Exact source bytes are restored and positives rerun.

Cartesian and inversion enumerate sum(3^n,n=0..8)=9841 vectors each; subset files enumerate 3+9+81+6561=6654 vectors at five moduli, 33270 comparisons each. Cap fixtures dominate memory/time; no benchmark claim.

Raw logs are external. Bound evidence hashes:

No whole-repository test, allocator injection, general proof, live parser/teacher/GPU, approximation-error or runtime improvement claim.

This page in Markdown โ†’ https://md.groupants.elamur.ai/free/en/mova/algorithms/src/inversion_count/mod.rs.groupant/