Nondeterministic Turing machines as finite structures
Lax904597.Machines · concepts/Lax904597/Machines.lean · lax-904597
No public endorsements yet.
Loading review…
Sign in with ORCIDNatural Language Statement
Definition
A nondeterministic Turing machine, together with its input, is presented as a finite structure: the universe holds the positions, the transitions, the states and the symbols, and relations record the order on positions, the attributes of each transition (the state it applies in, the symbol it reads, the state it moves to, the symbol it writes, the direction of the move), the start and accepting states, the blank symbol, and the input written on the tape.
Positions serve both as tape cells and as time steps, so the time bound is the number of positions, by construction and with no arithmetic. A configuration is a state, a head position and a tape; a step applies a transition at the head, and the machine accepts when some run from an initial configuration reaches an accepting state in fewer steps than there are positions. Well-formedness – the order is linear, there is a position, the input is functional and the blank symbol is unique – is folded into the yes-instances, since a relation symbol is not a linear order by itself.
Machine acceptance, the decision problem so defined, is the machine side of the Cook–Levin theorem. That it is isomorphism-invariant, which makes it a decision problem, is the one claim of this module: the proof transports runs along the isomorphism and belongs to the proofs of this submission, so the bundled problem takes the invariance proof as a parameter. By proof irrelevance the problem does not depend on which proof is supplied, and every statement about it is made for an arbitrary one.
Concept map
Lean source view on GitHub
| 1 | import Mathlib.ModelTheory.Semantics |
| 2 | import Mathlib.SetTheory.Cardinal.Finite |
| 3 | import Lax904597.Problems |
| 4 | |
| 5 | /-! |
| 6 | --- |
| 7 | title: Nondeterministic Turing machines as finite structures |
| 8 | type: definition |
| 9 | --- |
| 10 | A nondeterministic Turing machine, together with its input, is presented as |
| 11 | a finite structure: the universe holds the positions, the transitions, the |
| 12 | states and the symbols, and relations record the order on positions, the |
| 13 | attributes of each transition (the state it applies in, the symbol it |
| 14 | reads, the state it moves to, the symbol it writes, the direction of the |
| 15 | move), the start and accepting states, the blank symbol, and the input |
| 16 | written on the tape. |
| 17 | |
| 18 | Positions serve both as tape cells and as time steps, so the time bound is |
| 19 | the number of positions, by construction and with no arithmetic. A |
| 20 | configuration is a state, a head position and a tape; a step applies a |
| 21 | transition at the head, and the machine *accepts* when some run from an |
| 22 | initial configuration reaches an accepting state in fewer steps than there |
| 23 | are positions. Well-formedness – the order is linear, there is a position, |
| 24 | the input is functional and the blank symbol is unique – is folded into the |
| 25 | yes-instances, since a relation symbol is not a linear order by itself. |
| 26 | |
| 27 | Machine acceptance, the decision problem so defined, is the machine side of |
| 28 | the Cook–Levin theorem. That it is isomorphism-invariant, which makes it a |
| 29 | decision problem, is the one claim of this module: the proof transports runs |
| 30 | along the isomorphism and belongs to the proofs of this submission, so the |
| 31 | bundled problem takes the invariance proof as a parameter. By proof |
| 32 | irrelevance the problem |
| 33 | does not depend on which proof is supplied, and every statement about it is |
| 34 | made for an arbitrary one. |
| 35 | -/ |
| 36 | |
| 37 | namespace Lax904597.Machines |
| 38 | |
| 39 | open FirstOrder FirstOrder.Language FirstOrder.Language.Structure Lax904597.Problems |
| 40 | |
| 41 | variable {A : Type} |
| 42 | |
| 43 | /-- A binary relation is a linear order: reflexive, transitive, antisymmetric |
| 44 | and total. -/ |
| 45 | def IsLinOrd (Le : A → A → Prop) : Prop := |
| 46 | (∀ a, Le a a) ∧ (∀ a b c, Le a b → Le b c → Le a c) ∧ |
| 47 | (∀ a b, Le a b → Le b a → a = b) ∧ ∀ a b, Le a b ∨ Le b a |
| 48 | |
| 49 | /-- `p` is a lowest position. -/ |
| 50 | def MinPos (Le : A → A → Prop) (Posn : A → Prop) (p : A) : Prop := |
| 51 | Posn p ∧ ∀ q, Posn q → Le p q |
| 52 | |
| 53 | /-- `q` is the next position above `p`. -/ |
| 54 | def SuccPos (Le : A → A → Prop) (Posn : A → Prop) (p q : A) : Prop := |
| 55 | Posn p ∧ Posn q ∧ Le p q ∧ p ≠ q ∧ ∀ r, Posn r → Le p r → Le r q → r = p ∨ r = q |
| 56 | |
| 57 | /-- A configuration: the current state, the position of the head, and the |
| 58 | contents of the tape. -/ |
| 59 | @[ext] |
| 60 | structure Config (A : Type) where |
| 61 | /-- The current state. -/ |
| 62 | state : A |
| 63 | /-- The cell the head is on. -/ |
| 64 | head : A |
| 65 | /-- The symbol in each cell. -/ |
| 66 | tape : A → A |
| 67 | |
| 68 | /-- A Turing machine presented as relations on a universe: the sorts, the |
| 69 | marks, the attributes of the transitions, the initial tape and the order along |
| 70 | which the head moves. -/ |
| 71 | structure TMData (A : Type) where |
| 72 | /-- Being a position – a tape cell, and equally a time step. -/ |
| 73 | Posn : A → Prop |
| 74 | /-- The order on positions, along which the head moves. -/ |
| 75 | Le : A → A → Prop |
| 76 | /-- Being a transition. -/ |
| 77 | Tr : A → Prop |
| 78 | /-- Being a start state. -/ |
| 79 | Start : A → Prop |
| 80 | /-- Being an accepting state. -/ |
| 81 | Acc : A → Prop |
| 82 | /-- Being the blank symbol. -/ |
| 83 | Blank : A → Prop |
| 84 | /-- This transition moves the head right (rather than left). -/ |
| 85 | Right : A → Prop |
| 86 | /-- The state a transition applies in. -/ |
| 87 | Src : A → A → Prop |
| 88 | /-- The symbol a transition reads. -/ |
| 89 | Read : A → A → Prop |
| 90 | /-- The state a transition moves to. -/ |
| 91 | Dst : A → A → Prop |
| 92 | /-- The symbol a transition writes. -/ |
| 93 | Write : A → A → Prop |
| 94 | /-- The input: the symbol initially in a cell. -/ |
| 95 | Inp : A → A → Prop |
| 96 | |
| 97 | namespace TMData |
| 98 | |
| 99 | variable (M : TMData A) |
| 100 | |
| 101 | /-- The symbols a cell may initially hold: the input symbol where the input is |
| 102 | defined, the blank elsewhere. -/ |
| 103 | def InitTape (p a : A) : Prop := M.Inp p a ∨ ((∀ b, ¬ M.Inp p b) ∧ M.Blank a) |
| 104 | |
| 105 | /-- Being an initial configuration: a start state, the head on the lowest |
| 106 | position, and an initial tape. -/ |
| 107 | def IsInit (c : Config A) : Prop := |
| 108 | M.Start c.state ∧ MinPos M.Le M.Posn c.head ∧ ∀ p, M.InitTape p (c.tape p) |
| 109 | |
| 110 | /-- One step: some transition applies in the current state to the symbol under |
| 111 | the head; it writes in that cell, changes state, and moves the head to the |
| 112 | neighboring position in the direction it names. Other cells are unchanged, |
| 113 | and a move off the end of the tape is impossible, so the run stops there. -/ |
| 114 | def Step (c c' : Config A) : Prop := |
| 115 | ∃ τ, M.Tr τ ∧ M.Src τ c.state ∧ M.Read τ (c.tape c.head) ∧ |
| 116 | M.Dst τ c'.state ∧ M.Write τ (c'.tape c.head) ∧ |
| 117 | (∀ p, p ≠ c.head → c'.tape p = c.tape p) ∧ |
| 118 | ((M.Right τ ∧ SuccPos M.Le M.Posn c.head c'.head) ∨ |
| 119 | (¬ M.Right τ ∧ SuccPos M.Le M.Posn c'.head c.head)) |
| 120 | |
| 121 | /-- Reaching one configuration from another in exactly `n` steps. -/ |
| 122 | def StepsIn : ℕ → Config A → Config A → Prop |
| 123 | | 0, c, c' => c = c' |
| 124 | | n + 1, c, c' => ∃ d, M.Step c d ∧ StepsIn n d c' |
| 125 | |
| 126 | /-- Acceptance: some run from an initial configuration reaches an accepting |
| 127 | state in fewer steps than there are positions. -/ |
| 128 | def Accepts : Prop := |
| 129 | ∃ (c₀ c : Config A) (n : ℕ), M.IsInit c₀ ∧ n < Nat.card {p : A // M.Posn p} ∧ |
| 130 | M.StepsIn n c₀ c ∧ M.Acc c.state |
| 131 | |
| 132 | /-- Well-formedness: the order is linear, there is a position to start on, the |
| 133 | input is functional, and there is exactly one blank symbol. -/ |
| 134 | def WellFormed : Prop := |
| 135 | IsLinOrd M.Le ∧ (∃ p, M.Posn p) ∧ |
| 136 | (∀ p a b, M.Inp p a → M.Inp p b → a = b) ∧ |
| 137 | (∃ b, M.Blank b) ∧ (∀ a b, M.Blank a → M.Blank b → a = b) |
| 138 | |
| 139 | end TMData |
| 140 | |
| 141 | /-- Relation symbols of machine instances. -/ |
| 142 | inductive turingRel : ℕ → Type |
| 143 | /-- `posn p`: `p` is a position – a tape cell, and equally a time step. -/ |
| 144 | | posn : turingRel 1 |
| 145 | /-- `tr τ`: `τ` is a transition. -/ |
| 146 | | tr : turingRel 1 |
| 147 | /-- `start q`: `q` is a start state. -/ |
| 148 | | start : turingRel 1 |
| 149 | /-- `acc q`: `q` is an accepting state. -/ |
| 150 | | acc : turingRel 1 |
| 151 | /-- `blank a`: `a` is the blank symbol. -/ |
| 152 | | blank : turingRel 1 |
| 153 | /-- `right τ`: the transition `τ` moves the head right. -/ |
| 154 | | right : turingRel 1 |
| 155 | /-- `le p q`: the linear order along which the head moves. -/ |
| 156 | | le : turingRel 2 |
| 157 | /-- `tsrc τ q`: `τ` applies in the state `q`. -/ |
| 158 | | tsrc : turingRel 2 |
| 159 | /-- `tread τ a`: `τ` applies when reading the symbol `a`. -/ |
| 160 | | tread : turingRel 2 |
| 161 | /-- `tdst τ q`: `τ` moves to the state `q`. -/ |
| 162 | | tdst : turingRel 2 |
| 163 | /-- `twrite τ a`: `τ` writes the symbol `a`. -/ |
| 164 | | twrite : turingRel 2 |
| 165 | /-- `inp p a`: the cell `p` initially holds the symbol `a`. -/ |
| 166 | | inp : turingRel 2 |
| 167 | deriving DecidableEq |
| 168 | |
| 169 | /-- The relational language of machine instances: positions with their order, |
| 170 | transitions with their attributes, the distinguished states and symbol, and the |
| 171 | initial tape. -/ |
| 172 | def turing : Language := |
| 173 | ⟨fun _ => Empty, turingRel⟩ |
| 174 | |
| 175 | instance instIsRelationalTuring : IsRelational turing := fun _ => (inferInstance : IsEmpty Empty) |
| 176 | |
| 177 | /-- The position symbol. -/ |
| 178 | abbrev tmPosn : turing.Relations 1 := .posn |
| 179 | /-- The transition symbol. -/ |
| 180 | abbrev tmTr : turing.Relations 1 := .tr |
| 181 | /-- The start-state symbol. -/ |
| 182 | abbrev tmStart : turing.Relations 1 := .start |
| 183 | /-- The accepting-state symbol. -/ |
| 184 | abbrev tmAcc : turing.Relations 1 := .acc |
| 185 | /-- The blank symbol. -/ |
| 186 | abbrev tmBlank : turing.Relations 1 := .blank |
| 187 | /-- The move-right symbol. -/ |
| 188 | abbrev tmRight : turing.Relations 1 := .right |
| 189 | /-- The order symbol. -/ |
| 190 | abbrev tmLe : turing.Relations 2 := .le |
| 191 | /-- The transition-source symbol. -/ |
| 192 | abbrev tmSrc : turing.Relations 2 := .tsrc |
| 193 | /-- The transition-read symbol. -/ |
| 194 | abbrev tmRead : turing.Relations 2 := .tread |
| 195 | /-- The transition-destination symbol. -/ |
| 196 | abbrev tmDst : turing.Relations 2 := .tdst |
| 197 | /-- The transition-write symbol. -/ |
| 198 | abbrev tmWrite : turing.Relations 2 := .twrite |
| 199 | /-- The input symbol. -/ |
| 200 | abbrev tmInp : turing.Relations 2 := .inp |
| 201 | |
| 202 | section Shorthands |
| 203 | |
| 204 | variable [turing.Structure A] |
| 205 | |
| 206 | /-- Being a position. -/ |
| 207 | def TMPosn (a : A) : Prop := RelMap tmPosn ![a] |
| 208 | /-- Being a transition. -/ |
| 209 | def TMTr (a : A) : Prop := RelMap tmTr ![a] |
| 210 | /-- Being a start state. -/ |
| 211 | def TMStart (a : A) : Prop := RelMap tmStart ![a] |
| 212 | /-- Being an accepting state. -/ |
| 213 | def TMAcc (a : A) : Prop := RelMap tmAcc ![a] |
| 214 | /-- Being the blank symbol. -/ |
| 215 | def TMBlank (a : A) : Prop := RelMap tmBlank ![a] |
| 216 | /-- Moving the head right. -/ |
| 217 | def TMRight (a : A) : Prop := RelMap tmRight ![a] |
| 218 | /-- The order on positions. -/ |
| 219 | def TMLe (a b : A) : Prop := RelMap tmLe ![a, b] |
| 220 | /-- The state a transition applies in. -/ |
| 221 | def TMSrc (a b : A) : Prop := RelMap tmSrc ![a, b] |
| 222 | /-- The symbol a transition reads. -/ |
| 223 | def TMRead (a b : A) : Prop := RelMap tmRead ![a, b] |
| 224 | /-- The state a transition moves to. -/ |
| 225 | def TMDst (a b : A) : Prop := RelMap tmDst ![a, b] |
| 226 | /-- The symbol a transition writes. -/ |
| 227 | def TMWrite (a b : A) : Prop := RelMap tmWrite ![a, b] |
| 228 | /-- The initial contents of a cell. -/ |
| 229 | def TMInp (a b : A) : Prop := RelMap tmInp ![a, b] |
| 230 | |
| 231 | end Shorthands |
| 232 | |
| 233 | /-- The machine an instance describes. -/ |
| 234 | def tmData (A : Type) [turing.Structure A] : TMData A where |
| 235 | Posn := TMPosn |
| 236 | Le := TMLe |
| 237 | Tr := TMTr |
| 238 | Start := TMStart |
| 239 | Acc := TMAcc |
| 240 | Blank := TMBlank |
| 241 | Right := TMRight |
| 242 | Src := TMSrc |
| 243 | Read := TMRead |
| 244 | Dst := TMDst |
| 245 | Write := TMWrite |
| 246 | Inp := TMInp |
| 247 | |
| 248 | /-- Well-formed acceptance is isomorphism-invariant: the statement. -/ |
| 249 | def NTMAcceptInvariant : Prop := |
| 250 | ∀ {A B : Type} [turing.Structure A] [turing.Structure B], (A ≃[turing] B) → |
| 251 | (((tmData A).WellFormed ∧ (tmData A).Accepts) ↔ ((tmData B).WellFormed ∧ (tmData B).Accepts)) |
| 252 | |
| 253 | /-- Well-formed acceptance is isomorphism-invariant: an isomorphism of |
| 254 | instances transports every symbol, hence runs and their acceptance. -/ |
| 255 | axiom ntmAccept_iso : NTMAcceptInvariant |
| 256 | |
| 257 | /-- Machine acceptance: does the well-formed machine described by the instance |
| 258 | accept its input in fewer steps than there are positions? Bundled with any |
| 259 | proof `h` of its invariance, `ntmAccept_iso` for one; the problem does not |
| 260 | depend on `h`. -/ |
| 261 | def NTMAccept (h : NTMAcceptInvariant) : DecisionProblem turing where |
| 262 | Holds := fun A _ => (tmData A).WellFormed ∧ (tmData A).Accepts |
| 263 | iso_invariant := h |
| 264 | |
| 265 | end Lax904597.Machines |
| 266 |
Builds on
Used by
Discussion
Ask a question or add context. Endorsements and structured flags are kept in the review panel above.
0 comments