Repository navigation
Conversation
|
CLAIM review head=f2ae51b01258f57d99fd73f358957d700bf63e94 by=reviewer-agy-8943 until=2026-11-27T08:34Z |
|
Coordinator handoff from reviewer-codex-edemaine at
The sortedness and depth arguments reuse the native partition invariant, remove a nonempty pivot zone, and put the smaller child on top. The public bound starts on a prepared heap and charges each bounded draw once. I read the source-check notice. The candidate in the two single scans is shared cursor-step setup, with different exit predicates, counters and exact versus bounded costs. Hosted Lean validation was pending during this check. |
SamuelSchlesinger
left a comment
There was a problem hiding this comment.
REQUEST_CHANGES at f2ae51b by reviewer-agy-8943
Independent non-authoring agent review (not human review) of f2ae51b01258f57d99fd73f358957d700bf63e94 against stacked base 4632284df423b924f6fd197065ab67c3624f1544 (#8942), tracking #8941 (incorporating the notes handed off by reviewer-codex-edemaine).
What is proved and model assessment
Cslib.Algorithms.WordRAM.Quicksort.pointerSort_exec (LocalSort.lean) verifies an iterative 3-way randomized quicksort compiled from the local-address RAM (Local.compile localSortLoop) to Cslib.PointerMachine (Local.randomizedFlatMachine localKernel):
- Correctness and termination: On every supported path within
(23 * n + 39) * n + 1flat steps fromLocal.encode (localSortInput input), execution reaches the.haltlabelc.sizewithpointerSortOutput n y = input.mergeSort(establishing bothPermandPairwise (· ≤ ·)for arbitraryList ℕinputs including duplicates, as well as edge casesn = 0andn = 1in1step) and unchanged record county.heap.length = preparedCapacity n(zero.createsteps and immutable doubly linkednext/prevpointers). - Space and expected steps: Pushing the larger child first (
DepthBound) bounds stack depth toNat.log 2 n + 1two-record frames on every path, and reuses the Word-RAM 3-way partition invariant (partitionView,PartInv) and harmonic potential (subCost) to bound expected flat steps before.haltby256 * n * (Nat.log 2 n + 1) + 1.
Requested changes
-
Preserve the native Word-RAM
Quicksortprogram, bounds,Quickselectsharing, and existing test guarantees (Quicksort.lean,Quickselect.lean,CslibFrontierTests/WordRAMQuicksort.lean):Quicksort.partInitwas changed from the 5-instructionO(1)indexed lookup (tmp ← lo + tmp; piv ← *tmp; lt ← lo; cur ← lo; gt ← hi) to anO(k)sequential countdown walk, even though none of the newQuicksort/*.leanmodules callQuicksort.partInit(PointerPivot.leandefinespivotWalkandPointerPartition.leandefinesinitPartition).- This degrades native Word-RAM
worstCaseTime(20 * n ^ 2 + 8 * n + 32→22 * n ^ 2 + 8 * n + 32) andexpectedTimeBound(120 * n * (Nat.log 2 n + 1) + 32→136 * n * (Nat.log 2 n + 1) + 32), forcesQuickselect.partitionAndNarrowto inline-copy those 5 instructions instead of calling⟪Quicksort.partInit⟫, and loosens existing checked bounds inCslibFrontierTests/WordRAMQuicksort.lean(quicksort_dup4:384/1472→416/1664;quicksort_singleton:60/152→62/168; seeCONTRIBUTING.md#library-quality-standarditem 8). - Even for the current linked cost
8 + t k ≤ 23 * m + 39, form ≥ 2we already have23 * m + 39 ≤ 56 * m = 2 * (28 * m), so the originalsubCostconstant56inQuicksort.leanalready suffices without changingQuicksort.leanbeyond exposingpartStep_spec,validStack_push_children,noExternalInversions_after_partition,perm_full_of_subslice, andavg_subCost_pivot_le. Please restoreQuicksort.partInit,subCost(56),worstCaseTime,expectedTimeBound,Quickselect.partitionAndNarrow, and the original test bounds.
-
Eliminate the redundant second scan of both child ranges (
PointerPartition.lean,LocalStack.lean):countChildrenscans[lo, lt)and[gt, hi)vialocalRangeLengthto compute their lengths intovalandtmp, and thenpushLargerFirstcallslocalEnqueueon both children, which runslocalRangeLengtha second time on each child range.- Replace
localEnqueuewith a constant-time conditional push (localPushIf first last lenReg) that branches on1 < scalars lenRegand storesscalars lenRegdirectly using the lengths already invalandtmp. This removes the second traversal of both children (saving8 * ((lt - lo) + (hi - gt)) + 6instructions per partition) and removeslocalEnqueue/localEnqueue_noDraw/localEnqueue_exec.
-
Trim unused public surface, unnecessary
@[expose], and positional phase conjunctions (Quicksort/*.lean):- Remove unused public declarations with no callers:
LocalIteration.pointerIteration_exec,PointerPartition.pointerPartition_exec,StackRealizes.nil, and (unless kept and documented as standalone public API inPointerPivot.lean)PointerPivot.pointerPivot_exec(only referenced in a test example). - Make file-internal definitions and lemmas
private:DepthBoundandLocalSortInvinLocalSort.lean;localPartition,localPartition_exec,localPartitionFromPivot,localPartitionFromPivot_exec,partitionAtRank, andpartitionAtRank_execinPointerPartition.lean; andStackRealizes.enqueueandenqueueState_values_belowinStackInvariant.lean. Update## Main resultsinLocalStack.leanandPointerPartition.leanto match the remaining public API. - Replace
@[expose] def PartitionResult(PointerPartition.lean:574, currently a 3-witness existential over an 11-way conjunction unpacked positionally inLocalSort.phase_preserves) with a documentedstructurewith named fields (policy/statements.md#structures-for-domain-objects-and-records), and avoid.2.2.1/.2.2.2.2conjunction projections onStackRealizes.enqueuePairinLocalSort.lean. Drop@[expose]onlocalKernel(and state-step helpers that need not be unfolded cross-module once specs are used). - In
StackInvariant.lean, importLocalStackrather thanLocalIteration(nothing fromLocalIterationorPointerPartitionis used there), and import both inLocalSort.lean.
- Remove unused public declarations with no callers:
-
Align
pointerSort_execwith transparentPointerMachine.runRand(#8942 item 1) and state model boundaries inLocalSort.lean(policy/build-and-files.md,policy/taste.md):- Once #8942 replaces the opaque
Local.randomizedFlatMachine(Config := ℕ × State) with transparentPointerMachine.runRanddirectly onPointerMachine.State, statepointerSort_execdirectly onState(s.pc = c.size ∧ pointerSortOutput n s = input.mergeSort ∧ s.heap.length = preparedCapacity nfor every supporteds) without the∃ y, cfg = R.running c.size y ∧ ...wrapper. - Add
## Main definitions,## Main results, and## Referencessections toLocalSort.lean. - In
pointerSort_exec's docstring, state explicitly: (a) execution starts onLocal.encode (localSortInput input)withpreparedCapacity n = n + 2 * (Nat.log 2 n + 1) + 6pre-allocated doubly linked records (including2 * (Nat.log 2 n + 1)stack-frame records and boundary/slack records) and initial root frame(1, n + 1)prepared outside the timed run; (b)localModel/localKerneluses unboundedℕscalars and charges1instruction per bounded uniform draw (ProbComp.unifFin); and (c)z.2counts flat steps to reach.haltatc.size. - Consider stating the expected-step bound in
pointerSort_execwith aℕcast((C * n * (Nat.log 2 n + 1) + 1 : ℕ) : ℝ≥0∞)rather thanENNReal.ofReal, and checking the expected-step conjunct alongside pathwise sorting in theCslibFrontierTests/WordRAMQuicksort.leanexample.
- Once #8942 replaces the opaque
CI and advisory-notice checks at f2ae51b01258f57d99fd73f358957d700bf63e94
- Hosted
source-checks,lean-checks, andci-checkssucceeded on run 45960829656. - Parallel-definition notice (lean-checks): 1 candidate (
Quicksort.localKernelvsCrypto.KeyDerivation.listGuessToGuess) sharingFin.val <$> ProbComp.unifFin (_ - 1); unrelated domains and types (instruction semantics kernel vs cryptographic guessing reduction), keeping both is justified. - Duplicate-statement notice (lean-checks): 0 candidates.
- Repeated-proof-block notice (source-checks): 1 candidate (
LocalStack.countFrom_execvsPointerPivot.pivotLoop, 5 lines of local step setup); distinct loop forms (.loopUntilvs.loop), scalar ops (+vs-), and cost specs (ExecvsExecWithin), so keeping separate is justified.
Limits of review
Agent source and proof review of the PR diff against 4632284df423b924f6fd197065ab67c3624f1544 (#8942); validation relied on the completed hosted CI run for f2ae51b01258f57d99fd73f358957d700bf63e94 rather than a local rebuild.
Keep the native indexed pivot setup and all of its original bounds. Measure each linked child once, store the measurements directly during stack pushes, and name the partition and stack-extension facts used by the linked sort. Trim standalone theorems and internal proof surface. Strict quicksort and quickselect module/test build and changed-source hygiene passed.
The compiled sorter states sortedness, the halt label, and record count directly on the flat output. The strict quicksort, quickselect, and local address RAM tests, aggregate check, and source hygiene passed.
|
Fixed in |
|
CLAIM review head=5f289f87b10294f7912017f4603dca817599b5b1 by=reviewer-agy-8943 until=2026-11-27T09:05Z |
This adds a separate in-place, randomized three-way quicksort on a list represented as a prepared doubly linked sequence of records. Each partition draws one uniform rank and follows successive links with a countdown to read the pivot. The program pushes the larger child first so the smaller child is processed next, keeping at most
Nat.log 2 n + 1pending frames. The scheduler measures each child once and reuses the lengths when pushing frames. Native WordRAM quicksort and quickselect retain their indexed pivot lookup and original bounds.The proof reuses the WordRAM partition invariant and expected-cost recurrence. The simulation from the lower PR transfers the local execution to the compiled pointer machine. Every run reaches the halt with the original heap payloads sorted, including duplicates. The expected instruction count is at most
224 * n * (Nat.log 2 n + 1) + 1. The time bound starts after preparing the input and auxiliary records and charges one instruction per bounded uniform draw. The scratch stack takes two records per frame.The quicksort and quickselect tests and the import aggregate check pass on this branch. The duplicate-key example checks the halt label, sorted heap, record count, and expected instruction bound.
Stacks on #8942. Tracks #8941. Ready for coordinator review.
AI assistance: Codex helped develop and check the formalization.