# Fin 3 row-sum replay The Left to Solve statement binds a natural-valued grid `m : Fin 3 → Fin 3 → ℕ` and a common sum `t : ℕ`. The definition below preserves the entire conjunction after those binders. Its third conjunct gives the row sum for each `i`. The additive theorem generated from `Fin.prod_univ_three` rewrites that sum to three terms in index order. The result is conditional on the grid predicate and leaves the existence question open. [Displayed statement](https://lefttosolve.com/p/MagicSquares.exists_magic_square_squares) · [Pinned statement](https://raw.githubusercontent.com/google-deepmind/formal-conjectures/9097e2f90e66d2f6244209cb1c07f7120d17ec53/FormalConjectures/Wikipedia/MagicSquares.lean) · [Fin sums](https://raw.githubusercontent.com/leanprover-community/mathlib4/0df444a360eaa60ab8c11dca51a86af692955474/Mathlib/Algebra/BigOperators/Fin.lean) · [IsSquare](https://raw.githubusercontent.com/leanprover-community/mathlib4/0df444a360eaa60ab8c11dca51a86af692955474/Mathlib/Algebra/Group/Even.lean) Formal Conjectures commit `9097e2f90e66d2f6244209cb1c07f7120d17ec53` pins Lean **4.33.1** and Mathlib **`0df444a360eaa60ab8c11dca51a86af692955474`**. [Lean pin](https://raw.githubusercontent.com/google-deepmind/formal-conjectures/9097e2f90e66d2f6244209cb1c07f7120d17ec53/lean-toolchain) · [Mathlib pin](https://raw.githubusercontent.com/google-deepmind/formal-conjectures/9097e2f90e66d2f6244209cb1c07f7120d17ec53/lake-manifest.json) ## Lean source Save as `Replay.lean`. ```lean import Mathlib.Algebra.BigOperators.Fin import Mathlib.Algebra.Group.Even open scoped BigOperators def Grid (m : Fin 3 → Fin 3 → ℕ) (t : ℕ) : Prop := m.Injective2 ∧ (∀ i j, 0 < (m i j) ∧ IsSquare (m i j)) ∧ (∀ i, ∑ j, m i j = t) ∧ (∀ j, ∑ i, m i j = t) ∧ m 0 0 + m 1 1 + m 2 2 = t ∧ m 0 2 + m 1 1 + m 2 0 = t theorem grid_row_sum (m : Fin 3 → Fin 3 → ℕ) (t : ℕ) (h : Grid m t) (i : Fin 3) : m i 0 + m i 1 + m i 2 = t := (Fin.sum_univ_three (m i)).symm.trans (h.2.2.1 i) #print axioms grid_row_sum ``` ## Online preparation Use an empty directory on Linux x86-64 with Bash, curl, jq, Python 3, GNU tar with Zstandard support, and sha256sum. Run the following in that directory. The byte length and SHA-256 are from GitHub's release metadata and were checked against the downloaded binary archive. [Release metadata](https://api.github.com/repos/leanprover/lean4/releases/tags/v4.33.1) ```bash set -euo pipefail release=https://github.com/leanprover/lean4/releases/download/v4.33.1/lean-4.33.1-linux.tar.zst curl -fsSL --retry 2 "$release" -o lean.tar.zst test "$(wc -c < lean.tar.zst)" -eq 570405234 echo '890afd185370f85666025b883914ab4f4b339136f8c96167b69cfb62aecaf235 lean.tar.zst' | sha256sum -c - tar --zstd -xf lean.tar.zst m=https://raw.githubusercontent.com/leanprover-community/mathlib4/0df444a360eaa60ab8c11dca51a86af692955474 for p in lakefile.lean lean-toolchain lake-manifest.json; do curl -fsSL --retry 2 "$m/$p" -o "$p" done ``` Save this as `Select.lean`. It uses Lean's header parser to walk exactly the two imports and their transitive imports, skipping modules supplied by the Lean release. It fetches individual source files to compute the cache keys. The keys use the pinned Mathlib root inputs, generation 5, normalized source text, path components and import hashes in header order. [Cache hash algorithm](https://raw.githubusercontent.com/leanprover-community/mathlib4/0df444a360eaa60ab8c11dca51a86af692955474/Cache/Hashing.lean) · [Cache generation and build paths](https://raw.githubusercontent.com/leanprover-community/mathlib4/0df444a360eaa60ab8c11dca51a86af692955474/Cache/IO.lean) ```lean import Lean.Elab.ParseImportsFast open Lean System abbrev Hashes := Std.HashMap Name UInt64 partial def visit (bases : Std.HashMap Name String) (root : UInt64) (mod : Name) : StateT Hashes IO UInt64 := do if let some key := (← get)[mod]? then return key let some base := bases[mod.getRoot]? | throw (IO.userError s!"unknown package: {mod}") let path := (mkFilePath (mod.components.map toString)).withExtension "lean" unless ← path.pathExists do IO.FS.createDirAll (path.parent.getD ".") let url := s!"{base}/{path}" let result ← IO.Process.output { cmd := "curl", args := #["-fsSL", "--retry", "2", url, "-o", path.toString] } unless result.exitCode == 0 do throw (IO.userError s!"{url}: {result.stderr}") let content ← IO.FS.readFile path let imports ← Lean.parseImports' content path.toString let mut deps := [] for imp in imports.imports do unless [`Init, `Lean, `Std, `Lake].contains imp.module.getRoot do deps := (← visit bases root imp.module) :: deps let key := hash (root :: hash path.components :: hash content.crlfToLf :: deps.reverse) modify fun keys => keys.insert mod key return key def main (args : List String) : IO Unit := do let mut bases : Std.HashMap Name String := ∅ for arg in args do let [pkg, url] := arg.splitOn "=" | throw (IO.userError s!"bad mapping: {arg}") bases := bases.insert pkg.toName url let inputs ← ["lakefile.lean", "lean-toolchain", "lake-manifest.json"].mapM fun p => return hash (← IO.FS.readFile (FilePath.mk p)).crlfToLf let root := hash ((5 : UInt64) :: inputs) let (_, keys) ← (do discard <| visit bases root `Mathlib.Algebra.BigOperators.Fin discard <| visit bases root `Mathlib.Algebra.Group.Even : StateT Hashes IO Unit).run ∅ for (mod, key) in keys.toArray do IO.println s!"{mod} {key.toNat}" ``` Run the following in the same directory. The upstream package commits come from Mathlib's pinned manifest. [Mathlib dependency pins](https://raw.githubusercontent.com/leanprover-community/mathlib4/0df444a360eaa60ab8c11dca51a86af692955474/lake-manifest.json) ```bash set -euo pipefail m=https://raw.githubusercontent.com/leanprover-community/mathlib4/0df444a360eaa60ab8c11dca51a86af692955474 declare -A prefix=([plausible]=Plausible [LeanSearchClient]=LeanSearchClient [importGraph]=ImportGraph [proofwidgets]=ProofWidgets [aesop]=Aesop [Qq]=Qq [batteries]=Batteries [Cli]=Cli) bases=("Mathlib=$m") while IFS=$'\t' read -r name url rev; do bases+=("${prefix[$name]}=https://raw.githubusercontent.com/${url#https://github.com/}/$rev") done < <(jq -r '.packages[] | [.name,.url,.rev] | @tsv' lake-manifest.json) lean-4.33.1-linux/bin/lean --run Select.lean "${bases[@]}" > keys.txt ``` Save as `download.py`. The downloader resolves each key to an individual Mathlib CI archive and records the successful URL, byte length and SHA-256 for every module in `archives.tsv`. [Archive naming](https://raw.githubusercontent.com/leanprover-community/mathlib4/0df444a360eaa60ab8c11dca51a86af692955474/Cache/Lean.lean) · [Cache containers](https://raw.githubusercontent.com/leanprover-community/mathlib4/0df444a360eaa60ab8c11dca51a86af692955474/Cache/Infra.lean) · [URL construction](https://raw.githubusercontent.com/leanprover-community/mathlib4/0df444a360eaa60ab8c11dca51a86af692955474/Cache/Requests.lean) ```python from concurrent.futures import ThreadPoolExecutor from pathlib import Path import hashlib import subprocess cache = Path("ltar") cache.mkdir(exist_ok=True) base = "https://lakecache.blob.core.windows.net" def fetch(line): module, key = line.split() name = f"{int(key):016x}.ltar" target = cache / name for container in ("mathlib4-master", "mathlib4"): url = f"{base}/{container}/f/{name}" result = subprocess.run( ["curl", "-fsSL", "--retry", "2", url, "-o", str(target)], capture_output=True, text=True) if result.returncode == 0: data = target.read_bytes() return f"{module}\t{url}\t{len(data)}\t{hashlib.sha256(data).hexdigest()}" print(f"{module}: {url}: {result.stderr.strip()}", flush=True) raise RuntimeError(f"unavailable cache entry for {module}: {name}") with ThreadPoolExecutor(max_workers=12) as workers: rows = list(workers.map(fetch, sorted(Path("keys.txt").read_text().splitlines()))) Path("archives.tsv").write_text("\n".join(rows) + "\n") ``` Run the download and unpack step. ```bash python3 download.py set -euo pipefail mkdir -p compiled root="$PWD" (cd compiled && "$root/lean-4.33.1-linux/bin/leantar" -x - < <( awk -F '\t' -v dir="$root/ltar/" '{n=split($2,p,"/"); print dir p[n]}' "$root/archives.tsv" )) ``` ## Offline compilation and kernel replay Save as `offline.sh`. This uses the release binaries and the unpacked module artifacts. LeanChecker separately replays the declarations introduced by `Replay` over its imported environment. [LeanChecker implementation](https://raw.githubusercontent.com/leanprover/lean4/819816b2e0a3bf405af45ae5c7af2491d8f5bee6/src/LeanChecker.lean) ```bash set -euo pipefail export PATH="$PWD/lean-4.33.1-linux/bin:$PATH" mkdir -p replay-build export LEAN_PATH="$PWD/replay-build:$PWD/compiled/.lake/build/lib/lean" for lib in "$PWD"/compiled/.lake/packages/*/.lake/build/lib/lean; do if [ -d "$lib" ]; then LEAN_PATH="$LEAN_PATH:$lib"; fi done lean --version lean -DwarningAsError=true -o replay-build/Replay.olean Replay.lean leanchecker --verbose Replay ``` Run with networking isolated using Linux `unshare`. ```bash unshare -Urn bash offline.sh ``` ## Observed result The online preparation resolved 839 modules and downloaded all 839 archives from the Mathlib master cache, totaling 68,585,420 compressed bytes. The generated tab-separated receipt `archives.tsv` was 149,721 bytes with SHA-256 `0946208ddd39a20ecff44d551ef72bfff36bd5046e2386d6f21a3b93217b9cb5`. Because it records each URL and file digest, the receipt hash checks the complete set of archive bytes. To compare a new download against this run, use: ```bash echo '0946208ddd39a20ecff44d551ef72bfff36bd5046e2386d6f21a3b93217b9cb5 archives.tsv' | sha256sum -c - ``` The two direct import archives both report `git=mathlib4@0df444a360eaa60ab8c11dca51a86af692955474` via the release's `leantar -k`. | Module | Archive | Bytes | SHA-256 | | --- | --- | ---: | --- | | `Mathlib.Algebra.BigOperators.Fin` | [4b8300f42d59918f.ltar](https://lakecache.blob.core.windows.net/mathlib4-master/f/4b8300f42d59918f.ltar) | 127735 | `fb914f38246be1e6041e8ab9f84662c583f7f4069a0c5b0141657d63e531a3d2` | | `Mathlib.Algebra.Group.Even` | [6580d0fc4cc5f2e6.ltar](https://lakecache.blob.core.windows.net/mathlib4-master/f/6580d0fc4cc5f2e6.ltar) | 24878 | `dc25f312b2075e9f0745534e1da0c99b743dfda9b549a213de4967df7778c932` | Offline compilation and the separate `leanchecker --verbose Replay` kernel replay both exited successfully inside the isolated network namespace. The output was: ```text Lean (version 4.33.1, x86_64-unknown-linux-gnu, commit 819816b2e0a3bf405af45ae5c7af2491d8f5bee6, Release) 'grid_row_sum' depends on axioms: [propext, Classical.choice, Quot.sound] replaying Replay ``` The axiom list contains Lean's standard propositional extensionality, classical choice, and quotient soundness. The proof is a term built from the Mathlib sum equality, symmetry, transitivity, and the row hypothesis.