Nondeterministic Turing machines as finite structures

Lax904597.Machines · concepts/Lax904597/Machines.lean · lax-904597

proven

Loading review…

Sign in with ORCID

Community review

Flags

Each flag is tied to a public ORCID identity and explains why this concept may be incorrect.

No flags have been submitted.

    Community review

    Flag this concept

    State precisely what appears incorrect. This explanation will be public under your ORCID name.

    No source line selected.

    Natural 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
    2 concepts; 1 descendant hidden
    100%
    Proven claimDefinitionThis conceptRelated conceptA → B: B builds on A
    Evidence

    Each proof establishes this claim relative to its assumptions.

    Lean source view on GitHub

    1import Mathlib.ModelTheory.Semantics
    2import Mathlib.SetTheory.Cardinal.Finite
    3import Lax904597.Problems
    4
    5/-!
    6---
    7title: Nondeterministic Turing machines as finite structures
    8type: definition
    9---
    10A nondeterministic Turing machine, together with its input, is presented as
    11a finite structure: the universe holds the positions, the transitions, the
    12states and the symbols, and relations record the order on positions, the
    13attributes of each transition (the state it applies in, the symbol it
    14reads, the state it moves to, the symbol it writes, the direction of the
    15move), the start and accepting states, the blank symbol, and the input
    16written on the tape.
    17
    18Positions serve both as tape cells and as time steps, so the time bound is
    19the number of positions, by construction and with no arithmetic. A
    20configuration is a state, a head position and a tape; a step applies a
    21transition at the head, and the machine *accepts* when some run from an
    22initial configuration reaches an accepting state in fewer steps than there
    23are positions. Well-formedness – the order is linear, there is a position,
    24the input is functional and the blank symbol is unique – is folded into the
    25yes-instances, since a relation symbol is not a linear order by itself.
    26
    27Machine acceptance, the decision problem so defined, is the machine side of
    28the Cook–Levin theorem. That it is isomorphism-invariant, which makes it a
    29decision problem, is the one claim of this module: the proof transports runs
    30along the isomorphism and belongs to the proofs of this submission, so the
    31bundled problem takes the invariance proof as a parameter. By proof
    32irrelevance the problem
    33does not depend on which proof is supplied, and every statement about it is
    34made for an arbitrary one.
    35-/
    36
    37namespace Lax904597.Machines
    38
    39open FirstOrder FirstOrder.Language FirstOrder.Language.Structure Lax904597.Problems
    40
    41variable {A : Type}
    42
    43/-- A binary relation is a linear order: reflexive, transitive, antisymmetric
    44and total. -/
    45def 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. -/
    50def 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`. -/
    54def 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
    58contents of the tape. -/
    59@[ext]
    60structure 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
    69marks, the attributes of the transitions, the initial tape and the order along
    70which the head moves. -/
    71structure 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
    97namespace TMData
    98
    99variable (M : TMData A)
    100
    101/-- The symbols a cell may initially hold: the input symbol where the input is
    102defined, the blank elsewhere. -/
    103def 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
    106position, and an initial tape. -/
    107def 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
    111the head; it writes in that cell, changes state, and moves the head to the
    112neighboring position in the direction it names. Other cells are unchanged,
    113and a move off the end of the tape is impossible, so the run stops there. -/
    114def 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. -/
    122def 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
    127state in fewer steps than there are positions. -/
    128def 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
    133input is functional, and there is exactly one blank symbol. -/
    134def 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
    139end TMData
    140
    141/-- Relation symbols of machine instances. -/
    142inductive 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,
    170transitions with their attributes, the distinguished states and symbol, and the
    171initial tape. -/
    172def turing : Language :=
    173 ⟨fun _ => Empty, turingRel⟩
    174
    175instance instIsRelationalTuring : IsRelational turing := fun _ => (inferInstance : IsEmpty Empty)
    176
    177/-- The position symbol. -/
    178abbrev tmPosn : turing.Relations 1 := .posn
    179/-- The transition symbol. -/
    180abbrev tmTr : turing.Relations 1 := .tr
    181/-- The start-state symbol. -/
    182abbrev tmStart : turing.Relations 1 := .start
    183/-- The accepting-state symbol. -/
    184abbrev tmAcc : turing.Relations 1 := .acc
    185/-- The blank symbol. -/
    186abbrev tmBlank : turing.Relations 1 := .blank
    187/-- The move-right symbol. -/
    188abbrev tmRight : turing.Relations 1 := .right
    189/-- The order symbol. -/
    190abbrev tmLe : turing.Relations 2 := .le
    191/-- The transition-source symbol. -/
    192abbrev tmSrc : turing.Relations 2 := .tsrc
    193/-- The transition-read symbol. -/
    194abbrev tmRead : turing.Relations 2 := .tread
    195/-- The transition-destination symbol. -/
    196abbrev tmDst : turing.Relations 2 := .tdst
    197/-- The transition-write symbol. -/
    198abbrev tmWrite : turing.Relations 2 := .twrite
    199/-- The input symbol. -/
    200abbrev tmInp : turing.Relations 2 := .inp
    201
    202section Shorthands
    203
    204variable [turing.Structure A]
    205
    206/-- Being a position. -/
    207def TMPosn (a : A) : Prop := RelMap tmPosn ![a]
    208/-- Being a transition. -/
    209def TMTr (a : A) : Prop := RelMap tmTr ![a]
    210/-- Being a start state. -/
    211def TMStart (a : A) : Prop := RelMap tmStart ![a]
    212/-- Being an accepting state. -/
    213def TMAcc (a : A) : Prop := RelMap tmAcc ![a]
    214/-- Being the blank symbol. -/
    215def TMBlank (a : A) : Prop := RelMap tmBlank ![a]
    216/-- Moving the head right. -/
    217def TMRight (a : A) : Prop := RelMap tmRight ![a]
    218/-- The order on positions. -/
    219def TMLe (a b : A) : Prop := RelMap tmLe ![a, b]
    220/-- The state a transition applies in. -/
    221def TMSrc (a b : A) : Prop := RelMap tmSrc ![a, b]
    222/-- The symbol a transition reads. -/
    223def TMRead (a b : A) : Prop := RelMap tmRead ![a, b]
    224/-- The state a transition moves to. -/
    225def TMDst (a b : A) : Prop := RelMap tmDst ![a, b]
    226/-- The symbol a transition writes. -/
    227def TMWrite (a b : A) : Prop := RelMap tmWrite ![a, b]
    228/-- The initial contents of a cell. -/
    229def TMInp (a b : A) : Prop := RelMap tmInp ![a, b]
    230
    231end Shorthands
    232
    233/-- The machine an instance describes. -/
    234def 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. -/
    249def 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
    254instances transports every symbol, hence runs and their acceptance. -/
    255axiom ntmAccept_iso : NTMAcceptInvariant
    256
    257/-- Machine acceptance: does the well-formed machine described by the instance
    258accept its input in fewer steps than there are positions? Bundled with any
    259proof `h` of its invariance, `ntmAccept_iso` for one; the problem does not
    260depend on `h`. -/
    261def NTMAccept (h : NTMAcceptInvariant) : DecisionProblem turing where
    262 Holds := fun A _ => (tmData A).WellFormed ∧ (tmData A).Accepts
    263 iso_invariant := h
    264
    265end Lax904597.Machines
    266
    Show Proof

    Discussion

    Ask a question or add context. Endorsements and structured flags are kept in the review panel above.

    Loading discussion…