diff --git a/research/fanin4-aligned-source-stream-seed9983a614/PROOF.md b/research/fanin4-aligned-source-stream-seed9983a614/PROOF.md new file mode 100644 index 0000000000..57bbb067e7 --- /dev/null +++ b/research/fanin4-aligned-source-stream-seed9983a614/PROOF.md @@ -0,0 +1,80 @@ +# FANIN4 stream identity and cost + +## Model + +Let L be a positive integer. There are four read-only source tapes containing +x0,...,x3 in F2^L and one writable target tape z in F2^L. The five tapes are +aligned in one fixed coefficient order and begin at head position zero. +One lane transition reads a source bit and advances that tape by one cell. +The target lane transition reads z[i], writes its updated bit, and advances +the target by one cell. A complete call returns every participating head to +zero. Reads, writes, head steps, bit XORs, call records, and reference fields +are charged separately. There is no scratch or frame conversion. + +For this package, L=4 and the coefficient space is F2[C2^2]. All source maps +are the identity, so the connector request is exactly z <- z + sum_j xj. +The + symbol is coefficientwise addition in characteristic two. + +## Correctness + +The singleton schedule applies z[i] <- z[i] + xj[i] for j=0,1,2,3 in four +complete passes. Since the xj are read-only, their values do not change. +The final target lane is z[i] + x0[i] + x1[i] + x2[i] + x3[i]. + +The fused schedule scans lane i once across all five tapes. It reads z[i] and +each xj[i], then applies four binary XOR gates to z[i] before advancing. Thus +the same final lane value is obtained. Every source is unchanged. Repeating +the fused map adds the same unchanged sum to z a second time and restores the +original target. All five heads return by L reverse head steps. + +This proves the map for arbitrary dirty sources and target and every L. The +exhaustive L=4 verifier checks 2^(5L)=1,048,576 joint states. The verifier +also checks the inverse, all head positions, the source values, the exact +resource ledgers, and failure controls. + +## Resource accounting + +For s sources, L coefficients, and one ordinary source-update record per +singleton call: + +| Resource | Singleton sequence | Fused scan | +|---|---:|---:| +| Reads | 2sL | (s+1)L | +| Writes | sL | L | +| XORs | sL | sL | +| Forward head steps | 2sL | (s+1)L | +| Return head steps | 2sL | (s+1)L | +| Call records | s | 1 | +| Source-reference fields | s | s | +| Target-reference fields | s | 1 | +| Scratch | 0 | 0 | +| Frame transitions | 0 | 0 | + +For s=4, L=4 these evaluate to reads 32/20, writes 16/4, XORs 16/16, +head moves 64/40, records 4/1, source-reference fields 4/4, target-reference +fields 4/1. + +Let r,w,h,p,d1,ds,t be nonnegative prices for one data read, one data write, +one head step, one XOR, one singleton record, one fused FANIN-s record, and +one target-reference field. The singleton-minus-fused cost is + + (s-1)L*r + (s-1)L*w + 2(s-1)L*h + s*d1 - ds + (s-1)t. + +The XOR and source-reference terms cancel. If records have uniform price +d1=ds=d, this simplifies to + + (s-1)(L*r + L*w + 2L*h + d + t). + +Any one-time opcode-table or source-frame conversion expense F is subtracted +from the saving. In the fixed aligned-frame stream instance F=0; an external +supplier must price its own instruction encoding and frame conversion before +claiming a net gain. + +## Scope boundary + +The construction realizes one identity-map FANIN4 update in a fixed aligned +coefficient frame. It does not establish that a current recursive supplier +emits these four source roles in the same frame, that the operation can be +used in a full mask-cover word at lower total cost, or that any global +multiplication exponent improves. The abstract query/addition limits from PR +#2249 remain intact because all four source XORs are charged. diff --git a/research/fanin4-aligned-source-stream-seed9983a614/README.md b/research/fanin4-aligned-source-stream-seed9983a614/README.md new file mode 100644 index 0000000000..2bd595bce8 --- /dev/null +++ b/research/fanin4-aligned-source-stream-seed9983a614/README.md @@ -0,0 +1,125 @@ +# FANIN4 in an aligned coefficient stream + +**A local reversible-stream construction. No new kappa or supplier admission is claimed.** + +## Result + +For four read-only source vectors and one arbitrary dirty target in the common +coefficient frame of F2[C2^2], the update + + z <- z XOR x0 XOR x1 XOR x2 XOR x3 + +can be performed in one synchronized scan. Each coefficient still pays all +four XORs. Compared with four complete singleton updates, the fused scan +reduces reads, writes, target-head returns, and call-record overhead. It uses +no scratch and leaves each source and the target head at its entry position. + +The claim is deliberately local. The sources use identity maps and are already +aligned in the same coefficient order. This does not construct the full +63-mask connector, map FANIN4 into the selected supplier, or change kappa. It +does show a concrete physical stream realization for a higher-arity connector +step while retaining the full per-source addition charge from Cycle 43. + +## Exact bill + +For s sources and L aligned coefficients, each singleton pass reads its source +and the current target, writes the target, and returns both heads. The fused +pass reads all s sources and the target once, writes the target once, and +returns all s+1 heads. + +| Resource | s singleton updates | One FANIN-s | At s=4, L=4 | +|---|---:|---:|---:| +| Data reads | 2sL | (s+1)L | 32 vs 20 | +| Data writes | sL | L | 16 vs 4 | +| Bit XORs | sL | sL | 16 vs 16 | +| Forward plus return head moves | 4sL | 2(s+1)L | 64 vs 40 | +| Call records | s | 1 | 4 vs 1 | +| Source-reference fields | s | s | 4 vs 4 | +| Target-reference fields | s | 1 | 4 vs 1 | +| Scratch cells / cleanup | 0 | 0 | 0 vs 0 | +| Frame transitions | 0 | 0 | 0 vs 0 | + +If read, write, head-step, uniform per-record, and target-reference prices are +r, w, h, d, and t, respectively, the fused bill saves exactly + + (s-1) * (L*r + L*w + 2*L*h + d + t). + +The source-reference fields and sL arithmetic XORs are fully charged and +unchanged. A variable-width encoding can use separate record prices d1 for a +singleton and ds for FANIN-s; its exact saving is + + (s-1)*L*(r+w+2*h) + s*d1 - ds + (s-1)*t. + +Thus a differently sized fused record is charged explicitly. Any frame +conversion cost F is subtracted from this saving; it is zero only in the +fixed aligned-frame instance studied here. + +## Exact interface and proof + +The object is the coefficient vector space of F2[C2^2], with L=4. Every +source xj and target z is an arbitrary 4-bit vector. Source maps are identity. +All five tapes begin at lane 0 in the same basis order. + +At each lane i, the fused schedule reads the four source bits and z[i], applies +four binary XOR gates to z[i], writes the updated target bit, and advances all +five heads by one lane. At the end it returns all heads by four lanes. +Therefore each lane of the target becomes + + z[i] XOR x0[i] XOR x1[i] XOR x2[i] XOR x3[i]. + +Sources are never written. The target is arbitrary and dirty. The map is its +own inverse because each source bit is unchanged and adding the same sum twice +over F2 restores z. The symbolic argument holds for every L; the exact verifier +exhausts every dirty state at L=4. + +Four singleton calls apply the same four XORs in four scans. Their source and +target heads return after each call. The resource counts in the table follow +directly from these two schedules; the verifier checks both ledgers and the +complete state map. + +## Reproduce + +From the repository root: + + python3 -B research/fanin4-aligned-source-stream-seed9983a614/verify.py + python3 scripts/research_package.py --package research/fanin4-aligned-source-stream-seed9983a614 --check-only + +Expected verifier line: + + PASS_FANIN4_STREAM states=1048576 L=4 xor=16/16 reads=32/20 writes=16/4 head_moves=64/40 records=4/1 unit_saving=54 controls=6 + +The registered command is the package verifier. Only the standard library is +used. + +## Predecessors and limits + +- PR #2128 adds the two-source DUO connector word and notes that a real + supplier-level physical realization was still missing. +- Cycle 43 PR #2249 proves an exact query/addition floor for the six-source + arity-4..6 abstract connector. This construction keeps the full tap price; + it concerns the lower-level stream bill. +- PRs #2171 and #2207 implement ordered single-source controlled streams and + fallback descriptors. This package studies one aligned common-target fan-in, + not their selected rank-42 normalizer. + +No full batch path is supplied. The 63-mask recursive interface, invocation +placement, source-frame alignment in a live provider, head-motion pricing in +that provider, and any effect on the selected exponent remain open. + +## Open next questions + +Can two or more source operators from an emitted multiplication-supplier frame +be put in this aligned common-target layout without spending the saved traffic +on source-frame conversion? It matters because this package assumes identity +source maps in one fixed frame. A feasible first step is to name one live +supplier's source/target roles and derive its exact entry and return permutation +bill for one FANIN4 call. The blocker is that no such supplier mapping is +established here. + +## Credit + +The source-call connector model and DUO comparison are due to the authors of +PR #2128; the abstract six-source high-arity floor is recorded in PR #2249. +This package supplies the local fused scan and its complete stream bill. +Prepared by Cinna Davis with substantial OpenAI Codex assistance. New work is +Apache-2.0. diff --git a/research/fanin4-aligned-source-stream-seed9983a614/RESULT.json b/research/fanin4-aligned-source-stream-seed9983a614/RESULT.json new file mode 100644 index 0000000000..6fb7ac0247 --- /dev/null +++ b/research/fanin4-aligned-source-stream-seed9983a614/RESULT.json @@ -0,0 +1,40 @@ +{ + "schema_version": 1, + "status": "PASS_FANIN4_STREAM", + "base_commit": "0fdd7d82de03e43c9e5a1000dfbcd380d1b687d2", + "seed": "9983a6144fbaa6291d717b9b2076e816", + "frame": "F2[C2^2]", + "source_count": 4, + "width": 4, + "dirty_states_checked": 1048576, + "costs": { + "singleton_sequence": { + "reads": 32, + "writes": 16, + "xors": 16, + "head_moves": 64, + "call_records": 4, + "source_refs": 4, + "target_refs": 4, + "scratch_cells": 0, + "frame_transitions": 0 + }, + "fanin4": { + "reads": 20, + "writes": 4, + "xors": 16, + "head_moves": 40, + "call_records": 1, + "source_refs": 4, + "target_refs": 1, + "scratch_cells": 0, + "frame_transitions": 0 + } + }, + "weighted_saving": "(s-1)*(L*r+L*w+2*L*h+d+t)", + "selected_kappa_claimed": false, + "supplier_admission_claimed": false, + "uniform_unit_price_saving": 54, + "variable_record_saving": "(s-1)*L*(r+w+2*h)+s*d1-ds+(s-1)*t-F", + "F_scope": "0 within the fixed aligned-frame instance; external source conversion and opcode-table costs must be added" +} diff --git a/research/fanin4-aligned-source-stream-seed9983a614/SOURCES.json b/research/fanin4-aligned-source-stream-seed9983a614/SOURCES.json new file mode 100644 index 0000000000..9dbc1f237e --- /dev/null +++ b/research/fanin4-aligned-source-stream-seed9983a614/SOURCES.json @@ -0,0 +1,68 @@ +{ + "schema_version": 1, + "base_commit": "0fdd7d82de03e43c9e5a1000dfbcd380d1b687d2", + "files": [ + { + "path": "certificates/selected-result.json", + "usage": "provenance", + "git_blob": "ae6a99f28aa468649eb66a361e764d769363bf11", + "sha256": "10b9e340fd8e354a37a0c4813c90e5de1dd42044b5f6aca4a3fc5a42b685fa32" + }, + { + "path": "docs/research/agent-task-catalog.json", + "usage": "provenance", + "git_blob": "e8870e5917057d59b730eb14721a778639eca050", + "sha256": "d46eb6d5b3e279428753d770653663fbdf1063e55c255ea073d768f344927ef7" + }, + { + "path": "docs/research/community-batch1099-review.md", + "usage": "provenance", + "git_blob": "6cb6ec061a071a06a2d18cd4dc3e9a2b05d28099", + "sha256": "7e76d2c38e5fce30587835b7251df1430488ae4dd372611ef69a3f03f476b6cb" + }, + { + "path": "research/community-frontier1094-audit/SELECTED-SOURCE.json", + "usage": "provenance", + "git_blob": "6a75493790b4eff9c4a98051ef8b01d6aad0d5a8", + "sha256": "79b3de5db3dbcb9829789ba58e9e45758926031e2a7d80667a08429d369be53b" + }, + { + "path": "research/community-frontier1094-audit/ASSEMBLY.json", + "usage": "provenance", + "git_blob": "5fd676df060f3b450782fdf5ff5f6beb63a5eeb0", + "sha256": "1122cb3a575f8c788345fc1bf4c108ac7c3250fb81a5988671f5eb04b3ab3045" + }, + { + "path": "docs/research/community-batch1099-integration.json", + "usage": "provenance", + "git_blob": "9d60e86c0e494d17bec5e28eda41b275c9ede64e", + "sha256": "684c50d1ac69863e1fcc0a34bf76c54bac576f6bbde79b3c67d8432a495c8c1c" + } + ], + "external_references": [ + { + "label": "PR #2128", + "head": "697369a971eed02c0fbcd75ebefec9f59945f08e", + "url": "https://github.com/CrocSwap/integer-mult-bounds/pull/2128", + "usage": "related abstract DUO construction; no code imported" + }, + { + "label": "Cycle 43 PR #2249", + "head": "0c6ed7958433b2cbc0beaa331ff882aa08221a0d", + "url": "https://github.com/CrocSwap/integer-mult-bounds/pull/2249", + "usage": "prior exact high-arity connector floor; no code imported" + }, + { + "label": "PR #2171", + "head": "416aac58960660edced905dc7d914c126d357290", + "url": "https://github.com/CrocSwap/integer-mult-bounds/pull/2171", + "usage": "adjacent selected controlled stream work" + }, + { + "label": "PR #2207", + "head": "149d55d90d3c0877ed3d78e1deca8eb2f72e0d00", + "url": "https://github.com/CrocSwap/integer-mult-bounds/pull/2207", + "usage": "adjacent selected fallback stream work" + } + ] +} diff --git a/research/fanin4-aligned-source-stream-seed9983a614/TASK.json b/research/fanin4-aligned-source-stream-seed9983a614/TASK.json new file mode 100644 index 0000000000..799fba501e --- /dev/null +++ b/research/fanin4-aligned-source-stream-seed9983a614/TASK.json @@ -0,0 +1,89 @@ +{ + "schema_version": 1, + "task_id": "connector-alphabet-change", + "priority_revision": 4, + "base_commit": "0fdd7d82de03e43c9e5a1000dfbcd380d1b687d2", + "original_card": { + "id": "connector-alphabet-change", + "question": "Does one explicit extension of the connector alphabet offer a favorable completed budget?", + "starting_points": [ + "research/universal-connector-boundary/README.md", + "certificates/selected-result.json" + ], + "methods": [ + "Construct and charge a legal extra operation on a minimal instance.", + "Prove a target-specific bound after adding one specified operation family." + ], + "acceptance_gate": "State which fixed-alphabet premise changes and charge its implementation; do not declare it free.", + "first_step": { + "command": [ + "python3", + "-B", + "research/universal-connector-boundary/verify.py" + ], + "expected": "PASS_UNIVERSAL_CONNECTOR_BOUNDARY", + "inspect": "Reproduce the 945-pairing control, then read Exact interface to list the allowed calls.", + "scope": "Positive control / source-bound orientation only; this is not a new construction or full cold replay." + }, + "first_experiment": "Add exactly one legally priced operation and test a tiny dirty-input instance before enumerating larger cases.", + "bottleneck_link": "A new legal connector operation could change recursive interface cost; a larger census in the same alphabet is a separate side result.", + "allowed_variations": [ + "Choose one additional operation and a small source count for its first counterexample." + ], + "scope_change_triggers": [ + "Keeping the alphabet and only increasing the source count; record this as a changed assignment." + ], + "overlap_terms": [ + "universal connector", + "source-toggle", + "connector alphabet" + ] + }, + "scope_status": "changed", + "actual_scope": "One physical FANIN4 update in the aligned F2[C2^2] coefficient frame: four arbitrary read-only source vectors, one arbitrary dirty target, identity source maps, L=4 coefficients. Compare one synchronized in-place scan with four complete singleton scans, fully charging XORs, reads, writes, head travel, call records, source and target reference fields, scratch, and frame changes.", + "scope_change_reason": "The original connector-alphabet card was refined after Cycle 43 PR #2249 established the exact abstract query/addition floor for arity-4..6 source calls at full per-tap cost. This task changes the operation granularity to one explicit aligned physical stream macro and checks its complete lower-level traffic bill. It preserves the four source XORs per coefficient and makes no full-batch or exponent claim.", + "bottleneck_link": "Recursive source calls also require data movement and complete head restoration. A fused source update can reduce that implementation bill while keeping the abstract source-add count unchanged. The benefit reaches the selected construction only if emitted source roles are already aligned in a live supplier frame.", + "budget_minutes": 60, + "stop_rule": "Stop after one FANIN4 map in F2[C2^2], its exact general-L identity and cost formula, and exhaustive verification of all 2^20 dirty states at L=4. Do not expand to the 63-mask batch or claim supplier admission.", + "outcome": "completed", + "evidence": "The fused scan has reads=20, writes=4, XORs=16, head_moves=40, call_records=1, source_refs=4, target_refs=1 at L=4. Four singleton updates have reads=32, writes=16, XORs=16, head_moves=64, call_records=4, source_refs=4, target_refs=4. Both restore all heads and preserve all sources. At uniform per-record price d the weighted saving is (s-1)(L*r+L*w+2L*h+d+t); for variable record prices d1 and ds it is (s-1)L(r+w+2h)+s*d1-ds+(s-1)t, less any opcode-table or frame-conversion expense F. Equal unit prices on all listed resources give an exact net saving of 54 units. The verifier checks all 1,048,576 arbitrary dirty L=4 states and five failure controls. No supplier mapping or kappa change.", + "author": "Cinna Davis", + "ai_assistance": "Substantial OpenAI Codex assistance: scoped construction design, exact cost derivation, verifier implementation, package drafting, and replay. The proof is a direct F2 lane identity with complete finite-state replay; no independent human or proof-assistant verification is claimed.", + "open_questions": [ + { + "question": "Can a current multiplication supplier emit four source roles in this common coefficient order without a paid frame conversion?", + "why_it_matters": "The local stream saving changes recursive cost only if an actual supplier presents the sources and target in the aligned frame.", + "first_step": "Choose one source/target tuple in a pinned supplier and derive the full entry and return permutation and head bill for FANIN4.", + "blocked_by": "No selected or pending supplier mapping to this identity-map common-frame operation has been established." + } + ], + "no_open_questions_reason": null, + "selector": { + "seed": "9983a6144fbaa6291d717b9b2076e816", + "track": "recursive-interfaces", + "original_scope_key": "ff4f5113ed2256ab19ed", + "refined_scope_key": "6fc5220c69a447d604bb" + }, + "predecessors": [ + { + "pr": 2128, + "head": "697369a971eed02c0fbcd75ebefec9f59945f08e", + "relation": "two-source DUO abstract connector; no supplier-level physical realization claimed" + }, + { + "pr": 2249, + "head": "0c6ed7958433b2cbc0beaa331ff882aa08221a0d", + "relation": "Cycle 43 exact abstract high-arity query/addition floor; full per-tap charge retained" + }, + { + "pr": 2171, + "head": "416aac58960660edced905dc7d914c126d357290", + "relation": "adjacent selected single-source controlled stream execution" + }, + { + "pr": 2207, + "head": "149d55d90d3c0877ed3d78e1deca8eb2f72e0d00", + "relation": "adjacent mixed fallback single-source stream execution" + } + ] +} diff --git a/research/fanin4-aligned-source-stream-seed9983a614/VERIFY.json b/research/fanin4-aligned-source-stream-seed9983a614/VERIFY.json new file mode 100644 index 0000000000..1901749807 --- /dev/null +++ b/research/fanin4-aligned-source-stream-seed9983a614/VERIFY.json @@ -0,0 +1,18 @@ +{ + "schema_version": 1, + "status": "ready", + "python": "3.12", + "requirements": [], + "inputs": [], + "commands": [ + { + "argv": [ + "{python}", + "-B", + "{package}/verify.py" + ], + "timeout_seconds": 120, + "checks": "Compare exact four-source fused and singleton maps on all 2^20 dirty states at L=4; verify the forward/inverse identity, returned heads, read-only sources, complete resource vectors, unit-weighted saving 54, omitted-source controls and omitted-return control." + } + ] +} diff --git a/research/fanin4-aligned-source-stream-seed9983a614/verify.py b/research/fanin4-aligned-source-stream-seed9983a614/verify.py new file mode 100644 index 0000000000..41c7e3757c --- /dev/null +++ b/research/fanin4-aligned-source-stream-seed9983a614/verify.py @@ -0,0 +1,237 @@ +#!/usr/bin/env python3 +"""Exact dirty-state and resource replay for the aligned FANIN4 stream.""" + +from dataclasses import dataclass + + +def need(condition, message): + if not condition: + raise ValueError(message) + + +@dataclass +class Meter: + reads: int = 0 + writes: int = 0 + xors: int = 0 + forward_moves: int = 0 + return_moves: int = 0 + call_records: int = 0 + source_refs: int = 0 + target_refs: int = 0 + scratch_cells: int = 0 + frame_transitions: int = 0 + + @property + def head_moves(self): + return self.forward_moves + self.return_moves + + +def singleton_sequence(sources, target, width, return_heads=True): + """Four complete in-place source-to-target ADD passes.""" + need(len(sources) == 4, "four sources required") + need(width >= 1, "positive coefficient width") + limit = 1 << width + need(all(type(x) is int and 0 <= x < limit for x in sources), "source extent") + need(type(target) is int and 0 <= target < limit, "target extent") + + meter = Meter() + current = target + for source in sources: + out = 0 + for lane in range(width): + source_bit = (source >> lane) & 1 + target_bit = (current >> lane) & 1 + out |= (source_bit ^ target_bit) << lane + current = out + meter.reads += 2 * width + meter.writes += width + meter.xors += width + meter.forward_moves += 2 * width + meter.return_moves += 2 * width if return_heads else 0 + meter.call_records += 1 + meter.source_refs += 1 + meter.target_refs += 1 + return current, meter, (0, 0, 0, 0, 0) if return_heads else (width,) * 2 + + +def fanin4(sources, target, width, return_heads=True): + """One aligned pass; apply all four source XORs to each dirty target lane.""" + need(len(sources) == 4, "four sources required") + need(width >= 1, "positive coefficient width") + limit = 1 << width + need(all(type(x) is int and 0 <= x < limit for x in sources), "source extent") + need(type(target) is int and 0 <= target < limit, "target extent") + + meter = Meter() + out = 0 + for lane in range(width): + bit = (target >> lane) & 1 + for source in sources: + bit ^= (source >> lane) & 1 + meter.xors += 1 + out |= bit << lane + + meter.reads = 5 * width + meter.writes = width + meter.forward_moves = 5 * width + meter.return_moves = 5 * width if return_heads else 0 + meter.call_records = 1 + meter.source_refs = 4 + meter.target_refs = 1 + heads = (0,) * 5 if return_heads else (width,) * 5 + return out, meter, heads + + +def omitted_source(sources, target, omitted): + out = target + for j, source in enumerate(sources): + if j != omitted: + out ^= source + return out + + +def charge_vector(meter): + return { + "reads": meter.reads, + "writes": meter.writes, + "xors": meter.xors, + "head_moves": meter.head_moves, + "records": meter.call_records, + "source_refs": meter.source_refs, + "target_refs": meter.target_refs, + "scratch_cells": meter.scratch_cells, + "frame_transitions": meter.frame_transitions, + } + + +def weighted_saving(single, fused, prices): + """Exact singleton-minus-fused difference under a declared resource price.""" + return sum( + (single[name] - fused[name]) * price + for name, price in prices.items() + ) + + +def trace_head_returns(width): + """Replay the pointer schedule independently of the data values.""" + single_heads = [0] * 5 + single_forward = 0 + single_return = 0 + for source_index in range(4): + for lane in range(width): + need(single_heads[source_index] == lane and single_heads[4] == lane, + "singleton inputs are not aligned") + single_heads[source_index] += 1 + single_heads[4] += 1 + single_forward += 2 + need(single_heads[source_index] == width and single_heads[4] == width, + "singleton scan extent") + single_return += single_heads[source_index] + single_heads[4] + single_heads[source_index] = 0 + single_heads[4] = 0 + need(single_heads == [0] * 5, "singleton return scan") + + fused_heads = [0] * 5 + fused_forward = 0 + for lane in range(width): + need(fused_heads == [lane] * 5, "fused inputs are not aligned") + fused_heads = [position + 1 for position in fused_heads] + fused_forward += 5 + need(fused_heads == [width] * 5, "fused scan extent") + fused_return = sum(fused_heads) + fused_heads = [0] * 5 + need(fused_heads == [0] * 5, "fused return scan") + return { + "single_forward": single_forward, + "single_return": single_return, + "fused_forward": fused_forward, + "fused_return": fused_return, + } + + +def exhaustive_dirty_states(width): + """Enumerate every joint state of four sources and one target.""" + states = 1 << (5 * width) + mask = (1 << width) - 1 + for packed in range(states): + sources = tuple((packed >> (j * width)) & mask for j in range(4)) + target = (packed >> (4 * width)) & mask + fused, _, heads = fanin4(sources, target, width) + singles, _, single_heads = singleton_sequence(sources, target, width) + need(fused == singles, "fused and singleton target maps differ") + back, _, back_heads = fanin4(sources, fused, width) + need(back == target, "fused inverse failed") + need(heads == single_heads == back_heads == (0,) * 5, "head return failed") + need(sources == tuple((packed >> (j * width)) & mask for j in range(4)), + "read-only source changed") + return states + + +def main(): + width = 4 + sources = (0b1011, 0b0110, 0b1101, 0b1001) + target = 0b0101 + fused, fused_meter, fused_heads = fanin4(sources, target, width) + singles, single_meter, single_heads = singleton_sequence(sources, target, width) + need(fused == singles, "positive fixture") + need(fused_heads == single_heads == (0,) * 5, "returned heads") + need(fanin4(sources, fused, width)[0] == target, "explicit reverse") + + fused_cost = charge_vector(fused_meter) + single_cost = charge_vector(single_meter) + need(fused_cost == { + "reads": 20, "writes": 4, "xors": 16, "head_moves": 40, + "records": 1, "source_refs": 4, "target_refs": 1, + "scratch_cells": 0, "frame_transitions": 0, + }, "fused exact cost") + need(single_cost == { + "reads": 32, "writes": 16, "xors": 16, "head_moves": 64, + "records": 4, "source_refs": 4, "target_refs": 4, + "scratch_cells": 0, "frame_transitions": 0, + }, "singleton exact cost") + trace = trace_head_returns(width) + need((trace["single_forward"], trace["single_return"]) == (32, 32), + "singleton pointer trace") + need((trace["fused_forward"], trace["fused_return"]) == (20, 20), + "fused pointer trace") + need(trace["single_forward"] + trace["single_return"] == single_cost["head_moves"], + "singleton charged head trace") + need(trace["fused_forward"] + trace["fused_return"] == fused_cost["head_moves"], + "fused charged head trace") + unit_prices = { + "reads": 1, "writes": 1, "xors": 1, "head_moves": 1, + "records": 1, "source_refs": 1, "target_refs": 1, + "scratch_cells": 1, "frame_transitions": 1, + } + unit_saving = weighted_saving(single_cost, fused_cost, unit_prices) + need(unit_saving == 54, "uniform-price full bill") + + # Each omitted source is detected on a basis-state positive control. + for omitted in range(4): + basis_sources = tuple(1 if j == omitted else 0 for j in range(4)) + correct = fanin4(basis_sources, 0, width)[0] + bad = omitted_source(basis_sources, 0, omitted) + need(correct != bad, "omitted-source control failed") + + # A schedule that skips the return scan is rejected by the entry-head contract. + _, _, no_return_heads = fanin4(sources, target, width, return_heads=False) + need(no_return_heads == (width,) * 5, "no-return control fixture") + need(no_return_heads != (0,) * 5, "no-return control was accepted") + + states = exhaustive_dirty_states(width) + need(states == 1_048_576, "incomplete dirty-state domain") + print( + "PASS_FANIN4_STREAM " + f"states={states} L={width} " + f"xor={single_cost['xors']}/{fused_cost['xors']} " + f"reads={single_cost['reads']}/{fused_cost['reads']} " + f"writes={single_cost['writes']}/{fused_cost['writes']} " + f"head_moves={single_cost['head_moves']}/{fused_cost['head_moves']} " + f"records={single_cost['records']}/{fused_cost['records']} " + f"unit_saving={unit_saving} controls=6" + ) + + +if __name__ == "__main__": + main()