karel as a turing machine
DESCRIPTION
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. - PowerPoint PPT PresentationTRANSCRIPT
Karel as a Turing Karel as a Turing MachineMachine
CSE-111 12/03/04CSE-111 12/03/04
FactsFacts
Any programming language which Any programming language which satisfies Boehm & Jacopini's conditions can satisfies Boehm & Jacopini's conditions can be expressed by means of a Turing be expressed by means of a Turing machine.machine.
The reverse is also true i.e. any The reverse is also true i.e. any programming language can be used to programming language can be used to program a Turing Machineprogram a Turing Machine
Turing Machine-RecapitulationTuring Machine-Recapitulation
A tape of (infinite) A tape of (infinite) squares with a tape squares with a tape head.head.
Squares contain 0 , Squares contain 0 , 11
Tape head
Turing Machine-RecapitulationTuring Machine-Recapitulation
Performs the Performs the following actionsfollowing actions
Tape head
Performs the Performs the following actionsfollowing actions
1.1. Move-left-one-Move-left-one-squaresquare
Turing Machine-RecapitulationTuring Machine-Recapitulation
Tape head
Performs the Performs the following actionsfollowing actions
1.1. Move-left-one-Move-left-one-squaresquare
2.2. Move-right-one-Move-right-one-squaresquare
Turing Machine-RecapitulationTuring Machine-Recapitulation
Tape head
Performs the Performs the following actionsfollowing actions
1.1. Move-left-one-Move-left-one-squaresquare
2.2. Move-right-one-Move-right-one-squaresquare
3.3. Print-0-at-current-Print-0-at-current-squaresquare
Turing Machine-RecapitulationTuring Machine-Recapitulation
Tape head
Performs the Performs the following actionsfollowing actions
1.1. Move-left-one-Move-left-one-squaresquare
2.2. Move-right-one-Move-right-one-squaresquare
3.3. Print-0-at-current-Print-0-at-current-squaresquare
4.4. Print-1-at-current-Print-1-at-current-squaresquare
Turing Machine-RecapitulationTuring Machine-Recapitulation
Tape head
Performs the Performs the following actionsfollowing actions
1.1. Move-left-one-Move-left-one-squaresquare
2.2. Move-right-one-Move-right-one-squaresquare
3.3. Print-0-at-current-Print-0-at-current-squaresquare
4.4. Print-1-at-current-Print-1-at-current-squaresquare
5.5. EraseErase
Turing Machine-RecapitulationTuring Machine-Recapitulation
Tape head
Karel as a TMKarel as a TM
For Karel to work as a TMFor Karel to work as a TM
find an infinite tape.find an infinite tape.
A way to represent 0 and 1.A way to represent 0 and 1.
Program Karel to imitate the 5 Program Karel to imitate the 5 functions or verbsfunctions or verbs
Infinite Tape?Infinite Tape?
Karel has Karel has infinite number infinite number
of streetsof streets infinite number infinite number
of avenuesof avenues
Infinite TapeInfinite Tape
The 1The 1stst street and street and
11stst avenue can be avenue can be
used as the tape.used as the tape. Each corner can be Each corner can be
used as a square on used as a square on the tapethe tape
Representation of 0 and 1Representation of 0 and 1
2 beeper on 2 beeper on corner can corner can represent a represent a square with a 0square with a 0
1 beeper on 1 beeper on corner can corner can represent a represent a square with a 1square with a 1
A corner with no A corner with no beepers is a blank beepers is a blank squaresquare
Programming Karel to become a TMProgramming Karel to become a TM
Define 5 new instructions in Karel for each Define 5 new instructions in Karel for each of the 5 functions/verbs of Turing Machine.of the 5 functions/verbs of Turing Machine.
Define all other instructions that may be Define all other instructions that may be required (by the 5 verb-instructions or the required (by the 5 verb-instructions or the main program) .main program) .
Write the main program which should Write the main program which should perform the task of the Turing machineperform the task of the Turing machine
Move-left-one-squareMove-left-one-square
face-westface-west If on first street, If on first street,
(except on 1(except on 1stst ave) ave)
front should be clear front should be clear so move.so move.
Move-left-one-squareMove-left-one-square
face-westface-west If on first street, If on first street,
(except on 1(except on 1stst ave) ave)
front should be clear front should be clear so move.so move.
If on first avenue,If on first avenue,
face-north and move.face-north and move.
Move-left-one-squareMove-left-one-square
face-westface-west If on first street, If on first street,
(except on 1(except on 1stst ave) ave)front should be clear front should be clear so move.so move.
If on first avenue,If on first avenue,face-north and move.face-north and move.
If on a corner with no If on a corner with no beeper, print -0 or put beeper, print -0 or put 2 beepers2 beepers
define-new-instruction move-left as begin define-new-instruction move-left as begin face-west; face-west; if front-is-clear then if front-is-clear then
move move else else begin begin
face-north; face-north; move move
end; end; if not-next-to-a-beeper then if not-next-to-a-beeper then
print-0;print-0; end; end;
Move-left-one-squareMove-left-one-square
define-new-instruction move-right as begin define-new-instruction move-right as begin face-south; face-south; if front-is-clear then if front-is-clear then
move move else else begin begin
face-east; face-east; move move
end; end; if not-next-to-a-beeper then if not-next-to-a-beeper then
print-0;print-0; end; end;
Move-right-one-squareMove-right-one-square
Print-0Print-0
Pick all the beepers Pick all the beepers at the current at the current corner (there may corner (there may be 1 or 2 beepers be 1 or 2 beepers already)already)
define-new-instruction define-new-instruction print-0 as print-0 as
begin begin
pickallbeeperpickallbeeper; ; putbeeper; putbeeper;
putbeeper putbeeper
end; end;
Print-0Print-0
Pick all the beepers Pick all the beepers at the current at the current corner (there may corner (there may be 1 or 2 beepers be 1 or 2 beepers already)already)
Put 2 beepersPut 2 beepers..
define-new-instruction define-new-instruction print-0 as print-0 as
begin begin
pickallbeeper; pickallbeeper; putbeeper; putbeeper;
putbeeper;putbeeper;
end; end;
Print-1Print-1
Pick all the beepers Pick all the beepers at the current at the current corner (there may corner (there may be 1 or 2 beepers be 1 or 2 beepers already)already)
define-new-instruction define-new-instruction print-1 as print-1 as
begin begin
pickallbeeperpickallbeeper; ; putbeeper; putbeeper;
end; end;
Print-0Print-0
Pick all the beepers Pick all the beepers at the current at the current corner (there may corner (there may be 1 or 2 beepers be 1 or 2 beepers already)already)
Put 1 beeperPut 1 beeper..
define-new-instruction define-new-instruction print-1 as print-1 as
begin begin
pickallbeeper; pickallbeeper; putbeeper; putbeeper;
end; end;
EraseEraseCheck whether at left endCheck whether at left end
• Move left but don’t print 0(or put beeper)Move left but don’t print 0(or put beeper)• if not on a beeper (i.e indeed on left end)if not on a beeper (i.e indeed on left end)
Move right but don’t print 0 ( go back to previous Move right but don’t print 0 ( go back to previous corner) corner)
Pick all the beepers (erase)Pick all the beepers (erase)Move right but don’t print 0(move to square on right)Move right but don’t print 0(move to square on right)
• if on a beeperif on a beeperMove right but don’t print 0Move right but don’t print 0
Check whether at right endCheck whether at right end
EraseEraseCheck whether at right endCheck whether at right end
• Move right but don’t print 0(or put beeper)Move right but don’t print 0(or put beeper)• if not on a beeper (i.e indeed on right end)if not on a beeper (i.e indeed on right end)
Move left but don’t print 0 ( go back to previous Move left but don’t print 0 ( go back to previous corner) corner)
Pick all the beepers (erase)Pick all the beepers (erase)Move left but don’t print 0(move to square on left)Move left but don’t print 0(move to square on left)
• if on a beeperif on a beeperMove left but don’t print 0Move left but don’t print 0
EraseErasedefine-new-instruction erase asdefine-new-instruction erase as begin begin
move-left-without-move-left-without-putting-beeper; putting-beeper;
if not-next-to-a-beeper if not-next-to-a-beeper then then
begin begin moveright-without-moveright-without-putting-beeper; putting-beeper;
pickallbeeper;pickallbeeper;moveright-without-moveright-without-putting-beeper putting-beeper end end
else else begin begin
moveright-without-moveright-without-putting-beeper;putting-beeper;
moveright-without-moveright-without-putting-beeper;putting-beeper;
if not-next-to-a-beeper if not-next-to-a-beeper then then
begin begin moveleft-moveleft-without-puttingwithout-putting-beeper; -beeper; pickallbeeper; pickallbeeper; moveleft-moveleft-without-puttingwithout-putting-beeper -beeper
end end elseelse
moveleft-moveleft-without-without- putting-putting-beeper beeper endendend; end;
How to write a Turing Machine How to write a Turing Machine flowchart in Karelflowchart in Karel
Example: Example: Negation of Negation of
a binary digit a binary digit
I/P: 1 squareI/P: 1 square
O/P: 2 squareO/P: 2 square
How to write a Turing Machine How to write a Turing Machine flowchart in Karelflowchart in Karel
Example: Example: Negation of Negation of
a binary digit a binary digit
I/P: 1 squareI/P: 1 square
O/P: 2 squareO/P: 2 square
Start
Is 0?
Move right Move right
Print 1Stop
FalseTrue
How to write a Turing Machine How to write a Turing Machine flowchart in Karelflowchart in Karel
For Karel:For Karel:
See whether he is See whether he is on the corner with on the corner with
one or two beepers,one or two beepers,
move right , and move right , and place beepers place beepers accordinglyaccordingly
Start
Is 0?
Move right Move right
Print 1Stop
FalseTrue
How to write a Turing Machine How to write a Turing Machine flowchart in Karelflowchart in Karel
beginning-of-executionbeginning-of-execution
if next-to-a-beeper then if next-to-a-beeper then begin begin
pickbeeper;pickbeeper; if next-to-a-beeper then if next-to-a-beeper then begin begin
putbeeper; putbeeper; move-right; move-right; print-1 print-1
end end else else begin begin
putbeeper;putbeeper;move-right move-right
endendend; end; turnoff; turnoff; end-of-execution end-of-execution
Start
Is 0?
Move right Move right
Print 1Stop
FalseTrue