Datasets:
File size: 8,424 Bytes
8ca48d7 | 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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 | /-
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
public import FormalConjecturesForMathlib.Computability.TuringMachine.PostTuringMachine
public import Mathlib.Computability.TuringMachine.StackTuringMachine
public import Mathlib.Data.ENat.Lattice
@[expose] public section
/-! # Turing Machines, Busy Beaver version.
A variant on the definition of the TM0 model in Mathlib: while the statements the
`TM` can make in the usual `TM0` are split into two categories (either write at the
current position or move left/right), we want to combine the two to stick to the
`BB` convention: first write at the current position and then move left/right.
Note that this Turing Machine model also works with states of type `Option Γ`. This
is because in the busy beaver context, the turing machines also have an addition
"halting" state
See also https://git.sr.ht/~vigoux/busybeaver for a different approach to formalising
these objects.
-/
namespace Turing
open Mathlib
open Relation StateTransition
open Nat
namespace BusyBeaver
-- type of tape symbols
variable (Γ : Type*)
-- type of "labels" or TM states
variable (Λ : Type*)
/-- A Turing machine "statement" is just a command to write a symbol on the tape
(at the current position) and then move left or right. -/
structure Stmt where write ::
symbol : Γ
dir : Dir
deriving Inhabited
/-- A Post-Turing machine with symbol type `Γ` and label type `Λ`
is a function which, given the current state `q : Λ` and
the tape head `a : Γ`, either halts (returns `none`) or returns
a new state `q' : Option Λ` and a `Stmt` describing what to do: a
command to write a symbol and move left or right. Notice that there
are two ways of halting at a given `(state, head)` pair: either
the machine halts immediately (i.e. the function returns `none`),
or the machine moves to the "halting state", i.e. `none : Option Λ`
and performs one last action.
Typically, both `Λ` and `Γ` are required to be inhabited; the default value
for `Γ` is the "blank" tape value, and the default value of `Λ` is
the initial state. -/
@[nolint unusedArguments]
def Machine [Inhabited Λ] :=
Λ → Γ → Option (Option Λ × Stmt Γ)
instance Machine.inhabited [Inhabited Λ] : Inhabited (Machine Γ Λ) := by
unfold Machine; infer_instance
/-- The configuration state of a Turing machine during operation
consists of a label (machine state), and a tape.
The tape is represented in the form `(a, L, R)`, meaning the tape
looks like `L.rev ++ [a] ++ R` with the machine currently reading
the `a`. The lists are automatically extended with blanks as the
machine moves around. -/
@[ext]
structure Cfg [Inhabited Γ] where
/-- The current machine state. -/
q : Option Λ
/-- The current state of the tape: current symbol, left and right parts. -/
tape : Tape Γ
variable {Γ Λ}
variable [Inhabited Λ]
variable [Inhabited Γ]
instance Cfg.inhabited : Inhabited (Cfg Γ Λ) := ⟨⟨default, default⟩⟩
namespace Machine
/-- Execution semantics of the Turing machine. -/
def step (M : Machine Γ Λ) : Cfg Γ Λ → Option (Cfg Γ Λ)
| ⟨none, _⟩ => none
| ⟨some q, T⟩ => (M q T.1).map
fun ⟨q', a⟩ ↦ ⟨q', match a with
| Stmt.write a d => (T.write a).move d⟩
/-- The statement `Reaches M s₁ s₂` means that `s₂` is obtained
starting from `s₁` after a finite number of steps from `s₂`. -/
def Reaches (M : Machine Γ Λ) : Cfg Γ Λ → Cfg Γ Λ → Prop := ReflTransGen fun a b ↦ b ∈ step M a
/-- The initial configuration. -/
def init (l : List Γ) : Cfg Γ Λ := ⟨some default, Tape.mk₁ l⟩
/-- Evaluate a Turing machine on initial input to a final state,
if it terminates. -/
def eval (M : Machine Γ Λ) (l : List Γ) : Part (ListBlank Γ) :=
(StateTransition.eval (step M) (init l)).map fun c ↦ c.tape.right₀
def multiStep (M : Machine Γ Λ) (config : Cfg Γ Λ) (n : ℕ) : Option (Cfg Γ Λ) :=
(Option.bind · (step M))^[n] config
@[simp]
lemma multiStep_zero (M : Machine Γ Λ) (config : Cfg Γ Λ) : M.multiStep config 0 = some config :=
rfl
@[simp]
lemma multiStep_one (M : Machine Γ Λ) (config : Cfg Γ Λ) : M.multiStep config 1 = M.step config :=
rfl
@[simp]
lemma multiStep_succ (M : Machine Γ Λ) (config : Cfg Γ Λ) (n : ℕ) :
M.multiStep config (n + 1) = Option.bind (M.multiStep config n) M.step := by
rw [multiStep, Function.iterate_succ', Function.comp_apply, multiStep]
@[simp]
lemma multiStep_eq_none_mono {M : Machine Γ Λ} {config : Cfg Γ Λ} {m n : ℕ}
(H : M.multiStep config n = none) (hnm : n ≤ m) :
M.multiStep config m = none := by
induction hnm with
| refl => exact H
| @step m hnm H =>
rw [multiStep_succ, H]
rfl
variable {Γ Λ : Type*} [Inhabited Λ] [Inhabited Γ]
variable (M : Machine Γ Λ)
/--
`M.IsHaltingInput l` is the predicate that `M` is a halting configuration for `M`.
-/
def IsHaltingInput (l : List Γ) : Prop := (eval M l).Dom
-- TODO(Paul-Lez): Do we actually need this?
/--
`M.HaltsAtConfiguration s` is the predicate that `M` is a halting configuration for `M`.
-/
def IsHaltingConfiguration (s : Cfg Γ Λ) : Prop := (step M s).isNone
/--
The property that a Turing Machine `M` eventually halts when starting from an empty tape
-/
class IsHalting : Prop where
halts : M.IsHaltingInput []
/--
The predicate that a machine starting at configuration `s` stops after at most `n` steps, i.e.
reaches a configuration from which there are no defined transitions.
-/
def HaltsAfter (s : Cfg Γ Λ) (n : ℕ) : Prop :=
M.multiStep s (n+1) = none
lemma haltsAfter_zero_iff (s : Cfg Γ Λ) :
HaltsAfter M s 0 ↔ step M s = none := by
rw [HaltsAfter, multiStep, Function.iterate_one, Option.bind_some]
lemma isHalting_iff_exists_haltsAt : IsHalting M ↔ ∃ n, M.HaltsAfter (init []) n :=
⟨fun _ ↦ eval_dom_iff.mpr IsHalting.halts, fun H ↦ ⟨eval_dom_iff.mp H⟩⟩
lemma exists_of_not_haltsAfter (s : Cfg Γ Λ) (n : ℕ) (H : ¬M.HaltsAfter s n) :
∃ (a : Λ) (b : Tape Γ), M.multiStep s n = some ⟨a, b⟩ := by
contrapose! H
rw [HaltsAfter, multiStep_succ]
obtain H | ⟨⟨u, u'⟩, hu⟩ := (Option.eq_none_or_eq_some (M.multiStep s n))
· simp [H]
· suffices u = none by rw [hu, this] ; rfl
rw [Option.eq_none_iff_forall_ne_some]
intro a hc
subst hc
simp [hu] at H ⊢
lemma not_isHalting_iff_forall_isSome_multiStep :
¬ IsHalting M ↔ ∀ n, M.multiStep (init []) (n + 1) |>.isSome := by
simp_rw [isHalting_iff_exists_haltsAt, HaltsAfter, Option.isSome_iff_ne_none]
push Not
rfl
lemma not_isHalting_of_forall_isSome (H : ∀ l s, ∃ a b, M l s = some (some a, b)) :
¬IsHalting M := by
rw [not_isHalting_iff_forall_isSome_multiStep]
intro n
induction n with
| zero =>
obtain ⟨a, b, H⟩ := H default (Tape.mk₁ []).head
simp [init, step, H]
| succ n ih =>
obtain ⟨a, b, hab⟩ := exists_of_not_haltsAfter _ _ _ (by rwa [Option.isSome_iff_ne_none] at ih)
obtain ⟨c, d, hcd⟩ := H a b.head
obtain ⟨e, f, hef⟩ := H c (Tape.move d.dir (Tape.write d.symbol b)).head
simp [multiStep_succ, multiStep_succ, hab, step, hcd, hef]
noncomputable def haltingNumber : ENat :=
-- The smallest `n` such that `M` halts after `n` steps when starting from an empty tape.
-- If no such `n` exists then this is equal to `⊤`.
sInf {(n : ENat) | (n : ℕ) (_ : HaltsAfter M (init []) n) }
theorem haltingNumber_def (n : ℕ) (hn : ∃ a, M.multiStep (init []) n = some a)
(ha' : M.multiStep (init []) (n + 1) = none) :
M.haltingNumber = n := by
refine IsGLB.sInf_eq (IsLeast.isGLB ⟨⟨n, by rwa [HaltsAfter], rfl⟩, fun m ⟨k, _, _⟩ ↦ ?_⟩)
cases m
· exact le_top
· by_contra! hc
simp_all [multiStep_eq_none_mono ‹_› (show k + 1 ≤ n by aesop)]
end Machine
end BusyBeaver
end Turing
|