2.2 The Read Evaluate Print Loop

GAP is an interactive system. It continuously executes a read evaluate print loop. Each expression you type at the keyboard is read by GAP, evaluated, and then the result is shown.

The interactive nature of GAP allows you to type an expression at the keyboard and see its value immediately. You can define a function and apply it to arguments to see how it works. You may even write whole programs containing lots of functions and test them without leaving the program.

When your program is large it will be more convenient to write it on a file and then read that file into GAP. Preparing your functions in a file has several advantages. You can compose your functions more carefully in a file (with your favorite text editor), you can correct errors without retyping the whole function and you can keep a copy for later use. Moreover you can write lots of comments into the program text, which are ignored by GAP, but are very useful for human readers of your program text. GAP treats input from a file in the same way that it treats input from the keyboard. Further details can be found in section Read in the Reference Manual.

A simple calculation with GAP is as easy as one can imagine. You type the problem just after the prompt, terminate it with a semicolon and then pass the problem to the program with the return key. For example, to multiply the difference between 9 and 7 by the sum of 5 and 6, that is to calculate (9 - 7) \* (5 + 6), you type exactly this last sequence of symbols followed by ; and return.

gap> (9 - 7) * (5 + 6);
22
gap> 
Then GAP echoes the result 22 on the next line and shows with the prompt that it is ready for the next problem. Henceforth, we will no longer print this additional prompt.

If you make a mistake while typing the line, but before typing the final return, you can use the delete key (or sometimes backspace key) to delete the last typed character. You can also move the cursor back and forward in the line with ctl-B and ctl-F and insert or delete characters anywhere in the line. The line editing commands are fully described in section Line Editing of the reference manual.

If you did omit the semicolon at the end of the line but have already typed return, then GAP has read everything you typed, but does not know that the command is complete. The program is waiting for further input and indicates this with a partial prompt >. This problem is solved by simply typing the missing semicolon on the next line of input. Then the result is printed and the normal prompt returns.

gap> (9 - 7) * (5 + 6)
> ;
22 
So the input can consist of several lines, and GAP prints a partial prompt > in each input line except the first, until the command is completed with a semicolon. (GAP may already evaluate part of the input when return is typed, so for long calculations it might take some time until the partial prompt appears.) Whenever you see the partial prompt and you cannot decide what GAP is still waiting for, then you have to type semicolons until the normal prompt returns. In every situation the exact meaning of the prompt gap> is that the program is waiting for a new problem.

But even if you mistyped the command more seriously, you do not have to type it all again. Suppose you mistyped or forgot the last closing parenthesis. Then your command is syntactically incorrect and GAP will notice it, incapable of computing the desired result.

gap> (9 - 7) * (5 + 6;
Syntax error: ) expected
(9 - 7) * (5 + 6;
                ^
Instead of the result an error message occurs indicating the place where an unexpected symbol occurred with an arrow sign ^ under it. As a computer program cannot know what your intentions really were, this is only a hint. But in this case GAP is right by claiming that there should be a closing parenthesis before the semicolon. Now you can type ctl-P to recover the last line of input. It will be written after the prompt with the cursor in the first position. Type ctl-E to take the cursor to the end of the line, then ctl-B to move the cursor one character back. The cursor is now on the position of the semicolon. Enter the missing parenthesis by simply typing ). Now the line is correct and may be passed to GAP by hitting the return key. Note that for this action it is not necessary to move the cursor past the last character of the input line.

Each line of commands you type is sent to GAP for evaluation by pressing return regardless of the position of the cursor in that line. We will no longer mention the return key from now on.

Sometimes a syntax error will cause GAP to enter a break loop. This is indicated by the special prompt brk>. If another syntax error occurs while GAP is in a break loop, the prompt will change to brk_02>, brk_03> and so on. You can leave the current break loop and exit to the next outer one by either typing quit; or by hitting ctl-D. Eventually GAP will return to its normal state and show its normal prompt gap> again.

[Top] [Previous] [Up] [Next] [Index]

GAP 4 manual
February 2000