Karel Language Description

The Karel language is a simple imperative programming language for directing an on-screen robot. The robot can move around a grid, pick up and place “beepers”, and test aspects of its environment to determine what to do next. The syntax of the language is based on Pascal.

Program State

Conceptually, the Karel execution environment consists of two parts:

  1. A world which is defined by a grid of squares.

    • Each square is either clear or a wall. The robot can move into clear squares, but not into wall squares.

    • A clear square may contain zero or more beepers. Beepers are small objects that the robot can pick up, put down, and carry around.

  2. A robot which has a position, facing, and a bag of beepers.

    • The position of the robot is a Cartesian coordinate indicating which square of the grid the robot is on.

    • The facing of the robot is one of the four cardinal directions (north, south, east, west) and indicates which direction the robot is currently pointing toward. When the robot moves, it will move in the direction it is facing. Additionally, we define relative directionsfront, back, left, right—with respect to the direction the robot is currently facing. For example, if the robot is facing east, then left refers to the cardinal direction north.

    • The bag of beepers contains zero or more beepers. When the robot picks up a beeper, it adds it to the bag. When the robot puts down a beeper, it takes it out of the bag. It is an error to try to put down a beeper when the bag is empty.

Types and functions related to the state of a Karel program are defined in the Haskell module KarelState.hs provided in the assignment’s support files. Some example worlds and initial robot states are provided in KarelExamples.hs.


A Karel program consists of commands, such as move, turn left, and pick beeper. Additionally, a Karel program can define and invoke macros. A macro is just a statement or block of statements that is given a name to be easily reused.

The concrete syntax of (our version of) Karel is defined by the following grammar. Additionally, we will use curly braces {} to indicate comments in Karel programs, even though these are not included in the syntax definition.

nat ::= (any natural number)
macro ::= (any macro name)
prog ::= defs  define main as stmt a Karel program
defs ::= ε  |  def  defs sequence of definitions
stmts ::= ε  |  stmt ; stmts sequence of statements
card ::= north | south | east | west cardinal directions
dir ::= front | back | right | left relative directions
def ::= define macro as stmt macro definition
test ::= not test boolean negation
| facing card ? am I facing … ?
| clear dir ? can I move … ?
| beeper? is there a beeper here?
| empty? is my bag empty?
stmt ::= shutdown end the program
| move move forward
| pick beeper take a beeper
| put beeper leave a beeper
| turn dir rotate in place
| call macro invoke a macro
| iterate nat times stmt fixed repetition loop
| if test then stmt else stmt conditional branch
| while test do stmt conditional loop
| begin stmts end statement block

The abstract syntax of Karel is defined as a set of Haskell data types in KarelSyntax.hs provided in the assignment’s support file.

Example Program

Here’s an example of a complete Karel program, written in concrete syntax.

{ fetch: moves in a straight line until it finds a beeper,
         then picks it up }
define fetch as
  while not beeper? do    { keep doing until we find a beeper }
    if clear front?       { can we move forward? }
      then move           { if yes, then do it }
      else shutdown;      { TODO: install laser to shoot walls }
  pick beeper;

define main as
  put beeper;   { put a beeper here to mark our place }
  call fetch;   { go get a new beeper }
  turn back;    { turn around }
  call fetch;   { go back to where we started }
  put beeper;   { put down the marker beeper }
  put beeper;   { put down the new beeper }
  shutdown;     { end the program }

Haskell functions for generating example Karel programs are provided in KarelExamples.hs. You can evaluate fetcher 1 to generate a program equivalent to the one above.

Back to course home page