Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions LeanPool/CryptidsGoldbach.lean
Original file line number Diff line number Diff line change
Expand Up @@ -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
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`
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.
-/
1,090 changes: 1,090 additions & 0 deletions LeanPool/CryptidsGoldbach/Brocard28.lean

Large diffs are not rendered by default.

84 changes: 84 additions & 0 deletions LeanPool/CryptidsGoldbach/BrocardSemantics.lean
Original file line number Diff line number Diff line change
@@ -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
159 changes: 1 addition & 158 deletions LeanPool/CryptidsGoldbach/Goldbach21.lean
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Authors: Maxence Faldor, Moritz Firsching
-/
module

public import LeanPool.CryptidsGoldbach.Machines
public import LeanPool.CryptidsGoldbach.MachineSupport

/-!
# Goldbach21
Expand Down Expand Up @@ -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
Expand All @@ -176,89 +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]

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)⟩ := by
induction k generalizing R with
| zero => rfl
| succ k ih =>
rw [List.replicate_succ, ListBlank.append, nSteps_succ, step_mkL_left 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)) :
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]
simp only [Option.bind_some]
rw [ih (L.cons b), append_replicate_cons]

/-- 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)
Expand Down Expand Up @@ -1205,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
Expand Down
3 changes: 3 additions & 0 deletions LeanPool/CryptidsGoldbach/Imports.lean
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ 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.MachineSupport
public import LeanPool.CryptidsGoldbach.Machines
public import LeanPool.CryptidsGoldbach.Propositions
public import LeanPool.CryptidsGoldbach.Semantics
Loading
Loading