turing-machines

Turing machine does not repeat a given sequence correctly


i need to write a Turing machine that repeats a given sequence of capital letters. The alphabet of the tape can be {A, B, _}. For example, if initial input is ABB, then the result is ABBABB.

Im writing this using Turing machine simulator in https://morphett.info/turing/turing.html

The problem of code i wrote is that instead of ABBABB it returns ABB#ABB. I have no idea how to remove #, because without it, i does not work.

The logic i wrote:

; Repeats a given sequence of capital letters
; Alphabet: {A, B, _}
; States: {0, 1, 2, 3, 4, 5, 6, HALT}

; State 0: Move to the first blank (_)
0 A A r 0
0 B B r 0
0 _ # l 1

; State 1: Move left to the start of the input sequence
1 A A l 1
1 B B l 1
1 _ _ r 2

; State 2: Mark and copy each character to the end
2 A X r 3
2 B Y r 4
2 # # l HALT

; State 3: Find end and copy 'A'
3 A A r 3
3 B B r 3
3 # # r 3
3 _ A l 5

; State 4: Find end and copy 'B'
4 A A r 4
4 B B r 4
4 # # r 4
4 _ B l 6

; State 5: Move back to the start after copying 'A'
5 A A l 5
5 B B l 5
5 # # l 5
5 X A r 2

; State 6: Move back to the start after copying 'B'
6 A A l 6
6 B B l 6
6 # # l 6
6 Y B r 2


Solution

  • Lets first rewrite the program to only use symbols from the alphabet:

    (state 2 becomes state 0) There will be 5 stages for each symbol:

    0 A _ r 1a
    0 B _ r 1b
    0 _ _ l HALT
    
    ; reach next(middle) `_`
    1a _ _ r 2a
    1a * * r 1a
    
    ; reach next(last) `_`, then write A
    2a _ A l 3a
    2a * * r 2a
    
    ; return to previous(middle) `_`
    3a _ _ l 4a
    3a * * l 3a
    
    ; return to prevous(first) `_`, then write A
    4a _ A r 0
    4a * * l 4a
    
    ; idem for B (1b, 2b, 3b, 4b)
     
    

    Now we need to shift the second half left 1 place:

        ; reach last `_`
        shift1 _ _ l replace_
        shift1 * * r shift1
        
        ; replace with `_`, then replace prevoius with symbol
        replace_ A _ l replaceA
        replace_ B _ l replaceB
        replace_ _ _ l HALT
    
        ; idem with A and B (replaceA, replaceB)
    

    Another approach would be to start the copying from the second place, then copy the first to the middle _:

    before0 * * r 0
    
    
    cf1 _ _ r cf2
    cf1 * * l cf1
    
    cf2 A A r cfa
    cf2 B B r cfb
    
    cfa _ A l HALT
    cfa * * r cfa
    
    cfb _ B l HALT
    cfb * * r cfb
    

    Lastly(if we can use external symbols) we could copy to the final location but with placeholder symbols, then replace them:

        0 A X r 1a
        0 B Y r 1b
        0 * * * 3
    
        1a _ a l 2
        1a * * r 1a
    
        1b _ b l 2
        1b * * r 1b
    
        2 X A r 0
        2 Y B r 0
        2 * * l 2
    
        3 a A r 3
        3 b B r 3
        3 _ _ * HALT