CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

| Download

GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it

Path: gap4r8 / doc / tut / chap2.txt
Views: 418346
1
2
2 A First Session with GAP
3
4
This tutorial introduces you to the GAP system. It is written with users in
5
mind who have just managed to start GAP for the first time on their computer
6
and want to learn the basic facts about GAP by playing around with some
7
instructive examples. Therefore, this tutorial contains at many places
8
examples consisting of several lines of input (which you should type on your
9
terminal) followed by the corresponding output ( which GAP produces as an
10
answer to your input).
11
12
We encourage you to actually run through these examples on your computer.
13
This will support your feeling for GAP as a tool, which is the leading aim
14
of this tutorial. Do not believe any statement in it as long as you cannot
15
verify it for your own version of GAP. You will learn to distinguish between
16
small deviations of the behavior of your personal GAP from the printed
17
examples and serious nonsense.
18
19
Since the printing routines of GAP are in some sense machine dependent you
20
will for instance encounter a different layout of the printed objects in
21
different environments. But the contents should always be the same. In case
22
you encounter serious nonsense it is highly recommended that you send a bug
23
report to mailto:[email protected].
24
25
The examples in this tutorial should explain everything you have to know in
26
order to be able to use GAP. The reference manual then gives a more
27
systematic treatment of the various types of objects that GAP can
28
manipulate. It seems desirable neither to start this systematic course with
29
the most elementary (and most boring) structures, nor to confront you with
30
all the complex data types before you know how they are composed from
31
elementary structures. For this reason this tutorial wants to provide you
32
with a basic understanding of GAP objects, on which the reference manual
33
will then build when it explains everything in detail. So after having
34
mastered this tutorial, you can immediately plunge into the exciting parts
35
of GAP and only read detailed information about elementary things (in the
36
reference manual) when you really need them.
37
38
Each chapter of this tutorial contains a section with references to the
39
reference manual at the end.
40
41
42
2.1 Starting and Leaving GAP
43
44
If the program is correctly installed then you usually start GAP by simply
45
typing gap at the prompt of your operating system followed by the Return
46
key, sometimes this is also called the Newline key.
47
48
 Example 
49
$ gap
50

51
52
GAP answers your request with its beautiful banner and then it shows its own
53
prompt gap> asking you for further input. (You can avoid the banner with the
54
command line option -b; more command line options are described in
55
Section 'Reference: Command Line Options'.)
56
57
 Example 
58
gap> 
59

60
61
The usual way to end a GAP session is to type quit; at the gap> prompt. Do
62
not omit the semicolon!
63
64
 Example 
65
gap> quit;
66
$ 
67

68
69
On some systems you could type Ctrl-D to yield the same effect. In any
70
situation GAP is ended by typing Ctrl-C twice within a second. Here as
71
always, a combination like Ctrl-D means that you have to press the D key
72
while you hold down the Ctrl key.
73
74
On some systems (for example the Apple Macintosh) minor changes might be
75
necessary. This is explained in GAP installation instructions (see the
76
INSTALL file in the GAP root directory, or the GAP website).
77
78
In most places whitespace characters (i.e. Spaces, Tabs and Returns) are
79
insignificant for the meaning of GAP input. Identifiers and keywords must
80
however not contain any whitespace. On the other hand, sometimes there must
81
be whitespace around identifiers and keywords to separate them from each
82
other and from numbers. We will use whitespace to format more complicated
83
commands for better readability.
84
85
A comment in GAP starts with the symbol # and continues to the end of the
86
line. Comments are treated like whitespace by GAP. We use comments in the
87
printed examples in this tutorial to explain certain lines of input or
88
output.
89
90
91
2.2 Loading Source Code from a File
92
93
The most convenient way of creating larger pieces of GAP code is to write
94
them to some text file. For this purpose you can simply use your favorite
95
text editor. You can load such a file into GAP using the Read (Reference:
96
Read) function:
97
98
 Example 
99
gap> Read("../../GAPProgs/Example.g");
100

