Soundness of the TM → Grammar Construction #
This file proves that the grammar tmToGrammar constructed from a TM0 machine is
sound: it only generates words on which the TM halts.
Proof strategy #
We define a "terminal content" function terminalContent that extracts, from each symbol
in a sentential form, the original input character it represents. This function is
invariant under all simulation and cleanup grammar rules (but increases during generation).
We then define a forward invariant GI with five constructors:
initial: the start symbolgenerating: generation phasesimulating: simulation phase (tracks TM configuration correspondence)cleanup: post-halt cleanup (tracksterminalContent = wand TM halts onw)done: terminal-only form
The key insight is that cleanup uses terminalContent instead of tracking a derivation
to a terminal form. This avoids the non-confluence issue in the cleanup phase.
Terminal content #
Extract the "original input character" from a grammar symbol.
Terminals contribute their value. Nonterminals contribute their orig field (if some).
Boundary markers, start, genMore, and none-orig nonterminals contribute nothing.
Equations
- symbolOriginal (symbol.terminal t) = some t
- symbolOriginal (symbol.nonterminal (TMtoGrammarNT.cell (some t) a)) = some t
- symbolOriginal (symbol.nonterminal (TMtoGrammarNT.headCell a (some t) a_1)) = some t
- symbolOriginal (symbol.nonterminal (TMtoGrammarNT.haltCell (some t))) = some t
- symbolOriginal x✝ = none
Instances For
The "terminal content" of a sentential form: the list of original input characters. This is invariant under simulation and cleanup rules.
Equations
Instances For
Terminal-only forms can't be transformed #
Terminal-only forms can't be transformed.
Derivation from terminal form is trivial.
No rule has cell as input_N.
The corrected forward invariant #
Whether a symbol is a nonterminal.
Equations
Instances For
The forward invariant for the tmToGrammar construction.
This invariant tracks five phases:
- initial: The start symbol
[S]. - generating: Building the initial configuration
[LB, genMore, cells..., RB]. - simulating: Simulating TM steps. Tracks correspondence with actual TM configs.
- cleanup: Post-halt phase. Tracks
terminalContent = wwhere TM halts onw. - done: Terminal-only form
w.map terminalwhere TM halts onw.
- initial {T Λ : Type} [Inhabited Λ] {M : Turing.TM0.Machine (Option T) Λ} : GI M [symbol.nonterminal TMtoGrammarNT.start]
- generating {T Λ : Type} [Inhabited Λ] {M : Turing.TM0.Machine (Option T) Λ} (ts : List T) : GI M ([symbol.nonterminal TMtoGrammarNT.leftBound, symbol.nonterminal TMtoGrammarNT.genMore] ++ List.map (fun (t : T) => symbol.nonterminal (TMtoGrammarNT.cell (some t) (some t))) ts ++ [symbol.nonterminal TMtoGrammarNT.rightBound])
- simulating {T Λ : Type} [Inhabited Λ] {M : Turing.TM0.Machine (Option T) Λ} (tc : TwoTrackConfig) (tmCfg : Turing.TM0.Cfg (Option T) Λ) (hcorr : TMCorresponds tc tmCfg) (hreach : Turing.Reaches (Turing.TM0.step M) (Turing.TM0.init (List.map some (extractInput (twoTrackOriginals tc)))) tmCfg) : GI M (encodeTwoTrack tc)
- cleanup {T Λ : Type} [Inhabited Λ] {M : Turing.TM0.Machine (Option T) Λ} (sf : List (symbol T (TMtoGrammarNT T Λ))) (w : List T) (hhalt : (Turing.TM0.eval M (List.map some w)).Dom) (hcontent : terminalContent sf = w) (hhas_nt : ∃ s ∈ sf, isNonterminal s) (hno_start : ∀ s ∈ sf, s ≠ symbol.nonterminal TMtoGrammarNT.start) (hno_genMore : ∀ s ∈ sf, s ≠ symbol.nonterminal TMtoGrammarNT.genMore) (hno_headCell : ∀ s ∈ sf, ∀ (q : Λ) (orig cur : Option T), s ≠ symbol.nonterminal (TMtoGrammarNT.headCell q orig cur)) : GI M sf
- done {T Λ : Type} [Inhabited Λ] {M : Turing.TM0.Machine (Option T) Λ} (w : List T) (hhalt : (Turing.TM0.eval M (List.map some w)).Dom) : GI M (List.map symbol.terminal w)
Instances For
Terminal halts #
Preservation lemmas #
Detailed classification of simulation rules matching headCell q orig cur when M q cur = some (q', action).
When a simulation rule fires on encodeTwoTrack tc, the result is encodeTwoTrack tc' for some tc' with stepTwoTrack M tc = some tc'.
Invariant preserved from simulating state.
Invariant preserved from done state (vacuously true).
The invariant is preserved by grammar transforms.
Every reachable form satisfies the invariant.
Main soundness theorem #
Soundness: If the grammar generates w, then TM halts on w.
The grammar constructed from a TM generates exactly the TM's language.