Lax434930.SpaceMachines
Finite Turing machines with a read-only input tape
concepts/Lax434930/SpaceMachines.lean · lax-434930
No public endorsements yet.
Loading review…
Sign in with ORCIDConcept map
Definition
A machine has finitely many control states, a finite work alphabet with a blank symbol, a read-only binary input tape between distinct endmarkers, and one initially blank, semi-infinite work tape. Both heads start at position zero, which is the input's left endmarker and the work tape's leftmost cell. One transition reads the two scanned symbols, writes one work symbol, changes state, and moves each head by at most one cell. An outward move at a tape boundary leaves that head in place.
The finite transition table gives a finite set of possible actions for each state and pair of scanned symbols. A deterministic machine has at most one action in every such set. A configuration with no successor is terminal, and its control state supplies an accept/reject bit. Acceptance means that some computation branch reaches an accepting terminal configuration. A decider has a finite bound on the lengths of all branches for each input, and accepts exactly the strings in its language. This bound need not be computable or satisfy any specified time bound.
Only the work tape is charged as space. Bounding its head below at every reachable configuration bounds every visited cell to the prefix , including blank cells and cells later erased. The input head is confined to positions and cannot act as an unbounded free counter. The transition table sees neither head position nor the input length; it sees only the control state and scanned symbols.
Lean source view on GitHub
| 1 | import Lax434930.PolynomialTime |
| 2 | import Mathlib.Data.Finset.Basic |
| 3 | |
| 4 | /-! |
| 5 | --- |
| 6 | title: Finite Turing machines with a read-only input tape |
| 7 | type: definition |
| 8 | --- |
| 9 | A machine has finitely many control states, a finite work alphabet with a |
| 10 | blank symbol, a read-only binary input tape between distinct endmarkers, |
| 11 | and one initially blank, semi-infinite work tape. Both heads start at |
| 12 | position zero, which is the input's left endmarker and the work tape's |
| 13 | leftmost cell. One transition reads the two scanned symbols, writes one |
| 14 | work symbol, changes state, and moves each head by at most one cell. |
| 15 | An outward move at a tape boundary leaves that head in place. |
| 16 | |
| 17 | The finite transition table gives a finite set of possible actions for |
| 18 | each state and pair of scanned symbols. A deterministic machine has at |
| 19 | most one action in every such set. A configuration with no successor is |
| 20 | terminal, and its control state supplies an accept/reject bit. Acceptance |
| 21 | means that some computation branch reaches an accepting terminal |
| 22 | configuration. A decider has a finite bound on the lengths of all branches |
| 23 | for each input, and accepts exactly the strings in its language. This |
| 24 | bound need not be computable or satisfy any specified time bound. |
| 25 | |
| 26 | Only the work tape is charged as space. Bounding its head below at |
| 27 | every reachable configuration bounds every visited cell to the prefix |
| 28 | , including blank cells and cells later erased. The input |
| 29 | head is confined to positions and cannot act as an unbounded free |
| 30 | counter. The transition table sees neither head position nor the input |
| 31 | length; it sees only the control state and scanned symbols. |
| 32 | -/ |
| 33 | |
| 34 | namespace Lax434930.SpaceMachines |
| 35 | |
| 36 | open PolynomialTime |
| 37 | |
| 38 | /-- The input alphabet, with two distinct endmarkers. -/ |
| 39 | inductive InputSymbol |
| 40 | | leftEnd |
| 41 | | bit (value : Bool) |
| 42 | | rightEnd |
| 43 | deriving DecidableEq, Fintype |
| 44 | |
| 45 | /-- An elementary head movement, including staying in place. -/ |
| 46 | inductive Move |
| 47 | | left |
| 48 | | stay |
| 49 | | right |
| 50 | deriving DecidableEq, Fintype |
| 51 | |
| 52 | /-- Move a head on a semi-infinite tape, keeping it at zero on a leftward exit. -/ |
| 53 | def Move.apply : Move → ℕ → ℕ |
| 54 | | .left, i => i - 1 |
| 55 | | .stay, i => i |
| 56 | | .right, i => i + 1 |
| 57 | |
| 58 | /-- Read the immutable input at a head position, including its endmarkers. -/ |
| 59 | def readInput (w : Word) : ℕ → InputSymbol |
| 60 | | 0 => .leftEnd |
| 61 | | i + 1 => match w[i]? with |
| 62 | | some b => .bit b |
| 63 | | none => .rightEnd |
| 64 | |
| 65 | /-- One local transition: change state, write a work symbol, and move the heads. -/ |
| 66 | structure Action (Γ Q : Type) where |
| 67 | state : Q |
| 68 | write : Γ |
| 69 | inputMove : Move |
| 70 | workMove : Move |
| 71 | |
| 72 | /-- The complete finite description of a possibly nondeterministic machine. -/ |
| 73 | structure Machine where |
| 74 | Γ : Type |
| 75 | Q : Type |
| 76 | [alphabet : Fintype Γ] |
| 77 | [control : Fintype Q] |
| 78 | blank : Γ |
| 79 | start : Q |
| 80 | transition : Q → InputSymbol → Γ → Finset (Action Γ Q) |
| 81 | accept : Q → Bool |
| 82 | |
| 83 | attribute [instance] Machine.alphabet Machine.control |
| 84 | |
| 85 | /-- The two head positions, control state, and work-tape contents. -/ |
| 86 | structure Configuration (Γ Q : Type) where |
| 87 | state : Q |
| 88 | inputHead : ℕ |
| 89 | workHead : ℕ |
| 90 | tape : ℕ → Γ |
| 91 | |
| 92 | /-- Configurations for a fixed machine. Only reachable configurations are used. -/ |
| 93 | abbrev Machine.Config (M : Machine) := Configuration M.Γ M.Q |
| 94 | |
| 95 | /-- The work tape is blank; the input is supplied separately to the step relation. -/ |
| 96 | def Machine.initial (M : Machine) : M.Config := |
| 97 | ⟨M.start, 0, 0, fun _ => M.blank⟩ |
| 98 | |
| 99 | /-- Execute one action. The input head is clipped to the two endmarkers. -/ |
| 100 | def Machine.execute (M : Machine) (w : Word) (c : M.Config) |
| 101 | (a : Action M.Γ M.Q) : M.Config := |
| 102 | ⟨a.state, min (a.inputMove.apply c.inputHead) (w.length + 1), |
| 103 | a.workMove.apply c.workHead, Function.update c.tape c.workHead a.write⟩ |
| 104 | |
| 105 | /-- One legal transition consults only the state and the two scanned symbols. -/ |
| 106 | def Machine.Step (M : Machine) (w : Word) (c d : M.Config) : Prop := |
| 107 | ∃ a ∈ M.transition c.state (readInput w c.inputHead) (c.tape c.workHead), |
| 108 | d = M.execute w c a |
| 109 | |
| 110 | /-- A computation prefix consisting of exactly the indicated number of transitions. -/ |
| 111 | inductive Machine.Run (M : Machine) (w : Word) : ℕ → M.Config → Prop |
| 112 | | zero : M.Run w 0 M.initial |
| 113 | | succ {n : ℕ} {c d : M.Config} : |
| 114 | M.Run w n c → M.Step w c d → M.Run w (n + 1) d |
| 115 | |
| 116 | /-- No choice of action is available at a terminal configuration. -/ |
| 117 | def Machine.Terminal (M : Machine) (w : Word) (c : M.Config) : Prop := |
| 118 | ∀ d : M.Config, ¬ M.Step w c d |
| 119 | |
| 120 | /-- All computation branches on this input have bounded finite length. -/ |
| 121 | def Machine.HaltsOn (M : Machine) (w : Word) : Prop := |
| 122 | ∃ t : ℕ, ∀ (n : ℕ) (c : M.Config), M.Run w n c → n ≤ t |
| 123 | |
| 124 | /-- Existential acceptance at a terminal configuration. -/ |
| 125 | def Machine.Accepts (M : Machine) (w : Word) : Prop := |
| 126 | ∃ (n : ℕ) (c : M.Config), M.Run w n c ∧ M.Terminal w c ∧ M.accept c.state = true |
| 127 | |
| 128 | /-- A decider halts on every branch and accepts exactly its language. -/ |
| 129 | def Machine.Decides (M : Machine) (A : Language) : Prop := |
| 130 | ∀ w : Word, M.HaltsOn w ∧ (M.Accepts w ↔ w ∈ A) |
| 131 | |
| 132 | /-- The transition table has at most one action for each local observation. -/ |
| 133 | def Machine.Deterministic (M : Machine) : Prop := |
| 134 | ∀ (q : M.Q) (i : InputSymbol) (b : M.Γ), |
| 135 | ∀ a ∈ M.transition q i b, ∀ a' ∈ M.transition q i b, a = a' |
| 136 | |
| 137 | /-- Every branch stays within the first `s` work cells, whether blank or nonblank. -/ |
| 138 | def Machine.UsesSpace (M : Machine) (w : Word) (s : ℕ) : Prop := |
| 139 | ∀ (n : ℕ) (c : M.Config), M.Run w n c → c.workHead < s |
| 140 | |
| 141 | end Lax434930.SpaceMachines |
| 142 |
Builds on
From Mathlib
Community review
Discussion
Ask a question or add context. Endorsements and structured flags are kept in the review panel above; your ORCID profile must share a public name.
0 comments