1 / 30

Karel as a Turing Machine

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.

richardryan
Download Presentation

Karel as a Turing Machine

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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. • The reverse is also true i.e. any programming language can be used to program a Turing Machine

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

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

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

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

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

  8. 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

  9. 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

  10. 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

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

  12. 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

  13. 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

  14. 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

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

  16. 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.

  17. 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

  18. 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;

  19. 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;

  20. 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

  21. 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

  22. 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

  23. 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

  24. 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

  25. 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

  26. 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

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

  28. 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

  29. 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

  30. 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

More Related