Langlib

Langlib.Automata.DeterministicLinearBounded.Definition

Deterministic Linearly Bounded Automata #

A deterministic linearly bounded automaton (DLBA) is a deterministic Turing machine whose read/write head is restricted to the portion of the tape containing the input. This makes the configuration space finite, yielding powerful decidability results that do not hold for unrestricted Turing machines.

We adapt Mathlib's Turing machine framework (Turing.TM0, Turing.TM1) by replacing the infinite Turing.Tape with a fixed-length BoundedTape.

Main Definitions #

Main Results #

References #

Implementation Notes #

We use Fin (n + 1) to index tape cells, guaranteeing at least one cell exists. Head movement is clamped at boundaries: moving left at position 0 or right at the last position leaves the head in place. This is the standard convention for DLBAs, where boundary markers prevent the head from leaving the input region.

Direction #

inductive DLBA.Dir :

Direction for head movement. We include stay since the head is clamped at tape boundaries and this simplifies the definition of moveHead.

Instances For
    Equations
    Equations
    Instances For

      Bounded Tape #

      structure DLBA.BoundedTape (Γ : Type u_1) (n : ) :
      Type u_1

      A bounded tape of n + 1 cells over alphabet Γ, with a read/write head. This replaces the infinite Turing.Tape used in unrestricted Turing machines. The tape always has at least one cell (indexed by Fin (n + 1)).

      • contents : Fin (n + 1)Γ

        Contents of each tape cell

      • head : Fin (n + 1)

        Current head position

      Instances For
        def DLBA.instDecidableEqBoundedTape.decEq {Γ✝ : Type u_1} {n✝ : } [DecidableEq Γ✝] (x✝ x✝¹ : BoundedTape Γ✝ n✝) :
        Decidable (x✝ = x✝¹)
        Equations
        Instances For
          def DLBA.BoundedTape.read {Γ : Type u_1} {n : } (t : BoundedTape Γ n) :
          Γ

          Read the symbol under the head.

          Equations
          Instances For
            def DLBA.BoundedTape.write {Γ : Type u_1} {n : } (t : BoundedTape Γ n) (a : Γ) :

            Write a symbol at the current head position.

            Equations
            Instances For
              def DLBA.BoundedTape.moveHead {Γ : Type u_1} {n : } (t : BoundedTape Γ n) (d : Dir) :

              Move the head, clamping at boundaries (left end = 0, right end = n).

              Equations
              Instances For
                noncomputable instance DLBA.BoundedTape.instFintype {Γ : Type u_1} {n : } [Fintype Γ] :
                Equations

                Machine Definition #

                structure DLBA.Machine (Γ : Type u_1) (Λ : Type u_2) :
                Type (max u_1 u_2)

                A deterministic linearly bounded automaton.

                • Γ is the tape alphabet (must include a blank symbol, but we keep it general)
                • Λ is the finite set of states
                • transition maps (state, symbol) to (new_state, symbol_to_write, direction), or none to signal halting.
                • accept determines which states are accepting (used to define the recognized language).
                • initial is the start state.
                • transition : ΛΓOption (Λ × Γ × Dir)

                  Transition function. Returns none to halt.

                • accept : ΛBool

                  Which states are accepting (decidable predicate).

                • initial : Λ

                  The initial state.

                Instances For

                  Configuration #

                  structure DLBA.Cfg (Γ : Type u_1) (Λ : Type u_2) (n : ) :
                  Type (max u_1 u_2)

                  A configuration of a DLBA running on a tape of n + 1 cells.

                  • state : Λ

                    Current state of the machine.

                  • tape : BoundedTape Γ n

                    Current tape contents and head position.

                  Instances For
                    def DLBA.instDecidableEqCfg.decEq {Γ✝ : Type u_1} {Λ✝ : Type u_2} {n✝ : } [DecidableEq Γ✝] [DecidableEq Λ✝] (x✝ x✝¹ : Cfg Γ✝ Λ✝ n✝) :
                    Decidable (x✝ = x✝¹)
                    Equations
                    Instances For
                      instance DLBA.instDecidableEqCfg {Γ✝ : Type u_1} {Λ✝ : Type u_2} {n✝ : } [DecidableEq Γ✝] [DecidableEq Λ✝] :
                      DecidableEq (Cfg Γ✝ Λ✝ n✝)
                      Equations
                      noncomputable instance DLBA.Cfg.instFintype {Γ : Type u_1} {Λ : Type u_2} {n : } [Fintype Γ] [Fintype Λ] :
                      Fintype (Cfg Γ Λ n)
                      Equations

                      Step Function #

                      def DLBA.step {Γ : Type u_1} {Λ : Type u_2} {n : } (M : Machine Γ Λ) (cfg : Cfg Γ Λ n) :
                      Option (Cfg Γ Λ n)

                      Execute one step of the DLBA. Returns none if the machine halts (i.e., the transition function returns none for the current state and symbol). The configuration just before halting can be inspected for acceptance.

                      Equations
                      Instances For
                        def DLBA.iterateStep {Γ : Type u_1} {Λ : Type u_2} {n : } (M : Machine Γ Λ) (cfg : Cfg Γ Λ n) :
                        Option (Cfg Γ Λ n)

                        Iterate the step function k times. Returns some cfg' if the machine is still running after k steps, or none if it halted at or before step k.

                        Equations
                        Instances For
                          @[simp]
                          theorem DLBA.iterateStep_zero {Γ : Type u_1} {Λ : Type u_2} {n : } (M : Machine Γ Λ) (cfg : Cfg Γ Λ n) :
                          iterateStep M cfg 0 = some cfg
                          theorem DLBA.iterateStep_succ {Γ : Type u_1} {Λ : Type u_2} {n : } (M : Machine Γ Λ) (cfg : Cfg Γ Λ n) (k : ) :
                          iterateStep M cfg (k + 1) = (iterateStep M cfg k).bind (step M)
                          theorem DLBA.iterateStep_none_mono {Γ : Type u_1} {Λ : Type u_2} {n : } (M : Machine Γ Λ) (cfg : Cfg Γ Λ n) {k : } (hk : iterateStep M cfg k = none) (j : ) :
                          iterateStep M cfg (k + j) = none
                          theorem DLBA.iterateStep_some_of_succ_some {Γ : Type u_1} {Λ : Type u_2} {n : } (M : Machine Γ Λ) (cfg : Cfg Γ Λ n) {k : } {cfg' : Cfg Γ Λ n} (hk : iterateStep M cfg (k + 1) = some cfg') :
                          ∃ (cfg'' : Cfg Γ Λ n), iterateStep M cfg k = some cfg''
                          theorem DLBA.iterateStep_add {Γ : Type u_1} {Λ : Type u_2} {n : } (M : Machine Γ Λ) (cfg : Cfg Γ Λ n) (i j : ) :
                          iterateStep M cfg (i + j) = (iterateStep M cfg i).bind fun (c : Cfg Γ Λ n) => iterateStep M c j

                          Halting and Acceptance #

                          def DLBA.Halts {Γ : Type u_1} {Λ : Type u_2} {n : } (M : Machine Γ Λ) (cfg : Cfg Γ Λ n) :

                          The machine halts from configuration cfg if there exists a step at which iterateStep returns none.

                          Equations
                          Instances For
                            noncomputable def DLBA.haltingCfg {Γ : Type u_1} {Λ : Type u_2} {n : } (M : Machine Γ Λ) (cfg : Cfg Γ Λ n) (h : Halts M cfg) :
                            Cfg Γ Λ n

                            The halting configuration: the configuration of the machine at the last step before it halts. This is where we check acceptance.

                            Equations
                            Instances For
                              def DLBA.Accepts {Γ : Type u_1} {Λ : Type u_2} {n : } (M : Machine Γ Λ) (cfg : Cfg Γ Λ n) :

                              The machine accepts from configuration cfg if it halts and the last configuration before halting has an accepting state.

                              Equations
                              Instances For
                                def DLBA.Rejects {Γ : Type u_1} {Λ : Type u_2} {n : } (M : Machine Γ Λ) (cfg : Cfg Γ Λ n) :

                                The machine rejects from configuration cfg if it halts and the last configuration before halting has a non-accepting state.

                                Equations
                                Instances For
                                  def DLBA.initCfg {Γ : Type u_1} {Λ : Type u_2} {n : } (M : Machine Γ Λ) (w : Fin (n + 1)Γ) :
                                  Cfg Γ Λ n

                                  The initial configuration for input w on a tape of n + 1 cells.

                                  Equations
                                  Instances For
                                    noncomputable def DLBA.loadList {Γ : Type u_1} (w : List Γ) (hw : w []) :

                                    Load a non-empty list onto a bounded tape.

                                    Equations
                                    Instances For
                                      noncomputable def DLBA.initCfgList {Γ : Type u_1} {Λ : Type u_2} (M : Machine Γ Λ) (w : List Γ) (hw : w []) :
                                      Cfg Γ Λ (w.length - 1)

                                      Initial configuration for a non-empty list input.

                                      Equations
                                      Instances For
                                        def DLBA.Language {Γ : Type u_1} {Λ : Type u_2} (M : Machine Γ Λ) (n : ) :
                                        Set (Fin (n + 1)Γ)

                                        The language recognized by a DLBA: the set of inputs (of each length) that the machine accepts.

                                        Equations
                                        Instances For
                                          noncomputable def DLBA.LanguageOfMachine {Γ : Type u_1} {Λ : Type u_2} (M : Machine Γ Λ) :

                                          The language recognized by a DLBA, defined on non-empty lists.

                                          Equations
                                          Instances For
                                            noncomputable def DLBA.LanguageViaEmbed {T : Type u_1} {Γ : Type u_2} {Λ : Type u_3} (M : Machine Γ Λ) (embed : TΓ) :

                                            Recognition via an embedding from the input alphabet into the tape alphabet.

                                            Equations
                                            Instances For
                                              noncomputable def DLBA.LanguageRecognized {T : Type u_1} {Γ : Type u_2} {Λ : Type u_3} (M : Machine Γ Λ) (embed : TΓ) (acceptEmpty : Bool) :

                                              Recognition with an explicit decision for the empty word (see LBA.LanguageRecognized).

                                              Equations
                                              Instances For

                                                Complement Machine #

                                                def DLBA.complementMachine {Γ : Type u_1} {Λ : Type u_2} (M : Machine Γ Λ) :
                                                Machine Γ Λ

                                                The complement of a deterministic DLBA: same transitions, negated acceptance. This is the key construction for showing closure under complement.

                                                Equations
                                                Instances For
                                                  @[simp]
                                                  theorem DLBA.complement_step_eq {Γ : Type u_1} {Λ : Type u_2} {n : } (M : Machine Γ Λ) (cfg : Cfg Γ Λ n) :
                                                  @[simp]
                                                  theorem DLBA.complement_iterateStep_eq {Γ : Type u_1} {Λ : Type u_2} {n : } (M : Machine Γ Λ) (cfg : Cfg Γ Λ n) (k : ) :
                                                  theorem DLBA.complement_halts_iff {Γ : Type u_1} {Λ : Type u_2} {n : } (M : Machine Γ Λ) (cfg : Cfg Γ Λ n) :
                                                  theorem DLBA.complement_accepts_iff_rejects {Γ : Type u_1} {Λ : Type u_2} {n : } (M : Machine Γ Λ) (cfg : Cfg Γ Λ n) :
                                                  theorem DLBA.complement_rejects_iff_accepts {Γ : Type u_1} {Λ : Type u_2} {n : } (M : Machine Γ Λ) (cfg : Cfg Γ Λ n) :
                                                  theorem DLBA.accepts_or_rejects_of_halts {Γ : Type u_1} {Λ : Type u_2} {n : } (M : Machine Γ Λ) (cfg : Cfg Γ Λ n) (h : Halts M cfg) :
                                                  Accepts M cfg Rejects M cfg
                                                  theorem DLBA.not_accepts_and_rejects {Γ : Type u_1} {Λ : Type u_2} {n : } (M : Machine Γ Λ) (cfg : Cfg Γ Λ n) :
                                                  ¬(Accepts M cfg Rejects M cfg)
                                                  theorem DLBA.complement_language {Γ : Type u_1} {Λ : Type u_2} {n : } (M : Machine Γ Λ) (w : Fin (n + 1)Γ) (hh : Halts M (initCfg M w)) :

                                                  Decidability of Halting #

                                                  theorem DLBA.not_halts_of_iterate_eq {Γ : Type u_1} {Λ : Type u_2} {n : } (M : Machine Γ Λ) (cfg : Cfg Γ Λ n) {i j : } (hij : i < j) (hi : iterateStep M cfg i = iterateStep M cfg j) (hi_some : (iterateStep M cfg i).isSome = true) :
                                                  ¬Halts M cfg
                                                  theorem DLBA.exists_iterate_eq_of_long_run {Γ : Type u_1} {Λ : Type u_2} {n : } [Fintype Γ] [Fintype Λ] [DecidableEq Γ] [DecidableEq Λ] (M : Machine Γ Λ) (cfg : Cfg Γ Λ n) (hrun : kFintype.card (Cfg Γ Λ n), (iterateStep M cfg k).isSome = true) :
                                                  ∃ (i : ) (j : ), i < j j Fintype.card (Cfg Γ Λ n) iterateStep M cfg i = iterateStep M cfg j (iterateStep M cfg i).isSome = true
                                                  theorem DLBA.halts_iff_haltsWithin {Γ : Type u_1} {Λ : Type u_2} {n : } [Fintype Γ] [Fintype Λ] [DecidableEq Γ] [DecidableEq Λ] (M : Machine Γ Λ) (cfg : Cfg Γ Λ n) :
                                                  Halts M cfg kFintype.card (Cfg Γ Λ n), iterateStep M cfg k = none
                                                  noncomputable instance DLBA.decidableHalts {Γ : Type u_1} {Λ : Type u_2} {n : } [Fintype Γ] [Fintype Λ] [DecidableEq Γ] [DecidableEq Λ] (M : Machine Γ Λ) (cfg : Cfg Γ Λ n) :
                                                  Equations
                                                  theorem DLBA.accepts_iff_bounded {Γ : Type u_1} {Λ : Type u_2} {n : } [Fintype Γ] [Fintype Λ] [DecidableEq Γ] [DecidableEq Λ] (M : Machine Γ Λ) (cfg : Cfg Γ Λ n) :
                                                  Accepts M cfg kFintype.card (Cfg Γ Λ n), iterateStep M cfg (k + 1) = none ∃ (cfg' : Cfg Γ Λ n), iterateStep M cfg k = some cfg' M.accept cfg'.state = true
                                                  noncomputable instance DLBA.decidableAccepts {Γ : Type u_1} {Λ : Type u_2} {n : } [Fintype Γ] [Fintype Λ] [DecidableEq Γ] [DecidableEq Λ] (M : Machine Γ Λ) (cfg : Cfg Γ Λ n) :
                                                  Equations
                                                  • One or more equations did not get rendered due to their size.
                                                  theorem DLBA.rejects_iff_bounded {Γ : Type u_1} {Λ : Type u_2} {n : } [Fintype Γ] [Fintype Λ] [DecidableEq Γ] [DecidableEq Λ] (M : Machine Γ Λ) (cfg : Cfg Γ Λ n) :
                                                  Rejects M cfg kFintype.card (Cfg Γ Λ n), iterateStep M cfg (k + 1) = none ∃ (cfg' : Cfg Γ Λ n), iterateStep M cfg k = some cfg' M.accept cfg'.state = false
                                                  noncomputable instance DLBA.decidableRejects {Γ : Type u_1} {Λ : Type u_2} {n : } [Fintype Γ] [Fintype Λ] [DecidableEq Γ] [DecidableEq Λ] (M : Machine Γ Λ) (cfg : Cfg Γ Λ n) :
                                                  Equations
                                                  • One or more equations did not get rendered due to their size.
                                                  noncomputable instance DLBA.decidableLanguage {Γ : Type u_1} {Λ : Type u_2} {n : } [Fintype Γ] [Fintype Λ] [DecidableEq Γ] [DecidableEq Λ] (M : Machine Γ Λ) (w : Fin (n + 1)Γ) :
                                                  Equations

                                                  Closure Under Intersection and Union #

                                                  def DLBA.productMachine {Γ : Type u_1} {Λ₁ : Type u_2} {Λ₂ : Type u_3} (M₁ : Machine Γ Λ₁) (M₂ : Machine Γ Λ₂) :
                                                  Machine Γ (Λ₁ × Λ₂ × Bool)

                                                  Product machine for showing closure under intersection. Runs two LBAs in parallel by taking their product state space. Both machines share the same tape (for the intersection construction, we need them to operate on the same input).

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

                                                    Configuration Space Cardinality #

                                                    theorem DLBA.card_cfg {Γ : Type u_1} {Λ : Type u_2} {n : } [Fintype Γ] [Fintype Λ] :
                                                    Fintype.card (Cfg Γ Λ n) = Fintype.card Λ * Fintype.card Γ ^ (n + 1) * (n + 1)
                                                    theorem DLBA.card_boundedTape {Γ : Type u_1} {n : } [Fintype Γ] :
                                                    Fintype.card (BoundedTape Γ n) = Fintype.card Γ ^ (n + 1) * (n + 1)
                                                    def is_DLBA {T : Type} [Fintype T] [DecidableEq T] (L : Language T) :

                                                    A language over a finite alphabet T is DLBA-recognizable if it is accepted by some finite deterministic linearly bounded automaton over the tape alphabet Option (T ⊕ Γ) (arbitrary finite work alphabet Γ), with the input written canonically via some ∘ Sum.inl and an explicit decision acceptEmpty for the empty word — the same convention as the Turing-machine/recursive definitions.

                                                    Equations
                                                    • One or more equations did not get rendered due to their size.
                                                    Instances For
                                                      def DLBA {T : Type} [Fintype T] [DecidableEq T] :

                                                      The class of deterministic LBA languages.

                                                      Equations
                                                      Instances For