Documentation

Complexitylib.Languages.Palindromes

palindromes: strings equal to their reverse #

A classical non-regular language decided in linear time by a 1-work-tape TM. The machine copies the input to its work tape, rewinds the input head, then scans the input forward and the work tape backward, comparing bits.

Main definitions #

Main results #

Design #

Phases (see comments on PalindromePhase):

  1. start: move all heads from cell 0 (▷) to cell 1.
  2. copy: scan input forward, copying each bit to the work tape. When input reads , move input head left and enter rewindInput.
  3. rewindInput: move input head left until reading . On , move input head right to cell 1 and work head left by one, entering compare.
  4. compare: compare input and work bits. On match, advance input right and work left. On , halt with output 1. On mismatch, enter reject.
  5. reject: consume remaining input, then halt with output 0.

Control states of palindromesTM.

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

    Palindrome-decider TM with one work tape. The work tape is used as a copy of the input, which is then scanned backward during the compare phase.

    Equations
    • One or more equations did not get rendered due to their size.
    Instances For
      structure Complexity.TM.CopyInv (c : Cfg 1 palindromesTM.Q) (x : List Bool) (k : ) :

      Invariant during the copy phase: work tape holds the first k bits of x, heads are at cell k+1. We use List.get? via getD rather than a sized getElem to avoid embedding proofs in the structure fields.

      Instances For
        structure Complexity.TM.RewindInv (c : Cfg 1 palindromesTM.Q) (x : List Bool) (j : ) :

        Invariant during the rewindInput phase: work tape holds all of x, input head is somewhere in [0 .. x.length].

        Instances For
          structure Complexity.TM.CompareInv (c : Cfg 1 palindromesTM.Q) (x : List Bool) (k : ) :

          Invariant during the compare phase after k matches: input head at k+1, work head at |x|-k.

          Instances For

            Invariant during the reject phase: input head somewhere in [1 .. x.length+1]; work doesn't matter.

            Instances For

              Pointwise palindrome condition: ∀ i < |x|, x[i] = x[|x|-1-i].

              Equations
              Instances For

                palindromesTM halts in 3·|x| + 4 steps on every input, writing the correct answer to output cell 1.

                The palindromes language — binary strings equal to their reverse.

                Equations
                Instances For