Advanced ALICE Features

In this chapter, we will discuss some useful editing operations that go beyond the simple ones we have covered so far.

For this chapter, you should start up Alice and load in the sieve sample program that you used last chapter.

Immediate Mode and Single-stepping

We have already talked about single-stepping through your program, but we haven't done much with it. Single-stepping can really help you find bugs quickly if you use it correctly.

To use single-stepping profitably, you have to know about immediate mode. This lets you do work in between single steps. Here's how it works.

You should have the sieve program loaded up now. Press [F2] until you have stepped through the first few statements. Now, press [F6] to summon the Run-Time Menu and choose the Immediate mode block selection from that menu. This puts you into immediate mode.

You will see Alice draw an immediate mode block in the middle of your program. This looks like a little program on its own:

Screen Shot immblock not available


The Immediate block is your chance for a fast question and answer session with Alice. For example, now that you are in immediate mode, type

?i

You will see this turn into

writeln(i);

in the immediate block. If you now press [ENTER], the current value of i will be printed in the output area at the bottom of your screen. Type

?sum

and press [ENTER]. Alice will print out the current value of the variable sum. In fact, you can type ?name for any variable name in the program and Alice will print out that variable's value.

You can use ? to stand for

writeln(Value);

outside an immediate mode block too -- the shorthand works anytime.

Immediate mode can be a lot of help when you're trying to figure out what your program does. Just single-step, then go into immediate mode to find out what values have changed.

But immediate mode is even more versatile. For example, you can type in expressions like

? a / 3

(followed by [ENTER]) and Alice will type out the answer. This lets you use immediate mode like a little desk calculator. You can create new variables in the Immediate block's declaration area, and use these variables for simple work. Just declare a variable, then type a normal Pascal assignment, as in

b := 4;

Always press the [ENTER] key at the end of immediate mode directives like this, to tell Alice that you're finished typing. Alice will not execute your instructions until you press [ENTER].

When you are done working in immediate mode, simply press [F2] again to go back to single-stepping. Once you find out what is wrong with your program, you can go back to normal editing.

