Karel as a Turing Machine CSE-111 12/03/04 Facts Any programming language which satisfies Boehm & Jacopini's conditions can be expressed by means of a Turing machine. The reverse is also true i.e. any programming language can be used to program a Turing Machine Turing Machine-Recapitulation A tape of (infinite) squares with a tape head. Squares contain 0 , 1 Tape head Turing Machine-Recapitulation Performs the following actions Tape head Turing Machine-Recapitulation 1. Performs the following actions Move-left-onesquare Tape head Turing Machine-Recapitulation 1. 2. Performs the following actions Move-left-onesquare Move-right-onesquare Tape head Turing Machine-Recapitulation 1. 2. 3. Performs the following actions Move-left-onesquare Move-right-onesquare Print-0-at-currentsquare Tape head Turing Machine-Recapitulation 1. 2. 3. 4. Performs the following actions Move-left-onesquare Move-right-onesquare Print-0-at-currentsquare Print-1-at-currentsquare Tape head Turing Machine-Recapitulation 1. 2. 3. 4. 5. Performs the following actions Move-left-onesquare Move-right-onesquare Print-0-at-currentsquare Print-1-at-currentsquare Erase Tape head Karel as a TM For Karel to work as a TM find an infinite tape. A way to represent 0 and 1. Program Karel to imitate the 5 functions or verbs Infinite Tape? Karel has infinite number of streets infinite number of avenues Infinite Tape The 1st street and 1st avenue can be used as the tape. Each corner can be used as a square on the tape Representation of 0 and 1 2 beeper on corner can represent a square with a 0 1 beeper on corner can represent a square with a 1 A corner with no beepers is a blank square Programming Karel to become a TM Define 5 new instructions in Karel for each of the 5 functions/verbs of Turing Machine. Define all other instructions that may be required (by the 5 verb-instructions or the main program) . Write the main program which should perform the task of the Turing machine Move-left-one-square face-west If on first street, (except on 1st ave) front should be clear so move. Move-left-one-square face-west If on first street, (except on 1st ave) front should be clear so move. If on first avenue, face-north and move. Move-left-one-square face-west If on first street, (except on 1st ave) front should be clear so move. If on first avenue, face-north and move. If on a corner with no beeper, print -0 or put 2 beepers Move-left-one-square define-new-instruction move-left as begin face-west; if front-is-clear then move else begin face-north; move end; if not-next-to-a-beeper then print-0; end; Move-right-one-square define-new-instruction move-right as begin face-south; if front-is-clear then move else begin face-east; move end; if not-next-to-a-beeper then print-0; end; Print-0 Pick all the beepers at the current corner (there may be 1 or 2 beepers already) define-new-instruction print-0 as begin pickallbeeper; putbeeper; putbeeper end; Print-0 Pick all the beepers at the current corner (there may be 1 or 2 beepers already) Put 2 beepers. define-new-instruction print-0 as begin pickallbeeper; putbeeper; putbeeper; end; Print-1 Pick all the beepers at the current corner (there may be 1 or 2 beepers already) define-new-instruction print-1 as begin pickallbeeper; putbeeper; end; Print-0 Pick all the beepers at the current corner (there may be 1 or 2 beepers already) Put 1 beeper. define-new-instruction print-1 as begin pickallbeeper; putbeeper; end; Erase Check whether at left end • Move left but don’t print 0(or put beeper) • if not on a beeper (i.e indeed on left end) Move right but don’t print 0 ( go back to previous corner) Pick all the beepers (erase) Move right but don’t print 0(move to square on right) • if on a beeper Move right but don’t print 0 Check whether at right end Erase Check whether at right end • Move right but don’t print 0(or put beeper) • if not on a beeper (i.e indeed on right end) Move left but don’t print 0 ( go back to previous corner) Pick all the beepers (erase) Move left but don’t print 0(move to square on left) • if on a beeper Move left but don’t print 0 Erase define-new-instruction erase as begin move-left-withoutputting-beeper; if not-next-to-a-beeper then begin moveright-withoutputting-beeper; pickallbeeper; moveright-withoutputting-beeper end else begin moveright-withoutputting-beeper; moveright-withoutputting-beeper; if not-next-to-a-beeper then begin moveleftwithout-putting -beeper; pickallbeeper; moveleftwithout-putting -beeper end else moveleft-withoutputting-beeper end end; How to write a Turing Machine flowchart in Karel Example: Negation of a binary digit I/P: 1 square O/P: 2 square How to write a Turing Machine flowchart in Karel Example: Negation of a binary digit I/P: 1 square O/P: 2 square Start True Move right Is 0? False Move right Print 1 Stop How to write a Turing Machine flowchart in Karel For Karel: See whether he is on the corner with one or two beepers, True move right , and place beepers Move right accordingly Start Is 0? False Move right Print 1 Stop How to write a Turing Machine flowchart in Karel beginning-of-execution if next-to-a-beeper then begin pickbeeper; if next-to-a-beeper then begin putbeeper; move-right; print-1 end else begin putbeeper; move-right end end; turnoff; end-of-execution Start True Move right Is 0? False Move right Print 1 Stop