diff --git a/research/chain-necklace-generator/PROOF.md b/research/chain-necklace-generator/PROOF.md new file mode 100644 index 0000000000..4477918f6d --- /dev/null +++ b/research/chain-necklace-generator/PROOF.md @@ -0,0 +1,159 @@ +# Direct generation of valid cyclic chain schedules + +This contribution was produced by Nick Petalas. Apache-2.0. + +## Contract and contribution + +Use SHIN09-git's [PR6080](https://github.com/CrocSwap/integer-mult-bounds/pull/6080) +depth-three contract. A word has `m` copies of each of the letters `1,2,3`. +Write `A,B,T` for its scattered subsequence counts `12,23,123`. It is valid +for target `c` when `A=B=0` and `T=c` modulo `m`. In the four-role dirty chain, +these are the complete endpoint conditions, with arbitrary compatible maps +and arbitrary initial data. This endpoint characterization and its rotation +invariance are cited predecessor results. We do not audit or extend them. + +We construct exactly the lexicographically smallest word in each valid +rotation class. The new step is a suffix-residue completion test joined to +classical fixed-content necklace generation. The written construction works +for every positive integer `m`. The supplied bounded implementation accepts +only `1<=m<=5`, and `0<=c (A,B,T), +append 2: (A,B,T) -> (A+a,B,T), +append 3: (A,B,T) -> (A,B+b,T+A). +``` + +Reduce each coordinate modulo `m` and take a set union. Every nonempty word +has exactly one last letter. Induction on length therefore proves equality +with the defined `D[r]`, including both existence and exhaustiveness. Every +predecessor cell precedes its successor in the chosen order. + +There are `(m+1)^3` cells and at most `m^3` states per cell. There are at most +`3*m^3*(m+1)^3` state transitions. Thus preprocessing needs `O(m^6)` residue +operations and state storage. This is an arithmetic/state bound, not a +worst-case claim about the runtime of a language's hash table. + +## Necessary and sufficient completion test + +Let a prefix have counts `(a,b,d)` and residues `(A,B,T)`. Its suffix must +have counts `r=(m-a,m-b,m-d)`. Let `(u,v,w)` be the suffix's residues. Split +each counted subsequence at the concatenation boundary. Exactly + +```text +A_total = A + u + a*r2, +B_total = B + v + b*r3, +T_total = T + w + A*r3 + a*v. +``` + +The triple count has four possibilities: all in the prefix, all in the +suffix, its first two letters in the prefix, or only its first letter in +the prefix. No commuting-map assumption enters these integer identities. + +Consequently define, modulo `m`, + +```text +u0 = -A-a*r2, +v0 = -B-b*r3, +w0 = c-T-A*r3-a*v0. +``` + +A balanced valid completion exists if and only if `(u0,v0,w0)` belongs to +`D[r]`. Necessity follows by substitution. For sufficiency, the exact table +supplies a suffix word with these residues, and the three concatenation +identities prove validity. This is a completion test among unrestricted +suffix words. It need not certify that a completion is a canonical necklace. +Its use as a rejection test is nevertheless exact and safe. + +## Canonical traversal and safe pruning + +We use the classical prenecklace extension theorem, not a new necklace +algorithm. A prenecklace is a prefix of a lexicographically least rotation. +Track the length `p` of its longest Lyndon prefix. At the next one-based +position `t`, the permissible symbols are at least `word[t-p]`. Equality +retains `p`; a larger symbol replaces `p` by `t`. At length `n`, such a +prenecklace is a necklace exactly when `p` divides `n`. See Sawada, +[A fast algorithm to generate necklaces with fixed content](https://www.socs.uoguelph.ca/~sawada/papers/alph.pdf), +Theorem 1 and Figure 1, following Fredricksen, Kessler, Maiorana and Ruskey's +necklace-generation lineage. We use the simple extension rule, not Sawada's +constant-amortized-time refinement. + +Start with letter `1`, since every balanced necklace contains that least +letter and begins with it. Extend only when the letter count remains at +most `m`. Update the prefix residues and apply the suffix test above. +Reject a prefix only when the test fails. At length `3m`, emit only if +`p` divides `3m`. Use a depth-first explicit stack, exploring the smaller +symbol first. + +Every emitted word has the required counts and residues, by the completion +test with an empty suffix. The cited extension theorem makes it canonical. +Conversely, take any valid necklace. Every prefix obeys the extension rule +and count bound. Its actual remaining suffix witnesses table membership, +so no prefix is pruned. Its terminal period divides `3m`, so it is emitted. +Different traversal paths have different words. This proves completeness +and uniqueness, including periodic necklaces. It also gives lexicographic +output order without rotation canonicalization. + +The stack has depth at most `3m` and at most `1+6m` pending nodes. Stored +prefix tuples occupy `O(m^2)` space. The result list adds output storage. +The implementation bounds popped nodes by the full ternary-tree size +`(3^(3m+1)-1)/2`; its input cap makes this bound explicit. No output-sensitive +or polynomial total-time theorem is asserted for the traversal. + +## First working example and exact comparison + +At `(5,1)` the complete suffix table has 216 cells and 9,438 states. Building +it performs 20,912 state transitions. Canonical traversal pops 3,888 nodes, +tries 9,266 symbols, performs 7,007 prefix transitions and 7,007 completion +lookups, rejects 3,120 prefixes, and tests 520 full prenecklaces. The period +test emits 356 words. It performs no rotation tests. All these counts include +the whole table, including cells not subsequently queried. + +The separate literal method enumerates all 756,756 balanced words, performs +3,783,780 middle-letter scans, finds 5,340 valid words and canonicalizes +them using 80,100 rotations. Each middle-letter scan computes the numbers +of preceding `1`s and following `3`s. Their product contributes to `T`. +Its set of canonical words agrees exactly with the generator's output. + +These counters measure named algorithmic operations. They do not pretend +that a table transition, Python tuple copy, set lookup and integer comparison +have identical cost. `benchmark.py` separately times five alternating calls +to the complete two implementations in fresh child processes. Timed regions +include startup, imports, table construction or literal enumeration, +canonicalization when applicable, JSON serialization of every output word, +and process exit. The parent checks complete returned sets after each pair, +outside timing. + +## Scope and credit + +This is a scoped constructive proof, exact finite conformance certificate +and local algorithm comparison. It adds no new cyclic-class formula, chain +existence theorem, or generally new necklace method. SHIN09-git supplies +the source question, cyclic contract and period conventions. Nick Petalas's +#6037/#4257/#4281/#4748 supply the credited count/endpoint/rotation background; +their programs and counts are not used to generate the new output. Ross +Richardson's #943/#934 retain the complete-call dirty-chain lineage. The +classical substring-count and dynamic-programming ingredients retain their +usual attribution. Exact source prose pins are in SOURCES.json. + +Rotation equivalence does not preserve every physical routing or precision +cost. Native realization, physical selection, ownership, dirty restoration, +precision, tape movement, stock, compiler admission and the full supplier +bill remain separate obligations. No selected assumption, complete +multiplication theorem or new kappa follows. Independent human and formal +proof-kernel review are unclaimed. diff --git a/research/chain-necklace-generator/README.md b/research/chain-necklace-generator/README.md new file mode 100644 index 0000000000..67ab20fed6 --- /dev/null +++ b/research/chain-necklace-generator/README.md @@ -0,0 +1,154 @@ +# Generate valid cyclic chain schedules without literal-word enumeration + +This contribution was produced by Nick Petalas. Apache-2.0. + +Scoped constructive proof with exact finite conformance and a local algorithm +comparison. This answers the representative-generation question in SHIN09-git's +[PR6080](https://github.com/CrocSwap/integer-mult-bounds/pull/6080). + +At `(m,c)=(5,1)`, the new generator emits the 356 credited valid cyclic classes +by testing 520 complete prenecklaces, with no rotation canonicalization. Its +complete preprocessing takes 20,912 residue transitions and stores 9,438 states +in 216 cells. Traversal pops 3,888 prefixes, tries 9,266 symbols, performs +7,007 prefix updates and completion lookups, and rejects 3,120 prefixes. +The separate literal comparator examines all 756,756 balanced words and +canonicalizes 5,340 valid words using 80,100 rotations. The complete output +sets agree. These named work counters are not interchangeable machine costs. + +The construction joins an exact suffix-residue completion test to the +classical fixed-content prenecklace extension rule. [PROOF.md](PROOF.md) proves +safe pruning and exactly-once output for all positive moduli as an abstract +algorithm. The bounded implementation supports only `1<=m<=5`; its full +finite conformance covers every target in that range. The modulus-one case +is a combinatorial convention, not a characteristic-one ring. + +## Reproduce + +From the repository root, standard-library CPython 3.12: + +```sh +/Users/nick/git/integer-mult-bounds/.venv/bin/python3.12 -I -W error -B research/chain-necklace-generator/generate.py +/Users/nick/git/integer-mult-bounds/.venv/bin/python3.12 -I -W error -B research/chain-necklace-generator/verify.py +/Users/nick/git/integer-mult-bounds/.venv/bin/python3.12 -I -O -W error -B research/chain-necklace-generator/verify.py +/Users/nick/git/integer-mult-bounds/.venv/bin/python3.12 -B scripts/research_package.py --package research/chain-necklace-generator +/Users/nick/git/integer-mult-bounds/.venv/bin/python3.12 -B scripts/check_research_commit.py --package research/chain-necklace-generator --check-source-history +``` + +Any CPython 3.12 executable can replace the local path. `generate.py` prints +all 356 words using the source's letters `1,2,3`, together with work counters. +Internally the code uses `0,1,2`. The direct verifier prints a generated JSON +receipt bound to all eight package files and `PASS_CHAIN_NECKLACE_GENERATOR`. +It writes no files. No receipt is handwritten or committed. + +The verifier compares all 15 target cases through modulus five against a +separate positional enumerator, with 3,927,606 literal word evaluations and +2,510 emitted representatives in total across those cases. Every emitted +word receives a complete four-role integer-matrix endpoint check. Small +suffix tables cover 187 distinct residue states checked against independently +constructed matrix-signature sets, and 210 completion queries. Eleven controls cover invalid inputs, periodic +classes, canonical orientation and chronological direction. In particular, +the `(3,1)` fixture retains two period-three classes and two period-nine +classes; filtering out all repeated words would be wrong. + +## Local comparison + +To reproduce the complete fresh-process comparison: + +```sh +/Users/nick/git/integer-mult-bounds/.venv/bin/python3.12 -I -W error -B research/chain-necklace-generator/benchmark.py +``` + +The script runs five alternating pairs at `(5,1)`, checks every returned word, +counts errors, and emits raw samples, environment, work counts and code hashes. +Every timed region includes process startup, imports, the complete computation, +JSON output of all 356 words and process exit. Each generator run rebuilds its +table. Parent-side output comparison is outside timing. No lazy iterator or +persisted cache is used. Timing is an optional local observation, not a CI +acceptance threshold or a comparison with the fastest possible generator. + +On macOS 27.0.1 arm64 with CPython 3.12.15, the five-pair median complete +process time was 0.034534 seconds for the generator and 0.651576 seconds +for the literal comparator. The respective ranges were +0.033659 to 0.035091 seconds and 0.646261 to 0.653718 seconds. All ten processes +succeeded, with zero errors and five complete output-set comparisons. +Raw generator samples in seconds were `0.034534042, 0.035090750, 0.033994666, 0.033658959, 0.034655708`; +corresponding literal samples were `0.653717667, 0.646840042, 0.651576250, 0.652623667, 0.646261417`. +The shared host had 18 logical cores and load averages 1.86/2.80/3.06 before +measurement. Other processes remained running; interleaving exposes both +methods to that environment. These observations are local, not a guarantee +on another machine or a supplier runtime claim. + +The literal comparator uses compiled binary searches to count the preceding +and following letters, and only canonicalizes words already known valid. +A separate profiling run identifies its full-word scan and binary searches +as the main computational cost. The new computation spends its remaining +work in prenecklace traversal, suffix construction and completion lookups. +Both run serially with the same interpreter and flags. No native supplier, +network or disk workload occurs in either computation. + +## Scope and credit + +SHIN09-git supplies the question, rotation/period contract and credited count +in PR6080. Nick Petalas's #6037/#4257/#4281/#4748 are count, endpoint and +rotation background; none of their code or stored count tables is imported +or replayed. Ross Richardson's #943/#934 retain the complete-call dirty-chain +lineage. The prenecklace theorem and fixed-content generation are classical; +credit Fredricksen, Kessler, Maiorana, Ruskey and Joe Sawada's +[2003 paper](https://www.socs.uoguelph.ca/~sawada/papers/alph.pdf), Theorem 1 and +Figure 1. The new contribution is the suffix-completion application, generator +and its exact finite work comparison, not a new general necklace algorithm. + +[SOURCES.json](SOURCES.json) pins source prose at canonical base +`e54523517d0f03e58352e647c403a5af3e525a57`. Those files are historical provenance, +not runtime inputs. The source PR's head is +`abdcee87d8eb0f46db522b16596406f43e890d7e`. [TASK.json](TASK.json) records the +explicit change from `formal-interface-lemma` to this combinatorial side +question. The selected-baseline orientation passed with #7000 selected; +it supplies source-binding orientation only. The selected frontier is unchanged. +[Advance CLAIM](https://github.com/CrocSwap/integer-mult-bounds/discussions/1166#discussioncomment-19592067). + +There is no new class-count formula, constant-amortized-time result, native +implementation, paid-profile saving, selected-assumption discharge, new kappa +or complete multiplication theorem. Physical routing, ownership, restoration, +precision, tape movement, stock, compiler and complete consumer costs remain +unpriced. No predecessor scientific suite, selected cold replay, Lean or Docker +run is included. Independent human and formal proof-kernel review are unclaimed. + +## Implementation bounds + +All retained Python functions fit within 60 physical lines. There is no +recursion, dynamic evaluation or network access. The modulus cap is five, +each suffix cell has at most 125 residues, and there are at most 216 cells. +Traversal has an explicit ternary-tree node bound and depth at most 15. +Matrix checks use four rows; receipt hashing accepts eight files capped at +256 KiB each. Explicit exceptions remain active under optimized execution. + +Power of Ten rule 3 has a managed-runtime deviation in all three scripts. +Python allocates tuples, sets, dictionaries and exact integers during these +bounded computations. The caps above, stack depth, bounded output domain and +fixed source inventory constrain live storage. Runtime allocation internals +are not measured. A native fixed-buffer implementation would be required +before claiming allocation-free execution or lifting these limits. + +Rule 9 has a standard-library dispatch deviation for iterators, sorting, +JSON and argument parsing. Targets are the named standard-library functions +and the two fixed comparison methods. The optional benchmark launches exactly +ten sequential bounded children, each with a 30-second timeout and checked +exit status, stderr and complete output. The registered scientific verifier +launches no child process. Warning-strict normal/optimized execution and AST +inspection provide local diagnostics; Ruff is unavailable in the specified +environment. No shared diagnostic configuration is changed. + +## Open next questions + +1. Can the suffix-state representation support a specified larger modulus + with a useful memory bound? First estimate or prove the reachable cell + sizes before raising the implementation cap. No output-sensitive traversal + bound is supplied here. +2. Can an actual chain consumer use these representatives at lower complete + cost? First specify its physical routing, precision and restored-owner + contract and price the emitted schedules. Rotation equivalence alone + supplies no such saving. + +The structured questions and blockers are in TASK.json. No extension is +reserved or implemented after this first verified generator. diff --git a/research/chain-necklace-generator/SOURCES.json b/research/chain-necklace-generator/SOURCES.json new file mode 100644 index 0000000000..ee4e244dc5 --- /dev/null +++ b/research/chain-necklace-generator/SOURCES.json @@ -0,0 +1,40 @@ +{ + "schema_version": 1, + "base_commit": "e54523517d0f03e58352e647c403a5af3e525a57", + "files": [ + { + "path": "certificates/selected-result.json", + "usage": "provenance", + "git_blob": "75d77b328ea02ded535e7f23aee5b1e55d0bb1a8", + "sha256": "154c3d30455d778fd3343b6b4851f0fa4665f0f7625b0adb3675e2bafbbf6bf1" + }, + { + "path": "docs/research/agent-task-catalog.json", + "usage": "provenance", + "git_blob": "3404fbdd5e7529a0674eef26114b5071e4586cec", + "sha256": "ead6cdc9d0af6d58e8a49744d8ec8c7c15e13d2ba93c276c434bcf9b51b93a81" + }, + { + "path": "research/chain-cyclic-schedules/PROOF.md", + "usage": "provenance", + "git_blob": "d826b2e8f6f038cf5ed341e442149af13fd43ae2", + "sha256": "faffad9f8f947818e7debff730de86bf3bbdbfa9b1ad72e13817a43e58085c85" + }, + { + "path": "research/chain-cyclic-schedules/README.md", + "usage": "provenance", + "git_blob": "7b887b49d662a634576450f0c4115f064439a658", + "sha256": "f09a86865ae702b54188c6706bd33d76890afdbd829900f1aefbae9a4a867e74" + } + ], + "bibliography": [ + { + "title": "A fast algorithm to generate necklaces with fixed content", + "author": "Joe Sawada", + "year": 2003, + "doi": "10.1016/S0304-3975(03)00049-5", + "url": "https://www.socs.uoguelph.ca/~sawada/papers/alph.pdf", + "used": "Theorem 1 and Figure 1, credited prenecklace extension theorem; not a runtime input." + } + ] +} diff --git a/research/chain-necklace-generator/TASK.json b/research/chain-necklace-generator/TASK.json new file mode 100644 index 0000000000..83c52d876b --- /dev/null +++ b/research/chain-necklace-generator/TASK.json @@ -0,0 +1,110 @@ +{ + "schema_version": 1, + "task_id": "formal-interface-lemma", + "priority_revision": 8, + "base_commit": "e54523517d0f03e58352e647c403a5af3e525a57", + "original_card": { + "id": "formal-interface-lemma", + "question": "Can one uncovered interface implication be formalized with exact caller and certificate bindings?", + "starting_points": [ + "docs/research/research-family-status.md", + "certificates/selected-result.json", + "research/gate-operand-parity-lean/README.md", + "research/staged544-certified-endpoint/README.md", + "research/unconditional-batched-crt/README.md" + ], + "methods": [ + "Formalize a missing boundary lemma identified by the selected review.", + "Audit a formal theorem-to-certificate binding independently." + ], + "acceptance_gate": "State the exact formalized proposition and remaining assumptions; avoid duplicate arithmetic formalizations already present.", + "first_step": { + "command": [ + "python3", + "-B", + "scripts/research_selected_baseline.py" + ], + "expected": "PASS_RESEARCH_BASELINE_BINDINGS_ONLY", + "inspect": "Resolve the current selected review, separate complex/ordinary BIT/admitted BIT/outer budgets, and compare the card against live handoffs before starting. Historical ceilings require exact source/model equivalence.", + "scope": "Source-binding orientation only; not a scientific replay or all-size proof. Materialize selected inputs if this checkout is sparse." + }, + "first_experiment": "Write one missing statement with quantified hypotheses, actual caller and existing coverage. A compact-switch lemma is a candidate only after its mathematical contract is reviewed. Establish a bounded proof plan before a separately budgeted proof-assistant build; do not rerun large Lean jobs as orientation.", + "bottleneck_link": "A missing interface proof can reduce the conditional gap even if it does not change kappa.", + "allowed_variations": [ + "Narrow to one formal lemma or one theorem-to-certificate binding." + ], + "scope_change_triggers": [ + "Formalizing already-covered arithmetic and claiming the full multiplication theorem." + ], + "overlap_terms": [ + "hyperplane end", + "formal interface", + "Lean binding", + "3807", + "3857", + "4100", + "gate-operand-parity-lean", + "staged544-certified-endpoint", + "unconditional-batched-crt" + ], + "existing_checks": [ + { + "scope": "PR #3807: scoped gate-operand parity formalization.", + "evidence": "research/gate-operand-parity-lean/README.md", + "remaining": "Exact theorem-to-caller binding and genuinely uncovered interface lemmas.", + "status": "author-checked", + "source_commit": "4c3530f42a6abcc48e7a6bcffd6c901911dc8269", + "selected_record_sha256": "bb2eaa58647b353f88d0aec2eafb9d30df1be53589231b23e12ceef3e4bad129" + }, + { + "scope": "PR #3857: certified real-power moment/endpoint arithmetic.", + "evidence": "research/staged544-certified-endpoint/README.md", + "remaining": "Uniform algorithmic implications, not another endpoint digit.", + "status": "author-checked", + "source_commit": "ef4315b8caa605e47a2515004845eb2d2ed71a86", + "selected_record_sha256": "bb2eaa58647b353f88d0aec2eafb9d30df1be53589231b23e12ceef3e4bad129" + }, + { + "scope": "PR #4100: compact-switch formalization proposed as future validation.", + "evidence": "research/unconditional-batched-crt/README.md", + "remaining": "Audit the statement and assumptions before a formal proof campaign.", + "status": "author-checked", + "source_commit": "337d04939571a637aeab54a0f58f54fde811663a", + "selected_record_sha256": "bb2eaa58647b353f88d0aec2eafb9d30df1be53589231b23e12ceef3e4bad129" + } + ] + }, + "scope_status": "changed", + "actual_scope": "Construct a suffix-pruned canonical generator for valid balanced depth-three cyclic chain schedules, prove safe completion pruning, and certify the first working instance m=5,c=1.", + "scope_change_reason": "The assignment requests original work rather than an audit. This takes Open next question 1 of SHIN09-git PR6080 as a constructive combinatorial side result; it does not formalize a selected caller.", + "bottleneck_link": "This removes literal-word enumeration and rotation deduplication from the named schedule exploration step. It does not change the complete paid supplier bill or discharge a selected framework assumption.", + "budget_minutes": 60, + "stop_rule": "Stop at the first verified representative generator and its complete finite work comparison. Do not extend the modulus or seek another result.", + "outcome": "completed", + "evidence": "PROOF.md gives the suffix-composition and canonical completeness proofs. verify.py compares every target for 1<=m<=5 with a separate literal positional enumerator, checks emitted endpoints by four-role matrices, and emits a package-hash receipt. benchmark.py compares five alternating fresh-process pairs.", + "author": "Nick Petalas", + "ai_assistance": "Independent human and formal proof-kernel review are unclaimed.", + "open_questions": [ + { + "question": "Can the same completion-state representation support a specified larger modulus with a useful memory bound?", + "why_it_matters": "The supplied implementation deliberately stops at modulus five.", + "first_step": "Estimate or prove the reachable suffix-state counts before raising the implementation cap.", + "blocked_by": "No output-sensitive or improved general traversal-time bound is supplied." + }, + { + "question": "Can a physically specified chain consumer use the generated classes at lower complete cost?", + "why_it_matters": "Rotation equivalence alone does not preserve routing or precision costs.", + "first_step": "Choose one actual caller and price each emitted schedule including restoration and hosting.", + "blocked_by": "No native consumer, physical cost contract or supplier invoice is supplied." + } + ], + "claim_type": [ + "scoped constructive proof", + "exact finite conformance certificate", + "local algorithm comparison" + ], + "source_question": "SHIN09-git PR #6080 Open next question 1", + "predecessor_author": "SHIN09-git", + "claim_url": "https://github.com/CrocSwap/integer-mult-bounds/discussions/1166#discussioncomment-19592067", + "playbook": "Investigation" +} diff --git a/research/chain-necklace-generator/VERIFY.json b/research/chain-necklace-generator/VERIFY.json new file mode 100644 index 0000000000..28470f8d2b --- /dev/null +++ b/research/chain-necklace-generator/VERIFY.json @@ -0,0 +1,34 @@ +{ + "schema_version": 1, + "status": "ready", + "python": "3.12", + "requirements": [], + "inputs": [], + "commands": [ + { + "argv": [ + "{python}", + "-I", + "-W", + "error", + "-B", + "{package}/verify.py" + ], + "timeout_seconds": 120, + "checks": "Complete finite conformance, full emitted matrix endpoints, suffix membership, periodic boundary, invalid inputs and package-hash receipt." + }, + { + "argv": [ + "{python}", + "-I", + "-O", + "-W", + "error", + "-B", + "{package}/verify.py" + ], + "timeout_seconds": 120, + "checks": "Same complete checks with assertions disabled." + } + ] +} diff --git a/research/chain-necklace-generator/benchmark.py b/research/chain-necklace-generator/benchmark.py new file mode 100644 index 0000000000..3ac28d261e --- /dev/null +++ b/research/chain-necklace-generator/benchmark.py @@ -0,0 +1,64 @@ +"""Five alternating fresh-process comparisons; JSON output, no file writes.""" + +import argparse +import hashlib +import json +import platform +import statistics +import subprocess +import sys +from pathlib import Path +from time import perf_counter + +sys.path.insert(0, str(Path(__file__).resolve().parent)) +from generate import representatives +from verify import literal_classes, need + + +def emit_method(method: str) -> None: + if method == "generator": + words, work = representatives(5, 1) + else: + words, work = literal_classes(5, 1) + print(json.dumps(dict(words=sorted(words), work=work), sort_keys=True)) + + +def main() -> None: + samples = [] + for trial in range(5): + order = ("generator", "literal") if trial % 2 == 0 else ("literal", "generator") + results = {} + elapsed = {} + for method in order: + begin = perf_counter() + run = subprocess.run([sys.executable, "-I", "-W", "error", "-B", + str(Path(__file__).resolve()), "--method", method], + capture_output=True, text=True, timeout=30, check=True) + elapsed[method] = perf_counter() - begin + need(not run.stderr, "timed child emitted diagnostics") + results[method] = json.loads(run.stdout) + words, work = results["generator"]["words"], results["generator"]["work"] + baseline = results["literal"]["words"] + baseline_work = results["literal"]["work"] + need(len(words) == 356 and words == baseline, "timed outputs differ") + samples.append(dict(order=order, seconds=elapsed, generator_work=work, + literal_work=baseline_work)) + medians = {method: statistics.median(s["seconds"][method] for s in samples) + for method in ("generator", "literal")} + paths = ("generate.py", "verify.py", "benchmark.py") + hashes = {name: hashlib.sha256(Path(__file__).with_name(name).read_bytes()).hexdigest() + for name in paths} + print(json.dumps(dict(python=sys.version, platform=platform.platform(), + samples=samples, medians=medians, code_sha256=hashes, + errors=0, complete_output_comparisons=5), + sort_keys=True)) + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--method", choices=("generator", "literal")) + arguments = parser.parse_args() + if arguments.method: + emit_method(arguments.method) + else: + main() diff --git a/research/chain-necklace-generator/generate.py b/research/chain-necklace-generator/generate.py new file mode 100644 index 0000000000..1bdf26b2b3 --- /dev/null +++ b/research/chain-necklace-generator/generate.py @@ -0,0 +1,105 @@ +"""Exact suffix-pruned chain necklaces; standard-library Python 3.12.""" + +import json +from itertools import product + +MAX_MODULUS = 5 + + +def domain(modulus: int, target: int) -> None: + if type(modulus) is not int or not 1 <= modulus <= MAX_MODULUS: + raise ValueError("modulus must be an integer in 1..5") + if type(target) is not int or not 0 <= target < modulus: + raise ValueError("target must be an integer in 0..modulus-1") + + +def step(counts: tuple[int, ...], residue: tuple[int, ...], + symbol: int, modulus: int) -> tuple[int, int, int]: + """Append 0, 1 or 2, corresponding to the chain's letters 1, 2, 3.""" + a, b, t = residue + if symbol == 1: + a += counts[0] + elif symbol == 2: + b += counts[1] + t += residue[0] + return a % modulus, b % modulus, t % modulus + + +def suffix_table(modulus: int) -> tuple[dict, dict]: + domain(modulus, 0) + table = {(0, 0, 0): {(0, 0, 0)}} + work = {"table_transitions": 0, "table_cells": (modulus + 1) ** 3} + for counts in product(range(modulus + 1), repeat=3): + if counts == (0, 0, 0): + continue + residues = set() + for symbol in range(3): + if counts[symbol] == 0: + continue + previous = tuple(n - (j == symbol) for j, n in enumerate(counts)) + for residue in table[previous]: + residues.add(step(previous, residue, symbol, modulus)) + work["table_transitions"] += 1 + table[counts] = residues + work["table_states"] = sum(len(values) for values in table.values()) + return table, work + + +def can_finish(modulus: int, target: int, counts: tuple[int, ...], + residue: tuple[int, ...], table: dict) -> bool: + remaining = tuple(modulus - n for n in counts) + a, b, t = residue + need_a = (-a - counts[0] * remaining[1]) % modulus + need_b = (-b - counts[1] * remaining[2]) % modulus + need_t = (target - t - a * remaining[2] - counts[0] * need_b) % modulus + return (need_a, need_b, need_t) in table[remaining] + + +def representatives(modulus: int, target: int) -> tuple[tuple, dict]: + """Return each lexicographically least valid rotation exactly once.""" + domain(modulus, target) + table, work = suffix_table(modulus) + work.update(nodes=0, symbol_trials=0, prefix_transitions=0, + suffix_lookups=0, suffix_rejections=0, terminal_tests=0, + rotation_tests=0, outputs=0) + length = 3 * modulus + stack = [((0,), (1, 0, 0), (0, 0, 0), 1)] + result = [] + node_bound = (3 ** (length + 1) - 1) // 2 + for _ in range(node_bound): + if not stack: + break + word, counts, residue, period = stack.pop() + work["nodes"] += 1 + if len(word) == length: + work["terminal_tests"] += 1 + if length % period == 0: + if residue != (0, 0, target): + raise RuntimeError("completion invariant failed") + result.append(word) + continue + lower = word[len(word) - period] + for symbol in range(2, lower - 1, -1): + work["symbol_trials"] += 1 + if counts[symbol] == modulus: + continue + next_counts = tuple(n + (j == symbol) for j, n in enumerate(counts)) + next_residue = step(counts, residue, symbol, modulus) + work["prefix_transitions"] += 1 + work["suffix_lookups"] += 1 + if not can_finish(modulus, target, next_counts, next_residue, table): + work["suffix_rejections"] += 1 + continue + next_period = period if symbol == lower else len(word) + 1 + stack.append((word + (symbol,), next_counts, next_residue, next_period)) + else: + raise RuntimeError("finite tree bound exhausted") + work["outputs"] = len(result) + return tuple(result), work + + +if __name__ == "__main__": + words, counts = representatives(5, 1) + print(json.dumps({"modulus": 5, "target": 1, "work": counts, + "representatives": ["".join(str(x + 1) for x in w) + for w in words]}, sort_keys=True)) diff --git a/research/chain-necklace-generator/verify.py b/research/chain-necklace-generator/verify.py new file mode 100644 index 0000000000..e6dc5f62e1 --- /dev/null +++ b/research/chain-necklace-generator/verify.py @@ -0,0 +1,154 @@ +"""Independent literal comparison and exact checks; writes only to stdout.""" + +import hashlib +import json +import sys +from bisect import bisect_left, bisect_right +from itertools import combinations, product +from pathlib import Path + +sys.path.insert(0, str(Path(__file__).resolve().parent)) +from generate import can_finish, domain, representatives, suffix_table + + +def need(condition: bool, message: str) -> None: + if not condition: + raise RuntimeError(message) + + +def matrix_signature(word: tuple[int, ...]) -> tuple[int, int, int]: + rows = [[int(i == j) for j in range(4)] for i in range(4)] + for symbol in word: + source, target = rows[symbol], rows[symbol + 1] + rows[symbol + 1] = [x + y for x, y in zip(source, target)] + return rows[2][0], rows[3][1], rows[3][0] + + +def canonical(word: tuple[int, ...]) -> tuple[int, ...]: + return min(word[j:] + word[:j] for j in range(len(word))) + + +def literal_classes(modulus: int, target: int) -> tuple[set, dict]: + domain(modulus, target) + length = 3 * modulus + classes = set() + work = dict(literal_words=0, pair_scans=0, valid_words=0, rotation_tests=0) + for ones in combinations(range(length), modulus): + one_set = set(ones) + rest = tuple(j for j in range(length) if j not in one_set) + for twos in combinations(rest, modulus): + work["literal_words"] += 1 + two_set = set(twos) + threes = tuple(j for j in rest if j not in two_set) + a = b = t = 0 + for middle in twos: + left = bisect_left(ones, middle) + right = modulus - bisect_right(threes, middle) + a += left + b += right + t += left * right + work["pair_scans"] += 1 + if (a % modulus, b % modulus, t % modulus) != (0, 0, target): + continue + word = tuple(0 if j in one_set else 1 if j in two_set else 2 + for j in range(length)) + classes.add(canonical(word)) + work["valid_words"] += 1 + work["rotation_tests"] += length + return classes, work + + +def small_suffix_checks() -> dict: + states = queries = 0 + for modulus in range(1, 4): + table, _ = suffix_table(modulus) + for counts in product(range(min(2, modulus) + 1), repeat=3): + words = (w for w in product(range(3), repeat=sum(counts)) + if tuple(w.count(j) for j in range(3)) == counts) + signatures = {tuple(x % modulus for x in matrix_signature(w)) + for w in words} + need(table[counts] == signatures, "suffix table differs from matrices") + states += len(signatures) + for size in range(4): + for prefix in product(range(3), repeat=size): + counts = tuple(prefix.count(j) for j in range(3)) + if max(counts, default=0) > modulus: + continue + a, b, t = matrix_signature(prefix) + remaining = tuple(modulus - n for n in counts) + for target in range(modulus): + expected = any((a + aa + counts[0] * remaining[1]) % modulus == 0 + and (b + bb + counts[1] * remaining[2]) % modulus == 0 + and (t + tt + a * remaining[2] + counts[0] * bb) + % modulus == target + for aa, bb, tt in table[remaining]) + need(can_finish(modulus, target, counts, (a, b, t), table) + == expected, "completion composition differs") + queries += 1 + return dict(suffix_states_checked=states, completion_queries=queries) + + +def controls() -> int: + cases = [(0, 0), (6, 0), (True, 0), (2, -1), (2, 2), (2, True)] + for modulus, target in cases: + try: + representatives(modulus, target) + except ValueError: + continue + raise RuntimeError("invalid domain accepted") + words, _ = representatives(3, 1) + need(len(words) == 4, "periodic boundary lost") + periods = [min(j for j in range(1, len(w) + 1) + if w == w[j:] + w[:j]) for w in words] + need(sorted(periods) == [3, 3, 9, 9], "nonprimitive classes lost") + need(canonical((2, 0, 1)) != (2, 0, 1), "noncanonical control failed") + need(matrix_signature((0, 1, 2) * 3)[2] % 3 == 1, "cubic direction lost") + need(matrix_signature((2, 1, 0))[2] == 0, "reversal control lost") + return len(cases) + 5 + + +def package_hashes() -> dict: + names = ("README.md", "PROOF.md", "SOURCES.json", "TASK.json", "VERIFY.json", + "generate.py", "verify.py", "benchmark.py") + root = Path(__file__).resolve().parent + need({p.name for p in root.iterdir() if p.is_file()} == set(names), + "unexpected package inventory") + hashes = {} + for name in names: + path = root / name + need(path.stat().st_size <= 262144, "oversized package source") + hashes[name] = hashlib.sha256(path.read_bytes()).hexdigest() + return hashes + + +def main() -> None: + cases = [] + for modulus in range(1, 6): + for target in range(modulus): + words, work = representatives(modulus, target) + baseline, literal_work = literal_classes(modulus, target) + need(len(words) == len(set(words)), "duplicate output") + need(words == tuple(sorted(words)), "output order is not lexicographic") + need(set(words) == baseline, "literal class mismatch") + for word in words: + need(word == canonical(word), "noncanonical output") + need(tuple(word.count(j) for j in range(3)) == (modulus,) * 3, + "wrong content") + need(tuple(x % modulus for x in matrix_signature(word)) + == (0, 0, target), "literal matrix endpoint failed") + cases.append(dict(modulus=modulus, target=target, + generator=work, literal=literal_work, + words_sha256=hashlib.sha256(bytes(x for w in words + for x in w)).hexdigest())) + first = next(row for row in cases if (row["modulus"], row["target"]) == (5, 1)) + need(first["generator"]["outputs"] == 356, "first working example failed") + checks = small_suffix_checks() + checks["controls"] = controls() + checks["cases"] = cases + checks["files"] = package_hashes() + print(json.dumps(checks, sort_keys=True)) + print("PASS_CHAIN_NECKLACE_GENERATOR") + + +if __name__ == "__main__": + main()