/-
Copyright 2025 The Formal Conjectures Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-/
module
publicimport FormalConjecturesUtil
/-!
# Busy Beaver
The Busy Beaver problem asks for the maximum number of steps that an n-state, 2-symbol Turing
machine can take before halting, when started on an empty tape.
*References:*
- [The Busy Beaver Challenge](https://wiki.bbchallenge.org/wiki/Main_Page)
-/
@[expose] publicsection
universe u v
open Turing BusyBeaver
namespace BusyBeaver
structure Candidate (n : ℕ) where
Γ : Type
Λ : Type
Γ_fintype : Fintype Γ
Γ_card : Fintype.card Γ = 2
Γ_inhabited : Inhabited Γ
Λ_fintype : Fintype Λ
Λ_card : Fintype.card Λ = n
Λ_inhabited : Inhabited Λ
M : Machine Γ Λ
M_isHalting : M.IsHalting
instance {n : ℕ} {M : Candidate n} : Fintype M.Γ := M.Γ_fintype
instance {n : ℕ} {M : Candidate n} : Fintype M.Λ := M.Λ_fintype
instance {n : ℕ} {M : Candidate n} : Inhabited M.Γ := M.Γ_inhabited
instance {n : ℕ} {M : Candidate n} : Inhabited M.Λ := M.Λ_inhabited
/--
`BB(n)` is the `n`-th Busy Beaver number.
*This is the maximum shifts function*, not the "number of ones function"
-/
noncomputabledef BB (n : ℕ) : ℕ :=
sSup { N | ∃ C : Candidate n, C.M.haltingNumber = N}
/--
To compute `BB n`, we need only consider machines with states and symbols indexed in `Fin`.
-/
@[category API, AMS 3]
theorem sanity_check (n : ℕ) [NeZero n] :
BB n = sSup {N | ∃ (M : Machine (Fin 2) (Fin n)) (_ : M.IsHalting),
M.haltingNumber = N} := by
sorry
/-- The value of the Busy Beaver function for 1 state is 1. -/
@[category test, AMS 3]
theorem BB_1 : BB 1 = 1 := by
sorry
/-- The value of the Busy Beaver function for 2 states is 6. -/
@[category textbook, AMS 3]
theorem BB_2 : BB 2 = 6 := by
sorry
/-- The value of the Busy Beaver function for 3 states is 21. -/
@[category textbook, AMS 3]
theorem BB_3 : BB 3 = 21 := by
sorry
/-- The value of the Busy Beaver function for 4 states is 107. -/
@[category textbook, AMS 3]
theorem BB_4 : BB 4 = 107 := by
sorry
/-- The value of the Busy Beaver function for 5 states is 47176870. -/
@[category research solved, AMS 3]
theorem BB_5 : BB 5 = 47176870 := by
sorry
def BB_6.answer : ℕ := sorry
/--
Determine the value of the Busy Beaver function at n = 6.
-/
@[category research open, question value, AMS 3]
theorem BB_6 : BB 6 = BB_6.answer := by
sorry
end BusyBeaver
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140import FormalConjectures.Wikipedia.BusyBeaver
namespace Submission
open Turing BusyBeaver
theorem exists_default_equiv {α β : Type} [Inhabited α] [Inhabited β] [Fintype α] [Fintype β]
(h : Fintype.card α = Fintype.card β) : ∃ e : α ≃ β, e default = default := by
classical
obtain ⟨e⟩ : Nonempty (α ≃ β) := ⟨Fintype.equivOfCardEq h⟩
exact ⟨e.trans (Equiv.swap (e default) default), by simp⟩
variable {Γ Λ Γ2 Λ2 : Type} [Inhabited Γ] [Inhabited Λ] [Inhabited Γ2] [Inhabited Λ2]
/-- Relabel the symbols and states of a machine along bijections. -/def relabelM (f : Γ ≃ Γ2) (g : Λ ≃ Λ2) (M : Machine Γ Λ) : Machine Γ2 Λ2 :=
fun q a => (M (g.symm q) (f.symm a)).map fun p => (p.1.map g, Stmt.write (f p.2.symbol) p.2.dir)
/-- Relabel a configuration, keeping the blank symbol and initial state fixed. -/def relabelCfg (f : Γ ≃ Γ2) (hf : f default = default) (g : Λ ≃ Λ2) (c : Cfg Γ Λ) :
Cfg Γ2 Λ2 :=
⟨c.q.map g, c.tape.map ⟨⇑f, hf⟩⟩
theorem step_relabelM (f : Γ ≃ Γ2) (hf : f default = default) (g : Λ ≃ Λ2) (M : Machine Γ Λ)
(c : Cfg Γ Λ) :
Machine.step (relabelM f g M) (relabelCfg f hf g c) =
(Machine.step M c).map (relabelCfg f hf g) := by
rcases c with ⟨_ | q, T⟩
· rfl
· have key : relabelM f g M (g q) (f T.1) =
(M q T.1).map (fun p => (p.1.map g, Stmt.write (f p.2.symbol) p.2.dir)) := by
simp [relabelM]
rcases hM : M q T.1 with _ | ⟨qn, ⟨a, d⟩⟩
· simp [Machine.step, relabelCfg, key, hM]
· simp [Machine.step, relabelCfg, key, hM, Tape.map_move, Tape.map_write]
theorem multiStep_relabelM (f : Γ ≃ Γ2) (hf : f default = default) (g : Λ ≃ Λ2) (M : Machine Γ Λ)
(c : Cfg Γ Λ) (k : ℕ) :
(relabelM f g M).multiStep (relabelCfg f hf g c) k =
(M.multiStep c k).map (relabelCfg f hf g) := by
induction k with
| zero => simp
| succ k ih =>
simp only [Machine.multiStep_succ, ih]
rcases hk : M.multiStep c k with _ | x
· simp
· simp [step_relabelM]
theorem haltsAfter_relabelM (f : Γ ≃ Γ2) (hf : f default = default) (g : Λ ≃ Λ2)
(hg : g default = default) (M : Machine Γ Λ) (k : ℕ) :
(relabelM f g M).HaltsAfter (Machine.init []) k ↔ M.HaltsAfter (Machine.init []) k := by
have hinit : relabelCfg f hf g (Machine.init []) = Machine.init [] := by
simp [Machine.init, relabelCfg, hg, Tape.map_mk₁]
have h := multiStep_relabelM f hf g M (Machine.init []) (k + 1)
rw [hinit] at h
unfold Machine.HaltsAfter
rw [h]
rcases M.multiStep (Machine.init []) (k + 1) with _ | x <;> simp
theorem haltingNumber_relabelM (f : Γ ≃ Γ2) (hf : f default = default) (g : Λ ≃ Λ2)
(hg : g default = default) (M : Machine Γ Λ) :
(relabelM f g M).haltingNumber = M.haltingNumber := by
unfold Machine.haltingNumber
congr 1
apply Set.ext
intro x
constructor
· rintro ⟨n, hn, rfl⟩
exact ⟨n, (haltsAfter_relabelM f hf g hg M n).1 hn, rfl⟩
· rintro ⟨n, hn, rfl⟩
exact ⟨n, (haltsAfter_relabelM f hf g hg M n).2 hn, rfl⟩
theorem haltingNumber_ne_top (M : Machine Γ Λ) (h : M.IsHalting) : M.haltingNumber ≠ ⊤ := by
obtain ⟨k, hk⟩ := (Machine.isHalting_iff_exists_haltsAt M).1 h
unfold Machine.haltingNumber
exact ne_top_of_le_ne_top (ENat.natCast_ne_top k) (sInf_le ⟨k, hk, rfl⟩)
instance finiteDir : Finite Dir :=
Finite.of_injective (fun d : Dir => match d with | .left => (0 : Fin 2) | .right => 1)
(by rintro (_ | _) (_ | _) h <;> simp_all)
instance finiteStmt : Finite (Stmt (Fin 2)) :=
Finite.of_injective (fun s : Stmt (Fin 2) => (s.symbol, s.dir))
(by rintro ⟨a, d⟩ ⟨b, e⟩ h; simp_all)
instance finiteMachine (n : ℕ) [NeZero n] : Finite (Machine (Fin 2) (Fin n)) := by
unfold Machine
infer_instance
theorem solution : type_of% @BusyBeaver.sanity_check := by
intro n _
unfold BusyBeaver.BB
set T : Set ℕ∞ := {x | ∃ (M : Machine (Fin 2) (Fin n)) (_ : M.IsHalting), M.haltingNumber = x}
with hT
have hset : {x : ℕ∞ | ∃ C : Candidate n, C.M.haltingNumber = x} = T := by
rw [hT]
apply Set.ext
intro x
constructor
· rintro ⟨C, rfl⟩
obtain ⟨f, hf⟩ := exists_default_equiv (α := C.Γ) (β := Fin 2) (by simp [C.Γ_card])
obtain ⟨g, hg⟩ := exists_default_equiv (α := C.Λ) (β := Fin n) (by simp [C.Λ_card])
obtain ⟨k, hk⟩ := (Machine.isHalting_iff_exists_haltsAt C.M).1 C.M_isHalting
exact ⟨relabelM f g C.M,
(Machine.isHalting_iff_exists_haltsAt _).2 ⟨k, (haltsAfter_relabelM f hf g hg C.M k).2 hk⟩,
haltingNumber_relabelM f hf g hg C.M⟩
· rintro ⟨M, hM, rfl⟩
exact ⟨⟨Fin 2, Fin n, inferInstance, by simp, inferInstance, inferInstance, by simp,
inferInstance, M, hM⟩, rfl⟩
have hTfin : T.Finite := by
refine Set.Finite.subset
(Set.Finite.image (fun M : Machine (Fin 2) (Fin n) => M.haltingNumber)
(Set.toFinite {M : Machine (Fin 2) (Fin n) | M.IsHalting})) ?_
rintro x ⟨M, hM, rfl⟩
exact ⟨M, hM, rfl⟩
have hne : T.Nonempty := by
let M0 : Machine (Fin 2) (Fin n) := fun _ _ => none
have h0 : M0.IsHalting := (Machine.isHalting_iff_exists_haltsAt M0).2
⟨0, (Machine.haltsAfter_zero_iff M0 _).2 (by simp [M0, Machine.step, Machine.init])⟩
exact ⟨M0.haltingNumber, M0, h0, rfl⟩
have hS : {N : ℕ | ∃ C : Candidate n, C.M.haltingNumber = (N : ℕ∞)} =
Set.preimage (Nat.cast : ℕ → ℕ∞) T := by
ext N
exact Set.ext_iff.mp hset N
rw [hS]
have hSfin : (Set.preimage (Nat.cast : ℕ → ℕ∞) T).Finite :=
hTfin.preimage Nat.cast_injective.injOn
have hSne : (Set.preimage (Nat.cast : ℕ → ℕ∞) T).Nonempty := by
obtain ⟨x, M, hM, rfl⟩ := hne
obtain ⟨N, hN⟩ := ENat.ne_top_iff_exists.1 (haltingNumber_ne_top M hM)
exact ⟨N, M, hM, hN.symm⟩
have hm := hSne.csSup_mem hSfin
have hgreat : IsGreatest T ((sSup (Set.preimage (Nat.cast : ℕ → ℕ∞) T) : ℕ) : ℕ∞) := by
refine ⟨hm, ?_⟩
rintro x ⟨M, hM, rfl⟩
obtain ⟨N, hN⟩ := ENat.ne_top_iff_exists.1 (haltingNumber_ne_top M hM)
rw [← hN]
exact Nat.cast_le.mpr (le_csSup hSfin.bddAbove ⟨M, hM, hN.symm⟩)
exact (IsGreatest.csSup_eq hgreat).symm
end Submission
Check it yourself
Project
formal-conjectures
Commit
f606af160664c5a10952e9a957b100a1cd45bbeb
Lean
leanprover/lean4:v4.33.1
Mathlib
0df444a360eaa60ab8c11dca51a86af692955474
With Git and Elan installed, put the download in a fresh working directory and run this there. Use a disposable environment. Lean prints the axioms at the end. The checker allows propext, Classical.choice, and Quot.sound.
(
set -eu
proof="$PWD/2bdb2402-7e12-4a64-b900-3d04407236a6.lean"
git clone https://github.com/google-deepmind/formal-conjectures.git formal-conjectures
git -C formal-conjectures checkout --detach f606af160664c5a10952e9a957b100a1cd45bbeb
cd formal-conjectures
elan toolchain install leanprover/lean4:v4.33.1
lake +leanprover/lean4:v4.33.1 exe cache get
lake +leanprover/lean4:v4.33.1 build '+FormalConjectures.Wikipedia.BusyBeaver'
check_dir="$(mktemp -d)"
trap 'rm -f "$check_dir/Check.lean"; rmdir "$check_dir"' EXIT
printf "%s\n" "import Lean" > "$check_dir/Check.lean"
cat "$proof" >> "$check_dir/Check.lean"
cat >> "$check_dir/Check.lean" <<'SOLVE_CHECK'
example : type_of% @_root_.BusyBeaver.sanity_check := @_root_.Submission.solution
#print axioms Submission.solution
SOLVE_CHECK
lake +leanprover/lean4:v4.33.1 env lean "$check_dir/Check.lean"
)
Download the receipt verifier and public key. Run python3 verify-proof.py certificate.json 2bdb2402-7e12-4a64-b900-3d04407236a6.lean proof-key.pem with OpenSSL 3.
Relabel any Candidate machine onto Fin 2 and Fin n with default-preserving bijections, transfer haltingNumber, then bound via finitely many Fin machines.