Exploring ALICE

In this chapter, we'll walk through some more simple editing operations using Alice. Again, you should follow along on the computer as you read so you can actually see Alice in action.

Loading Saved Programs

Last chapter, we saved our sample program in a file we called ``squares.ap''. We'll be doing more with this program, so the first thing to do is to get the program back from the file. This process is called ``loading.''

Start up Alice in the usual way. This time, choose the selection

Load in an existing Pascal Program

from the Starting Menu. Remember, to choose something from a menu, press the letter that labels the selection or press the up or down arrow key until the selection you want is highlighted, and then press the space bar. Do this now.

Alice will respond by giving you a menu of files and directories, just as it did when you wanted to save your program. This time, though, you should see your file ``squares.ap'' listed on this menu. Alice knows that this file contains an Alice Pascal program because of the ``.ap'' on the end.

Choose the ``squares.ap'' file name from the list in the usual way. Alice will load the program from the file and display the source code on the terminal screen.

By the way, the program you are loading must be one that was previously saved by Alice. We've mentioned before that Alice always uses a special internal format to store programs. If the program wasn't saved in this format, Alice won't be able to load it successfully and will give you a message saying so.

Now that you've loaded the program, you can run it or single step through it again if you like. To run it normally, press [F1]; to single step, press [F2]. Do this just to remind yourself how Alice works.

Moving The Cursor

Many of the things that we will do with Alice in the rest of the chapter depend on the position of the cursor. Because of this, we're going to spend some time describing different ways to move the cursor around your program.

So far, we have been using the arrow keys and TAB to move the cursor around on the screen. This is a simple process, but there are other keys that may also be useful.

When you are moving the cursor around your program there are several special keys that you'll find useful. We stress that these are only for moving the cursor through your program; you should not use them for moving the cursor when you are selecting something from a menu.

As you have seen, the TAB key jumps to the next changeable thing in your program. For example, move the cursor to the start of the for statement in your squares program, i.e. the keyword for in

Screen Shot 8 10 forsq1 not available

If you press TAB once, you will move to the i, because the i could be changed to a different variable name.

Screen Shot 8 10 forsq2 not available

If you press TAB again, the cursor will jump to the number 1.

Screen Shot 8 10 forsq3 not available

It skips over the := because that can't be changed; it always has to be there in the for statement. Keep pressing TAB and the cursor will keep jumping forward. It skips over the to (which can be changed to downto but only in a special way), over the do begin and over the end. These are skipped because they are part of the standard for statement template and cannot be changed.

When you get to the last statement of the program and press TAB, nothing happens. You're at the end, so you can't go forward any more. Are you stuck there? Not at all. You can go backwards by using the usual arrow keys. You can go to the very top of your program by pressing the [HOME] key. Or, you can hold the SHIFT key and press TAB.

Press SHIFT-TAB and see what happens. The cursor moves backwards. SHIFT-TAB is the reverse of TAB. Pressing it over and over will move backwards through all the user-entered things in your program. Try it.

The [ENTER] key is like the carriage return on a typewriter. When you press it, Alice moves the cursor down to the next line. However, Alice does a little more as well. Move the cursor to the begin line of your program (before the for loop) and press [ENTER]. You will see that Alice creates a new Statement placeholder before the for loop. If you press [ENTER] again, Alice will stay on the Statement placeholder it has just made. Move the cursor to the keyword for and press [ENTER]. This time, you will get an empty Statement placeholder inside the for loop.

