Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
116 commits
Select commit Hold shift + click to select a range
6094ec9
feat(exactTriangle): relate lopsided queries and matrix products
edemaine Oct 28, 2026
dec963a
feat(exactTriangle): prove modular counts and the selected-prime bound
edemaine Oct 28, 2026
0d8da3e
doc(threeSum): cite the lopsided triangle paper and clarify adaptatio…
edemaine Oct 28, 2026
8d80ecc
doc(bibliography): sort the new paper entry
edemaine Oct 28, 2026
fd69be7
fix(exactTriangle): repair the prime-window header and hashing docume…
edemaine Oct 28, 2026
ce646c7
feat(exactTriangle): split modular queries and recover witnesses
edemaine Oct 28, 2026
2cae26c
doc(threeSum): record finite splitting and recovery milestone
edemaine Oct 28, 2026
dc4206a
refactor(exactTriangle): reuse the natural residue bound and clarify …
edemaine Oct 28, 2026
2434f47
feat(matrix): prove the tensor recursion and pruned evaluation core
edemaine Oct 28, 2026
62dc601
doc(matrix): clarify the overlapping contributing leaf
edemaine Oct 28, 2026
4f3226c
doc(matrix): make tensor and pruning explanations standalone
edemaine Oct 28, 2026
19ee942
feat(wordram): count Chan–He collisions with native histograms
edemaine Oct 28, 2026
a712205
fix(wordram): preserve collision-counting parameters explicitly
edemaine Oct 28, 2026
844522a
feat(matrix): prove finite thin-product correctness and sparse bounds
edemaine Oct 28, 2026
5cbb93d
feat(wordram): compute Chan–He parameters from native input
edemaine Oct 28, 2026
b4936ce
doc(matrix): explain finite thin-product guarantees in the Lean API
edemaine Oct 28, 2026
0505439
fix(wordram): repair parameter proof hygiene and ceiling-log docs
edemaine Oct 28, 2026
ad3e097
feat(wordram): implement shared tensor encodings
edemaine Oct 28, 2026
3b9234d
test(wordram): connect encoder witnesses to charged runs
edemaine Oct 28, 2026
46e9e72
feat(wordram): checkpoint native pruned-query controller proofs
edemaine Oct 28, 2026
68bbe40
refactor(schonhage): reuse binomial bounds and sharpen signed tests
edemaine Oct 28, 2026
cb261f6
feat(WordRAM): prepare and encode every shared matrix band
edemaine Oct 28, 2026
87ff44c
feat(wordram): checkpoint native pruned accumulation and frame transi…
edemaine Oct 28, 2026
b3b8047
feat(wordram): checkpoint actual pruned-query root-frame stores
edemaine Oct 28, 2026
6603615
fix(nat): reuse existing tuple digit identity in band preparation
edemaine Oct 28, 2026
79c9407
feat(wordram): checkpoint complete native pruned-controller phases
edemaine Oct 28, 2026
99b8a1b
doc(matrix): replace copied source prose with API descriptions
edemaine Oct 28, 2026
12abeee
refactor(finset): use order-embedding names in OrderGlue
edemaine Oct 28, 2026
829613f
feat(WordRAM): prove all-depth pruned controller execution
edemaine Oct 28, 2026
a93c021
style(WordRAM): normalize native proof file modes
edemaine Oct 28, 2026
0c2e0eb
feat(wordram): complete initialized sparse pruned-query execution
edemaine Oct 28, 2026
60bec64
chore(wordram): combine band and query evaluator prerequisites
edemaine Oct 29, 2026
06b3563
feat(wordram): checkpoint computed wanted-query routing
edemaine Oct 29, 2026
7772a34
feat(wordram): checkpoint wanted count-pass proofs
edemaine Oct 29, 2026
702c14b
doc(finset): restore ordered-gluing source credit
SamuelSchlesinger Oct 29, 2026
ce77926
fix(wordram): address pruned-query review findings
edemaine Oct 29, 2026
48a0fa3
chore(exactTriangle): merge current main for sibling integration
codex Oct 29, 2026
3fb98bd
chore(exactTriangle): join reviewed query splitting and counting layers
codex Oct 29, 2026
0b5cfe4
chore(wordram): retain reviewed pruned-query parent repair
edemaine Oct 29, 2026
d1bc583
chore(exactTriangle): reconcile modular-hashing integration documenta…
SamuelSchlesinger Oct 29, 2026
c0b0511
chore(exactTriangle): carry hashing integration through query splitting
SamuelSchlesinger Oct 29, 2026
a76a7d3
chore(exactTriangle): carry documentation integration through native …
SamuelSchlesinger Oct 29, 2026
4f6de79
fix(imports): deduplicate aggregate after counting integration
SamuelSchlesinger Oct 29, 2026
02f1b49
fix(imports): deduplicate aggregate after query-splitting integration
SamuelSchlesinger Oct 29, 2026
60dfe9a
Merge commit '02f1b495322d8cfee81969186795670e3574defa' into coordina…
SamuelSchlesinger Oct 29, 2026
6f48d5c
Merge remote-tracking branch 'origin/main' into coordinator/integrate…
SamuelSchlesinger Oct 29, 2026
b17ece3
Merge commit '4f6de797203eb5dac02b2cadd9e8fa33e7427d57' into coordina…
SamuelSchlesinger Oct 29, 2026
e031b72
Merge remote-tracking branch 'origin/main' into coordinator/integrate…
SamuelSchlesinger Oct 29, 2026
b79ae8e
fix(imports): regenerate aggregate after Chan-He parameter integration
SamuelSchlesinger Oct 29, 2026
3187bca
chore(wordram): retain reviewed sibling source-credit fix
edemaine Oct 29, 2026
a6e4005
feat(wordram): route and group wanted thin-product queries
edemaine Oct 29, 2026
249f960
Merge commit '6f48d5c0efdfea1984c4accf368de9eaa9716b95' into coordina…
SamuelSchlesinger Oct 29, 2026
08a0415
Merge remote-tracking branch 'origin/main' into coordinator/integrate…
SamuelSchlesinger Oct 29, 2026
b0fa45b
Merge commit '08a0415413677534785113b7cf643b1ba9a6ee78' into coordina…
SamuelSchlesinger Oct 29, 2026
bd9bd2b
fix(imports): regenerate aggregate after shared-encoder integration
SamuelSchlesinger Oct 29, 2026
ab3e96c
chore(integration): reconcile splitting layer with landed hashing parent
SamuelSchlesinger Oct 29, 2026
9c8b9fc
perf(wordram): reduce native parameter proof heartbeat budget
SamuelSchlesinger Oct 29, 2026
0c9b346
Merge remote-tracking branch 'github/main' into coordinator-heartbeat…
SamuelSchlesinger Oct 29, 2026
da6a301
chore(integration): adopt splitting layer after its parent landed
SamuelSchlesinger Oct 29, 2026
452924e
chore(wordram): scope measured parameter proof budget to reduction task
SamuelSchlesinger Oct 29, 2026
6aaf569
Merge commit 'da6a301aca1e0c03e6953b3329474b2236f0fac8' into coordina…
SamuelSchlesinger Oct 29, 2026
5f6aad5
chore(integration): refresh tensor recursion supplier after hashing l…
SamuelSchlesinger Oct 29, 2026
d91296f
perf(schonhage): focus cell arithmetic and use the default heartbeat …
SamuelSchlesinger Oct 29, 2026
d8f4162
fix(doc): restore unrelated upstream coordination records
SamuelSchlesinger Oct 29, 2026
a63864f
chore(three-sum): merge corrected supplier preserving scope and sourc…
SamuelSchlesinger Oct 29, 2026
781297c
chore(three-sum): merge refreshed thin-product supplier preserving en…
SamuelSchlesinger Oct 29, 2026
1f2cfc4
Merge commit '781297cef838ef99c33e4036195e58b06ca46779' into coordina…
SamuelSchlesinger Oct 29, 2026
b4aa7b5
Merge remote-tracking branch 'origin/main' into coordinator/integrate…
SamuelSchlesinger Oct 29, 2026
780531b
fix(imports): regenerate band-preparation aggregate from merged inven…
SamuelSchlesinger Oct 29, 2026
04349d4
Merge commit '780531b426051080bb274a0511f49142af03557c' into coordina…
SamuelSchlesinger Oct 29, 2026
5bf1f0e
doc(order-glue): preserve reviewed source credit through supplier merge
SamuelSchlesinger Oct 29, 2026
ec46a4f
chore(schonhage): integrate reviewed query and band suppliers
SamuelSchlesinger Oct 29, 2026
a9d3fb9
chore(schonhage): integrate wanted routing with refreshed query supplier
SamuelSchlesinger Oct 29, 2026
91f485e
chore(schonhage): adopt actual reviewed naming-refactor supplier
SamuelSchlesinger Oct 29, 2026
a677276
Merge remote-tracking branch 'origin/main' into coordinator/refresh-1…
SamuelSchlesinger Oct 29, 2026
8a81617
Merge remote-tracking branch 'github/main' into coordinator-refresh-1…
SamuelSchlesinger Oct 29, 2026
9e9cb10
Merge commit 'a67727699b41ff9fa7d7227964d67c26699ab7f6' into coordina…
SamuelSchlesinger Oct 29, 2026
c3e1bcd
doc(order-glue): restore paper citation and finite scope
SamuelSchlesinger Oct 29, 2026
606fd61
Merge commit 'c3e1bcd3c8d9bb1dc4819d1b0a994bf52119731b' into coordina…
SamuelSchlesinger Oct 29, 2026
59c6f80
feat(wordram): checkpoint native queried-product construction (#1581)
edemaine Oct 29, 2026
9d33c95
Merge commit '606fd614454016f738d69d2cd768cb4d9a9ad795' into coordina…
SamuelSchlesinger Oct 29, 2026
936457e
Merge commit '9d33c950c695a353b20fe43fd7524f25663a4bce' into coordina…
SamuelSchlesinger Oct 29, 2026
4b2d75c
Merge remote-tracking branch 'github/main' into coordinator-queue-146…
SamuelSchlesinger Oct 29, 2026
ea38fc5
Merge remote-tracking branch 'origin/main' into coordinator/refresh-1…
SamuelSchlesinger Oct 29, 2026
6a86e25
fix(imports): regenerate thin-product aggregate after main integration
SamuelSchlesinger Oct 29, 2026
6dbd0cc
Merge commit '6a86e258db83b7724f5bd93c43c0cbc4221a992f' into coordina…
SamuelSchlesinger Oct 29, 2026
c94a52f
Merge commit '4b2d75ce50dbca1101868ed3726170770d0a3e77' into coordina…
SamuelSchlesinger Oct 29, 2026
d072322
Merge remote-tracking branch 'origin/main' into coordinator/repair-15…
SamuelSchlesinger Oct 29, 2026
61e7fe7
doc(wordram): separate shared-encoder integration records
SamuelSchlesinger Oct 29, 2026
e21cbd0
chore(integration): preserve local band-refresh preparation
SamuelSchlesinger Oct 29, 2026
d2e8386
Merge commit '38f32f97e76ad36666b99d1f0d39a64fc0ef3306' into coordina…
SamuelSchlesinger Oct 29, 2026
f55b418
Merge commit 'd2e8386e3352241f97fc1c3faa3da53c1fbb1f68' into coordina…
SamuelSchlesinger Oct 29, 2026
8f25b31
Merge commit 'f04ad02616791b3fa37ff236ac27b0a8690ca985' into coordina…
SamuelSchlesinger Oct 29, 2026
debd649
doc(matrix): keep progress links out of shared reference append block
SamuelSchlesinger Oct 29, 2026
af20863
feat(wordram): checkpoint complete queried-product source before supp…
edemaine Oct 29, 2026
cf25502
chore(wordram): merge actual reviewed queried-product supplier
edemaine Oct 29, 2026
ff31104
feat(wordram): execute queried thin-matrix products (#1581)
edemaine Oct 29, 2026
9a57e13
style(Schonhage): remove trailing space in queried products
edemaine Oct 29, 2026
37306ef
doc(Schonhage): retain band catalogue encoding contract
edemaine Oct 29, 2026
b71ae0e
Merge commit 'cf63e382d02ba396b4f8d460958d70b15735f23e' into coordina…
SamuelSchlesinger Oct 29, 2026
8527c1f
Merge commit 'cf63e382d02ba396b4f8d460958d70b15735f23e' into coordina…
SamuelSchlesinger Oct 29, 2026
82f86fe
Merge commit '8527c1f4d25246fee901303e7c0f1310d30306c0' into coordina…
SamuelSchlesinger Oct 29, 2026
d429833
Merge commit '82f86fec9b93d546f96542987255aa9f16377b3b' into coordina…
SamuelSchlesinger Oct 29, 2026
f43c26e
Merge commit '82f86fec9b93d546f96542987255aa9f16377b3b' into coordina…
SamuelSchlesinger Oct 29, 2026
168a8be
Merge commit 'f43c26edef5bc586602e0678be9f8a59759bd5f2' into coordina…
SamuelSchlesinger Oct 29, 2026
7d82da6
Merge commit 'd429833391f88fe96223bef8124be7e6b21a117f' into coordina…
SamuelSchlesinger Oct 29, 2026
70898da
fix(imports): regenerate wanted-routing aggregate from actual inventory
SamuelSchlesinger Oct 29, 2026
a8cb7c9
chore(schonhage): refresh queried products onto actual wanted parent
SamuelSchlesinger Oct 29, 2026
7cb7ade
wip(wordram): checkpoint callable thin-product source
edemaine Oct 29, 2026
904c620
chore(wordram): merge reviewed queried-product parent into callable c…
edemaine Oct 29, 2026
f5e6949
feat(WordRAM): run queried thin products in caller-owned memory
edemaine Oct 29, 2026
fd1f2fc
chore(WordRAM): merge pinned landed main into callable product child
edemaine Oct 29, 2026
ce10624
refactor(WordRAM): align ArrayNormalize module name and admission prose
edemaine Oct 29, 2026
8f46341
feat(ExactTriangle): checkpoint actual record product composition
edemaine Oct 29, 2026
8612aff
chore(ExactTriangle): merge pinned main into record product child
edemaine Oct 29, 2026
96842bb
feat(ExactTriangle): validate initialized record product adapter
edemaine Oct 29, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Complexity.lean
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,23 @@ public import Complexity.Algorithms.WordRAM.ExactTriangle.PreparationInput
public import Complexity.Algorithms.WordRAM.ExactTriangle.PreparationSpec
public import Complexity.Algorithms.WordRAM.ExactTriangle.QueryScan
public import Complexity.Algorithms.WordRAM.ExactTriangle.QuerySpec
public import Complexity.Algorithms.WordRAM.ExactTriangle.RecordProduct
public import Complexity.Algorithms.WordRAM.ExactTriangle.RecordProduct.Bounds
public import Complexity.Algorithms.WordRAM.ExactTriangle.RecordProduct.Copy
public import Complexity.Algorithms.WordRAM.ExactTriangle.RecordProduct.Decode
public import Complexity.Algorithms.WordRAM.ExactTriangle.RecordProduct.Entry
public import Complexity.Algorithms.WordRAM.ExactTriangle.RecordProduct.Exec
public import Complexity.Algorithms.WordRAM.ExactTriangle.RecordProduct.First
public import Complexity.Algorithms.WordRAM.ExactTriangle.RecordProduct.Flat
public import Complexity.Algorithms.WordRAM.ExactTriangle.RecordProduct.Header
public import Complexity.Algorithms.WordRAM.ExactTriangle.RecordProduct.InputSpec
public import Complexity.Algorithms.WordRAM.ExactTriangle.RecordProduct.Kernel
public import Complexity.Algorithms.WordRAM.ExactTriangle.RecordProduct.Prepare
public import Complexity.Algorithms.WordRAM.ExactTriangle.RecordProduct.Producer
public import Complexity.Algorithms.WordRAM.ExactTriangle.RecordProduct.ProducerQueries
public import Complexity.Algorithms.WordRAM.ExactTriangle.RecordProduct.Program
public import Complexity.Algorithms.WordRAM.ExactTriangle.RecordProduct.RawInput
public import Complexity.Algorithms.WordRAM.ExactTriangle.RecordProduct.SourceBounds
public import Complexity.Algorithms.WordRAM.ExactTriangle.ResidueClasses
public import Complexity.Algorithms.WordRAM.ExactTriangle.WriteMatrices
public import Complexity.Algorithms.WordRAM.FusionTree.Binary
Expand Down
32 changes: 25 additions & 7 deletions Complexity/Algorithms/WordRAM/ExactTriangle/PreparationInput.lean
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pre-halt state and the final halt charged.

## Main results

* `PreparationInput.exec_input`: structured execution for composition, without the final halt.
* `PreparationInput.runs_input`: initialized execution, exact readback, fits and charged halt.
* `PreparationInput.bounded_reachable`: input-derived word and address bounds at every prefix.
* `PreparationInput.width_le_log`, `PreparationInput.wordBound_le_pow`: logarithmic physical
Expand Down Expand Up @@ -213,18 +214,17 @@ private theorem input_fits {n D g p A : ℕ} (values : List ℤ)
have hs := hmag.of_natAbs_le ((hv z hz).trans (by omega : A ≤ A + p + 1))
exact (Signed.encode_fits hs).mono (by dsimp only [width]; omega)

/-- Complete initialized execution at every admitted physical width. The prefix witness and
the output predicate use the same actual pre-halt memory, and the bound includes the halt. -/
theorem runs_input {n D g p A W : ℕ} (values : List ℤ)
/-- Structured initialized execution for real native caller composition. The budget excludes
only the final halt; all existing original-input, width and exact readback hypotheses remain.
This does not accept an execution, layout or intermediate-fit certificate. -/
theorem exec_input {n D g p A W : ℕ} (values : List ℤ)
(hlen : values.length = 3 * (n * n))
(hD : 1 ≤ D) (hDn : D ≤ n) (hg : 1 ≤ g) (hp : 0 < p)
(hv : ∀ z ∈ values, z.natAbs ≤ A) (hw : width n D g p A ≤ W) :
let C := fun rho => Nat.count (fun q => (values.getD q 0 % (p : ℤ)).toNat = rho) (n * n)
let K := ChunkCatalogue.before C (queryCap n D) p
∃ y t, t ≤ Instances.timeBound n D p K (numPieces n D g) ∧
Runs W flatProgram (State.init (input n D g p values)) ⟨Instances.program.size, y⟩
(t - 1) ∧
Executes W flatProgram (State.init (input n D g p values)) (y.regs Instances.output) t ∧
∃ y, ExecWithin W Instances.program (.init (input n D g p values)) y
(Instances.timeBound n D p K (numPieces n D g) - 1) ∧
Instances.Output n D g p (fun q => values.getD q 0)
(.init (input n D g p values)) y ∧
InputFits W (input n D g p values) ∧ y.Bounded W := by
Expand All @@ -244,6 +244,24 @@ theorem runs_input {n D g p A W : ℕ} (values : List ℤ)
have hew := ExecWithin.mono_width Instances.program_fixed.2 hy hw
have hi := (input_fits values hlen hv).mono hw
have hybounded := hew.bounded (Data.Bounded.init hi)
exact ⟨y, hew, hout, hi, hybounded⟩

/-- Complete initialized execution at every admitted physical width. The prefix witness and
the output predicate use the same actual pre-halt memory, and the bound includes the halt. -/
theorem runs_input {n D g p A W : ℕ} (values : List ℤ)
(hlen : values.length = 3 * (n * n))
(hD : 1 ≤ D) (hDn : D ≤ n) (hg : 1 ≤ g) (hp : 0 < p)
(hv : ∀ z ∈ values, z.natAbs ≤ A) (hw : width n D g p A ≤ W) :
let C := fun rho => Nat.count (fun q => (values.getD q 0 % (p : ℤ)).toNat = rho) (n * n)
let K := ChunkCatalogue.before C (queryCap n D) p
∃ y t, t ≤ Instances.timeBound n D p K (numPieces n D g) ∧
Runs W flatProgram (State.init (input n D g p values)) ⟨Instances.program.size, y⟩
(t - 1) ∧
Executes W flatProgram (State.init (input n D g p values)) (y.regs Instances.output) t ∧
Instances.Output n D g p (fun q => values.getD q 0)
(.init (input n D g p values)) y ∧
InputFits W (input n D g p values) ∧ y.Bounded W := by
obtain ⟨y, hew, hout, hi, hybounded⟩ := exec_input values hlen hD hDn hg hp hv hw
obtain ⟨t, ht, he⟩ := hew
have hr := he.runs (pc := 0) (p := flatProgram) (Program.Contains.self _).append_left
refine ⟨y, t + 1, ?_, ?_, ?_, hout, hi, hybounded⟩
Expand Down
65 changes: 65 additions & 0 deletions Complexity/Algorithms/WordRAM/ExactTriangle/RecordProduct.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/-
Copyright (c) 2026 Erik Demaine. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Erik Demaine
-/

module
-- upstream: candidate

public import Complexity.Algorithms.WordRAM.ExactTriangle.RecordProduct.SourceBounds

/-!
# Native ExactTriangle record-to-product adapter

## Main definitions

* `RecordProduct.program`: fixed 320-register consumer of any occupied producer record ordinal.
* `RecordProduct.entryProgram`: actual original-input producer followed by ordinal-zero selection
and that consumer; original geometry proves the first record exists.
* `RecordProduct.leftMatrix`, `RecordProduct.rightMatrix`: the unchanged native writer's actual
raw zero/one matrices, read in their respective row-major orders.
* `RecordProduct.admissionWidth`, `RecordProduct.wordBound`: original-header/weight bounds for
every sufficiently wide physical execution and its complete prefix words/address support.

## Main results

* `RecordProduct.exec_of_output`: actual producer readback derives all six record words, every
matrix/query pointer and separation, the coordinate occurrences and their sparsity; the actual
callee returns all requested product values and preserves the producer catalogue and registers.
* `RecordProduct.runs_input`: complete native `State.init`, same-data prefix `Runs`, returned
pointer, charged halt, exact product words, caller frame and occurrence sparsity. This fixed
initialized client selects the first record, rather than solving all prepared instances.
* `RecordProduct.timeBound_le`: actual callee work plus `O(nD+Q)` descriptor/log/header/copy/
invocation overhead. There is no whole-window/catalogue initialization or snapshot.
* `RecordProduct.thin_timeBound_sparse`: under `D=4^m`, `L=19m`, `D^18≤n` and `Q·2^m≤n²`, the
per-record bound including halt is `34603008·(19m+1)·n²/D^(1/18)`. The actual chunk derives
its occurrence bound; a mere distinct-set bound instead retains the additive occurrence term.
* `RecordProduct.admissionWidth_le_log`: original polynomial header/weight envelopes give the
positive logarithmic threshold `(257κ+5906)·clog₂(n+2)` as a sufficient upper envelope.
* `RecordProduct.bounded_reachable_original`, `RecordProduct.wordBound_le_pow`: all wider native
prefixes have polynomial words/support independent of the chosen physical width.

The native code computes the record stride/address, reads every descriptor word, recovers `m`
with the existing binary-logarithm program and division by two, writes original local headers,
and copies raw X/Y and all interleaved coordinate occurrences above the actual producer heap.
The entire 163-register callable bank is renamed together. Memory rebasing is performed by its
actual compiler; register renaming alone is not a memory relocation. Raw matrix entries remain
zero/one during adapter copying: the actual callee already charges their canonical-signed doubling.

`PreparationInput.exec_input` exposes the existing structured initialized producer execution,
and `PreparationInput.runs_input` reuses it without changing its statement or cost. Stage lemmas
in the submodules are internal implementation interfaces. No intermediate fits, layouts, routing
tables, run/time/solver or sparse-bound certificate is an original input hypothesis.

This is per-record composition, not an aggregate subcubic ExactTriangle solver. Global parameter
and prime selection, the outer instance loop, backend/witness reconstruction, the full ordinary
distinct-position 3SUM composition and the improved 1.9992 endpoint remain open under #1394.

## References

The existing matrix writer and queried-product mathematics retain their pinned
[AlmanWilliams2026] / Anthropic source mapping. This adapter, register framing, initialized
composition and charged native proofs are original work; they do not transfer source Light
semantics or instruction constants. The value-only matrix wrappers reuse `WriteMatrices.entry`.
-/
154 changes: 154 additions & 0 deletions Complexity/Algorithms/WordRAM/ExactTriangle/RecordProduct/Bounds.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/-
Copyright (c) 2026 Erik Demaine. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Erik Demaine
-/

module
-- upstream: candidate

public import Complexity.Algorithms.WordRAM.ExactTriangle.RecordProduct.Producer
public import Complexity.Algorithms.WordRAM.ExactTriangle.PreparationInput

/-!
# Input-derived admission and charged adapter overhead

The actual producer supplies a polynomial bound for every pre-adapter word and address.
Its existing `wordBound` determines a sufficient positive logarithmic physical width for the
selected product's computed window. Adaptation is linear in `nD+Q`; this per-record statement
does not sum an ExactTriangle solver or establish its aggregate subcubic bound.
-/

public section

namespace Cslib.Algorithms.WordRAM.ExactTriangle.RecordProduct

open Cslib.WordRAM Cslib.Algorithms.ExactTriangle
open Cslib.Algorithms.MatrixMultiplication.Schonhage
open Cslib.Algorithms.WordRAM.MatrixMultiplication.Schonhage

/-- Original-input physical width covering the actual producer, every dirty producer word
before normalization, and all relocated product addresses. -/
@[expose] def admissionWidth (n D g p A : ℕ) : ℕ :=
PreparationInput.width n D g p A +
256 * Nat.clog 2 (2 * PreparationInput.wordBound n D g p A + n + n ^ 2 + 5)

/-- Physical admission is positive, including parameter boundary cases. -/
theorem admissionWidth_pos (n D g p A : ℕ) : 0 < admissionWidth n D g p A := by
unfold admissionWidth PreparationInput.width
omega

/-- Actual producer word/address bounds and the computed chunk size discharge the physical
fit numerically; neither a window nor an intermediate arithmetic certificate is an input. -/
theorem physical_fits {W n D g p A b Q : ℕ}
(hb : b ≤ PreparationInput.wordBound n D g p A) (hQ : Q ≤ n ^ 2)
(hW : admissionWidth n D g p A ≤ W) :
Fits W (2 * b + 4 * CallableProduct.windowSize n Q + 1024) := by
have hw : 256 * Nat.clog 2 (2 * PreparationInput.wordBound n D g p A + n + n ^ 2 + 5) ≤ W :=
by unfold admissionWidth at hW; omega
have hfit := CallableProduct.physical_fits hw
have hM : CallableProduct.windowSize n Q ≤ CallableProduct.windowSize n (n ^ 2) := by
apply Nat.pow_le_pow_right (by decide)
exact Nat.mul_le_mul_left 128 (Nat.clog_mono_right 2 (by omega))
exact hfit.of_le (by omega)

/-- The final threshold is logarithmic in the original outer dimension under the producer's
standard polynomial original-header/weight hypotheses. The constant is positive for every κ. -/
theorem admissionWidth_le_log {n D g p A κ : ℕ} (hn : 1 ≤ n)
(hD : D ≤ n) (hg : g ≤ n) (hp : p ≤ n) (hA : A ≤ (n + 2) ^ κ) :
admissionWidth n D g p A ≤ (257 * κ + 5906) * Nat.clog 2 (n + 2) := by
let b := n + 2
have hb : 2 ≤ b := by dsimp only [b]; omega
have hP := PreparationInput.wordBound_le_pow hn hD hg hp hA
change PreparationInput.wordBound n D g p A ≤ b ^ (κ + 19) at hP
have hunit : 1 ≤ b ^ (κ + 19) := Nat.one_le_pow _ _ (by omega)
have hnpow : n ≤ b ^ (κ + 19) := (show n ≤ b by dsimp only [b]; omega).trans
(Nat.le_self_pow (by omega) b)
have hn2 : n ^ 2 ≤ b ^ (κ + 19) :=
(Nat.pow_le_pow_left (by dsimp only [b]; omega : n ≤ b) 2).trans
(Nat.pow_le_pow_right (by omega : 1 ≤ b) (by omega : 2 ≤ κ + 19))
have harg : 2 * PreparationInput.wordBound n D g p A + n + n ^ 2 + 5 ≤ b ^ (κ + 23) := by
have hsum : 2 * PreparationInput.wordBound n D g p A + n + n ^ 2 + 5 ≤
9 * b ^ (κ + 19) := by omega
have hc : 9 ≤ b ^ 4 := (show 9 ≤ 2 ^ 4 by decide).trans (Nat.pow_le_pow_left hb 4)
calc
_ ≤ 9 * b ^ (κ + 19) := hsum
_ ≤ b ^ 4 * b ^ (κ + 19) := Nat.mul_le_mul_right _ hc
_ = b ^ (κ + 23) := by rw [← Nat.pow_add]; congr 1; omega
have hbase := Nat.le_pow_clog (by decide : 1 < (2 : ℕ)) b
have hlogBound := harg.trans (Nat.pow_le_pow_left hbase (κ + 23))
rw [← Nat.pow_mul] at hlogBound
have hlog := Nat.clog_le_of_le_pow hlogBound
have hprep := PreparationInput.width_le_log hn hD hg hp hA
change Nat.clog 2 (2 * PreparationInput.wordBound n D g p A + n + n ^ 2 + 5) ≤
Nat.clog 2 b * (κ + 23) at hlog
unfold admissionWidth
dsimp only [b] at hlog
nlinarith only [hlog, hprep]

/-- All real descriptor/log/header/copy/invocation work is linear in original matrices and
occurrences. This includes standalone halt, but not another execution of the producer. -/
theorem timeBound_le {m n : ℕ} (qs : List (Fin n × Fin n)) (hN : D m ^ 18 ≤ n) :
timeBound (m := m) qs + 1 ≤
3 * (QueriedProduct.timeBound (show m ≤ 19 * m by omega) qs + 1) +
4096 * (n * D m + qs.length + 1) := by
have hn : 1 ≤ n := Nat.pow_pos (Nat.pow_pos (by decide : 0 < (4 : ℕ))) |>.trans_le hN
have hm : m ≤ D m := by
have hlog := Nat.log_le_self 2 (4 ^ m)
rw [log_two_four_pow] at hlog
change m ≤ 4 ^ m
omega
have hND : D m ≤ n * D m := Nat.le_mul_of_pos_left _ hn
have ht := CallableProduct.timeBound_le (m := m) qs
unfold timeBound
omega

/-- The actual callee envelope already dominates a second copy of this linear adaptation.
This comparison reuses its established sparse bounds without a separate polylog oracle. -/
theorem timeBound_le_twice_callable {m n : ℕ} (qs : List (Fin n × Fin n))
(hN : D m ^ 18 ≤ n) :
timeBound (m := m) qs + 1 ≤ 2 * (CallableProduct.timeBound (m := m) qs + 3) := by
have hn : 1 ≤ n := Nat.pow_pos (Nat.pow_pos (by decide : 0 < (4 : ℕ))) |>.trans_le hN
have hm : m ≤ n * D m := by
have hlog := Nat.log_le_self 2 (4 ^ m)
rw [log_two_four_pow] at hlog
have hmul : D m ≤ n * D m := Nat.le_mul_of_pos_left _ hn
change 4 ^ m ≤ n * D m at hmul
omega
unfold timeBound CallableProduct.timeBound CallableProduct.coreTime QueriedProduct.timeBound
QueriedProduct.inputEnd
omega

/-- Distinct-set sparsity retains the actual list-occurrence charge. -/
theorem thin_timeBound {m n : ℕ} (qs : List (Fin n × Fin n)) (hN : D m ^ 18 ≤ n)
(hQ : qs.toFinset.card * 2 ^ m ≤ n ^ 2) :
((timeBound (m := m) qs + 1 : ℕ) : ℝ) ≤
524288 * ((19 * m + 1 : ℕ) : ℝ) *
(65 * ((D m : ℝ) ^ (-(1 / 18 : ℝ)) * (n : ℝ) ^ 2) + qs.length) := by
have ht : ((timeBound (m := m) qs + 1 : ℕ) : ℝ) ≤
2 * ((CallableProduct.timeBound (m := m) qs + 3 : ℕ) : ℝ) := by
exact_mod_cast timeBound_le_twice_callable qs hN
have h := CallableProduct.thin_timeBound qs hN hQ
calc
_ ≤ 2 * ((CallableProduct.timeBound (m := m) qs + 3 : ℕ) : ℝ) := ht
_ ≤ 2 * (262144 * ((19 * m + 1 : ℕ) : ℝ) *
(65 * ((D m : ℝ) ^ (-(1 / 18 : ℝ)) * (n : ℝ) ^ 2) + qs.length)) := by linarith
_ = _ := by ring

/-- Occurrence sparsity, which actual producer chunks supply, absorbs every occurrence charge.
The bound includes the adapter's standalone halt; producer execution is charged separately. -/
theorem thin_timeBound_sparse {m n : ℕ} (qs : List (Fin n × Fin n))
(hN : D m ^ 18 ≤ n) (hQ : qs.length * 2 ^ m ≤ n ^ 2) :
((timeBound (m := m) qs + 1 : ℕ) : ℝ) ≤
34603008 * ((19 * m + 1 : ℕ) : ℝ) * (n : ℝ) ^ 2 / (D m : ℝ) ^ (1 / 18 : ℝ) := by
have ht : ((timeBound (m := m) qs + 1 : ℕ) : ℝ) ≤
2 * ((CallableProduct.timeBound (m := m) qs + 3 : ℕ) : ℝ) := by
exact_mod_cast timeBound_le_twice_callable qs hN
have h := CallableProduct.thin_timeBound_sparse qs hN hQ
calc
_ ≤ 2 * ((CallableProduct.timeBound (m := m) qs + 3 : ℕ) : ℝ) := ht
_ ≤ 2 * (17301504 * ((19 * m + 1 : ℕ) : ℝ) * (n : ℝ) ^ 2 /
(D m : ℝ) ^ (1 / 18 : ℝ)) := by linarith
_ = _ := by ring

end Cslib.Algorithms.WordRAM.ExactTriangle.RecordProduct
Loading
Loading