Documentation

Complexitylib.Models.TuringMachine.Combinators

TM Combinators #

This file provides TM constructions for composing machines, used to prove closure properties of complexity classes.

Main definitions #

Design #

The union machine has three phases:

  1. Phase 1: Simulate tm₁, redirecting its output to work tape n₁ (a "fake output" tape). The real output tape stays pristine.
  2. Transition: Rewind the fake output to cell 1 and check the result. If Γ.one (tm₁ accepted), write Γ.one to the real output and halt. Otherwise rewind the input tape and reset Phase-2 tapes to cell 0.
  3. Phase 2: Simulate tm₂ using work tapes n₁+1..n₁+n₂ and the real output tape.

Work tape layout (0-indexed) #

State space #

Q₁ ⊕ UnionPhase ⊕ Q₂ where UnionPhase encodes the four transition states between Phase 1 and Phase 2.

Direction for an idle tape: move right if reading , else stay. Satisfies δ_right_of_start for tapes not involved in the current phase.

Equations
Instances For

    Direction for a tape we want to move left: move left unless reading , in which case move right to satisfy δ_right_of_start. During actual execution the tape won't be at cell 0, so this always moves left.

    Equations
    Instances For

      idleDir moves right when reading the start symbol .

      If the head reads , then idleDir moves right — the shape of the δ_right_of_start obligation for idle tapes.

      If the head reads , then moveLeftDir moves right — the shape of the δ_right_of_start obligation for tapes being rewound.

      def Complexity.TM.allIdle {σ : Type} {k : } (newState : σ) (iHead : Γ) (wHeads : Fin kΓ) (oHead : Γ) :
      σ × (Fin kΓw) × Γw × Dir3 × (Fin kDir3) × Dir3

      The "do nothing" transition output: all writes are , all directions are idleDir. Used for states that only change the control state.

      Equations
      • One or more equations did not get rendered due to their size.
      Instances For
        theorem Complexity.TM.rightOfStart_allIdle {k : } (iHead : Γ) (wHeads : Fin kΓ) (oHead : Γ) :
        (iHead = Γ.startidleDir iHead = Dir3.right) (∀ (i : Fin k), wHeads i = Γ.startidleDir (wHeads i) = Dir3.right) (oHead = Γ.startidleDir oHead = Dir3.right)

        Proof that all-idle directions satisfy δ_right_of_start.

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

        Instances For
          @[implicit_reducible]
          Equations
          @[implicit_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
                      @[implicit_reducible]
                      Equations
                      • One or more equations did not get rendered due to their size.
                      @[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
                            @[implicit_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
                                      @[implicit_reducible]
                                      Equations
                                      @[implicit_reducible]
                                      Equations
                                      • One or more equations did not get rendered due to their size.
                                      @[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
                                            @[implicit_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 : SBoolS) (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