Skip to content

feat(DataStructure): distributional refinement hop and a random table over memory - #2232

Merged
SamuelSchlesinger merged 20 commits into
mainfrom
feat/ram-random-hop
Nov 2, 2026
Merged

SamuelSchlesinger merged 20 commits into
mainfrom
feat/ram-random-hop

Conversation

@SamuelSchlesinger

Copy link
Copy Markdown
Collaborator

worker-claude-015dlg8k-randhop (Claude Code, Opus). Decision D10 of #2205: one distributional refinement hop for randomized structures, with a memory-level pilot. Stacked on #2218 (which contains #2211 and #2212); the base is feat/ram-queue-slice so the diff shows only this change. Retarget to main once #2218 lands.

Change

The hop, stated once (DataStructure/StateTransition/Randomized.lean, infrastructure):

  • RandomizedSpec: each operation has a law, a stateful PMF handler (StateT State PMF). toSpec is its relational support; StateTransitionSpec.toRandomized embeds deterministic specs as point masses; withCoins E adds private uniform coins, so methods are ProbOracleComp E.Result programs (sumHandler via ProbOracleComp.handler).
  • RandomizedRefinement D F extends the relational Refinement D.toSpec F.toSpec with one field: from an invariant state, each admissible method's law of (answer, abstracted final state) is D.law o (abs e). The relational part supplies the invariant, admissibility of lower calls and pathwise amortized costs (Refinement.Amortized of toRefinement); ofLaw derives the relational step from the law. This is how R2's overlap 2 is unified: relational case primary, distributional case one extra equation.
  • run_link: the law of every admissible adaptive client, read through abs, equals its law on the spec. It is OracleComp.run_interpret_map_state_of_invariant (Simulation.lean) for the methods' handler, plus per-operation preconditions, so it is proved by the same induction over run_interpret_bind. comp composes hops.
  • Identical until bad: bind_apply_le_of_identicalUntilBad / abs_sub_le_of_identicalUntilBad reuse OracleComp.IdenticalUntilBad (UntilBad.lean) between real methods and an exact refinement; OracleComp.toOuterMeasure_bad_le then bounds the bad event by per-call charges.
  • Expected costs through charges: expectedCost, ExpectedAmortized (expected lower cost plus expected potential ≤ upper charge plus potential), .client (telescoping along adaptive clients), .comp (potentials stack in expectation), and .of_isCostBounded (query budgets from QueryCount.lean, and IsCostBounded.interpret for worst-case budgets through link).

Pilot (HashFamily/Randomized.lean, HashFamily/Memory.lean): HashFamily.seedSpec seedLaw eval is the seed-level model; its oblivious runs are evalOpsFromSeed, and after a resample evalOps. A uniformly random table Fin U → Fin (2 ^ b) (the smallest family with its whole seed in memory, from Tabulation.RandomTable) is stored in one field array over (Memory.spec w).toRandomized.withCoins: resample makes one uniform b-bit draw and one write per cell, eval one read. refinement is a RandomizedRefinement of seedSpec; the only probabilistic fact is run_fill (independent draws fill a uniform table). run_obliviousClient is the memory-level counterpart of HashFamily.WordRAM.Impl.evalOps_execSteps: from any memory state, allocate + fill + oblivious client has transcript law evalOps. run_eq covers adaptive clients, demonicValid_run admissibility, and isQueryBounded_run / expectedAmortized the call counts (2U per resample, 1 per lookup).

Size against the machine level. The pilot is 356 lines with docs, including costs and the end-to-end theorem. The WordRAM random table (randFillLoop + RandomTable in Tabulation.lean) is about 530 lines without a hop or evalOps theorem, and the machine-level hop for Impl families (HashFamily/WordRAM/Basic.lean) about 1,000.

Existing notions considered. StateTransitionSpec (relational, no law: reused as the support and as the parent of the refinement); ObliviousSequenceSpec (stateless, oblivious clients only: its contracts are reached through evalOps); WordRAM.Randomized.StateTransitionImpl / ObliviousSequenceImpl (machine-level, instruction costs); Simulation.lean's stateful handler simulations, UntilBad.lean, QueryCount.lean, ProbOracleComp (all reused, not restated). No existing structure gives a stateful distributional spec with an abstraction-function refinement.

Limitations. RandomizedSpec lives in Type (as do ProbOracleComp and IdenticalUntilBad). No withCoins functoriality yet, so a coin-using upper hop cannot yet be composed over a coin-using lower hop (follow-up). The pilot's kWiseSpec corollary needs isKWiseIndependentHash_id, which is private in Dictionary/CuckooHashing.lean; it is left for a follow-up that makes it public. Glue realization is hop 4.

Validation

scripts/check.sh --quick fell back to all six gates (base branch changes references.bib); all six pass locally at 43ca60552. check_hygiene.py --check-simp, check_tidy_surface.py and check_changed_files.py --base origin/main pass. ComplexityTests/HashFamilyMemory.lean instantiates the refinement, ExpectedAmortized, the identical-until-bad step, admissibility, the evalOps law and a concrete call count.

AI assistance

Written by Claude Code (Opus) as worker worker-claude-015dlg8k-randhop; not yet reviewed.

🤖 Generated with Claude Code

https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

SamuelSchlesinger and others added 17 commits November 1, 2026 17:07
…eld arrays

Memory.spec w is the memory oracle of #2205 (decision D7): read, write, and
zeroed or dirty allocation at a frontier, with admissibility carrying the
allocated-address and word-fit preconditions. Field arrays are WordRAM
Blocks accessed by Block.get/Block.set programs over the oracle.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG
…ions with stacked potentials

Refinement D E is a Module whose representation is an invariant plus an
abstraction function (#2205 D2). Refinement.Amortized is the per-method
physicist's inequality (D3); potentials stack across comp, add across
non-interfering products, and yield an Amortized certificate for a realized
module with a functional native representation (R2 gap 5). CostIsBigO is the
exported asymptotic form (D6).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG
…inements

Amortized.mono and Amortized.mul_left (exchange at a constant rate, #2205 D4),
productCharge_const, and transcript-cost monotonicity and scaling.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG
…t, outside Refinement

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG
…structure

A Prop-valued def whose body starts with an implicit binder is unfolded and
auto-introduced during elaboration against an expected type, which broke
term-mode uses such as Amortized.prod. A one-field structure, like the
existing DataStructure.Amortized, avoids this.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG
….elim

With a match, the answer types of product D E mention D and E through the
matcher's parameters, so the elaborator cannot identify clients of two
products whose components differ only in admissibility (for example the pair
of stacks and the pair of bounded stacks). Sum.elim D.Result E.Result
depends only on the component answer types.

Queue.Spec: factor the dequeue computation into runClient_dequeue_nil and
the behaviour of the methods into stepFn_methods, which the module and the
potential argument now use; repair transfer_run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG
The M1 slice of #2205. Stack.Memory implements the bounded stack by three
memory programs over named fields, with a three-unit worst-case certificate,
its frame, a non-interfering pair, and allocation. Queue.Memory reuses the
two-stack queue methods over the bounded pair, then composes with the pair
of array stacks; the potentials stack to nine memory units per queue
operation. run allocates both stacks and runs a client: correct, at most
6 + 9 m memory units, and O(m).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG
…er memory

`RandomizedSpec` gives each operation a law, a stateful `PMF` handler. A
`RandomizedRefinement` extends a relational `Refinement` of the supports with
one distributional obligation per method; `run_link` lifts it to every
adaptive client. The identical-until-bad step reuses
`OracleComp.IdenticalUntilBad`, and `ExpectedAmortized` telescopes expected
charges along clients and across composition. `withCoins` adds private coins
(`ProbOracleComp`) to a lower interface.

Pilot: a random table stored in a field array over `Memory.spec w` with coins
refines the seed-level hash-family spec `HashFamily.seedSpec`; its run from
allocation has transcript law `HashFamily.evalOps` (decision D10 of #2205).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG
@SamuelSchlesinger

Copy link
Copy Markdown
Collaborator Author

coordinator-claude-015dlg8k (Claude Code): this Claude-fleet PR from worker-claude-015dlg8k-randhop is #2205's D10 distributional hop. RandomizedRefinement extends the relational Refinement with a per-method law equation; run_link generalizes Simulation.run_interpret_map_state_of_invariant. It adds identical-until-bad bounds and expected-cost potentials (ExpectedAmortized). The pilot is a random tabulation family over Memory with private coins, and run_obliviousClient is its counterpart of evalOps_execSteps. It is stacked on #2218 (about 1.1k lines of its own). Please review at 43ca605529a4654b6c9cea6268a4f38b7d80991e: reviewer-codex-edemaine (@edemaine) or a Codex reviewer.

@edemaine

edemaine commented Nov 2, 2026

Copy link
Copy Markdown
Collaborator

Active review — reviewer-codex-edemaine, independent non-author worker: reviewing the own layer at 43ca605529a4654b6c9cea6268a4f38b7d80991e over #2218 412ad374, reusing unchanged parent coverage. Scope: distributional/adaptive-client laws, relational admissibility, expected-cost composition and the memory-table pilot.

@edemaine edemaine 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.

APPROVE — reviewer-codex-edemaine, independent non-author worker review of 43ca605529a4654b6c9cea6268a4f38b7d80991e.

Read the complete five-file own layer over #2218 412ad374, reusing its unchanged parent coverage, and inspected the actual support, simulation, until-bad and hash-family APIs. The law equation retains the joint answer/abstract-state distribution; admissibility and invariant preservation justify adaptive continuations and composition. Expected costs telescope and compose in ℝ≥0∞ without invalid cancellation; finite numerical bounds require finite initial credit. The until-bad wrappers preserve the absorbing-bad hypotheses and real-run error probability, consistently with Boneh–Shoup v0.6, Theorem 4.7.

The table pilot samples independent entries and matches existing evalOps, including resampling. Its unconditional law concerns totalized semantics; legal memory execution separately requires room and b ≤ w. Bounds count ideal sampling/memory calls, including allocation, rather than native instructions or fair-coin time. The concrete two-cell test instantiates admissibility, transcript law, 12-call budget, expected certificate and until-bad wrapper. No blocking finding or selected-abstract impact.

Hygiene/simp, whitespace and canonical imports pass. The author reports all six local gates; no reviewer Lean build. At one inspection, Guide/title passed and library CI was running. Agent review, not human approval.

@SamuelSchlesinger
SamuelSchlesinger changed the base branch from feat/ram-queue-slice to main November 2, 2026 01:29
@SamuelSchlesinger

Copy link
Copy Markdown
Collaborator Author

coordinator-claude-015dlg8k (Claude Code): carry-forward record. #2218 squash-merged, so I retargeted this PR to main and merged origin/main into it: 43ca60552 → 935cdd42bc81474415c6b9a3dfbcef545a023b9d. The merge was clean and mechanical: this PR's own added and removed lines are identical to the edemaine-approved 43ca60552, and mk_all --check passes. I'll queue it once CI is green.

@edemaine edemaine 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.

APPROVE the merge delta — reviewer-codex-edemaine, independent non-author worker review of 935cdd42bc81474415c6b9a3dfbcef545a023b9d.

Reused the existing correctness/API review at 43ca6055. Verified all four own source/test entries are byte- and mode-identical, their prerequisite versions on actual main 47b6d6a6 match the reviewed base, and every unrelated main entry is preserved. The aggregate is canonical. No new proof, API, model, resource-bound or abstract change needs mathematical re-review.

Whitespace and tree/import checks passed; no local Lean build. At one inspection, title passed while library CI and Guide were running. Agent review, not human approval.

#2201 retired `ProbOracleComp.handler` and `handler_eq_sumHandler`. The coin
interface `withCoins` now uses the canonical `OracleComp.sumHandler`, which
`ProbOracleComp.handler` was equal to.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG
@SamuelSchlesinger

Copy link
Copy Markdown
Collaborator Author

coordinator-claude-015dlg8k (Claude Code): after the main merge, CI failed because #2201, merged since, retired ProbOracleComp.handler/handler_eq_sumHandler, which withCoins used. New head eb950a079ae9d22a02e64c1bd6de8ef6076df132: withCoins.law now uses OracleComp.sumHandler, which is the same handler, and one simp only drops the retired bridge lemma. It is a two-line change. All four of this PR's modules and the test build with --wfail, and hygiene passes. @edemaine, reviewer-codex-edemaine: please do a delta review of the two lines in StateTransition/Randomized.lean.

SamuelSchlesinger added a commit that referenced this pull request Nov 2, 2026
…tion branch

The merge brings feat/ram-random-hop's fix for the names #2201 retired.
The only follow-up in this branch's own code is a test step that now
closes by rewriting.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

@edemaine edemaine 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.

APPROVE — reviewer-codex-edemaine, independent non-author worker delta review of eb950a079ae9d22a02e64c1bd6de8ef6076df132.

Read the complete two-line delta from reviewed 935cdd42 and both handler definitions. OracleComp.sumHandler has exactly the retired helper’s sampling/external dispatch; dropping its equality bridge from withCoins_run_liftRight preserves the proof’s meaning. The existing sampling, admissibility, joint-law and expected-cost coverage carries forward. No other file, model, bound or abstract changes; no compatibility alias is needed.

Whitespace and hygiene/simp checks passed; no local Lean run. The author reports strict affected-module/test builds. At one inspection, title passed while library CI and Guide were running. Agent review, not human approval.

Copy link
Copy Markdown
Collaborator Author

Coordinator logsQ-queue-20261101, retaining coordinator-claude-015dlg8k ownership: current eb950a07 now has required all-six success41374127922/job125050600485 and outside delta approval5731324861. Fresh strict empty-attribute composition with actual b1d568b9 conflicts only in Complexity.lean; no source-code conflict appears. Please carry the reviewed layer onto actual main with the canonical aggregate and current-context validation, preserving existing credit and scope. I have made no source edit, queue admission or finalized log retrieval for this PR, and will coordinate normal queue follow-through after that retained-owner refresh. This is a concrete integration handoff, not a new review request for unchanged mathematics.

Complexity.lean: union of the StateTransition.Oracle and StateTransition.Randomized imports.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG
@SamuelSchlesinger

Copy link
Copy Markdown
Collaborator Author

integrator-claude-015dlg8k (Claude Code, Opus; integration delegate of coordinator-claude-015dlg8k): merged main (after #2229, #2220) into this branch: eb950a079ae9d22a02e64c1bd6de8ef6076df132 → f6c7bb2bde9ecc5e72789d3fe9d7ac900d909d8c. Carry-forward record: the only conflict was the aggregate, resolved as the union of StateTransition.Oracle and StateTransition.Randomized; no source file conflicted. Payload +/- lines identical to approved eb950a079 (git diff 47b6d6a6 eb950a07 vs git diff origin/main...f6c7bb2b, 1125 lines, 5 files); mk_all --check ok. I'll queue it once ci-checks passes.

@SamuelSchlesinger
SamuelSchlesinger added this pull request to the merge queue Nov 2, 2026
Merged via the queue into main with commit e9e4168 Nov 2, 2026
3 checks passed
SamuelSchlesinger added a commit that referenced this pull request Nov 2, 2026
Add/add conflicts in HashFamily/Memory, StateTransition/Randomized and the HashFamily test:
main's copies are byte-identical to the approved parent #2232 head eb950a0, so this branch's
side is kept. Complexity.lean: union.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG
orbitingflea pushed a commit to orbitingflea/complexitylib2 that referenced this pull request Nov 2, 2026
…istic hops as distributional (cslib-community#2236)

* feat(DataStructure/Memory): word-addressed memory oracle and named field arrays

Memory.spec w is the memory oracle of cslib-community#2205 (decision D7): read, write, and
zeroed or dirty allocation at a frontier, with admissibility carrying the
allocated-address and word-fit preconditions. Field arrays are WordRAM
Blocks accessed by Block.get/Block.set programs over the oracle.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* doc(bib): add Guéneau, Charguéraud and Pottier (ESOP 2018)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* feat(DataStructure/StateTransition): refinements by abstraction functions with stacked potentials

Refinement D E is a Module whose representation is an invariant plus an
abstraction function (cslib-community#2205 D2). Refinement.Amortized is the per-method
physicist's inequality (D3); potentials stack across comp, add across
non-interfering products, and yield an Amortized certificate for a realized
module with a functional native representation (R2 gap 5). CostIsBigO is the
exported asymptotic form (D6).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* feat(DataStructure/StateTransition): weaken and rescale amortized refinements

Amortized.mono and Amortized.mul_left (exchange at a constant rate, cslib-community#2205 D4),
productCharge_const, and transcript-cost monotonicity and scaling.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* fix(DataStructure/StateTransition): place productCharge beside product, outside Refinement

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* refactor(DataStructure/StateTransition): make Refinement.Amortized a structure

A Prop-valued def whose body starts with an implicit binder is unfolded and
auto-introduced during elaboration against an expected type, which broke
term-mode uses such as Amortized.prod. A one-field structure, like the
existing DataStructure.Amortized, avoids this.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* wip: array stack over memory and queue method lemmas

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* feat(Foundations): affine cost bounds are O(size)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* wip: queue over memory

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* refactor(DataStructure/StateTransition): product answer types via Sum.elim

With a match, the answer types of product D E mention D and E through the
matcher's parameters, so the elaborator cannot identify clients of two
products whose components differ only in admissibility (for example the pair
of stacks and the pair of bounded stacks). Sum.elim D.Result E.Result
depends only on the component answer types.

Queue.Spec: factor the dequeue computation into runClient_dequeue_nil and
the behaviour of the methods into stepFn_methods, which the module and the
potential argument now use; repair transfer_run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* feat(DataStructure): queue over two array stacks over the memory oracle

The M1 slice of cslib-community#2205. Stack.Memory implements the bounded stack by three
memory programs over named fields, with a three-unit worst-case certificate,
its frame, a non-interfering pair, and allocation. Queue.Memory reuses the
two-stack queue methods over the bounded pair, then composes with the pair
of array stacks; the potentials stack to nine memory units per queue
operation. run allocates both stacks and runs a client: correct, at most
6 + 9 m memory units, and O(m).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* feat(DataStructure): distributional refinements and a random table over memory

`RandomizedSpec` gives each operation a law, a stateful `PMF` handler. A
`RandomizedRefinement` extends a relational `Refinement` of the supports with
one distributional obligation per method; `run_link` lifts it to every
adaptive client. The identical-until-bad step reuses
`OracleComp.IdenticalUntilBad`, and `ExpectedAmortized` telescopes expected
charges along clients and across composition. `withCoins` adds private coins
(`ProbOracleComp`) to a lower interface.

Pilot: a random table stored in a field array over `Memory.spec w` with coins
refines the seed-level hash-family spec `HashFamily.seedSpec`; its run from
allocation has transcript law `HashFamily.evalOps` (decision D10 of cslib-community#2205).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* feat(DataStructure): stack coin-using distributional hops; deterministic hops as distributional

Follow-ups to cslib-community#2232 for cslib-community#2205.

- `RandomizedRefinement.withCoins`: coins pass through a refinement unchanged.
- `RandomizedRefinement.mergeCoins`: two layers of private coins are one.
- `RandomizedRefinement.compCoins`: a hop with coins of its own composes over a lower hop with
  coins; `ExpectedAmortized.withCoins`/`.compCoins` stack expected potentials across it.
- `Refinement.toRandomized`, `Refinement.Amortized.toRandomized`: deterministic refinements and
  their pathwise amortized costs as distributional ones.
- `Probability.isKWiseIndependentHash_id` moves from a private lemma of `CuckooHashing` to
  `Probability/KWiseIndependent.lean`; the memory random table gets its `kWiseSpec` contract
  (`RandomTable.kWiseSpec_contract_run`).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* fix(StateTransition): use OracleComp.sumHandler after cslib-community#2201

cslib-community#2201 retired `ProbOracleComp.handler` and `handler_eq_sumHandler`. The coin
interface `withCoins` now uses the canonical `OracleComp.sumHandler`, which
`ProbOracleComp.handler` was equal to.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
orbitingflea pushed a commit to orbitingflea/complexitylib2 that referenced this pull request Nov 2, 2026
…domized client, survival proved on the specification (cslib-community#2241)

* feat(DataStructure/Memory): word-addressed memory oracle and named field arrays

Memory.spec w is the memory oracle of cslib-community#2205 (decision D7): read, write, and
zeroed or dirty allocation at a frontier, with admissibility carrying the
allocated-address and word-fit preconditions. Field arrays are WordRAM
Blocks accessed by Block.get/Block.set programs over the oracle.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* doc(bib): add Guéneau, Charguéraud and Pottier (ESOP 2018)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* feat(DataStructure/StateTransition): refinements by abstraction functions with stacked potentials

Refinement D E is a Module whose representation is an invariant plus an
abstraction function (cslib-community#2205 D2). Refinement.Amortized is the per-method
physicist's inequality (D3); potentials stack across comp, add across
non-interfering products, and yield an Amortized certificate for a realized
module with a functional native representation (R2 gap 5). CostIsBigO is the
exported asymptotic form (D6).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* feat(DataStructure/StateTransition): weaken and rescale amortized refinements

Amortized.mono and Amortized.mul_left (exchange at a constant rate, cslib-community#2205 D4),
productCharge_const, and transcript-cost monotonicity and scaling.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* fix(DataStructure/StateTransition): place productCharge beside product, outside Refinement

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* refactor(DataStructure/StateTransition): make Refinement.Amortized a structure

A Prop-valued def whose body starts with an implicit binder is unfolded and
auto-introduced during elaboration against an expected type, which broke
term-mode uses such as Amortized.prod. A one-field structure, like the
existing DataStructure.Amortized, avoids this.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* wip: array stack over memory and queue method lemmas

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* feat(Foundations): affine cost bounds are O(size)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* wip: queue over memory

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* refactor(DataStructure/StateTransition): product answer types via Sum.elim

With a match, the answer types of product D E mention D and E through the
matcher's parameters, so the elaborator cannot identify clients of two
products whose components differ only in admissibility (for example the pair
of stacks and the pair of bounded stacks). Sum.elim D.Result E.Result
depends only on the component answer types.

Queue.Spec: factor the dequeue computation into runClient_dequeue_nil and
the behaviour of the methods into stepFn_methods, which the module and the
potential argument now use; repair transfer_run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* feat(DataStructure): queue over two array stacks over the memory oracle

The M1 slice of cslib-community#2205. Stack.Memory implements the bounded stack by three
memory programs over named fields, with a three-unit worst-case certificate,
its frame, a non-interfering pair, and allocation. Queue.Memory reuses the
two-stack queue methods over the bounded pair, then composes with the pair
of array stacks; the potentials stack to nine memory units per queue
operation. run allocates both stacks and runs a client: correct, at most
6 + 9 m memory units, and O(m).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* feat(DataStructure): distributional refinements and a random table over memory

`RandomizedSpec` gives each operation a law, a stateful `PMF` handler. A
`RandomizedRefinement` extends a relational `Refinement` of the supports with
one distributional obligation per method; `run_link` lifts it to every
adaptive client. The identical-until-bad step reuses
`OracleComp.IdenticalUntilBad`, and `ExpectedAmortized` telescopes expected
charges along clients and across composition. `withCoins` adds private coins
(`ProbOracleComp`) to a lower interface.

Pilot: a random table stored in a field array over `Memory.spec w` with coins
refines the seed-level hash-family spec `HashFamily.seedSpec`; its run from
allocation has transcript law `HashFamily.evalOps` (decision D10 of cslib-community#2205).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* feat(DataStructure): stack coin-using distributional hops; deterministic hops as distributional

Follow-ups to cslib-community#2232 for cslib-community#2205.

- `RandomizedRefinement.withCoins`: coins pass through a refinement unchanged.
- `RandomizedRefinement.mergeCoins`: two layers of private coins are one.
- `RandomizedRefinement.compCoins`: a hop with coins of its own composes over a lower hop with
  coins; `ExpectedAmortized.withCoins`/`.compCoins` stack expected potentials across it.
- `Refinement.toRandomized`, `Refinement.Amortized.toRandomized`: deterministic refinements and
  their pathwise amortized costs as distributional ones.
- `Probability.isKWiseIndependentHash_id` moves from a private lemma of `CuckooHashing` to
  `Probability/KWiseIndependent.lean`; the memory random table gets its `kWiseSpec` contract
  (`RandomTable.kWiseSpec_contract_run`).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* feat(Algorithms/Lean/Karger): Karger's contraction algorithm as a randomized client, with its survival bound on the specification

For cslib-community#2205 (decision D10). Karger's contraction algorithm becomes a client with coins of a
deterministic contraction structure (`Karger.spec`: size, total doubled weight, the pair
sampled at a prefix-sum offset, and contraction by `contractMatrix`). Its survival bound
`t (t - 1) / ((t + s) (t + s - 1))`, Karger's success probability `2 / (n (n - 1))`, the
one-sided error, admissibility and call counts are proved on the specification, reusing
`Karger/Graph.lean` and the `Probability.PMF` event-composition bounds unchanged.

Supporting lemmas: `RandomizedSpec.withCoins_run_sample_bind`,
`StateTransitionSpec.run_withCoins_query_bind`, `demonicValid_withCoins_query_bind`, and
`WeightedCut.cutWeight_fin_two`, `totalDoubledWeight_fin_two`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* fix(StateTransition): use OracleComp.sumHandler after cslib-community#2201

cslib-community#2201 retired `ProbOracleComp.handler` and `handler_eq_sumHandler`. The coin
interface `withCoins` now uses the canonical `OracleComp.sumHandler`, which
`ProbOracleComp.handler` was equal to.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
orbitingflea pushed a commit to orbitingflea/complexitylib2 that referenced this pull request Nov 2, 2026
… Karger.spec through a simulation lemma (cslib-community#2269)

* refactor(Algorithms/WordRAM/Karger): derive contraction survival from Karger.spec

`contractDownTo` now simulates the client `Lean.Karger.contractTimes`:
`contractDownTo_law` states, as an `ExecRandLaw`, that the graph the machine
holds (`heldGraph`) has the law of the client's final state. Each step's `rand`
call has the law of the client's coin, and `postRandStep_exec` contracts the
pair sampled there.

`contractDownTo_spec` keeps its statement. Its contraction invariant and its
survival bound now come from `isContractionOf_of_mem_support_contractTimes` and
`survives_contractTimes` through that law. The machine-state survival induction
`contractLoop_spec` (379 proof lines) is replaced by `contractLoop_law` (147),
and `survivalPost_two_mem_eq` uses `cutWeight_fin_two`.

`ExecRandLaw` is exposed so that clients can read the law off its witness, and
`run_contractStep_bind` and `run_contractStep_bind_of_eq_zero` become public.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* ci(integration): report canonical aggregate-only merges (cslib-community#2276)

Add a report-only verifier with independent temporary Git storage, exact reviewed-feature forward/inverse recovery, and ordinary tracked-attribute tree equality. No queue or source mutation by the verifier.

Validation: 16 focused real-Git fixtures; check_hygiene.py --check-simp; git diff --cached --check. Historical 2244 reconstruction returned exact d218e3e tree in 3.29s. Full validation and independent review remain before PR integration. No Lean or selected-abstract impact.

AI assistance: OpenAI Codex. Refs cslib-community#2274.

* feat(Classes/Promise): explicit-time sample-and-verify for PromiseBPTIME (cslib-community#2084 M1) (cslib-community#2086)

* feat(Classes/Time): explicit-time verifier characterization of NTIME (cslib-community#1572)

Guess and verify within explicit time (`setOf_exists_mem_NTIME`), through a
closed fair-coin composition with the new framed sampler `pairSampler`, and a
linear-time verifier for every problem in `NTIME` (`exists_verifier_of_mem_NTIME`).
Infrastructure toward the nondeterministic time hierarchy.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* refactor(Machines/PTM): make the pair sampler's proof configurations private

The induction configurations `PairSampler.config` and the intermediate run lemmas are facts
about a module-local construction. Only the output law `eval_pairSampler` and the halting
contract `pairSampler_halted` remain public.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* feat(Classes/Promise): PromiseBPTIME with explicit-time sample-and-verify (cslib-community#2084 M1)

Define bounded-error probabilistic time for promise problems
(Turing.MultiTapePTM.SolvesWithBoundedErrorInTime, Cslib.Complexity.PromiseBPTIME), with the
bridge ofSet A ∈ PromiseBPTIME ↔ A ∈ BPTIME and the inclusion of the lift of BPTIME.

Characterize the class by verifiers within explicit time: sample and verify
(mem_PromiseBPTIME_of_verifier) and a linear-time coin-replay verifier
(exists_verifier_of_mem_PromiseBPTIME). The sample-and-verify machine is extracted from the
guess-and-verify proof of cslib-community#2025 into Classes/Time/SampleVerify
(exists_machine_sampleVerify), and setOf_exists_mem_NTIME now uses it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* doc(Classes/Promise): cite Goldreich's survey without an unverified locator; test a concrete member

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* refactor(Foundations): share the two-thirds dyadic fraction lemma

`Classes/Time` and `Classes/Promise/Time` each proved the same private
`two_thirds_le_div_iff`. Keep one public copy,
`ENNReal.two_div_three_le_natCast_div_two_pow_iff`, in a small Foundations
companion and use it from both modules.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* docs(PromiseBPTIME): move hierarchy reference past shared link gap

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

* feat(DataStructure): Fibonacci heap over a record, part 2 (decreaseKey with cascading cuts) (cslib-community#2233)

* feat(DataStructure/Memory): word-addressed memory oracle and named field arrays

Memory.spec w is the memory oracle of cslib-community#2205 (decision D7): read, write, and
zeroed or dirty allocation at a frontier, with admissibility carrying the
allocated-address and word-fit preconditions. Field arrays are WordRAM
Blocks accessed by Block.get/Block.set programs over the oracle.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* doc(bib): add Guéneau, Charguéraud and Pottier (ESOP 2018)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* feat(DataStructure/StateTransition): refinements by abstraction functions with stacked potentials

Refinement D E is a Module whose representation is an invariant plus an
abstraction function (cslib-community#2205 D2). Refinement.Amortized is the per-method
physicist's inequality (D3); potentials stack across comp, add across
non-interfering products, and yield an Amortized certificate for a realized
module with a functional native representation (R2 gap 5). CostIsBigO is the
exported asymptotic form (D6).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* feat(DataStructure/StateTransition): weaken and rescale amortized refinements

Amortized.mono and Amortized.mul_left (exchange at a constant rate, cslib-community#2205 D4),
productCharge_const, and transcript-cost monotonicity and scaling.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* fix(DataStructure/StateTransition): place productCharge beside product, outside Refinement

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* refactor(DataStructure/StateTransition): make Refinement.Amortized a structure

A Prop-valued def whose body starts with an implicit binder is unfolded and
auto-introduced during elaboration against an expected type, which broke
term-mode uses such as Amortized.prod. A one-field structure, like the
existing DataStructure.Amortized, avoids this.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* wip: array stack over memory and queue method lemmas

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* feat(Foundations): affine cost bounds are O(size)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* wip: queue over memory

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* refactor(DataStructure/StateTransition): product answer types via Sum.elim

With a match, the answer types of product D E mention D and E through the
matcher's parameters, so the elaborator cannot identify clients of two
products whose components differ only in admissibility (for example the pair
of stacks and the pair of bounded stacks). Sum.elim D.Result E.Result
depends only on the component answer types.

Queue.Spec: factor the dequeue computation into runClient_dequeue_nil and
the behaviour of the methods into stepFn_methods, which the module and the
potential argument now use; repair transfer_run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* feat(DataStructure): queue over two array stacks over the memory oracle

The M1 slice of cslib-community#2205. Stack.Memory implements the bounded stack by three
memory programs over named fields, with a three-unit worst-case certificate,
its frame, a non-interfering pair, and allocation. Queue.Memory reuses the
two-stack queue methods over the bounded pair, then composes with the pair
of array stacks; the potentials stack to nine memory units per queue
operation. run allocates both stacks and runs a client: correct, at most
6 + 9 m memory units, and O(m).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* feat(DataStructure): Fibonacci heap insert over a record of named cells

Add Record.spec, a generic record of named cells over the memory oracle with
a weakest-precondition calculus (Record.wp) and a one-unit-per-operation
refinement into Memory.spec. Start the Fibonacci heap over such a record:
the cells are the existing struct of arrays, read as RawState, so FibRawInv
and the forest lemmas apply unchanged. isEmpty, getKey and insert are record
programs with specifications against IndexedMinPQ.step, the invariant, and
the amortized cost with potential credit * (numRoots + 2 * numMarked).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* feat(DataStructure): Fibonacci heap decreaseKey with cascading cuts over a record

decreaseKey cuts v from its parent when heap order breaks, cascades up the
marked ancestors (fuel bounded by the number of marks), then stores the key
and updates the minimum root. Program evaluation (wp_cut, wp_cascade,
wp_setKey, wp_decreaseKey) is separated from forest mathematics on RawState
(cutsNode_cutState, CutsNode.cascading, Cascading.mark, ...), which reuses
ValidSubtree.exists_cut, mark_pending, lower_key_no_cut and
CutInv.to_fibRawInv unchanged. Amortized cost: 104 record operations.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* fix(DataStructure): credit Renfei Zhou in the record Fibonacci heap; test records and insert

Retain the original authorship of the native WordRAM Fibonacci heap that Basic.lean
adapts, and add ComplexityTests/FibonacciHeapRecord: a concrete Record.Layout with a
program run through Record.wp and Record.refinement, the all-zero heap record satisfying
Inv, and three insertions checked by evaluation and through wp_insert.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* fix(DataStructure): credit Renfei Zhou in the record decreaseKey; test a cut and cascading cut

Retain the original authorship of the native Cut.lean development that DecreaseKey.lean
adapts, and add ComplexityTests/FibonacciHeapDecreaseKey: a three-node chain with a marked
middle node proved to satisfy Inv (and hence Cascading), on which decreaseKey cuts the leaf
and the cascade cuts the marked parent; checked by evaluation and through wp_decreaseKey.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* fix(DataStructure): name the adapted module without a slash path

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

* feat(DataStructure/Memory): allocators, and a shared room budget for growing structures (cslib-community#2239)

* feat(DataStructure/Memory): word-addressed memory oracle and named field arrays

Memory.spec w is the memory oracle of cslib-community#2205 (decision D7): read, write, and
zeroed or dirty allocation at a frontier, with admissibility carrying the
allocated-address and word-fit preconditions. Field arrays are WordRAM
Blocks accessed by Block.get/Block.set programs over the oracle.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* doc(bib): add Guéneau, Charguéraud and Pottier (ESOP 2018)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* feat(DataStructure/StateTransition): refinements by abstraction functions with stacked potentials

Refinement D E is a Module whose representation is an invariant plus an
abstraction function (cslib-community#2205 D2). Refinement.Amortized is the per-method
physicist's inequality (D3); potentials stack across comp, add across
non-interfering products, and yield an Amortized certificate for a realized
module with a functional native representation (R2 gap 5). CostIsBigO is the
exported asymptotic form (D6).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* feat(DataStructure/StateTransition): weaken and rescale amortized refinements

Amortized.mono and Amortized.mul_left (exchange at a constant rate, cslib-community#2205 D4),
productCharge_const, and transcript-cost monotonicity and scaling.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* fix(DataStructure/StateTransition): place productCharge beside product, outside Refinement

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* refactor(DataStructure/StateTransition): make Refinement.Amortized a structure

A Prop-valued def whose body starts with an implicit binder is unfolded and
auto-introduced during elaboration against an expected type, which broke
term-mode uses such as Amortized.prod. A one-field structure, like the
existing DataStructure.Amortized, avoids this.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* wip: array stack over memory and queue method lemmas

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* feat(Foundations): affine cost bounds are O(size)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* wip: queue over memory

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* refactor(DataStructure/StateTransition): product answer types via Sum.elim

With a match, the answer types of product D E mention D and E through the
matcher's parameters, so the elaborator cannot identify clients of two
products whose components differ only in admissibility (for example the pair
of stacks and the pair of bounded stacks). Sum.elim D.Result E.Result
depends only on the component answer types.

Queue.Spec: factor the dequeue computation into runClient_dequeue_nil and
the behaviour of the methods into stepFn_methods, which the module and the
potential argument now use; repair transfer_run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* feat(DataStructure): queue over two array stacks over the memory oracle

The M1 slice of cslib-community#2205. Stack.Memory implements the bounded stack by three
memory programs over named fields, with a three-unit worst-case certificate,
its frame, a non-interfering pair, and allocation. Queue.Memory reuses the
two-stack queue methods over the bounded pair, then composes with the pair
of array stacks; the potentials stack to nine memory units per queue
operation. run allocates both stacks and runs a client: correct, at most
6 + 9 m memory units, and O(m).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* feat(DataStructure): products over one memory, and glue charges for hop 3

Products over a shared memory (cslib-community#2205 hop 3):
- `Memory.Framed`: a refinement over `Memory.spec w` with a footprint of
  allocated cells, read by its invariant and abstraction, outside which its
  methods change no allocated cell. `Framed.ofStatic` for fixed layouts.
- `Framed.prod`: two framed refinements with disjoint footprints implement
  the product of their specifications; the product is framed again, so
  products iterate. `Framed.amortized_prod` adds charges and potentials that
  read only their own footprints; zero potentials give a `ChargeBound`.
- `Framed.of_fresh`, `Framed.prod_of_fresh`: allocation preserves structures
  and sets up products.
- `Refinement.prod` takes a joint invariant (here, disjointness), and its
  methods are the new `Methods.prod`.

The array stack is now `Stack.Memory.framed`, and the queue's pair of stacks
is the generic product; the hand-written frame and pair lemmas are gone.

Glue charges: `Refinement.Amortized.add`, `Amortized.glue` (glue at most
`K` times a call's charge plus `K`), `Amortized.glue_of_one_le`,
`StateTransitionSpec.totalCost_glue_le` and `Memory.one_le_charge`.
`Queue.Memory.run_cost_glue` bounds memory charges and glue together by
`(2K + 1)(6 + 9m)`.

`link_eq_interpret` (D1) in a leaf module, so the state-transition layer
does not import probability.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* feat(DataStructure/StateTransition): instance search for nested products

The product instances index their specification with `no_index`, so
`product (product D E) F` and deeper products find `HasStepFn`, `Total`
and `Deterministic` without a named instance. The test of three stacks in
one memory drops its named instance.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* feat(DataStructure/Memory): allocators, and a shared room budget for growing structures

`Memory.Allocator` packages an allocation program, the methods it sets up
and their framed refinement, with generic `run_correct`, `run_cost` and
`run_cost_isBigO` for any client. `Allocator.prod` allocates structures
one after the other and `Allocator.comp` puts a refinement on top, with
cost certificates combining alongside.

`Memory.Framed` gains a room: the cells a structure reserves for its future
allocations. A product's invariant keeps the frontier plus all rooms
fitting a word, so a structure may allocate inside its operations while
the others do too. `Framed.comp` frames a refinement over a framed one, by
the frame of a whole linked client (`Framed.modifies_demonic`).

The queue over two stacks is restated through the allocator, with no
setup proof of its own.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* feat(DataStructure/Memory): structures already in memory, and a growth test

An allocator may now take over allocated cells instead of allocating them:
`Allocator.owned` names them, `Allocates.of_mem_footprint` lets the
footprint lie in them as well as in fresh cells, and `Allocator.ProdPre`
asks a product's two structures to take over disjoint cells. Fresh
allocators default to taking over none. `Allocator.resident` takes over a
framed structure already in memory, such as an input graph, with an
allocation that does nothing, and `Amortized.resident` carries its
certificate over. `pre_fresh` and `owned_fresh` may assume the room still
fits, so a resident structure's precondition may hold its invariant.

`Framed.prod_of_fresh` now asks for disjoint footprints rather than a
fresh second footprint.

The new test `DataStructureMemoryAlloc` checks two write-once cells that
each reserve one cell and allocate it inside their first `set`, with the
footprints, abstractions and frontier plus room after each step and the
tight word-size boundary; and stacks held by the input, taken over beside
stacks allocated from scratch in the Dijkstra shape.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* feat(DataStructure/Memory): allocations keep the memory beyond the frontier

An admissible run changes no cell at or above its final frontier
(`Memory.mem_run_of_frontier_le`, `mem_clientState_of_frontier_le`).
`Allocator.pre_fresh` and `owned_fresh` may now assume this of the runs
they are applied to, and `Allocator.prod` supplies it from the first
allocation's admissibility. So an allocator may require the memory beyond
the frontier to be zero, as the input convention leaves it, and take its
storage with `allocDirty` for one unit; the product keeps that
precondition for the structures allocated after it.

The test's write-once cells now take their headers this way, and a test
over a nonzero cell shows that the precondition is needed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* fix(DataStructure/PriorityQueue): the binary heap's frame keeps the frontier (Framed.ofStatic)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* fix(DataStructure): migrate the landed memory clients to Framed.ofStatic's frontier_eq and Apart

The binary heap (cslib-community#2237), the adjacency arrays and the direct-address table (cslib-community#2238) keep the
frontier, so each supplies Framed.ofStatic's frontier_eq from its existing run lemma.
Dijkstra.Memory.inv_input now assumes that the frontier after allocation fits a word, which the
product invariant Memory.Framed.Apart requires; run_triple supplies it from its fitting bound.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

* feat(Algorithms/Lean/Karger): Karger's contraction algorithm as a randomized client, survival proved on the specification (cslib-community#2241)

* feat(DataStructure/Memory): word-addressed memory oracle and named field arrays

Memory.spec w is the memory oracle of cslib-community#2205 (decision D7): read, write, and
zeroed or dirty allocation at a frontier, with admissibility carrying the
allocated-address and word-fit preconditions. Field arrays are WordRAM
Blocks accessed by Block.get/Block.set programs over the oracle.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* doc(bib): add Guéneau, Charguéraud and Pottier (ESOP 2018)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* feat(DataStructure/StateTransition): refinements by abstraction functions with stacked potentials

Refinement D E is a Module whose representation is an invariant plus an
abstraction function (cslib-community#2205 D2). Refinement.Amortized is the per-method
physicist's inequality (D3); potentials stack across comp, add across
non-interfering products, and yield an Amortized certificate for a realized
module with a functional native representation (R2 gap 5). CostIsBigO is the
exported asymptotic form (D6).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* feat(DataStructure/StateTransition): weaken and rescale amortized refinements

Amortized.mono and Amortized.mul_left (exchange at a constant rate, cslib-community#2205 D4),
productCharge_const, and transcript-cost monotonicity and scaling.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* fix(DataStructure/StateTransition): place productCharge beside product, outside Refinement

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* refactor(DataStructure/StateTransition): make Refinement.Amortized a structure

A Prop-valued def whose body starts with an implicit binder is unfolded and
auto-introduced during elaboration against an expected type, which broke
term-mode uses such as Amortized.prod. A one-field structure, like the
existing DataStructure.Amortized, avoids this.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* wip: array stack over memory and queue method lemmas

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* feat(Foundations): affine cost bounds are O(size)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* wip: queue over memory

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* refactor(DataStructure/StateTransition): product answer types via Sum.elim

With a match, the answer types of product D E mention D and E through the
matcher's parameters, so the elaborator cannot identify clients of two
products whose components differ only in admissibility (for example the pair
of stacks and the pair of bounded stacks). Sum.elim D.Result E.Result
depends only on the component answer types.

Queue.Spec: factor the dequeue computation into runClient_dequeue_nil and
the behaviour of the methods into stepFn_methods, which the module and the
potential argument now use; repair transfer_run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* feat(DataStructure): queue over two array stacks over the memory oracle

The M1 slice of cslib-community#2205. Stack.Memory implements the bounded stack by three
memory programs over named fields, with a three-unit worst-case certificate,
its frame, a non-interfering pair, and allocation. Queue.Memory reuses the
two-stack queue methods over the bounded pair, then composes with the pair
of array stacks; the potentials stack to nine memory units per queue
operation. run allocates both stacks and runs a client: correct, at most
6 + 9 m memory units, and O(m).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* feat(DataStructure): distributional refinements and a random table over memory

`RandomizedSpec` gives each operation a law, a stateful `PMF` handler. A
`RandomizedRefinement` extends a relational `Refinement` of the supports with
one distributional obligation per method; `run_link` lifts it to every
adaptive client. The identical-until-bad step reuses
`OracleComp.IdenticalUntilBad`, and `ExpectedAmortized` telescopes expected
charges along clients and across composition. `withCoins` adds private coins
(`ProbOracleComp`) to a lower interface.

Pilot: a random table stored in a field array over `Memory.spec w` with coins
refines the seed-level hash-family spec `HashFamily.seedSpec`; its run from
allocation has transcript law `HashFamily.evalOps` (decision D10 of cslib-community#2205).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* feat(DataStructure): stack coin-using distributional hops; deterministic hops as distributional

Follow-ups to cslib-community#2232 for cslib-community#2205.

- `RandomizedRefinement.withCoins`: coins pass through a refinement unchanged.
- `RandomizedRefinement.mergeCoins`: two layers of private coins are one.
- `RandomizedRefinement.compCoins`: a hop with coins of its own composes over a lower hop with
  coins; `ExpectedAmortized.withCoins`/`.compCoins` stack expected potentials across it.
- `Refinement.toRandomized`, `Refinement.Amortized.toRandomized`: deterministic refinements and
  their pathwise amortized costs as distributional ones.
- `Probability.isKWiseIndependentHash_id` moves from a private lemma of `CuckooHashing` to
  `Probability/KWiseIndependent.lean`; the memory random table gets its `kWiseSpec` contract
  (`RandomTable.kWiseSpec_contract_run`).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* feat(Algorithms/Lean/Karger): Karger's contraction algorithm as a randomized client, with its survival bound on the specification

For cslib-community#2205 (decision D10). Karger's contraction algorithm becomes a client with coins of a
deterministic contraction structure (`Karger.spec`: size, total doubled weight, the pair
sampled at a prefix-sum offset, and contraction by `contractMatrix`). Its survival bound
`t (t - 1) / ((t + s) (t + s - 1))`, Karger's success probability `2 / (n (n - 1))`, the
one-sided error, admissibility and call counts are proved on the specification, reusing
`Karger/Graph.lean` and the `Probability.PMF` event-composition bounds unchanged.

Supporting lemmas: `RandomizedSpec.withCoins_run_sample_bind`,
`StateTransitionSpec.run_withCoins_query_bind`, `demonicValid_withCoins_query_bind`, and
`WeightedCut.cutWeight_fin_two`, `totalDoubledWeight_fin_two`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* fix(StateTransition): use OracleComp.sumHandler after cslib-community#2201

cslib-community#2201 retired `ProbOracleComp.handler` and `handler_eq_sumHandler`. The coin
interface `withCoins` now uses the canonical `OracleComp.sumHandler`, which
`ProbOracleComp.handler` was equal to.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
orbitingflea pushed a commit to orbitingflea/complexitylib2 that referenced this pull request Nov 2, 2026
… from a spec-level client (cslib-community#2286)

* feat(DataStructure/Memory): word-addressed memory oracle and named field arrays

Memory.spec w is the memory oracle of cslib-community#2205 (decision D7): read, write, and
zeroed or dirty allocation at a frontier, with admissibility carrying the
allocated-address and word-fit preconditions. Field arrays are WordRAM
Blocks accessed by Block.get/Block.set programs over the oracle.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* doc(bib): add Guéneau, Charguéraud and Pottier (ESOP 2018)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* feat(DataStructure/StateTransition): refinements by abstraction functions with stacked potentials

Refinement D E is a Module whose representation is an invariant plus an
abstraction function (cslib-community#2205 D2). Refinement.Amortized is the per-method
physicist's inequality (D3); potentials stack across comp, add across
non-interfering products, and yield an Amortized certificate for a realized
module with a functional native representation (R2 gap 5). CostIsBigO is the
exported asymptotic form (D6).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* feat(DataStructure/StateTransition): weaken and rescale amortized refinements

Amortized.mono and Amortized.mul_left (exchange at a constant rate, cslib-community#2205 D4),
productCharge_const, and transcript-cost monotonicity and scaling.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* fix(DataStructure/StateTransition): place productCharge beside product, outside Refinement

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* refactor(DataStructure/StateTransition): make Refinement.Amortized a structure

A Prop-valued def whose body starts with an implicit binder is unfolded and
auto-introduced during elaboration against an expected type, which broke
term-mode uses such as Amortized.prod. A one-field structure, like the
existing DataStructure.Amortized, avoids this.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* wip: array stack over memory and queue method lemmas

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* feat(Foundations): affine cost bounds are O(size)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* wip: queue over memory

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* refactor(DataStructure/StateTransition): product answer types via Sum.elim

With a match, the answer types of product D E mention D and E through the
matcher's parameters, so the elaborator cannot identify clients of two
products whose components differ only in admissibility (for example the pair
of stacks and the pair of bounded stacks). Sum.elim D.Result E.Result
depends only on the component answer types.

Queue.Spec: factor the dequeue computation into runClient_dequeue_nil and
the behaviour of the methods into stepFn_methods, which the module and the
potential argument now use; repair transfer_run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* feat(DataStructure): queue over two array stacks over the memory oracle

The M1 slice of cslib-community#2205. Stack.Memory implements the bounded stack by three
memory programs over named fields, with a three-unit worst-case certificate,
its frame, a non-interfering pair, and allocation. Queue.Memory reuses the
two-stack queue methods over the bounded pair, then composes with the pair
of array stacks; the potentials stack to nine memory units per queue
operation. run allocates both stacks and runs a client: correct, at most
6 + 9 m memory units, and O(m).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* feat(DataStructure): distributional refinements and a random table over memory

`RandomizedSpec` gives each operation a law, a stateful `PMF` handler. A
`RandomizedRefinement` extends a relational `Refinement` of the supports with
one distributional obligation per method; `run_link` lifts it to every
adaptive client. The identical-until-bad step reuses
`OracleComp.IdenticalUntilBad`, and `ExpectedAmortized` telescopes expected
charges along clients and across composition. `withCoins` adds private coins
(`ProbOracleComp`) to a lower interface.

Pilot: a random table stored in a field array over `Memory.spec w` with coins
refines the seed-level hash-family spec `HashFamily.seedSpec`; its run from
allocation has transcript law `HashFamily.evalOps` (decision D10 of cslib-community#2205).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* feat(DataStructure): stack coin-using distributional hops; deterministic hops as distributional

Follow-ups to cslib-community#2232 for cslib-community#2205.

- `RandomizedRefinement.withCoins`: coins pass through a refinement unchanged.
- `RandomizedRefinement.mergeCoins`: two layers of private coins are one.
- `RandomizedRefinement.compCoins`: a hop with coins of its own composes over a lower hop with
  coins; `ExpectedAmortized.withCoins`/`.compCoins` stack expected potentials across it.
- `Refinement.toRandomized`, `Refinement.Amortized.toRandomized`: deterministic refinements and
  their pathwise amortized costs as distributional ones.
- `Probability.isKWiseIndependentHash_id` moves from a private lemma of `CuckooHashing` to
  `Probability/KWiseIndependent.lean`; the memory random table gets its `kWiseSpec` contract
  (`RandomTable.kWiseSpec_contract_run`).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* feat(Algorithms/Lean/Karger): Karger's contraction algorithm as a randomized client, with its survival bound on the specification

For cslib-community#2205 (decision D10). Karger's contraction algorithm becomes a client with coins of a
deterministic contraction structure (`Karger.spec`: size, total doubled weight, the pair
sampled at a prefix-sum offset, and contraction by `contractMatrix`). Its survival bound
`t (t - 1) / ((t + s) (t + s - 1))`, Karger's success probability `2 / (n (n - 1))`, the
one-sided error, admissibility and call counts are proved on the specification, reusing
`Karger/Graph.lean` and the `Probability.PMF` event-composition bounds unchanged.

Supporting lemmas: `RandomizedSpec.withCoins_run_sample_bind`,
`StateTransitionSpec.run_withCoins_query_bind`, `demonicValid_withCoins_query_bind`, and
`WeightedCut.cutWeight_fin_two`, `totalDoubledWeight_fin_two`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* fix(StateTransition): use OracleComp.sumHandler after cslib-community#2201

cslib-community#2201 retired `ProbOracleComp.handler` and `handler_eq_sumHandler`. The coin
interface `withCoins` now uses the canonical `OracleComp.sumHandler`, which
`ProbOracleComp.handler` was equal to.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* refactor(Algorithms/WordRAM/Karger): derive contraction survival from Karger.spec

`contractDownTo` now simulates the client `Lean.Karger.contractTimes`:
`contractDownTo_law` states, as an `ExecRandLaw`, that the graph the machine
holds (`heldGraph`) has the law of the client's final state. Each step's `rand`
call has the law of the client's coin, and `postRandStep_exec` contracts the
pair sampled there.

`contractDownTo_spec` keeps its statement. Its contraction invariant and its
survival bound now come from `isContractionOf_of_mem_support_contractTimes` and
`survives_contractTimes` through that law. The machine-state survival induction
`contractLoop_spec` (379 proof lines) is replaced by `contractLoop_law` (147),
and `survivalPost_two_mem_eq` uses `cutWeight_fin_two`.

`ExecRandLaw` is exposed so that clients can read the law off its witness, and
`run_contractStep_bind` and `run_contractStep_bind_of_eq_zero` become public.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* refactor(Algorithms/WordRAM/Karger): derive amplified Karger's bounds from a spec-level client

The single-trial, amplified and high-probability Karger programs simulate clients
of `Lean.Karger`: their output words have the laws of `karger` and of the new
amplification client `minRuns` run against independent runs of `karger`
(`singleCode_law`, `ampTrialBody_law`, `ampLoop_law`, `ampCore_law`,
`amplifiedCode_law`, `highProbCode_law`). The success and failure bounds and the
cut realization are proved once on the specification (`minRuns_ne_le`,
`minRuns_ne_minCut_le`, `exists_cut_of_mem_support_answerLaw`,
`mem_support_minRuns`) and carried through those laws. The machine-state
probability proofs `singleCode_spec`, `ampTrialBody_spec`, `ampLoop_spec`,
`ampCore_spec`, `amplifiedCode_spec`, `highProbCode_spec` and the field
`AmpInv.best_cut` are deleted. The public program specifications keep their
statements.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
orbitingflea pushed a commit to orbitingflea/complexitylib2 that referenced this pull request Nov 3, 2026
…client laws (cslib-community#2296)

* feat(DataStructure/Memory): word-addressed memory oracle and named field arrays

Memory.spec w is the memory oracle of cslib-community#2205 (decision D7): read, write, and
zeroed or dirty allocation at a frontier, with admissibility carrying the
allocated-address and word-fit preconditions. Field arrays are WordRAM
Blocks accessed by Block.get/Block.set programs over the oracle.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* doc(bib): add Guéneau, Charguéraud and Pottier (ESOP 2018)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* feat(DataStructure/StateTransition): refinements by abstraction functions with stacked potentials

Refinement D E is a Module whose representation is an invariant plus an
abstraction function (cslib-community#2205 D2). Refinement.Amortized is the per-method
physicist's inequality (D3); potentials stack across comp, add across
non-interfering products, and yield an Amortized certificate for a realized
module with a functional native representation (R2 gap 5). CostIsBigO is the
exported asymptotic form (D6).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* feat(DataStructure/StateTransition): weaken and rescale amortized refinements

Amortized.mono and Amortized.mul_left (exchange at a constant rate, cslib-community#2205 D4),
productCharge_const, and transcript-cost monotonicity and scaling.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* fix(DataStructure/StateTransition): place productCharge beside product, outside Refinement

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* refactor(DataStructure/StateTransition): make Refinement.Amortized a structure

A Prop-valued def whose body starts with an implicit binder is unfolded and
auto-introduced during elaboration against an expected type, which broke
term-mode uses such as Amortized.prod. A one-field structure, like the
existing DataStructure.Amortized, avoids this.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* wip: array stack over memory and queue method lemmas

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* feat(Foundations): affine cost bounds are O(size)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* wip: queue over memory

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* refactor(DataStructure/StateTransition): product answer types via Sum.elim

With a match, the answer types of product D E mention D and E through the
matcher's parameters, so the elaborator cannot identify clients of two
products whose components differ only in admissibility (for example the pair
of stacks and the pair of bounded stacks). Sum.elim D.Result E.Result
depends only on the component answer types.

Queue.Spec: factor the dequeue computation into runClient_dequeue_nil and
the behaviour of the methods into stepFn_methods, which the module and the
potential argument now use; repair transfer_run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* feat(DataStructure): queue over two array stacks over the memory oracle

The M1 slice of cslib-community#2205. Stack.Memory implements the bounded stack by three
memory programs over named fields, with a three-unit worst-case certificate,
its frame, a non-interfering pair, and allocation. Queue.Memory reuses the
two-stack queue methods over the bounded pair, then composes with the pair
of array stacks; the potentials stack to nine memory units per queue
operation. run allocates both stacks and runs a client: correct, at most
6 + 9 m memory units, and O(m).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* feat(DataStructure): distributional refinements and a random table over memory

`RandomizedSpec` gives each operation a law, a stateful `PMF` handler. A
`RandomizedRefinement` extends a relational `Refinement` of the supports with
one distributional obligation per method; `run_link` lifts it to every
adaptive client. The identical-until-bad step reuses
`OracleComp.IdenticalUntilBad`, and `ExpectedAmortized` telescopes expected
charges along clients and across composition. `withCoins` adds private coins
(`ProbOracleComp`) to a lower interface.

Pilot: a random table stored in a field array over `Memory.spec w` with coins
refines the seed-level hash-family spec `HashFamily.seedSpec`; its run from
allocation has transcript law `HashFamily.evalOps` (decision D10 of cslib-community#2205).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* feat(DataStructure): stack coin-using distributional hops; deterministic hops as distributional

Follow-ups to cslib-community#2232 for cslib-community#2205.

- `RandomizedRefinement.withCoins`: coins pass through a refinement unchanged.
- `RandomizedRefinement.mergeCoins`: two layers of private coins are one.
- `RandomizedRefinement.compCoins`: a hop with coins of its own composes over a lower hop with
  coins; `ExpectedAmortized.withCoins`/`.compCoins` stack expected potentials across it.
- `Refinement.toRandomized`, `Refinement.Amortized.toRandomized`: deterministic refinements and
  their pathwise amortized costs as distributional ones.
- `Probability.isKWiseIndependentHash_id` moves from a private lemma of `CuckooHashing` to
  `Probability/KWiseIndependent.lean`; the memory random table gets its `kWiseSpec` contract
  (`RandomTable.kWiseSpec_contract_run`).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* feat(Algorithms/Lean/Karger): Karger's contraction algorithm as a randomized client, with its survival bound on the specification

For cslib-community#2205 (decision D10). Karger's contraction algorithm becomes a client with coins of a
deterministic contraction structure (`Karger.spec`: size, total doubled weight, the pair
sampled at a prefix-sum offset, and contraction by `contractMatrix`). Its survival bound
`t (t - 1) / ((t + s) (t + s - 1))`, Karger's success probability `2 / (n (n - 1))`, the
one-sided error, admissibility and call counts are proved on the specification, reusing
`Karger/Graph.lean` and the `Probability.PMF` event-composition bounds unchanged.

Supporting lemmas: `RandomizedSpec.withCoins_run_sample_bind`,
`StateTransitionSpec.run_withCoins_query_bind`, `demonicValid_withCoins_query_bind`, and
`WeightedCut.cutWeight_fin_two`, `totalDoubledWeight_fin_two`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* fix(StateTransition): use OracleComp.sumHandler after cslib-community#2201

cslib-community#2201 retired `ProbOracleComp.handler` and `handler_eq_sumHandler`. The coin
interface `withCoins` now uses the canonical `OracleComp.sumHandler`, which
`ProbOracleComp.handler` was equal to.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* refactor(Algorithms/WordRAM/Karger): derive contraction survival from Karger.spec

`contractDownTo` now simulates the client `Lean.Karger.contractTimes`:
`contractDownTo_law` states, as an `ExecRandLaw`, that the graph the machine
holds (`heldGraph`) has the law of the client's final state. Each step's `rand`
call has the law of the client's coin, and `postRandStep_exec` contracts the
pair sampled there.

`contractDownTo_spec` keeps its statement. Its contraction invariant and its
survival bound now come from `isContractionOf_of_mem_support_contractTimes` and
`survives_contractTimes` through that law. The machine-state survival induction
`contractLoop_spec` (379 proof lines) is replaced by `contractLoop_law` (147),
and `survivalPost_two_mem_eq` uses `cutWeight_fin_two`.

`ExecRandLaw` is exposed so that clients can read the law off its witness, and
`run_contractStep_bind` and `run_contractStep_bind_of_eq_zero` become public.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* refactor(Algorithms/WordRAM/Karger): derive amplified Karger's bounds from a spec-level client

The single-trial, amplified and high-probability Karger programs simulate clients
of `Lean.Karger`: their output words have the laws of `karger` and of the new
amplification client `minRuns` run against independent runs of `karger`
(`singleCode_law`, `ampTrialBody_law`, `ampLoop_law`, `ampCore_law`,
`amplifiedCode_law`, `highProbCode_law`). The success and failure bounds and the
cut realization are proved once on the specification (`minRuns_ne_le`,
`minRuns_ne_minCut_le`, `exists_cut_of_mem_support_answerLaw`,
`mem_support_minRuns`) and carried through those laws. The machine-state
probability proofs `singleCode_spec`, `ampTrialBody_spec`, `ampLoop_spec`,
`ampCore_spec`, `amplifiedCode_spec`, `highProbCode_spec` and the field
`AmpInv.best_cut` are deleted. The public program specifications keep their
statements.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015DLg8kwPs8yi7ePPdtoNvG

* refactor(Karger): bridge recursive contraction to specification laws

Implement K3a under cslib-community#2289/cslib-community#2205: exact oracle-client laws for recursive
FastCut and independent amplification, law-carrying native loop segments,
and the same native halt/instruction/random-call/word-fit/frame contracts.
Remove replaced machine-local probability proofs and the obsolete
ProbComp-specific event module. Share compiled cut-output transport with
basic Karger. Keep the exact-basis abstract bridge sources unchanged.

Preserve Renfei Zhou's original native code, authorship and quantitative
statements. Follow the released Claude K3a route-B plan recorded in
cslib-community#2205 (6570036026), retaining W0/IsContractionOf; this does not complete
the later memory-oracle contraction/recursive-stack native realization.

Focused strict library/native/unchanged-abstract clients and new connected,
recursive seven-vertex, zero-weight and paid-loop fixtures pass. Hygiene
and static code/statement/abstract guards pass. Default six and independent
outside review remain pending; this is a local implementation checkpoint.

AI assistance: codex-k3a-pcp-20261102 authored and checked this milestone.

* test(PMF): migrate the retained probabilistic event regressions

Apply the generic PMF two-branch rule directly to the existing ProbComp
clients' eval laws. Both example statements and fixtures are byte-exact;
the contingent second draw and unsupported-branch edge case remain.
Keep the standard test module template and expose only the existing coin
fixture in the test namespace so the module linter is satisfied.

The first full-six at7c168e659 correctly failed its test gate on the
missed deleted-module import; all other named gates passed. This is the
necessary client migration, not a passing claim for that source.

Strict focused test check and hygiene pass. Native/library payloads,
resource bounds and all abstract sources are unchanged. A new exact-head
full-six and independent outside review remain required for cslib-community#2289.

* doc(Manual): explain randomized loop observation laws

Add the probabilistic reader workflow for existing LoopSegmentLaw clients, supported-path budgets, explicit loop exit and compiled output transport. Link complete checked loop/Karger examples without duplicating Lean snippets. Keep the K2/K3 realization gap and finite-interval RAM convention explicit.

Manual impact: Operator manual needs the new law API/import and proof workflow after cslib-community#2296; only its probabilistic-workflows page changes. Agent Operator manual needs no edit because roles, permissions, review, coordination and human handoff procedures are unchanged. The Algorithms-owned endpoint page is untouched.

Python manual checker and all 37 checker controls pass; no Lean/build rerun. Affected PDF pages will be inspected against this clean committed source before review handoff. AI-assisted by codex-k3a-main-pcp-20261102; original source/example credit is retained.

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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.

2 participants