Langlib

Langlib.Automata.Turing.Equivalence.TMToGrammar.Helpers

Helper Lemmas for TM → Grammar Construction #

This file contains the detailed helper lemmas needed to prove the correctness of the tmToGrammar construction.

Encoding TM configurations as sentential forms #

A TM0 configuration ⟨q, tape⟩ together with an "original input track" is encoded as a sentential form of the grammar. The encoding represents a finite window of the tape as a sequence of cell/headCell nonterminals flanked by boundary markers.

structure TwoTrackConfig {T Λ : Type} :

A "two-track tape segment" encodes a finite portion of the TM tape along with the original input at each position.

leftCells are the cells to the left of the head (in order from left to right). headState is the TM state at the head position. headOrig is the original input at the head position. headCur is the current tape content at the head position. rightCells are the cells to the right of the head (in order from left to right).

Each cell is a pair (original, current).

Instances For
    def encodeTwoTrack {T Λ : Type} (cfg : TwoTrackConfig) :

    Encode a TwoTrackConfig as a sentential form of the grammar.

    Equations
    • One or more equations did not get rendered due to their size.
    Instances For
      def initTwoTrack {T Λ : Type} [Inhabited Λ] (w : List T) :

      The initial two-track configuration for input w. The head is at the leftmost position (or on blank for empty input).

      Equations
      Instances For

        Phase 1: Generation #

        The grammar derives the encoding of the initial configuration from [start]:

        1. S → leftBound · genMore · rightBound
        2. Repeatedly: genMore → genMore · cell(tᵢ) (adding cells right-to-left)
        3. genMore → headCell(q₀, t₁) (or headCell(q₀, none, none) for empty input)

        Phase 3: Cleanup #

        After the TM halts, the grammar converts all nonterminals to terminals.

        def encodeHalted {T Λ : Type} (originals : List (Option T)) :

        Encode the "halted" configuration: all cells converted to haltCells.

        Equations
        • One or more equations did not get rendered due to their size.
        Instances For
          def extractInput {T : Type} (originals : List (Option T)) :

          Extract the original input from a list of originals (keep only some values).

          Equations
          Instances For
            theorem haltCells_to_terminals {T : Type} [Fintype T] {Λ : Type} [Inhabited Λ] [Fintype Λ] (M : Turing.TM0.Machine (Option T) Λ) (originals : List (Option T)) :
            theorem remove_boundaries {T : Type} [Fintype T] {Λ : Type} [Inhabited Λ] [Fintype Λ] (M : Turing.TM0.Machine (Option T) Λ) (originals : List (Option T)) :
            grammar_derives (tmToGrammar T Λ M) (encodeHalted originals) (List.map (fun (orig : Option T) => symbol.nonterminal (TMtoGrammarNT.haltCell orig)) originals)
            theorem cleanup_derives {T : Type} [Fintype T] {Λ : Type} [Inhabited Λ] [Fintype Λ] (M : Turing.TM0.Machine (Option T) Λ) (originals : List (Option T)) :

            Rule membership helpers #

            theorem gen_rule_mem {T : Type} [Fintype T] {Λ : Type} [Inhabited Λ] [Fintype Λ] (M : Turing.TM0.Machine (Option T) Λ) (r : grule T (TMtoGrammarNT T Λ)) (hr : r generationRules T Λ M) :
            r (tmToGrammar T Λ M).rules

            A rule from the generation rules is in the grammar.

            theorem sim_rule_mem {T : Type} [Fintype T] {Λ : Type} [Inhabited Λ] [Fintype Λ] (M : Turing.TM0.Machine (Option T) Λ) (r : grule T (TMtoGrammarNT T Λ)) (hr : r simulationRules T Λ M) :
            r (tmToGrammar T Λ M).rules

            A rule from the simulation rules is in the grammar.

            theorem cleanup_rule_mem {T : Type} [Fintype T] {Λ : Type} [Inhabited Λ] [Fintype Λ] (M : Turing.TM0.Machine (Option T) Λ) (r : grule T (TMtoGrammarNT T Λ)) (hr : r cleanupRules T Λ M) :
            r (tmToGrammar T Λ M).rules

            A rule from the cleanup rules is in the grammar.

            def twoTrackOriginals {T Λ : Type} (cfg : TwoTrackConfig) :

            Get the originals from a TwoTrackConfig.

            Equations
            Instances For

              Phase 2: Simulation #

              The simulation phase shows that each TM0 step can be mirrored by a grammar derivation step on the encoded sentential form.

              noncomputable def stepTwoTrack {T Λ : Type} [Inhabited Λ] (M : Turing.TM0.Machine (Option T) Λ) (cfg : TwoTrackConfig) :

              Compute the next TwoTrackConfig after one TM0 step. Returns none if the TM halts.

              Equations
              • One or more equations did not get rendered due to their size.
              Instances For
                theorem mem_allOptT {T : Type} [Fintype T] (x : Option T) :
                theorem mem_allΛ {Λ : Type} [Fintype Λ] (q : Λ) :
                q allΛ Λ
                theorem sim_write {T : Type} [Fintype T] {Λ : Type} [Inhabited Λ] [Fintype Λ] (M : Turing.TM0.Machine (Option T) Λ) (q q' : Λ) (orig γ γ' : Option T) (hM : M q γ = some (q', Turing.TM0.Stmt.write γ')) (prefix_ suffix_ : List (symbol T (TMtoGrammarNT T Λ))) :
                theorem sim_move_right {T : Type} [Fintype T] {Λ : Type} [Inhabited Λ] [Fintype Λ] (M : Turing.TM0.Machine (Option T) Λ) (q q' : Λ) (orig γ orig' γ' : Option T) (hM : M q γ = some (q', Turing.TM0.Stmt.move Turing.Dir.right)) (prefix_ suffix_ : List (symbol T (TMtoGrammarNT T Λ))) :
                theorem sim_move_left {T : Type} [Fintype T] {Λ : Type} [Inhabited Λ] [Fintype Λ] (M : Turing.TM0.Machine (Option T) Λ) (q q' : Λ) (orig γ orig'' γ'' : Option T) (hM : M q γ = some (q', Turing.TM0.Stmt.move Turing.Dir.left)) (prefix_ suffix_ : List (symbol T (TMtoGrammarNT T Λ))) :
                theorem simulation_one_step {T : Type} [Fintype T] {Λ : Type} [Inhabited Λ] [Fintype Λ] (M : Turing.TM0.Machine (Option T) Λ) (cfg cfg' : TwoTrackConfig) (hstep : stepTwoTrack M cfg = some cfg') :
                theorem simulation_multi_step {T : Type} [Fintype T] {Λ : Type} [Inhabited Λ] [Fintype Λ] (M : Turing.TM0.Machine (Option T) Λ) (cfg : TwoTrackConfig) (n : ) (cfg' : TwoTrackConfig) (hsteps : (fun (c : Option TwoTrackConfig) => c >>= stepTwoTrack M)^[n] (some cfg) = some cfg') :

                Phase 2.5: Halt conversion #

                When the TM halts, convert the two-track encoding to the halted encoding.

                theorem halt_headCell_to_haltCell {T : Type} [Fintype T] {Λ : Type} [Inhabited Λ] [Fintype Λ] (M : Turing.TM0.Machine (Option T) Λ) (q : Λ) (orig cur : Option T) (h_halts : M q cur = none) (prefix_ suffix_ : List (symbol T (TMtoGrammarNT T Λ))) :
                theorem propagate_halt_right {T : Type} [Fintype T] {Λ : Type} [Inhabited Λ] [Fintype Λ] (M : Turing.TM0.Machine (Option T) Λ) (cells : List (Option T × Option T)) (orig : Option T) (prefix_ suffix_ : List (symbol T (TMtoGrammarNT T Λ))) :
                grammar_derives (tmToGrammar T Λ M) (prefix_ ++ [symbol.nonterminal (TMtoGrammarNT.haltCell orig)] ++ List.map (fun (x : Option T × Option T) => match x with | (o, c) => symbol.nonterminal (TMtoGrammarNT.cell o c)) cells ++ suffix_) (prefix_ ++ [symbol.nonterminal (TMtoGrammarNT.haltCell orig)] ++ List.map (fun (x : Option T × Option T) => match x with | (o, snd) => symbol.nonterminal (TMtoGrammarNT.haltCell o)) cells ++ suffix_)
                theorem propagate_halt_left {T : Type} [Fintype T] {Λ : Type} [Inhabited Λ] [Fintype Λ] (M : Turing.TM0.Machine (Option T) Λ) (cells : List (Option T × Option T)) (anchor_orig : Option T) (prefix_ suffix_ : List (symbol T (TMtoGrammarNT T Λ))) :
                grammar_derives (tmToGrammar T Λ M) (prefix_ ++ List.map (fun (x : Option T × Option T) => match x with | (o, c) => symbol.nonterminal (TMtoGrammarNT.cell o c)) cells ++ [symbol.nonterminal (TMtoGrammarNT.haltCell anchor_orig)] ++ suffix_) (prefix_ ++ List.map (fun (x : Option T × Option T) => match x with | (o, snd) => symbol.nonterminal (TMtoGrammarNT.haltCell o)) cells ++ [symbol.nonterminal (TMtoGrammarNT.haltCell anchor_orig)] ++ suffix_)

                Composing all phases #

                Correspondence between TM0.Cfg and TwoTrackConfig #

                structure TMCorresponds {T Λ : Type} (tc : TwoTrackConfig) (tmCfg : Turing.TM0.Cfg (Option T) Λ) :

                A TwoTrackConfig corresponds to a TM0.Cfg if the head state, head symbol, and tape contents match (with the tape being blank beyond the TwoTrackConfig window).

                Instances For
                  theorem corresponds_step_none {T Λ : Type} [Inhabited Λ] (M : Turing.TM0.Machine (Option T) Λ) (tc : TwoTrackConfig) (tmCfg : Turing.TM0.Cfg (Option T) Λ) (hcorr : TMCorresponds tc tmCfg) (hhalt : Turing.TM0.step M tmCfg = none) :
                  theorem corresponds_step_some {T Λ : Type} [Inhabited Λ] (M : Turing.TM0.Machine (Option T) Λ) (tc : TwoTrackConfig) (tmCfg tmCfg' : Turing.TM0.Cfg (Option T) Λ) (hcorr : TMCorresponds tc tmCfg) (hstep : Turing.TM0.step M tmCfg = some tmCfg') :
                  ∃ (tc' : TwoTrackConfig), stepTwoTrack M tc = some tc' TMCorresponds tc' tmCfg'

                  Main Correctness Theorems #

                  theorem sim_reaches_halts {T : Type} [Fintype T] {Λ : Type} [Inhabited Λ] [Fintype Λ] (M : Turing.TM0.Machine (Option T) Λ) (tc : TwoTrackConfig) (tmCfg : Turing.TM0.Cfg (Option T) Λ) (hcorr : TMCorresponds tc tmCfg) (tmCfg_halt : Turing.TM0.Cfg (Option T) Λ) (hreaches : Turing.Reaches (Turing.TM0.step M) tmCfg tmCfg_halt) (hhalt : Turing.TM0.step M tmCfg_halt = none) :