Karel as a Turing Machine - PowerPoint PPT Presentation

About This Presentation
Title:

Karel as a Turing Machine

Description:

If on a corner with no beeper, print -0 or put 2 beepers ... Move right but don't print 0 ( go back to previous corner) Pick all the beepers (erase) ... – PowerPoint PPT presentation

Number of Views:65
Avg rating:3.0/5.0
Slides: 31
Provided by: Viks
Learn more at: https://cse.buffalo.edu
Category:
Tags: karel | machine | print | turing

less

Transcript and Presenter's Notes

Title: Karel as a Turing Machine


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
Turing Machine-Recapitulation
  • A tape of (infinite) squares with a tape head.
  • Squares contain 0 , 1

4
Turing Machine-Recapitulation
  • Performs the following actions

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

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

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

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

21
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

22
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

23
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

24
Erase
  • Check whether at left end
  • Move left but dont print 0(or put beeper)
  • if not on a beeper (i.e indeed on left end)
  • Move right but dont print 0 ( go back to
    previous corner)
  • Pick all the beepers (erase)
  • Move right but dont print 0(move to square on
    right)
  • if on a beeper
  • Move right but dont print 0
  • Check whether at right end

25
Erase
  • Check whether at right end
  • Move right but dont print 0(or put beeper)
  • if not on a beeper (i.e indeed on right end)
  • Move left but dont print 0 ( go back to previous
    corner)
  • Pick all the beepers (erase)
  • Move left but dont print 0(move to square on
    left)
  • if on a beeper
  • Move left but dont print 0

26
Erase
  • 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
  • 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

27
How to write a Turing Machine flowchart in Karel
  • 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
  • I/P 1 square
  • O/P 2 square

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

Start
True
Is 0?
False
Move right
Move right
Print 1
Stop
30
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
Is 0?
False
Move right
Move right
Print 1
Stop
Write a Comment
User Comments (0)
About PowerShow.com