Elamur GroupantsโŒ•
โ˜ฐ

algorithm-cartesian-tree

The groupant beside algorithms/src/cartesian_tree/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
tie_orientationtie_orientationpartial relational specificationtie_orientation
stack_amortizationstack_amortizationpartial relational specificationstack_amortization
allocation_partitionallocation_partitionpartial relational specificationallocation_partition

Model text

descriptant

cartesian_tree mod.rs

Epoch 2026-10-30: prospective model selected bounded exact computation before implementation. At most n pushes and n pops, O(n) lawful-Ord comparisons and O(n) storage: three link vectors and one stack. Comparison cost is external to this count; no timing gain is asserted.

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

cartesian_tree mod.rs model

Forward

For each interval [lo,hi), choose the least index r attaining its minimum, recursively attach [lo,r) left and (r,hi) right. Empty interval gives None. The stack implements this tree: pop strictly greater suffixes, attach the last popped root as the new left child, then replace the surviving top's right child. Return explicit root/parent/left/right indices.

Invariant

Inorder is exactly 0..n. Each nonroot has one reciprocal parent link, every child has value >= its parent, and equal-value ancestry selects the smaller index first. Stack indices increase and values do not decrease. n<=2^20.

Cost

At most n pushes and n pops, O(n) lawful-Ord comparisons and O(n) storage: three link vectors and one stack. Comparison cost is external to this count; no timing gain is asserted.

behavior_complement

Universe: admitted input sequences paired with candidate four-vector trees. Absent outputs include cycles, missing indices, nonreciprocal links, roots other than the leftmost global minimum, and any subtree root differing from that interval's leftmost minimum. These recursive interval constraints characterize the unique tree on lawful Ord. Over-cap input paired with Ok is absent; allocator refusal may yield AllocationFailed.

reverse_reconstruction

Given an observed tree retain all sequences whose leftmost-minimum recursive decomposition is that tree. Monotone value embeddings preserve the tree, so [1,2] and [8,9] share a right chain; [1,1] does too. Shape loses magnitudes and some equality information. Empty output has length zero; errors retain broad input/allocator fibers.

tie_orientation

Strict greater-than popping preserves equal predecessors. For all-equal n>0 the root is 0 and right[i]=i+1 until the last node. Replacing > with >= changes this fiber and is rejected by the recursive oracle.

stack_amortization

Potential Phi=stack.len: each pop lowers Phi by one, each input pushes once. Total pops <=n, although one step may pop n-1. The full-length equal chain test exercises heap vectors without recursive production traversal.

allocation_partition

TooLong has priority over all allocation attempts. Within cap three link vectors and stack use try_reserve_exact; later refusal drops local allocations and leaves caller input untouched. Allocation refusal is modeled but not injected.

evidence

Evidence, 2026-10-30

Source SHA-256: cba32da7416cb71429b8d492de8f57fd8d116bfdc9ea30c0922264478e4d0dd9. 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/cartesian_tree/mod.rs.groupant/