101
102
You can either give the full absolute path name of the source file or its
103
relative path name from the GAP root directory (the directory containing
104
bin/, doc/, lib/, etc.).
105
106
107
2.3 The Read Evaluate Print Loop
108
109
GAP is an interactive system. It continuously executes a read evaluate print
110
loop. Each expression you type at the keyboard is read by GAP, evaluated,
111
and then the result is shown.
112
113
The interactive nature of GAP allows you to type an expression at the
114
keyboard and see its value immediately. You can define a function and apply
115
it to arguments to see how it works. You may even write whole programs
116
containing lots of functions and test them without leaving the program.
117
118
When your program is large it will be more convenient to write it on a file
119
and then read that file into GAP. Preparing your functions in a file has
120
several advantages. You can compose your functions more carefully in a file
121
(with your favorite text editor), you can correct errors without retyping
122
the whole function and you can keep a copy for later use. Moreover you can
123
write lots of comments into the program text, which are ignored by GAP, but
124
are very useful for human readers of your program text. GAP treats input
125
from a file in the same way that it treats input from the keyboard. Further
126
details can be found in section Read (Reference: Read).
127
128
A simple calculation with GAP is as easy as one can imagine. You type the
129
problem just after the prompt, terminate it with a semicolon and then pass
130
the problem to the program with the Return key. For example, to multiply the
131
difference between 9 and 7 by the sum of 5 and 6, that is to calculate (9 -
132
7) * (5 + 6), you type exactly this last sequence of symbols followed by ;
133
and Return.
134
135
 Example 
136
gap> (9 - 7) * (5 + 6);
137
22
138
gap> 
139

140
141
Then GAP echoes the result 22 on the next line and shows with the prompt
142
that it is ready for the next problem. Henceforth, we will no longer print
143
this additional prompt.
144
145
If you make a mistake while typing the line, but before typing the final
146
Return, you can use the Delete key (or sometimes Backspace key) to delete
147
the last typed character. You can also move the cursor back and forward in
148
the line with Ctrl-B and Ctrl-F and insert or delete characters anywhere in
149
the line. The line editing commands are fully described in
150
section 'Reference: Line Editing'.
151
152
If you did omit the semicolon at the end of the line but have already typed
153
Return, then GAP has read everything you typed, but does not know that the
154
command is complete. The program is waiting for further input and indicates
155
this with a partial prompt >. This problem is solved by simply typing the
156
missing semicolon on the next line of input. Then the result is printed and
157
the normal prompt returns.
158
159
 Example 
160
gap> (9 - 7) * (5 + 6)
161
> ;
162
22
163

164
165
So the input can consist of several lines, and GAP prints a partial prompt >
166
in each input line except the first, until the command is completed with a
167
semicolon. (GAP may already evaluate part of the input when Return is typed,
168
so for long calculations it might take some time until the partial prompt
169
appears.) Whenever you see the partial prompt and you cannot decide what GAP
170
is still waiting for, then you have to type semicolons until the normal
171
prompt returns. In every situation the exact meaning of the prompt gap> is
172
that the program is waiting for a new problem.
173
174
But even if you mistyped the command more seriously, you do not have to type
175
it all again. Suppose you mistyped or forgot the last closing parenthesis.
176
Then your command is syntactically incorrect and GAP will notice it,
177
incapable of computing the desired result.
178
179
 Example 
180
gap> (9 - 7) * (5 + 6;
181
Syntax error: ) expected
182
(9 - 7) * (5 + 6;
183
 ^
184

185
186
Instead of the result an error message occurs indicating the place where an
187
unexpected symbol occurred with an arrow sign ^ under it. As a computer
188
program cannot know what your intentions really were, this is only a hint.
189
But in this case GAP is right by claiming that there should be a closing
190
parenthesis before the semicolon. Now you can type Ctrl-P to recover the
191
last line of input. It will be written after the prompt with the cursor in
192
the first position. Type Ctrl-E to take the cursor to the end of the line,
193
then Ctrl-B to move the cursor one character back. The cursor is now on the
194
position of the semicolon. Enter the missing parenthesis by simply typing ).
195
Now the line is correct and may be passed to GAP by hitting the Return key.
196
Note that for this action it is not necessary to move the cursor past the
197
last character of the input line.
198
199
Each line of commands you type is sent to GAP for evaluation by pressing
200
Return regardless of the position of the cursor in that line. We will no
201
longer mention the Return key from now on.
202
203
Sometimes a syntax error will cause GAP to enter a break loop. This is
204
indicated by the special prompt brk>. If another syntax error occurs while
205
GAP is in a break loop, the prompt will change to brk_02>, brk_03> and so
206
on. You can leave the current break loop and exit to the next outer one by
207
either typing quit; or by hitting Ctrl-D. Eventually GAP will return to its
208
normal state and show its normal prompt gap> again.
209
210
211
2.4 Constants and Operators
212
213
In an expression like (9 - 7) * (5 + 6) the constants 5, 6, 7, and 9 are
214
being composed by the operators +, * and - to result in a new value.
215
216
There are three kinds of operators in GAP, arithmetical operators,
217
comparison operators, and logical operators. You have already seen that it
218
is possible to form the sum, the difference, and the product of two integer
219
values. There are some more operators applicable to integers in GAP. Of
220
course integers may be divided by each other, possibly resulting in
221
noninteger rational values.
222
223
 Example 
224
gap> 12345/25;
225
2469/5
226

227
228
Note that the numerator and denominator are divided by their greatest common
229
divisor and that the result is uniquely represented as a division
230
instruction.
231
232
The next self-explanatory example demonstrates negative numbers.
233
234
 Example 
235
gap> -3; 17 - 23;
236
-3
237
-6
238

239
240
The exponentiation operator is written as ^. This operation in particular
241
might lead to very large numbers. This is no problem for GAP as it can
242
handle numbers of (almost) any size.
243
244
 Example 
245
gap> 3^132;
246
955004950796825236893190701774414011919935138974343129836853841
247

248
249
The mod operator allows you to compute one value modulo another.
250
251
 Example 
252
gap> 17 mod 3;
253
2
254

255
256
Note that there must be whitespace around the keyword mod in this example
257
since 17mod3 or 17mod would be interpreted as identifiers. The whitespace
258
around operators that do not consist of letters, e.g., the operators * and
259
-, is not necessary.
260
261
GAP knows a precedence between operators that may be overridden by
262
parentheses.
263
264
 Example 
265
gap> (9 - 7) * 5 = 9 - 7 * 5;
266
false
267

268
269
Besides these arithmetical operators there are comparison operators in GAP.
270
A comparison results in a boolean value which is another kind of constant.
271
The comparison operators =, <>, <, <=, > and >=, test for equality,
272
inequality, less than, less than or equal, greater than and greater than or
273
equal, respectively.
274
275
 Example 
276
gap> 10^5 < 10^4;
277
false
278

279
280
The boolean values true and false can be manipulated via logical operators,
281
i. e., the unary operator not and the binary operators and and or. Of course
282
boolean values can be compared, too.
283
284
 Example 
285
gap> not true; true and false; true or false;
286
false
287
false
288
true
289
gap> 10 > 0 and 10 < 100;
290
true
291

292
293
Another important type of constants in GAP are permutations. They are
294
written in cycle notation and they can be multiplied.
295
296
 Example 
297
gap> (1,2,3);
298
(1,2,3)
299
gap> (1,2,3) * (1,2);
300
(2,3)
301

302
303
The inverse of the permutation (1,2,3) is denoted by (1,2,3)^-1. Moreover
304
the caret operator ^ is used to determine the image of a point under a
305
permutation and to conjugate one permutation by another.
306
307
 Example 
308
gap> (1,2,3)^-1;
309
(1,3,2)
310
gap> 2^(1,2,3);
311
3
312
gap> (1,2,3)^(1,2);
313
(1,3,2)
314

315
316
The various other constants that GAP can deal with will be introduced when
317
they are used, for example there are elements of finite fields such as Z(8),
318
and complex roots of unity such as E(4).
319
320
The last type of constants we want to mention here are the characters, which
321
are simply objects in GAP that represent arbitrary characters from the
322
character set of the operating system. Character literals can be entered in
323
GAP by enclosing the character in singlequotes '.
324
325
 Example 
326
gap> 'a';
327
'a'
328
gap> '*';
329
'*'
330

331
332
There are no operators defined for characters except that characters can be
333
compared.
334
335
In this section you have seen that values may be preceded by unary operators
336
and combined by binary operators placed between the operands. There are
337
rules for precedence which may be overridden by parentheses. A comparison
338
results in a boolean value. Boolean values are combined via logical
339
operators. Moreover you have seen that GAP handles numbers of arbitrary
340
size. Numbers and boolean values are constants. There are other types of
341
constants in GAP like permutations. You are now in a position to use GAP as
342
a simple desktop calculator.
343
344
345
2.5 Variables versus Objects
346
347
The constants described in the last section are specified by certain
348
combinations of digits and minus signs (in the case of integers) or digits,
349
commas and parentheses (in the case of permutations). These sequences of
350
characters always have the same meaning to GAP. On the other hand, there are
351
variables, specified by a sequence of letters and digits (including at least
352
one letter), and their meaning depends on what has been assigned to them. An
353
assignment is done by a GAP command sequence_of_letters_and_digits :=
354
meaning, where the sequence on the left hand side is called the identifier
355
of the variable and it serves as its name. The meaning on the right hand
356
side can be a constant like an integer or a permutation, but it can also be
357
almost any other GAP object. From now on, we will use the term object to
358
denote something that can be assigned to a variable.
359
360
There must be no whitespace between the : and the = in the assignment
361
operator. Also do not confuse the assignment operator with the single
362
equality sign = which in GAP is only used for the test of equality.
363
364
 Example 
365
gap> a:= (9 - 7) * (5 + 6);
366
22
367
gap> a;
368
22
369
gap> a * (a + 1);
370
506
371
gap> a = 10;
372
false
373
gap> a:= 10;
374
10
375
gap> a * (a + 1);
376
110
377

378
379
After an assignment the assigned object is echoed on the next line. The
380
printing of the object of a statement may be in every case prevented by
381
typing a double semicolon.
382
383
 Example 
384
gap> w:= 2;; 
385

386
387
After the assignment the variable evaluates to that object if evaluated.
388
Thus it is possible to refer to that object by the name of the variable in
389
any situation.
390
391
This is in fact the whole secret of an assignment. An identifier is bound to
392
an object and from this moment points to that object. Nothing more. This
393
binding is changed by the next assignment to that identifier. An identifier
394
does not denote a block of memory as in some other programming languages. It
395
simply points to an object, which has been given its place in memory by the
396
GAP storage manager. This place may change during a GAP session, but that
397
doesn't bother the identifier. The identifier points to the object, not to a
398
place in the memory.
399
400
For the same reason it is not the identifier that has a type but the object.
401
This means on the other hand that the identifier a which now is bound to an
402
integer object may in the same session point to any other object regardless
403
of its type.
404
405
Identifiers may be sequences of letters and digits containing at least one
406
letter. For example abc and a0bc1 are valid identifiers. But also 123a is a
407
valid identifier as it cannot be confused with any number. Just 1234
408
indicates the number 1234 and cannot be at the same time the name of a
409
variable.
410
411
Since GAP distinguishes upper and lower case, a1 and A1 are different
412
identifiers. Keywords such as quit must not be used as identifiers. You will
413
see more keywords in the following sections.
414
415
In the remaining part of this manual we will ignore the difference between
416
variables, their names (identifiers), and the objects they point to. It may
417
be useful to think from time to time about what is really meant by terms
418
such as the integer w.
419
420
There are some predefined variables coming with GAP. Many of them you will
421
find in the remaining chapters of this manual, since functions are also
422
referred to via identifiers.
423
424
You can get an overview of all GAP variables by entering NamesGVars(). Many
425
of these are predefined. If you are interested in the variables you have
426
defined yourself in the current GAP session, you can enter NamesUserGVars().
427
428
 Example 
429
gap> NamesUserGVars();
430
[ "a", "w" ]
431

432
433
This seems to be the right place to state the following rule: The name of
434
every global variable in the GAP library starts with a capital letter. Thus
435
if you choose only names starting with a small letter for your own variables
436
you will not attempt to overwrite any predefined variable. (Note that most
437
of the predefined variables are read-only, and trying to change their values
438
will result in an error message.)
439
440
There are some further interesting variables one of which will be introduced
441
now.
442
443
Whenever GAP returns an object by printing it on the next line this object
444
is assigned to the variable last. So if you computed
445
446
 Example 
447
gap> (9 - 7) * (5 + 6);
448
22
449

450
451
and forgot to assign the object to the variable a for further use, you can
452
still do it by the following assignment.
453
454
 Example 
455
gap> a:= last;
456
22
457

458
459
Moreover there are variables last2 and last3, you can guess their values.
460
461
In this section you have seen how to assign objects to variables. These
462
objects can later be accessed through the name of the variable, its
463
identifier. You have also encountered the useful concept of the last
464
variables storing the latest returned objects. And you have learned that a
465
double semicolon prevents the result of a statement from being printed.
466
467
468
2.6 Objects vs. Elements
469
470
In the last section we mentioned that every object is given a certain place
471
in memory by the GAP storage manager (although that place may change in the
472
course of a GAP session). In this sense, objects at different places in
473
memory are never equal, and if the object pointed to by the variable a (to
474
be more precise, the variable with identifier a) is equal to the object
475
pointed to by the variable b, then we should better say that they are not
476
only equal but identical. GAP provides the function IsIdenticalObj
477
(Reference: IsIdenticalObj) to test whether this is the case.
478
479
 Example 
480
gap> a:= (1,2);; IsIdenticalObj( a, a );
481
true
482
gap> b:= (1,2);; IsIdenticalObj( a, b );
483
false
484
gap> b:= a;; IsIdenticalObj( a, b );
485
true
486

487
488
As the above example indicates, GAP objects a and b can be unequal although
489
they are equal from a mathematical point of view, i.e., although we should
490
have a = b. It may be that the objects a and b are stored in different
491
places in memory, or it may be that we have an equivalence relation defined
492
on the set of objects under which a and b belong to the same equivalence
493
class. For example, if a = x^3 and b = x^{-5} are words in the finitely
494
presented group ⟨ x ∣ x^2 = 1 ⟩, we would have a = b in that group.
495
496
GAP uses the equality operator = to denote such a mathematical equality, not
497
the identity of objects. Hence we often have a = b although IsIdenticalObj(
498
a, b ) = false. The operator = defines an equivalence relation on the set of
499
all GAP objects, and we call the corresponding equivalence classes elements.
500
Phrasing it differently, the same element may be represented by various GAP
501
objects.
502
503
Non-trivial examples of elements that are represented by different objects
504
(objects that really look different, not ones that are merely stored in
505
different memory places) will occur only when we will be considering
506
composite objects such as lists or domains.
507
508
509
2.7 About Functions
510
511
A program written in the GAP language is called a function. Functions are
512
special GAP objects. Most of them behave like mathematical functions. They
513
are applied to objects and will return a new object depending on the input.
514
The function Factorial (Reference: Factorial), for example, can be applied
515
to an integer and will return the factorial of this integer.
516
517
 Example 
518
gap> Factorial(17);
519
355687428096000
520

521
522
Applying a function to arguments means to write the arguments in parentheses
523
following the function. Several arguments are separated by commas, as for
524
the function Gcd (Reference: Gcd) which computes the greatest common divisor
525
of two integers.
526
527
 Example 
528
gap> Gcd(1234, 5678);
529
2
530

531
532
There are other functions that do not return an object but only produce a
533
side effect, for example changing one of their arguments. These functions
534
are sometimes called procedures. The function Print (Reference: Print) is
535
only called for the side effect of printing something on the screen.
536
537
 Example 
538
gap> Print(1234, "\n");
539
1234
540

541
542
In order to be able to compose arbitrary text with Print (Reference: Print),
543
this function itself will not produce a line break after printing. Thus we
544
had another newline character "\n" printed to start a new line.
545
546
Some functions will both change an argument and return an object such as the
547
function Sortex (Reference: Sortex) that sorts a list and returns the
548
permutation of the list elements that it has performed. You will not
549
understand right now what it means to change an object. We will return to
550
this subject several times in the next sections.
551
552
A comfortable way to define a function yourself is the maps-to operator ->
553
consisting of a minus sign and a greater sign with no whitespace between
554
them. The function cubed which maps a number to its cube is defined on the
555
following line.
556
557
 Example 
558
gap> cubed:= x -> x^3;
559
function( x ) ... end
560

561
562
After the function has been defined, it can now be applied.
563
564
 Example 
565
gap> cubed(5);
566
125
567

568
569
More complicated functions, especially functions with more than one argument
570
cannot be defined in this way. You will see how to write your own GAP
571
functions in Section 4.1.
572
573
In this section you have seen GAP objects of type function. You have learned
574
how to apply a function to arguments. This yields as result a new object or
575
a side effect. A side effect may change an argument of the function.
576
Moreover you have seen an easy way to define a function in GAP with the
577
maps-to operator.
578
579
580
2.8 Help
581
582
The content of the GAP manuals is also available as on-line help. A GAP
583
session loads a long list of index entries. This typically contains all
584
chapter and section headers, all names of documented functions, operations
585
and so on, as well as some explicit index entries defined in the manuals.
586
587
The format of a query is as follows.
588
589
?[book:][?]topic
590
591
A simple example would be to type ?help at the GAP prompt. If there is a
592
single section with index entry topic then this is displayed directly.
593
594
If there are several matches you get an overview like in the example below.
595
596
 Example 
597
gap> ?sets
598
Help: several entries match this topic - type ?2 to get match [2]
599

600
[1] Tutorial: Sets
601
[2] Reference: Sets
602
[3] Reference: sets
603
[4] Reference: Sets of Subgroups
604
[5] Reference: setstabilizer
605

606
607
GAP's manuals consist of several books, which are indicated before the colon
608
in the list above. A help query can be restricted to one book by using the
609
optional book: part. For example ?tut : sets will display the first of these
610
help sections. More precisely, the parts of the string book which are
611
separated by white space are interpreted as beginnings of the first words in
612
the name of the book. Try ?books to see the list of available books and
613
their names.
614
615
The search for a matching topic (and optional book) is done case
616
insensitively. If there is another ? before the topic, then a substring
617
search for topic is performed on all index entries. Otherwise the parts of
618
topic which are separated by white space are considered as beginnings of the
619
first words in an index entry.
620
621
White space is normalized in the search string (and the index entries).
622
623
Examples. All the following queries lead to the chapter of the reference
624
manual which explains the use of GAP's help system in more detail.
625
626
 Example 
627
gap> ?Reference: The Help System
628
gap> ? REF : t h s
629
gap> ?ref:? help system 
630

631
632
The query ??sets shows all help sections in all books whose index entries
633
contain the substring sets.
634
635
As mentioned in the example above a complete list of commands for the help
636
system is available in Section ?Ref: The Help System of the reference
637
manual. In particular there are commands to browse through the help
638
sections, see ?Ref: Browsing through the Sections and there is a way to
639
influence the way how the help sections are displayed, see ?Ref:
640
SetHelpViewer. For example you can use an external pager program, a Web
641
browser, dvi-previewer and/or pdf-viewer for reading GAP's online help.
642
643
644
2.9 Further Information introducing the System
645
646
For large amounts of input data, it might be advisable to write your input
647
first into a file, and then read this into GAP; see Read (Reference: Read),
648
Edit (Reference: Edit) for this.
649
650
The definition of the GAP syntax can be looked up in Chapter 'Reference: The
651
Programming Language'. A complete list of command line editing facilities is
652
found in Section 'Reference: Line Editing'. The break loop is described in
653
Section 'Reference: Break Loops'.
654
655
Operators are explained in more detail in Sections 'Reference: Expressions'
656
and 'Reference: Comparisons'. You will find more information about boolean
657
values in Chapters 'Reference: Booleans' and 'Reference: Boolean Lists'.
658
Permutations are described in Chapter 'Reference: Permutations' and
659
characters in Chapter 'Reference: Strings and Characters'.
660
661
Variables and assignments are described in more detail in 'Reference:
662
Variables' and 'Reference: Assignments'. A complete list of keywords is
663
contained in 'Reference: Keywords'.
664
665
More about functions can be found in 'Reference: Function Calls'
666
and 'Reference: Procedure Calls'.
667
668
669