Documentation

Atlas.TheoryOfComputation.code.SelfReference

A self-contained description of a Turing machine over the tape alphabet Γ. Bundles the (existential) state type Q, its DecidableEq instance, the underlying TM, and an optional explicit source/encoding source : List Γ used by certain concrete encodings.

Instances For

    The language L(d) recognized by the Turing machine packaged inside d.

    Instances For

      M.haltsWithOutput w out says that running M.machine on input w reaches a halt configuration after some number of steps n, and at that point the first out.length cells of the tape spell out out. This is the "halt with out on the tape" predicate used to formalize the printing/output behavior of TMs.

      Instances For
        structure SelfReference.TMEncoding (Γ : Type) :

        A TMEncoding packages the data Sipser uses to set up the self-reproducing TM construction:

        • encode : turns a TM description into its string code ⟨M⟩;
        • printTMDesc w : the "printer" machine P_w that on any input halts with w on the tape (printTMDesc_spec);
        • q_computable : the function q(w) = ⟨P_w⟩ is computable. This is the formal counterpart of the textbook Lemma giving the computable function q : Σ* → Σ* with q(w) = ⟨P_w⟩.
        Instances For

          IsMinimalTM enc d says d is a minimal Turing machine for its language under the encoding enc: no strictly shorter description d' recognizes the same language. Formally, for every d', |⟨d'⟩| < |⟨d⟩| implies L(d') ≠ L(d).

          Instances For
            def SelfReference.MIN_TM {Γ : Type} (enc : TMEncoding Γ) :
            Set (List Γ)

            MIN_TM = {⟨M⟩ | M is a minimal TM}: the set of codes of minimal Turing machines under the encoding enc. This is the language whose Turing-unrecognizability is shown via the Recursion Theorem.

            Instances For

              The data needed to build Sipser's self-reproducing TM SELF. Bundles:

              • an encoding encode : TMDescriptionList Γ;
              • the printer-machine constructor printTM together with its halting spec;
              • a sequential compose operator that runs A then B (output of A becomes input of B) and whose code is some concatenation combineDesc (encode A) (encode B);
              • a "quine helper" quineB that on input input halts with ⟨P_input⟩ ⟨quineB⟩ on the tape. This is exactly the structure used in the proof that some TM SELF prints its own description on every input.
              Instances For

                The computable map q(w) := ⟨P_w⟩ inside a SelfRefFramework: it sends a string w to the code of the printer-machine that outputs w.

                Instances For

                  An extension of SelfRefFramework exposing a concrete second-stage machine machineB whose halting spec is stated directly in terms of the framework's function q, matching the textbook construction B(input) = ⟨P_q(input)⟩ ⟨machineB⟩.

                  Instances For
                    theorem SelfReference.exists_self_reproducing_tm {Γ : Type} (F : SelfRefFramework Γ) :
                    ∃ (M : TMDescription Γ), ∀ (w : List Γ), M.haltsWithOutput w (F.encode M)

                    Self-Reproducing TM (Sipser, Lecture 11). Given a SelfRefFramework F, there exists a TM SELF such that on every input w, SELF halts with ⟨SELF⟩ on the tape. The witness is SELF := compose (printTM ⟨quineB⟩) quineB.

                    structure SelfReference.TapeOutputFixedPoint {Γ : Type} (encode : TMDescription ΓList Γ) :

                    A "tape-output" fixed-point witness: a TM fixedTM whose output on input is g input ⟨fixedTM⟩. This packages the self-referential ability to depend on one's own code at the tape level, parameterized by an external two-argument map g.

                    Instances For
                      structure TuringMachine.TMCoding (Γ : Type) :

                      An abstract acceptable indexing ("coding") of Turing machines over alphabet Γ, sufficient to formalize the Recursion / Fixed-point theorems and the MIN_TM unrecognizability proof. It records:

                      • an index type TMIndex with an encode/decode pair and language assignment;
                      • a pairing function pair on strings;
                      • an smn function with the standard s-m-n specification;
                      • a notion IsComputable of computable index transformations, closed under the needed operations (composition, applying smn, and the diagonal x ↦ smn x x);
                      • representable: every computable h : TMIndexTMIndex is "implemented" by some index e₀, i.e. languageOf (smn e₀ x) = languageOf (h x);
                      • languages_beyond_length: arbitrarily long codes are needed to span all languages — there exist indices whose language differs from all shorter ones;
                      • recognizable_enumSearch_computable: a recognizable set of codes plus an enumeration witness t that strictly increases code-length yields a computable t.
                      Instances For
                        theorem TuringMachine.recursion_theorem {Γ : Type} (C : TMCoding Γ) (t : C.TMIndexC.TMIndex) (ht : C.IsComputable t) :
                        ∃ (R : C.TMIndex), C.languageOf R = C.languageOf (t R)

                        Recursion Theorem (language form). For any computable transformation t : TMIndexTMIndex of TM indices, there exists R whose language equals the language of t R. This is the language-level avatar of Sipser's statement that for any TM T there is a TM R such that R on w behaves like T on ⟨w, R⟩.

                        theorem TuringMachine.fixed_point_theorem {Γ : Type} (C : TMCoding Γ) (f : List ΓList Γ) (_hf : IsComputableFunction f) (hf_maps_to_valid : ∀ (M : C.TMIndex), ∃ (S : C.TMIndex), C.decode (f (C.encode M)) = some S) (ht_computable : ∀ (t : C.TMIndexC.TMIndex), (∀ (M : C.TMIndex), C.decode (f (C.encode M)) = some (t M))C.IsComputable t) :
                        ∃ (R : C.TMIndex) (S : C.TMIndex), C.decode (f (C.encode R)) = some S C.languageOf R = C.languageOf S

                        Fixed-point Theorem (Sipser, Lecture 11, Ex 2). For any computable function f : Σ* → Σ* (with codomain landing in valid encodings), there is a TM R and a TM S with f(⟨R⟩) = ⟨S⟩ and L(R) = L(S). In other words, every computable transformation on TM descriptions has a "fixed point" up to language equality.

                        def TuringMachine.PrintsAndHalts {Γ Q : Type} [DecidableEq Q] (M : TM Q Γ) (w : List Γ) :

                        PrintsAndHalts M w says: started on the empty tape, M reaches an accept configuration after some n steps, with cells 0, …, |w|-1 holding the symbols of w and everywhere else the blank symbol. Captures "M halts having printed exactly w".

                        Instances For
                          def TuringMachine.PrintsAndHaltsOnInput {Γ Q : Type} [DecidableEq Q] (M : TM Q Γ) (input w : List Γ) :

                          Same as PrintsAndHalts but starting from an arbitrary input instead of the blank tape: after some n steps M accepts and the first |w| tape cells contain w.

                          Instances For
                            noncomputable def TuringMachine.printTM {Γ : Type} (blank : Γ) (inputAlpha : Set Γ) (h_blank : blankinputAlpha) (w : List Γ) :
                            TM (Fin (w.length + 2)) Γ

                            The concrete printer-machine P_w for a string w: a TM with |w| + 2 states (one per character, plus an accept and a reject state) that on any input writes w[0], w[1], …, w[|w|-1] left-to-right and accepts. This is the explicit construction underlying the computable function q(w) = ⟨P_w⟩.

                            Instances For
                              theorem TuringMachine.printTM_invariant_any_input {Γ : Type} [DecidableEq Γ] (blank : Γ) (inputAlpha : Set Γ) (h_blank : blankinputAlpha) (w input : List Γ) (k : ) (hk : k w.length) :
                              ((printTM blank inputAlpha h_blank w).runOnInput input k).state = k, ((printTM blank inputAlpha h_blank w).runOnInput input k).headPos = k ∀ (j : ) (hj : j < k), ((printTM blank inputAlpha h_blank w).runOnInput input k).tape j = w[j]

                              Key loop invariant of printTM: after k ≤ |w| steps on any input, the machine is in state k, the head is at position k, and tape cells 0, …, k-1 already hold the first k symbols of w. Proved by induction on k.

                              theorem TuringMachine.printTM_haltsWithOutput {Γ : Type} [DecidableEq Γ] (blank : Γ) (inputAlpha : Set Γ) (h_blank : blankinputAlpha) (w input : List Γ) :
                              ∃ (n : ), (printTM blank inputAlpha h_blank w).isHaltConfig ((printTM blank inputAlpha h_blank w).runOnInput input n) ((printTM blank inputAlpha h_blank w).runOnInput input n).readTape w.length = w

                              Correctness of printTM: on any input, after exactly |w| steps the printer machine P_w halts (in fact accepts) and the first |w| tape cells (read off by Config.readTape) spell out w. This realizes the haltsWithOutput spec demanded by TMEncoding.

                              theorem TuringMachine.computable_q_is_computable {Γ : Type} (enc : SelfReference.TMEncoding Γ) :
                              ∃ (q : List ΓList Γ), IsComputableFunction q ∀ (w : List Γ), ∃ (Pw : SelfReference.TMDescription Γ), q w = enc.encode Pw ∀ (input : List Γ), Pw.haltsWithOutput input w

                              Abstract form of the textbook Lemma: from any TMEncoding enc we get a computable function q : Σ* → Σ* with q(w) = ⟨P_w⟩, where P_w is a TM that on every input halts with w on the tape.

                              noncomputable def TuringMachine.concreteTMEncoding {Γ : Type} [DecidableEq Γ] (blank : Γ) (inputAlpha : Set Γ) (h_blank : blankinputAlpha) :

                              A concrete instance of TMEncoding built from the explicit printTM construction. The encoding of a description is taken to be its stored source field, and printTMDesc w packages the printTM w machine together with source := w, so q(w) = encode(printTMDesc w) = w is the identity, which is clearly computable.

                              Instances For
                                theorem TuringMachine.computable_q {Γ : Type} [DecidableEq Γ] (blank : Γ) (inputAlpha : Set Γ) (h_blank : blankinputAlpha) :
                                ∃ (q : List ΓList Γ), IsComputableFunction q ∀ (w : List Γ), ∃ (Pw : SelfReference.TMDescription Γ), q w = (concreteTMEncoding blank inputAlpha h_blank).encode Pw ∀ (input : List Γ), Pw.haltsWithOutput input w

                                Lemma (Computable function q) (Sipser, Lecture 11): instantiated with the concrete encoding. There is a computable q : Σ* → Σ* such that for every w the description P_w := printTMDesc w satisfies q w = ⟨P_w⟩ and on any input P_w halts with w on the tape.

                                Minimality in the TMCoding setting: M is minimal if no strictly shorter-coded index M' has the same language.

                                Instances For

                                  MIN_TM = {⟨M⟩ | M is a minimal TM} expressed in the TMCoding framework.

                                  Instances For
                                    theorem TuringMachine.exists_minimal_for_language {Γ : Type} (C : TMCoding Γ) (M : C.TMIndex) :
                                    ∃ (M' : C.TMIndex), C.languageOf M' = C.languageOf M IsMinimalIndex C M'

                                    Every TM index M has a minimal equivalent: there exists M' with the same language as M such that M' is minimal. Proved by strong induction on the code-length, descending to a shorter equivalent whenever M itself is not minimal.

                                    MIN_TM contains codes of arbitrarily large length: for every n there is a minimal index B with |⟨B⟩| ≥ n. Combines languages_beyond_length with exists_minimal_for_language (a shorter minimal equivalent could not differ from all length-< n indices).

                                    theorem TuringMachine.min_tm_arbitrarily_long_strict {Γ : Type} (C : TMCoding Γ) (n : ) :
                                    ∃ (B : C.TMIndex), IsMinimalIndex C B n < (C.encode B).length

                                    Strict version of min_tm_has_arbitrarily_long_elements: for every n some minimal index B has |⟨B⟩| > n.

                                    MIN_TM is T-unrecognizable (Sipser, Lecture 11, Ex 3). The language of codes of minimal Turing machines is not Turing-recognizable. The proof is by contradiction via the Recursion Theorem: if MIN_TM were recognizable, an enumeration would produce a computable map t sending each M to a strictly longer minimal machine, but then any fixed point R of t from the Recursion Theorem (with L(R) = L(t R)) would witness t R not being minimal — a contradiction.

                                    A TMCoding augmented with a computable "complement" operation compl mapping each index M to an index whose language is the set complement of L(M). Useful for applying the Recursion Theorem to constructions that flip acceptance.

                                    Instances For

                                      A_TM is undecidable — new proof via the Recursion Theorem (Sipser, Lecture 11, Ex 1). Suppose H decided A_TM. Let D be H with its accept and reject states swapped (so D decides the complement of A_TM). By the self-reference property of enc, there is a description d₀ whose computation behaves like running D on ⟨d₀⟩, giving d₀.accepts ↔ ¬ d₀.accepts, contradiction.

                                      The complement \overline{A_{TM}} of the acceptance language A_TM. This language is famously not Turing-recognizable and is the one whose "unprovability" gives Gödel-style results in this development.

                                      Instances For

                                        A minimal abstract "formal proof system" over a TM-encoding enc. It picks out a Turing-recognizable set provable ⊆ Σ* of provable statements about TM descriptions, with the soundness assumption that if ⟨d⟩ ∈ provable then d does not accept (the statement "d is non-accepting" is true).

                                        Instances For

                                          A FormalProofSystem is complete if every true non-acceptance statement is provable: whenever d does not accept, ⟨d⟩ ∈ provable. Gödel's First Incompleteness Theorem says no sound, recognizable system can be complete in this sense.

                                          Instances For
                                            theorem GodelIncompleteness.godel_first_incompleteness {Γ : Type} (enc : TuringMachine.TMEncoding Γ) (provable : Set (List Γ)) (h_provable_recognizable : TuringMachine.IsTuringRecognizable provable) (h_sound : ∀ (d : TuringMachine.TMDesc Γ), enc.encode d provable¬d.accepts) :
                                            ∃ (d : TuringMachine.TMDesc Γ), ¬d.accepts enc.encode dprovable

                                            Gödel's First Incompleteness Theorem (Sipser, Lecture 11). For any sound proof system whose set of provable statements about TMs is Turing-recognizable, there is a true statement that is unprovable: there exists a description d such that d does not accept (true statement) yet ⟨d⟩ ∉ provable.

                                            The proof uses the self-reference property of the encoding enc to build d₀ whose machine effectively simulates the recognizer P of provable on ⟨d₀⟩; soundness then forces d₀ to be non-accepting and unprovable.

                                            theorem GodelIncompleteness.true_but_unprovable_statement {Γ : Type} (enc : TuringMachine.TMEncoding Γ) (provable : Set (List Γ)) (h_sound : ∀ (d : TuringMachine.TMDesc Γ), enc.encode d provable¬d.accepts) {QP : Type} [DecidableEq QP] (P : TuringMachine.TM QP Γ) (hP : P.language = provable) :
                                            ∃ (d : TuringMachine.TMDesc Γ), ¬d.accepts enc.encode dprovable

                                            A True but Unprovable Statement (Sipser, Lecture 11). Concrete version of Gödel's theorem: given an explicit TM P whose language is provable and soundness of provable, there is a description d₀ such that (1) d₀ does not accept (the statement ⟨d₀⟩ ∈ \overline{A_{TM}} is true) and (2) ⟨d₀⟩ ∉ provable (it has no proof). The witness d₀ corresponds to Sipser's machine R which on any input obtains its own code, forms the statement φ_U = "⟨R, 0⟩ ∈ \overline{A_{TM}}", and searches for a proof of φ_U.