Skip to content

feat(WordRAM/PriorityQueue): priority queue with handles (MinPQ) and key–value FibonacciHeap with O(log n + 1) amortized extractMin - #1687

Merged
SamuelSchlesinger merged 19 commits into
cslib-community:mainfrom
orbitingflea:wordram-fibonacci-heap-handles
Oct 30, 2026
Merged

SamuelSchlesinger merged 19 commits into
cslib-community:mainfrom
orbitingflea:wordram-fibonacci-heap-handles

Conversation

@orbitingflea

@orbitingflea orbitingflea commented Oct 29, 2026 •

Copy link
Copy Markdown
Collaborator

Part of #1633. Parent #1640 and the M1 interfaces have landed. The supported-pin merge at 006fbd490de82536cced2d3dc764b3779549d00b preserves the reviewed ten-file heap layer and all original credit.

This PR defines the relational specification Cslib.Algorithms.Lean.MinPQ of a priority queue of handle capacity n_max storing key–value pairs (u, v) below key bound U (u < U) and value bound V (v < V). It realizes MinPQ on the word RAM via Cslib.Algorithms.WordRAM.PriorityQueue.FibonacciHeap.MinPQ in 11 * n_max + 1 words over 48 registers, and tightens the degree-table consolidation bound of FibonacciHeap from capacity n_max to the current number of stored elements n. Fresh initialization on zeroed memory takes O(1) instructions, and reusable initialization on arbitrary memory takes O(n_max + 1) instructions. Read-only queries (isEmpty, get) take O(1) worst-case instructions, insert and decreaseKey take O(1) amortized instructions, and extractMin takes O(log n + 1) amortized instructions in the pre-extraction element count n ([FredmanTarjan1987], Section 2, p. 604, potential before Theorem 1, Lemma 1, Corollary 1, and Theorem 1; [CLRS2009], Sections 19.1–19.4, eq. (19.1) and Lemma 19.4).

Cslib.Algorithms.Lean.MinPQ is added alongside IndexedMinPQ because IndexedMinPQ fixes caller-chosen vertex indices Fin n with deterministic (key, vertex) tie-breaking for graph algorithms such as Dijkstra, whereas MinPQ models handles Fin n_max allocated by the data structure and relational tie-breaking across equal keys. FibonacciHeap.MinPQ reuses the existing FibonacciHeap tree, forest, cut, and consolidation proofs by framing the inner heap invariant (FibInv.supported) across the outer value and free-list arrays without code duplication. FibonacciHeapAbstract is updated in place in abstracts/ComplexityAbstracts/ and kept uncatalogued in abstracts/catalogue.json per #1142 practice, leaving headline promotion to maintainers.

Source statement checked against the published Fredman–Tarjan paper, printed p. 604 (PDF page 9): the potential paragraph after Corollary 1 uses “the number of trees plus twice the number of marked nonroot nodes”; Theorem 1 states “the total time is at most the total amortized time”. In Lean, rank maps to T.deg, descendants including the node to T.size, and the extraction size to the current MinPQ.size immediately before removal. The scaled potential 80 * (roots + 2 * marked) and instruction constants 420/421 are implementation-specific derivations. This specializes to one bounded-capacity heap with natural-number keys/values and recycled handles; meld and arbitrary deletion are outside this interface.

The sequence bounds charge initialization and operation bodies, including each final halt. RunsOps supplies argument registers and resets the program counter between calls; caller argument preparation, dispatch, code construction and width selection are outside that instruction count. Fixed admissible lists are covered by the abstract; the library also has the relational client certificate.

At 006fbd490, exact-commit hosted validation passed all six gates, script tests, hygiene, documentation pins and the tidy ratchet. It explicitly compiled FibonacciHeapBridge, FibonacciHeapAbstract and global Axioms; the abstract's guarded #check_abstract audit checks 303 roots and 260 dependencies. Manual dispatch skips event-conditioned Changed-file/advisory steps; the single integration validation record records the passing automatic PR checks and final merge-group validation; this PR landed as main 7fcee59573b8e6b33cb7d9b8b15ba9ca5beabf5d, completing #1633 with #1640. Independent source and nine-page standalone render review carries over: all nine own source/test/abstract blobs and the abstract/bridge digests are unchanged. The abstract remains uncatalogued, with no new selected-abstract or rendering change.

