310 likes | 353 Views
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.
E N D
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
Tape head 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
Turing Machine-Recapitulation • Performs the following actions • Move-left-one-square Tape head
Tape head Turing Machine-Recapitulation • Performs the following actions • Move-left-one-square • Move-right-one-square
Tape head Turing Machine-Recapitulation • Performs the following actions • Move-left-one-square • Move-right-one-square • Print-0-at-current-square
Tape head Turing Machine-Recapitulation • Performs the following actions • Move-left-one-square • Move-right-one-square • Print-0-at-current-square • Print-1-at-current-square
Tape head Turing Machine-Recapitulation • Performs the following actions • Move-left-one-square • Move-right-one-square • Print-0-at-current-square • Print-1-at-current-square • Erase
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;
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-0
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-1
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; Print-0
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
define-new-instruction erase as begin move-left-without- putting-beeper; if not-next-to-a-beeper then begin moveright-without- putting-beeper; pickallbeeper; moveright-without- putting-beeper end else begin moveright-without- putting-beeper; moveright-without- putting-beeper; if not-next-to-a-beeper then begin moveleft- without-putting -beeper; pickallbeeper; moveleft- without-putting -beeper end else moveleft-without- putting-beeper end end; Erase
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 How to write a Turing Machine flowchart in Karel Start True Is 0? False Move right Move right Print 1 Stop
For Karel: See whether he is on the corner with one or two beepers, move right , and place beepers accordingly How to write a Turing Machine flowchart in Karel Start True Is 0? False Move right Move right Print 1 Stop
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 How to write a Turing Machine flowchart in Karel Start True Is 0? False Move right Move right Print 1 Stop