Immediate mode can also be handy if Alice stopped in the middle of a program because of an error. You can go into immediate mode and examine the values of your variables to see what went wrong. When you want to go back to normal editing, however, there's a catch. Alice is in the middle of running a program. Alice is using some portion of the computer's memory to hold information about the program in progress (e.g. the values of the program's variables). This information is sometimes called a suspended state, because it's a little like a single frame in a movie film -- it gives a picture of what your program is like at a particular point of execution.

If you want to single step your program from the beginning again, you no longer need the suspended state information because you aren't going to run this particular program any further.

To get rid of a suspended state, call up the Run-Time Menu by pressing [F6] and choose the

Pop suspended state

selection. This cleans away the suspended state information for your half-finished program so you can start from the beginning again. You shouldn't have to worry too much about this, since many operations pop suspended states for you when necessary.

If you want to go back to editing with the full-screen, press [F6] and choose the Debug off selection.

Another useful selection from the Run-Time Menu is

Pop suspended & Run

This clears away a half-finished program and then runs the program from the beginning.

Notice that when you leave immediate mode, your immediate code block stays in the middle of your program. If you leave immediate mode, single-step a bit, then go back into immediate mode, you can re-use these statements without having to type them again. For example, to re-use the instruction

writeln(i);

just move the cursor to the instruction and press [ENTER]. Alice will print out the new value of i.

You can experiment with immediate mode a little more if you want. When you done, you should get rid of the immediate mode block, just to keep the program tidy. Move the cursor to the beginning of the block and press [F10]. This should highlight the whole block. Press [DEL] to delete the block.

There's another thing you can do instead of single-stepping. You can do it right now, since you're stopped while single-stepping. Call up the Run-Time Menu by pressing [F6] and choose the Continue selection. Instead of more single-stepping, Alice will put on a burst of speed and execute the rest of your program normally. This lets you single-step for a while, then go on to normal execution when you've single-stepped enough.

Hiding and Revealing

The HIDE command hides a section of your code. To show what we mean, move the cursor to the first for loop of the sieve program and highlight the whole loop (by pressing [F10] and sweeping out that section of code). Also highlight the statement that comes just before the for (the first instruction of the program).

You're ready now to execute the HIDE command. You can do this in two ways: summon up the Changes Menu by pressing [F5] and then selecting the Hide entry; or by pressing [ALT-H]. You will see that Alice replaces the highlighted code with

 ... {Comment} ...

Has the code disappeared? Run the program and find out.

You will see that the program is working exactly the same as always. The code has simply been hidden. The designers of Alice knew that many parts of a program are necessary but not particularly tricky, i.e. straightforward code. Alice lets you hide such sections of code, so that you can concentrate on the more important or more complicated parts of the program. The hidden code is still there, but it doesn't take up space on the screen when the program is displayed.

The Comment placeholder is left in place of the hidden code, so you can put in a description of what the hidden code does. For example, you might fill in the comment you just made with

 ... {Init loop} ...

There's another reason why hidden code is convenient. Try single-stepping through your program by pressing [F2]. You will see that Alice does the hidden code in a single chunk. This means you can single step through unimportant code quickly until you get to the more important things.

Of course, sometimes you will find that the hidden code isn't so unimportant after all, and you'll want to look at it again. The REVEAL command brings back code that has been hidden. Before you issue the REVEAL command, the cursor must be positioned on hidden code. Move the cursor to the code we just hide, i.e. anywhere on the

 ... {Init loop} ...

line. To reveal what was there, just press [ALT-R] or choose the Reveal selection from the Changes Menu. You will see that Alice restores the hidden code.

Screen Shot restcode not available

Notice that Alice leaves the comment where it is. If you choose to hide the code again, the comment that you made the first time will be put back in place again. Hide the code again, and you will see how it works. You will also notice that the comment is automatically highlighted when you highlight the code that was hidden -- from this point onward, the comment is regarded as part of that code.

Workspaces

Call up the Miscellaneous Menu by pressing [SHIFT-F9]. You'll notice that one of the entries reads

Change Workspace

The next natural question is, ``What is a workspace?''

You can picture a workspace as a ``notebook'' inside the computer, a place where programs can be written down. When you load a program from a file, Alice actually gets the program, copies it all into the workspace notebook, and then displays the contents of the notebook on the screen. This means that you have been using a workspace ever since you started using Alice -- you just didn't use the name.

The Change Workspace entry on the Miscellaneous Menu tells Alice that you want to work in a different notebook for a while. Alice sets aside the old notebook containing the old program and asks you what new workspace notebook you would like to use.

To see how this works, start by loading the sieve program. Next, select the Change Workspace entry from the menu by highlighting it and pressing any key. Alice will provide you with a menu of workspaces that you're currently using. Of course, the only workspace you have right now is the one you started out with. This workspace happens to be called ``main''. (You may have noticed

WS: main

on the bottom of your display screen while you have been using Alice. Now you understand that this means you are in a WorkSpace called ``main''.) The workspace menu also has an entry that reads

New workspace

This is what you should choose now, because we're going to go to a new workspace. When you choose this entry, Alice will ask

Workspace Name?

to find out what new workspace (notebook) you want to look at. Alice wants you to type in a name for the workspace. (This name should follow the same rules that Pascal uses for variable names.) So let's type in a name like this:

Workspace Name? newprog

Press [ENTER] when you have entered the workspace name, so Alice knows that you have typed the whole name.

Once you do this, Alice will lay out a new program template, the way it does when you first start Alice up. You will also notice that the bottom line of the screen shows

WS: newprog

to indicate that you are now in the workspace named ``newprog''. When you start a new notebook, you can do anything you want with it. For example, create the following program in this workspace.

program sums(input,output);
var
    i : integer; {A counter}
    sum : integer; {Sum}