The general rule is this: if the cursor is not on a placeholder, pressing [ENTER] will move to the next line and create an appropriate placeholder there (if there isn't one there already). If you are in a declaration section, this new placeholder will be a declaration placeholder; if you are in the executable part of your program, it will be a Statement placeholder. If the cursor is already on a placeholder of the appropriate type, pressing [ENTER] does nothing. Thus, [ENTER] always gives you a new placeholder.

Try this and see how it works. Leave a few empty placeholders lying around for what we're going to do in the next paragraph.

The characters [CTRL-[<-]] and [CTRL-[->]] are something like TAB and SHIFT-TAB, in that they jump through the program. (These are arrow keys on the keypad on the right of your keyboard.) [CTRL-[<-]] will jump forward to the nearest empty placeholder in the program, while [CTRL-[->]] will jump backward to the nearest empty placeholder. Use [ENTER] to create a few empty placeholders in your program if you haven't already, then try out [CTRL-[->]] and [CTRL-[<-]] to see how they work. What happens when you get to the end of the program and push [CTRL-[<-]]? Try it and see.

We should also point out that [CTRL-[->]] and [CTRL-[<-]] will jump to parts of your program that have been highlighted because they contain errors. (Remember how the variable name junk was highlighted in our work last chapter because the variable hadn't been declared.) Error-highlighted things are almost the same as empty placeholders; they aren't correct Pascal, so Alice behaves as if they still have to be filled in with something.

Adding Things to a Program

In the next few sections, we're going to cover a few basic editing operations: adding things to a program, deleting things from the program, and changing things that are already there.

We'll start with adding things. The most common things to add are probably statements and declarations. To add a statement, you need to make a Statement placeholder in the position where you want the statement to go. You should have the old sample program on your screen now.

Screen Shot 1 12 squares not available

Let's add a writeln instruction before the for loop so we can print out a heading before the list of numbers. The easiest way to do this is to move the cursor to the word begin before the for statement and press [ENTER]. As you've seen before, the cursor will jump down a line and create an empty Statement placeholder. You can now fill this with a writeln instruction like

writeln('This is a table of numbers and squares.');

You learned how to enter a writeln instruction in the last chapter. When you have entered the instruction, run the program to see how it works.

Now move to the declaration for i in the declaration section and press [ENTER]. The cursor will move down a line, and this time create an empty Variable-Declaration placeholder. Just for the sake of interest, let's fill in a declaration for a character variable here.

c : char;  {A character}

By now, you should know how to fill all this in. You've already filled in this sort of thing when you set up the declaration for i.

Now you know how to add Statement and Declaration placeholders to your program. What about adding other things? For example, we have the declaration

i : integer;  {A counter}

How can we change this to something like

j, i : integer;  {A counter}

In other words, how do you add another variable name to a declaration?

The first step is to add a placeholder to the declaration. Move the cursor to the i in the declaration and press [INS]. This stands for ``insert.'' You should see Alice display this.

Screen Shot 1 14 squares2 not available

Pressing [INS] has inserted the placeholder you want. You can now fill this in with a j to get

Screen Shot 1 14 squares3 not available

Now there's a trick here. Alice has to guess what kind of placeholder you want inserted. The placeholder it chooses is always the smallest that makes sense.

To understand what we're talking about, move the cursor to the word integer in the declaration we've been working with and press [INS] again. Alice sees that you are pointing to a data type and asks if it makes sense to add another Type placeholder here. Of course, it doesn't, because

    name : type, type;

is illegal in Pascal. The smallest thing that Alice can insert is an entire Variable-Declaration placeholder. You'll see that this is exactly what happens when you press [INS].

Screen Shot 1 14 squares4 not available

Let's try this somewhere else. Move down to the instruction


Put the cursor on top of the first argument i and press [INS]. You'll see that you get

Screen Shot 9 15 squares5 not available

Why? Because you were pointing to i which filled in a Value placeholder, so Alice could insert the same sort of placeholder. What happens if you move the cursor to the word writeln and press [INS]? You'll get a new Statement placeholder in front of the writeln. You get the smallest kind of placeholder that can be inserted here. Just to make sure you know what's happening, move the cursor to the i*i and press [INS] again. You'll insert a Value placeholder. Make sure you understand why.

You will probably want to experiment a little with inserting new placeholders here and there in your program. It won't always work because there are some places where new placeholders can't be added. Make sure you understand why Alice does what it does in the different locations you try.

Pressing [INS] inserts new placeholders -- puts them in front of something else. We can also append new placeholders. This is the same as inserting except that the new placeholder is put down after the thing that the cursor points to.

Move the cursor to the i*i of the writeln instruction and press [CTRL-E]. You will see that a new Value placeholder is added after the i*i. In the same way, if you move the cursor to a variable name in a declaration and press [CTRL-E], you will get a new Name placeholder. Pressing [CTRL-E] extends things like variable lists and lists of declarations. Experiment with this until you are comfortable with it.

Deleting Things

If you've been experimenting with inserting and appending new placeholders, you have a lot of empty placeholders in your program right now. In this section, we'll learn how to get rid of those empty placeholders and also how to delete other kinds of material.

Before you can delete something, you have to tell Alice what you want to delete. You do this by sweeping out the part of your program that you want to disappear. Let's start simply. Move the cursor to an empty Statement placeholder and press [F10]. We call this the SELECT key, because you use it to select a part of your program. When you press the key, you'll see the placeholder highlighted.

Screen Shot sel1 not available

Alice always highlights things you select, whether you are selecting an item from a menu or a section of code to delete.

We'll get rid of this placeholder right away. Once the placeholder is highlighted, press [DEL]. You should see the placeholder disappear. The [DEL] key can be thought of as the DELETE key.

Now what happens if you select the wrong part of your program? Nothing, because you can always ``unselect'' it. To unselect something, just press [F10] again. All the highlighting goes away and you can start over. Just to try it, press [F10] now and highlight any part of your code. Press [F10] again and all the highlighting goes away.

Now let's branch out a little. Your var declaration section should have a declaration like this.

c : char;  {A character}

Move the cursor to the type char in this declaration and select it by pressing [F10]. You will see the word char light up. What happens when you move the cursor to the left towards the variable name c? You should see the whole declaration light up now.

Screen Shot sel2 not available

Try moving the cursor up and down. You should see the lines around the declaration light up as well. If you move the cursor far enough, you'll see the whole program suddenly light up.

What is happening here? Alice looks at the position of the cursor and highlights the smallest complete block of Pascal code that contains both the cursor and the thing the cursor was pointing to when you pressed [F10]. If you start off at one declaration and move to an adjacent one, Alice highlights the two declarations. But what happens if you move to the keyword var that starts the variable declaration section? You'll see that Alice highlights the complete variable declaration section. If you highlight the start of a section, Alice will highlight the entire section and everything it contains, because Alice always highlights complete things. Sometimes the only complete block of code that contains what you have swept out is the entire program, so you will see the whole program light up.

For now, all we want to do is delete the declaration

c : char;  {A character}

so move the cursor back to this line. You should see that the highlighting shrinks again until only the declaration is highlighted. Delete the declaration by pressing [DEL] again.

You can now delete all the empty placeholders in your program. Just move to the placeholder, highlight it by pressing [F10], then press [DEL]. Do this to clean away all the placeholders you don't want.

Sometimes you want to delete something from your program but leave a placeholder behind; in fact, sometimes you must leave a placeholder behind to keep the program correct. To show what we mean, go to the first line of the for loop

for i := 1 to 10 do begin

Move the cursor on top of the 10 and press [F10]. The 10 will light up. What will happen if you press [DEL]? Try it.

You'll see that you get a Finish placeholder. Normally, pressing [DEL] doesn't leave a placeholder when it deletes things. However, Alice has to leave a placeholder in this case. Without such a placeholder, the for statement doesn't have the right form. (If you try this deletion, press [CTRL-U] afterwards to UNDO it.)

There is a second command for deleting things from your program. We call this command CLIP. To issue the CLIP command, highlight the section of code that you want to delete, then press [CTRL-P] (the P stands for ``prune'', a synonym for CLIP -- pruning and clipping hedges are the same thing). The difference between the first Delete command and CLIP is that CLIP always leaves a placeholder where the material was deleted. Another difference is that you cannot clip anything that would have to leave behind more than one placeholder. For example, if you have


you couldn't highlight both i and i*i and try to clip them, because that would have to leave behind


For the same reason, you can't normally clip two consecutive statements; you would have to DELETE them instead. CLIP leaves behind one and only one placeholder. Generally, you only use CLIP for small single things, like a parameter in a list being passed to a subprogram. (This is in fact the most common use of CLIP.)

A special note: many Alice commands, including DELETE and CLIP, let you alter small things without highlighting them first. You can delete what we call a leaf just by placing the cursor on it and doing a DELETE or CLIP. A leaf is a simple thing that doesn't contain any other things. Examples are variable names, constants, strings, comments and placeholders.

Changing Your Program

You can now do everything you need to create or edit a program. You know how to fill in placeholders, add placeholders, and delete placeholders.

On your screen now you should have the sample program we've been working with all along. Below we're going to list some changes you should try to make. Try to make the change yourself before reading our description of how we would make the change. Always run the program after you have made the change so you can have the satisfaction of seeing the new program work.

Have the program print out the first ten cubes instead of the first ten squares.

To do this, move the cursor to i*i in the writeln instruction. Press the [F10] key and move the cursor (if necessary) to highlight the i*i. Press [CTRL-P]. This deletes the i*i but leaves a Value placeholder behind. Fill in this placeholder with i*i*i. Press TAB when you want to indicate that you have finished typing the expression.

Have the program print out the first 20 cubes.

Move the cursor to the 10 in the first line of the for statement. Press [F10] to highlight the number. Press [CTRL-P] or [DEL] to delete the number, leaving behind a placeholder. Fill in the placeholder with the number 20. Press TAB when you want to indicate that you have finished typing the expression.

Have the program print out the first 20 squares and cubes.

You want a writeln instruction of the form

writeln( i, i*i, i*i*i );

Move the cursor to the first argument i. Press [CTRL-E] to extend the list of output values. This will create a new Value placeholder. Fill in this placeholder with i*i, then press TAB.

Have the program print out the first 20 squares, then the first 20 cubes.

Move the cursor to the start of the i*i*i argument in the writeln instruction. Press [F10]. Move the cursor right until the whole argument is highlighted. Press [DEL]. The argument will disappear, leaving no placeholder. Move the cursor to the end of the for loop. Press [CTRL-E] or [ENTER]. A new Statement placeholder will appear. Fill this placeholder with a for loop of the form

for i := 1 to 20 do begin

Run the program.

Put headings on both the list of squares and the list of cubes.

Move the cursor to the keyword for at the beginning of the first for loop. Press [INS]. A new Statement placeholder will appear. Put in a writeln instruction that prints out a heading, e.g.

writeln("The first twenty squares:");

Do the same for the second for loop.

Delete the ``writeln'' instruction that gives the heading for the table of squares and the for loop that prints out the squares.

Move the cursor to the keyword writeln in the writeln instruction that you want to delete. Press [F10]. Move the cursor down one line. You should see the writeln instruction and the whole for loop highlighted. Press [DEL].

Change the name of the program from ``squares'' to ``cubes.''

Move the cursor to the program name on the program statement. Press [F10], then [CTRL-P]. This will give you an empty Program-name placeholder. Fill in the program name appropriately.

Change the comment after the program statement to describe the new program.

Move the cursor to the comment. Press [F10] and move the cursor until the whole comment is highlighted. Press [CTRL-P] to get a new {Comment} placeholder. Fill in this placeholder with an appropriate comment.

Saving Changes

We have made a lot of changes to the program we have been working with. You could save a copy of the program as before, by pressing [ALT-S], but you don't want to do this at the moment. If you press [ALT-S], Alice will not ask you for a filename the way it did the first time you saved the program. Instead, it will automatically save the program in the file from which the program was loaded. Alice was set up this way so you only don't have to specify the file name every time you save the program.

However, our program has been changed so that it writes out cubes instead of squares; thus it doesn't make sense to save it in the old file named ``squares.ap''. Before we press [ALT-S] to save the program, we have to tell Alice to save it in a new file.

To do this, press [F8] to get the Files Menu. Choose the selection

Set Filename

Alice will give you the same sort of menu of files and directories that you got the first time you saved a program. Choose the selection

Enter a file name

from this menu and enter a new name, e.g. ``cubes.ap''. Press [ALT-S] now, and Alice will save the program in the new file instead of the old one.

Now that we've mentioned the Files Menu, let's go back to it and look at it in more detail. Press [F8] to call up the menu again. You'll notice that it has a Save selection and a Load New File selection (amongst others). Choose the Save one to see what happens. Remember that to choose something from a menu, you highlight the selection and then you press any key on the keyboard. When you choose Save, Alice saves a copy of your program in the current file, just as if you had pressed [ALT-S]. This is a general rule in Alice: choosing a command from a menu is exactly the same as pressing the special key or key sequence that issues that command.

To prove this, call up the Files Menu again by pressing [F8]. This time choose the Load New File selection. Alice will print the same kind of menu you get when you choose the

Load an existing Pascal Program

from the Starting Menu. Choose ``squares.ap'' from this menu. You will see that Alice loads back your old ``squares'' program.

Alice gives you a quicker way of loading programs, just as it gives you a quick way to save programs. Pressing [ALT-L] has exactly the same effect as choosing the Load New File selection from the Files Menu.

If you want, you can go on to change the ``squares'' program in different ways. Experimenting with Alice is the best way to learn how things work. Remember that when you want to quit, choose Quit ALICE from the Files Menu or press [ALT-Q].

Chapter Summary

In this chapter, we talked about

  1. Loading an existing program.
  2. Pressing [INS] to insert a new placeholder.
  3. Pressing [CTRL-E] to append a new placeholder.
  4. Highlighting sections of code by pressing [F10] and moving the cursor to ``sweep out'' the section.
  5. Unselecting highlighted code by pressing [F10] again.
  6. Deleting highlighted sections of code. Pressing [CTRL-P] always leaves an empty placeholder where the code was deleted. Pressing [DEL] only leaves an empty placeholder when the placeholder is needed to keep the program correct.
  7. Changing sections of code. Highlight the section first, delete it with [DEL] or [CTRL-P], then retype what you want.
  8. Using the Set Filename selection of the Files Menu if you want to save a changed program in a new file.
  9. Using the Load New File selection of the Files Menu or [ALT-L] if you want to load a new program.