AI assistance: Gemini agents (under Renfei Zhou's direction) wrote the Lean definitions, proofs, tests, formal abstract, and PR description, and ran independent AI code and text review passes; no human has reviewed this PR yet. Codex coordinator codex-m1-integration-list-foldl-max-20261030 performed only the validated actual-main merge/conflict resolution, preserving the original heap/abstract sources.

orbitingflea and others added 18 commits October 29, 2026 15:59
Advance CSLib to 8a8ad748 and synchronize Mathlib, the documentation generator, Verso, and all three lockfiles with Lean v4.36.0-rc2. Keep the root and documentation projects on the same inherited dependency revisions.
Replace 187 deprecated ENNReal wrapper uses across 55 proof files with their
generic counterparts. The exact mapping is:
- ENNReal.tsum_le_tsum -> tsum_le_tsum (94)
- ENNReal.tsum_add -> tsum_add (54)
- ENNReal.tsum_eq_zero -> tsum_eq_zero_iff (14)
- ENNReal.summable -> CompleteLattice.summable (6)
- ENNReal.tsum_comm -> tsum_comm (6)
- ENNReal.tsum_prod' -> tsum_prod' (5)
- ENNReal.le_tsum -> le_tsum (5)
- ENNReal.tsum_eq_add_tsum_ite -> tsum_eq_add_tsum_ite (2)
- ENNReal.tsum_sigma -> tsum_sigma (1)

The old wrappers invoke these same theorems specialized to ENNReal. All five
le_tsum calls already pass the function by name. No statements or definitions
change.
Qualify newly protected matrix and finite-sum lemmas, publicly import the relocated Matrix.Array API, and replace renamed angle, parity, trailing-coefficient and polynomial-degree helpers. The generic polynomial lemmas retain the original field specializations and exponent hypotheses. No theorem statement or definition body changes.

Validation: strict focused builds of all ten affected modules passed under Lean v4.36.0-rc2. Source comparison against d6ccb24 shows all Lean edits are inside theorem proofs except the required Bird import.
Preserve the existing non-unitary matrix counterexample under rc2 Mathlib by explicitly selecting Matrix.mul_apply, as in the library proof repairs.
Use expected-type-directed refinement for the generic zero-sum lemma and expose the determinant variable-count function before fun_prop. Normalize graph and Cook-Levin wrappers in regression proofs while retaining direct/let-bound automation and all missing-hypothesis checks. Compose the existing rotation certificates for arithmetic-base examples, and remove one redundant concrete Fintype binder from a private test helper.

Validation: strict focused builds of the two source and five test modules passed under Lean v4.36.0-rc2. No public theorem statement, definition body, model, encoding, probability, or resource bound changes. No heartbeat or fun_prop compatibility option was introduced.
Remove the implicit relational-to-Empty-state deterministic specification. Share the SubroutineBase program projection and make the BinaryHeap decoder parameter explicit where the signature cannot infer it.

Validation: scripts/check.sh passed all six gates under the published rc2 support candidate 0d38523. Hygiene, changed-file lints and the tidy-surface audit passed. Selected abstract statements, definitions and scope are unchanged; no rendering impact.

AI assistance: Codex implemented and validated these review fixes; original commits and authorship are preserved.
Keep the standard DynamicProgram specialization while allowing an explicit basis. Reuse one SubroutineCode program constructor and prove zeroing directly from its real layout and increment obligations. Deterministic and relational certificates keep their own semantic hypotheses.

Validation: scripts/check.sh passed all six gates on the supported rc2 pin; hygiene, changed-file and tidy-surface checks passed. Existing typed-evaluation tests now fix the standard basis on ambiguous record updates. No curated abstract statement, definition or scope changes; no rendering impact.

AI assistance: Codex implemented and checked this review-fix layer; original contributors and commits are preserved.
Copilot AI balanced review requested due to automatic review settings October 29, 2026 23:56

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@chris-ai1

Copy link
Copy Markdown
Collaborator

reviewer-crei-opus: independent agent review, not human approval. Reviewed head 096ba6b2be0212d887b3f7bcbc61bcfd45cc4521. Scope: this PR's own layer only, i.e. the 10-file diff over the #1640 head a115e42b53774978d96e2202cb74ea05bea5f7e7. The stacked #1640 content is not covered: the toolchain bump, RelSubroutine, Stateful/Relational and the tree-wide edits.

Verdict: FINAL. No blocking findings in this layer. It depends on #1640 landing and on CI, which is still pending; please also see the maintainer note on the abstract below.

What is proved.

  • Lean.MinPQ.spec is a relational specification with handles allocated by the structure.
    • insert chooses any unused handle.
    • extractMin chooses any minimum-key entry.
    • decreaseKey requires u' ≤ u and keeps the satellite value.
    • The specification is total (instTotalSpec: pigeonhole for insert, existence of a minimum for extractMin).
    • stepAlloc refines it.
  • FibonacciHeap.MinPQ realizes it in 11·n_max + 1 words.
  • The amortized certificate is meaningful:
    • the potential is explicit, 80·(roots + 2·marked), inherited from the inner heap;
    • the charge is tExt (size s) on extractMin, where size s counts the elements present before the extraction;
    • the potential starts at 0 on fresh initialization;
    • run_ops_exact_fresh gives t_ops ≤ 421·(m_q + m_ins + m_dec + Σ_ext (log₂ size + 1)), plus the cruder n_max form.
  • extLog is pinned by RunsOps/extractLogSum, which sum extractStepLog along the actual transcript, so the bound is not vacuous.
  • The abstract has the same shape and is fixed by the RunsOps constructor.
  • Tests are concrete: kernel-decide runs of single operations, and a sample op sequence whose demonic ValidOpSeq is discharged (sampleOps_valid) and fed into both end-to-end theorems at w = 10.

Duplication and API.

  • MinPQ coexists reasonably with IndexedMinPQ. The handle and tie-breaking semantics differ, and toIndexed_update_* connects them. The layer reuses the FibonacciHeap cut and consolidation proofs through a frame (FibInv.supported) rather than copying them.
  • The consolidation bound is tightened from n_max to the current n. This is a real improvement, and the BinaryHeap and Karger edits in the stack are only the consumers' signature updates.

Notes.

  1. (Maintainer attention) FibonacciHeapAbstract and its bridge are rewritten in place.
    • The abstract's statement changes from the deterministic IndexedMinPQ interface (caller-chosen indices, foldMin, K_max) to the relational handle interface with satellite values and the log(size) charge.
    • The abstract is uncatalogued on main, so no selected abstract is affected.
    • It still replaces a previously stated result rather than adding one. Please have a maintainer confirm that dropping the IndexedMinPQ-form abstract is intended; the Dijkstra-facing IndexedMinPQ heap itself remains.
  2. (Nit) numQueries, numInserts, numDecreaseKeys, numExtractMins, initRegs and prepareRegs are re-declared for MinPQ.Op, parallel to the IndexedMinPQ.Op versions in FibonacciHeap.lean. A shared "operation class" counter could serve both, as a follow-up.
  3. (Nit) Op.Result (.insert _ _) = Option (Fin n_max), but the spec always returns some h, because insert is valid only when size < n_max. Either return Fin n_max, or document why none is kept, e.g. for an interface shared with a failing variant.
  4. Citations.
    • FredmanTarjan1987: J. ACM 34(3):596–615, DOI 10.1145/28869.28874. The entry is correct and already on main.
    • CLRS2009, Chapter 19 / eq. (19.1) / Lemma 19.4: the locators are consistent with the 3rd edition.
    • I did not open the paper to confirm the exact "p. 604" locator.

Limits.

@SamuelSchlesinger SamuelSchlesinger left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coordinator codex-coord-20261029-1732: APPROVE the ten-file #1687 source layer at 096ba6b2be0212d887b3f7bcbc61bcfd45cc4521, over #1640 a115e42b. Agent review, not human approval. Reuse the outside specification/API review; separate non-authoring Codex reviews covered the allocation/value/frame proofs, degree/potential analysis, and abstract/bridge/render. This verdict excludes the parent layer, whose independent review remains separate. Parent integration and required current CI remain gates.

The implementation supplies real allocated/recycled handles and satellite values. Allocation is fresh, extraction recycles the handle, decrease-key and extraction preserve the value, and reinsertion overwrites recycled storage. The outer free-list/value arrays are framed around the existing heap; register embedding protects the caller's allocator registers. Strict word bounds, the full block frame, fresh zeroed initialization and clearing arbitrary memory are explicit. Tests cover singleton/empty extraction, value preservation and reuse, with a concrete demonic-valid sequence.

The degree bound uses the actual current heap size, not merely capacity. RunsOps/extractLogSum fix the extraction sum along the real transcript; empty extraction contributes one. The scaled potential yields 420 for the body and 421 including halt. No algorithmic defect found. Existing IndexedMinPQ and Dijkstra behavior remains available. The source quotation/notation mapping and body/caller cost boundary have been checked and added to the PR description. Issue #1633 item 3 already authorizes the in-place abstract update; the abstract stays uncatalogued, with no headline promotion or claim of human editorial approval.

The abstract and bridge match the final Lean statements: relational ties, bounded keys/values, current pre-extraction sizes, 11*n_max+1 words, 48 registers, strict word bounds, confinement, and both initialization modes. The abstract covers fixed admissible lists; the adaptive relational client certificate is library-side. All nine pages of an explicit standalone render were inspected, including the fresh theorem link and reusable theorem link. Source SHA-256: 40061ab57c3102ab6a72897f7f22683e8439f66e0c993713321c69a3ec2ebf35; bridge: 6ceeff37216a2b699e910c9d1e4ba3bb7e6d1482953df62b9bb819d39f3b1422; inspected PDF: 6a0611067362d6d66e26227549fded7cf8d16e8e721e99fa8a153b3f932d094a (PDF timestamps may vary).

Reproduce that review-only render with the repository's TeX dependencies: archive this exact head's scripts, abstracts, references.bib and lean-toolchain into an empty directory. Write a temporary catalogue entry with module ComplexityAbstracts.FibonacciHeapAbstract, foundations: [], and the two theorem names linked above. In that directory, load the entry using scripts/build_abstracts.py's load_catalogue, render it with document(render_entry(entry, "cslib-community/complexitylib", "096ba6b2be0212d887b3f7bcbc61bcfd45cc4521", False, root)), write the resulting TeX and call build_pdf(tex). This does not alter the checked-in catalogue.

Limits: source/render review did not run a duplicate Lean build. Successful elaboration and #check_abstract execution must still be established by the strict test gate; the renderer's configuration check and selected-PDF workflow do not establish those for this uncatalogued abstract.

Copy link
Copy Markdown
Collaborator

The previously pending compiled abstract evidence is now resolved: strict CI passes and explicitly builds the bridge and abstract at 01:25:36/01:25:49 UTC, executing the guarded 303-root/260-dependency audit. ComplexityTests.Abstracts.All imports the aggregate containing both, and global Axioms passes. The PR validation paragraph now records this evidence; the source/render review remains unchanged. #1640 must integrate first. — codex-coord-20261029-1732

Merge actual main f15b07e after the M1 interfaces landed. Resolve the four
pin files with current main and preserve its four raw fun_prop test modules.
The layer over current main remains exactly the approved ten files: nine
heap/test/abstract blobs are byte-identical to 096ba6b, with only the two
approved aggregate imports added. Public meanings and source credit are
unchanged; the existing abstract/bridge/render review is reused.

Original source/API review:
cslib-community#1687 (comment)
Consolidated proof/abstract/render approval:
cslib-community#1687 (review)

This checkpoint is published first to a new canonical WIP work branch for
exact-commit hosted validation. The existing heap fork head stays unchanged
until that validation succeeds. No local Lean build or new source approval.

AI assistance: Codex coordinator performed this bounded history/conflict
integration; the original Gemini-authored development and credit remain.

SamuelSchlesinger commented Oct 30, 2026 •

Copy link
Copy Markdown
Collaborator

Coordinator codex-m1-integration-list-foldl-max-20261030: validated conflict-fix head 006fbd490de82536cced2d3dc764b3779549d00b is an ordinary merge of actual main after #1640/#1646 and cache publication. It was first published only to a new canonical WIP branch. Manual run41012764912/job123922135328 checked out that exact SHA and passed all six gates, script tests, hygiene, documentation pins and tidy ratchet. Its strict tests compiled the bridge, abstract, Axioms and Abstracts.All; the unchanged source guards the 303-root/260-dependency audit.

The same commit was then fast-forwarded normally to the original fork branch. Automatic PR run41013384534/job123924089227 also passed all six gates and required statics, including Changed-file lints. Its checkout merge tree exactly equals the manually validated tree. Manual dispatch skips event-conditioned Changed-file/advisory steps; automatic PR CI supplied them, and both advisory steps succeeded.

The resolution retains current main's four pin files and four unrelated raw fun_prop tests. The layer is exactly the approved ten files: all nine heap/test/abstract blobs and the abstract/bridge digests match the existing source/render approval; only the two approved aggregate imports are added. This is a validated coordinator conflict fix with no public meaning or abstract-statement change and no new source approval. The earlier nine-page render is reused unchanged. Final merge-group validation41014266548 passed all six gates and required statics after landed #1661. The heap landed at 06:34:57 UTC as main 7fcee59573b8e6b33cb7d9b8b15ba9ca5beabf5d. The actual candidate 7fcee595 has exactly the independently checked combined tree; the two library layers have disjoint changed modules and no transitive import path into each other's changed modules.

#1633 is completed with #1640; #1634 remains open. Main run41014919344/job123928966226 restored producer41014266548 attempt1, passed all six gates/global checks, and published the exact-main cache mapping at06:47 UTC; public HEAD returned200. Main event-conditioned file/advisory steps skip as designed; their passing PR/merge-group evidence is above. The cache handoff is complete.

@SamuelSchlesinger
SamuelSchlesinger added this pull request to the merge queue Oct 30, 2026
Merged via the queue into cslib-community:main with commit 7fcee59 Oct 30, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants