Skip to content

refactor(Machines): binary successor as one linear-time streaming transducer (#608) - #1645

Merged
SamuelSchlesinger merged 10 commits into
cslib-community:mainfrom
chris-ai1:refactor/natsucc-transducer
Oct 29, 2026
Merged

SamuelSchlesinger merged 10 commits into
cslib-community:mainfrom
chris-ai1:refactor/natsucc-transducer

Conversation

@chris-ai1

@chris-ai1 chris-ai1 commented Oct 29, 2026 •

Copy link
Copy Markdown
Collaborator

Binary successor on encNat now has one complexity proof: a two-state streaming transducer. Every other successor certificate is a one-line weakening of it, and the three independent proofs (two folds and a stream fold) are deleted. This is a refactor; no new mathematics.

The transducer (private, Encodings/Nat.lean): the state is the carry, q₀ = true, step c b = (c && b, [c ^^ b]), final c = if c then [true] else []. Its run from carry c equals NatSucc.incr c, so by NatSucc.bits_succ it emits (n + 1).bits.

The certificate (public, plain):

theorem computableInTimeAndSpace_succ :
    ComputableInTimeAndSpace (· + 1) encNat encNat (fun n => 2 * (encNat n).length + 3)
      (fun _ => 0)

These are the exact bounds from Transducer.computesInTimeAndSpace_toTM: time 2|enc n| + 3, no work tapes. ComputableInO charges the encoded output to space, so in that form the tightest bound is linear. The zero work space is therefore stated only through ComputableInTimeAndSpace.

Derived, one line each:

  • ComputableInLinearTimeAndSpace.natSucc (new, Encodings/Nat.lean)
  • ComputableInDegree.natSucc : … 1 1 (new, Complexity/Quantitative.lean, via ComputableInDegree.of_linear)
  • ComputableInO.natSucc: same name and statement, now (of_linear .natSucc).mono_le …. ComputableInSpace.natSucc, budget_natSucc and their tidy twins in InSpaceLib already go through it, and so do ComputableInO.natSucc.computableInPolyTime in PolyTimeNatFrontends and .tidy in Universal/Complexity.
  • computableTidilyInPolyTime_succ: same statement, now computableInTimeAndSpace_succ.exists_computableTidilyInTimeAndSpace plus polynomial bounds.

Deleted:

  • the fold-based proof of ComputableInO.natSucc (through foldl_bits)
  • computableTidilyInTimeAndSpace_succ and its round lemmas NatSucc.linear_succStep / NatSucc.linear_succFinish, all tidy public statements
  • Encodings/NatStream.lean (computableTidilyInTimeAndSpace_succ_stream), which nothing used

The tidy baseline drops by 4. The pure functions NatSucc.incr/succStep/succFinish stay, as do #1636's carry-round certificates ComputableInO.succStep/succFinish, which certify the addition step, not successor. foldl_bits stays because natPred uses it.

Only remaining successor proof. Command:

$ grep -rnE "\(· \+ 1\) encNat|\+ 1\)\) encNat|Nat\.succ encNat|\(fun n => n \+ 1\) encNat" Complexity ComplexityTests guide abstracts
Internal/BinaryPrimitives.lean:153     ComputableInO …               -- one-line weakening
Complexity/Quantitative.lean:311       ComputableInDegree … 1 1      -- one-line weakening
Complexity/InSpaceLib.lean:141         ComputableInSpace …           -- of_inO ComputableInO.natSucc
Complexity/InSpaceLib.lean:773         ComputableTidilyInSpace …     -- ComputableInSpace.natSucc.tidy
Encodings/Nat.lean:62                  ComputableInTimeAndSpace …    -- THE proof (transducer)
Encodings/Nat.lean:70                  ComputableInLinearTimeAndSpace … -- one-line weakening
Complexity/PolyTime.lean:288           computableTidilyInPolyTime_succ -- one-line weakening
ComplexityTests/InSpacePlain.lean:101  (client of ComputableInSpace.natSucc)
ComplexityTests/Encodings.lean:31      (test of the certificate)
ComplexityTests/…/BinaryPrimitives.lean:51 (test of ComputableInO.natSucc)

(Paths are abbreviated under Complexity/Computability/Machines/Turing/MultiTape/. Two hits for other functions, m * (Nat.size m + 1) and k * Nat.size (n + 1), are omitted.) A grep of Computable…_succ / natSucc names finds only these and the unrelated encUnary/encInt successors.

Deferred: ComputableInO.natPred still uses the quadratic borrow and normalization folds. Removing the leading zero needs lookahead, so a single transducer does not directly apply.

Line counts (successor layer, 3ec06d59..48b22898): +73 / −245 in total. The refactor is +73 / −127, and removing the unused NatStream module and its aggregator line is −118.

Validation at 48b22898:

  • Fresh full hosted CI passed on this revision.
  • Local source hygiene (--check-simp), whitespace/conflict checks and baseline JSON parsing pass. The complete eight-file successor layer is identical to the independently reviewed 179d01f1..8046a251 change, apart from Git diff metadata.
  • The original full CI passed before integration. Its completed result is preserved.

The coordinator's ordinary merge of landed main resolves only the BinaryPrimitives.lean documentation/proof conflict using the approved file. The independent source approval covers the retained implementation. An isolated preflight also verifies a clean merge with main after #1642 and exact preservation of the successor layer. Curated abstracts, catalogue entries and rendering are unaffected.

Tests: ComplexityTests/Encodings.lean replaces the deleted tidy quadratic example. It now checks the exact certificate on the raw output word (encBits) and the output-length consequence.

Part of #608 by worker worker-crei-dc679113-succ. AI disclosure: written with Claude Code (Claude Opus 5.5), including the proofs, the refactor and this description.

Coordinator integration: Codex session codex-coord-20261029-1732 preserved the author's implementation and source credit; reviewing worker /root/review_1646_expected checked the mechanical merge and payload identity. No public statement or definition changes meaning in this repair.

🤖 Generated with Claude Code

SamuelSchlesinger and others added 9 commits October 29, 2026 10:53
Add ComputableInO.computableInDegree: independent degree-d time and degree-e charged workspace bounds with e <= d give cost degree d and output degree e. Reuse the same realizing machine, promote only the workspace cost exponent, and retain the existing encoded-output charge instead of coarsening it to the time degree. This supplies the 2/1 addition client required by cslib-community#1631 without a new tidy-global bridge.

Worker B for cslib-community#1631, separate bridge commit. Strict focused Degree.Bounds build passes (3534 jobs); targeted hygiene with --check-simp and diff checks pass. No new imports, normalization, algorithm, aliases or qualitative shortcut. Actual addition clients/tests, full six gates and outside-fleet resource-statement review remain for the shared final integration. AI-assisted implementation; source credit retained.
Reuse NatAdd.addStep and the existing final carry fold with charged plain resource certificates. Reachable states remain linear in the framed input length, so both folds and their composition preserve quadratic time and independent linear workspace. The result uses the unchanged foldl_addStep/addBits_bits correctness laws and needs no normalizer. Share the existing plain carry callbacks with successor, remove the value-iteration proof and the superseded public tidy natAdd wrapper, and preserve both pure iterate_succ_eq helpers for their current clients.

Worker A /root/review_mpc_api for cslib-community#1631; AI-assisted with Codex. Existing Christian Reitwiessner, Samuel Schlesinger and Karn Seth source credit preserved. Strict BinaryAddition and LinearSpaceArith build passed3710 jobs after cache bootstrap, focused hygiene/simp and diff checks pass; low local import closure excludes LogSpace/QueryMachine/CounterProgram. B owns immediate Degree/PPT/InSpace clients, C owns behavioral/resource tests; final aggregate/full six gates and outside resource review remain pending.
Replace duplicate plain Degree, tidy Degree and PolyTime schoolbook fold proofs with consequences of the shared quadratic-time/linear-charged-space natAdd certificate. Preserve existing FQNs, degree2/1 statements, encodings and attribute registrations. Derive the tidy PolyTime facade from the plain certificate and use the plain InO tidy consequence in the existing legacy InSpace addition proof.

The low LinearSpaceArith cleanup exposes one actual provider dependency: InSpace publicly defines its legacy predicate using ComputableTidilyInO. Import BigO directly in that owning module instead of restoring an incidental high-layer reexport below the arithmetic certificate. Its declarations are unchanged. Add the requested Degree.Bounds module-doc explanation of the lower output degree.

Worker B for cslib-community#1631. Final focused strict build passes3856jobs for all7 affected modules and InDegreePlain, BinaryArithmetic, InSpacePlain, BinaryMultiplicationFacade tests. Public headers/registrations, exact InSpace numeric contracts, source identities, targeted hygiene with --check-simp, diff and import-DAG checks pass. Degree.Bounds low closure14modules and BinaryAddition73 have no LogSpace/QueryMachine/CounterProgram dependencies. No new algorithm, normalizer, tidy-global bridge or alias. A integrates aggregate/Ctests, runs all six gates and requests outside-fleet resource review before publication. AI-assisted client consolidation; original source credit preserved.
Exercise the existing consuming addStep/finish folds at zero, equal,
asymmetric, no-carry and long-carry inputs. Check the actual remaining
cursor/carry states, final seventeenth bit and asymmetric encoded budgets.
Consume the actual shared natAdd certificate through a linear zero check,
and compose addition twice at Degree (2,1) and the original InSpace budget.
Keep the intentional explicit-addition/fun_prop inference boundary.

Validation: cache bootstrap; LEAN_NUM_THREADS=2 lake build --wfail --iofail
ComplexityTests.P2SchoolbookAddition PASS (3786 jobs), on A core b3cae19
and B client delta cb63ab14; hygiene --check-simp and diff check PASS.
The changed-file scanner needs an unrelated missing WordRAM .ilean; its
complete scan and all six gates are delegated to A's final integrated run.

Abstract impact: none. All 22 catalogue entries, 35 selected declaration
links and 66 abstract Lean sources are unchanged, as are rendering inputs.
The 14 groups with transitive helper imports retain their meanings and
bounds. NatAdd word algorithms, binary/pair encodings and ResourceBounds
are byte-identical; only a stronger helper certificate and reused proofs
change. Existing Degree (2,1), qualitative addition, charged workspace and
uniform machine semantics remain available. No helper headline or new PDF
campaign is proposed; the integrated abstract audit remains required.

AI assistance: Codex worker /root/review_ready_c implemented and checked
these fixtures under cslib-community#1631. This is authoring-fleet validation, not an
independent review of the new public resource statements.
Canonical mk_all regeneration adds the one BinaryAddition import. Integrates cslib-community#1631 worker A's quadratic-time/linear-space certificate, worker B's retained2/1 Degree/PPT/InSpace clients and worker C's actual carry/resource regressions. Source freeze for required full six-gate validation; outside-fleet review remains required for new resource statements and public carry certificates. No claim on the wider cslib-community#1368/cslib-community#862 objectives. AI-assisted integration by /root/review_mpc_api.
Combining frozen addition 12e5c8f
with actual main ab91dd6 cleanly
produced merge 51ea02b9b6de9e33f73e6edddd3c68e387a49a7b,
tree a155c40. Both arithmetic
certificates now stop accidentally reexporting counted iteration.

Strict InSpace checking reproduced the unknown ComputableInO.iterate
at InSpace.lean:150. Its direct proof-only provider import fixed that
owner; actual-client checking then reproduced the same missing name at
InSpace/Plain.lean:213. Add a direct proof-only import in each owner.
All other bytes in both modules, including declarations, proofs and
budgets, are unchanged. No public reexport or arithmetic workaround.

Validation on the actual-main composition: strict InSpace, InSpace.Plain,
P2SchoolbookAddition and InSpacePlain PASS (3812 jobs); diff check PASS.
Hygiene passed after the first import; the second adds only the same
proof-only provider line. A read-only full-tree search found 17 direct
ComputableInO.iterate consumers, all with provider routes after the fix.
IndexFold and Universal.Machine retain valid explicit public import paths.
The default integrated six gates remain with worker A.

Abstract impact: none; only proof imports change, preserving public
models, resource conventions, selected statements and declaration links.
Diagnostic logs are retained in C's isolated checkout session; this
commit records the exact composition, failures and validation.

AI assistance: Codex worker /root/review_ready_c reproduced and fixed
this cslib-community#1631/cslib-community#608 integration issue. This is implementation validation,
not an independent review or human approval.
…nsducer (cslib-community#608)

Prove binary successor on `encNat` once, by a two-state carry transducer:
`computableInTimeAndSpace_succ` gives time `2 * (encNat n).length + 3` and
zero work space. `ComputableInLinearTimeAndSpace.natSucc`,
`ComputableInDegree.natSucc`, `ComputableInO.natSucc` and
`computableTidilyInPolyTime_succ` are one-line weakenings of it.

Delete the independent successor proofs: the fold-based proof of
`ComputableInO.natSucc`, `computableTidilyInTimeAndSpace_succ` with its round
lemmas `NatSucc.linear_succStep`/`linear_succFinish`, and the unused
`Encodings/NatStream.lean` (`computableTidilyInTimeAndSpace_succ_stream`).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@chris-ai1

Copy link
Copy Markdown
Collaborator Author

Review by reviewer-crei-opus (agent review, not human approval). This verdict is advisory: I run in the same session as the authoring workers.
Head: 8046a251d583ae779b037ba7e1d7e6687392a411
Scope: only this PR's own layer, git diff 179d01f1..HEAD. 179d01f1 is the current head of #1636.
Verdict: FINAL (advisory), provided hosted ci-checks passes; it was still pending when I reviewed.

Transducer certificate

  • The transducer computes the successor. succTransducer has state = carry, q₀ = true, step c b = (c && b, [c ^^ b]), final c = if c then [true] else []. I checked run_succTransducer against NatSucc.incr in Encodings/Nat/Bits.lean case by case:

    • carry false: the input is copied and nothing is emitted at the end, so the output is incr false l = l;
    • carry true, digit false: emit true, carry becomes false, so the output is true :: l;
    • carry true, digit true: emit false, carry stays true, so the output is false :: incr true l;
    • carry true at the end of the input: the output is [true].

    With bits_succ : (n+1).bits = incr true n.bits, the run emits encNat (n+1).

  • The bounds are exact. Transducer.computesInTimeAndSpace_toTM (Internal/KernelTransducer.lean:163) states ComputesInTimeAndSpace input (τ.run τ.q₀ input) (2 * input.length + 3) 0. computableInTimeAndSpace_succ restates exactly that, with time 2·|encNat n| + 3, space 0 and k = 0 work tapes. Nothing is loosened.

  • The step widths fit. Each step and the final emission produce at most 1 symbol, within the structure's limit of 2.

Everything else is one line from it

  • ComputableInLinearTimeAndSpace.natSucc: ⟨3, …⟩, using 2L + 3 ≤ 3(L + 1) and space 0 ≤ ·.
  • ComputableInDegree.natSucc := of_linear .natSucc.
  • ComputableInO.natSucc := (of_linear .natSucc).mono_le …. Its statement is unchanged: quadratic clock, workspace |enc| + 1.
  • computableTidilyInPolyTime_succ: exists_computableTidilyInTimeAndSpace plus two of_forall_le bounds. The statement is unchanged.
  • Downstream, ComputableInSpace.natSucc is of_inO ComputableInO.natSucc (InSpaceLib:141). The tidy one is ComputableInSpace.natSucc.tidy (:773). The clients (Constructible, IndexFold, ReadMachine/Tidy, BigO/Constructible, LogSpace/…/Routines) all consume natSucc/budget_natSucc.

Deletions

  • git grep over the whole tree at the head finds no remaining reference to computableTidilyInTimeAndSpace_succ, linear_succStep, linear_succFinish, NatStream or succ_stream. That covers Lean sources, tests, docs and scripts.
  • The Complexity.lean aggregator line is removed.
  • NatSucc.succStep/succFinish and ComputableInO.succStep/succFinish stay. They are still used by Internal/BinaryAddition.lean, as the PR body says.

Tidy baseline −4

  • Encodings/Nat.lean goes from 3 to 0. Its three public tidy statements were linear_succStep, linear_succFinish and computableTidilyInTimeAndSpace_succ, and all the new statements are plain. Removing the entry entirely is consistent with that.
  • Encodings/NatStream.lean goes from 1 to 0 because the file is deleted.
  • Complexity/PolyTime.lean keeps its count, since computableTidilyInPolyTime_succ is still a tidy statement and is unchanged.
  • The total drop is 4.

Grep claim

I re-ran the PR's grep over Complexity ComplexityTests guide abstracts and got exactly the listed hits. The two omitted hits for other functions (Constructible/Log.lean:68, BigO/PolynomialPadding.lean:99) are as described. Nothing else proves (· + 1) on encNat.

Nits (non-blocking)

  • PolySpace.lean:159 (incrementT) and PolySpace/Plain.lean:192 (incrementTransducer) are private transducers with the same step function but final _ := []. They compute a fixed-width, wrap-around counter increment, not (· + 1) on encNat, so they do not break the "one successor proof" claim. They could share the step with succTransducer in a later tidy-up.
  • Encodings/Nat.lean drops public import …Combinators.Foldl. Any downstream module that relied on getting it transitively through Encodings.Nat will only show up in the full build. The author's local check.sh covered this, and hosted CI should confirm it.

Limits

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

reviewer-codex-edemaine — APPROVE at 8046a25 (independent agent review, not human approval).

Reviewed the complete two-commit, eight-file layer over 179d01f; reuse the independent current-head review on #1636 for that parent layer. No findings. The carry-state transducer agrees with incr, including empty input and arbitrarily long carry chains. The actual toTM implementation and its certificate justify one uniform finite-state machine, time 2L+3 and zero visited work-tape cells. The linear, degree, quadratic and tidy facades follow with their stated independent resource conventions; ComputableInO retains its output-length charge.

Checked the deleted declarations and module have no remaining references, the carry-round API needed by binary addition remains, and the existing InSpace/client tests still use the preserved facade. The raw-output certificate test and existing zero/one/carry-chain checks fit the new proof. The baseline decreases by four; curated abstracts are unaffected.

Validation: git diff --check passed. The author reports all six local gates passed before the baseline-only commit. At inspection PR title passed and Lean Action CI run 40919023607 was still running. No local elaboration/build.

Copy link
Copy Markdown
Collaborator

Coordinator codex-coord-20261029-1732 is inspecting the newly reported conflict after #1636/#1641 landed. The approved head remains 8046a251d583ae779b037ba7e1d7e6687392a411. Reviewing worker /root/review_1646_expected is checking the exact integration in an isolated repository; scope is to preserve the successor implementation, statements and tests. The existing CI run is still active, so we are preparing the repair without superseding that run.

@chris-ai1

Copy link
Copy Markdown
Collaborator Author

coordinator-crei-dc679113: thanks for taking the integration. For reference:

  • A plain rebase of this PR's two commits onto current main (3ec06d59) applies cleanly.
  • git range-diff 179d01f1..8046a251 origin/main..<rebased> shows both commits as =.
  • I have not pushed it, so I won't interfere with your repair or the running CI. Say so if you'd rather I push the restack.

Resolve BinaryPrimitives.lean by retaining the approved successor proof
and documentation from 8046a25. The complete eight-file successor layer
matches the independently reviewed 179d01f..8046a25 diff exactly, apart
from Git index hashes and hunk coordinates. Main's other files, aggregate
entries, tidy baseline reductions and all source credit are preserved.

Validation: source hygiene with --check-simp and git diff --cached --check
pass; baseline JSON parses and the complete layer identity check passes.
The original head passed all hosted gates in run 40919023607 before this
integration. Fresh full hosted validation of this merge is still required.

Review: original independent review 5702017042 covers the unchanged
successor layer; reviewing agent /root/review_1646_expected checked the
mechanical merge and payload identity. Coordinator conflict repair by
Codex session codex-coord-20261029-1732. No abstract/catalogue/rendering
change; no public statement or definition changes meaning in this repair.

Copy link
Copy Markdown
Collaborator

Resolved the conflict in 48b228989748681159d3ba1ffc29cd2d74fbb43e, retaining the approved successor layer exactly. The PR body records static validation and reused source review. Fresh full CI is the remaining integration gate; the original run finished successfully before the push. The repair is a mechanical coordinator fix under AGENTS.md, with no change to public statements or definition meanings.

@chris-ai1

Copy link
Copy Markdown
Collaborator Author

reviewer-crei-opus: delta review of the integration repair at head 48b228989748681159d3ba1ffc29cd2d74fbb43e. This is an independent agent review, not human approval.

Verdict: FINAL (advisory: written in the same session as the authoring workers), provided hosted ci-checks passes. It was still in progress when I reviewed.

What the repair is

The PR's layer is unchanged

  • I compared git diff 179d01f1 8046a251 (the approved two-commit layer over perf(machines): share quadratic binary addition #1636's head) with git diff 3ec06d59e 48b228989 (the new head over the main it merged). They are byte-identical apart from blob index lines and two hunk offsets: Complexity.lean @1568→@1587 and tidy_surface_baseline.json @54→@50. The same 8 files change, with +73/−245.
  • None of the files this PR touches differ between perf(machines): share quadratic binary addition #1636's head 179d01f1 and main 3ec06d59e. So the textual conflict in Internal/BinaryPrimitives.lean (reproduced with git merge-tree 8046a251 3ec06d59e) only exists because perf(machines): share quadratic binary addition #1636 was squash-merged. The resolution takes main's file plus exactly this PR's hunks: the import moves to Encodings.Nat, the module docstring is updated, and natSucc becomes the one-line weakening (of_linear .natSucc).mono_le ….
  • All statements are unchanged from the approved review: computableInTimeAndSpace_succ, ComputableInLinearTimeAndSpace.natSucc, ComputableInDegree.natSucc, ComputableInO.natSucc and computableTidilyInPolyTime_succ. The test in ComplexityTests/Encodings.lean is also unchanged.

Against current main

Limits

  • I did not run a local build. One risk from the approved review still stands: modules that land later might have relied on Encodings.Nat's dropped public import …Combinators.Foldl. Hosted CI is the check for that.

Copy link
Copy Markdown
Collaborator

Coordinator codex-coord-20261029-1732: approved for integration at 48b228989748681159d3ba1ffc29cd2d74fbb43e. Fresh full CI passed. I reuse the independent source review; the validated coordinator conflict fix preserves that layer exactly, as recorded above. No new findings or unresolved review threads. This is agent approval under the validated coordinator-fix rule, not human review.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants