6.1 Main Loop

The normal interaction with GAP happens in the so--called read--eval--print loop. This means that you type an input, GAP first reads it, evaluates it, and then shows the result. Note that the term print may be confusing since there is a GAP function called Print (see View and Print) which is in fact not used in the read--eval--print loop, but traditions are hard to break. In the following, whenever we want to express that GAP places some characters on the standard output, we will say that GAP shows something.

The exact sequence in the read eval print loop is as follows.

To signal that it is ready to accept your input, GAP shows the prompt gap>. When you see this, you know that GAP is waiting for your input.

Note that every statement must be terminated by a semicolon. You must also enter return before GAP starts to read and evaluate your input. Because GAP does not do anything until you enter return, you can edit your input to fix typos and only when everything is correct enter return and have GAP take a look at it (see Line Editing). It is also possible to enter several statements as input on a single line. Of course each statement must be terminated by a semicolon.

It is absolutely acceptable to enter a single statement on several lines. When you have entered the beginning of a statement, but the statement is not yet complete, and you enter return, GAP will show the partial prompt >. When you see this, you know that GAP is waiting for the rest of the statement. This happens also when you forget the semicolon ; that terminates every GAP statement. Note that when return has been entered and the current statement is not yet complete, GAP will already evaluate those parts of the input that are complete, for example function calls that appear as arguments in another function call which needs several input lines. So it may happen that one has to wait some time for the partial prompt.

When you enter return, GAP first checks your input to see if it is syntactically correct (see Chapter The Programming Language for the definition of syntactically correct). If it is not, GAP prints an error message of the following form

gap> 1 * ;
Syntax error: expression expected
1 * ;
    ^ 

The first line tells you what is wrong about the input, in this case the * operator takes two expressions as operands, so obviously the right one is missing. If the input came from a file (see Read), this line will also contain the filename and the line number. The second line is a copy of the input. And the third line contains a caret pointing to the place in the previous line where GAP realized that something is wrong. This need not be the exact place where the error is, but it is usually quite close.

Sometimes, you will also see a partial prompt after you have entered an input that is syntactically incorrect. This is because GAP is so confused by your input, that it thinks that there is still something to follow. In this case you should enter ;return repeatedly, ignoring further error messages, until you see the full prompt again. When you see the full prompt, you know that GAP forgave you and is now ready to accept your next -- hopefully correct -- input.

If your input is syntactically correct, GAP evaluates or executes it, i.e., performs the required computations (see Chapter The Programming Language for the definition of the evaluation).

If you do not see a prompt, you know that GAP is still working on your last input. Of course, you can type ahead, i.e., already start entering new input, but it will not be accepted by GAP until GAP has completed the ongoing computation.

When GAP is ready it will usually show the result of the computation, i.e., the value computed. Note that not all statements produce a value, for example, if you enter a for loop, nothing will be printed, because the for loop does not produce a value that could be shown.

Also sometimes you do not want to see the result. For example if you have computed a value and now want to assign the result to a variable, you probably do not want to see the value again. You can terminate statements by two semicolons to suppress showing the result.

If you have entered several statements on a single line GAP will first read, evaluate, and show the first one, then read, evaluate, and show the second one, and so on. This means that the second statement will not even be checked for syntactical correctness until GAP has completed the first computation.

After the result has been shown GAP will display another prompt, and wait for your next input. And the whole process starts all over again. Note that if you have entered several statements on a single line, a new prompt will only be printed after GAP has read, evaluated, and shown the last statement.

In each statement that you enter, the result of the previous statement that produced a value is available in the variable last. The next to previous result is available in last2 and the result produced before that is available in last3.

gap> 1; 2; 3;
1
2
3
gap> last3 + last2 * last;
7 

Also in each statement the time spent by the last statement, whether it produced a value or not, is available in the variable time. This is an integer that holds the number of milliseconds.

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

GAP 4 manual
February 2000