Equivalence of Unrestricted Grammars and Turing Machines #
This file defines the grammar construction for simulating TM0 machines, and states the equivalence between unrestricted (Type-0) grammars and Turing machines.
The correctness proofs and the main equivalence theorem are in TMtoGrammarHelpers.lean.
Main definitions #
is_TM: A language is TM-recognizable if there exists a finite-state Turing machine (using Mathlib'sTuring.TM0model) that halts on inputwiffw ∈ L.TMtoGrammarNT: Nonterminal symbols for the grammar simulating a TM.tmToGrammar: The grammar that simulates a TM0 machine.
Construction #
We construct an unrestricted grammar that simulates a TM0 machine using "two-track" nonterminals.
Nonterminals for the grammar simulating a TM0 machine.
start: Initial symbolgenMore: Generation-phase marker for adding more input cellscell orig cur: Tape cell with original inputorigand current contentcurheadCell q orig cur: Head position in stateq, original inputorig, currentcurleftBound/rightBound: Tape boundary markershaltCell orig: Cell in cleanup phase, remembering original inputorig
- start {T Λ : Type} : TMtoGrammarNT T Λ
- genMore {T Λ : Type} : TMtoGrammarNT T Λ
- cell {T Λ : Type} : Option T → Option T → TMtoGrammarNT T Λ
- headCell {T Λ : Type} : Λ → Option T → Option T → TMtoGrammarNT T Λ
- leftBound {T Λ : Type} : TMtoGrammarNT T Λ
- rightBound {T Λ : Type} : TMtoGrammarNT T Λ
- haltCell {T Λ : Type} : Option T → TMtoGrammarNT T Λ
Instances For
Generation rules: nondeterministically create an initial TM configuration for an arbitrary input word.
The grammar builds the initial configuration from right to left:
S → leftBound · genMore · rightBound- Repeatedly:
genMore → genMore · cell(some t, some t)(adds cells to the RIGHT) - Finally:
genMore → headCell(q₀, some t, some t)orgenMore → headCell(q₀, none, none)(replaces genMore with the TM head, placing it at the leftmost position)
For input [t₁, t₂, ..., tₙ], the derivation is:
S → LB · genMore · RB
→ LB · genMore · cell(tₙ) · RB
→ ... → LB · genMore · cell(t₂) · ... · cell(tₙ) · RB
→ LB · headCell(q₀, t₁) · cell(t₂) · ... · cell(tₙ) · RB
Equations
- One or more equations did not get rendered due to their size.
Instances For
Simulation rules: encode TM transitions as grammar rules.
For each (q, γ) with M q γ = some (q', action):
- Write γ':
headCell(q, orig, γ) → headCell(q', orig, γ') - Move right:
headCell(q, orig, γ) cell(orig', γ') → cell(orig, γ) headCell(q', orig', γ')(with boundary extension when at the right edge) - Move left:
cell(o'', γ'') headCell(q, orig, γ) → headCell(q', o'', γ'') cell(orig, γ)(with boundary extension when at the left edge)
Equations
- One or more equations did not get rendered due to their size.
Instances For
Cleanup rules: when TM halts, extract original input as terminals.
- Convert halted head cell to halt marker
- Propagate halt to all remaining cells
- Replace halt markers with original terminal symbols (or ε for blanks)
- Remove boundary markers
Equations
- One or more equations did not get rendered due to their size.
Instances For
The grammar simulating TM0 machine M.
Equations
- tmToGrammar T Λ M = { nt := TMtoGrammarNT T Λ, initial := TMtoGrammarNT.start, rules := generationRules T Λ M ++ simulationRules T Λ M ++ cleanupRules T Λ M }