begin
sum := 0;
for i := 1 to 15 do begin
    sum := sum + i;
    end;
writeln('The sum of the first 15 integers is ',sum);
sum := 0;
for i := 1 to 15 do begin
    sum := sum + i*i;
    end;
writeln('The sum of the first 15 squares is ',sum);
end.

What has happened to the sieve program in the meantime? Alice just set the notebook aside for a moment so you could work in the workspace ``newprog''. To get back to the old program, select Change Workspace from the Miscellaneous Menu again. This time when Alice shows the menu of available workspaces, choose ``main''. When you've done this, Alice will return you to the sums program. By switching back and forth, you can actually be editing three or four programs all at the same time.

You might wonder why you would want to edit several programs at a time. One answer is that different programs often have identical sections of code. For example, two programs may both be able to use the same function or procedure, or maybe they may share a set of type declarations. In such cases, having more than one workspace can save you a lot of typing. You just have to create the shared code once, and then you can move or copy it from a program in one workspace to a program in another.

Here's an introduction to how it works. Remember that the Deletion Menu had an entry called Move to workspace? We're going to use it now, but first we have to highlight something to move. Go to the ``newprog'' workspace and highlight the code

sum := 0;
for i := 1 to 15 do begin
    sum := sum + i*i;
    end;
writeln('The sum of the first 15 squares is ',sum);

You should know how to select this code by now. Now, call up the Deletion Menu by pressing [F3] and select the Move to workspace entry.

Again, Alice will offer a menu of existing workspaces. We want another new workspace, so choose the

New workspace

selection. You will see that Alice asks

Workspace Name?

and waits for you to answer.

What is happening here? The MOVE command moves code to another workspace. This is something like taking a page of looseleaf paper out of one notebook and putting it into another. Let's use the name ``squareloop'' for the workspace where we're sending this code. Type ``squareloop'' in answer to Alice's question, then press [ENTER].

The highlighted material will disappear. Where has it gone? To the ``squareloop'' workspace. How can we get it back? That's the next step.

Move the cursor to the begin that starts the instruction part of your program. Press [ENTER] to get a Statement placeholder immediately following the begin. Put the cursor on this placeholder. Now, summon the Insertion Menu by pressing [F4], and choose the Get workspace selection. When Alice offers the usual menu of workspaces, choose ``squareloop''.

What happens this time? Alice uses the code from the ``squareloop'' workspace to fill in the Statement placeholder. By moving the block of code to a workspace and then getting the code back in a different location, we have moved the whole block of code from one place in the program to another. If you run the program now, you will see that the sum of the first 15 integers is printed after the first 15 squares.

Now, let's go back to something we were talking about earlier, moving code from one program to a different program. The process is exactly the same. LOAD the two programs into separate workspaces. MOVE the code from the original program to an unused workspace. Change workspaces to the second program. GET the code from the workspace where it was being held.

There's one more command that plays around with workspaces. The Copy selection on the Changes Menu is like MOVE, in that it puts code from one workspace into another workspace. However, COPY doesn't remove the code from the original workspace. In other words, if you highlight a section of code and copy it, a copy of the highlighted code goes into the destination workspace and the original stays in its home workspace. With MOVE, the highlighted code disappears from its home workspace and reappears in the destination workspace.

When MOVE and COPY send code to another workspace, they get rid of whatever was already in that workspace. This means that you can't just MOVE or COPY code into another program. You have to MOVE or COPY into an unused workspace, then GET the material for the other program.

Chapter Summary

In this chapter, we talked about

  1. Using Immediate Mode blocks.
  2. Cleaning away half-finished programs with the Pop suspended state selection of the Run-Time Menu.
  3. The Continue selection of the Run-Time Menu.
  4. The HIDE command ([ALT-H]).
  5. The REVEAL command ([ALT-R]).
  6. Changing workspaces.
  7. Moving source code to workspaces and getting it back.
  8. Copying source code into a workspace.