Quelle chap4_mj.html
Sprache: HTML
|
|
| products/sources/formale Sprachen/GAP/doc/ref/chap4_mj.html |
 |
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<script type="text/javascript"
src="https://cdn.jsdelivr.net/npm/mathjax@2/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
<title>GAP (ref) - Chapter 4: The Programming Language</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta name="generator" content="GAPDoc2HTML" />
<link rel="stylesheet" type="text/css" href="manual.css" />
<script src="manual.js" type="text/javascript"></script>
<script type="text/javascript">overwriteStyle();</script>
</head>
<body class="chap4" onload="jscontent()">
<div class="chlinktop"><span class="chlink1">Goto Chapter: </span><a href="chap0_mj.html">Top</a> <a href="chap1_mj.html">1</a> <a href="chap2_mj.html">2</a> <a href="chap3_mj.html">3</a> <a href="chap4_mj.html">4</a> <a href="chap5_mj.html">5</a> <a href="chap6_mj.html">6</a> <a href="chap7_mj.html">7</a> <a href="chap8_mj.html">8</a> <a href="chap9_mj.html">9</a> <a href="chap10_mj.html">10</a> <a href="chap11_mj.html">11</a> <a href="chap12_mj.html">12</a> <a href="chap13_mj.html">13</a> <a href="chap14_mj.html">14</a> <a href="chap15_mj.html">15</a> <a href="chap16_mj.html">16</a> <a href="chap17_mj.html">17</a> <a href="chap18_mj.html">18</a> <a href="chap19_mj.html">19</a> <a href="chap20_mj.html">20</a> <a href="chap21_mj.html">21</a> <a href="chap22_mj.html">22</a> <a href="chap23_mj.html">23</a> <a href="chap24_mj.html">24</a> <a href="chap25_mj.html">25</a> <a href="chap26_mj.html">26</a> <a href="chap27_mj.html">27</a> <a href="chap28_mj.html">28</a> <a href="chap29_mj.html">29</a> <a href="chap30_mj.html">30</a> <a href="chap31_mj.html">31</a> <a href="chap32_mj.html">32</a> <a href="chap33_mj.html">33</a> <a href="chap34_mj.html">34</a> <a href="chap35_mj.html">35</a> <a href="chap36_mj.html">36</a> <a href="chap37_mj.html">37</a> <a href="chap38_mj.html">38</a> <a href="chap39_mj.html">39</a> <a href="chap40_mj.html">40</a> <a href="chap41_mj.html">41</a> <a href="chap42_mj.html">42</a> <a href="chap43_mj.html">43</a> <a href="chap44_mj.html">44</a> <a href="chap45_mj.html">45</a> <a href="chap46_mj.html">46</a> <a href="chap47_mj.html">47</a> <a href="chap48_mj.html">48</a> <a href="chap49_mj.html">49</a> <a href="chap50_mj.html">50</a> <a href="chap51_mj.html">51</a> <a href="chap52_mj.html">52</a> <a href="chap53_mj.html">53</a> <a href="chap54_mj.html">54</a> <a href="chap55_mj.html">55</a> <a href="chap56_mj.html">56</a> <a href="chap57_mj.html">57</a> <a href="chap58_mj.html">58</a> <a href="chap59_mj.html">59</a> <a href="chap60_mj.html">60</a> <a href="chap61_mj.html">61</a> <a href="chap62_mj.html">62</a> <a href="chap63_mj.html">63</a> <a href="chap64_mj.html">64</a> <a href="chap65_mj.html">65</a> <a href="chap66_mj.html">66</a> <a href="chap67_mj.html">67</a> <a href="chap68_mj.html">68</a> <a href="chap69_mj.html">69</a> <a href="chap70_mj.html">70</a> <a href="chap71_mj.html">71</a> <a href="chap72_mj.html">72</a> <a href="chap73_mj.html">73</a> <a href="chap74_mj.html">74</a> <a href="chap75_mj.html">75</a> <a href="chap76_mj.html">76</a> <a href="chap77_mj.html">77</a> <a href="chap78_mj.html">78</a> <a href="chap79_mj.html">79</a> <a href="chap80_mj.html">80</a> <a href="chap81_mj.html">81</a> <a href="chap82_mj.html">82</a> <a href="chap83_mj.html">83</a> <a href="chap84_mj.html">84</a> <a href="chap85_mj.html">85</a> <a href="chap86_mj.html">86</a> <a href="chap87_mj.html">87</a> <a href="chapBib_mj.html">Bib</a> <a href="chapInd_mj.html">Ind</a> </div>
<div class="chlinkprevnexttop"> <a href="chap0_mj.html">[Top of Book]</a> <a href="chap0_mj.html#contents">[Contents]</a> <a href="chap3_mj.html">[Previous Chapter]</a> <a href="chap5_mj.html">[Next Chapter]</a> </div>
<p id="mathjaxlink" class="pcenter"><a href="chap4.html">[MathJax off]</a></p>
<p><a id="X7FE7C0C17E1ED118" name="X7FE7C0C17E1ED118"></a></p>
<div class="ChapSects"><a href="chap4_mj.html#X7FE7C0C17E1ED118">4 <span class="Heading">The Programming Language</span></a>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap4_mj.html#X7B5FF6827DFBDF20">4.1 <span class="Heading">Language Overview</span></a>
</span>
</div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap4_mj.html#X80A85A707B6F4BE7">4.2 <span class="Heading">Lexical Structure</span></a>
</span>
</div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap4_mj.html#X7E90E6607F4E4943">4.3 <span class="Heading">Symbols</span></a>
</span>
</div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap4_mj.html#X7C53CEFC8641B919">4.4 <span class="Heading">Whitespaces</span></a>
</span>
</div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap4_mj.html#X87506BDC7D5F789E">4.5 <span class="Heading">Keywords</span></a>
</span>
</div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap4_mj.html#X860313A179A5163F">4.6 <span class="Heading">Identifiers</span></a>
</span>
<div class="ContSSBlock">
<span class="ContSS"><br /><span class="nocss"> </span><a href="chap4_mj.html#X85CF993B7D19F2C4">4.6-1 IsValidIdentifier</a></span>
<span class="ContSS"><br /><span class="nocss"> </span><a href="chap4_mj.html#X839A7F8E84BBCA57">4.6-2 <span class="Heading">Conventions about Identifiers</span></a>
</span>
</div></div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap4_mj.html#X7BAFE9C1817253C6">4.7 <span class="Heading">Expressions</span></a>
</span>
</div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap4_mj.html#X7A4C2D0E7E286B4F">4.8 <span class="Heading">Variables</span></a>
</span>
<div class="ContSSBlock">
<span class="ContSS"><br /><span class="nocss"> </span><a href="chap4_mj.html#X842B89D4860FD5DB">4.8-1 IsBound</a></span>
<span class="ContSS"><br /><span class="nocss"> </span><a href="chap4_mj.html#X7BABB3E77F52626C">4.8-2 Unbind</a></span>
</div></div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap4_mj.html#X816FBEEA85782EC2">4.9 <span class="Heading">More About Global Variables</span></a>
</span>
<div class="ContSSBlock">
<span class="ContSS"><br /><span class="nocss"> </span><a href="chap4_mj.html#X7CD3523B84744EB2">4.9-1 IsReadOnlyGlobal</a></span>
<span class="ContSS"><br /><span class="nocss"> </span><a href="chap4_mj.html#X850CE44478254F27">4.9-2 MakeReadOnlyGlobal</a></span>
<span class="ContSS"><br /><span class="nocss"> </span><a href="chap4_mj.html#X832AAF13861968BE">4.9-3 MakeReadWriteGlobal</a></span>
<span class="ContSS"><br /><span class="nocss"> </span><a href="chap4_mj.html#X847706237E72418F">4.9-4 MakeConstantGlobal</a></span>
<span class="ContSS"><br /><span class="nocss"> </span><a href="chap4_mj.html#X84BB4B1E872849FF">4.9-5 ValueGlobal</a></span>
<span class="ContSS"><br /><span class="nocss"> </span><a href="chap4_mj.html#X823D4BC378395B32">4.9-6 IsBoundGlobal</a></span>
<span class="ContSS"><br /><span class="nocss"> </span><a href="chap4_mj.html#X829A5F0E811F77D3">4.9-7 UnbindGlobal</a></span>
<span class="ContSS"><br /><span class="nocss"> </span><a href="chap4_mj.html#X7D39D3E17CF49F5B">4.9-8 BindGlobal</a></span>
<span class="ContSS"><br /><span class="nocss"> </span><a href="chap4_mj.html#X876A6EB68745A510">4.9-9 NamesGVars</a></span>
<span class="ContSS"><br /><span class="nocss"> </span><a href="chap4_mj.html#X7E604AF579A7BC92">4.9-10 NamesSystemGVars</a></span>
<span class="ContSS"><br /><span class="nocss"> </span><a href="chap4_mj.html#X870169447AF490D8">4.9-11 NamesUserGVars</a></span>
</div></div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap4_mj.html#X7DF8774F7D542298">4.10 <span class="Heading">Namespaces for <strong class="pkg">GAP</strong> packages</span></a>
</span>
</div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap4_mj.html#X815F71EA7BC0EB6F">4.11 <span class="Heading">Function</span></a>
</span>
</div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap4_mj.html#X78C70489791FDF43">4.12 <span class="Heading">Function Calls</span></a>
</span>
<div class="ContSSBlock">
<span class="ContSS"><br /><span class="nocss"> </span><a href="chap4_mj.html#X80B93A9C7E0A57F4">4.12-1 <span class="Heading">Function Call With Arguments</span></a>
</span>
<span class="ContSS"><br /><span class="nocss"> </span><a href="chap4_mj.html#X867D54987EF86D1D">4.12-2 <span class="Heading">Function Call With Options</span></a>
</span>
</div></div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap4_mj.html#X7A274A1F8553B7E6">4.13 <span class="Heading">Comparisons</span></a>
</span>
</div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap4_mj.html#X7B66C8707B5DE10A">4.14 <span class="Heading">Arithmetic Operators</span></a>
</span>
</div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap4_mj.html#X8543285D87361BE6">4.15 <span class="Heading">Statements</span></a>
</span>
<div class="ContSSBlock">
<span class="ContSS"><br /><span class="nocss"> </span><a href="chap4_mj.html#X7E6A50307F4D3FAE">4.15-1 <span class="Heading">Assignments</span></a>
</span>
<span class="ContSS"><br /><span class="nocss"> </span><a href="chap4_mj.html#X825803DE78251DA6">4.15-2 <span class="Heading">Procedure Calls</span></a>
</span>
<span class="ContSS"><br /><span class="nocss"> </span><a href="chap4_mj.html#X875000188622700D">4.15-3 <span class="Heading">If</span></a>
</span>
<span class="ContSS"><br /><span class="nocss"> </span><a href="chap4_mj.html#X87AA46408783383F">4.15-4 <span class="Heading">While</span></a>
</span>
<span class="ContSS"><br /><span class="nocss"> </span><a href="chap4_mj.html#X8295CBF47FAA05C9">4.15-5 <span class="Heading">Repeat</span></a>
</span>
<span class="ContSS"><br /><span class="nocss"> </span><a href="chap4_mj.html#X78783E777867638A">4.15-6 <span class="Heading">For</span></a>
</span>
<span class="ContSS"><br /><span class="nocss"> </span><a href="chap4_mj.html#X7B60C6127E183021">4.15-7 <span class="Heading">Break</span></a>
</span>
<span class="ContSS"><br /><span class="nocss"> </span><a href="chap4_mj.html#X7CCBA2247AA366BD">4.15-8 <span class="Heading">Continue</span></a>
</span>
<span class="ContSS"><br /><span class="nocss"> </span><a href="chap4_mj.html#X812C6ABC7A182E9E">4.15-9 <span class="Heading">Return (With or without Value)</span></a>
</span>
</div></div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap4_mj.html#X8732D9257FFCEA1B">4.16 <span class="Heading">Syntax Trees</span></a>
</span>
<div class="ContSSBlock">
<span class="ContSS"><br /><span class="nocss"> </span><a href="chap4_mj.html#X81558D66810BEA67">4.16-1 SyntaxTree</a></span>
</div></div>
</div>
<h3>4 <span class="Heading">The Programming Language</span></h3>
<p>This chapter describes the <strong class="pkg">GAP</strong> programming language. It should allow you, in principle, to predict the result of each and every input. In order to know what we are talking about, we first have to look more closely at the process of interpretation and the various representations of data involved.</p>
<p><a id="X7B5FF6827DFBDF20" name="X7B5FF6827DFBDF20"></a></p>
<h4>4.1 <span class="Heading">Language Overview</span></h4>
<p>First we have the input to <strong class="pkg">GAP</strong>, given as a string of characters. How those characters enter <strong class="pkg">GAP</strong> is operating system dependent, e.g., they might be entered at a terminal, pasted with a mouse into a window, or read from a file. The mechanism does not matter. This representation of expressions by characters is called the <em>external representation</em> of the expression. Every expression has at least one external representation that can be entered to get exactly this expression.</p>
<p>The input, i.e., the external representation, is transformed in a process called <em>reading</em> to an internal representation. At this point the input is analyzed and inputs that are not legal external representations, according to the rules given below, are rejected as errors. Those rules are usually called the <em>syntax</em> of a programming language.</p>
<p>The internal representation created by reading is called either an <em>expression</em> or a <em>statement</em>. Later we will distinguish between those two terms. However for now we will use them interchangeably. The exact form of the internal representation does not matter. It could be a string of characters equal to the external representation, in which case the reading would only need to check for errors. It could be a series of machine instructions for the processor on which <strong class="pkg">GAP</strong> is running, in which case the reading would more appropriately be called compilation. It is in fact a tree-like structure.</p>
<p>After the input has been read it is again transformed in a process called <em>evaluation</em> or <em>execution</em>. Later we will distinguish between those two terms too, but for the moment we will use them interchangeably. The name hints at the nature of this process, it replaces an expression with the value of the expression. This works recursively, i.e., to evaluate an expression first the subexpressions are evaluated and then the value of the expression is computed from those values according to rules given below. Those rules are usually called the <em>semantics</em> of a programming language.</p>
<p>The result of the evaluation is, not surprisingly, called a <em>value</em>. Again the form in which such a value is represented internally does not matter. It is in fact a tree-like structure again.</p>
<p>The last process is called <em>printing</em>. It takes the value produced by the evaluation and creates an external representation, i.e., a string of characters again. What you do with this external representation is up to you. You can look at it, paste it with the mouse into another window, or write it to a file.</p>
<p>Lets look at an example to make this more clear. Suppose you type in the following string of 8 characters</p>
<div class="example"><pre>
1 + 2 * 3;
</pre></div>
<p><strong class="pkg">GAP</strong> takes this external representation and creates a tree-like internal representation, which we can picture as follows</p>
<div class="example"><pre>
+
/ \
1 *
/ \
2 3
</pre></div>
<p>This expression is then evaluated. To do this <strong class="pkg">GAP</strong> first evaluates the right subexpression <code class="code">2*3</code>. Again, to do this <strong class="pkg">GAP</strong> first evaluates its subexpressions 2 and 3. However they are so simple that they are their own value, we say that they are self-evaluating. After this has been done, the rule for <code class="code">*</code> tells us that the value is the product of the values of the two subexpressions, which in this case is clearly 6. Combining this with the value of the left operand of the <code class="code">+</code>, which is self-evaluating, too, gives us the value of the whole expression 7. This is then printed, i.e., converted into the external representation consisting of the single character <code class="code">7</code>.</p>
<p>In this fashion we can predict the result of every input when we know the syntactic rules that govern the process of reading and the semantic rules that tell us for every expression how its value is computed in terms of the values of the subexpressions. The syntactic rules are given in sections <a href="chap4_mj.html#X80A85A707B6F4BE7"><span class="RefLink">4.2</span></a>, <a href="chap4_mj.html#X7E90E6607F4E4943"><span class="RefLink">4.3</span></a>, <a href="chap4_mj.html#X7C53CEFC8641B919"><span class="RefLink">4.4</span></a>, <a href="chap4_mj.html#X87506BDC7D5F789E"><span class="RefLink">4.5</span></a>, and <a href="chap4_mj.html#X860313A179A5163F"><span class="RefLink">4.6</span></a>, the semantic rules are given in sections <a href="chap4_mj.html#X7BAFE9C1817253C6"><span class="RefLink">4.7</span></a>, <a href="chap4_mj.html#X7A4C2D0E7E286B4F"><span class="RefLink">4.8</span></a>, <a href="chap4_mj.html#X78C70489791FDF43"><span class="RefLink">4.12</span></a>, <a href="chap4_mj.html#X7A274A1F8553B7E6"><span class="RefLink">4.13</span></a>, <a href="chap4_mj.html#X7B66C8707B5DE10A"><span class="RefLink">4.14</span></a>, <a href="chap4_mj.html#X8543285D87361BE6"><span class="RefLink">4.15</span></a>, <a href="chap4_mj.html#X7E6A50307F4D3FAE"><span class="RefLink">4.15-1</span></a>, <a href="chap4_mj.html#X825803DE78251DA6"><span class="RefLink">4.15-2</span></a>, <a href="chap4_mj.html#X875000188622700D"><span class="RefLink">4.15-3</span></a>, <a href="chap4_mj.html#X87AA46408783383F"><span class="RefLink">4.15-4</span></a>, <a href="chap4_mj.html#X8295CBF47FAA05C9"><span class="RefLink">4.15-5</span></a>, <a href="chap4_mj.html#X78783E777867638A"><span class="RefLink">4.15-6</span></a>, <a href="chap4_mj.html#X815F71EA7BC0EB6F"><span class="RefLink">4.11</span></a>, and the chapters describing the individual data types.</p>
<p><a id="X80A85A707B6F4BE7" name="X80A85A707B6F4BE7"></a></p>
<h4>4.2 <span class="Heading">Lexical Structure</span></h4>
<p>Most input of <strong class="pkg">GAP</strong> consists of sequences of the following characters.</p>
<p>Digits, uppercase and lowercase letters, <strong class="button">Space</strong>, <strong class="button">Tab</strong>, <strong class="button">Newline</strong>, <strong class="button">Return</strong> and the special characters</p>
<div class="example"><pre>
" ' ( ) * + , - #
. / : ; < = > ~
[ \ ] ^ _ { } !
</pre></div>
<p>It is possible to use other characters in identifiers by escaping them with backslashes, but we do not recommend the use of this feature. Inside strings (see section <a href="chap4_mj.html#X7E90E6607F4E4943"><span class="RefLink">4.3</span></a> and chapter <a href="chap27_mj.html#X7D28329B7EDB8F47"><span class="RefLink">27</span></a>) and comments (see <a href="chap4_mj.html#X7C53CEFC8641B919"><span class="RefLink">4.4</span></a>) the full character set supported by the computer is allowed.</p>
<p><a id="X7E90E6607F4E4943" name="X7E90E6607F4E4943"></a></p>
<h4>4.3 <span class="Heading">Symbols</span></h4>
<p>The process of reading, i.e., of assembling the input into expressions, has a subprocess, called <em>scanning</em>, that assembles the characters into symbols. A <em>symbol</em> is a sequence of characters that form a lexical unit. The set of symbols consists of keywords, identifiers, strings, integers, and operator and delimiter symbols.</p>
<p>A <em>keyword</em> is a reserved word (see <a href="chap4_mj.html#X87506BDC7D5F789E"><span class="RefLink">4.5</span></a>). An <em>identifier</em> is a sequence of letters, digits and underscores (or other characters escaped by backslashes) that contains at least one non-digit and is not a keyword (see <a href="chap4_mj.html#X860313A179A5163F"><span class="RefLink">4.6</span></a>). An integer is a sequence of digits (see <a href="chap14_mj.html#X853DF11B80068ED5"><span class="RefLink">14</span></a>), possibly prepended by <code class="code">-</code> and <code class="code">+</code> sign characters. A <em>string</em> is a sequence of arbitrary characters enclosed in double quotes (see <a href="chap27_mj.html#X7D28329B7EDB8F47"><span class="RefLink">27</span></a>).</p>
<p>Operator and delimiter symbols are</p>
<div class="example"><pre>
+ - * / ^ ~ !.
= <> < <= > >= ![
:= . .. -> , ; [
] { } ( ) :
</pre></div>
<p>Note also that during the process of scanning all whitespace is removed (see <a href="chap4_mj.html#X7C53CEFC8641B919"><span class="RefLink">4.4</span></a>).</p>
<p><a id="X7C53CEFC8641B919" name="X7C53CEFC8641B919"></a></p>
<h4>4.4 <span class="Heading">Whitespaces</span></h4>
<p>The characters <strong class="button">Space</strong>, <strong class="button">Tab</strong>, <strong class="button">Newline</strong>, and <strong class="button">Return</strong> are called <em>whitespace characters</em>. Whitespace is used as necessary to separate lexical symbols, such as integers, identifiers, or keywords. For example <code class="code">Thorondor</code> is a single identifier, while <code class="code">Th or ondor</code> is the keyword <code class="keyw">or</code> between the two identifiers <code class="code">Th</code> and <code class="code">ondor</code>. Whitespace may occur between any two symbols, but not within a symbol. Two or more adjacent whitespace characters are equivalent to a single whitespace. Apart from the role as separator of symbols, whitespace characters are otherwise insignificant. Whitespace characters may also occur inside a string, where they are significant. Whitespace characters should also be used freely for improved readability.</p>
<p>A <em>comment</em> starts with the character <code class="code">#</code>, which is sometimes called sharp or hatch, and continues to the end of the line on which the comment character appears. The whole comment, including <code class="code">#</code> and the <strong class="button">Newline</strong> character is treated as a single whitespace. Inside a string, the comment character <code class="code">#</code> loses its role and is just an ordinary character.</p>
<p>For example, the following statement</p>
<div class="example"><pre>
if i<0 then a:=-i;else a:=i;fi;
</pre></div>
<p>is equivalent to</p>
<div class="example"><pre>
if i < 0 then # if i is negative
a := -i; # take its additive inverse
else # otherwise
a := i; # take itself
fi;
</pre></div>
<p>(which by the way shows that it is possible to write superfluous comments). However the first statement is <em>not</em> equivalent to</p>
<div class="example"><pre>
ifi<0thena:=-i;elsea:=i;fi;
</pre></div>
<p>since the keyword <code class="keyw">if</code> must be separated from the identifier <code class="code">i</code> by a whitespace, and similarly <code class="keyw">then</code> and <code class="code">a</code>, and <code class="keyw">else</code> and <code class="code">a</code> must be separated.</p>
<p><a id="X87506BDC7D5F789E" name="X87506BDC7D5F789E"></a></p>
<h4>4.5 <span class="Heading">Keywords</span></h4>
<p><em>Keywords</em> are reserved words that are used to denote special operations or are part of statements. They must not be used as identifiers. The list of keywords is contained in the <code class="code">GAPInfo.Keywords</code> component of the <code class="code">GAPInfo</code> record (see <a href="chap3_mj.html#X8354754E7935F935"><span class="RefLink">3.5-1</span></a>). We will show how to print it in a nice table, demonstrating at the same time some list manipulation techniques:</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">keys:=SortedList( GAPInfo.Keywords );; l:=Length( keys );;</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">arr:= List( [ 0 .. Int( l/4 )-1 ], i-> keys{ 4*i + [ 1 .. 4 ] } );;</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">if l mod 4 <> 0 then Add( arr, keys{[ 4*Int(l/4) + 1 .. l ]} ); fi;</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">Length( keys ); PrintArray( arr );</span>
35
[ [ Assert, Info, IsBound, QUIT ],
[ TryNextMethod, Unbind, and, atomic ],
[ break, continue, do, elif ],
[ else, end, false, fi ],
[ for, function, if, in ],
[ local, mod, not, od ],
[ or, quit, readonly, readwrite ],
[ rec, repeat, return, then ],
[ true, until, while ] ]
</pre></div>
<p>Note that (almost) all keywords are written in lowercase and that they are case sensitive. For example <code class="keyw">else</code> is a keyword; <code class="code">Else</code>, <code class="code">eLsE</code>, <code class="code">ELSE</code> and so forth are ordinary identifiers. Keywords must not contain whitespace, for example <code class="code">el if</code> is not the same as <code class="keyw">elif</code>.</p>
<p><em>Note</em>: Several tokens from the list of keywords above may appear to be normal identifiers representing functions or literals of various kinds but are actually implemented as keywords for technical reasons. The only consequence of this is that those identifiers cannot be re-assigned, and do not actually have function objects bound to them, which could be assigned to other variables or passed to functions. These keywords are <code class="keyw">true</code>, <code class="keyw">false</code>, <code class="func">Assert</code> (<a href="chap7_mj.html#X830E443284780FB9"><span class="RefLink">7.5-3</span></a>), <code class="func">IsBound</code> (<a href="chap4_mj.html#X842B89D4860FD5DB"><span class="RefLink">4.8-1</span></a>), <code class="func">Unbind</code> (<a href="chap4_mj.html#X7BABB3E77F52626C"><span class="RefLink">4.8-2</span></a>), <code class="func">Info</code> (<a href="chap7_mj.html#X864E4B6886E2697D"><span class="RefLink">7.4-6</span></a>) and <code class="func">TryNextMethod</code> (<a href="chap78_mj.html#X7EED949B83046A7F"><span class="RefLink">78.5-1</span></a>).</p>
<p>Keywords <code class="code">atomic</code>, <code class="code">readonly</code>, <code class="code">readwrite</code> are not used at the moment. They are reserved for the future version of GAP to prevent their accidental use as identifiers.</p>
<p><a id="X860313A179A5163F" name="X860313A179A5163F"></a></p>
<h4>4.6 <span class="Heading">Identifiers</span></h4>
<p>An <em>identifier</em> is used to refer to a variable (see <a href="chap4_mj.html#X7A4C2D0E7E286B4F"><span class="RefLink">4.8</span></a>). An identifier usually consists of letters, digits, underscores <code class="code">_</code>, and <q>at</q>-characters <code class="code">@</code>, and must contain at least one non-digit. An identifier is terminated by the first character not in this class. Note that the <q>at</q>-character <code class="code">@</code> is used to implement namespaces, see Section <a href="chap4_mj.html#X7DF8774F7D542298"><span class="RefLink">4.10</span></a> for details.</p>
<p>Examples of valid identifiers are</p>
<div class="example"><pre>
a foo aLongIdentifier
hello Hello HELLO
x100 100x _100
some_people_prefer_underscores_to_separate_words
WePreferMixedCaseToSeparateWords
abc@def
</pre></div>
<p>Note that case is significant, so the three identifiers in the second line are distinguished.</p>
<p>The backslash <code class="code">\</code> can be used to include other characters in identifiers; a backslash followed by a character is equivalent to the character, except that this escape sequence is considered to be an ordinary letter. For example</p>
<div class="example"><pre>
G\(2\,5\)
</pre></div>
<p>is an identifier, not a call to a function <code class="code">G</code>.</p>
<p>An identifier that starts with a backslash is never a keyword, so for example <code class="code">\*</code> and <code class="code">\mod</code> are identifiers.</p>
<p>The length of identifiers is not limited, however only the first <span class="SimpleMath">\(1023\)</span> characters are significant. The escape sequence <code class="code">\</code><strong class="button">newline</strong> is ignored, making it possible to split long identifiers over multiple lines.</p>
<p><a id="X85CF993B7D19F2C4" name="X85CF993B7D19F2C4"></a></p>
<h5>4.6-1 IsValidIdentifier</h5>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ IsValidIdentifier</code>( <var class="Arg">str</var> )</td><td class="tdright">( function )</td></tr></table></div>
<p>returns <code class="keyw">true</code> if the string <var class="Arg">str</var> would form a valid identifier consisting of letters, digits and underscores; otherwise it returns <code class="keyw">false</code>. It does not check whether <var class="Arg">str</var> contains characters escaped by a backslash <code class="code">\</code>.</p>
<p>Note that the <q>at</q>-character is used to implement namespaces for global variables in packages. See <a href="chap4_mj.html#X7DF8774F7D542298"><span class="RefLink">4.10</span></a> for details.</p>
<p><a id="X839A7F8E84BBCA57" name="X839A7F8E84BBCA57"></a></p>
<h5>4.6-2 <span class="Heading">Conventions about Identifiers</span></h5>
<p>(The following rule is stated also in Section <a href="../../doc/tut/chap2_mj.html#X82F47EA88347AC2D"><span class="RefLink">Tutorial: Variables versus Objects</span></a>.)</p>
<p>The name of almost every global variable in the <strong class="pkg">GAP</strong> library and in <strong class="pkg">GAP</strong> packages starts with a <em>capital letter</em>. (See Section <a href="chap6_mj.html#X81667F568237B232"><span class="RefLink">6.1</span></a> for the few exceptions.) For user variables, we recommend only choosing names that start with a <em>lower case letter</em>, in order to avoid name clashes.</p>
<p>For example, valid <strong class="pkg">GAP</strong> input which assigns some user variables whose names start with capital letters may run into errors with a newer version of <strong class="pkg">GAP</strong> or in a <strong class="pkg">GAP</strong> session with more or newer packages, because it may happen that these variables are predefined global variables in this situation.</p>
<p><a id="X7BAFE9C1817253C6" name="X7BAFE9C1817253C6"></a></p>
<h4>4.7 <span class="Heading">Expressions</span></h4>
<p>An <em>expression</em> is a construct that evaluates to a value. Syntactic constructs that are executed to produce a side effect and return no value are called <em>statements</em> (see <a href="chap4_mj.html#X8543285D87361BE6"><span class="RefLink">4.15</span></a>). Expressions appear as right hand sides of assignments (see <a href="chap4_mj.html#X7E6A50307F4D3FAE"><span class="RefLink">4.15-1</span></a>), as actual arguments in function calls (see <a href="chap4_mj.html#X78C70489791FDF43"><span class="RefLink">4.12</span></a>), and in statements.</p>
<p>Note that an expression is not the same as a value. For example <code class="code">1 + 11</code> is an expression, whose value is the integer 12. The external representation of this integer is the character sequence <code class="code">12</code>, i.e., this sequence is output if the integer is printed. This sequence is another expression whose value is the integer <span class="SimpleMath">\(12\)</span>. The process of finding the value of an expression is done by the interpreter and is called the <em>evaluation</em> of the expression.</p>
<p>The simplest cases of expressions are the following:</p>
<ul>
<li><p>variables (see Section <a href="chap4_mj.html#X7A4C2D0E7E286B4F"><span class="RefLink">4.8</span></a>),</p>
</li>
<li><p>function literals (see Section <a href="chap4_mj.html#X815F71EA7BC0EB6F"><span class="RefLink">4.11</span></a>),</p>
</li>
<li><p>function calls (see Section <a href="chap4_mj.html#X78C70489791FDF43"><span class="RefLink">4.12</span></a>),</p>
</li>
<li><p>integer literals (see Chapter <a href="chap14_mj.html#X853DF11B80068ED5"><span class="RefLink">14</span></a>),</p>
</li>
<li><p>floating point literals (see Chapter <a href="chap19_mj.html#X81AA901181CA568F"><span class="RefLink">19</span></a>),</p>
</li>
<li><p>permutation literals (see Chapter <a href="chap42_mj.html#X80F808307A2D5AB8"><span class="RefLink">42</span></a>),</p>
</li>
<li><p>string literals (see Chapter <a href="chap27_mj.html#X7D28329B7EDB8F47"><span class="RefLink">27</span></a>),</p>
</li>
<li><p>character literals (see Chapter <a href="chap27_mj.html#X7D28329B7EDB8F47"><span class="RefLink">27</span></a>),</p>
</li>
<li><p>list literals (see Chapter <a href="chap21_mj.html#X7B256AE5780F140A"><span class="RefLink">21</span></a>), and</p>
</li>
<li><p>record literals (see Chapter <a href="chap29_mj.html#X7AA1073C7E943DD7"><span class="RefLink">29</span></a>).</p>
</li>
</ul>
<p>Expressions, for example the simple expressions mentioned above, can be combined with the operators to form more complex expressions. Of course those expressions can then be combined further with the operators to form even more complex expressions. The <em>operators</em> fall into three classes. The <em>comparisons</em> are <code class="code">=</code>, <code class="code"><></code>, <code class="code"><</code>, <code class="code"><=</code>, <code class="code">></code>, <code class="code">>=</code>, and <code class="keyw">in</code> (see <a href="chap4_mj.html#X7A274A1F8553B7E6"><span class="RefLink">4.13</span></a> and <a href="chap30_mj.html#X82D39CF980FDBFFA"><span class="RefLink">30.6</span></a>). The <em>arithmetic operators</em> are <code class="code">+</code>, <code class="code">-</code>, <code class="code">*</code>, <code class="code">/</code>, <code class="keyw">mod</code>, and <code class="code">^</code> (see <a href="chap4_mj.html#X7B66C8707B5DE10A"><span class="RefLink">4.14</span></a>). The <em>logical operators</em> are <code class="keyw">not</code>, <code class="keyw">and</code>, and <code class="keyw">or</code> (see <a href="chap20_mj.html#X79AD41A185FD7213"><span class="RefLink">20.4</span></a>).</p>
<p>The following example shows a very simple expression with value 4 and a more complex expression.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">2 * 2;</span>
4
<span class="GAPprompt">gap></span> <span class="GAPinput">2 * 2 + 9 = Fibonacci(7) and Fibonacci(13) in Primes;</span>
true
</pre></div>
<p>The following table lists all operators by precedence, from highest to lowest, and also indicates whether the operator is left associative (aka left-to-right) or right associative (aka right-to-left) or neither.</p>
<div class="pcenter"><table class="GAPDocTable">
<tr>
<td class="tdleft"><em>operator</em></td>
<td class="tdleft"><em>associativity</em></td>
</tr>
<tr>
<td class="tdleft"><em>arithmetic (see <a href="chap4_mj.html#X7B66C8707B5DE10A"><span class="RefLink">4.14</span></a>)</em></td>
<td class="tdleft"></td>
</tr>
<tr>
<td class="tdleft"> <code class="code">^</code></td>
<td class="tdleft">none</td>
</tr>
<tr>
<td class="tdleft"> unary <code class="code">+</code>, unary <code class="code">-</code></td>
<td class="tdleft">right-to-left</td>
</tr>
<tr>
<td class="tdleft"> <code class="code">*</code>, <code class="code">/</code>, <code class="keyw">mod</code></td>
<td class="tdleft">left-to-right</td>
</tr>
<tr>
<td class="tdleft"> binary <code class="code">+</code>, binary <code class="code">-</code></td>
<td class="tdleft">left-to-right</td>
</tr>
<tr>
<td class="tdleft"><em>comparison (see <a href="chap4_mj.html#X7A274A1F8553B7E6"><span class="RefLink">4.13</span></a>)</em></td>
<td class="tdleft"></td>
</tr>
<tr>
<td class="tdleft"> <code class="code">=</code>, <code class="code"><></code>, <code class="code"><</code>, <code class="code"><=</code>, <code class="code">></code>, <code class="code">>=</code>, and <code class="keyw">in</code></td>
<td class="tdleft">none</td>
</tr>
<tr>
<td class="tdleft"><em>logical (see <a href="chap20_mj.html#X79AD41A185FD7213"><span class="RefLink">20.4</span></a>)</em></td>
<td class="tdleft"></td>
</tr>
<tr>
<td class="tdleft"> <code class="keyw">not</code></td>
<td class="tdleft">right-to-left</td>
</tr>
<tr>
<td class="tdleft"> <code class="keyw">and</code></td>
<td class="tdleft">left-to-right</td>
</tr>
<tr>
<td class="tdleft"> <code class="keyw">or</code></td>
<td class="tdleft">left-to-right</td>
</tr>
</table><br />
</div>
<p><a id="X7A4C2D0E7E286B4F" name="X7A4C2D0E7E286B4F"></a></p>
<h4>4.8 <span class="Heading">Variables</span></h4>
<p>A <em>variable</em> is a location in a <strong class="pkg">GAP</strong> program that points to a value. We say the variable is <em>bound</em> to this value. If a variable is evaluated it evaluates to this value.</p>
<p>Initially an ordinary variable is not bound to any value. The variable can be bound to a value by <em>assigning</em> this value to the variable (see <a href="chap4_mj.html#X7E6A50307F4D3FAE"><span class="RefLink">4.15-1</span></a>). Because of this we sometimes say that a variable that is not bound to any value has no assigned value. Assignment is in fact the only way by which a variable, which is not an argument of a function, can be bound to a value. After a variable has been bound to a value an assignment can also be used to bind the variable to another value.</p>
<p>A special class of variables is the class of <em>arguments</em> of functions. They behave similarly to other variables, except they are bound to the value of the actual arguments upon a function call (see <a href="chap4_mj.html#X78C70489791FDF43"><span class="RefLink">4.12</span></a>).</p>
<p>Each variable has a name that is also called its <em>identifier</em>. This is because in a given scope an identifier identifies a unique variable (see <a href="chap4_mj.html#X860313A179A5163F"><span class="RefLink">4.6</span></a>). A <em>scope</em> is a lexical part of a program text. There is the <em>global scope</em> that encloses the entire program text, and there are local scopes that range from the <code class="keyw">function</code> keyword, denoting the beginning of a function definition, to the corresponding <code class="keyw">end</code> keyword. A <em>local scope</em> introduces new variables, whose identifiers are given in the formal argument list and the <code class="keyw">local</code> declaration of the function (see <a href="chap4_mj.html#X815F71EA7BC0EB6F"><span class="RefLink">4.11</span></a>). Usage of an identifier in a program text refers to the variable in the innermost scope that has this identifier as its name. Because this mapping from identifiers to variables is done when the program is read, not when it is executed, <strong class="pkg">GAP</strong> is said to have <em>lexical scoping</em>. The following example shows how one identifier refers to different variables at different points in the program text.</p>
<div class="example"><pre>
g := 0; # global variable g
x := function ( a, b, c )
local y;
g := c; # c refers to argument c of function x
y := function ( y )
local d, e, f;
d := y; # y refers to argument y of function y
e := b; # b refers to argument b of function x
f := g; # g refers to global variable g
return d + e + f;
end;
return y( a ); # y refers to local y of function x
end;
</pre></div>
<p>It is important to note that the concept of a variable in <strong class="pkg">GAP</strong> is quite different from the concept of a variable in most compiled programming languages.</p>
<p>In those languages a variable denotes a block of memory. The value of the variable is stored in this block. So in those languages two variables can have the same value, but they can never have identical values, because they denote different blocks of memory. Note that some languages have the concept of a reference argument. It seems as if such an argument and the variable used in the actual function call have the same value, since changing the argument's value also changes the value of the variable used in the actual function call. But this is not so; the reference argument is actually a pointer to the variable used in the actual function call, and it is the compiler that inserts enough magic to make the pointer invisible. In order for this to work the compiler needs enough information to compute the amount of memory needed for each variable in a program, which is readily available in the declarations.
<p>In <strong class="pkg">GAP</strong> on the other hand each variable just points to a value, and different variables can share the same value.</p>
<p><a id="X842B89D4860FD5DB" name="X842B89D4860FD5DB"></a></p>
<h5>4.8-1 IsBound</h5>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ IsBound</code>( <var class="Arg">ident</var> )</td><td class="tdright">( function )</td></tr></table></div>
<p><code class="func">IsBound</code> returns <code class="keyw">true</code> if the variable <var class="Arg">ident</var> points to a value, and <code class="keyw">false</code> otherwise.</p>
<p>For records and lists <code class="func">IsBound</code> can be used to check whether components or entries, respectively, are bound (see Chapters <a href="chap29_mj.html#X7AA1073C7E943DD7"><span class="RefLink">29</span></a> and <a href="chap21_mj.html#X7B256AE5780F140A"><span class="RefLink">21</span></a>).</p>
<p><a id="X7BABB3E77F52626C" name="X7BABB3E77F52626C"></a></p>
<h5>4.8-2 Unbind</h5>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ Unbind</code>( <var class="Arg">ident</var> )</td><td class="tdright">( function )</td></tr></table></div>
<p>deletes the identifier <var class="Arg">ident</var>. If there is no other variable pointing to the same value as <var class="Arg">ident</var> was, this value will be removed by the next garbage collection. Therefore <code class="func">Unbind</code> can be used to get rid of unwanted large objects.</p>
<p>For records and lists <code class="func">Unbind</code> can be used to delete components or entries, respectively (see Chapters <a href="chap29_mj.html#X7AA1073C7E943DD7"><span class="RefLink">29</span></a> and <a href="chap21_mj.html#X7B256AE5780F140A"><span class="RefLink">21</span></a>).</p>
<p><a id="X816FBEEA85782EC2" name="X816FBEEA85782EC2"></a></p>
<h4>4.9 <span class="Heading">More About Global Variables</span></h4>
<p>The vast majority of variables in <strong class="pkg">GAP</strong> are defined at the outer level (the global scope). They are used to access functions and other objects created either in the <strong class="pkg">GAP</strong> library or packages or in the user's code.
<p>Note that for packages there is a mechanism to implement package local namespaces on top of this global namespace. See Section <a href="chap4_mj.html#X7DF8774F7D542298"><span class="RefLink">4.10</span></a> for details.</p>
<p>Certain special facilities are provided for manipulating global variables which are not available for other types of variable (such as local variables or function arguments).</p>
<p>First, such variables may be marked <em>read-only</em> using <code class="func">MakeReadOnlyGlobal</code> (<a href="chap4_mj.html#X850CE44478254F27"><span class="RefLink">4.9-2</span></a>). In which case attempts to change them will fail. Most of the global variables defined in the <strong class="pkg">GAP</strong> library are so marked. <em>read-only</em> variables can be made read-write again by calling <code class="func">MakeReadWriteGlobal</code> (<a href="chap4_mj.html#X832AAF13861968BE"><span class="RefLink">4.9-3</span></a>). GAP also features <em>constant</em> variables, which are created by calling <code class="func">MakeConstantGlobal</code> (<a href="chap4_mj.html#X847706237E72418F"><span class="RefLink">4.9-4</span></a>). Constant variables can never be changed. In some cases, GAP can optimise code which uses <em>constant</em> variables, as their value never changes. In this version GAP these optimisations can be observed by printing the function back out, but this behaviour may change in future.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">globali := 1 + 2;;</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">globalb := true;;</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">MakeConstantGlobal("globali");</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">MakeConstantGlobal("globalb");</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">f := function()</span>
<span class="GAPprompt">></span> <span class="GAPinput"> if globalb then</span>
<span class="GAPprompt">></span> <span class="GAPinput"> return globali + 1;</span>
<span class="GAPprompt">></span> <span class="GAPinput"> else</span>
<span class="GAPprompt">></span> <span class="GAPinput"> return globali + 2;</span>
<span class="GAPprompt">></span> <span class="GAPinput"> fi;</span>
<span class="GAPprompt">></span> <span class="GAPinput">end;;</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">Print(f);</span>
function ( )
return 3 + 1;
end
</pre></div>
<p>Second, a group of functions are supplied for accessing and altering the values assigned to global variables. Use of these functions differs from the use of assignment, <code class="func">Unbind</code> (<a href="chap4_mj.html#X7BABB3E77F52626C"><span class="RefLink">4.8-2</span></a>) and <code class="func">IsBound</code> (<a href="chap4_mj.html#X842B89D4860FD5DB"><span class="RefLink">4.8-1</span></a>) statements, in two ways. First, these functions always affect global variables, even if local variables of the same names exist. Second, the variable names are passed as strings, rather than being written directly into the statements.</p>
<p>Note that the functions <code class="func">NamesGVars</code> (<a href="chap4_mj.html#X876A6EB68745A510"><span class="RefLink">4.9-9</span></a>), <code class="func">NamesSystemGVars</code> (<a href="chap4_mj.html#X7E604AF579A7BC92"><span class="RefLink">4.9-10</span></a>), and <code class="func">NamesUserGVars</code> (<a href="chap4_mj.html#X870169447AF490D8"><span class="RefLink">4.9-11</span></a>), deal with the <em>global namespace</em>.</p>
<p><a id="X7CD3523B84744EB2" name="X7CD3523B84744EB2"></a></p>
<h5>4.9-1 IsReadOnlyGlobal</h5>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ IsReadOnlyGlobal</code>( <var class="Arg">name</var> )</td><td class="tdright">( function )</td></tr></table></div>
<p>returns <code class="keyw">true</code> if the global variable named by the string <var class="Arg">name</var> is read-only and <code class="keyw">false</code> otherwise (the default).</p>
<p><a id="X850CE44478254F27" name="X850CE44478254F27"></a></p>
<h5>4.9-2 MakeReadOnlyGlobal</h5>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ MakeReadOnlyGlobal</code>( <var class="Arg">name</var> )</td><td class="tdright">( function )</td></tr></table></div>
<p>marks the global variable named by the string <var class="Arg">name</var> as read-only.</p>
<p>A warning is given if <var class="Arg">name</var> has no value bound to it or if it is already read-only.</p>
<p><a id="X832AAF13861968BE" name="X832AAF13861968BE"></a></p>
<h5>4.9-3 MakeReadWriteGlobal</h5>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ MakeReadWriteGlobal</code>( <var class="Arg">name</var> )</td><td class="tdright">( function )</td></tr></table></div>
<p>marks the global variable named by the string <var class="Arg">name</var> as read-write.</p>
<p>A warning is given if <var class="Arg">name</var> is already read-write.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">xx := 17;</span>
17
<span class="GAPprompt">gap></span> <span class="GAPinput">IsReadOnlyGlobal("xx");</span>
false
<span class="GAPprompt">gap></span> <span class="GAPinput">xx := 15;</span>
15
<span class="GAPprompt">gap></span> <span class="GAPinput">MakeReadOnlyGlobal("xx");</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">xx := 16;</span>
Variable: 'xx' is read only
not in any function
Entering break read-eval-print loop ...
you can 'quit;' to quit to outer loop, or
you can 'return;' after making it writable to continue
<span class="GAPbrkprompt">brk></span> <span class="GAPinput">quit;</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">IsReadOnlyGlobal("xx");</span>
true
<span class="GAPprompt">gap></span> <span class="GAPinput">MakeReadWriteGlobal("xx");</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">xx := 16;</span>
16
<span class="GAPprompt">gap></span> <span class="GAPinput">IsReadOnlyGlobal("xx");</span>
false
</pre></div>
<p><a id="X847706237E72418F" name="X847706237E72418F"></a></p>
<h5>4.9-4 MakeConstantGlobal</h5>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ MakeConstantGlobal</code>( <var class="Arg">name</var> )</td><td class="tdright">( function )</td></tr></table></div>
<p>MakeConstantGlobal ( <var class="Arg">name</var> ) marks the global variable named by the string <var class="Arg">name</var> as constant. A constant variable can never be reassigned or made read-write again.</p>
<p>A warning is given if <var class="Arg">name</var> is already constant.</p>
<p><a id="X84BB4B1E872849FF" name="X84BB4B1E872849FF"></a></p>
<h5>4.9-5 ValueGlobal</h5>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ ValueGlobal</code>( <var class="Arg">name</var> )</td><td class="tdright">( function )</td></tr></table></div>
<p>returns the value currently bound to the global variable named by the string <var class="Arg">name</var>. An error is raised if no value is currently bound.</p>
<p><a id="X823D4BC378395B32" name="X823D4BC378395B32"></a></p>
<h5>4.9-6 IsBoundGlobal</h5>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ IsBoundGlobal</code>( <var class="Arg">name</var> )</td><td class="tdright">( function )</td></tr></table></div>
<p>returns <code class="keyw">true</code> if a value currently bound to the global variable named by the string <var class="Arg">name</var> and <code class="keyw">false</code> otherwise.</p>
<p><a id="X829A5F0E811F77D3" name="X829A5F0E811F77D3"></a></p>
<h5>4.9-7 UnbindGlobal</h5>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ UnbindGlobal</code>( <var class="Arg">name</var> )</td><td class="tdright">( function )</td></tr></table></div>
<p>removes any value currently bound to the global variable named by the string <var class="Arg">name</var>. Nothing is returned.</p>
<p>A warning is given if <var class="Arg">name</var> was not bound. The global variable named by <var class="Arg">name</var> must be writable, otherwise an error is raised.</p>
<p><a id="X7D39D3E17CF49F5B" name="X7D39D3E17CF49F5B"></a></p>
<h5>4.9-8 BindGlobal</h5>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ BindGlobal</code>( <var class="Arg">name</var>, <var class="Arg">val</var> )</td><td class="tdright">( function )</td></tr></table></div>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ BindConstant</code>( <var class="Arg">name</var>, <var class="Arg">val</var> )</td><td class="tdright">( function )</td></tr></table></div>
<p><code class="func">BindGlobal</code> and <code class="func">BindConstant</code> set the global variable named by the string <var class="Arg">name</var> to the value <var class="Arg">val</var>, provided that variable is writable. <code class="func">BindGlobal</code> makes the resulting variable read-only, while <code class="func">BindConstant</code> makes it constant. If <var class="Arg">name</var> already had a value, a warning message is printed.</p>
<p>This is intended to be the normal way to create and set <q>official</q> global variables (such as operations, filters and constants).</p>
<p>Caution should be exercised in using these functions, especially <code class="func">UnbindGlobal</code> (<a href="chap4_mj.html#X829A5F0E811F77D3"><span class="RefLink">4.9-7</span></a>) as unexpected changes in global variables can be very confusing for the user.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">xx := 16;</span>
16
<span class="GAPprompt">gap></span> <span class="GAPinput">IsReadOnlyGlobal("xx");</span>
false
<span class="GAPprompt">gap></span> <span class="GAPinput">ValueGlobal("xx");</span>
16
<span class="GAPprompt">gap></span> <span class="GAPinput">IsBoundGlobal("xx");</span>
true
<span class="GAPprompt">gap></span> <span class="GAPinput">BindGlobal("xx",17);</span>
#W BIND_GLOBAL: variable `xx' already has a value
<span class="GAPprompt">gap></span> <span class="GAPinput">xx;</span>
17
<span class="GAPprompt">gap></span> <span class="GAPinput">IsReadOnlyGlobal("xx");</span>
true
<span class="GAPprompt">gap></span> <span class="GAPinput">MakeReadWriteGlobal("xx");</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">Unbind(xx);</span>
</pre></div>
<p><a id="X876A6EB68745A510" name="X876A6EB68745A510"></a></p>
<h5>4.9-9 NamesGVars</h5>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ NamesGVars</code>( )</td><td class="tdright">( function )</td></tr></table></div>
<p>This function returns an immutable (see <a href="chap12_mj.html#X7F0C119682196D65"><span class="RefLink">12.6</span></a>) sorted (see <a href="chap21_mj.html#X80ABC25582343910"><span class="RefLink">21.19</span></a>) list of all the global variable names known to the system. This includes names of variables which were bound but have now been unbound and some other names which have never been bound but have become known to the system by various routes.</p>
<p><a id="X7E604AF579A7BC92" name="X7E604AF579A7BC92"></a></p>
<h5>4.9-10 NamesSystemGVars</h5>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ NamesSystemGVars</code>( )</td><td class="tdright">( function )</td></tr></table></div>
<p>This function returns an immutable sorted list of all the global variable names created by the <strong class="pkg">GAP</strong> library when <strong class="pkg">GAP</strong> was started.</p>
<p><a id="X870169447AF490D8" name="X870169447AF490D8"></a></p>
<h5>4.9-11 NamesUserGVars</h5>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ NamesUserGVars</code>( )</td><td class="tdright">( function )</td></tr></table></div>
<p>This function returns an immutable sorted list of the global variable names created since the library was read, to which a value is currently bound.</p>
<p><a id="X7DF8774F7D542298" name="X7DF8774F7D542298"></a></p>
<h4>4.10 <span class="Heading">Namespaces for <strong class="pkg">GAP</strong> packages</span></h4>
<p>As mentioned in Section <a href="chap4_mj.html#X816FBEEA85782EC2"><span class="RefLink">4.9</span></a> above all global variables share a common namespace. This can relatively easily lead to name clashes, in particular when many <strong class="pkg">GAP</strong> packages are loaded at the same time. To give package code a way to have a package local namespace without breaking backward compatibility of the <strong class="pkg">GAP</strong> language, the following simple rule has been devised:</p>
<p>If in package code a global variable that ends with an <q>at</q>-character <code class="code">@</code> is accessed in any way, the name of the package is appended before accessing it. Here, <q>package code</q> refers to everything which is read with <code class="func">ReadPackage</code> (<a href="chap76_mj.html#X870954577B27DCAB"><span class="RefLink">76.3-1</span></a>). As the name of the package the entry <code class="code">PackageName</code> in its <code class="file">PackageInfo.g</code> file is taken. As for all identifiers, this name is case sensitive.</p>
<p>For example, if the following is done in the code of a package with name <code class="code">xYz</code>:</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">a@ := 12;</span>
</pre></div>
<p>Then actually the global variable <code class="code">a@xYz</code> is assigned. Further accesses to <code class="code">a@</code> within the package code will all be redirected to <code class="code">a@xYz</code>. This includes all the functions described in Section <a href="chap4_mj.html#X816FBEEA85782EC2"><span class="RefLink">4.9</span></a> and indeed all the functions described Section <a href="chap79_mj.html#X87E29BA57C8208A4"><span class="RefLink">79.10</span></a> like for example <code class="func">DeclareCategory</code> (<a href="chap13_mj.html#X879DE2A17A6C6E92"><span class="RefLink">13.3-5</span></a>). Note that from code in the same package it is still possible to access the same global variable via <code class="code">a@xYz</code> explicitly.</p>
<p>All other code outside the package as well as interactive user input that wants to refer to that variable <code class="code">a@xYz</code> must do so explicitly by using <code class="code">a@xYz</code>.</p>
<p>Since in earlier releases of <strong class="pkg">GAP</strong> the <q>at</q>-character <code class="code">@</code> was not a legal character (without using backslashes), this small extension of the language does not break any old code.</p>
<p><a id="X815F71EA7BC0EB6F" name="X815F71EA7BC0EB6F"></a></p>
<h4>4.11 <span class="Heading">Function</span></h4>
<p><code class="code">function( [ <var class="Arg">arg-ident</var> {, <var class="Arg">arg-ident</var>} ] )</code></p>
<p><code class="code"> [local <var class="Arg">loc-ident</var> {, <var class="Arg">loc-ident</var>} ; ]</code></p>
<p><code class="code"> <var class="Arg">statements</var></code></p>
<p><code class="code">end</code></p>
<p>A function literal can be assigned to a variable or to a list element or a record component. Later this function can be called as described in <a href="chap4_mj.html#X78C70489791FDF43"><span class="RefLink">4.12</span></a>.</p>
<p>The following is an example of a function definition. It is a function to compute values of the Fibonacci sequence (see <code class="func">Fibonacci</code> (<a href="chap16_mj.html#X85AE1D70803A886C"><span class="RefLink">16.3-1</span></a>)).</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">fib := function ( n )</span>
<span class="GAPprompt">></span> <span class="GAPinput"> local f1, f2, f3, i;</span>
<span class="GAPprompt">></span> <span class="GAPinput"> f1 := 1; f2 := 1;</span>
<span class="GAPprompt">></span> <span class="GAPinput"> for i in [3..n] do</span>
<span class="GAPprompt">></span> <span class="GAPinput"> f3 := f1 + f2;</span>
<span class="GAPprompt">></span> <span class="GAPinput"> f1 := f2;</span>
<span class="GAPprompt">></span> <span class="GAPinput"> f2 := f3;</span>
<span class="GAPprompt">></span> <span class="GAPinput"> od;</span>
<span class="GAPprompt">></span> <span class="GAPinput"> return f2;</span>
<span class="GAPprompt">></span> <span class="GAPinput"> end;;</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">List( [1..10], fib );</span>
[ 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 ]
</pre></div>
<p>Because for each of the formal arguments <var class="Arg">arg-ident</var> and for each of the formal locals <var class="Arg">loc-ident</var> a new variable is allocated when the function is called (see <a href="chap4_mj.html#X78C70489791FDF43"><span class="RefLink">4.12</span></a>), it is possible that a function calls itself. This is usually called <em>recursion</em>. The following is a recursive function that computes values of the Fibonacci sequence.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">fib := function ( n )</span>
<span class="GAPprompt">></span> <span class="GAPinput"> if n < 3 then</span>
<span class="GAPprompt">></span> <span class="GAPinput"> return 1;</span>
<span class="GAPprompt">></span> <span class="GAPinput"> else</span>
<span class="GAPprompt">></span> <span class="GAPinput"> return fib(n-1) + fib(n-2);</span>
<span class="GAPprompt">></span> <span class="GAPinput"> fi;</span>
<span class="GAPprompt">></span> <span class="GAPinput"> end;;</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">List( [1..10], fib );</span>
[ 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 ]
</pre></div>
<p>Note that the recursive version needs <code class="code">2 * fib(<var class="Arg">n</var>)-1</code> steps to compute <code class="code">fib(<var class="Arg">n</var>)</code>, while the iterative version of <code class="code">fib</code> needs only <code class="code"><var class="Arg">n</var>-2</code> steps. Both are not optimal however, the library function <code class="func">Fibonacci</code> (<a href="chap16_mj.html#X85AE1D70803A886C"><span class="RefLink">16.3-1</span></a>) only needs about < | |