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 #
TM.palindromesTM— 6-state 1-work-tape palindrome decider.Language.palindromes—{x : List Bool | x = x.reverse}.
Main results #
palindromesTM_reachesIn— halts in3·|x| + 4steps on every input.palindromes_in_DTIME,palindromes_mem_P.
Design #
Phases (see comments on PalindromePhase):
start: move all heads from cell 0 (▷) to cell 1.copy: scan input forward, copying each bit to the work tape. When input reads□, move input head left and enterrewindInput.rewindInput: move input head left until reading▷. On▷, move input head right to cell 1 and work head left by one, enteringcompare.compare: compare input and work bits. On match, advance input right and work left. On□, halt with output1. On mismatch, enterreject.reject: consume remaining input, then halt with output0.
Control states of palindromesTM.
- start : PalindromePhase
- copy : PalindromePhase
- rewindInput : PalindromePhase
- compare : PalindromePhase
- reject : PalindromePhase
- done : PalindromePhase
Instances For
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
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
Invariant during the rewindInput phase: work tape holds all of x,
input head is somewhere in [0 .. x.length].
Instances For
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
palindromesTM halts in 3·|x| + 4 steps on every input, writing
the correct answer to output cell 1.
palindromesTM decides Language.palindromes in time 3·|x| + 4.
palindromes ∈ DTIME(3n + 4).