From 3a13a5ab0b5086c9c01e625c7616f4bea7482797 Mon Sep 17 00:00:00 2001 From: Vasily Ilin Date: Thu, 26 Nov 2026 11:07:59 +0000 Subject: [PATCH 1/7] Import the 28-state Brocard Turing machine --- LeanPool/CryptidsGoldbach.lean | 17 +- LeanPool/CryptidsGoldbach/Brocard28.lean | 1118 +++++++++++++++++ .../CryptidsGoldbach/BrocardSemantics.lean | 84 ++ LeanPool/CryptidsGoldbach/Imports.lean | 2 + LeanPool/projects/cryptids-goldbach.yaml | 26 +- 5 files changed, 1236 insertions(+), 11 deletions(-) create mode 100644 LeanPool/CryptidsGoldbach/Brocard28.lean create mode 100644 LeanPool/CryptidsGoldbach/BrocardSemantics.lean diff --git a/LeanPool/CryptidsGoldbach.lean b/LeanPool/CryptidsGoldbach.lean index 2df97c5623..d49c368907 100644 --- a/LeanPool/CryptidsGoldbach.lean +++ b/LeanPool/CryptidsGoldbach.lean @@ -6,22 +6,25 @@ Authors: Maxence Faldor, Moritz Firsching module public import LeanPool.CryptidsGoldbach.Semantics +public import LeanPool.CryptidsGoldbach.BrocardSemantics /-! -# A 21-state Turing machine for Goldbach's conjecture +# Finite-state Turing machines for Goldbach and Brocard Source: url:https://github.com/google-deepmind/cryptids/tree/f873434871b2301d5c8c91ba6fa224f92c93c1e1 -Authors: Maxence Faldor, Moritz Firsching +Authors: Maxence Faldor, Moritz Firsching, Daniel Veljjanoski Status: verified -Main declarations: `ConjectureMachines.Goldbach21.Semantics.halts_candidate_iff_not_goldbach` -Tags: computability, turing-machines, goldbach-conjecture, machine-simulation +Main declarations: `ConjectureMachines.Goldbach21.Semantics.halts_candidate_iff_not_goldbach`, + `ConjectureMachines.Brocard28.halts_brocard28_iff_exists_square` +Tags: computability, turing-machines, goldbach-conjecture, machine-simulation, brocard-problem MSC: 03D10, 11P32 -/ /-! -# A 21-state Goldbach Turing machine +# Goldbach and Brocard Turing machines Adapted from google-deepmind/cryptids at f873434871b2301d5c8c91ba6fa224f92c93c1e1. -The imported scope is the complete Goldbach proof and its finite-machine infrastructure. -The Riemann, ZF and Busy Beaver developments are outside this project. +The 21-state machine uses the Goldbach proof and its finite-machine infrastructure. +The 28-state Brocard machine and its halting equivalence come from the same upstream repository +at 481f4e3e39907304dbcad33e508bf21d80c6d5a6. -/ diff --git a/LeanPool/CryptidsGoldbach/Brocard28.lean b/LeanPool/CryptidsGoldbach/Brocard28.lean new file mode 100644 index 0000000000..fa1e70c51f --- /dev/null +++ b/LeanPool/CryptidsGoldbach/Brocard28.lean @@ -0,0 +1,1118 @@ +/- +Copyright (c) 2026 Google LLC. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Daniel Veljjanoski +-/ + +-- Modified for Lean Pool. + +module + +public import LeanPool.CryptidsGoldbach.Goldbach21 +public import Mathlib.Data.Nat.Factorial.Basic +import Mathlib.Tactic.Ring + +/-! +# Brocard28 + +The 28-state Turing machine `brocard_28` halts on the blank tape if and only if Brocard's +problem `n! + 1 = m ^ 2` has a solution with `n > 7`, i.e. beyond the three known solutions +`n = 4, 5, 7`. + +## Provenance and Attribution + +The machine and the proofs are by Daniel Veljjanoski, with Claude (Anthropic) as assistant +(November 2026). Earlier machines for the same problem are Charlie Reams's 61-state machine +with a Lean proof (`calr3/tinycryptids`, November 2026) and a 43-state table without a +machine-checked proof by Tandi, Gonzalez-Hendrix and Brown (); +Gasarch's SIGACT News Open Problems Column (September 2026, Open Problem 6.2) asks to improve +the 43-state count and reports Jonathan Brown's conjecture that about 30 states may suffice. + +## Machine Layout + +The tape holds, left to right, a block `N` of `n` ones, a separator, and a block `B` of ones, +all in unary. Between rounds `B` holds `n! / 8` ones (`3` at `n = 4`). A round multiplies `B` +by `n + 1` and grows `N` by one cell, then tests whether `(n + 1)! / 8` is a triangular number +`1 + 2 + ⋯ + k`; this holds if and only if `(n + 1)! + 1` is a perfect square (`m = 2k + 1`). + +* Multiply (states `2`–`11`): `LN0`/`LN1` walk left and write a `1` left of `N`; `EN` walks a + hole through `N`, and for every cell `R0`/`R1`/`L0`/`L1`/`EC` copy the template `B` once, + cell by cell, to the right end; `X` merges. `B` becomes `(n + 1) B + 1` ones. +* Test (states `1`, `12`–`19`): a hole walks right through `B` (`TLs`/`TLx` create it, `TE` + moves it); a block `S` right of `B` holds the current chunk size with a hole walking left + (`TR0`/`TR1`, `SB`); each wrap of `S` adds a cell to `S` for free (the blank beyond `S` + acts as the hole), and `SC` then checks whether `B` is exhausted: if so, success. +* Fail (states `20`–`22`): `ER`/`ER2`/`ER3` erase `S`, and `ER3` erases the last cell of `B` + on the way back, which cancels the extra cell left by the merge. +* Gate (states `23`–`27`): on success `M0`–`M3` walk left over `B` counting its ones modulo + `4`. At the separator, residue `0` halts (`M0` reading `0` is undefined); residues `2` and `3` + return via `Bs`, which fills the hole of `B`, and `ER` erases `S`. Since `32 ∣ n!` for + `n ≥ 8`, a success with `n ≥ 8` has residue `0`, while the known solutions have + `5! / 8 = 15 ≡ 3` and `7! / 8 = 630 ≡ 2 (mod 4)` and are suppressed. Residue `1` never + occurs at a success, so `M1` reading `0` is free for the start. +* Start: the machine starts in `GO` (state `0`) on the blank tape. `GO` and the two cells the + loop never reads (`TLx` and `M1` reading `0`) steer the loop's own states to write + `1111 0 111` in `46` steps, ending in `LN1` on the blank left of `N`: the configuration + every round passes through. +-/ + +@[expose] public section + +namespace ConjectureMachines.Brocard28 + +open Turing +open StateTransition +open Goldbach21.Tape (mkL_cons mk'_cons) +open Goldbach21 (nSteps nSteps_zero nSteps_succ nSteps_reaches nSteps_add nSteps_bind_eq + step_nSteps step_one reaches_imp_nSteps nSteps_eq_none_of_step_none) + +local notation "mkL" => Goldbach21.Tape.mkL + +/-- The blank half of the binary tape. -/ +def B0 : ListBlank (Fin 2) := ListBlank.mk [] + +theorem B0_cons_zero : B0.cons 0 = B0 := ListBlank.cons_head_tail B0 + +theorem mkRight_blank (L : ListBlank (Fin 2)) : Tape.mk' L B0 = ⟨0, L, B0⟩ := rfl + +/-- Prepend `k` ones to a half tape. -/ +def ones (k : ℕ) (L : ListBlank (Fin 2)) : ListBlank (Fin 2) := + ListBlank.append (List.replicate k 1) L + +theorem ones_succ' (k : ℕ) (L : ListBlank (Fin 2)) : ones (k + 1) L = ones k (L.cons 1) := by + rw [ones, List.replicate_succ', ListBlank.append_assoc] + rfl + +theorem ones_add (j k : ℕ) (L : ListBlank (Fin 2)) : ones (j + k) L = ones j (ones k L) := by + rw [ones, List.replicate_add, ListBlank.append_assoc] + rfl + +/-- The 28-state, two-symbol transition table from the upstream `brocard_28` artifact. -/ +def brocard28 : Machine 28 2 := Machine.ofRows [ + [some (1, .left, 10), some (1, .right, 3)], + [some (0, .left, 10), some (0, .right, 13)], + [some (0, .left, 3), some (1, .left, 2)], + [some (1, .right, 4), some (1, .left, 3)], + [some (0, .right, 11), some (0, .right, 5)], + [some (0, .right, 10), some (1, .right, 5)], + [some (0, .right, 7), some (1, .right, 6)], + [some (1, .left, 8), some (1, .right, 7)], + [some (0, .left, 9), some (1, .left, 8)], + [some (1, .right, 10), some (1, .left, 9)], + [some (0, .left, 2), some (0, .right, 6)], + [some (1, .right, 12), some (1, .right, 11)], + [some (0, .right, 1), some (1, .left, 12)], + [some (0, .right, 14), some (1, .right, 13)], + [some (1, .left, 15), some (1, .right, 14)], + [some (0, .left, 16), some (0, .left, 17)], + [some (0, .left, 23), some (1, .right, 13)], + [some (0, .left, 18), some (1, .left, 17)], + [some (1, .right, 19), some (1, .left, 18)], + [some (0, .right, 20), some (0, .right, 13)], + [some (0, .right, 21), some (0, .right, 20)], + [some (0, .left, 22), some (0, .right, 21)], + [some (0, .left, 22), some (0, .left, 2)], + [none, some (1, .left, 24)], + [some (1, .right, 0), some (1, .left, 25)], + [some (0, .right, 27), some (1, .left, 26)], + [some (0, .right, 27), some (1, .left, 23)], + [some (1, .right, 20), some (1, .right, 27)] +] + +theorem mkL_B0 (R : ListBlank (Fin 2)) : mkL B0 R = ⟨0, B0, R⟩ := rfl + +theorem stepR {q q' : Fin 28} {a b : Fin 2} + (h : brocard28.transition q a = some (b, .right, q')) + (L R : ListBlank (Fin 2)) : + step brocard28 ⟨q, ⟨a, L, R⟩⟩ = some ⟨q', Tape.mk' (L.cons b) R⟩ := by + simp only [step, h, Option.map_some]; rfl + +theorem stepL {q q' : Fin 28} {a b : Fin 2} + (h : brocard28.transition q a = some (b, .left, q')) + (L R : ListBlank (Fin 2)) : + step brocard28 ⟨q, ⟨a, L, R⟩⟩ = some ⟨q', mkL L (R.cons b)⟩ := by + simp only [step, h, Option.map_some]; rfl + +/-! ## Transition Table + +States in file order: +`0` = GO, `1` = TLx, `2` = LN0, `3` = LN1, `4` = EN, `5` = S1, `6` = R0, `7` = R1, `8` = L0, +`9` = L1, `10` = EC, `11` = X, `12` = TLs, `13` = TR0, `14` = TR1, `15` = SB, `16` = SC, +`17` = TL0, `18` = TL1, `19` = TE, `20` = ER, `21` = ER2, `22` = ER3, `23` = M0, `24` = M1, +`25` = M2, `26` = M3, `27` = Bs. +-/ + +theorem sGO_0 (L R) : step brocard28 ⟨0, ⟨0, L, R⟩⟩ = some ⟨10, mkL L (R.cons 1)⟩ := + stepL rfl L R +theorem sGO_1 (L R) : step brocard28 ⟨0, ⟨1, L, R⟩⟩ = some ⟨3, Tape.mk' (L.cons 1) R⟩ := + stepR rfl L R +theorem sTLx_0 (L R) : step brocard28 ⟨1, ⟨0, L, R⟩⟩ = some ⟨10, mkL L (R.cons 0)⟩ := + stepL rfl L R +theorem sTLx_1 (L R) : step brocard28 ⟨1, ⟨1, L, R⟩⟩ = some ⟨13, Tape.mk' (L.cons 0) R⟩ := + stepR rfl L R +theorem sLN0_0 (L R) : step brocard28 ⟨2, ⟨0, L, R⟩⟩ = some ⟨3, mkL L (R.cons 0)⟩ := + stepL rfl L R +theorem sLN0_1 (L R) : step brocard28 ⟨2, ⟨1, L, R⟩⟩ = some ⟨2, mkL L (R.cons 1)⟩ := + stepL rfl L R +theorem sLN1_0 (L R) : step brocard28 ⟨3, ⟨0, L, R⟩⟩ = some ⟨4, Tape.mk' (L.cons 1) R⟩ := + stepR rfl L R +theorem sLN1_1 (L R) : step brocard28 ⟨3, ⟨1, L, R⟩⟩ = some ⟨3, mkL L (R.cons 1)⟩ := + stepL rfl L R +theorem sEN_0 (L R) : step brocard28 ⟨4, ⟨0, L, R⟩⟩ = some ⟨11, Tape.mk' (L.cons 0) R⟩ := + stepR rfl L R +theorem sEN_1 (L R) : step brocard28 ⟨4, ⟨1, L, R⟩⟩ = some ⟨5, Tape.mk' (L.cons 0) R⟩ := + stepR rfl L R +theorem sS1_0 (L R) : step brocard28 ⟨5, ⟨0, L, R⟩⟩ = some ⟨10, Tape.mk' (L.cons 0) R⟩ := + stepR rfl L R +theorem sS1_1 (L R) : step brocard28 ⟨5, ⟨1, L, R⟩⟩ = some ⟨5, Tape.mk' (L.cons 1) R⟩ := + stepR rfl L R +theorem sR0_0 (L R) : step brocard28 ⟨6, ⟨0, L, R⟩⟩ = some ⟨7, Tape.mk' (L.cons 0) R⟩ := + stepR rfl L R +theorem sR0_1 (L R) : step brocard28 ⟨6, ⟨1, L, R⟩⟩ = some ⟨6, Tape.mk' (L.cons 1) R⟩ := + stepR rfl L R +theorem sR1_0 (L R) : step brocard28 ⟨7, ⟨0, L, R⟩⟩ = some ⟨8, mkL L (R.cons 1)⟩ := + stepL rfl L R +theorem sR1_1 (L R) : step brocard28 ⟨7, ⟨1, L, R⟩⟩ = some ⟨7, Tape.mk' (L.cons 1) R⟩ := + stepR rfl L R +theorem sL0_0 (L R) : step brocard28 ⟨8, ⟨0, L, R⟩⟩ = some ⟨9, mkL L (R.cons 0)⟩ := + stepL rfl L R +theorem sL0_1 (L R) : step brocard28 ⟨8, ⟨1, L, R⟩⟩ = some ⟨8, mkL L (R.cons 1)⟩ := + stepL rfl L R +theorem sL1_0 (L R) : step brocard28 ⟨9, ⟨0, L, R⟩⟩ = some ⟨10, Tape.mk' (L.cons 1) R⟩ := + stepR rfl L R +theorem sL1_1 (L R) : step brocard28 ⟨9, ⟨1, L, R⟩⟩ = some ⟨9, mkL L (R.cons 1)⟩ := + stepL rfl L R +theorem sEC_0 (L R) : step brocard28 ⟨10, ⟨0, L, R⟩⟩ = some ⟨2, mkL L (R.cons 0)⟩ := + stepL rfl L R +theorem sEC_1 (L R) : step brocard28 ⟨10, ⟨1, L, R⟩⟩ = some ⟨6, Tape.mk' (L.cons 0) R⟩ := + stepR rfl L R +theorem sX_0 (L R) : step brocard28 ⟨11, ⟨0, L, R⟩⟩ = some ⟨12, Tape.mk' (L.cons 1) R⟩ := + stepR rfl L R +theorem sX_1 (L R) : step brocard28 ⟨11, ⟨1, L, R⟩⟩ = some ⟨11, Tape.mk' (L.cons 1) R⟩ := + stepR rfl L R +theorem sTLs_0 (L R) : step brocard28 ⟨12, ⟨0, L, R⟩⟩ = some ⟨1, Tape.mk' (L.cons 0) R⟩ := + stepR rfl L R +theorem sTLs_1 (L R) : step brocard28 ⟨12, ⟨1, L, R⟩⟩ = some ⟨12, mkL L (R.cons 1)⟩ := + stepL rfl L R +theorem sTR0_0 (L R) : step brocard28 ⟨13, ⟨0, L, R⟩⟩ = some ⟨14, Tape.mk' (L.cons 0) R⟩ := + stepR rfl L R +theorem sTR0_1 (L R) : step brocard28 ⟨13, ⟨1, L, R⟩⟩ = some ⟨13, Tape.mk' (L.cons 1) R⟩ := + stepR rfl L R +theorem sTR1_0 (L R) : step brocard28 ⟨14, ⟨0, L, R⟩⟩ = some ⟨15, mkL L (R.cons 1)⟩ := + stepL rfl L R +theorem sTR1_1 (L R) : step brocard28 ⟨14, ⟨1, L, R⟩⟩ = some ⟨14, Tape.mk' (L.cons 1) R⟩ := + stepR rfl L R +theorem sSB_0 (L R) : step brocard28 ⟨15, ⟨0, L, R⟩⟩ = some ⟨16, mkL L (R.cons 0)⟩ := + stepL rfl L R +theorem sSB_1 (L R) : step brocard28 ⟨15, ⟨1, L, R⟩⟩ = some ⟨17, mkL L (R.cons 0)⟩ := + stepL rfl L R +theorem sSC_0 (L R) : step brocard28 ⟨16, ⟨0, L, R⟩⟩ = some ⟨23, mkL L (R.cons 0)⟩ := + stepL rfl L R +theorem sSC_1 (L R) : step brocard28 ⟨16, ⟨1, L, R⟩⟩ = some ⟨13, Tape.mk' (L.cons 1) R⟩ := + stepR rfl L R +theorem sTL0_0 (L R) : step brocard28 ⟨17, ⟨0, L, R⟩⟩ = some ⟨18, mkL L (R.cons 0)⟩ := + stepL rfl L R +theorem sTL0_1 (L R) : step brocard28 ⟨17, ⟨1, L, R⟩⟩ = some ⟨17, mkL L (R.cons 1)⟩ := + stepL rfl L R +theorem sTL1_0 (L R) : step brocard28 ⟨18, ⟨0, L, R⟩⟩ = some ⟨19, Tape.mk' (L.cons 1) R⟩ := + stepR rfl L R +theorem sTL1_1 (L R) : step brocard28 ⟨18, ⟨1, L, R⟩⟩ = some ⟨18, mkL L (R.cons 1)⟩ := + stepL rfl L R +theorem sTE_0 (L R) : step brocard28 ⟨19, ⟨0, L, R⟩⟩ = some ⟨20, Tape.mk' (L.cons 0) R⟩ := + stepR rfl L R +theorem sTE_1 (L R) : step brocard28 ⟨19, ⟨1, L, R⟩⟩ = some ⟨13, Tape.mk' (L.cons 0) R⟩ := + stepR rfl L R +theorem sER_0 (L R) : step brocard28 ⟨20, ⟨0, L, R⟩⟩ = some ⟨21, Tape.mk' (L.cons 0) R⟩ := + stepR rfl L R +theorem sER_1 (L R) : step brocard28 ⟨20, ⟨1, L, R⟩⟩ = some ⟨20, Tape.mk' (L.cons 0) R⟩ := + stepR rfl L R +theorem sER2_0 (L R) : step brocard28 ⟨21, ⟨0, L, R⟩⟩ = some ⟨22, mkL L (R.cons 0)⟩ := + stepL rfl L R +theorem sER2_1 (L R) : step brocard28 ⟨21, ⟨1, L, R⟩⟩ = some ⟨21, Tape.mk' (L.cons 0) R⟩ := + stepR rfl L R +theorem sER3_0 (L R) : step brocard28 ⟨22, ⟨0, L, R⟩⟩ = some ⟨22, mkL L (R.cons 0)⟩ := + stepL rfl L R +theorem sER3_1 (L R) : step brocard28 ⟨22, ⟨1, L, R⟩⟩ = some ⟨2, mkL L (R.cons 0)⟩ := + stepL rfl L R +theorem sM0_0 (L R : ListBlank (Fin 2)) : step brocard28 ⟨23, ⟨0, L, R⟩⟩ = none := rfl +theorem sM0_1 (L R) : step brocard28 ⟨23, ⟨1, L, R⟩⟩ = some ⟨24, mkL L (R.cons 1)⟩ := + stepL rfl L R +theorem sM1_0 (L R) : step brocard28 ⟨24, ⟨0, L, R⟩⟩ = some ⟨0, Tape.mk' (L.cons 1) R⟩ := + stepR rfl L R +theorem sM1_1 (L R) : step brocard28 ⟨24, ⟨1, L, R⟩⟩ = some ⟨25, mkL L (R.cons 1)⟩ := + stepL rfl L R +theorem sM2_0 (L R) : step brocard28 ⟨25, ⟨0, L, R⟩⟩ = some ⟨27, Tape.mk' (L.cons 0) R⟩ := + stepR rfl L R +theorem sM2_1 (L R) : step brocard28 ⟨25, ⟨1, L, R⟩⟩ = some ⟨26, mkL L (R.cons 1)⟩ := + stepL rfl L R +theorem sM3_0 (L R) : step brocard28 ⟨26, ⟨0, L, R⟩⟩ = some ⟨27, Tape.mk' (L.cons 0) R⟩ := + stepR rfl L R +theorem sM3_1 (L R) : step brocard28 ⟨26, ⟨1, L, R⟩⟩ = some ⟨23, mkL L (R.cons 1)⟩ := + stepL rfl L R +theorem sBs_0 (L R) : step brocard28 ⟨27, ⟨0, L, R⟩⟩ = some ⟨20, Tape.mk' (L.cons 1) R⟩ := + stepR rfl L R +theorem sBs_1 (L R) : step brocard28 ⟨27, ⟨1, L, R⟩⟩ = some ⟨27, Tape.mk' (L.cons 1) R⟩ := + stepR rfl L R + +/-! ## Sweeps over Ones -/ + +theorem sweepR {q : Fin 28} + (h : ∀ L R, step brocard28 ⟨q, ⟨1, L, R⟩⟩ = some ⟨q, Tape.mk' (L.cons 1) R⟩) + (k : ℕ) (L R : ListBlank (Fin 2)) : + nSteps brocard28 k ⟨q, Tape.mk' L (ones k R)⟩ = some ⟨q, Tape.mk' (ones k L) R⟩ := by + induction k generalizing L with + | zero => rfl + | succ k ih => + rw [nSteps_succ, show ones (k + 1) R = (ones k R).cons 1 from rfl, + mk'_cons, h, Option.bind_some, ih, ones_succ'] + +theorem sweepL {q : Fin 28} + (h : ∀ L R, step brocard28 ⟨q, ⟨1, L, R⟩⟩ = some ⟨q, mkL L (R.cons 1)⟩) + (k : ℕ) (L R : ListBlank (Fin 2)) : + nSteps brocard28 k ⟨q, mkL (ones k L) R⟩ = some ⟨q, mkL L (ones k R)⟩ := by + induction k generalizing R with + | zero => rfl + | succ k ih => + rw [nSteps_succ, show ones (k + 1) L = (ones k L).cons 1 from rfl, + mkL_cons, h, Option.bind_some, ih, ones_succ'] + +/-! ## Tape Encoding and Configurations -/ + +/-- The cells left of `B`: the separator, then the `n` cells of `N`. -/ +def NL (n : ℕ) : ListBlank (Fin 2) := (ones n B0).cons 0 + +/-- Start of a round: `LN0` on the last of the `v + 1` cells of `B`, `N` has `n` cells. -/ +def roundCfg (n v : ℕ) : Cfg 28 2 := ⟨2, Tape.mk' (ones v (NL n)) (B0.cons 1)⟩ + +/-- Start of a test: `TLx` on the first of the `M` cells of `B`. -/ +def testCfg (n M : ℕ) : Cfg 28 2 := ⟨1, Tape.mk' (NL n) (ones M B0)⟩ + +/-- Inside the copy loop: `R0` right of the cursor hole; `u` template cells are done and `v` +remain, `M` has `m` cells, and `N`'s hole has `a` cells to its left and `b` to its right. -/ +def copyCfg (a b u v m : ℕ) : Cfg 28 2 := + ⟨6, Tape.mk' ((ones u ((ones b ((ones a B0).cons 0)).cons 0)).cons 0) + (ones v ((ones m B0).cons 0))⟩ + +/-- `EN` on the first of the `b` remaining cells of `N`, `a` cells to its left; the template +has `V` cells and `M` has `m`. -/ +def enCfg (a b V m : ℕ) : Cfg 28 2 := + ⟨4, Tape.mk' (ones a B0) (ones b ((ones V ((ones m B0).cons 0)).cons 0))⟩ + +/-- Inside the test: `TR0` on the separator right of `B`; the hole of `B` has `a` cells to its +left and `r` to its right; `S` has `p` cells, a hole, and `q` cells (`q = 0` and the blank +beyond `S` as the hole for a solid `S` of `p` cells). -/ +def advCfg (n a r p q : ℕ) : Cfg 28 2 := + ⟨13, Tape.mk' (ones r ((ones a (NL n)).cons 0)) ((ones p ((ones q B0).cons 0)).cons 0)⟩ + +theorem ones_zero (L : ListBlank (Fin 2)) : ones 0 L = L := rfl + +theorem ones_zero_B0 : ones 0 B0 = B0 := rfl + +theorem advCfg_solid (n a r p : ℕ) : + advCfg n a r p 0 = ⟨13, Tape.mk' (ones r ((ones a (NL n)).cons 0)) ((ones p B0).cons 0)⟩ := by + simp only [advCfg, ones_zero_B0, B0_cons_zero] + +/-! ## Multiply (States `2`–`11`) -/ + +/-- One copy step: the cursor moves one cell right and `M` gains a cell. -/ +theorem copy_step (a b u v m : ℕ) : + ∃ s, nSteps brocard28 s (copyCfg a b u (v + 1) m) = some (copyCfg a b (u + 1) v (m + 1)) := by + set W := (ones b ((ones a B0).cons 0)).cons 0 with hW + set Y := ones u W with hY + have h₁ := sweepR sR0_1 (v + 1) (Y.cons 0) ((ones m B0).cons 0) + have h₂ : step brocard28 ⟨6, Tape.mk' (ones (v + 1) (Y.cons 0)) ((ones m B0).cons 0)⟩ = + some ⟨7, Tape.mk' ((ones (v + 1) (Y.cons 0)).cons 0) (ones m B0)⟩ := by + rw [mk'_cons, sR0_0] + have h₃ := sweepR sR1_1 m ((ones (v + 1) (Y.cons 0)).cons 0) B0 + have h₄ : step brocard28 ⟨7, Tape.mk' (ones m ((ones (v + 1) (Y.cons 0)).cons 0)) B0⟩ = + some ⟨8, mkL (ones m ((ones (v + 1) (Y.cons 0)).cons 0)) (B0.cons 1)⟩ := by + rw [mkRight_blank, sR1_0] + have h₅ := sweepL sL0_1 m ((ones (v + 1) (Y.cons 0)).cons 0) (B0.cons 1) + have h₆ : step brocard28 ⟨8, mkL ((ones (v + 1) (Y.cons 0)).cons 0) (ones m (B0.cons 1))⟩ = + some ⟨9, mkL (ones (v + 1) (Y.cons 0)) ((ones (m + 1) B0).cons 0)⟩ := by + rw [mkL_cons, sL0_0, ones_succ' m] + have h₇ := sweepL sL1_1 (v + 1) (Y.cons 0) ((ones (m + 1) B0).cons 0) + have h₈ : nSteps brocard28 2 ⟨9, mkL (Y.cons 0) (ones (v + 1) ((ones (m + 1) B0).cons 0))⟩ = + some (copyCfg a b (u + 1) v (m + 1)) := by + change nSteps brocard28 2 ⟨9, mkL (Y.cons 0) ((ones v ((ones (m + 1) B0).cons 0)).cons 1)⟩ = + some ⟨6, Tape.mk' ((Y.cons 1).cons 0) (ones v ((ones (m + 1) B0).cons 0))⟩ + simp only [mkL_cons, nSteps_succ, sL1_0, mk'_cons, nSteps_zero, Option.bind_some, sEC_1] + exact ⟨_, nSteps_bind_eq h₁ (step_nSteps h₂ (nSteps_bind_eq h₃ (step_nSteps h₄ + (nSteps_bind_eq h₅ (step_nSteps h₆ (nSteps_bind_eq h₇ h₈))))))⟩ + +/-- The last copy step: the cursor leaves the template, `LN0`/`LN1` return to the hole of `N` +and fill it, and `EN` reads the next cell of `N`. -/ +theorem copy_done (a b u m : ℕ) : + ∃ s, nSteps brocard28 s (copyCfg a b u 0 m) = some (enCfg (a + 1) b (u + 1) (m + 1)) := by + set W := (ones b ((ones a B0).cons 0)).cons 0 with hW + set Y := ones u W with hY + have h₂ : step brocard28 (copyCfg a b u 0 m) = + some ⟨7, Tape.mk' ((Y.cons 0).cons 0) (ones m B0)⟩ := by + change step brocard28 ⟨6, Tape.mk' (Y.cons 0) ((ones m B0).cons 0)⟩ = _ + rw [mk'_cons, sR0_0] + have h₃ := sweepR sR1_1 m ((Y.cons 0).cons 0) B0 + have h₄ : step brocard28 ⟨7, Tape.mk' (ones m ((Y.cons 0).cons 0)) B0⟩ = + some ⟨8, mkL (ones m ((Y.cons 0).cons 0)) (B0.cons 1)⟩ := by + rw [mkRight_blank, sR1_0] + have h₅ := sweepL sL0_1 m ((Y.cons 0).cons 0) (B0.cons 1) + have h₆ : nSteps brocard28 3 ⟨8, mkL ((Y.cons 0).cons 0) (ones m (B0.cons 1))⟩ = + some ⟨2, mkL (Y.cons 1) ((ones (m + 1) B0).cons 0)⟩ := by + rw [ones_succ' m] + simp only [mkL_cons, nSteps_succ, sL0_0, nSteps_zero, Option.bind_some, sL1_0, mk'_cons, + sEC_0] + have h₇ := sweepL sLN0_1 (u + 1) W ((ones (m + 1) B0).cons 0) + have h₈ : step brocard28 ⟨2, mkL W (ones (u + 1) ((ones (m + 1) B0).cons 0))⟩ = + some ⟨3, mkL (ones b ((ones a B0).cons 0)) + ((ones (u + 1) ((ones (m + 1) B0).cons 0)).cons 0)⟩ := by + rw [hW, mkL_cons, sLN0_0] + have h₉ := sweepL sLN1_1 b ((ones a B0).cons 0) + ((ones (u + 1) ((ones (m + 1) B0).cons 0)).cons 0) + have h₁₀ : step brocard28 ⟨3, mkL ((ones a B0).cons 0) + (ones b ((ones (u + 1) ((ones (m + 1) B0).cons 0)).cons 0))⟩ = + some (enCfg (a + 1) b (u + 1) (m + 1)) := by + rw [mkL_cons, sLN1_0] + rfl + have hY1 : Y.cons 1 = ones (u + 1) W := rfl + rw [hY1] at h₆ + exact ⟨_, step_nSteps h₂ (nSteps_bind_eq h₃ (step_nSteps h₄ (nSteps_bind_eq h₅ + (nSteps_bind_eq h₆ (nSteps_bind_eq h₇ (step_nSteps h₈ (nSteps_bind_eq h₉ + (step_one h₁₀))))))))⟩ + +/-- A whole copy of the template. -/ +theorem copy_loop (a b u : ℕ) : ∀ v m, ∃ s, nSteps brocard28 s (copyCfg a b u v m) = + some (enCfg (a + 1) b (u + v + 1) (m + v + 1)) := by + intro v + induction v generalizing u with + | zero => intro m; simpa using copy_done a b u m + | succ v ih => + intro m + obtain ⟨s₁, hs₁⟩ := copy_step a b u v m + obtain ⟨s₂, hs₂⟩ := ih (u + 1) (m + 1) + refine ⟨s₁ + s₂, ?_⟩ + rw [show u + (v + 1) + 1 = u + 1 + v + 1 by omega, + show m + (v + 1) + 1 = m + 1 + v + 1 by omega] + exact nSteps_bind_eq hs₁ hs₂ + +/-- `EN` finds a cell of `N`: it becomes the hole, `S1` runs to `B`, and `EC` makes the cursor. -/ +theorem en_step (a c w m : ℕ) : + ∃ s, nSteps brocard28 s (enCfg a (c + 1) (w + 1) m) = some (copyCfg a c 0 w m) := by + have h₁ : step brocard28 (enCfg a (c + 1) (w + 1) m) = + some ⟨5, Tape.mk' ((ones a B0).cons 0) + (ones c ((ones (w + 1) ((ones m B0).cons 0)).cons 0))⟩ := by + change step brocard28 ⟨4, Tape.mk' (ones a B0) + ((ones c ((ones (w + 1) ((ones m B0).cons 0)).cons 0)).cons 1)⟩ = _ + rw [mk'_cons, sEN_1] + have h₂ := sweepR sS1_1 c ((ones a B0).cons 0) ((ones (w + 1) ((ones m B0).cons 0)).cons 0) + have h₃ : nSteps brocard28 2 ⟨5, Tape.mk' (ones c ((ones a B0).cons 0)) + ((ones (w + 1) ((ones m B0).cons 0)).cons 0)⟩ = some (copyCfg a c 0 w m) := by + change nSteps brocard28 2 ⟨5, Tape.mk' (ones c ((ones a B0).cons 0)) + (((ones w ((ones m B0).cons 0)).cons 1).cons 0)⟩ = + some ⟨6, Tape.mk' (((ones c ((ones a B0).cons 0)).cons 0).cons 0) + (ones w ((ones m B0).cons 0))⟩ + simp only [mk'_cons, nSteps_succ, sS1_0, nSteps_zero, Option.bind_some, sEC_1] + exact ⟨_, step_nSteps h₁ (nSteps_bind_eq h₂ h₃)⟩ + +/-- The hole of `N` walks through the `b` remaining cells, one copy of the template each. -/ +theorem en_loop (w : ℕ) : ∀ a b m, ∃ s, nSteps brocard28 s (enCfg a b (w + 1) m) = + some (enCfg (a + b) 0 (w + 1) (m + b * (w + 1))) := by + intro a b + induction b generalizing a with + | zero => intro m; exact ⟨0, by simp [enCfg]⟩ + | succ c ih => + intro m + obtain ⟨s₁, hs₁⟩ := en_step a c w m + obtain ⟨s₂, hs₂⟩ := copy_loop a c 0 w m + obtain ⟨s₃, hs₃⟩ := ih (a + 1) (m + w + 1) + refine ⟨s₁ + s₂ + s₃, ?_⟩ + rw [show a + (c + 1) = a + 1 + c by omega, + show m + (c + 1) * (w + 1) = m + w + 1 + c * (w + 1) by ring] + simp only [Nat.zero_add] at hs₂ + exact nSteps_bind_eq (nSteps_bind_eq hs₁ hs₂) hs₃ + +/-- `N` exhausted: `X` merges the template and `M`, `TLs` returns to the start of `B`. -/ +theorem en_exit (a V m : ℕ) : + ∃ s, nSteps brocard28 s (enCfg a 0 V (m + 1)) = some (testCfg a (V + m + 2)) := by + have h₁ : step brocard28 (enCfg a 0 V (m + 1)) = + some ⟨11, Tape.mk' ((ones a B0).cons 0) (ones V ((ones (m + 1) B0).cons 0))⟩ := by + change step brocard28 ⟨4, Tape.mk' (ones a B0) + ((ones V ((ones (m + 1) B0).cons 0)).cons 0)⟩ = _ + rw [mk'_cons, sEN_0] + have h₂ := sweepR sX_1 V ((ones a B0).cons 0) ((ones (m + 1) B0).cons 0) + have h₃ : step brocard28 ⟨11, Tape.mk' (ones V ((ones a B0).cons 0)) + ((ones (m + 1) B0).cons 0)⟩ = some ⟨12, mkL (ones (V + 2) (NL a)) (ones m B0)⟩ := by + rw [mk'_cons, sX_0] + change some (⟨12, Tape.mk' ((ones V (NL a)).cons 1) ((ones m B0).cons 1)⟩ : Cfg 28 2) = + some (⟨12, mkL (((ones V (NL a)).cons 1).cons 1) (ones m B0)⟩ : Cfg 28 2) + rw [mk'_cons, mkL_cons] + have h₄ := sweepL sTLs_1 (V + 2) (NL a) (ones m B0) + have h₅ : step brocard28 ⟨12, mkL (NL a) (ones (V + 2) (ones m B0))⟩ = + some (testCfg a (V + m + 2)) := by + rw [NL, mkL_cons, sTLs_0, ← ones_add] + rw [show V + 2 + m = V + m + 2 by omega] + rfl + exact ⟨_, step_nSteps h₁ (nSteps_bind_eq h₂ (step_nSteps h₃ (nSteps_bind_eq h₄ + (step_one h₅))))⟩ + +/-- `LN1` on the blank left of `N` writes the new cell of `N`, and `EN` reads `N`'s first +cell. -/ +theorem ln_exit (n v : ℕ) : + step brocard28 ⟨3, mkL B0 (ones n ((ones (v + 1) B0).cons 0))⟩ = + some (enCfg 1 n (v + 1) 0) := by + rw [mkL_B0, sLN1_0] + change _ = some (⟨4, Tape.mk' (B0.cons 1) + (ones n ((ones (v + 1) ((ones 0 B0).cons 0)).cons 0))⟩ : Cfg 28 2) + rw [ones_zero_B0, B0_cons_zero] + +/-- The start of a round: `LN0`/`LN1` write a cell left of `N` and `EN` reads its first cell. -/ +theorem round_start (n v : ℕ) : + ∃ s, nSteps brocard28 s (roundCfg n v) = some (enCfg 1 n (v + 1) 0) := by + have h₁ : roundCfg n v = ⟨2, mkL (ones (v + 1) (NL n)) B0⟩ := by + change (⟨2, Tape.mk' (ones v (NL n)) (B0.cons 1)⟩ : Cfg 28 2) = + ⟨2, mkL ((ones v (NL n)).cons 1) B0⟩ + rw [mk'_cons, mkL_cons] + have h₂ := sweepL sLN0_1 (v + 1) (NL n) B0 + have h₃ : step brocard28 ⟨2, mkL (NL n) (ones (v + 1) B0)⟩ = + some ⟨3, mkL (ones n B0) ((ones (v + 1) B0).cons 0)⟩ := by + rw [NL, mkL_cons, sLN0_0] + have h₄ := sweepL sLN1_1 n B0 ((ones (v + 1) B0).cons 0) + rw [h₁] + exact ⟨_, nSteps_bind_eq h₂ (step_nSteps h₃ (nSteps_bind_eq h₄ (step_one (ln_exit n v))))⟩ + +/-- From `EN` on the first cell of `N`: `B` with `v + 1` cells becomes `(n + 1) (v + 1) + 1` +cells and the test starts. -/ +theorem multiply_en (n v : ℕ) (hn : 1 ≤ n) : + ∃ s, nSteps brocard28 s (enCfg 1 n (v + 1) 0) = + some (testCfg (n + 1) ((n + 1) * (v + 1) + 1)) := by + obtain ⟨s₂, hs₂⟩ := en_loop v 1 n 0 + obtain ⟨m, hm⟩ : ∃ m, 0 + n * (v + 1) = m + 1 := ⟨n * (v + 1) - 1, by + have : 1 ≤ n * (v + 1) := Nat.one_le_iff_ne_zero.mpr (Nat.mul_ne_zero (by omega) (by omega)) + omega⟩ + rw [hm] at hs₂ + obtain ⟨s₃, hs₃⟩ := en_exit (1 + n) (v + 1) m + refine ⟨s₂ + s₃, ?_⟩ + have e : (n + 1) * (v + 1) + 1 = v + 1 + m + 2 := by + have : (n + 1) * (v + 1) = v + 1 + n * (v + 1) := by ring + omega + rw [show n + 1 = 1 + n by omega] at e ⊢ + rw [e] + exact nSteps_bind_eq hs₂ hs₃ + +/-- A whole round: `B` with `v + 1` cells becomes `(n + 1) (v + 1) + 1` cells and `N` grows. -/ +theorem multiply (n v : ℕ) (hn : 1 ≤ n) : + ∃ s, 1 ≤ s ∧ nSteps brocard28 s (roundCfg n v) = + some (testCfg (n + 1) ((n + 1) * (v + 1) + 1)) := by + obtain ⟨s₁, hs₁⟩ := round_start n v + obtain ⟨s₂, hs₂⟩ := multiply_en n v hn + refine ⟨s₁ + s₂, ?_, nSteps_bind_eq hs₁ hs₂⟩ + have : 1 ≤ s₁ := by + rcases Nat.eq_zero_or_pos s₁ with rfl | h + · simp [roundCfg, enCfg] at hs₁ + · exact h + omega + +/-! ## Test and Fail (States `1`, `12`–`22`) -/ + +/-- `k` zeros followed by `L`. -/ +def zeros (k : ℕ) (L : ListBlank (Fin 2)) : ListBlank (Fin 2) := + ListBlank.append (List.replicate k 0) L + +theorem zeros_succ' (k : ℕ) (L : ListBlank (Fin 2)) : zeros (k + 1) L = zeros k (L.cons 0) := by + rw [zeros, List.replicate_succ', ListBlank.append_assoc] + rfl + +theorem zeros_B0 (k : ℕ) : zeros k B0 = B0 := by + induction k with + | zero => rfl + | succ k ih => rw [zeros_succ', B0_cons_zero, ih] + +/-- A state that erases ones while moving right. -/ +theorem sweepR_erase {q : Fin 28} + (h : ∀ L R, step brocard28 ⟨q, ⟨1, L, R⟩⟩ = some ⟨q, Tape.mk' (L.cons 0) R⟩) + (k : ℕ) (L R : ListBlank (Fin 2)) : + nSteps brocard28 k ⟨q, Tape.mk' L (ones k R)⟩ = some ⟨q, Tape.mk' (zeros k L) R⟩ := by + induction k generalizing L with + | zero => rfl + | succ k ih => + rw [nSteps_succ, show ones (k + 1) R = (ones k R).cons 1 from rfl, + mk'_cons, h, Option.bind_some, ih, zeros_succ'] + +/-- A state that moves left over zeros. -/ +theorem sweepL_zeros {q : Fin 28} + (h : ∀ L R, step brocard28 ⟨q, ⟨0, L, R⟩⟩ = some ⟨q, mkL L (R.cons 0)⟩) + (k : ℕ) (L R : ListBlank (Fin 2)) : + nSteps brocard28 k ⟨q, mkL (zeros k L) R⟩ = some ⟨q, mkL L (zeros k R)⟩ := by + induction k generalizing R with + | zero => rfl + | succ k ih => + rw [nSteps_succ, show zeros (k + 1) L = (zeros k L).cons 0 from rfl, + mkL_cons, h, Option.bind_some, ih, zeros_succ'] + +/-- `TR0` right after the hole of `B`, with `r` cells of `B` still to pass. -/ +def tCfg (n a r p q : ℕ) : Cfg 28 2 := + ⟨13, Tape.mk' ((ones a (NL n)).cons 0) (ones r ((ones p ((ones q B0).cons 0)).cons 0))⟩ + +theorem t_to_adv (n a r p q : ℕ) : + nSteps brocard28 r (tCfg n a r p q) = some (advCfg n a r p q) := + sweepR sTR0_1 r _ _ + +/-- The test starts: `TLx` erases the first cell of `B`. -/ +theorem test_start (n r : ℕ) : + ∃ s, nSteps brocard28 s (testCfg n (r + 1)) = some (advCfg n 0 r 0 0) := by + have h₁ : step brocard28 (testCfg n (r + 1)) = some (tCfg n 0 r 0 0) := by + change step brocard28 ⟨1, Tape.mk' (NL n) ((ones r B0).cons 1)⟩ = + some ⟨13, Tape.mk' ((NL n).cons 0) (ones r ((ones 0 ((ones 0 B0).cons 0)).cons 0))⟩ + rw [mk'_cons, sTLx_1, ones_zero_B0, B0_cons_zero, ones_zero_B0, + B0_cons_zero] + exact ⟨_, step_nSteps h₁ (t_to_adv n 0 r 0 0)⟩ + +/-- One unit: the hole of `S` moves one cell left and the hole of `B` one cell right. -/ +theorem adv_step (n a r p q : ℕ) : + ∃ s, nSteps brocard28 s (advCfg n a (r + 1) (p + 1) q) = some (tCfg n (a + 1) r p (q + 1)) := by + set H := (ones a (NL n)).cons 0 with hH + set Lr := ones (r + 1) H with hLr + have h₁ : step brocard28 (advCfg n a (r + 1) (p + 1) q) = + some ⟨14, Tape.mk' (Lr.cons 0) (ones (p + 1) ((ones q B0).cons 0))⟩ := by + change step brocard28 ⟨13, Tape.mk' Lr ((ones (p + 1) ((ones q B0).cons 0)).cons 0)⟩ = _ + rw [mk'_cons, sTR0_0] + have h₂ := sweepR sTR1_1 (p + 1) (Lr.cons 0) ((ones q B0).cons 0) + have h₃ : nSteps brocard28 2 ⟨14, Tape.mk' (ones (p + 1) (Lr.cons 0)) ((ones q B0).cons 0)⟩ = + some ⟨17, mkL (ones p (Lr.cons 0)) ((ones (q + 1) B0).cons 0)⟩ := by + change nSteps brocard28 2 ⟨14, Tape.mk' ((ones p (Lr.cons 0)).cons 1) ((ones q B0).cons 0)⟩ = + some ⟨17, mkL (ones p (Lr.cons 0)) (((ones q B0).cons 1).cons 0)⟩ + simp only [mk'_cons, nSteps_succ, sTR1_0, mkL_cons, nSteps_zero, Option.bind_some, sSB_1] + have h₄ := sweepL sTL0_1 p (Lr.cons 0) ((ones (q + 1) B0).cons 0) + have h₅ : step brocard28 ⟨17, mkL (Lr.cons 0) (ones p ((ones (q + 1) B0).cons 0))⟩ = + some ⟨18, mkL Lr ((ones p ((ones (q + 1) B0).cons 0)).cons 0)⟩ := by + rw [mkL_cons, sTL0_0] + have h₆ := sweepL sTL1_1 (r + 1) H ((ones p ((ones (q + 1) B0).cons 0)).cons 0) + have h₇ : nSteps brocard28 2 + ⟨18, mkL H (ones (r + 1) ((ones p ((ones (q + 1) B0).cons 0)).cons 0))⟩ = + some (tCfg n (a + 1) r p (q + 1)) := by + change nSteps brocard28 2 ⟨18, mkL ((ones a (NL n)).cons 0) + ((ones r ((ones p ((ones (q + 1) B0).cons 0)).cons 0)).cons 1)⟩ = + some ⟨13, Tape.mk' (((ones a (NL n)).cons 1).cons 0) + (ones r ((ones p ((ones (q + 1) B0).cons 0)).cons 0))⟩ + simp only [mkL_cons, nSteps_succ, sTL1_0, mk'_cons, nSteps_zero, Option.bind_some, sTE_1] + exact ⟨_, step_nSteps h₁ (nSteps_bind_eq h₂ (nSteps_bind_eq h₃ (nSteps_bind_eq h₄ + (step_nSteps h₅ (nSteps_bind_eq h₆ h₇)))))⟩ + +/-- `p` units in a row, while `B` has enough cells. -/ +theorem adv_many (n : ℕ) : ∀ p a r p' q, ∃ s, nSteps brocard28 s (advCfg n a (r + p) (p + p') q) = + some (advCfg n (a + p) r p' (q + p)) := by + intro p + induction p with + | zero => intro a r p' q; exact ⟨0, by simp⟩ + | succ p ih => + intro a r p' q + obtain ⟨s₁, hs₁⟩ := adv_step n a (r + p) (p + p') q + have h₂ := t_to_adv n (a + 1) (r + p) (p + p') (q + 1) + obtain ⟨s₃, hs₃⟩ := ih (a + 1) r p' (q + 1) + refine ⟨s₁ + (r + p) + s₃, ?_⟩ + rw [show r + (p + 1) = r + p + 1 by omega, show p + 1 + p' = p + p' + 1 by omega, + show a + (p + 1) = a + 1 + p by omega, show q + (p + 1) = q + 1 + p by omega] + exact nSteps_bind_eq (nSteps_bind_eq hs₁ h₂) hs₃ + +/-- The wrap: the hole of `S` is at its first cell, `B` has cells left; `S` becomes solid with +one more cell and the next chunk starts. -/ +theorem adv_wrap (n a r q : ℕ) : + nSteps brocard28 4 (advCfg n a (r + 1) 0 q) = some (advCfg n a (r + 1) (q + 1) 0) := by + rw [advCfg_solid] + change nSteps brocard28 4 ⟨13, Tape.mk' ((ones r ((ones a (NL n)).cons 0)).cons 1) + (((ones q B0).cons 0).cons 0)⟩ = + some ⟨13, Tape.mk' ((ones r ((ones a (NL n)).cons 0)).cons 1) + ((((ones q B0).cons 1).cons 0))⟩ + simp only [mk'_cons, nSteps_succ, sTR0_0, nSteps_zero, Option.bind_some, sTR1_0, mkL_cons, + sSB_0, sSC_1] + +/-- The wrap with `B` exhausted: success, `M0` starts the gate on the last cell of `B`. -/ +theorem adv_success (n a q : ℕ) : + nSteps brocard28 4 (advCfg n a 0 0 q) = + some ⟨23, mkL (ones a (NL n)) (((ones (q + 1) B0).cons 0).cons 0)⟩ := by + change nSteps brocard28 4 ⟨13, Tape.mk' ((ones a (NL n)).cons 0) (((ones q B0).cons 0).cons 0)⟩ = + some ⟨23, mkL (ones a (NL n)) ((((ones q B0).cons 1).cons 0).cons 0)⟩ + simp only [mk'_cons, nSteps_succ, sTR0_0, nSteps_zero, Option.bind_some, sTR1_0, mkL_cons, + sSB_0, sSC_0] + +/-- A unit with `B` exhausted: `TE` finds the separator, `ER`/`ER2` erase `S`, `ER3` returns, +erasing the last cell of `B`, and the next round starts. -/ +theorem adv_fail (n a p q : ℕ) : + ∃ s, nSteps brocard28 s (advCfg n (a + 1) 0 (p + 1) q) = some (roundCfg n a) := by + set H := (ones (a + 1) (NL n)).cons 0 with hH + have h₁ : step brocard28 (advCfg n (a + 1) 0 (p + 1) q) = + some ⟨14, Tape.mk' (H.cons 0) (ones (p + 1) ((ones q B0).cons 0))⟩ := by + change step brocard28 ⟨13, Tape.mk' H ((ones (p + 1) ((ones q B0).cons 0)).cons 0)⟩ = _ + rw [mk'_cons, sTR0_0] + have h₂ := sweepR sTR1_1 (p + 1) (H.cons 0) ((ones q B0).cons 0) + have h₃ : nSteps brocard28 2 ⟨14, Tape.mk' (ones (p + 1) (H.cons 0)) ((ones q B0).cons 0)⟩ = + some ⟨17, mkL (ones p (H.cons 0)) ((ones (q + 1) B0).cons 0)⟩ := by + change nSteps brocard28 2 ⟨14, Tape.mk' ((ones p (H.cons 0)).cons 1) ((ones q B0).cons 0)⟩ = + some ⟨17, mkL (ones p (H.cons 0)) (((ones q B0).cons 1).cons 0)⟩ + simp only [mk'_cons, nSteps_succ, sTR1_0, mkL_cons, nSteps_zero, Option.bind_some, sSB_1] + have h₄ := sweepL sTL0_1 p (H.cons 0) ((ones (q + 1) B0).cons 0) + have h₅ : nSteps brocard28 3 ⟨17, mkL (H.cons 0) (ones p ((ones (q + 1) B0).cons 0))⟩ = + some ⟨20, Tape.mk' (((ones (a + 1) (NL n)).cons 1).cons 0) + (ones p ((ones (q + 1) B0).cons 0))⟩ := by + rw [hH] + simp only [mkL_cons, nSteps_succ, sTL0_0, nSteps_zero, Option.bind_some, sTL1_0, mk'_cons, + sTE_0] + have h₆ := sweepR_erase sER_1 p (((ones (a + 1) (NL n)).cons 1).cons 0) ((ones (q + 1) B0).cons 0) + set W := ((ones (a + 1) (NL n)).cons 1).cons 0 with hW + have h₇ : step brocard28 ⟨20, Tape.mk' (zeros p W) ((ones (q + 1) B0).cons 0)⟩ = + some ⟨21, Tape.mk' ((zeros p W).cons 0) (ones (q + 1) B0)⟩ := by + rw [mk'_cons, sER_0] + have h₈ := sweepR_erase sER2_1 (q + 1) ((zeros p W).cons 0) B0 + have h₉ : step brocard28 ⟨21, Tape.mk' (zeros (q + 1) ((zeros p W).cons 0)) B0⟩ = + some ⟨22, mkL (zeros (q + 1) ((zeros p W).cons 0)) B0⟩ := by + rw [mkRight_blank, sER2_0, B0_cons_zero] + have h₁₀ := sweepL_zeros sER3_0 (q + 1) ((zeros p W).cons 0) B0 + rw [zeros_B0] at h₁₀ + have h₁₁ : step brocard28 ⟨22, mkL ((zeros p W).cons 0) B0⟩ = some ⟨22, mkL (zeros p W) B0⟩ := by + rw [mkL_cons, sER3_0, B0_cons_zero] + have h₁₂ := sweepL_zeros sER3_0 p W B0 + rw [zeros_B0] at h₁₂ + have h₁₃ : nSteps brocard28 2 ⟨22, mkL W B0⟩ = some (roundCfg n a) := by + rw [hW] + change nSteps brocard28 2 ⟨22, mkL ((((ones a (NL n)).cons 1).cons 1).cons 0) B0⟩ = + some ⟨2, Tape.mk' (ones a (NL n)) (B0.cons 1)⟩ + simp only [mkL_cons, nSteps_succ, sER3_0, B0_cons_zero, nSteps_zero, Option.bind_some, sER3_1, + mk'_cons] + exact ⟨_, step_nSteps h₁ (nSteps_bind_eq h₂ (nSteps_bind_eq h₃ (nSteps_bind_eq h₄ + (nSteps_bind_eq h₅ (nSteps_bind_eq h₆ (step_nSteps h₇ (nSteps_bind_eq h₈ (step_nSteps h₉ + (nSteps_bind_eq h₁₀ (step_nSteps h₁₁ (nSteps_bind_eq h₁₂ h₁₃)))))))))))⟩ + +/-! ## Triangular Numbers and the Chunk Loop -/ + +/-- `pre s = 0 + 1 + ⋯ + (s - 1)`: the cells of `B` consumed before the chunk of size `s`. -/ +def pre : ℕ → ℕ + | 0 => 0 + | s + 1 => pre s + s + +/-- `tri k = 1 + 2 + ⋯ + k`. -/ +def tri (k : ℕ) : ℕ := pre (k + 1) + +theorem tri_succ (k : ℕ) : tri (k + 1) = tri k + (k + 1) := rfl + +theorem tri_strictMono : StrictMono tri := + strictMono_nat_of_lt_succ fun k => by rw [tri_succ]; omega + +theorem tri_eq_iff (j k : ℕ) : tri j = tri k ↔ j = k := tri_strictMono.injective.eq_iff + +/-- Nothing strictly between two consecutive triangular numbers is triangular. -/ +theorem not_tri_between {k x : ℕ} (h₁ : tri k < x) (h₂ : x < tri (k + 1)) : ¬ ∃ j, x = tri j := by + rintro ⟨j, rfl⟩ + have := tri_strictMono.lt_iff_lt.mp h₁ + have := tri_strictMono.lt_iff_lt.mp h₂ + omega + +/-- The success configuration: `M0` on the last cell of `B` (`a` ones, the hole after them), +`S` solid with `S` cells. -/ +def succCfg (n a S : ℕ) : Cfg 28 2 := + ⟨23, mkL (ones a (NL n)) (((ones S B0).cons 0).cons 0)⟩ + +/-- From the start of the chunk of size `s ≥ 1` with `r + 1` cells of `B` left: success if the +cells of `B` form a triangular number, otherwise the next round. -/ +theorem chunk_loop (n : ℕ) : ∀ r s a, 1 ≤ s → a = pre s → + (∃ j, a + r + 1 = tri j ∧ ∃ t, nSteps brocard28 t (advCfg n a (r + 1) s 0) = + some (succCfg n (a + r + 1) (j + 1))) ∨ + ((¬ ∃ j, a + r + 1 = tri j) ∧ ∃ t, nSteps brocard28 t (advCfg n a (r + 1) s 0) = + some (roundCfg n (a + r))) := by + intro r + induction r using Nat.strong_induction_on with + | _ r ih => + intro s a hs ha + rcases Nat.lt_or_ge (r + 1) s with hlt | hge + · -- not enough cells: fail inside this chunk + obtain ⟨p', hp'⟩ : ∃ p', s = r + 1 + (p' + 1) := ⟨s - r - 2, by omega⟩ + obtain ⟨t₁, ht₁⟩ := adv_many n (r + 1) a 0 (p' + 1) 0 + rw [Nat.zero_add, ← hp', show a + (r + 1) = a + r + 1 by omega] at ht₁ + obtain ⟨t₂, ht₂⟩ := adv_fail n (a + r) p' (r + 1) + refine Or.inr ⟨?_, t₁ + t₂, nSteps_bind_eq ht₁ ht₂⟩ + obtain ⟨s', rfl⟩ : ∃ s', s = s' + 1 := ⟨s - 1, by omega⟩ + have e1 : tri s' = pre s' + s' := rfl + have e2 : tri (s' + 1) = pre s' + s' + (s' + 1) := rfl + have e3 : pre (s' + 1) = pre s' + s' := rfl + have h1 : tri s' < a + r + 1 := by omega + have h2 : a + r + 1 < tri (s' + 1) := by omega + exact not_tri_between h1 h2 + · -- the whole chunk fits + obtain ⟨r', hr'⟩ : ∃ r', r + 1 = r' + s := ⟨r + 1 - s, by omega⟩ + obtain ⟨t₁, ht₁⟩ := adv_many n s a r' 0 0 + rw [Nat.zero_add, Nat.add_zero, ← hr'] at ht₁ + rcases Nat.eq_zero_or_pos r' with rfl | hr'pos + · -- exhausted exactly at the wrap: success + have ht₂ := adv_success n (a + s) s + refine Or.inl ⟨s, ?_, t₁ + 4, ?_⟩ + · rw [tri, pre, ha]; omega + · rw [show a + r + 1 = a + s by omega] + exact nSteps_bind_eq ht₁ ht₂ + · obtain ⟨r'', rfl⟩ : ∃ r'', r' = r'' + 1 := ⟨r' - 1, by omega⟩ + have ht₂ := adv_wrap n (a + s) r'' s + obtain ⟨j, hj, t₃, ht₃⟩ | ⟨hj, t₃, ht₃⟩ := + ih r'' (by omega) (s + 1) (a + s) (by omega) (by rw [ha]; rfl) + · refine Or.inl ⟨j, ?_, t₁ + 4 + t₃, ?_⟩ + · rw [← hj]; omega + · rw [show a + r + 1 = a + s + r'' + 1 by omega] + exact nSteps_bind_eq (nSteps_bind_eq ht₁ ht₂) ht₃ + · refine Or.inr ⟨?_, t₁ + 4 + t₃, ?_⟩ + · rw [show a + r + 1 = a + s + r'' + 1 by omega]; exact hj + · rw [show a + r = a + s + r'' by omega] + exact nSteps_bind_eq (nSteps_bind_eq ht₁ ht₂) ht₃ + +/-- The test of `B` with `M + 1` cells: the first cell is consumed; if `M` is triangular the +machine reaches the success configuration, otherwise the next round with `M` cells. -/ +theorem test_run (n M : ℕ) : + (∃ j, M = tri j ∧ ∃ t, nSteps brocard28 t (testCfg n (M + 1)) = + some (succCfg n M (j + 1))) ∨ + ((¬ ∃ j, M = tri j) ∧ ∃ t, nSteps brocard28 t (testCfg n (M + 1)) = + some (roundCfg n (M - 1))) := by + obtain ⟨t₀, ht₀⟩ := test_start n M + cases M with + | zero => + exact Or.inl ⟨0, rfl, t₀ + 4, nSteps_bind_eq ht₀ (adv_success n 0 0)⟩ + | succ r => + have ht₁ := adv_wrap n 0 r 0 + obtain ⟨j, hj, t, ht⟩ | ⟨hj, t, ht⟩ := chunk_loop n r 1 0 le_rfl rfl + · simp only [Nat.zero_add] at hj ht + exact Or.inl ⟨j, hj, t₀ + 4 + t, nSteps_bind_eq (nSteps_bind_eq ht₀ ht₁) ht⟩ + · simp only [Nat.zero_add] at hj ht + rw [Nat.add_sub_cancel] + exact Or.inr ⟨hj, t₀ + 4 + t, nSteps_bind_eq (nSteps_bind_eq ht₀ ht₁) ht⟩ + +/-! ## Gate (States `23`–`27`) and Start (State `0`) -/ + +/-- `M0`–`M3` count the ones of `B` modulo `4` walking left: four ones return to `M0`. -/ +theorem gate_four (k : ℕ) (L R : ListBlank (Fin 2)) : + nSteps brocard28 4 ⟨23, mkL (ones (k + 4) L) R⟩ = some ⟨23, mkL (ones k L) (ones 4 R)⟩ := by + change nSteps brocard28 4 ⟨23, mkL (((((ones k L).cons 1).cons 1).cons 1).cons 1) R⟩ = + some ⟨23, mkL (ones k L) ((((R.cons 1).cons 1).cons 1).cons 1)⟩ + simp only [mkL_cons, nSteps_succ, sM0_1, nSteps_zero, Option.bind_some, sM1_1, sM2_1, sM3_1] + +/-- `4 j` ones of `B` bring `M0` back to `M0`. -/ +theorem gate_mult (j : ℕ) : ∀ (k : ℕ) (L R : ListBlank (Fin 2)), + nSteps brocard28 (4 * j) ⟨23, mkL (ones (4 * j + k) L) R⟩ = + some ⟨23, mkL (ones k L) (ones (4 * j) R)⟩ := by + induction j with + | zero => intro k L R; simp only [Nat.mul_zero, Nat.zero_add, ones_zero]; rfl + | succ j ih => + intro k L R + have h₁ : nSteps brocard28 4 ⟨23, mkL (ones (4 * (j + 1) + k) L) R⟩ = + some ⟨23, mkL (ones (4 * j + k) L) (ones 4 R)⟩ := by + rw [show 4 * (j + 1) + k = 4 * j + k + 4 by omega]; exact gate_four _ L R + have := nSteps_bind_eq h₁ (ih k L (ones 4 R)) + rwa [← ones_add, show 4 * j + 4 = 4 * (j + 1) by omega, + show 4 + 4 * j = 4 * (j + 1) by omega] at this + +/-- Residue `0`: `M0` reaches the separator and halts (`M0` reading `0` is undefined). -/ +theorem gate_halt (n j : ℕ) (Y : ListBlank (Fin 2)) : + ∃ cfg, nSteps brocard28 (4 * j) ⟨23, mkL (ones (4 * j) (NL n)) Y⟩ = some cfg ∧ + step brocard28 cfg = none := by + refine ⟨⟨23, mkL (NL n) (ones (4 * j) Y)⟩, ?_, ?_⟩ + · have := gate_mult j 0 (NL n) Y + rwa [Nat.add_zero, ones_zero] at this + · rw [NL, mkL_cons, sM0_0] + +/-- Residue `2`: `M2` reads the separator and steps right into `Bs`. -/ +theorem gate_two (n j : ℕ) (Y : ListBlank (Fin 2)) : + nSteps brocard28 (4 * j + 3) ⟨23, mkL (ones (4 * j + 2) (NL n)) Y⟩ = + some ⟨27, Tape.mk' (NL n) (ones (4 * j + 2) Y)⟩ := by + have h₁ := gate_mult j 2 (NL n) Y + have h₂ : nSteps brocard28 3 ⟨23, mkL (ones 2 (NL n)) (ones (4 * j) Y)⟩ = + some ⟨27, Tape.mk' (NL n) (ones (4 * j + 2) Y)⟩ := by + change nSteps brocard28 3 ⟨23, mkL ((((ones n B0).cons 0).cons 1).cons 1) (ones (4 * j) Y)⟩ = + some ⟨27, Tape.mk' ((ones n B0).cons 0) (((ones (4 * j) Y).cons 1).cons 1)⟩ + simp only [mkL_cons, nSteps_succ, sM0_1, nSteps_zero, Option.bind_some, sM1_1, sM2_0, + mk'_cons] + exact nSteps_bind_eq h₁ h₂ + +/-- Residue `3`: `M3` reads the separator and steps right into `Bs`. -/ +theorem gate_three (n j : ℕ) (Y : ListBlank (Fin 2)) : + nSteps brocard28 (4 * j + 4) ⟨23, mkL (ones (4 * j + 3) (NL n)) Y⟩ = + some ⟨27, Tape.mk' (NL n) (ones (4 * j + 3) Y)⟩ := by + have h₁ := gate_mult j 3 (NL n) Y + have h₂ : nSteps brocard28 4 ⟨23, mkL (ones 3 (NL n)) (ones (4 * j) Y)⟩ = + some ⟨27, Tape.mk' (NL n) (ones (4 * j + 3) Y)⟩ := by + change nSteps brocard28 4 + ⟨23, mkL (((((ones n B0).cons 0).cons 1).cons 1).cons 1) (ones (4 * j) Y)⟩ = + some ⟨27, Tape.mk' ((ones n B0).cons 0) ((((ones (4 * j) Y).cons 1).cons 1).cons 1)⟩ + simp only [mkL_cons, nSteps_succ, sM0_1, nSteps_zero, Option.bind_some, sM1_1, sM2_1, sM3_0, + mk'_cons] + exact nSteps_bind_eq h₁ h₂ + +/-- After a suppressed success, `Bs` returns over `B` and fills its hole, `ER`/`ER2` erase the +solid `S`, and `ER3` erases the last cell of `B`: the next round. -/ +theorem gate_back (n a S : ℕ) : + ∃ s, nSteps brocard28 s + ⟨27, Tape.mk' (NL n) (ones (a + 1) (((ones S B0).cons 0).cons 0))⟩ = + some (roundCfg n a) := by + set R := ((ones S B0).cons 0).cons 0 with hR + have h₃ := sweepR sBs_1 (a + 1) (NL n) R + have h₄ : nSteps brocard28 2 ⟨27, Tape.mk' (ones (a + 1) (NL n)) R⟩ = + some ⟨21, Tape.mk' (((ones (a + 1) (NL n)).cons 1).cons 0) (ones S B0)⟩ := by + rw [hR] + simp only [mk'_cons, nSteps_succ, sBs_0, nSteps_zero, Option.bind_some, sER_0] + set W := ((ones (a + 1) (NL n)).cons 1).cons 0 with hW + have h₅ := sweepR_erase sER2_1 S W B0 + have h₆ : step brocard28 ⟨21, Tape.mk' (zeros S W) B0⟩ = some ⟨22, mkL (zeros S W) B0⟩ := by + rw [mkRight_blank, sER2_0, B0_cons_zero] + have h₇ := sweepL_zeros sER3_0 S W B0 + rw [zeros_B0] at h₇ + have h₈ : nSteps brocard28 2 ⟨22, mkL W B0⟩ = some (roundCfg n a) := by + rw [hW] + change nSteps brocard28 2 ⟨22, mkL ((((ones a (NL n)).cons 1).cons 1).cons 0) B0⟩ = + some ⟨2, Tape.mk' (ones a (NL n)) (B0.cons 1)⟩ + simp only [mkL_cons, nSteps_succ, sER3_0, B0_cons_zero, nSteps_zero, Option.bind_some, sER3_1, + mk'_cons] + exact ⟨_, nSteps_bind_eq h₃ (nSteps_bind_eq h₄ (nSteps_bind_eq h₅ (step_nSteps h₆ + (nSteps_bind_eq h₇ h₈))))⟩ + +/-- A success whose `B` has `a + 1 ≡ 2` or `3 (mod 4)` ones is suppressed: the next round +starts. -/ +theorem gate_suppressed (n a S : ℕ) (h : (a + 1) % 4 = 2 ∨ (a + 1) % 4 = 3) : + ∃ s, nSteps brocard28 s (succCfg n (a + 1) S) = some (roundCfg n a) := by + obtain ⟨s₃, hs₃⟩ := gate_back n a S + rcases h with h | h + · have h₂ := gate_two n ((a + 1) / 4) (((ones S B0).cons 0).cons 0) + rw [show 4 * ((a + 1) / 4) + 2 = a + 1 by omega] at h₂ + exact ⟨_, nSteps_bind_eq h₂ hs₃⟩ + · have h₂ := gate_three n ((a + 1) / 4) (((ones S B0).cons 0).cons 0) + rw [show 4 * ((a + 1) / 4) + 3 = a + 1 by omega] at h₂ + exact ⟨_, nSteps_bind_eq h₂ hs₃⟩ + +/-- A success whose `B` has `a + 1 ≡ 0 (mod 4)` ones halts. -/ +theorem gate_live (n a S : ℕ) (h : (a + 1) % 4 = 0) : + ∃ s cfg, nSteps brocard28 s (succCfg n (a + 1) S) = some cfg ∧ + step brocard28 cfg = none := by + obtain ⟨cfg, h₁, hnone⟩ := gate_halt n ((a + 1) / 4) (((ones S B0).cons 0).cons 0) + rw [show 4 * ((a + 1) / 4) = a + 1 by omega] at h₁ + exact ⟨_, cfg, h₁, hnone⟩ + +theorem init_eq : (init : Cfg 28 2) = ⟨0, Tape.mk' B0 B0⟩ := rfl + +/-- Start, steps `1`–`12`: `GO`, `EC`, `LN0`, `LN1`, `EN`, `X` write `1 0 1 1`; `TLs`, `TLx`, +`TR0` turn it into `1 0 0 1` and move on. -/ +theorem seed₁ : nSteps brocard28 12 ⟨0, Tape.mk' B0 B0⟩ = + some ⟨14, Tape.mk' (((((B0.cons 1).cons 0).cons 0).cons 1).cons 0) B0⟩ := by + rfl + +/-- Start, steps `13`–`24`: one chunk of the test runs on `1 0 0 1`. -/ +theorem seed₂ : nSteps brocard28 12 + ⟨14, Tape.mk' (((((B0.cons 1).cons 0).cons 0).cons 1).cons 0) B0⟩ = + some ⟨14, Tape.mk' (((((B0.cons 1).cons 0).cons 1).cons 0).cons 0) ((B0.cons 1).cons 0)⟩ := by + rfl + +/-- Start, steps `25`–`36`: `TR1`, `SB`, `SC` end the chunk as a success; `M0`, `M1` reading `0`, +`GO`, `LN1`, `EN`, `X` write `1111 0 111`; `TLs` reading `0` enters `TLx`. -/ +theorem seed₃ : nSteps brocard28 12 + ⟨14, Tape.mk' (((((B0.cons 1).cons 0).cons 1).cons 0).cons 0) ((B0.cons 1).cons 0)⟩ = + some ⟨1, Tape.mk' (ListBlank.cons 0 + ((((((((B0.cons 1).cons 1).cons 1).cons 1).cons 0).cons 1).cons 1).cons 1)) B0⟩ := by + rfl + +/-- Start, steps `37`–`46`: `TLx` reading `0`, `EC`, `LN0`, `LN1` walk left to the blank left of +`N`. -/ +theorem seed₄ : nSteps brocard28 10 + ⟨1, Tape.mk' (ListBlank.cons 0 + ((((((((B0.cons 1).cons 1).cons 1).cons 1).cons 0).cons 1).cons 1).cons 1)) B0⟩ = + some ⟨3, mkL B0 (ones 4 ((ones 3 B0).cons 0))⟩ := by + -- The computed right half includes two trailing blanks. + have run : nSteps brocard28 10 + ⟨1, Tape.mk' (ListBlank.mk [0, 1, 1, 1, 0, 1, 1, 1, 1]) B0⟩ = + some ⟨3, ⟨0, B0, ListBlank.mk ([1, 1, 1, 1, 0, 1, 1, 1] ++ List.replicate 2 0)⟩⟩ := rfl + change nSteps brocard28 10 ⟨1, Tape.mk' (ListBlank.mk [0, 1, 1, 1, 0, 1, 1, 1, 1]) B0⟩ = _ + rw [run, Goldbach21.mk_append_replicate_zero] + rfl + +/-- The start writes `1111 0 111` in `46` steps and ends in `LN1` on the blank left of `N`, the +configuration every round passes through (`ln_exit`). -/ +theorem seed : nSteps brocard28 46 init = some ⟨3, mkL B0 (ones 4 ((ones 3 B0).cons 0))⟩ := by + rw [init_eq] + exact nSteps_bind_eq seed₁ (nSteps_bind_eq seed₂ (nSteps_bind_eq seed₃ seed₄)) + +/-! ## Stages -/ + +/-- `V n = n! / 8`, the number of cells of `B` between rounds. -/ +def V (n : ℕ) : ℕ := n.factorial / 8 + +theorem eight_dvd_factorial {n : ℕ} (hn : 4 ≤ n) : 8 ∣ n.factorial := + Nat.dvd_trans (by decide : 8 ∣ Nat.factorial 4) (Nat.factorial_dvd_factorial hn) + +theorem V_succ {n : ℕ} (hn : 4 ≤ n) : V (n + 1) = (n + 1) * V n := by + rw [V, V, Nat.factorial_succ, Nat.mul_div_assoc _ (eight_dvd_factorial hn)] + +theorem V_pos {n : ℕ} (hn : 4 ≤ n) : 1 ≤ V n := by + induction n, hn using Nat.le_induction with + | base => decide + | succ n hn ih => rw [V_succ hn]; exact le_trans ih (Nat.le_mul_of_pos_left _ (by omega)) + +/-- For `n ≥ 8`, `32 ∣ n!`, so `4 ∣ n! / 8`: the gate halts on every success with `n ≥ 8`. -/ +theorem V_mod_four {n : ℕ} (hn : 8 ≤ n) : V n % 4 = 0 := by + obtain ⟨t, ht⟩ := + Nat.dvd_trans (by decide : 32 ∣ Nat.factorial 8) (Nat.factorial_dvd_factorial hn) + rw [V, ht]; omega + +/-- `x` is a triangular number `1 + 2 + ⋯ + j`. -/ +def IsTri (x : ℕ) : Prop := ∃ j, x = tri j + +theorem le_tri (j : ℕ) : j ≤ tri j := by + induction j with + | zero => exact le_rfl + | succ j ih => rw [tri_succ]; omega + +theorem isTri_iff (x : ℕ) : IsTri x ↔ ∃ j < x + 1, x = tri j := + ⟨fun ⟨j, h⟩ => ⟨j, by have := le_tri j; omega, h⟩, fun ⟨j, _, h⟩ => ⟨j, h⟩⟩ + +instance : DecidablePred IsTri := fun x => decidable_of_iff _ (isTri_iff x).symm + +/-- The configuration at the start of the round that multiplies by `n + 1`. -/ +def stage (n : ℕ) : Cfg 28 2 := roundCfg n (V n - 1) + +theorem stage_to_test {n : ℕ} (hn : 4 ≤ n) : + ∃ s, 1 ≤ s ∧ nSteps brocard28 s (stage n) = some (testCfg (n + 1) (V (n + 1) + 1)) := by + obtain ⟨s, hs1, hs⟩ := multiply n (V n - 1) (by omega) + have := V_pos hn + rw [show V n - 1 + 1 = V n by omega, ← V_succ hn] at hs + exact ⟨s, hs1, hs⟩ + +/-- A round whose test fails. -/ +theorem stage_fail {n : ℕ} (hn : 4 ≤ n) (h : ¬ IsTri (V (n + 1))) : + ∃ s, 1 ≤ s ∧ nSteps brocard28 s (stage n) = some (stage (n + 1)) := by + obtain ⟨s₁, hs₁1, hs₁⟩ := stage_to_test hn + rcases test_run (n + 1) (V (n + 1)) with ⟨j, hj, -⟩ | ⟨-, s₂, hs₂⟩ + · exact absurd ⟨j, hj⟩ h + · exact ⟨s₁ + s₂, by omega, nSteps_bind_eq hs₁ hs₂⟩ + +/-- A round whose test succeeds with `n + 1 = 5` (`V 5 = 15 ≡ 3`) or `n + 1 = 7` +(`V 7 = 630 ≡ 2 (mod 4)`): suppressed. -/ +theorem stage_suppressed {n : ℕ} (hn : 4 ≤ n) (h : IsTri (V (n + 1))) + (h57 : n + 1 = 5 ∨ n + 1 = 7) : + ∃ s, 1 ≤ s ∧ nSteps brocard28 s (stage n) = some (stage (n + 1)) := by + obtain ⟨s₁, hs₁1, hs₁⟩ := stage_to_test hn + rcases test_run (n + 1) (V (n + 1)) with ⟨j, -, s₂, hs₂⟩ | ⟨h', -⟩ + · have hV := V_pos (show 4 ≤ n + 1 by omega) + obtain ⟨a, ha⟩ : ∃ a, V (n + 1) = a + 1 := ⟨V (n + 1) - 1, by omega⟩ + have hres : (a + 1) % 4 = 2 ∨ (a + 1) % 4 = 3 := by + rw [← ha] + rcases h57 with h5 | h7 + · right; rw [h5]; decide + · left; rw [h7]; decide + rw [ha] at hs₁ hs₂ + obtain ⟨s₃, hs₃⟩ := gate_suppressed (n + 1) a (j + 1) hres + refine ⟨s₁ + s₂ + s₃, by omega, ?_⟩ + change nSteps brocard28 (s₁ + s₂ + s₃) (stage n) = some (roundCfg (n + 1) (V (n + 1) - 1)) + rw [ha, Nat.add_sub_cancel] + exact nSteps_bind_eq (nSteps_bind_eq hs₁ hs₂) hs₃ + · exact absurd h h' + +/-- A round whose test succeeds with `n + 1 ≥ 8` halts. -/ +theorem stage_halt {n : ℕ} (hn : 7 ≤ n) (h : IsTri (V (n + 1))) : + ∃ s cfg, nSteps brocard28 s (stage n) = some cfg ∧ step brocard28 cfg = none := by + obtain ⟨s₁, -, hs₁⟩ := stage_to_test (show 4 ≤ n by omega) + rcases test_run (n + 1) (V (n + 1)) with ⟨j, -, s₂, hs₂⟩ | ⟨h', -⟩ + · have hV := V_pos (show 4 ≤ n + 1 by omega) + obtain ⟨a, ha⟩ : ∃ a, V (n + 1) = a + 1 := ⟨V (n + 1) - 1, by omega⟩ + have hres : (a + 1) % 4 = 0 := by rw [← ha]; exact V_mod_four (by omega) + rw [ha] at hs₁ hs₂ + obtain ⟨s₃, cfg, hs₃, hnone⟩ := gate_live (n + 1) a (j + 1) hres + exact ⟨s₁ + s₂ + s₃, cfg, nSteps_bind_eq (nSteps_bind_eq hs₁ hs₂) hs₃, hnone⟩ + · exact absurd h h' + +theorem isTri_V5 : IsTri (V 5) := ⟨5, by decide⟩ + +theorem not_isTri_V6 : ¬ IsTri (V 6) := + not_tri_between (k := 12) (by decide) (by decide) + +theorem isTri_V7 : IsTri (V 7) := ⟨35, by decide⟩ + +/-- The start reaches the beginning of round `5`: `46` steps write `1111 0 111`, the first +round multiplies by `5`, its test finds `15 = tri 5`, and the gate suppresses the success +(`15 ≡ 3 (mod 4)`). -/ +theorem seed_stage : ∃ s, 5 ≤ s ∧ nSteps brocard28 s init = some (stage 5) := by + obtain ⟨s₃, hs₃⟩ := multiply_en 4 2 (by omega) + rcases test_run 5 15 with ⟨j, -, s₄, hs₄⟩ | ⟨h, -⟩ + · obtain ⟨s₅, hs₅⟩ := gate_suppressed 5 14 (j + 1) (Or.inr rfl) + refine ⟨46 + 1 + s₃ + s₄ + s₅, by omega, ?_⟩ + have hV : V 5 - 1 = 14 := by decide + rw [stage, hV] + exact nSteps_bind_eq (nSteps_bind_eq (nSteps_bind_eq (nSteps_bind_eq seed + (step_one (ln_exit 4 2))) hs₃) hs₄) hs₅ + · exact absurd isTri_V5 h + +/-- No counterexample with `8 ≤ m ≤ K`. -/ +def Clear (K : ℕ) : Prop := ∀ m, 8 ≤ m → m ≤ K → ¬ IsTri (V m) + +theorem clear_or_witness (K : ℕ) : Clear K ∨ ∃ m, 8 ≤ m ∧ m ≤ K ∧ IsTri (V m) := by + induction K with + | zero => exact Or.inl fun m h8 h0 => absurd (le_trans h8 h0) (by decide) + | succ K ih => + rcases ih with hc | ⟨m, hm8, hmK, hm⟩ + · by_cases hK : 8 ≤ K + 1 + · by_cases ht : IsTri (V (K + 1)) + · exact Or.inr ⟨K + 1, hK, le_rfl, ht⟩ + · refine Or.inl fun m h8 hK1 => ?_ + rcases Nat.lt_or_ge m (K + 1) with h | h + · exact hc m h8 (by omega) + · rw [show m = K + 1 by omega]; exact ht + · exact Or.inl fun m h8 hK1 => absurd (le_trans h8 hK1) hK + · exact Or.inr ⟨m, hm8, by omega, hm⟩ + +/-- Without a counterexample up to `n`, the machine reaches the start of round `n` in at least +`n` steps. -/ +theorem reach (n : ℕ) (hn : 5 ≤ n) (hc : Clear n) : + ∃ s, n ≤ s ∧ nSteps brocard28 s init = some (stage n) := by + induction n, hn using Nat.le_induction with + | base => exact seed_stage + | succ n hn ih => + obtain ⟨s₁, hs₁n, hs₁⟩ := ih fun m h8 hm => hc m h8 (by omega) + have step : ∃ s, 1 ≤ s ∧ nSteps brocard28 s (stage n) = some (stage (n + 1)) := by + rcases Nat.lt_or_ge (n + 1) 8 with h | h + · obtain rfl | rfl : n = 5 ∨ n = 6 := by omega + · exact stage_fail (by omega) not_isTri_V6 + · exact stage_suppressed (by omega) isTri_V7 (Or.inr rfl) + · exact stage_fail (by omega) (hc (n + 1) h le_rfl) + obtain ⟨s₂, hs₂1, hs₂⟩ := step + exact ⟨s₁ + s₂, by omega, nSteps_bind_eq hs₁ hs₂⟩ + +/-- The first counterexample halts the machine. -/ +theorem halt_of (m : ℕ) (hm : 8 ≤ m) (h : IsTri (V m)) (hc : Clear (m - 1)) : + ∃ s cfg, nSteps brocard28 s init = some cfg ∧ step brocard28 cfg = none := by + obtain ⟨s₁, -, hs₁⟩ := reach (m - 1) (by omega) hc + obtain ⟨s₂, cfg, hs₂, hnone⟩ := stage_halt (n := m - 1) (by omega) + (by rw [show m - 1 + 1 = m by omega]; exact h) + exact ⟨s₁ + s₂, cfg, nSteps_bind_eq hs₁ hs₂, hnone⟩ + +/-- The least counterexample has none below it. -/ +theorem first_witness (n : ℕ) : 8 ≤ n → IsTri (V n) → + ∃ m, 8 ≤ m ∧ IsTri (V m) ∧ Clear (m - 1) := by + induction n using Nat.strong_induction_on with + | _ n ih => + intro h8 hn + rcases clear_or_witness (n - 1) with hc | ⟨m, hm8, hmn, hm⟩ + · exact ⟨n, h8, hn, hc⟩ + · exact ih m (by omega) hm8 hm + +/-! ## Halting Equivalence -/ + +/-- The 28-state machine `brocard28` (`brocard_28`) halts on the blank tape if and only if +`n! / 8` is a triangular number for some `n ≥ 8`. -/ +theorem halts_brocard28_iff_exists_tri : + ConjectureMachines.Halts brocard28 ↔ ∃ n, 8 ≤ n ∧ IsTri (V n) := by + unfold ConjectureMachines.Halts + constructor + · intro hdom + rw [Part.dom_iff_mem] at hdom + obtain ⟨b, hb⟩ := hdom + rw [mem_eval] at hb + obtain ⟨hreach, hnone⟩ := hb + obtain ⟨k, hk⟩ := reaches_imp_nSteps hreach + rcases clear_or_witness (k + 8) with hc | ⟨m, hm8, -, hm⟩ + · obtain ⟨s, hks, hs⟩ := reach (k + 8) (by omega) hc + rw [nSteps_eq_none_of_step_none s hk hnone (by omega)] at hs + cases hs + · exact ⟨m, hm8, hm⟩ + · rintro ⟨n, h8, hn⟩ + obtain ⟨m, hm8, hm, hc⟩ := first_witness n h8 hn + obtain ⟨s, cfg, hs, hnone⟩ := halt_of m hm8 hm hc + rw [Part.dom_iff_mem] + exact ⟨cfg, mem_eval.mpr ⟨nSteps_reaches hs, hnone⟩⟩ + +end ConjectureMachines.Brocard28 diff --git a/LeanPool/CryptidsGoldbach/BrocardSemantics.lean b/LeanPool/CryptidsGoldbach/BrocardSemantics.lean new file mode 100644 index 0000000000..ede08ed6ef --- /dev/null +++ b/LeanPool/CryptidsGoldbach/BrocardSemantics.lean @@ -0,0 +1,84 @@ +/- +Copyright (c) 2026 Google LLC. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Daniel Veljjanoski +-/ + +-- Modified for Lean Pool. + +module + +public import LeanPool.CryptidsGoldbach.Brocard28 +public import Mathlib.Algebra.Group.Even +import Mathlib.Tactic.Ring + +/-! +# Semantics of `brocard_28` + +`Halts brocard28 ↔ ∃ n, 7 < n ∧ IsSquare (n ! + 1)`: the machine's triangular test of +`n! / 8` is equivalent to `n! + 1` being a perfect square, since `n! + 1 = m ^ 2` forces `m` odd, +`m = 2k + 1`, and then `n! / 8 = k (k + 1) / 2 = tri k`. +-/ + +@[expose] public section + +namespace ConjectureMachines + +/-- Brocard's conjecture says that no natural number beyond `7` has square `n! + 1`. -/ +def BrocardConjecture : Prop := ∀ n : ℕ, 7 < n → ¬ IsSquare (n.factorial + 1) + +end ConjectureMachines + +namespace ConjectureMachines.Brocard28 + +/-- `tri k = k * (k + 1) / 2`. -/ +theorem two_mul_tri (k : ℕ) : 2 * tri k = k * (k + 1) := by + induction k with + | zero => rfl + | succ k ih => rw [tri_succ, Nat.mul_add, ih]; ring + +/-- For `n ≥ 4`, `n! + 1` is a square if and only if `n! / 8` is triangular. -/ +theorem isSquare_iff_isTri {n : ℕ} (hn : 4 ≤ n) : IsSquare (n.factorial + 1) ↔ IsTri (V n) := by + obtain ⟨c, hc⟩ := eight_dvd_factorial hn + have hV : V n = c := by rw [V, hc, Nat.mul_div_cancel_left _ (by decide)] + rw [hV, hc] + constructor + · rintro ⟨m, hm⟩ + obtain ⟨k, hk | hk⟩ : ∃ k, m = 2 * k ∨ m = 2 * k + 1 := ⟨m / 2, by omega⟩ + · -- `m` even: `m * m` is even, but `8 c + 1` is odd + exfalso + rw [hk] at hm + have : 8 * c + 1 = 4 * (k * k) := by rw [hm]; ring + omega + · refine ⟨k, ?_⟩ + rw [hk] at hm + have h1 : 8 * c + 1 = 4 * (k * k) + 4 * k + 1 := by rw [hm]; ring + have h2 := two_mul_tri k + rw [Nat.mul_succ] at h2 + omega + · rintro ⟨k, hk⟩ + refine ⟨2 * k + 1, ?_⟩ + have h2 := two_mul_tri k + rw [Nat.mul_succ] at h2 + have h3 : (2 * k + 1) * (2 * k + 1) = 4 * (k * k) + 4 * k + 1 := by ring + omega + +/-- The 28-state machine halts on the blank tape if and only if Brocard's problem has a +solution beyond `n = 4, 5, 7`. -/ +theorem halts_brocard28_iff_exists_square : + ConjectureMachines.Halts brocard28 ↔ ∃ n, 7 < n ∧ IsSquare (n.factorial + 1) := by + rw [halts_brocard28_iff_exists_tri] + constructor + · rintro ⟨n, h8, hn⟩ + exact ⟨n, by omega, (isSquare_iff_isTri (by omega)).mpr hn⟩ + · rintro ⟨n, h7, hn⟩ + exact ⟨n, by omega, (isSquare_iff_isTri (by omega)).mp hn⟩ + +/-- The 28-state machine halts if and only if Brocard's conjecture is false. -/ +theorem halts_brocard28_iff_not_brocard : + ConjectureMachines.Halts brocard28 ↔ ¬ BrocardConjecture := by + rw [halts_brocard28_iff_exists_square] + exact ⟨fun ⟨n, h7, hn⟩ hB => hB n h7 hn, + fun h => by_contra fun hne => h fun n h7 hn => hne ⟨n, h7, hn⟩⟩ + +end ConjectureMachines.Brocard28 diff --git a/LeanPool/CryptidsGoldbach/Imports.lean b/LeanPool/CryptidsGoldbach/Imports.lean index 6b633fec35..d0f3bfba42 100644 --- a/LeanPool/CryptidsGoldbach/Imports.lean +++ b/LeanPool/CryptidsGoldbach/Imports.lean @@ -7,6 +7,8 @@ module -- shake: keep-all --deprecated_module: ignore -- Generated project imports; run `lake exe mk_all`. public import LeanPool.CryptidsGoldbach +public import LeanPool.CryptidsGoldbach.Brocard28 +public import LeanPool.CryptidsGoldbach.BrocardSemantics public import LeanPool.CryptidsGoldbach.Goldbach21 public import LeanPool.CryptidsGoldbach.Machines public import LeanPool.CryptidsGoldbach.Propositions diff --git a/LeanPool/projects/cryptids-goldbach.yaml b/LeanPool/projects/cryptids-goldbach.yaml index 2260a16fe1..1bd748ccfa 100644 --- a/LeanPool/projects/cryptids-goldbach.yaml +++ b/LeanPool/projects/cryptids-goldbach.yaml @@ -1,26 +1,37 @@ slug: cryptids-goldbach -title: A 21-state Turing machine for Goldbach's conjecture +title: Finite-state Turing machines for Goldbach and Brocard summary: A concrete 21-state binary Turing machine halts on the blank tape exactly when Goldbach's conjecture is false. The proof connects it to the earlier 25-state machine, verifies both machines' prime-search semantics, and preserves finite-machine execution, - tape-scanning and reachability infrastructure. This imports only the Goldbach dependency - closure of Cryptids, excluding its Riemann, ZF and Busy Beaver developments. + tape-scanning and reachability infrastructure. A second machine with 28 states + halts on the blank tape exactly when there exists a natural number n > 7 + for which n! + 1 is a square. branch: computability and number theory entry_module: LeanPool.CryptidsGoldbach authors: - Maxence Faldor - Moritz Firsching +- Daniel Veljjanoski source: url: https://github.com/google-deepmind/cryptids/tree/f873434871b2301d5c8c91ba6fa224f92c93c1e1 github_repo: google-deepmind/cryptids commit: f873434871b2301d5c8c91ba6fa224f92c93c1e1 announcement_url: https://leanprover.zulipchat.com/#narrow/channel/579630-Project-announcements/topic/Cryptids/near/632199475 announcement_date: '2026-10-16' + brocard_url: https://github.com/google-deepmind/cryptids/tree/481f4e3e39907304dbcad33e508bf21d80c6d5a6/artifacts/brocard_28 + brocard_commit: 481f4e3e39907304dbcad33e508bf21d80c6d5a6 + brocard_announcement_url: https://github.com/google-deepmind/cryptids/pull/27 + brocard_announcement_date: '2026-11-11' license: Apache-2.0 status: verified -provenance: AI +provenance: mix +provenance_evidence: >- + The Goldbach development was AI-authored. Daniel Veljjanoski designed the + Brocard machine and developed its proof, with assistance from Claude as + documented by upstream. main_declarations: - ConjectureMachines.Goldbach21.Semantics.halts_candidate_iff_not_goldbach +- ConjectureMachines.Brocard28.halts_brocard28_iff_exists_square main_results: - declaration: ConjectureMachines.Goldbach21.Semantics.halts_candidate_iff_not_goldbach informal: The explicit 21-state binary Turing machine halts from the blank tape if and @@ -33,11 +44,18 @@ main_results: - declaration: ConjectureMachines.Goldbach21.Semantics.halts_reference_iff_not_goldbach informal: The 25-state reference machine halts from the blank tape exactly when Goldbach's conjecture is false. +- declaration: ConjectureMachines.Brocard28.halts_brocard28_iff_exists_square + informal: The explicit 28-state binary Turing machine halts on the blank tape + exactly when n! + 1 is a square for some natural number n greater than seven. +- declaration: ConjectureMachines.Brocard28.isSquare_iff_isTri + informal: For every natural number n at least four, n! + 1 is a square + exactly when n! / 8 is a triangular number. tags: - computability - turing-machines - goldbach-conjecture - machine-simulation +- brocard-problem msc: - 03D10 - 11P32 From 74290ba3c862e7e8cbec2ed3ae11b213f501a8b7 Mon Sep 17 00:00:00 2001 From: Vasily Ilin Date: Thu, 26 Nov 2026 11:13:02 +0000 Subject: [PATCH 2/7] Share tape sweeps and expose the Brocard source link --- LeanPool/CryptidsGoldbach.lean | 2 +- LeanPool/CryptidsGoldbach/Brocard28.lean | 36 +++++++---------------- LeanPool/CryptidsGoldbach/Goldbach21.lean | 30 +++++++++++++++---- LeanPool/projects/cryptids-goldbach.yaml | 6 ++-- 4 files changed, 38 insertions(+), 36 deletions(-) diff --git a/LeanPool/CryptidsGoldbach.lean b/LeanPool/CryptidsGoldbach.lean index d49c368907..f801d71c1d 100644 --- a/LeanPool/CryptidsGoldbach.lean +++ b/LeanPool/CryptidsGoldbach.lean @@ -11,7 +11,7 @@ public import LeanPool.CryptidsGoldbach.BrocardSemantics /-! # Finite-state Turing machines for Goldbach and Brocard -Source: url:https://github.com/google-deepmind/cryptids/tree/f873434871b2301d5c8c91ba6fa224f92c93c1e1 +Source: url:https://github.com/google-deepmind/cryptids/tree/481f4e3e39907304dbcad33e508bf21d80c6d5a6 Authors: Maxence Faldor, Moritz Firsching, Daniel Veljjanoski Status: verified Main declarations: `ConjectureMachines.Goldbach21.Semantics.halts_candidate_iff_not_goldbach`, diff --git a/LeanPool/CryptidsGoldbach/Brocard28.lean b/LeanPool/CryptidsGoldbach/Brocard28.lean index fa1e70c51f..f81d4c0d36 100644 --- a/LeanPool/CryptidsGoldbach/Brocard28.lean +++ b/LeanPool/CryptidsGoldbach/Brocard28.lean @@ -69,9 +69,9 @@ open Goldbach21 (nSteps nSteps_zero nSteps_succ nSteps_reaches nSteps_add nSteps local notation "mkL" => Goldbach21.Tape.mkL /-- The blank half of the binary tape. -/ -def B0 : ListBlank (Fin 2) := ListBlank.mk [] +abbrev B0 : ListBlank (Fin 2) := Goldbach21.B0 -theorem B0_cons_zero : B0.cons 0 = B0 := ListBlank.cons_head_tail B0 +theorem B0_cons_zero : B0.cons 0 = B0 := Goldbach21.B0_cons_zero theorem mkRight_blank (L : ListBlank (Fin 2)) : Tape.mk' L B0 = ⟨0, L, B0⟩ := rfl @@ -259,22 +259,14 @@ theorem sBs_1 (L R) : step brocard28 ⟨27, ⟨1, L, R⟩⟩ = some ⟨27, Tape. theorem sweepR {q : Fin 28} (h : ∀ L R, step brocard28 ⟨q, ⟨1, L, R⟩⟩ = some ⟨q, Tape.mk' (L.cons 1) R⟩) (k : ℕ) (L R : ListBlank (Fin 2)) : - nSteps brocard28 k ⟨q, Tape.mk' L (ones k R)⟩ = some ⟨q, Tape.mk' (ones k L) R⟩ := by - induction k generalizing L with - | zero => rfl - | succ k ih => - rw [nSteps_succ, show ones (k + 1) R = (ones k R).cons 1 from rfl, - mk'_cons, h, Option.bind_some, ih, ones_succ'] + nSteps brocard28 k ⟨q, Tape.mk' L (ones k R)⟩ = some ⟨q, Tape.mk' (ones k L) R⟩ := + Goldbach21.nSteps_mk'_right_scan_of_step h k L R theorem sweepL {q : Fin 28} (h : ∀ L R, step brocard28 ⟨q, ⟨1, L, R⟩⟩ = some ⟨q, mkL L (R.cons 1)⟩) (k : ℕ) (L R : ListBlank (Fin 2)) : - nSteps brocard28 k ⟨q, mkL (ones k L) R⟩ = some ⟨q, mkL L (ones k R)⟩ := by - induction k generalizing R with - | zero => rfl - | succ k ih => - rw [nSteps_succ, show ones (k + 1) L = (ones k L).cons 1 from rfl, - mkL_cons, h, Option.bind_some, ih, ones_succ'] + nSteps brocard28 k ⟨q, mkL (ones k L) R⟩ = some ⟨q, mkL L (ones k R)⟩ := + Goldbach21.nSteps_mkL_left_scan_of_step h k L R /-! ## Tape Encoding and Configurations -/ @@ -529,23 +521,15 @@ theorem zeros_B0 (k : ℕ) : zeros k B0 = B0 := by theorem sweepR_erase {q : Fin 28} (h : ∀ L R, step brocard28 ⟨q, ⟨1, L, R⟩⟩ = some ⟨q, Tape.mk' (L.cons 0) R⟩) (k : ℕ) (L R : ListBlank (Fin 2)) : - nSteps brocard28 k ⟨q, Tape.mk' L (ones k R)⟩ = some ⟨q, Tape.mk' (zeros k L) R⟩ := by - induction k generalizing L with - | zero => rfl - | succ k ih => - rw [nSteps_succ, show ones (k + 1) R = (ones k R).cons 1 from rfl, - mk'_cons, h, Option.bind_some, ih, zeros_succ'] + nSteps brocard28 k ⟨q, Tape.mk' L (ones k R)⟩ = some ⟨q, Tape.mk' (zeros k L) R⟩ := + Goldbach21.nSteps_mk'_right_scan_of_step h k L R /-- A state that moves left over zeros. -/ theorem sweepL_zeros {q : Fin 28} (h : ∀ L R, step brocard28 ⟨q, ⟨0, L, R⟩⟩ = some ⟨q, mkL L (R.cons 0)⟩) (k : ℕ) (L R : ListBlank (Fin 2)) : - nSteps brocard28 k ⟨q, mkL (zeros k L) R⟩ = some ⟨q, mkL L (zeros k R)⟩ := by - induction k generalizing R with - | zero => rfl - | succ k ih => - rw [nSteps_succ, show zeros (k + 1) L = (zeros k L).cons 0 from rfl, - mkL_cons, h, Option.bind_some, ih, zeros_succ'] + nSteps brocard28 k ⟨q, mkL (zeros k L) R⟩ = some ⟨q, mkL L (zeros k R)⟩ := + Goldbach21.nSteps_mkL_left_scan_of_step h k L R /-- `TR0` right after the hole of `B`, with `r` cells of `B` still to pass. -/ def tCfg (n a r p q : ℕ) : Cfg 28 2 := diff --git a/LeanPool/CryptidsGoldbach/Goldbach21.lean b/LeanPool/CryptidsGoldbach/Goldbach21.lean index d7416c0145..b6bc27b44e 100644 --- a/LeanPool/CryptidsGoldbach/Goldbach21.lean +++ b/LeanPool/CryptidsGoldbach/Goldbach21.lean @@ -230,28 +230,46 @@ theorem append_replicate_add (m n : ℕ) (b : Fin 2) (L : ListBlank (Fin 2)) : ListBlank.append (List.replicate (m + n) b) L := by rw [← ListBlank.append_assoc, ← List.replicate_add] -theorem nSteps_mkL_left_scan {M : Machine states 2} {q : Fin states} {a b : Fin 2} - (h : M.transition q a = some (b, .left, q)) (k : ℕ) (L R : ListBlank (Fin 2)) : +/-- Scan to the left over a constant block using a uniform tape step. -/ +theorem nSteps_mkL_left_scan_of_step {M : Machine states 2} {q : Fin states} {a b : Fin 2} + (h : ∀ L R, step M ⟨q, ⟨a, L, R⟩⟩ = some ⟨q, Tape.mkL L (R.cons b)⟩) + (k : ℕ) (L R : ListBlank (Fin 2)) : nSteps M k ⟨q, Tape.mkL (ListBlank.append (List.replicate k a) L) R⟩ = some ⟨q, Tape.mkL L (ListBlank.append (List.replicate k b) R)⟩ := by induction k generalizing R with | zero => rfl | succ k ih => - rw [List.replicate_succ, ListBlank.append, nSteps_succ, step_mkL_left h] + rw [List.replicate_succ, ListBlank.append, nSteps_succ, Tape.mkL_cons, h] simp only [Option.bind_some] rw [ih (R.cons b), append_replicate_cons] -theorem nSteps_mk'_right_scan {M : Machine states 2} {q : Fin states} {a b : Fin 2} - (h : M.transition q a = some (b, .right, q)) (k : ℕ) (L R : ListBlank (Fin 2)) : +theorem nSteps_mkL_left_scan {M : Machine states 2} {q : Fin states} {a b : Fin 2} + (h : M.transition q a = some (b, .left, q)) (k : ℕ) (L R : ListBlank (Fin 2)) : + nSteps M k ⟨q, Tape.mkL (ListBlank.append (List.replicate k a) L) R⟩ = + some ⟨q, Tape.mkL L (ListBlank.append (List.replicate k b) R)⟩ := + nSteps_mkL_left_scan_of_step (fun l r ↦ by simpa only [Tape.mkL_cons] using step_mkL_left h l r) + k L R + +/-- Scan to the right over a constant block using a uniform tape step. -/ +theorem nSteps_mk'_right_scan_of_step {M : Machine states 2} {q : Fin states} {a b : Fin 2} + (h : ∀ L R, step M ⟨q, ⟨a, L, R⟩⟩ = some ⟨q, Tape.mk' (L.cons b) R⟩) + (k : ℕ) (L R : ListBlank (Fin 2)) : nSteps M k ⟨q, Tape.mk' L (ListBlank.append (List.replicate k a) R)⟩ = some ⟨q, Tape.mk' (ListBlank.append (List.replicate k b) L) R⟩ := by induction k generalizing L with | zero => rfl | succ k ih => - rw [List.replicate_succ, ListBlank.append, nSteps_succ, step_mk'_right h] + rw [List.replicate_succ, ListBlank.append, nSteps_succ, Tape.mk'_cons, h] simp only [Option.bind_some] rw [ih (L.cons b), append_replicate_cons] +theorem nSteps_mk'_right_scan {M : Machine states 2} {q : Fin states} {a b : Fin 2} + (h : M.transition q a = some (b, .right, q)) (k : ℕ) (L R : ListBlank (Fin 2)) : + nSteps M k ⟨q, Tape.mk' L (ListBlank.append (List.replicate k a) R)⟩ = + some ⟨q, Tape.mk' (ListBlank.append (List.replicate k b) L) R⟩ := + nSteps_mk'_right_scan_of_step (fun l r ↦ by simpa only [Tape.mk'_cons] using step_mk'_right h l r) + k L R + /-- The all-blank one-sided binary tape. -/ def B0 : ListBlank (Fin 2) := ListBlank.mk [] diff --git a/LeanPool/projects/cryptids-goldbach.yaml b/LeanPool/projects/cryptids-goldbach.yaml index 1bd748ccfa..0739795c6a 100644 --- a/LeanPool/projects/cryptids-goldbach.yaml +++ b/LeanPool/projects/cryptids-goldbach.yaml @@ -13,13 +13,13 @@ authors: - Moritz Firsching - Daniel Veljjanoski source: - url: https://github.com/google-deepmind/cryptids/tree/f873434871b2301d5c8c91ba6fa224f92c93c1e1 + url: https://github.com/google-deepmind/cryptids/tree/481f4e3e39907304dbcad33e508bf21d80c6d5a6 github_repo: google-deepmind/cryptids - commit: f873434871b2301d5c8c91ba6fa224f92c93c1e1 + commit: 481f4e3e39907304dbcad33e508bf21d80c6d5a6 + goldbach_commit: f873434871b2301d5c8c91ba6fa224f92c93c1e1 announcement_url: https://leanprover.zulipchat.com/#narrow/channel/579630-Project-announcements/topic/Cryptids/near/632199475 announcement_date: '2026-10-16' brocard_url: https://github.com/google-deepmind/cryptids/tree/481f4e3e39907304dbcad33e508bf21d80c6d5a6/artifacts/brocard_28 - brocard_commit: 481f4e3e39907304dbcad33e508bf21d80c6d5a6 brocard_announcement_url: https://github.com/google-deepmind/cryptids/pull/27 brocard_announcement_date: '2026-11-11' license: Apache-2.0 From af41592083298bd3ff9c40827ac296ffdc50e9e7 Mon Sep 17 00:00:00 2001 From: Vasily Ilin Date: Thu, 26 Nov 2026 11:49:40 +0000 Subject: [PATCH 3/7] Separate binary machine support from Goldbach proofs --- LeanPool/CryptidsGoldbach/Brocard28.lean | 13 +- LeanPool/CryptidsGoldbach/Goldbach21.lean | 177 +--------------- LeanPool/CryptidsGoldbach/Imports.lean | 1 + LeanPool/CryptidsGoldbach/MachineSupport.lean | 198 ++++++++++++++++++ 4 files changed, 205 insertions(+), 184 deletions(-) create mode 100644 LeanPool/CryptidsGoldbach/MachineSupport.lean diff --git a/LeanPool/CryptidsGoldbach/Brocard28.lean b/LeanPool/CryptidsGoldbach/Brocard28.lean index f81d4c0d36..b5f2996523 100644 --- a/LeanPool/CryptidsGoldbach/Brocard28.lean +++ b/LeanPool/CryptidsGoldbach/Brocard28.lean @@ -8,7 +8,7 @@ Authors: Daniel Veljjanoski module -public import LeanPool.CryptidsGoldbach.Goldbach21 +public import LeanPool.CryptidsGoldbach.MachineSupport public import Mathlib.Data.Nat.Factorial.Basic import Mathlib.Tactic.Ring @@ -898,13 +898,10 @@ theorem seed₄ : nSteps brocard28 10 ⟨1, Tape.mk' (ListBlank.cons 0 ((((((((B0.cons 1).cons 1).cons 1).cons 1).cons 0).cons 1).cons 1).cons 1)) B0⟩ = some ⟨3, mkL B0 (ones 4 ((ones 3 B0).cons 0))⟩ := by - -- The computed right half includes two trailing blanks. - have run : nSteps brocard28 10 - ⟨1, Tape.mk' (ListBlank.mk [0, 1, 1, 1, 0, 1, 1, 1, 1]) B0⟩ = - some ⟨3, ⟨0, B0, ListBlank.mk ([1, 1, 1, 1, 0, 1, 1, 1] ++ List.replicate 2 0)⟩⟩ := rfl - change nSteps brocard28 10 ⟨1, Tape.mk' (ListBlank.mk [0, 1, 1, 1, 0, 1, 1, 1, 1]) B0⟩ = _ - rw [run, Goldbach21.mk_append_replicate_zero] - rfl + change _ = some (⟨3, mkL B0 (ListBlank.cons 1 + (((((((B0.cons 1).cons 1).cons 1).cons 0).cons 1).cons 1).cons 1))⟩ : Cfg 28 2) + simp only [mkRight_blank, nSteps_succ, sTLx_0, B0_cons_zero, mkL_cons, nSteps_zero, + Option.bind_some, sEC_0, sLN0_1, sLN0_0, sLN1_1, mkL_B0] /-- The start writes `1111 0 111` in `46` steps and ends in `LN1` on the blank left of `N`, the configuration every round passes through (`ln_exit`). -/ diff --git a/LeanPool/CryptidsGoldbach/Goldbach21.lean b/LeanPool/CryptidsGoldbach/Goldbach21.lean index b6bc27b44e..3e67e52d34 100644 --- a/LeanPool/CryptidsGoldbach/Goldbach21.lean +++ b/LeanPool/CryptidsGoldbach/Goldbach21.lean @@ -5,7 +5,7 @@ Authors: Maxence Faldor, Moritz Firsching -/ module -public import LeanPool.CryptidsGoldbach.Machines +public import LeanPool.CryptidsGoldbach.MachineSupport /-! # Goldbach21 @@ -101,66 +101,6 @@ theorem candidate_iff_edited : Halts candM ↔ (eval (step edited) ⟨2, default⟩).Dom := (StateTransition.tr_eval_dom respects_embed rfl).symm -/-- Execute exactly `n` transitions, returning `none` if a transition halts early. -/ -def nSteps (M : Machine states 2) (n : ℕ) : Cfg states 2 → Option (Cfg states 2) := - Nat.rec (motive := fun _ => Cfg states 2 → Option (Cfg states 2)) - (fun c => some c) - (fun _ ih c => (step M c).bind ih) - n - -@[simp] -theorem nSteps_zero (M : Machine states 2) (c : Cfg states 2) : - nSteps M 0 c = some c := rfl - -@[simp] -theorem nSteps_succ (M : Machine states 2) (n : ℕ) (c : Cfg states 2) : - nSteps M (n + 1) c = (step M c).bind (nSteps M n) := rfl - -attribute [eqns nSteps_zero nSteps_succ] nSteps - -theorem nSteps_reaches {M : Machine states 2} {n : ℕ} {c c' : Cfg states 2} - (h : nSteps M n c = some c') : Reaches (step M) c c' := by - induction n generalizing c with - | zero => - injection h with h' - subst h' - exact Relation.ReflTransGen.refl - | succ n ih => - simp only [nSteps, Option.bind_eq_some_iff] at h - obtain ⟨c₁, hstep, hrest⟩ := h - exact Relation.ReflTransGen.head hstep (ih hrest) - -theorem nSteps_eval_dom {M : Machine states 2} {n : ℕ} {c c' : Cfg states 2} - (h : nSteps M n c = some c') : - (eval (step M) c).Dom ↔ (eval (step M) c').Dom := by - rw [StateTransition.reaches_eval (nSteps_reaches h)] - -theorem nSteps_add {M : Machine states 2} (m n : ℕ) (c : Cfg states 2) : - nSteps M (m + n) c = (nSteps M m c).bind (nSteps M n) := by - induction m generalizing c with - | zero => simp [nSteps] - | succ m ih => - rw [Nat.succ_add, nSteps, nSteps] - cases step M c <;> simp [ih] - -theorem nSteps_bind_eq {M : Machine states 2} {m n : ℕ} {c c₁ c₂ : Cfg states 2} - (h₁ : nSteps M m c = some c₁) (h₂ : nSteps M n c₁ = some c₂) : - nSteps M (m + n) c = some c₂ := by - rw [nSteps_add, h₁, Option.bind_some, h₂] - -theorem step_nSteps {M : Machine states 2} {n : ℕ} {c c₁ c₂ : Cfg states 2} - (h₁ : step M c = some c₁) (h₂ : nSteps M n c₁ = some c₂) : - nSteps M (n + 1) c = some c₂ := by - rw [nSteps, h₁, Option.bind_some, h₂] - -theorem step_one {M : Machine states 2} {c c₁ : Cfg states 2} (h : step M c = some c₁) : - nSteps M 1 c = some c₁ := by - rw [nSteps, h]; rfl - -theorem mk_append_replicate_zero (l : List (Fin 2)) (n : ℕ) : - ListBlank.mk (l ++ List.replicate n 0) = ListBlank.mk l := - Quotient.sound' (Or.inr ⟨n, rfl⟩) - theorem boot_reference_47 : nSteps refM 47 init = some ⟨19, ⟨1, ListBlank.mk [1, 1], ListBlank.mk [0, 1]⟩⟩ := by @@ -176,107 +116,6 @@ theorem boot_edited_125 : ListBlank.mk ([1, 1, 0, 1, 1, 1] ++ List.replicate 1 0)⟩⟩ := rfl rw [h, mk_append_replicate_zero, mk_append_replicate_zero] -/-- A tape whose head is the first cell of the left list, facing the right list. -/ -def Tape.mkL (L R : ListBlank (Fin 2)) : Tape (Fin 2) := - ⟨L.head, L.tail, R⟩ - -@[simp] -theorem Tape.mkL_cons (a : Fin 2) (L R : ListBlank (Fin 2)) : - Tape.mkL (L.cons a) R = ⟨a, L, R⟩ := by - simp [Tape.mkL] - -@[simp] -theorem Tape.mk'_cons (a : Fin 2) (L R : ListBlank (Fin 2)) : - Tape.mk' L (R.cons a) = ⟨a, L, R⟩ := by - simp [Tape.mk'] - -theorem step_right {M : Machine states 2} {q q' : Fin states} {a b : Fin 2} - (h : M.transition q a = some (b, .right, q')) (L R : ListBlank (Fin 2)) : - step M ⟨q, ⟨a, L, R⟩⟩ = some ⟨q', ⟨R.head, L.cons b, R.tail⟩⟩ := by - simp [step, h, Tape.write, Tape.move] - -theorem step_right_cons {M : Machine states 2} {q q' : Fin states} {a b c : Fin 2} - (h : M.transition q a = some (b, .right, q')) (L R : ListBlank (Fin 2)) : - step M ⟨q, ⟨a, L, R.cons c⟩⟩ = some ⟨q', ⟨c, L.cons b, R⟩⟩ := by - simp [step, h, Tape.write, Tape.move] - -theorem step_mkL_left {M : Machine states 2} {q q' : Fin states} {a b : Fin 2} - (h : M.transition q a = some (b, .left, q')) (L R : ListBlank (Fin 2)) : - step M ⟨q, Tape.mkL (L.cons a) R⟩ = some ⟨q', Tape.mkL L (R.cons b)⟩ := by - simp [step, h, Tape.mkL, Tape.write, Tape.move] - -theorem step_mkL_right {M : Machine states 2} {q q' : Fin states} {a b : Fin 2} - (h : M.transition q a = some (b, .right, q')) (L R : ListBlank (Fin 2)) : - step M ⟨q, Tape.mkL (L.cons a) R⟩ = some ⟨q', Tape.mk' (L.cons b) R⟩ := by - simp [step, h, Tape.mkL, Tape.mk', Tape.write, Tape.move] - -theorem step_mk'_right {M : Machine states 2} {q q' : Fin states} {a b : Fin 2} - (h : M.transition q a = some (b, .right, q')) (L R : ListBlank (Fin 2)) : - step M ⟨q, Tape.mk' L (R.cons a)⟩ = some ⟨q', Tape.mk' (L.cons b) R⟩ := by - simp [step, h, Tape.mk', Tape.write, Tape.move] - -theorem step_mk'_left {M : Machine states 2} {q q' : Fin states} {a b : Fin 2} - (h : M.transition q a = some (b, .left, q')) (L R : ListBlank (Fin 2)) : - step M ⟨q, Tape.mk' L (R.cons a)⟩ = some ⟨q', Tape.mkL L (R.cons b)⟩ := by - simp [step, h, Tape.mkL, Tape.mk', Tape.write, Tape.move] - -theorem append_replicate_cons (k : ℕ) (b : Fin 2) (L : ListBlank (Fin 2)) : - ListBlank.append (List.replicate k b) (L.cons b) = - ListBlank.append (List.replicate (k + 1) b) L := by - rw [List.replicate_succ', ListBlank.append_assoc, ListBlank.append, ListBlank.append] - -theorem append_replicate_add (m n : ℕ) (b : Fin 2) (L : ListBlank (Fin 2)) : - ListBlank.append (List.replicate m b) (ListBlank.append (List.replicate n b) L) = - ListBlank.append (List.replicate (m + n) b) L := by - rw [← ListBlank.append_assoc, ← List.replicate_add] - -/-- Scan to the left over a constant block using a uniform tape step. -/ -theorem nSteps_mkL_left_scan_of_step {M : Machine states 2} {q : Fin states} {a b : Fin 2} - (h : ∀ L R, step M ⟨q, ⟨a, L, R⟩⟩ = some ⟨q, Tape.mkL L (R.cons b)⟩) - (k : ℕ) (L R : ListBlank (Fin 2)) : - nSteps M k ⟨q, Tape.mkL (ListBlank.append (List.replicate k a) L) R⟩ = - some ⟨q, Tape.mkL L (ListBlank.append (List.replicate k b) R)⟩ := by - induction k generalizing R with - | zero => rfl - | succ k ih => - rw [List.replicate_succ, ListBlank.append, nSteps_succ, Tape.mkL_cons, h] - simp only [Option.bind_some] - rw [ih (R.cons b), append_replicate_cons] - -theorem nSteps_mkL_left_scan {M : Machine states 2} {q : Fin states} {a b : Fin 2} - (h : M.transition q a = some (b, .left, q)) (k : ℕ) (L R : ListBlank (Fin 2)) : - nSteps M k ⟨q, Tape.mkL (ListBlank.append (List.replicate k a) L) R⟩ = - some ⟨q, Tape.mkL L (ListBlank.append (List.replicate k b) R)⟩ := - nSteps_mkL_left_scan_of_step (fun l r ↦ by simpa only [Tape.mkL_cons] using step_mkL_left h l r) - k L R - -/-- Scan to the right over a constant block using a uniform tape step. -/ -theorem nSteps_mk'_right_scan_of_step {M : Machine states 2} {q : Fin states} {a b : Fin 2} - (h : ∀ L R, step M ⟨q, ⟨a, L, R⟩⟩ = some ⟨q, Tape.mk' (L.cons b) R⟩) - (k : ℕ) (L R : ListBlank (Fin 2)) : - nSteps M k ⟨q, Tape.mk' L (ListBlank.append (List.replicate k a) R)⟩ = - some ⟨q, Tape.mk' (ListBlank.append (List.replicate k b) L) R⟩ := by - induction k generalizing L with - | zero => rfl - | succ k ih => - rw [List.replicate_succ, ListBlank.append, nSteps_succ, Tape.mk'_cons, h] - simp only [Option.bind_some] - rw [ih (L.cons b), append_replicate_cons] - -theorem nSteps_mk'_right_scan {M : Machine states 2} {q : Fin states} {a b : Fin 2} - (h : M.transition q a = some (b, .right, q)) (k : ℕ) (L R : ListBlank (Fin 2)) : - nSteps M k ⟨q, Tape.mk' L (ListBlank.append (List.replicate k a) R)⟩ = - some ⟨q, Tape.mk' (ListBlank.append (List.replicate k b) L) R⟩ := - nSteps_mk'_right_scan_of_step (fun l r ↦ by simpa only [Tape.mk'_cons] using step_mk'_right h l r) - k L R - -/-- The all-blank one-sided binary tape. -/ -def B0 : ListBlank (Fin 2) := ListBlank.mk [] - -@[simp] -theorem B0_cons_zero : B0.cons 0 = B0 := - mk_append_replicate_zero [] 1 - /-- Two unary counters, of lengths `B` and `A`, separated by zero cells. -/ def counterTape (A B : ℕ) (L_base : ListBlank (Fin 2)) : ListBlank (Fin 2) := ListBlank.append (List.replicate B 1) @@ -1223,20 +1062,6 @@ theorem stage_halt (w : ℕ) (h : ¬ HasGoldbachPair (2 * w + 4)) : intro q' hq' ⟨hq1, hp1⟩ exact h ⟨q' + 1, 2 * w + 3 - q', hq1, hp1, by omega⟩ -theorem reaches_imp_nSteps {M : Machine states 2} {a b : Cfg states 2} - (h : Reaches (step M) a b) : ∃ k, nSteps M k a = some b := by - induction h with - | refl => exact ⟨0, rfl⟩ - | tail _ hstep ih => - obtain ⟨k, hk⟩ := ih - exact ⟨k + 1, nSteps_bind_eq hk (step_one hstep)⟩ - -theorem nSteps_eq_none_of_step_none {M : Machine states 2} {k : ℕ} (s : ℕ) {a b : Cfg states 2} - (hk : nSteps M k a = some b) (hnone : step M b = none) (hks : k < s) : - nSteps M s a = none := by - have hs : s = k + ((s - k - 1) + 1) := by omega - rw [hs, nSteps_add, hk, Option.bind_some, nSteps_succ, hnone, Option.bind_none] - theorem stage_iter_all (hall : ∀ w, HasGoldbachPair (2 * w + 4)) (N : ℕ) : ∃ s, N ≤ s ∧ nSteps refM s (stageCfg 0) = some (stageCfg N) := by induction N with diff --git a/LeanPool/CryptidsGoldbach/Imports.lean b/LeanPool/CryptidsGoldbach/Imports.lean index d0f3bfba42..b4a860dace 100644 --- a/LeanPool/CryptidsGoldbach/Imports.lean +++ b/LeanPool/CryptidsGoldbach/Imports.lean @@ -10,6 +10,7 @@ public import LeanPool.CryptidsGoldbach public import LeanPool.CryptidsGoldbach.Brocard28 public import LeanPool.CryptidsGoldbach.BrocardSemantics public import LeanPool.CryptidsGoldbach.Goldbach21 +public import LeanPool.CryptidsGoldbach.MachineSupport public import LeanPool.CryptidsGoldbach.Machines public import LeanPool.CryptidsGoldbach.Propositions public import LeanPool.CryptidsGoldbach.Semantics diff --git a/LeanPool/CryptidsGoldbach/MachineSupport.lean b/LeanPool/CryptidsGoldbach/MachineSupport.lean new file mode 100644 index 0000000000..180c486560 --- /dev/null +++ b/LeanPool/CryptidsGoldbach/MachineSupport.lean @@ -0,0 +1,198 @@ +/- +Copyright (c) 2026 Google LLC. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Maxence Faldor, Moritz Firsching +-/ +module + +public import LeanPool.CryptidsGoldbach.Machines + +/-! # Binary tape scans and finite execution + +The shared definitions and lemmas retain their original `Goldbach21` names. +-/ + +@[expose] public section + +namespace ConjectureMachines.Goldbach21 + +open Turing StateTransition + +variable {states : ℕ} + +/-- Execute exactly `n` transitions, returning `none` if a transition halts early. -/ +def nSteps (M : Machine states 2) (n : ℕ) : Cfg states 2 → Option (Cfg states 2) := + Nat.rec (motive := fun _ => Cfg states 2 → Option (Cfg states 2)) + (fun c => some c) + (fun _ ih c => (step M c).bind ih) + n + +@[simp] +theorem nSteps_zero (M : Machine states 2) (c : Cfg states 2) : + nSteps M 0 c = some c := rfl + +@[simp] +theorem nSteps_succ (M : Machine states 2) (n : ℕ) (c : Cfg states 2) : + nSteps M (n + 1) c = (step M c).bind (nSteps M n) := rfl + +attribute [eqns nSteps_zero nSteps_succ] nSteps + +theorem nSteps_reaches {M : Machine states 2} {n : ℕ} {c c' : Cfg states 2} + (h : nSteps M n c = some c') : Reaches (step M) c c' := by + induction n generalizing c with + | zero => + injection h with h' + subst h' + exact Relation.ReflTransGen.refl + | succ n ih => + simp only [nSteps, Option.bind_eq_some_iff] at h + obtain ⟨c₁, hstep, hrest⟩ := h + exact Relation.ReflTransGen.head hstep (ih hrest) + +theorem nSteps_eval_dom {M : Machine states 2} {n : ℕ} {c c' : Cfg states 2} + (h : nSteps M n c = some c') : + (eval (step M) c).Dom ↔ (eval (step M) c').Dom := by + rw [StateTransition.reaches_eval (nSteps_reaches h)] + +theorem nSteps_add {M : Machine states 2} (m n : ℕ) (c : Cfg states 2) : + nSteps M (m + n) c = (nSteps M m c).bind (nSteps M n) := by + induction m generalizing c with + | zero => simp [nSteps] + | succ m ih => + rw [Nat.succ_add, nSteps, nSteps] + cases step M c <;> simp [ih] + +theorem nSteps_bind_eq {M : Machine states 2} {m n : ℕ} {c c₁ c₂ : Cfg states 2} + (h₁ : nSteps M m c = some c₁) (h₂ : nSteps M n c₁ = some c₂) : + nSteps M (m + n) c = some c₂ := by + rw [nSteps_add, h₁, Option.bind_some, h₂] + +theorem step_nSteps {M : Machine states 2} {n : ℕ} {c c₁ c₂ : Cfg states 2} + (h₁ : step M c = some c₁) (h₂ : nSteps M n c₁ = some c₂) : + nSteps M (n + 1) c = some c₂ := by + rw [nSteps, h₁, Option.bind_some, h₂] + +theorem step_one {M : Machine states 2} {c c₁ : Cfg states 2} (h : step M c = some c₁) : + nSteps M 1 c = some c₁ := by + rw [nSteps, h]; rfl + +theorem mk_append_replicate_zero (l : List (Fin 2)) (n : ℕ) : + ListBlank.mk (l ++ List.replicate n 0) = ListBlank.mk l := + Quotient.sound' (Or.inr ⟨n, rfl⟩) + +/-- A tape whose head is the first cell of the left list, facing the right list. -/ +def Tape.mkL (L R : ListBlank (Fin 2)) : Tape (Fin 2) := + ⟨L.head, L.tail, R⟩ + +@[simp] +theorem Tape.mkL_cons (a : Fin 2) (L R : ListBlank (Fin 2)) : + Tape.mkL (L.cons a) R = ⟨a, L, R⟩ := by + simp [Tape.mkL] + +@[simp] +theorem Tape.mk'_cons (a : Fin 2) (L R : ListBlank (Fin 2)) : + Tape.mk' L (R.cons a) = ⟨a, L, R⟩ := by + simp [Tape.mk'] + +theorem step_right {M : Machine states 2} {q q' : Fin states} {a b : Fin 2} + (h : M.transition q a = some (b, .right, q')) (L R : ListBlank (Fin 2)) : + step M ⟨q, ⟨a, L, R⟩⟩ = some ⟨q', ⟨R.head, L.cons b, R.tail⟩⟩ := by + simp [step, h, Tape.write, Tape.move] + +theorem step_right_cons {M : Machine states 2} {q q' : Fin states} {a b c : Fin 2} + (h : M.transition q a = some (b, .right, q')) (L R : ListBlank (Fin 2)) : + step M ⟨q, ⟨a, L, R.cons c⟩⟩ = some ⟨q', ⟨c, L.cons b, R⟩⟩ := by + simp [step, h, Tape.write, Tape.move] + +theorem step_mkL_left {M : Machine states 2} {q q' : Fin states} {a b : Fin 2} + (h : M.transition q a = some (b, .left, q')) (L R : ListBlank (Fin 2)) : + step M ⟨q, Tape.mkL (L.cons a) R⟩ = some ⟨q', Tape.mkL L (R.cons b)⟩ := by + simp [step, h, Tape.mkL, Tape.write, Tape.move] + +theorem step_mkL_right {M : Machine states 2} {q q' : Fin states} {a b : Fin 2} + (h : M.transition q a = some (b, .right, q')) (L R : ListBlank (Fin 2)) : + step M ⟨q, Tape.mkL (L.cons a) R⟩ = some ⟨q', Tape.mk' (L.cons b) R⟩ := by + simp [step, h, Tape.mkL, Tape.mk', Tape.write, Tape.move] + +theorem step_mk'_right {M : Machine states 2} {q q' : Fin states} {a b : Fin 2} + (h : M.transition q a = some (b, .right, q')) (L R : ListBlank (Fin 2)) : + step M ⟨q, Tape.mk' L (R.cons a)⟩ = some ⟨q', Tape.mk' (L.cons b) R⟩ := by + simp [step, h, Tape.mk', Tape.write, Tape.move] + +theorem step_mk'_left {M : Machine states 2} {q q' : Fin states} {a b : Fin 2} + (h : M.transition q a = some (b, .left, q')) (L R : ListBlank (Fin 2)) : + step M ⟨q, Tape.mk' L (R.cons a)⟩ = some ⟨q', Tape.mkL L (R.cons b)⟩ := by + simp [step, h, Tape.mkL, Tape.mk', Tape.write, Tape.move] + +theorem append_replicate_cons (k : ℕ) (b : Fin 2) (L : ListBlank (Fin 2)) : + ListBlank.append (List.replicate k b) (L.cons b) = + ListBlank.append (List.replicate (k + 1) b) L := by + rw [List.replicate_succ', ListBlank.append_assoc, ListBlank.append, ListBlank.append] + +theorem append_replicate_add (m n : ℕ) (b : Fin 2) (L : ListBlank (Fin 2)) : + ListBlank.append (List.replicate m b) (ListBlank.append (List.replicate n b) L) = + ListBlank.append (List.replicate (m + n) b) L := by + rw [← ListBlank.append_assoc, ← List.replicate_add] + +/-- Scan to the left over a constant block using a uniform tape step. -/ +theorem nSteps_mkL_left_scan_of_step {M : Machine states 2} {q : Fin states} {a b : Fin 2} + (h : ∀ L R, step M ⟨q, ⟨a, L, R⟩⟩ = some ⟨q, Tape.mkL L (R.cons b)⟩) + (k : ℕ) (L R : ListBlank (Fin 2)) : + nSteps M k ⟨q, Tape.mkL (ListBlank.append (List.replicate k a) L) R⟩ = + some ⟨q, Tape.mkL L (ListBlank.append (List.replicate k b) R)⟩ := by + induction k generalizing R with + | zero => rfl + | succ k ih => + rw [List.replicate_succ, ListBlank.append, nSteps_succ, Tape.mkL_cons, h] + simp only [Option.bind_some] + rw [ih (R.cons b), append_replicate_cons] + +theorem nSteps_mkL_left_scan {M : Machine states 2} {q : Fin states} {a b : Fin 2} + (h : M.transition q a = some (b, .left, q)) (k : ℕ) (L R : ListBlank (Fin 2)) : + nSteps M k ⟨q, Tape.mkL (ListBlank.append (List.replicate k a) L) R⟩ = + some ⟨q, Tape.mkL L (ListBlank.append (List.replicate k b) R)⟩ := + nSteps_mkL_left_scan_of_step (fun l r ↦ by simpa only [Tape.mkL_cons] using step_mkL_left h l r) + k L R + +/-- Scan to the right over a constant block using a uniform tape step. -/ +theorem nSteps_mk'_right_scan_of_step {M : Machine states 2} {q : Fin states} {a b : Fin 2} + (h : ∀ L R, step M ⟨q, ⟨a, L, R⟩⟩ = some ⟨q, Tape.mk' (L.cons b) R⟩) + (k : ℕ) (L R : ListBlank (Fin 2)) : + nSteps M k ⟨q, Tape.mk' L (ListBlank.append (List.replicate k a) R)⟩ = + some ⟨q, Tape.mk' (ListBlank.append (List.replicate k b) L) R⟩ := by + induction k generalizing L with + | zero => rfl + | succ k ih => + rw [List.replicate_succ, ListBlank.append, nSteps_succ, Tape.mk'_cons, h] + simp only [Option.bind_some] + rw [ih (L.cons b), append_replicate_cons] + +theorem nSteps_mk'_right_scan {M : Machine states 2} {q : Fin states} {a b : Fin 2} + (h : M.transition q a = some (b, .right, q)) (k : ℕ) (L R : ListBlank (Fin 2)) : + nSteps M k ⟨q, Tape.mk' L (ListBlank.append (List.replicate k a) R)⟩ = + some ⟨q, Tape.mk' (ListBlank.append (List.replicate k b) L) R⟩ := + nSteps_mk'_right_scan_of_step (fun l r ↦ by simpa only [Tape.mk'_cons] using step_mk'_right h l r) + k L R + +/-- The all-blank one-sided binary tape. -/ +def B0 : ListBlank (Fin 2) := ListBlank.mk [] + +@[simp] +theorem B0_cons_zero : B0.cons 0 = B0 := + mk_append_replicate_zero [] 1 + +theorem reaches_imp_nSteps {M : Machine states 2} {a b : Cfg states 2} + (h : Reaches (step M) a b) : ∃ k, nSteps M k a = some b := by + induction h with + | refl => exact ⟨0, rfl⟩ + | tail _ hstep ih => + obtain ⟨k, hk⟩ := ih + exact ⟨k + 1, nSteps_bind_eq hk (step_one hstep)⟩ + +theorem nSteps_eq_none_of_step_none {M : Machine states 2} {k : ℕ} (s : ℕ) {a b : Cfg states 2} + (hk : nSteps M k a = some b) (hnone : step M b = none) (hks : k < s) : + nSteps M s a = none := by + have hs : s = k + ((s - k - 1) + 1) := by omega + rw [hs, nSteps_add, hk, Option.bind_some, nSteps_succ, hnone, Option.bind_none] + +end ConjectureMachines.Goldbach21 From 8cd74d12e2d4513a64b6588fe807886bef0faf4c Mon Sep 17 00:00:00 2001 From: Vasily Ilin Date: Thu, 26 Nov 2026 12:29:44 +0000 Subject: [PATCH 4/7] Replay Brocard startup through shared tape lemmas --- LeanPool/CryptidsGoldbach/Brocard28.lean | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/LeanPool/CryptidsGoldbach/Brocard28.lean b/LeanPool/CryptidsGoldbach/Brocard28.lean index b5f2996523..8711471a79 100644 --- a/LeanPool/CryptidsGoldbach/Brocard28.lean +++ b/LeanPool/CryptidsGoldbach/Brocard28.lean @@ -79,13 +79,11 @@ theorem mkRight_blank (L : ListBlank (Fin 2)) : Tape.mk' L B0 = ⟨0, L, B0⟩ : def ones (k : ℕ) (L : ListBlank (Fin 2)) : ListBlank (Fin 2) := ListBlank.append (List.replicate k 1) L -theorem ones_succ' (k : ℕ) (L : ListBlank (Fin 2)) : ones (k + 1) L = ones k (L.cons 1) := by - rw [ones, List.replicate_succ', ListBlank.append_assoc] - rfl +theorem ones_succ' (k : ℕ) (L : ListBlank (Fin 2)) : ones (k + 1) L = ones k (L.cons 1) := + (Goldbach21.append_replicate_cons k 1 L).symm -theorem ones_add (j k : ℕ) (L : ListBlank (Fin 2)) : ones (j + k) L = ones j (ones k L) := by - rw [ones, List.replicate_add, ListBlank.append_assoc] - rfl +theorem ones_add (j k : ℕ) (L : ListBlank (Fin 2)) : ones (j + k) L = ones j (ones k L) := + (Goldbach21.append_replicate_add j k 1 L).symm /-- The 28-state, two-symbol transition table from the upstream `brocard_28` artifact. -/ def brocard28 : Machine 28 2 := Machine.ofRows [ @@ -508,9 +506,8 @@ theorem multiply (n v : ℕ) (hn : 1 ≤ n) : def zeros (k : ℕ) (L : ListBlank (Fin 2)) : ListBlank (Fin 2) := ListBlank.append (List.replicate k 0) L -theorem zeros_succ' (k : ℕ) (L : ListBlank (Fin 2)) : zeros (k + 1) L = zeros k (L.cons 0) := by - rw [zeros, List.replicate_succ', ListBlank.append_assoc] - rfl +theorem zeros_succ' (k : ℕ) (L : ListBlank (Fin 2)) : zeros (k + 1) L = zeros k (L.cons 0) := + (Goldbach21.append_replicate_cons k 0 L).symm theorem zeros_B0 (k : ℕ) : zeros k B0 = B0 := by induction k with @@ -876,13 +873,15 @@ theorem init_eq : (init : Cfg 28 2) = ⟨0, Tape.mk' B0 B0⟩ := rfl `TR0` turn it into `1 0 0 1` and move on. -/ theorem seed₁ : nSteps brocard28 12 ⟨0, Tape.mk' B0 B0⟩ = some ⟨14, Tape.mk' (((((B0.cons 1).cons 0).cons 0).cons 1).cons 0) B0⟩ := by - rfl + simp only [mkRight_blank, nSteps_succ, sGO_0, mkL_B0, Option.bind_some, sEC_0, sLN0_0, sLN1_0, + mk'_cons, nSteps_zero, sEN_0, sX_0, sTLs_1, mkL_cons, sTLs_0, sTLx_1, sTR0_1, sTR0_0] /-- Start, steps `13`–`24`: one chunk of the test runs on `1 0 0 1`. -/ theorem seed₂ : nSteps brocard28 12 ⟨14, Tape.mk' (((((B0.cons 1).cons 0).cons 0).cons 1).cons 0) B0⟩ = some ⟨14, Tape.mk' (((((B0.cons 1).cons 0).cons 1).cons 0).cons 0) ((B0.cons 1).cons 0)⟩ := by - rfl + simp only [mkRight_blank, nSteps_succ, sTR1_0, mkL_cons, nSteps_zero, Option.bind_some, sSB_0, + sSC_1, mk'_cons, sTR0_0, sTR1_1, sSB_1, sTL0_0, sTL1_1, sTL1_0, sTE_1] /-- Start, steps `25`–`36`: `TR1`, `SB`, `SC` end the chunk as a success; `M0`, `M1` reading `0`, `GO`, `LN1`, `EN`, `X` write `1111 0 111`; `TLs` reading `0` enters `TLx`. -/ @@ -890,7 +889,8 @@ theorem seed₃ : nSteps brocard28 12 ⟨14, Tape.mk' (((((B0.cons 1).cons 0).cons 1).cons 0).cons 0) ((B0.cons 1).cons 0)⟩ = some ⟨1, Tape.mk' (ListBlank.cons 0 ((((((((B0.cons 1).cons 1).cons 1).cons 1).cons 0).cons 1).cons 1).cons 1)) B0⟩ := by - rfl + simp only [mk'_cons, nSteps_succ, sTR1_0, mkL_cons, nSteps_zero, Option.bind_some, sSB_0, sSC_0, + sM0_1, sM1_0, sGO_1, sLN1_0, sEN_0, sX_1, mkRight_blank, sX_0, sTLs_0] /-- Start, steps `37`–`46`: `TLx` reading `0`, `EC`, `LN0`, `LN1` walk left to the blank left of `N`. -/ From f925e3fbcdca1efd766bfae15c4ce9efc3d1dda5 Mon Sep 17 00:00:00 2001 From: Vasily Ilin Date: Thu, 26 Nov 2026 13:03:09 +0000 Subject: [PATCH 5/7] Share Brocard tape moves and erasure trace --- LeanPool/CryptidsGoldbach/Brocard28.lean | 69 ++++++++++++------------ 1 file changed, 33 insertions(+), 36 deletions(-) diff --git a/LeanPool/CryptidsGoldbach/Brocard28.lean b/LeanPool/CryptidsGoldbach/Brocard28.lean index 8711471a79..d1b2634efb 100644 --- a/LeanPool/CryptidsGoldbach/Brocard28.lean +++ b/LeanPool/CryptidsGoldbach/Brocard28.lean @@ -122,14 +122,14 @@ theorem mkL_B0 (R : ListBlank (Fin 2)) : mkL B0 R = ⟨0, B0, R⟩ := rfl theorem stepR {q q' : Fin 28} {a b : Fin 2} (h : brocard28.transition q a = some (b, .right, q')) (L R : ListBlank (Fin 2)) : - step brocard28 ⟨q, ⟨a, L, R⟩⟩ = some ⟨q', Tape.mk' (L.cons b) R⟩ := by - simp only [step, h, Option.map_some]; rfl + step brocard28 ⟨q, ⟨a, L, R⟩⟩ = some ⟨q', Tape.mk' (L.cons b) R⟩ := + Goldbach21.step_right h L R theorem stepL {q q' : Fin 28} {a b : Fin 2} (h : brocard28.transition q a = some (b, .left, q')) (L R : ListBlank (Fin 2)) : step brocard28 ⟨q, ⟨a, L, R⟩⟩ = some ⟨q', mkL L (R.cons b)⟩ := by - simp only [step, h, Option.map_some]; rfl + simpa only [mk'_cons] using Goldbach21.step_mk'_left h L R /-! ## Transition Table @@ -614,6 +614,29 @@ theorem adv_success (n a q : ℕ) : simp only [mk'_cons, nSteps_succ, sTR0_0, nSteps_zero, Option.bind_some, sTR1_0, mkL_cons, sSB_0, sSC_0] +/-- `ER2` erases the remaining ones of `S`. Then `ER3` returns over the entire erased block and +removes the last cell of `B` to start the next round. -/ +private theorem cleanup_to_round (n a erased remaining : ℕ) : + ∃ s, nSteps brocard28 s + ⟨21, Tape.mk' (zeros erased (((ones (a + 1) (NL n)).cons 1).cons 0)) (ones remaining B0)⟩ = + some (roundCfg n a) := by + let left := ((ones (a + 1) (NL n)).cons 1).cons 0 + have erase := sweepR_erase sER2_1 remaining (zeros erased left) B0 + have combine : zeros remaining (zeros erased left) = zeros (remaining + erased) left := + Goldbach21.append_replicate_add remaining erased 0 left + rw [combine] at erase + have turn : step brocard28 ⟨21, Tape.mk' (zeros (remaining + erased) left) B0⟩ = + some ⟨22, mkL (zeros (remaining + erased) left) B0⟩ := by + rw [mkRight_blank, sER2_0, B0_cons_zero] + have back := sweepL_zeros sER3_0 (remaining + erased) left B0 + rw [zeros_B0] at back + have finish : nSteps brocard28 2 ⟨22, mkL left B0⟩ = some (roundCfg n a) := by + change nSteps brocard28 2 ⟨22, mkL ((((ones a (NL n)).cons 1).cons 1).cons 0) B0⟩ = + some ⟨2, Tape.mk' (ones a (NL n)) (B0.cons 1)⟩ + simp only [mkL_cons, nSteps_succ, sER3_0, B0_cons_zero, nSteps_zero, Option.bind_some, sER3_1, + mk'_cons] + exact ⟨_, nSteps_bind_eq erase (step_nSteps turn (nSteps_bind_eq back finish))⟩ + /-- A unit with `B` exhausted: `TE` finds the separator, `ER`/`ER2` erase `S`, `ER3` returns, erasing the last cell of `B`, and the next round starts. -/ theorem adv_fail (n a p q : ℕ) : @@ -637,29 +660,15 @@ theorem adv_fail (n a p q : ℕ) : simp only [mkL_cons, nSteps_succ, sTL0_0, nSteps_zero, Option.bind_some, sTL1_0, mk'_cons, sTE_0] have h₆ := sweepR_erase sER_1 p (((ones (a + 1) (NL n)).cons 1).cons 0) ((ones (q + 1) B0).cons 0) - set W := ((ones (a + 1) (NL n)).cons 1).cons 0 with hW + let W := ((ones (a + 1) (NL n)).cons 1).cons 0 have h₇ : step brocard28 ⟨20, Tape.mk' (zeros p W) ((ones (q + 1) B0).cons 0)⟩ = some ⟨21, Tape.mk' ((zeros p W).cons 0) (ones (q + 1) B0)⟩ := by rw [mk'_cons, sER_0] - have h₈ := sweepR_erase sER2_1 (q + 1) ((zeros p W).cons 0) B0 - have h₉ : step brocard28 ⟨21, Tape.mk' (zeros (q + 1) ((zeros p W).cons 0)) B0⟩ = - some ⟨22, mkL (zeros (q + 1) ((zeros p W).cons 0)) B0⟩ := by - rw [mkRight_blank, sER2_0, B0_cons_zero] - have h₁₀ := sweepL_zeros sER3_0 (q + 1) ((zeros p W).cons 0) B0 - rw [zeros_B0] at h₁₀ - have h₁₁ : step brocard28 ⟨22, mkL ((zeros p W).cons 0) B0⟩ = some ⟨22, mkL (zeros p W) B0⟩ := by - rw [mkL_cons, sER3_0, B0_cons_zero] - have h₁₂ := sweepL_zeros sER3_0 p W B0 - rw [zeros_B0] at h₁₂ - have h₁₃ : nSteps brocard28 2 ⟨22, mkL W B0⟩ = some (roundCfg n a) := by - rw [hW] - change nSteps brocard28 2 ⟨22, mkL ((((ones a (NL n)).cons 1).cons 1).cons 0) B0⟩ = - some ⟨2, Tape.mk' (ones a (NL n)) (B0.cons 1)⟩ - simp only [mkL_cons, nSteps_succ, sER3_0, B0_cons_zero, nSteps_zero, Option.bind_some, sER3_1, - mk'_cons] + obtain ⟨_, finish⟩ := cleanup_to_round n a (p + 1) (q + 1) + rw [show zeros (p + 1) W = (zeros p W).cons 0 by + simp only [zeros, List.replicate_succ, ListBlank.append]] at finish exact ⟨_, step_nSteps h₁ (nSteps_bind_eq h₂ (nSteps_bind_eq h₃ (nSteps_bind_eq h₄ - (nSteps_bind_eq h₅ (nSteps_bind_eq h₆ (step_nSteps h₇ (nSteps_bind_eq h₈ (step_nSteps h₉ - (nSteps_bind_eq h₁₀ (step_nSteps h₁₁ (nSteps_bind_eq h₁₂ h₁₃)))))))))))⟩ + (nSteps_bind_eq h₅ (nSteps_bind_eq h₆ (step_nSteps h₇ finish))))))⟩ /-! ## Triangular Numbers and the Chunk Loop -/ @@ -831,20 +840,8 @@ theorem gate_back (n a S : ℕ) : some ⟨21, Tape.mk' (((ones (a + 1) (NL n)).cons 1).cons 0) (ones S B0)⟩ := by rw [hR] simp only [mk'_cons, nSteps_succ, sBs_0, nSteps_zero, Option.bind_some, sER_0] - set W := ((ones (a + 1) (NL n)).cons 1).cons 0 with hW - have h₅ := sweepR_erase sER2_1 S W B0 - have h₆ : step brocard28 ⟨21, Tape.mk' (zeros S W) B0⟩ = some ⟨22, mkL (zeros S W) B0⟩ := by - rw [mkRight_blank, sER2_0, B0_cons_zero] - have h₇ := sweepL_zeros sER3_0 S W B0 - rw [zeros_B0] at h₇ - have h₈ : nSteps brocard28 2 ⟨22, mkL W B0⟩ = some (roundCfg n a) := by - rw [hW] - change nSteps brocard28 2 ⟨22, mkL ((((ones a (NL n)).cons 1).cons 1).cons 0) B0⟩ = - some ⟨2, Tape.mk' (ones a (NL n)) (B0.cons 1)⟩ - simp only [mkL_cons, nSteps_succ, sER3_0, B0_cons_zero, nSteps_zero, Option.bind_some, sER3_1, - mk'_cons] - exact ⟨_, nSteps_bind_eq h₃ (nSteps_bind_eq h₄ (nSteps_bind_eq h₅ (step_nSteps h₆ - (nSteps_bind_eq h₇ h₈))))⟩ + obtain ⟨_, finish⟩ := cleanup_to_round n a 0 S + exact ⟨_, nSteps_bind_eq h₃ (nSteps_bind_eq h₄ finish)⟩ /-- A success whose `B` has `a + 1 ≡ 2` or `3 (mod 4)` ones is suppressed: the next round starts. -/ From 2e558380281839725d132d2f894605cc7e0fd9c6 Mon Sep 17 00:00:00 2001 From: Vasily Ilin Date: Thu, 26 Nov 2026 13:08:22 +0000 Subject: [PATCH 6/7] Pin the public Brocard 28-state announcement --- LeanPool/projects/cryptids-goldbach.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/LeanPool/projects/cryptids-goldbach.yaml b/LeanPool/projects/cryptids-goldbach.yaml index 0739795c6a..42a4e40fa0 100644 --- a/LeanPool/projects/cryptids-goldbach.yaml +++ b/LeanPool/projects/cryptids-goldbach.yaml @@ -20,8 +20,8 @@ source: announcement_url: https://leanprover.zulipchat.com/#narrow/channel/579630-Project-announcements/topic/Cryptids/near/632199475 announcement_date: '2026-10-16' brocard_url: https://github.com/google-deepmind/cryptids/tree/481f4e3e39907304dbcad33e508bf21d80c6d5a6/artifacts/brocard_28 - brocard_announcement_url: https://github.com/google-deepmind/cryptids/pull/27 - brocard_announcement_date: '2026-11-11' + brocard_announcement_url: https://github.com/google-deepmind/cryptids/commit/ab177608e88ce30697d1e705e1cd9531bef9cb8e + brocard_announcement_date: '2026-11-09' license: Apache-2.0 status: verified provenance: mix From 129acd13cfb03c21abce8e3243b759680f7adb5b Mon Sep 17 00:00:00 2001 From: Vasily Ilin Date: Thu, 26 Nov 2026 13:53:45 +0000 Subject: [PATCH 7/7] Share the Brocard copy and chunk-test traces --- LeanPool/CryptidsGoldbach/Brocard28.lean | 126 +++++++++++------------ 1 file changed, 60 insertions(+), 66 deletions(-) diff --git a/LeanPool/CryptidsGoldbach/Brocard28.lean b/LeanPool/CryptidsGoldbach/Brocard28.lean index d1b2634efb..7ca7393210 100644 --- a/LeanPool/CryptidsGoldbach/Brocard28.lean +++ b/LeanPool/CryptidsGoldbach/Brocard28.lean @@ -304,52 +304,50 @@ theorem advCfg_solid (n a r p : ℕ) : /-! ## Multiply (States `2`–`11`) -/ +/-- Starting at the separator, `R0` through `L1` append a cell to the output block and return to +the template. -/ +private theorem copy_append (left : ListBlank (Fin 2)) (m : ℕ) : + ∃ s, nSteps brocard28 s ⟨6, Tape.mk' left ((ones m B0).cons 0)⟩ = + some ⟨9, mkL left ((ones (m + 1) B0).cons 0)⟩ := by + have enter : step brocard28 ⟨6, Tape.mk' left ((ones m B0).cons 0)⟩ = + some ⟨7, Tape.mk' (left.cons 0) (ones m B0)⟩ := by + rw [mk'_cons, sR0_0] + have advance := sweepR sR1_1 m (left.cons 0) B0 + have append : step brocard28 ⟨7, Tape.mk' (ones m (left.cons 0)) B0⟩ = + some ⟨8, mkL (ones m (left.cons 0)) (B0.cons 1)⟩ := by + rw [mkRight_blank, sR1_0] + have back := sweepL sL0_1 m (left.cons 0) (B0.cons 1) + have exit : step brocard28 ⟨8, mkL (left.cons 0) (ones m (B0.cons 1))⟩ = + some ⟨9, mkL left ((ones (m + 1) B0).cons 0)⟩ := by + rw [mkL_cons, sL0_0, ones_succ' m] + exact ⟨_, step_nSteps enter (nSteps_bind_eq advance (step_nSteps append + (nSteps_bind_eq back (step_one exit))))⟩ + /-- One copy step: the cursor moves one cell right and `M` gains a cell. -/ theorem copy_step (a b u v m : ℕ) : ∃ s, nSteps brocard28 s (copyCfg a b u (v + 1) m) = some (copyCfg a b (u + 1) v (m + 1)) := by - set W := (ones b ((ones a B0).cons 0)).cons 0 with hW - set Y := ones u W with hY + let W := (ones b ((ones a B0).cons 0)).cons 0 + let Y := ones u W have h₁ := sweepR sR0_1 (v + 1) (Y.cons 0) ((ones m B0).cons 0) - have h₂ : step brocard28 ⟨6, Tape.mk' (ones (v + 1) (Y.cons 0)) ((ones m B0).cons 0)⟩ = - some ⟨7, Tape.mk' ((ones (v + 1) (Y.cons 0)).cons 0) (ones m B0)⟩ := by - rw [mk'_cons, sR0_0] - have h₃ := sweepR sR1_1 m ((ones (v + 1) (Y.cons 0)).cons 0) B0 - have h₄ : step brocard28 ⟨7, Tape.mk' (ones m ((ones (v + 1) (Y.cons 0)).cons 0)) B0⟩ = - some ⟨8, mkL (ones m ((ones (v + 1) (Y.cons 0)).cons 0)) (B0.cons 1)⟩ := by - rw [mkRight_blank, sR1_0] - have h₅ := sweepL sL0_1 m ((ones (v + 1) (Y.cons 0)).cons 0) (B0.cons 1) - have h₆ : step brocard28 ⟨8, mkL ((ones (v + 1) (Y.cons 0)).cons 0) (ones m (B0.cons 1))⟩ = - some ⟨9, mkL (ones (v + 1) (Y.cons 0)) ((ones (m + 1) B0).cons 0)⟩ := by - rw [mkL_cons, sL0_0, ones_succ' m] + obtain ⟨_, append⟩ := copy_append (ones (v + 1) (Y.cons 0)) m have h₇ := sweepL sL1_1 (v + 1) (Y.cons 0) ((ones (m + 1) B0).cons 0) have h₈ : nSteps brocard28 2 ⟨9, mkL (Y.cons 0) (ones (v + 1) ((ones (m + 1) B0).cons 0))⟩ = some (copyCfg a b (u + 1) v (m + 1)) := by change nSteps brocard28 2 ⟨9, mkL (Y.cons 0) ((ones v ((ones (m + 1) B0).cons 0)).cons 1)⟩ = some ⟨6, Tape.mk' ((Y.cons 1).cons 0) (ones v ((ones (m + 1) B0).cons 0))⟩ simp only [mkL_cons, nSteps_succ, sL1_0, mk'_cons, nSteps_zero, Option.bind_some, sEC_1] - exact ⟨_, nSteps_bind_eq h₁ (step_nSteps h₂ (nSteps_bind_eq h₃ (step_nSteps h₄ - (nSteps_bind_eq h₅ (step_nSteps h₆ (nSteps_bind_eq h₇ h₈))))))⟩ + exact ⟨_, nSteps_bind_eq h₁ (nSteps_bind_eq append (nSteps_bind_eq h₇ h₈))⟩ /-- The last copy step: the cursor leaves the template, `LN0`/`LN1` return to the hole of `N` and fill it, and `EN` reads the next cell of `N`. -/ theorem copy_done (a b u m : ℕ) : ∃ s, nSteps brocard28 s (copyCfg a b u 0 m) = some (enCfg (a + 1) b (u + 1) (m + 1)) := by set W := (ones b ((ones a B0).cons 0)).cons 0 with hW - set Y := ones u W with hY - have h₂ : step brocard28 (copyCfg a b u 0 m) = - some ⟨7, Tape.mk' ((Y.cons 0).cons 0) (ones m B0)⟩ := by - change step brocard28 ⟨6, Tape.mk' (Y.cons 0) ((ones m B0).cons 0)⟩ = _ - rw [mk'_cons, sR0_0] - have h₃ := sweepR sR1_1 m ((Y.cons 0).cons 0) B0 - have h₄ : step brocard28 ⟨7, Tape.mk' (ones m ((Y.cons 0).cons 0)) B0⟩ = - some ⟨8, mkL (ones m ((Y.cons 0).cons 0)) (B0.cons 1)⟩ := by - rw [mkRight_blank, sR1_0] - have h₅ := sweepL sL0_1 m ((Y.cons 0).cons 0) (B0.cons 1) - have h₆ : nSteps brocard28 3 ⟨8, mkL ((Y.cons 0).cons 0) (ones m (B0.cons 1))⟩ = + let Y := ones u W + obtain ⟨_, append⟩ := copy_append (Y.cons 0) m + have h₆ : nSteps brocard28 2 ⟨9, mkL (Y.cons 0) ((ones (m + 1) B0).cons 0)⟩ = some ⟨2, mkL (Y.cons 1) ((ones (m + 1) B0).cons 0)⟩ := by - rw [ones_succ' m] - simp only [mkL_cons, nSteps_succ, sL0_0, nSteps_zero, Option.bind_some, sL1_0, mk'_cons, - sEC_0] + simp only [mkL_cons, nSteps_succ, nSteps_zero, Option.bind_some, sL1_0, mk'_cons, sEC_0] have h₇ := sweepL sLN0_1 (u + 1) W ((ones (m + 1) B0).cons 0) have h₈ : step brocard28 ⟨2, mkL W (ones (u + 1) ((ones (m + 1) B0).cons 0))⟩ = some ⟨3, mkL (ones b ((ones a B0).cons 0)) @@ -364,9 +362,8 @@ theorem copy_done (a b u m : ℕ) : rfl have hY1 : Y.cons 1 = ones (u + 1) W := rfl rw [hY1] at h₆ - exact ⟨_, step_nSteps h₂ (nSteps_bind_eq h₃ (step_nSteps h₄ (nSteps_bind_eq h₅ - (nSteps_bind_eq h₆ (nSteps_bind_eq h₇ (step_nSteps h₈ (nSteps_bind_eq h₉ - (step_one h₁₀))))))))⟩ + exact ⟨_, nSteps_bind_eq append (nSteps_bind_eq h₆ (nSteps_bind_eq h₇ + (step_nSteps h₈ (nSteps_bind_eq h₉ (step_one h₁₀)))))⟩ /-- A whole copy of the template. -/ theorem copy_loop (a b u : ℕ) : ∀ v m, ∃ s, nSteps brocard28 s (copyCfg a b u v m) = @@ -546,25 +543,34 @@ theorem test_start (n r : ℕ) : B0_cons_zero] exact ⟨_, step_nSteps h₁ (t_to_adv n 0 r 0 0)⟩ -/-- One unit: the hole of `S` moves one cell left and the hole of `B` one cell right. -/ -theorem adv_step (n a r p q : ℕ) : - ∃ s, nSteps brocard28 s (advCfg n a (r + 1) (p + 1) q) = some (tCfg n (a + 1) r p (q + 1)) := by - set H := (ones a (NL n)).cons 0 with hH - set Lr := ones (r + 1) H with hLr - have h₁ : step brocard28 (advCfg n a (r + 1) (p + 1) q) = - some ⟨14, Tape.mk' (Lr.cons 0) (ones (p + 1) ((ones q B0).cons 0))⟩ := by - change step brocard28 ⟨13, Tape.mk' Lr ((ones (p + 1) ((ones q B0).cons 0)).cons 0)⟩ = _ +/-- Move the hole of `S` one cell left, returning to `TL1` at the right end of `B`. -/ +private theorem shift_chunk_hole (left : ListBlank (Fin 2)) (p q : ℕ) : + ∃ s, nSteps brocard28 s + ⟨13, Tape.mk' left ((ones (p + 1) ((ones q B0).cons 0)).cons 0)⟩ = + some ⟨18, mkL left ((ones p ((ones (q + 1) B0).cons 0)).cons 0)⟩ := by + have enter : step brocard28 + ⟨13, Tape.mk' left ((ones (p + 1) ((ones q B0).cons 0)).cons 0)⟩ = + some ⟨14, Tape.mk' (left.cons 0) (ones (p + 1) ((ones q B0).cons 0))⟩ := by rw [mk'_cons, sTR0_0] - have h₂ := sweepR sTR1_1 (p + 1) (Lr.cons 0) ((ones q B0).cons 0) - have h₃ : nSteps brocard28 2 ⟨14, Tape.mk' (ones (p + 1) (Lr.cons 0)) ((ones q B0).cons 0)⟩ = - some ⟨17, mkL (ones p (Lr.cons 0)) ((ones (q + 1) B0).cons 0)⟩ := by - change nSteps brocard28 2 ⟨14, Tape.mk' ((ones p (Lr.cons 0)).cons 1) ((ones q B0).cons 0)⟩ = - some ⟨17, mkL (ones p (Lr.cons 0)) (((ones q B0).cons 1).cons 0)⟩ + have advance := sweepR sTR1_1 (p + 1) (left.cons 0) ((ones q B0).cons 0) + have shift : nSteps brocard28 2 + ⟨14, Tape.mk' (ones (p + 1) (left.cons 0)) ((ones q B0).cons 0)⟩ = + some ⟨17, mkL (ones p (left.cons 0)) ((ones (q + 1) B0).cons 0)⟩ := by + change nSteps brocard28 2 ⟨14, Tape.mk' ((ones p (left.cons 0)).cons 1) ((ones q B0).cons 0)⟩ = + some ⟨17, mkL (ones p (left.cons 0)) (((ones q B0).cons 1).cons 0)⟩ simp only [mk'_cons, nSteps_succ, sTR1_0, mkL_cons, nSteps_zero, Option.bind_some, sSB_1] - have h₄ := sweepL sTL0_1 p (Lr.cons 0) ((ones (q + 1) B0).cons 0) - have h₅ : step brocard28 ⟨17, mkL (Lr.cons 0) (ones p ((ones (q + 1) B0).cons 0))⟩ = - some ⟨18, mkL Lr ((ones p ((ones (q + 1) B0).cons 0)).cons 0)⟩ := by + have back := sweepL sTL0_1 p (left.cons 0) ((ones (q + 1) B0).cons 0) + have exit : step brocard28 ⟨17, mkL (left.cons 0) (ones p ((ones (q + 1) B0).cons 0))⟩ = + some ⟨18, mkL left ((ones p ((ones (q + 1) B0).cons 0)).cons 0)⟩ := by rw [mkL_cons, sTL0_0] + exact ⟨_, step_nSteps enter (nSteps_bind_eq advance (nSteps_bind_eq shift + (nSteps_bind_eq back (step_one exit))))⟩ + +/-- One unit: the hole of `S` moves one cell left and the hole of `B` one cell right. -/ +theorem adv_step (n a r p q : ℕ) : + ∃ s, nSteps brocard28 s (advCfg n a (r + 1) (p + 1) q) = some (tCfg n (a + 1) r p (q + 1)) := by + let H := (ones a (NL n)).cons 0 + obtain ⟨_, shift⟩ := shift_chunk_hole (ones (r + 1) H) p q have h₆ := sweepL sTL1_1 (r + 1) H ((ones p ((ones (q + 1) B0).cons 0)).cons 0) have h₇ : nSteps brocard28 2 ⟨18, mkL H (ones (r + 1) ((ones p ((ones (q + 1) B0).cons 0)).cons 0))⟩ = @@ -574,8 +580,7 @@ theorem adv_step (n a r p q : ℕ) : some ⟨13, Tape.mk' (((ones a (NL n)).cons 1).cons 0) (ones r ((ones p ((ones (q + 1) B0).cons 0)).cons 0))⟩ simp only [mkL_cons, nSteps_succ, sTL1_0, mk'_cons, nSteps_zero, Option.bind_some, sTE_1] - exact ⟨_, step_nSteps h₁ (nSteps_bind_eq h₂ (nSteps_bind_eq h₃ (nSteps_bind_eq h₄ - (step_nSteps h₅ (nSteps_bind_eq h₆ h₇)))))⟩ + exact ⟨_, nSteps_bind_eq shift (nSteps_bind_eq h₆ h₇)⟩ /-- `p` units in a row, while `B` has enough cells. -/ theorem adv_many (n : ℕ) : ∀ p a r p' q, ∃ s, nSteps brocard28 s (advCfg n a (r + p) (p + p') q) = @@ -642,23 +647,12 @@ erasing the last cell of `B`, and the next round starts. -/ theorem adv_fail (n a p q : ℕ) : ∃ s, nSteps brocard28 s (advCfg n (a + 1) 0 (p + 1) q) = some (roundCfg n a) := by set H := (ones (a + 1) (NL n)).cons 0 with hH - have h₁ : step brocard28 (advCfg n (a + 1) 0 (p + 1) q) = - some ⟨14, Tape.mk' (H.cons 0) (ones (p + 1) ((ones q B0).cons 0))⟩ := by - change step brocard28 ⟨13, Tape.mk' H ((ones (p + 1) ((ones q B0).cons 0)).cons 0)⟩ = _ - rw [mk'_cons, sTR0_0] - have h₂ := sweepR sTR1_1 (p + 1) (H.cons 0) ((ones q B0).cons 0) - have h₃ : nSteps brocard28 2 ⟨14, Tape.mk' (ones (p + 1) (H.cons 0)) ((ones q B0).cons 0)⟩ = - some ⟨17, mkL (ones p (H.cons 0)) ((ones (q + 1) B0).cons 0)⟩ := by - change nSteps brocard28 2 ⟨14, Tape.mk' ((ones p (H.cons 0)).cons 1) ((ones q B0).cons 0)⟩ = - some ⟨17, mkL (ones p (H.cons 0)) (((ones q B0).cons 1).cons 0)⟩ - simp only [mk'_cons, nSteps_succ, sTR1_0, mkL_cons, nSteps_zero, Option.bind_some, sSB_1] - have h₄ := sweepL sTL0_1 p (H.cons 0) ((ones (q + 1) B0).cons 0) - have h₅ : nSteps brocard28 3 ⟨17, mkL (H.cons 0) (ones p ((ones (q + 1) B0).cons 0))⟩ = + obtain ⟨_, shift⟩ := shift_chunk_hole H p q + have h₅ : nSteps brocard28 2 ⟨18, mkL H ((ones p ((ones (q + 1) B0).cons 0)).cons 0)⟩ = some ⟨20, Tape.mk' (((ones (a + 1) (NL n)).cons 1).cons 0) (ones p ((ones (q + 1) B0).cons 0))⟩ := by rw [hH] - simp only [mkL_cons, nSteps_succ, sTL0_0, nSteps_zero, Option.bind_some, sTL1_0, mk'_cons, - sTE_0] + simp only [mkL_cons, nSteps_succ, nSteps_zero, Option.bind_some, sTL1_0, mk'_cons, sTE_0] have h₆ := sweepR_erase sER_1 p (((ones (a + 1) (NL n)).cons 1).cons 0) ((ones (q + 1) B0).cons 0) let W := ((ones (a + 1) (NL n)).cons 1).cons 0 have h₇ : step brocard28 ⟨20, Tape.mk' (zeros p W) ((ones (q + 1) B0).cons 0)⟩ = @@ -667,8 +661,8 @@ theorem adv_fail (n a p q : ℕ) : obtain ⟨_, finish⟩ := cleanup_to_round n a (p + 1) (q + 1) rw [show zeros (p + 1) W = (zeros p W).cons 0 by simp only [zeros, List.replicate_succ, ListBlank.append]] at finish - exact ⟨_, step_nSteps h₁ (nSteps_bind_eq h₂ (nSteps_bind_eq h₃ (nSteps_bind_eq h₄ - (nSteps_bind_eq h₅ (nSteps_bind_eq h₆ (step_nSteps h₇ finish))))))⟩ + exact ⟨_, nSteps_bind_eq shift (nSteps_bind_eq h₅ (nSteps_bind_eq h₆ + (step_nSteps h₇ finish)))⟩ /-! ## Triangular Numbers and the Chunk Loop -/