home *** CD-ROM | disk | FTP | other *** search
/ Math Solutions 1995 October / Math_Solutions_CD-ROM_Walnut_Creek_October_1995.iso / pc / mac / discrete / doc / language.tex < prev    next >
Encoding:
Text File  |  1993-05-05  |  47.9 KB  |  1,062 lines

  1. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2. %%
  3. %A  language.tex                GAP documentation            Martin Schoenert
  4. %%
  5. %A  @(#)$Id: language.tex,v 3.10 1993/02/19 10:48:42 gap Exp $
  6. %%
  7. %Y  Copyright 1990-1992,  Lehrstuhl D fuer Mathematik,  RWTH Aachen,  Germany
  8. %%
  9. %%  This file describes the {\GAP} programming language.
  10. %%
  11. %H  $Log: language.tex,v $
  12. %H  Revision 3.10  1993/02/19  10:48:42  gap
  13. %H  adjustments in line length and spelling
  14. %H
  15. %H  Revision 3.9  1993/02/18  15:52:09  felsch
  16. %H  index entries added
  17. %H
  18. %H  Revision 3.8  1993/02/17  07:43:50  felsch
  19. %H  examples fixed
  20. %H
  21. %H  Revision 3.7  1993/02/11  12:20:47  martin
  22. %H  changed reference "Strings" to "Strings and Characters"
  23. %H
  24. %H  Revision 3.6  1993/02/10  21:10:03  martin
  25. %H  fixed various typos, extended description for 3.2
  26. %H
  27. %H  Revision 3.5  1992/04/30  12:07:10  martin
  28. %H  changed a few sentences to avoid bold non-roman fonts
  29. %H
  30. %H  Revision 3.4  1992/04/09  11:36:01  martin
  31. %H  made a few changes so that two LaTeX passes suffice
  32. %H
  33. %H  Revision 3.3  1992/04/02  20:58:24  martin
  34. %H  fixed a wrong example in "While"
  35. %H
  36. %H  Revision 3.2  1992/03/11  15:59:22  martin
  37. %H  fixed several typos
  38. %H
  39. %H  Revision 3.1  1992/01/14  14:42:24  martin
  40. %H  changed the BNF for nicer online viewing
  41. %H
  42. %H  Revision 3.0  1991/12/27  16:10:27  martin
  43. %H  initial revision under RCS
  44. %H
  45. %%
  46. \Chapter{The Programming Language}
  47.  
  48. This chapter describes the {\GAP} programming language.   It should allow
  49. you in principle to predict the result of each and every input.  In order
  50. to know what we are talking about, we first have to  look more closely at
  51. the process of  interpretation  and the  various representations  of data
  52. involved.
  53.  
  54. First we have the input to {\GAP}, given as a string  of characters.  How
  55. those characters enter {\GAP} is operating system  dependent,  e.g., they
  56. might be entered at a  terminal, pasted with  a mouse into  a window,  or
  57. read from a file.  The mechanism does not matter.  This representation of
  58. expressions by characters is called the *external representation* of  the
  59. expression.  Every expression has  at  least one external  representation
  60. that can be entered to get exactly this expression.
  61.  
  62. The input, i.e., the external representation, is transformed in a process
  63. called *reading* to an internal representation.   At this point the input
  64. is analyzed  and inputs   that  are  not legal external  representations,
  65. according to the rules given below, are rejected  as errors.  Those rules
  66. are usually called the *syntax* of a programming language.
  67.  
  68. The  internal  representation  created by  reading  is called  either  an
  69. *expression* or a *statement*.   Later we  will distinguish between those
  70. two terms, however now we will use them interchangeably.  The exact  form
  71. of the internal  representation does not matter.  It could be a string of
  72. characters  equal  to  the external  representation,  in  which case  the
  73. reading  would  only  need to check for errors.  It could be a series  of
  74. machine instructions  for the  processor  on  which {\GAP} is running, in
  75. which case the reading would  more appropriately  be  called compilation.
  76. It is in fact a tree--like structure.
  77.  
  78. After the input has been read it is again transformed in a process called
  79. *evaluation* or *execution*.  Later we will distinguish between those two
  80. terms too, but for the moment we will use them interchangeably.  The name
  81. hints at the nature of this  process, it replaces an  expression with the
  82. value of the expression.  This  works  recursively,  i.e., to evaluate an
  83. expression  first the subexpressions  are evaluated and then the value of
  84. the expression is  computed according  to rules  given  below from  those
  85. values.  Those rules  are usually called the *semantics* of a programming
  86. language.
  87.  
  88. The result of the evaluation is, not surprisingly, called a *value*.  The
  89. set of values    is  of course a    much smaller  set  than  the   set of
  90. expressions;  for  every value there  are   several expressions that will
  91. evaluate to   this  value.  Again  the form  in  which such  a   value is
  92. represented  internally does not   matter.  It is    in fact a tree--like
  93. structure again.
  94.  
  95. The last process  is called *printing*.  It  takes the  value produced by
  96. the evaluation and creates an external representation,  i.e., a string of
  97. characters again.  What you do with this external representation is up to
  98. you. You can look at it, paste it with the  mouse into another window, or
  99. write it to a file.
  100.  
  101. Lets look at an example to make this more clear.  Suppose you type in the
  102. following string of 8 characters
  103.  
  104. |    1 + 2 * 3;|
  105.  
  106. {\GAP}  takes   this external representation   and creates   a  tree like
  107. internal representation, which we can picture as follows
  108.  
  109. |       +
  110.       / \
  111.      1   *
  112.         / \
  113.        2   3 |
  114.  
  115. This expression is then evaluated.  To do this {\GAP} first evaluates the
  116. right subexpression '2\*3'.   Again to do this {\GAP} first evaluates its
  117. subexpressions 2  and 3.  However they  are so simple that they are their
  118. own value, we say that  they are  self--evaluating.  After  this has been
  119. done,  the rule for '\*' tells us that the value is the  product  of  the
  120. values of the  two  subexpressions,  which  in  this case  is clearly  6.
  121. Combining  this with the value  of the left operand of  the '+', which is
  122. self--evaluating too gives us  the value of the whole expression 7.  This
  123. is  then  printed,  i.e.,  converted  into  the  external  representation
  124. consisting of the single character '7'.
  125.  
  126. In this fashion we can predict the result of every input when we know the
  127. syntactic rules that govern the process of reading and the semantic rules
  128. that tell us for every expression how  its value  is computed in terms of
  129. the  values of  the  subexpressions.  The  syntactic rules  are  given in
  130. sections  "Lexical  Structure",   "Symbols",  "Whitespaces",  "Keywords",
  131. "Identifiers", and "The  Syntax in BNF",  the semantic rules are given in
  132. sections  "Expressions",  "Variables", "Function  Calls",  "Comparisons",
  133. "Operations",   "Statements",  "Assignments",  "Procedure  Calls",  "If",
  134. "While", "Repeat",  "For", "Functions",  and the  chapters describing the
  135. individual data types.
  136.  
  137. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  138. \Section{Lexical Structure}
  139.  
  140. The input of {\GAP} consists of sequences of the following characters.
  141.  
  142. Digits, uppercase and lowercase letters,  <space>, <tab>, <newline>,  and
  143. the special characters
  144.  
  145. |    "       '       (       )       *       +       ,       _
  146.     .       /       :       ;       <       =       >       ~
  147.     [       \       ]       ^       _       {       }       & |
  148.  
  149. Other  characters  will  be signalled  as  illegal.   Inside strings  and
  150. comments the full character set supported by the computer is allowed.
  151.  
  152. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  153. \Section{Symbols}
  154.  
  155. The  process of reading,  i.e., of assembling the input into expressions,
  156. has a subprocess,  called  *scanning*, that assembles the characters into
  157. symbols.   A *symbol*  is  a sequence of characters that form  a  lexical
  158. unit.  The  set of  symbols  consists of  keywords, identifiers, strings,
  159. integers, and operator and delimiter symbols.
  160.  
  161. A keyword is a  reserved word  consisting entirely  of lowercase  letters
  162. (see "Keywords").  An identifier is a sequence of letters and digits that
  163. contains at  least one letter  and is not a keyword  (see "Identifiers").
  164. An integer is a  sequence of  digits  (see  "Integers").   A string is  a
  165. sequence  of   arbitrary  characters   enclosed  in  double  quotes  (see
  166. "Strings and Characters").
  167.  
  168. Operator and delimiter symbols are
  169.  
  170. |    +       -       *       /       ^       ~
  171.     =       <>      <       <=      >       >=
  172.     :=      .       ..      ->      ,       ;
  173.     [       ]       {       }       (       )  |
  174.  
  175. Note  that during the process of  scanning also all whitespace is removed
  176. (see "Whitespaces").
  177.  
  178. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  179. \Section{Whitespaces}%
  180. \index{space}%
  181. \index{blank}\index{tabulator}\index{newline}\index{comments}
  182.  
  183. The    characters <space>,  <tab>,   <newline>, and   <return> are called
  184. *whitespace characters*.   Whitespace is used   as necessary to  separate
  185. lexical symbols, such as integers, identifiers, or keywords.  For example
  186. 'Thorondor' is  a single identifier, while 'Th  or  ondor' is the keyword
  187. 'or' between the two identifiers 'Th'  and 'ondor'.  Whitespace may occur
  188. between any two  symbols, but not within a  symbol.  Two or more adjacent
  189. whitespaces are  equivalent to a single  whitespace.  Apart from the role
  190. as  separator  of  symbols,  whitespaces  are   otherwise  insignificant.
  191. Whitespaces may also  occur inside a string,  where they are significant.
  192. Whitespaces should also be used freely for improved readability.
  193.  
  194. A  *comment* starts with the   character '\#', which is sometimes  called
  195. sharp or hatch, and continues to the end of the line on which the comment
  196. character appears.  The whole  comment, including '\#' and  the <newline>
  197. character  is  treated  as a single    whitespace.  Inside a  string, the
  198. comment character '\#' looses its role and is just an ordinary character.
  199.  
  200. For example, the following statement
  201.  
  202. |    if i<0 then a:=-i;else a:=i;fi; |
  203.  
  204. is equivalent to
  205.  
  206. |    if i < 0  then      & if i is negative
  207.         a := -i;        &     take its inverse
  208.     else                & otherwise
  209.         a := i;         &     take itself
  210.     fi; |
  211.  
  212. (which by the  way  shows  that  it  is  possible  to  write  superfluous
  213. comments).  However the first statement is not equivalent to
  214.  
  215. |    ifi<0thena:=-i;elsea:=i;fi; |
  216.  
  217. since  the keyword 'if'  must  be separated from  the identifier 'i' by a
  218. whitespace,  and  similarly 'then'  and 'a',  and 'else' and  'a' must be
  219. separated.
  220.  
  221. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  222. \Section{Keywords}
  223.  
  224. *Keywords* are reserved words that are used  to denote special operations
  225. or  are part of  statements.  They must not be used  as identifiers.  The
  226. keywords are
  227.  
  228. |    and       do        elif      else      end       fi
  229.     for       function  if        in        local     mod
  230.     not       od        or        repeat    return    then
  231.     until     while     quit |
  232.  
  233. Note that all keywords are written in lowercase.  For example only 'else'
  234. is   a  keyword;  'Else', 'eLsE',  'ELSE'   and   so  forth  are ordinary
  235. identifiers.  Keywords must not contain  whitespace, for  example 'el if'
  236. is not the same as 'elif'.
  237.  
  238. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  239. \Section{Identifiers}
  240.  
  241. An identifier is used to  refer to   a  variable (see  "Variables").   An
  242. identifier consists of letters,  digits,  and underscores '\_',  and must
  243. contain at least one letter  or underscore.   An identifier is terminated
  244. by the first character not in this class.  Examples  of valid identifiers
  245. are
  246.  
  247. |    a                   foo                 aLongIdentifier
  248.     hello               Hello               HELLO
  249.     x100                100x                _100
  250.     some_people_prefer_underscores_to_separate_words
  251.     WePreferMixedCaseToSeparateWords |
  252.  
  253. Note that  case  is  significant, so  the three identifiers in the second
  254. line are distinguished.
  255.  
  256. The backslash |\| can be used to include other characters in identifiers;
  257. a backslash  followed  by  a character  is equivalent   to the character,
  258. except that this escape sequence  is considered to be an ordinary letter.
  259. For example |G\(2\,5\)| is an identifier, not a call to a function 'G'.
  260.  
  261. An identifier that  starts with  a backslash is  never a  keyword, so for
  262. example |\*| and |\mod| are identifier.
  263.  
  264. The length  of identifiers is not limited,   however only  the first 1023
  265. characters are significant.  The escape sequence |\|<newline> is ignored,
  266. making it possible to split long identifiers over multiple lines.
  267.  
  268. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  269. \Section{Expressions}%
  270. \index{evaluation}
  271.  
  272. An  *expression* is a construct  that  evaluates  to a value.   Syntactic
  273. constructs that are executed to produce a side effect and return no value
  274. are called *statements*  (see "Statements").  Expressions appear as right
  275. hand  sides of assignments  (see "Assignments"),  as  actual arguments in
  276. function calls (see "Function Calls"), and in statements.
  277.  
  278. Note that an expression is not the same as a value.  For example '1 + 11'
  279. is  an    expression, whose value is   the   integer  12.    The external
  280. representation of this integer is the character sequence '12', i.e., this
  281. sequence is output  if the integer is  printed.  This sequence is another
  282. expression whose value is  the integer 12.    The process of finding  the
  283. value of an  expression  is done by  the  interpreter and is  called  the
  284. *evaluation* of the expression.
  285.  
  286. Variables,  function  calls, and integer, permutation, string,  function,
  287. list, and record literals (see "Variables", "Function Calls", "Integers",
  288. "Permutations",   "Strings   and   Characters",   "Functions",   "Lists",
  289. "Records"), are the simplest cases of expressions.
  290.  
  291. Expressions,  for example the simple expressions  mentioned above, can be
  292. combined with the operators to form  more complex expressions.  Of course
  293. those expressions can then be combined further with the operators to form
  294. even more complex  expressions.  The *operators* fall into three classes.
  295. The  *comparisons*  are  '=', '\<>',  '\<=', '>',   '>=',  and  'in' (see
  296. "Comparisons" and "In").  The *arithmetic operators* are  '+', '-', '\*',
  297. '/', 'mod', and '\^'   (see "Operations").   The *logical  operators* are
  298. 'not', 'and', and 'or' (see "Operations for Booleans").
  299.  
  300. |    gap> 2 * 2;    # a very simple expression with value
  301.     4
  302.     gap> 2 * 2 + 9 = Fibonacci(7) and  Fibonacci(13) in Primes;
  303.     true            # a more complex expression |
  304.  
  305. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  306. \Section{Variables}%
  307. \index{scope}\index{bound}
  308.  
  309. A *variable*  is a location in a  {\GAP} program that  points to a value.
  310. We say the variable is *bound* to this value.  If a variable is evaluated
  311. it evaluates to this value.
  312.  
  313. Initially an ordinary variable is not  bound to  any value.  The variable
  314. can be bound to a  value  by *assigning* this value to  the variable (see
  315. "Assignments").  Because of this we sometimes say that a variable that is
  316. not bound to any value has no assigned value.  Assignment  is in fact the
  317. only way by which a variable, which is not an argument of a function, can
  318. be bound to  a  value.  After a variable  has  been bound to a   value an
  319. assignment can also be used to bind the variable to another value.
  320.  
  321. A special class of  variables are *arguments* of functions.  They  behave
  322. similarly to other variables, except they are  bound to the  value of the
  323. actual arguments upon a function call (see "Function Calls").
  324.  
  325. Each variable has a  name that is also called  its *identifier*.  This is
  326. because in  a given scope an identifier identifies a unique variable (see
  327. "Identifiers").  A *scope* is a lexical part of a program text.  There is
  328. the  global scope that encloses the  entire program  text, and there  are
  329. local  scopes that  range  from  the  'function'  keyword,  denoting  the
  330. beginning of a function  definition, to  the corresponding 'end' keyword.
  331. A local scope  introduces new  variables, whose identifiers are given  in
  332. the formal argument list and the 'local' declaration of the function (see
  333. "Functions").  Usage  of an  identifier in  a program  text refers to the
  334. variable in  the innermost scope  that has  this identifier as  its name.
  335. Because  this  mapping  from  identifiers  to variables is done  when the
  336. program is read, not when it is executed, {\GAP} is  said to have lexical
  337. scoping.   The  following example  shows  how  one identifier  refers  to
  338. different variables at different points in the program text.
  339.  
  340. |     g := 0;            & global variable g
  341.      x := function ( a, b, c )
  342.         local   y;
  343.         g := c;         & c refers to argument c of function x
  344.         y := function ( y )
  345.             local  d, e, f;
  346.             d := y;     & y refers to argument y of function y
  347.             e := b;     & b refers to argument b of function x
  348.             f := g;     & g refers to global variable g
  349.             return d + e + f;
  350.         end;
  351.         return y( a );  & y refers to local y of function x
  352.     end; |
  353.  
  354. It is important to note that the concept of a variable in {\GAP} is quite
  355. different from the concept  of  a variable in programming languages  like
  356. PASCAL.  In  those languages a variable  denotes a block of  memory.  The
  357. value of the variable is stored in this block.  So in those languages two
  358. variables can  have the  same value,  but  they can never  have identical
  359. values,  because  they  denote different  blocks of  memory.   (Note that
  360. PASCAL has  the concept of a reference  argument.  It seems as if such an
  361. argument and the variable used in the actual function  call have the same
  362. value, since changing the argument\'s value also changes the value of the
  363. variable  used in  the actual  function call.   But this is  not  so; the
  364. reference  argument is actually  a pointer  to the variable  used  in the
  365. actual function call, and it is the compiler that inserts enough magic to
  366. make the  pointer  invisible.)  In  order  for this to work the  compiler
  367. needs enough  information to compute the amount of memory needed for each
  368. variable in a  program,  which is  readily  available in the declarations
  369. PASCAL requires for every variable.
  370.  
  371. In {\GAP} on the other hand each variable justs points to a value.
  372.  
  373. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  374. \Section{Function Calls}
  375.  
  376. '<function-var>()' \\
  377. '<function-var>( <arg-expr> \{',' <arg-expr>\} )'
  378.  
  379. The function call has the effect  of calling the function <function-var>.
  380. The precise semantics are as follows.
  381.  
  382. First  {\GAP} evaluates the <function-var>.  Usually  <function-var> is a
  383. variable,  and  {\GAP} does nothing more  than taking  the value of  this
  384. variable.   It is allowed  though  that  <function-var> is a more complex
  385. expression,  namely it can for example be  a selection of  a list element
  386. '<list-var>[<int-expr>]',  or   a   selection  of   a   record  component
  387. '<record-var>.<ident>'.  In any  case {\GAP} tests whether the value is a
  388. function.  If it is not, {\GAP} signals an error.
  389.  
  390. Next {\GAP} checks that the number of actual arguments <arg-expr>s agrees
  391. with the number of formal arguments as given  in the function definition.
  392. If they do not agree {\GAP}  signals an error.  An  exception is the case
  393. when there is  exactly one formal argument  with the name 'arg', in which
  394. case any number of actual arguments is allowed.
  395.  
  396. Now {\GAP} allocates for each formal argument and for each formal local a
  397. new variable.  Remember that a variable is a location in a {\GAP} program
  398. that points  to  a value.  Thus for   each formal argument  and  for each
  399. formal local such a location is allocated.
  400.  
  401. Next the arguments <arg-expr>s are evaluated, and the values are assigned
  402. to the newly created variables corresponding to the formal arguments.  Of
  403. course the first value  is assigned  to the new variable corresponding to
  404. the  first  formal argument, the second   value   is  assigned to the new
  405. variable corresponding   to   the second    formal argument, and   so on.
  406. However, {\GAP} does not make any guarantee about the order  in which the
  407. arguments are evaluated.  They might be evaluated left to right, right to
  408. left, or in  any  other order, but  each  argument is evaluated once.  An
  409. exception again occurs if the function has  only one formal argument with
  410. the name 'arg'.  In this case the values  of all the actual arguments are
  411. stored  in   a  list and this   list  is assigned  to the    new variable
  412. corresponding to the formal argument 'arg'.
  413.  
  414. The  new variables corresponding to the  formal  locals are initially not
  415. bound to any   value.   So trying   to evaluate  those   variables before
  416. something has been assigned to them will signal an error.
  417.  
  418. Now the body of the function, which is  a statement, is executed.  If the
  419. identifier of one of the formal arguments or formal locals appears in the
  420. body of the function it refers to the new variable that was allocated for
  421. this formal argument or formal local, and evaluates to  the value of this
  422. variable.
  423.  
  424. If during the execution of the body of the function  a 'return' statement
  425. with  an expression (see "Return") is executed,  execution of the body is
  426. terminated  and  the value  of  the function  call  is the value  of  the
  427. expression  of  the 'return'.  If  during the  execution of  the  body  a
  428. 'return'  statement  without an expression is executed,  execution of the
  429. body  is  terminated and the function  call does not produce  a value, in
  430. which case  we call this call a procedure  call (see  "Procedure Calls").
  431. If the  execution of  the body completes without execution of  a 'return'
  432. statement, the  function call again produces no  value, and again we talk
  433. about a procedure call.
  434.  
  435. |    gap> Fibonacci( 11 );
  436.         # a call to the function 'Fibonacci' with actual argument '11'
  437.     89 |
  438.         
  439. |    gap> G.operations.RightCosets( G, Intersection( U, V ) );;
  440.         # a call to the function in 'G.operations.RightCosets'
  441.         # where the second actual argument is another function call |
  442.  
  443. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  444. \Section{Comparisons}
  445.  
  446. '<left-expr> =   <right-expr>' \\
  447. '<left-expr> \<> <right-expr>'
  448.  
  449. The operator '=' tests for equality of  its two operands and evaluates to
  450. 'true' if they are equal and to 'false'  otherwise.  Likewise '\<>' tests
  451. for  inequality of its  two operands.  Note  that any two  objects can be
  452. compared, i.e., '=' and '\<>' will never  signal an error.  For each type
  453. of objects the definition of equality is given in the respective chapter.
  454. Objects of different types are  never equal, i.e.,  '=' evaluates in this
  455. case to 'false', and '\<>' evaluates to 'true'.
  456.  
  457. '<left-expr> \<\ <right-expr>' \\
  458. '<left-expr> >   <right-expr>' \\
  459. '<left-expr> \<= <right-expr>' \\
  460. '<left-expr> >=  <right-expr>'
  461.  
  462. '\<' denotes less than, '\<=' less  than or equal,  '>' greater than, and
  463. '>=' greater than or equal of its two operands.  For each type of objects
  464. the definition of the ordering  is given in the  respective chapter.  The
  465. ordering  of objects  of different types  is  as follows.   Rationals are
  466. smallest,  next are  cyclotomics,  followed   by finite field   elements,
  467. permutations,  words, words in  solvable groups, boolean values, strings,
  468. functions, lists, and records are largest.
  469.  
  470. Comparison operators, which includes the operator 'in' (see "In") are not
  471. associative, i.e., it is not allowed to write '<a> =  <b> \<> <c> = <d>',
  472. you  must use '(<a>  =  <b>) \<>  (<c>  = <d>)'  instead.  The comparison
  473. operators have   higher precedence   than    the logical  operators  (see
  474. "Operations  for  Booleans"),  but lower  precedence  than the arithmetic
  475. operators (see "Operations").  Thus, for example, '<a> \*\ <b> =  <c> and
  476. <d>' is interpreted, '((<a> \*\ <b>) = <c>) and <d>)'.
  477.  
  478. |    gap> 2 * 2 + 9 = Fibonacci(7);    # a comparison where the left
  479.     true                              # operand is an expression |
  480.  
  481. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  482. \Section{Operations}%
  483. \index{precedence}\index{associativity}
  484.  
  485. '+ <right-expr>' \\
  486. '- <right-expr>' \\
  487. '<left-expr> +   <right-expr>' \\
  488. '<left-expr> -   <right-expr>' \\
  489. '<left-expr> \*\ <right-expr>' \\
  490. '<left-expr> /   <right-expr>' \\
  491. '<left-expr> mod <right-expr>' \\
  492. '<left-expr> \^\ <right-expr>'
  493.  
  494. The arithmetic operators are  '+',  '-',  '\*',  '/',  'mod',  and  '\^'.
  495. The meanings (semantic) of those  operators generally depend on the types
  496. of the operands  involved, and they are  defined in the  various chapters
  497. describing the types.  However basically the meanings are as follows.
  498.  
  499. '+' denotes the addition,  and '-'  the  subtraction  of ring  and  field
  500. elements.   '\*' is  the  multiplication of  group  elements,  '/' is the
  501. multiplication of the left operand with the inverse of the right operand.
  502. 'mod' is only defined for integers  and rationals and denotes  the modulo
  503. operation.  '+' and '-' can also be used as unary  operations.  The unary
  504. '+' is ignored and unary '-' is equivalent to multiplication by -1.  '\^'
  505. denotes powering of a group  element  if the right operand is an integer,
  506. and is  also used to denote operation if  the  right operand  is  a group
  507. element.
  508.  
  509. The *precedence* of those operators is as follows.  The powering operator
  510. '\^' has  the highest precedence, followed by the unary operators '+' and
  511. '-',  which are followed by the  multiplicative  operators '\*', '/', and
  512. 'mod', and  the additive  binary operators '+' and  '-'  have  the lowest
  513. precedence.   That  means that the expression  '-2 \^\ -2 \*\  3 + 1'  is
  514. interpreted as '(-(2  \^\ (-2)) \*\ 3) + 1'.  If in doubt use parentheses
  515. to clarify your intention.
  516.  
  517. The *associativity* of the arithmetic operators is as follows.'\^' is not
  518. associative,  i.e., it is  illegal to write '2\^3\^4', use parentheses to
  519. clarify whether  you mean '(2\^3)  \^\  4' or '2 \^\ (3\^4)'.  The  unary
  520. operators '+' and '-' are right associative, because they are written  to
  521. the left of their operands.  '\*', '/', 'mod', '+',  and '-' are all left
  522. associative, i.e., '1-2-3' is interpreted as '(1-2)-3'  not as '1-(2-3)'.
  523. Again, if in doubt use parentheses to clarify your intentions.
  524.  
  525. The  arithmetic  operators  have higher  precedence  than  the comparison
  526. operators (see  "Comparisons" and  "In")  and the logical  operators (see
  527. "Operations for  Booleans").  Thus, for  example, '<a> \*\ <b> =  <c> and
  528. <d>' is interpreted, '((<a> \*\ <b>) = <c>) and <d>'.
  529.  
  530. |    gap> 2 * 2 + 9;    & a very simple arithmetic expression
  531.     13 |
  532.  
  533. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  534. \Section{Statements}%
  535. \index{execution}
  536.  
  537. Assignments (see "Assignments"), Procedure calls (see "Procedure Calls"),
  538. 'if'  statements  (see  "If"),  'while'  (see  "While"),  'repeat'   (see
  539. "Repeat") and  'for'  loops (see "For"), and the 'return'  statement (see
  540. "Return") are called statements.  They can be entered interactively or be
  541. part of a function definition.  Every statement must  be terminated by  a
  542. semicolon.
  543.  
  544. Statements, unlike expressions, have no value.  They are executed only to
  545. produce an effect.  For example an assignment has the effect of assigning
  546. a   value to a  variable, a  'for' loop   has the  effect  of executing a
  547. statement sequence for  all elements in a  list and so  on.  We will talk
  548. about *evaluation* of expressions  but about *execution* of statements to
  549. emphasize this difference.
  550.  
  551. It is possible to use expressions as statements.  However this does cause
  552. a warning.
  553.  
  554. |    gap> if i <> 0  then  k = 16/i;  fi;
  555.     Syntax error: warning, this statement has no effect
  556.     if i <> 0  then  k = 16/i;  fi;
  557.                              ^ |
  558.  
  559. As you can see from  the  example this is useful for those  users who are
  560. used to languages where '=' instead of '\:=' denotes assignment.
  561.  
  562. A sequence  of  one or more  statements is a statement sequence, and  may
  563. occur  everywhere instead of  a single statement.  There  is nothing like
  564. PASCAL\'s  BEGIN-END, instead each  construct is terminated by a keyword.
  565. The most simple  statement sequence is a single  semicolon, which can  be
  566. used as an empty statement sequence.
  567.  
  568. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  569. \Section{Assignments}%
  570. \index{assignment!variable}
  571.  
  572. '<var> \:= <expr>;'
  573.  
  574. The *assignment* has the effect of assigning the value of the expressions
  575. <expr> to the variable <var>.
  576.  
  577. The variable <var> may be an  ordinary variable (see "Variables"), a list
  578. element selection '<list-var>[<int-expr>]' (see  "List Assignment")  or a
  579. record  component   selection    '<record-var>.<ident>'   (see    "Record
  580. Assignment").  Since a list element or a record component may itself be a
  581. list or a record the  left hand side of  an assignment may be arbitrarily
  582. complex.
  583.  
  584. Note that variables do  not have a type.  Thus  any value may be assigned
  585. to any variable.   For example a  variable with  an integer  value may be
  586. assigned a permutation or a list or anything else.
  587.  
  588. If the expression  <expr> is a  function  call then  this  function  must
  589. return a value.   If the  function does not  return a  value  an error is
  590. signalled and you enter a break loop  (see "Break Loops").   As usual you
  591. can leave  the  break   loop   with  'quit;'.    If  you  enter   'return
  592. <return-expr>;' the value of the expression  <return-expr> is assigned to
  593. the variable, and execution continues after the assignment.
  594.  
  595. |    gap> S6 := rec( size := 720 );; S6;
  596.     rec(
  597.       size := 720 )
  598.     gap> S6.generators := [ (1,2), (1,2,3,4,5) ];; S6;
  599.     rec(
  600.       size := 720,
  601.       generators := [ (1,2), (1,2,3,4,5) ] )
  602.     gap> S6.generators[2] := (1,2,3,4,5,6);; S6;
  603.     rec(
  604.       size := 720,
  605.       generators := [ (1,2), (1,2,3,4,5,6) ] ) |
  606.  
  607. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  608. \Section{Procedure Calls}
  609.  
  610. '<procedure-var>();' \\
  611. '<procedure-var>( <arg-expr> \{',' <arg-expr>\} );'
  612.  
  613. The   procedure  call   has   the   effect   of  calling   the  procedure
  614. <procedure-var>.   A  procedure call is done exactly like a function call
  615. (see "Function Calls").  The distinction between functions and procedures
  616. is only for the  sake  of  the discussion,  {\GAP} does  not  distinguish
  617. between them.
  618.  
  619. A *function* does return a value but does not produce a  side effect.  As
  620. a convention the name of a function is a noun, denoting what the function
  621. returns, e.g., 'Length', 'Concatenation' and 'Order'.
  622.  
  623. A *procedure* is a  function that does  not return  a value but  produces
  624. some   effect.  Procedures  are called   only  for   this effect.  As   a
  625. convention the name of a procedure is a verb, denoting what the procedure
  626. does, e.g., 'Print', 'Append' and 'Sort'.
  627.  
  628. |    gap> Read( "myfile.g" );     & a call to the procedure Read
  629.     gap> l := [ 1, 2 ];;
  630.     gap> Append( l, [3,4,5] );    & a call to the procedure Append |
  631.  
  632. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  633. \Section{If}%
  634. \index{if}\index{if statement}%
  635. \index{fi}\index{then}\index{else}\index{elif}
  636.  
  637. 'if <bool-expr1>  then <statements1> \\
  638. \{ elif <bool-expr2>  then <statements2> \} \\
  639. {}[ else <statements3> ] \\
  640. fi;'
  641.  
  642. The 'if' statement  allows  one  to  execute statements depending  on the
  643. value of some boolean expression. The execution is done as follows.
  644.  
  645. First the expression <bool-expr1> following the 'if' is evaluated.  If it
  646. evaluates to 'true' the statement sequence  <statements1> after the first
  647. 'then' is executed, and the execution of the 'if' statement is complete.
  648.  
  649. Otherwise the expressions <bool-expr2> following the 'elif' are evaluated
  650. in turn.  There may  be any number of 'elif' parts, possibly none at all.
  651. As soon  as an expression evaluates to 'true' the corresponding statement
  652. sequence <statements2> is executed and execution of the 'if' statement is
  653. complete.
  654.  
  655. If the 'if' expression  and all, if  any, 'elif' expressions  evaluate to
  656. 'false'  and there is  an 'else'  part, which is  optional, its statement
  657. sequence <statements3>  is  executed   and the   execution of    the 'if'
  658. statement is complete.  If there is no 'else' part  the 'if' statement is
  659. complete without executing any statement sequence.
  660.  
  661. Since the 'if' statement is  terminated by the  'fi' keyword there  is no
  662. question where an 'else' part belongs, i.e., {\GAP} has no dangling else.\\
  663. In 'if <expr1>  then  if <expr2>  then <stats1>  else <stats2>  fi;  fi;'\\
  664. the 'else' part  belongs  to  the  second   'if'  statement,  whereas  in\\
  665. 'if <expr1>  then  if  <expr2>  then  <stats1>  fi;  else  <stats2>  fi;'\\
  666. the 'else' part belongs to the first 'if' statement.
  667.  
  668. Since an if statement is  not an expression it  is not possible to  write
  669.  
  670. |    abs := if x  > 0  then  x;  else  -x;  fi;|
  671.  
  672. which would,  even  if  legal  syntax, be   meaningless,  since  the 'if'
  673. statement does not produce a value that could be assigned to 'abs'.
  674.  
  675. If one expression evaluates neither to 'true'  nor to 'false' an error is
  676. signalled and a break loop (see "Break Loops") is  entered.  As usual you
  677. can leave   the break loop  with 'quit;'.   If you  enter 'return true;',
  678. execution  of the  'if' statement  continues  as if the expression  whose
  679. evaluation    failed had evaluated to  'true'.     Likewise, if you enter
  680. 'return  false;', execution of  the  'if' statement  continues as if  the
  681. expression whose evaluation failed had evaluated to 'false'.
  682.  
  683. |    gap> i := 10;;
  684.     gap> if 0 < i  then
  685.     >        s := 1;
  686.     >    elif i < 0  then
  687.     >        s := -1;
  688.     >    else
  689.     >        s := 0;
  690.     >    fi;
  691.     gap> s;
  692.     1        # the sign of i |
  693.  
  694. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  695. \Section{While}%
  696. \index{while}\index{while loop}\index{loop!while}
  697.  
  698. 'while <bool-expr>  do <statements>  od;'
  699.  
  700. The  'while' loop executes the statement  sequence <statements> while the
  701. condition <bool-expr> evaluates to 'true'.
  702.  
  703. First <bool-expr> is evaluated.  If it evaluates  to 'false' execution of
  704. the 'while' loop  terminates and the  statement immediately following the
  705. 'while' loop is executed next.  Otherwise if  it evaluates  to 'true' the
  706. <statements> are executed and the whole process begins again.
  707.  
  708. The  difference  between the 'while'   loop and the  'repeat  until' loop
  709. (see "Repeat") is that the <statements>  in  the  'repeat until' loop are
  710. executed at least  once, while the  <statements> in the  'while' loop are
  711. not executed at all if <bool-expr> is 'false' at the first iteration.
  712.  
  713. If  <bool-expr>  does  not  evaluate to 'true'  or 'false'  an  error  is
  714. signalled  and a break loop (see "Break Loops") is entered.  As usual you
  715. can  leave the break loop with 'quit;'.   If you  enter  'return false;',
  716. execution  continues with the  next  statement immediately following  the
  717. 'while'  loop.   If you  enter  'return  true;',  execution continues  at
  718. <statements>, after  which  the  next evaluation of <bool-expr> may cause
  719. another error.
  720.  
  721. |    gap> i := 0;;  s := 0;;
  722.     gap> while s <= 200  do
  723.     >        i := i + 1;  s := s + i^2;
  724.     >    od;
  725.     gap> s;
  726.     204        # first sum of the first i squares larger than 200 |
  727.  
  728. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  729. \Section{Repeat}%
  730. \index{repeat}\index{repeat loop}\index{loop!repeat}\index{until}
  731.  
  732. 'repeat <statements>  until <bool-expr>;'
  733.  
  734. The 'repeat' loop executes  the statement sequence <statements> until the
  735. condition <bool-expr> evaluates to 'true'.
  736.  
  737. First <statements> are executed.   Then <bool-expr> is  evaluated.  If it
  738. evaluates   to 'true' the  'repeat'   loop  terminates and  the statement
  739. immediately following the  'repeat' loop is  executed next.  Otherwise if
  740. it evaluates to 'false' the whole process begins again with the execution
  741. of the <statements>.
  742.  
  743. The  difference  between the 'while'  loop (see "While")  and the 'repeat
  744. until' loop  is that the  <statements>  in  the  'repeat until' loop  are
  745. executed  at least once,  while the  <statements> in the 'while' loop are
  746. not executed at all if <bool-expr> is 'false' at the first iteration.
  747.  
  748. If  <bool-expr>  does  not evaluate to  'true'   or  'false' a  error  is
  749. signalled and a break loop (see "Break Loops") is entered.  As  usual you
  750. can leave  the break loop  with 'quit;'.   If  you  enter 'return true;',
  751. execution continues with the  next statement   immediately following  the
  752. 'repeat' loop.   If you  enter 'return   false;', execution continues  at
  753. <statements>, after which the next  evaluation  of  <bool-expr> may cause
  754. another error.
  755.  
  756. |    gap> i := 0;;  s := 0;;
  757.     gap> repeat
  758.     >        i := i + 1;  s := s + i^2;
  759.     >    until s > 200;
  760.     gap> s;
  761.     204        # first sum of the first i squares larger than 200 |
  762.  
  763. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  764. \Section{For}%
  765. \index{for}\index{for loop}\index{loop!for}\index{do}\index{od}
  766.  
  767. 'for <simple-var>  in <list-expr>  do <statements>  od;'
  768.  
  769. The 'for' loop executes   the statement sequence <statements>  for  every
  770. element of the list <list-expr>.
  771.  
  772. The statement sequence  <statements> is  first executed with <simple-var>
  773. bound  to  the first element of  the list  <list>, then with <simple-var>
  774. bound to the second element of <list> and  so on.  <simple-var> must be a
  775. simple  variable,   it   must not     be   a list     element   selection
  776. '<list-var>[<int-expr>]'     or       a    record    component  selection
  777. '<record-var>.<ident>'.
  778.  
  779. The execution of the 'for' loop is exactly equivalent to the 'while' loop
  780.  
  781. |    |<loop-list>| := |<list>|;
  782.     |<loop-index>| := 1;
  783.     while |<loop-index>| <= Length(|<loop-list>|) do
  784.         |<variable>| := |<loop-list>|[|<loop-index>|];
  785.         |<statements>|
  786.         |<loop-index>| := |<loop-index>| + 1;
  787.     od; |
  788.  
  789. with the   exception   that  <loop-list> and <loop-index>  are  different
  790. variables for each 'for' loop that do not interfere with each other.
  791.  
  792. The list <list> is very often a range.\\
  793. 'for <variable> in [<from>..<to>] do <statements> od;'\\
  794. corresponds to the more common\\
  795. 'for <variable> from <from> to <to>  do <statements> od;'\\
  796. in other programming languages.
  797.  
  798. |    gap> s := 0;;
  799.     gap> for i  in [1..100]  do
  800.     >        s := s + i;
  801.     > od;
  802.     gap> s;
  803.     5050 |
  804.  
  805. Note in the following example  how the modification of  the *list* in the
  806. loop body causes the loop body also to be executed for the new values
  807.  
  808. |    gap> l := [ 1, 2, 3, 4, 5, 6 ];;
  809.     gap> for i  in l  do
  810.     >        Print( i, " " );
  811.     >        if i mod 2 = 0  then Add( l, 3 * i / 2 );  fi;
  812.     > od;  Print( "\n" );
  813.     1 2 3 4 5 6 3 6 9 9
  814.     gap> l;
  815.     [ 1, 2, 3, 4, 5, 6, 3, 6, 9, 9 ] |
  816.  
  817. Note  in  the following example that  the  modification of the *variable*
  818. that holds the list has no influence on the loop
  819.  
  820. |    gap> l := [ 1, 2, 3, 4, 5, 6 ];;
  821.     gap> for i  in l  do
  822.     >        Print( i, " " );
  823.     >        l := [];
  824.     > od;  Print( "\n" );
  825.     1 2 3 4 5 6
  826.     gap> l;
  827.     [  ] |
  828.  
  829. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  830. \Section{Functions}%
  831. \index{function}\index{end}\index{local}%
  832. \index{recursion}\index{recursive functions}%
  833. \index{environment}\index{body}
  834.  
  835. |function (| [ <arg-ident> \{|,| <arg-ident>\} ] |)
  836.     |[ |local   |<loc-ident> \{|, |<loc-ident>\} |;| ]|
  837.     |<statements>|
  838. end|
  839.  
  840. A function is in fact  a literal  and  not  a statement.  Such a function
  841. literal can be  assigned to a variable  or to  a list element or a record
  842. component.  Later  this function can  be called as described in "Function
  843. Calls".
  844.  
  845. The following is an example  of a function definition.   It is a function
  846. to compute values of the Fibonacci sequence (see "Fibonacci")
  847.  
  848. |    gap> fib := function ( n )
  849.     >         local  f1,  f2,  f3,  i;
  850.     >         f1 := 1;  f2 := 1;
  851.     >         for i  in [3..n]  do
  852.     >             f3 := f1 + f2;
  853.     >             f1 := f2;
  854.     >             f2 := f3;
  855.     >         od;
  856.     >         return f2;
  857.     >     end;;
  858.     gap> List( [1..10], fib );
  859.     [ 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 ] |
  860.  
  861. Because for  each of the formal arguments <arg-ident> and for each of the
  862. formal locals <loc-ident>  a  new variable is allocated when the function
  863. is called (see "Function  Calls"), it is  possible that a  function calls
  864. itself.  This is usually called *recursion*. The following is a recursive
  865. function that computes values of the Fibonacci sequence
  866.  
  867. |    gap> fib := function ( n )
  868.     >         if n < 3  then
  869.     >             return 1;
  870.     >         else
  871.     >             return fib(n-1) + fib(n-2);
  872.     >         fi;
  873.     >     end;;
  874.     gap> List( [1..10], fib );
  875.     [ 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 ] |
  876.  
  877. Note that the recursive version needs '2 \*\ fib(<n>)-1' steps to compute
  878. 'fib(<n>)', while the  iterative version   of 'fib'  needs  only  '<n>-2'
  879. steps.   Both are not optimal  however,  the library function 'Fibonacci'
  880. only needs on the order of 'Log(<n>)' steps.
  881.  
  882. '<arg-ident> -> <expr>'
  883.  
  884. This  is a  shorthand for\\
  885. 'function (  <arg-ident> ) return <expr>; end'.\\
  886. <arg-ident>  must  be  a single  identifier, i.e., it is  not possible to
  887. write functions of several arguments this way.  Also 'arg' is not treated
  888. specially,  so  it  is  also impossible to  write  functions that take  a
  889. variable number of arguments this way.
  890.  
  891. The following is an example of a typical use of such a function
  892.  
  893. |    gap> Sum( List( [1..100], x -> x^2 ) );
  894.     338350 |
  895.  
  896. When a function <fun1> definition  is evaluated  inside  another function
  897. <fun2>, {\GAP}  binds all the identifiers inside the function <fun1> that
  898. are identifiers of an argument or a  local of <fun2> to the corresponding
  899. variable.  This set of bindings is called the environment of the function
  900. <fun1>.  When <fun1> is called, its body is executed in this environment.
  901. The  following implementation of a simple stack uses this.  Values can be
  902. pushed  onto  the  stack  and  then  later  be  popped  off  again.   The
  903. interesting thing here  is that the  functions 'push'  and  'pop'  in the
  904. record returned by 'Stack' access the local variable 'stack' of  'Stack'.
  905. When 'Stack' is called a new  variable  for  the  identifier  'stack'  is
  906. created.   When the function  definitions of  'push' and  'pop' are  then
  907. evaluated (as part of the  'return' statement)  each reference to 'stack'
  908. is bound to this new variable.  Note also that the two stacks 'A' and 'B'
  909. do not interfere, because each call of 'Stack' creates a new variable for
  910. 'stack'.
  911.  
  912. |    gap> Stack := function ()
  913.     >         local   stack;
  914.     >         stack := [];
  915.     >         return rec(
  916.     >             push := function ( value )
  917.     >                 Add( stack, value );
  918.     >             end,
  919.     >             pop := function ()
  920.     >                 local   value;
  921.     >                 value := stack[Length(stack)];
  922.     >                 Unbind( stack[Length(stack)] );
  923.     >                 return value;
  924.     >             end
  925.     >         );
  926.     >    end;;
  927.     gap> A := Stack();;
  928.     gap> B := Stack();;
  929.     gap> A.push( 1 );  A.push( 2 );  A.push( 3 );
  930.     gap> B.push( 4 );  B.push( 5 );  B.push( 6 );
  931.     gap> A.pop();  A.pop();  A.pop();
  932.     3
  933.     2
  934.     1
  935.     gap> B.pop();  B.pop();  B.pop();
  936.     6
  937.     5
  938.     4 |
  939.  
  940. This feature should be used rarely, since its implementation in {\GAP} is
  941. not very efficient.
  942.  
  943. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  944. \Section{Return}%
  945. \index{return}
  946.  
  947. 'return;'
  948.  
  949. In this form 'return' terminates the call of  the innermost function that
  950. is currently executing, and control returns to  the calling function.  An
  951. error  is signalled if no  function is currently  executing.  No value is
  952. returned by the function.
  953.  
  954. 'return <expr>;'
  955.  
  956. In this form 'return' terminates the call of the innermost  function that
  957. is currently executing, and returns  the value  of the expression <expr>.
  958. Control returns  to the calling  function.  An error is  signalled if  no
  959. function is currently executing.
  960.  
  961. Both statements  can also be used in  break loops  (see  "Break  Loops").
  962. 'return;'  has the effect  that the  computation  continues  where it was
  963. interrupted by an error or the user hitting  '<ctr>C'.   'return <expr>;'
  964. can be used to continue execution after an error.  What  happens with the
  965. value <expr> depends on the particular error.
  966.  
  967. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  968. \Section{The Syntax in BNF}%
  969. \index{BNF}
  970.  
  971. This section contains the definition  of the {\GAP} syntax in Backus-Naur
  972. form.
  973.  
  974. A  BNF is a set of rules, whose left side  is the name of  a  syntactical
  975. construct.  Those  names  are enclosed in angle  brackets and  written in
  976. <italics>.  The right side of each rule contains a possible form for that
  977. syntactic  construct.   Each  right  side  may  contain  names  of  other
  978. syntactic  constructs,  again  enclosed in angle brackets and written  in
  979. <italics>,  or character  sequences that  must  occur literally; they are
  980. written in 'typewriter style'.
  981.  
  982. Furthermore  each righthand side  can contain  the  following metasymbols
  983. written in *boldface*.  If the right  hand  side contains forms separated
  984. by a pipe symbol  ($\|$)  this means  that one  of the possible forms can
  985. occur.  If a part of a form  is enclosed  in square brackets  ([ ])  this
  986. means that this part is optional, i.e.  might be present or  missing.  If
  987. part  of the form is enclosed  in curly braces  (\{  \})  this means that
  988. the part may occur arbitrarily often, or possibly be missing.
  989.  
  990. \newpage
  991. \begin{tabbing}
  992. <Permutation>  \=\:= \= <Expr>                                      \kill
  993. <Ident>        \>\:=  \>'a'$\|$...$\|$'z'$\|$'A'$\|$...$\|$'Z'$\|$'\_'
  994. \{'a'$\|$...$\|$'z'$\|$'A'$\|$...$\|$'Z'$\|$'0'$\|$...$\|$'9'$\|$'\_'\}\\
  995. <Var>          \>\:=  \><Ident>                                        \\
  996.                \>$\|$ \><Var> '.' <Ident>                              \\
  997.                \>$\|$ \><Var> '.' '(' <Expr> ')'                       \\
  998.                \>$\|$ \><Var> '[' <Expr> ']'                           \\
  999.                \>$\|$ \><Var> '\{' <Expr> '\}'                         \\
  1000.                \>$\|$ \><Var> '(' [ <Expr> \{ ',' <Expr> \} ] ')'      \\
  1001. <List>         \>\:=  \>'[' [ <Expr> ] \{',' [ <Expr> ] \} ']'         \\
  1002.                \>$\|$ \>'[' <Expr> [, <Expr> ] '..' <Expr> ']'         \\
  1003. <Record>       \>\:=  \>'rec(' [ <Ident> '\:=' <Expr>
  1004.                            \{',' <Ident> '\:=' <Expr> \} ] ')'         \\
  1005. <Permutation>  \>\:=  \>'(' <Expr> \{',' <Expr> \} ')'
  1006.                      \{ '(' <Expr> \{',' <Expr> \} ')' \}              \\
  1007. <Function>     \>\:=  \>'function (' [ <Ident> \{',' <Ident> \} ] ')'  \\
  1008.                \>     \>    [ 'local'  <Ident> \{',' <Ident> \} ';' ]  \\
  1009.                \>     \>    <Statements>                               \\
  1010.                \>     \>'end'                                          \\
  1011. <Char>         \>\:=  \>|'| <any character> |'|                        \\
  1012. <String>       \>\:=  \>'\"' \{ <any character> \} '\"'                \\
  1013. <Int>          \>\:=  \>'0'$\|$'1'$\|$...$\|$'9'
  1014.                      \{ '0'$\|$'1'$\|$...$\|$'9' \}                    \\
  1015. <Atom>         \>\:=  \><Int>                                          \\
  1016.                \>$\|$ \><Var>                                          \\
  1017.                \>$\|$ \>'(' <Expr> ')'                                 \\
  1018.                \>$\|$ \><Permutation>                                  \\
  1019.                \>$\|$ \><Char>                                         \\
  1020.                \>$\|$ \><String>                                       \\
  1021.                \>$\|$ \><Function>                                     \\
  1022.                \>$\|$ \><List>                                         \\
  1023.                \>$\|$ \><Record>                                       \\
  1024. <Factor>       \>\:=  \>\{'+'$\|$'-'\} <Atom> [ '\^' \{'+'$\|$'-'\} <Atom> ]\\
  1025. <Term>         \>\:=  \><Factor> \{ '\*'$\|$'/'$\|$'mod' <Factor> \}   \\
  1026. <Arith>        \>\:=  \><Term> \{ '+'$\|$'-' <Term> \}                 \\
  1027. <Rel>          \>\:=  \>\{ 'not' \} <Arith>
  1028.        \{ |=|$\|$|<>|$\|$|<|$\|$|>|$\|$|<=|$\|$|>=|$\|$|in| <Arith> \} \\
  1029. <And>          \>\:=  \><Rel> \{ 'and' <Rel> \}                        \\
  1030. <Log>          \>\:=  \><And> \{ 'or' <And> \}                         \\
  1031. <Expr>         \>\:=  \><Log>                                          \\
  1032.                \>$\|$ \><Var> [ '->' <Log> ]                           \\
  1033. <Statement>    \>\:=  \><Expr>                                         \\
  1034.                \>$\|$ \><Var> '\:=' <Expr>                             \\
  1035.                \>$\|$ \>'if'   <Expr>  'then' <Statements>             \\
  1036.                \>     \>\{ 'elif' <Expr>  'then' \=<Statements> \}     \\
  1037.                \>      \>[ 'else'                \><Statements>  ] 'fi'\\
  1038.                \>$\|$ \>'for' <Var> 'in' <Expr> 'do' <Statements> 'od' \\
  1039.                \>$\|$ \>'while' <Expr>  'do' <Statements>  'od'        \\
  1040.                \>$\|$ \>'repeat' <Statements>  'until' <Expr>          \\
  1041.                \>$\|$ \>'return' [ <Expr> ]                            \\
  1042.                \>$\|$ \>'quit'                                         \\
  1043. <Statements>   \>\:=  \>\{ <Statement> ';' \}                          \\
  1044.                \>$\|$ \>';'
  1045. \end{tabbing}
  1046.  
  1047.  
  1048. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1049. %%
  1050. %E  Emacs . . . . . . . . . . . . . . . . . . . . . local Emacs variables
  1051. %%
  1052. %%  Local Variables:
  1053. %%  mode:               outline
  1054. %%  outline-regexp:     "\\\\Chapter\\|\\\\Section"
  1055. %%  fill-column:        73
  1056. %%  eval:               (hide-body)
  1057. %%  End:
  1058. %%
  1059.  
  1060.  
  1061.  
  1062.