Documentation

LeanPool.BeyondBethe.Complexitylib.Models.TuringMachine.Combinators.Defs

Turing-machine combinator definitions #

State spaces, tape layouts, transition functions, and start-marker invariants for union, complement, branching, sequencing, looping, scanning, and input retargeting. The public Combinators module documents and re-exports these constructions.

Intermediate states between Phase 1 and Phase 2 of the union machine.

Instances For
    @[instance_reducible]
    Equations
    @[instance_reducible]
    Equations
    • One or more equations did not get rendered due to their size.
    @[reducible, inline]
    abbrev Complexity.TM.UnionQ (Q₁ Q₂ : Type) :

    The state type for the union TM.

    Equations
    Instances For
      def Complexity.TM.fakeOutIdx {n₁ n₂ : ℕ} :
      Fin (n₁ + 1 + n₂)

      Index of the fake output tape (work tape n₁).

      Equations
      Instances For
        def Complexity.TM.phase1WorkReads {n₁ n₂ : ℕ} (wHeads : Fin (n₁ + 1 + n₂) → Γ) (i : Fin n₁) :

        Read tm₁'s work tapes from the composite work tapes.

        Equations
        Instances For
          def Complexity.TM.phase2WorkReads {n₁ n₂ : ℕ} (wHeads : Fin (n₁ + 1 + n₂) → Γ) (j : Fin n₂) :

          Read tm₂'s work tapes from the composite work tapes.

          Equations
          Instances For
            def Complexity.TM.unionTM {n₁ n₂ : ℕ} (tm₁ : TM n₁) (tm₂ : TM n₂) :
            TM (n₁ + 1 + n₂)

            Construct a TM deciding L₁ ∪ L₂ from TMs deciding L₁ and L₂.

            The composite machine has n₁ + 1 + n₂ work tapes:

            • 0 .. n₁-1 for tm₁'s work tapes
            • n₁ for tm₁'s redirected output
            • n₁+1 .. n₁+n₂ for tm₂'s work tapes
            Equations
            • One or more equations did not get rendered due to their size.
            Instances For

              Intermediate states for the complement machine's output-flipping phase.

              Instances For
                @[reducible, inline]

                The state type for the complement TM.

                Equations
                Instances For
                  def Complexity.TM.complementTM {n : ℕ} (tm : TM n) :
                  TM n

                  Construct a TM deciding Lᶜ from a TM deciding L.

                  The complement machine has the same number of work tapes as the original. It runs in three stages:

                  1. Simulate: Run the original TM. When it halts, transition to rewind.
                  2. Rewind: Move the output head left to ▷ (cell 0), then right to cell 1.
                  3. Flip: Read output cell 1, write the flipped bit, and halt.
                  Equations
                  • One or more equations did not get rendered due to their size.
                  Instances For

                    Intermediate states for the conditional branching machine.

                    Instances For
                      @[instance_reducible]
                      Equations
                      @[reducible, inline]
                      abbrev Complexity.TM.IfQ (QT QThen QElse : Type) :

                      The state type for the conditional branching TM.

                      Equations
                      Instances For
                        def Complexity.TM.ifTM {n : ℕ} (tmTest tmThen tmElse : TM n) :
                        TM n

                        Conditional branching: run tmTest to completion, read its output at cell 1, then run tmThen (if output = Γ.one) or tmElse (otherwise).

                        All three machines share the same n work tapes, input tape, and output tape. Work tape contents are preserved across all transitions via readBackWrite, maintaining shared state for the branch machines.

                        Phases #

                        1. Test: Simulate tmTest. When it halts, enter rewind.
                        2. Rewind output: Move output head left to ▷ (cell 0).
                        3. Check: Move output head right to cell 1, read the test result. If Γ.one, enter tmThen.qstart. Otherwise, enter tmElse.qstart.
                        4. Branch: Simulate tmThen or tmElse. When the branch machine halts, transition to the done halt state.

                        Time #

                        t_test + (output_head_pos + 2) + 1 + t_branch + 1 where output_head_pos ≤ t_test. Total: at most 2·t_test + t_branch + 4.

                        Equations
                        • One or more equations did not get rendered due to their size.
                        Instances For
                          @[reducible, inline]
                          abbrev Complexity.TM.SeqQ (Q₁ Q₂ : Type) :

                          The state type for the sequential composition TM.

                          Equations
                          Instances For
                            def Complexity.TM.seqTM {n : ℕ} (tm₁ tm₂ : TM n) :
                            TM n

                            Sequential composition: run tm₁ to completion, then tm₂ on the same tapes.

                            Both machines share the same n work tapes, input tape, and output tape. When tm₁ halts, there is one transition step that:

                            • Changes state from Q₁ to Q₂ (entering tm₂.qstart)
                            • Preserves all tape cell contents (via readBackWrite)
                            • Moves any tape head at position 0 to position 1 (forced by δ_right_of_start)
                            • Leaves all other tape head positions unchanged

                            After the transition, tm₂ runs from the resulting tape state. Total time: t₁ + 1 + t₂ where t₁ and t₂ are the run times of tm₁ and tm₂ respectively.

                            Equations
                            • One or more equations did not get rendered due to their size.
                            Instances For

                              Intermediate states for the loop machine's output-checking phase.

                              Instances For
                                @[instance_reducible]
                                Equations
                                @[reducible, inline]
                                abbrev Complexity.TM.LoopQ (QBody QTest : Type) :

                                The state type for the loop TM.

                                Equations
                                Instances For
                                  def Complexity.TM.loopTM {n : ℕ} (tmBody tmTest : TM n) :
                                  TM n

                                  Loop combinator: repeatedly run tmBody then tmTest, halting when the test's output at cell 1 is Γ.one.

                                  Both machines share the same n work tapes, input tape, and output tape. Work tape contents are preserved across transitions (via readBackWrite), allowing the body to accumulate state across iterations.

                                  Phases #

                                  1. Body: Simulate tmBody. When it halts, transition to test.
                                  2. Test: Simulate tmTest. When it halts, enter rewind.
                                  3. Rewind output: Move output head left to ▷ (cell 0).
                                  4. Check: Move right to cell 1, read the test result. If Γ.one, enter done (halt). Otherwise, transition back to body.

                                  Use case #

                                  The UTM's main loop: loopTM simStepTM checkHaltTM runs one simulation step, then checks if the simulated machine has halted.

                                  Equations
                                  • One or more equations did not get rendered due to their size.
                                  Instances For

                                    Control states of a generic finite-state scanner parameterized by a user-supplied scan-state type S. The machine has a one-time start step that advances off cell 0, then a stream of scan s states holding the current scan state, and finally a done halt state.

                                    Instances For
                                      @[instance_reducible]
                                      Equations
                                      • One or more equations did not get rendered due to their size.
                                      def Complexity.TM.scannerTM {S : Type} [DecidableEq S] [Fintype S] (s₀ : S) (scanStep : S → Bool → S) (finalOutput : S → Γw) :
                                      TM 0

                                      Generic finite-state scanner.

                                      A 0-work-tape TM parameterized by a scan-state type S, an initial state s₀, a transition function scanStep : S → Bool → S (called on each input bit), and a finalizer finalOutput : S → Γw (the symbol to emit when end-of-input is reached).

                                      Semantics: runs left-to-right through the input, folding scanStep over the bits starting from s₀; when a blank is reached, writes finalOutput (finalState) to output cell 1 and halts.

                                      This captures the common "read once, fold into a fixed-size state" pattern used by evenLength, allZeros/allOnes, containsZero/containsOne, lengthDivBy k, lastBit, and similar regular-language scanners.

                                      Halts in |x| + 2 steps on every input (1 start + |x| scans + 1 halt).

                                      Equations
                                      • One or more equations did not get rendered due to their size.
                                      Instances For
                                        def Complexity.TM.retargetInput {k : ℕ} (M : TM k) :
                                        TM (k + 1)

                                        Given a DTM M : TM k, construct a DTM retargetInput M : TM (k + 1) that behaves like M but reads its "input" from work tape k (the last work tape) instead of the real input tape.

                                        Tape layout:

                                        • Real input tape: ignored (moved idly each step, never read).
                                        • Work tapes 0..k-1: mirror M's work tapes.
                                        • Work tape k: plays the role of M's input tape (read-only, no writes except a no-op readBackWrite that preserves cells).

                                        When work tape k is initialized with Tape.init (z.map Γ.ofBool), the machine simulates M on input z.

                                        Used in witnessLang NTM constructions where the verifier DTM's "input" (e.g. pair(x, y)) is built on a work tape rather than supplied on the real input tape.

                                        Equations
                                        • One or more equations did not get rendered due to their size.
                                        Instances For