Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Karel as a Turing Machine CSE-111 12/03/04

2 Facts Any programming language which satisfies Boehm & Jacopini's conditions can be expressed by means of a Turing machine. 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 The reverse is also true i.e. any programming language can be used to program a Turing Machine

3 Turing Machine-Recapitulation A tape of (infinite) squares with a tape head. A tape of (infinite) squares with a tape head. Squares contain 0, 1 Squares contain 0, 1 Tape head

4 Turing Machine-Recapitulation Performs the following actions Performs the following actions Tape head

5 Performs the following actions Performs the following actions 1. Move-left-one- square Turing Machine-Recapitulation Tape head

6 Performs the following actions Performs the following actions 1. Move-left-one- square 2. Move-right-one- square Turing Machine-Recapitulation Tape head

7 Performs the following actions Performs the following actions 1. Move-left-one- square 2. Move-right-one- square 3. Print-0-at-current- square Turing Machine-Recapitulation Tape head

8 Performs the following actions Performs the following actions 1. Move-left-one- square 2. Move-right-one- square 3. Print-0-at-current- square 4. Print-1-at-current- square Turing Machine-Recapitulation Tape head

9 Performs the following actions Performs the following actions 1. Move-left-one- square 2. Move-right-one- square 3. Print-0-at-current- square 4. Print-1-at-current- square 5. Erase Turing Machine-Recapitulation Tape head

10 Karel as a TM For 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 functions or verbs Program Karel to imitate the 5 functions or verbs

11 Infinite Tape? Karel has Karel has infinite number of streets infinite number of streets infinite number of avenues infinite number of avenues

12 Infinite Tape The 1 st street and The 1 st street and 1 st avenue can be used as the tape. Each corner can be used as a square on the tape Each corner can be used as a square on the tape

13 Representation of 0 and 1 2 beeper on corner can represent a square with a 0 2 beeper on corner can represent a square with a 0 1 beeper on corner can represent a square with a 1 1 beeper on corner can represent a square with a 1 A corner with no beepers is a blank square A corner with no beepers is a blank square

14 Programming Karel to become a TM Define 5 new instructions in Karel for each of the 5 functions/verbs of Turing Machine. 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). 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 Write the main program which should perform the task of the Turing machine

15 Move-left-one-square face-west face-west If on first street, (except on 1 st ave) If on first street, (except on 1 st ave) front should be clear so move. front should be clear so move.

16 Move-left-one-square face-west face-west If on first street, (except on 1 st ave) If on first street, (except on 1 st ave) front should be clear so move. front should be clear so move. If on first avenue, If on first avenue, face-north and move.

17 Move-left-one-square face-west face-west If on first street, (except on 1 st ave) If on first street, (except on 1 st ave) front should be clear so move. If on first avenue, If on first avenue, face-north and move. If on a corner with no beeper, print -0 or put 2 beepers If on a corner with no beeper, print -0 or put 2 beepers

18 define-new-instruction move-left as begin face-west; if front-is-clear then moveelsebeginface-north;moveend; if not-next-to-a-beeper then print-0; end; end; Move-left-one-square

19 define-new-instruction move-right as begin face-south; if front-is-clear then moveelsebeginface-east;moveend; if not-next-to-a-beeper then print-0; end; end; Move-right-one-square

20 Print-0 Pick all the beepers at the current corner (there may be 1 or 2 beepers already) 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; putbeeperend;

21 Print-0 Pick all the beepers at the current corner (there may be 1 or 2 beepers already) Pick all the beepers at the current corner (there may be 1 or 2 beepers already) Put 2 beepers. Put 2 beepers. define-new-instruction print-0 as begin pickallbeeper; putbeeper; putbeeper;end;

22 Print-1 Pick all the beepers at the current corner (there may be 1 or 2 beepers already) 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;

23 Print-0 Pick all the beepers at the current corner (there may be 1 or 2 beepers already) Pick all the beepers at the current corner (there may be 1 or 2 beepers already) Put 1 beeper. Put 1 beeper. define-new-instruction print-1 as begin pickallbeeper; putbeeper; end;

24 Erase Check 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 corner)  Pick all the beepers (erase)  Move right but don’t print 0(move to square on right) if on a beeperif on a beeper  Move right but don’t print 0 Check whether at right end

25 Erase 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 corner)  Pick all the beepers (erase)  Move left but don’t print 0(move to square on left) if on a beeperif on a beeper  Move left but don’t print 0

26 Erase define-new-instruction erase as begin begin move-left-without- putting-beeper; if not-next-to-a-beeper then begin moveright-without- putting-beeper; pickallbeeper; moveright-without- putting-beeper endelsebegin moveright-without- putting-beeper; moveright-without-putting-beeper; if not-next-to-a-beeper then if not-next-to-a-beeper thenbegin moveleft- without-putting -beeper; pickallbeeper; moveleft- without-putting -beeperendelse moveleft-without- putting-beeper moveleft-without- putting-beeper end endend;

27 How to write a Turing Machine flowchart in Karel Example: Negation of a binary digit Example: Negation of a binary digit I/P: 1 square O/P: 2 square

28 How to write a Turing Machine flowchart in Karel Example: Negation of a binary digit Example: Negation of a binary digit I/P: 1 square O/P: 2 square Start Is 0? Move right Print 1 Stop False True

29 How to write a Turing Machine flowchart in Karel For Karel: For Karel: See whether he is on the corner with one or two beepers, move right, and place beepers accordingly move right, and place beepers accordingly Start Is 0? Move right Print 1 Stop False True

30 How to write a Turing Machine flowchart in Karel beginning-of-execution beginning-of-execution if next-to-a-beeper then beginpickbeeper; if next-to-a-beeper then begin if next-to-a-beeper then beginputbeeper;move-right;print-1endelsebeginputbeeper;move-rightendend;turnoff;end-of-execution Start Is 0? Move right Print 1 Stop False True


Download ppt "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."

Similar presentations


Ads by Google