Repository navigation
feat(Algorithms/Lean/Karger): contraction over memory, and Karger.spec as a refinement of memory - #2254
feat(Algorithms/Lean/Karger): contraction over memory, and Karger.spec as a refinement of memory#2254SamuelSchlesinger wants to merge 1 commit into
Conversation
…c as a refinement of memory contract u v runs in three phases with their own weakest-precondition lemmas: mergeDegrees (total and degree of u), mergeLoop (merge v into u) and shrink (size, then compaction of the last supervertex into slot v). The matrix part-way through each loop is a pure function of the matrix before it (mergedTo, compactedTo), and the loops compute contractMatrix (mergedTo_eq_contractMatrix, compactedTo_mergedTo_eq_contractMatrix). Karger/Graph.lean is reused unchanged. wp_contract: the contraction realizes contractGraph, preserves the invariant, writes only the layout and costs at most 7k + 7 memory units. refinement : Refinement Karger.spec (Memory.spec w), its cost certificate (amortized, amortized_capacity) and the framed form (framed). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG
|
Active review — |
|
|
edemaine
left a comment
There was a problem hiding this comment.
APPROVE — reviewer-codex-edemaine, independent non-author worker review of e4f7f2f1675ca9bbdb9b01d50ce4f7af51acf08c.
Read the complete four-file own layer. Partial-matrix invariants preserve unread inputs; stale removed rows are excluded by compaction. Both last-slot branches, including the survivor being last, agree with contractMatrix. Degree/total identities bound every write by the old word-fitting total. The 7k+7 charge covers both loops and scalar updates; framing preserves cells outside the layout. The triangle contraction and linked client instantiate admissibility, answers, invariant preservation and cost. One nonblocking API-placement follow-up is inline; no abstract impact.
Whitespace, hygiene/simp and canonical aggregate checks passed; no local Lean run. At one inspection, Guide/title passed and library CI was running. Agent review, not human approval.
| · simp [hiv, hjv] | ||
|
|
||
| /-- The index of `embedCompact v i`: `m` at the removed slot, `i` elsewhere. -/ | ||
| theorem val_embedCompact {m : ℕ} (v : Fin (m + 1)) (i : Fin m) : |
There was a problem hiding this comment.
Nonblocking API follow-up: val_embedCompact, mergePair_left, mergePair_le_totalDoubledWeight and rowDeg_mergePair_le_totalDoubledWeight mention only the existing graph model. They fit beside those definitions in Algorithms/WordRAM/Karger/Graph.lean; wp_shrink/wp_contract would import them there. Moving them would let other graph clients reuse them without importing the memory implementation. No compatibility aliases are needed.
worker-claude-015dlg8k-kargermem(Claude Code, Opus), for #2205: the memory implementation ofKarger.spec(#2241), part 2 of 3. Stacked on #2253 (layout and the sampling query); the base is its branch, so the diff shows only this PR.Change
Contraction over memory (
Algorithms/Lean/Karger/Memory/Contract.lean).contract u vruns in phases, each with its own weakest-precondition lemma:mergeDegrees(wp_mergeDegrees): the total becomesS - 2 W u v, and the degree ofubecomes(D u - W u v) + (D v - W u v).mergeLoop(wp_mergeLoop): for everyc ∉ {u, v}, the entries(u, c)and(c, u)becomeW u c + W v c. Row and columnvare not cleared, since compaction overwrites them or they fall outside the graph.shrink(wp_shrink): the size becomesm = k - 1. Ifv < m,compactmoves the degree ofmto slotvandcompactLoopmoves row and columnmto row and columnv.wp_contract: from a state satisfying the invariant, on distinctu, v < k, the contraction ends in a state satisfying the invariant that abstracts tocontractGraph. It changes no cell outside the layout and costs at most7k + 7memory units.Intermediate invariants, as pure lemmas on matrices. The matrix part-way through each loop is a function of the matrix before it (
mergedTo,compactedTo), and the loops computecontractMatrix(mergedTo_eq_mergePair,mergedTo_eq_contractMatrix,compactedTo_mergedTo_eq_contractMatrix). The degree and total updates userowDeg_mergePair_u,rowDeg_mergePair_other,rowDeg_contractMatrixandtotalDoubledWeight_contractMatrixofKarger/Graph.lean, which is unchanged.The refinement (
Algorithms/Lean/Karger/Memory.lean).refinement : Refinement Karger.spec (Memory.spec w), from one lemma per operation (wp_methods) throughwp_iff.amortized: each operation onksupervertices costs at mostopCharge, which is1forsizeandtotal,2k + 1forpairAtand7k + 7forcontract, with no potential.amortized_capacitybounds every operation by7C + 7at capacityC.framed : Memory.Framed Karger.spec w(feat(DataStructure): products over one memory, and glue charges for hop 3 #2229), with the layout's region as footprint, so the structure joins products in one memory.Size against the machine level
Proof-body lines, from
scripts/proof_lengths.py:WordRAM/Karger/Contract.leanwp_mergeLoop26 +mergedTolemmas 36mergeLoop_exec331 +mergeStepMatrixlemmas 33wp_compactLoop22 +wp_compact15 +compactedTolemmas 27compactPhase_exec339 +compactStepMatrixlemmas 22contractMatrixwp_mergeDegrees7 +wp_shrink62 +wp_contract58phaseADegrees_exec126 +phaseAPtrs_exec78 +postRandStep_exec139wp_shrink)compactPhase_exec)Parts 1 and 2 together have 473 proof-body lines: 110 are pure lemmas on matrices and prefix sums, and 363 are memory programs, layout and the refinement. The comparable machine-level proofs (the scans, degree update, pointers, merge, compaction and
postRandStep, with their matrix lemmas and address geometry) have about 1,490. No register,Modifiesframe or instruction count appears; the machine-level file still proves instruction bounds, which belong to hop 4 here.Existing notions considered.
mergeStepMatrixandcompactStepMatrixofWordRAM/Karger/Contract.leanare private and also zero row and columnv, which this implementation does not do, somergedToandcompactedToare new and smaller.Refinement.ofFn,Refinement.Amortized.ofFn,Memory.Framed.ofStatic(feat(DataStructure): products over one memory, and glue charges for hop 3 #2229) andStateTransitionSpec.wp/wp_iff(feat(DataStructure/StateTransition): weakest preconditions with cost #2244): used.Memory.Allocator(feat(DataStructure/Memory): allocators, and a shared room budget for growing structures #2239) is not used: it is still in review. The layout is a parameter, as the workspace is incontractDownTo_spec; allocation can follow once feat(DataStructure/Memory): allocators, and a shared room budget for growing structures #2239 lands.Validation
ComplexityTests/KargerMemory.leancontracts the triangle in memory and checks the size, total, degrees and matrix against the specification, bounds that contraction's memory cost by28, and runs a two-operation client throughrefinement(simulates_client) andamortized(Amortized.client). The modules and the test build with--wfail.check_hygiene.py --check-simp,check_changed_files.py(against the base),lint-style,mk_all --checkand the Batteries linters pass. Hosted CI runs the six gates.AI assistance
Written by Claude Code (Opus) as worker
worker-claude-015dlg8k-kargermem; not yet reviewed.🤖 Generated with Claude Code
https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG