SSL chap79.html
Interaktion und PortierbarkeitHTML
|
|
| products/Sources/formale Sprachen/GAP/doc/ref/chap79.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>
< title>GAP (ref) - Chapter 79: Creating New Objects</ 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= "chap79" onload= "jscontent()">
< div class= "chlinktop">< span class= "chlink1">Goto Chapter: </ span>< a href= "chap0.html">Top</ a> < a href="chap1.html">1</a> <a href="chap2.html">2</a> <a href="chap3.html">3</a> <a href="chap4.html">4</a> <a href="chap5.html">5</a> <a href="chap6.html">6</a> <a href="chap7.html">7</a> <a href="chap8.html">8</a> <a href="chap9.html">9</a> <a href="chap10.html">10</a> <a href="chap11.html">11</a> <a href="chap12.html">12</a> <a href="chap13.html">13</a> <a href="chap14.html">14</a> <a href="chap15.html">15</a> <a href="chap16.html">16</a> <a href="chap17.html">17</a> <a href="chap18.html">18</a> <a href="chap19.html">19</a> <a href="chap20.html">20</a> <a href="chap21.html">21</a> <a href="chap22.html">22</a> <a href="chap23.html">23</a> <a href="chap24.html">24</a> <a href="chap25.html">25</a> <a href="chap26.html">26</a> <a href="chap27.html">27</a> <a href="chap28.html">28</a> <a href="chap29.html">29</a> <a href="chap30.html">30</a> <a href="chap31.html">31</a> <a href="chap32.html">32</a> <a href="chap33.html">33</a> <a href="chap34.html">34</a> <a href="chap35.html">35</a> <a href="chap36.html">36</a> <a href="chap37.html">37</a> <a href="chap38.html">38</a> <a href="chap39.html">39</a> <a href="chap40.html">40</a> <a href="chap41.html">41</a> <a href="chap42.html">42</a> <a href="chap43.html">43</a> <a href="chap44.html">44</a> <a href="chap45.html">45</a> <a href="chap46.html">46</a> <a href="chap47.html">47</a> <a href="chap48.html">48</a> <a href="chap49.html">49</a> <a href="chap50.html">50</a> <a href="chap51.html">51</a> <a href="chap52.html">52</a> <a href="chap53.html">53</a> <a href="chap54.html">54</a> <a href="chap55.html">55</a> <a href="chap56.html">56</a> <a href="chap57.html">57</a> <a href="chap58.html">58</a> <a href="chap59.html">59</a> <a href="chap60.html">60</a> <a href="chap61.html">61</a> <a href="chap62.html">62</a> <a href="chap63.html">63</a> <a href="chap64.html">64</a> <a href="chap65.html">65</a> <a href="chap66.html">66</a> <a href="chap67.html">67</a> <a href="chap68.html">68</a> <a href="chap69.html">69</a> <a href="chap70.html">70</a> <a href="chap71.html">71</a> <a href="chap72.html">72</a> <a href="chap73.html">73</a> <a href="chap74.html">74</a> <a href="chap75.html">75</a> <a href="chap76.html">76</a> <a href="chap77.html">77</a> <a href="chap78.html">78</a> <a href="chap79.html">79</a> <a href="chap80.html">80</a> <a href="chap81.html">81</a> <a href="chap82.html">82</a> <a href="chap83.html">83</a> <a href="chap84.html">84</a> <a href="chap85.html">85</a> <a href="chap86.html">86</a> <a href="chap87.html">87</a> <a href="chapBib.html">Bib</a> <a href="chapInd.html">Ind</a> </div>
<div class="chlinkprevnexttop"> <a href="chap0.html">[Top of Book]</a> <a href="chap0.html#contents">[Contents]</a> <a href="chap78.html">[Previous Chapter]</a> <a href="chap80.html">[Next Chapter]</a> </div>
<p id="mathjaxlink" class="pcenter"><a href="chap79_mj.html">[MathJax on]</a></p>
<p><a id="X83548994805AD1C9" name="X83548994805AD1C9"></a></p>
<div class="ChapSects"><a href="chap79.html#X83548994805AD1C9">79 <span class="Heading">Creating New Objects</span></a>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap79.html#X82E86CF37B123FD4">79.1 <span class="Heading">Creating Objects</span></a>
</span>
<div class="ContSSBlock">
<span class="ContSS"><br /><span class="nocss"> </span><a href="chap79.html#X7CB5C12E813F512B">79.1-1 Objectify</a></span>
<span class="ContSS"><br /><span class="nocss"> </span><a href="chap79.html#X85377AC07E775066">79.1-2 ObjectifyWithAttributes</a></span>
</div></div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap79.html#X866E223484649E5A">79.2 <span class="Heading">Component Objects</span></a>
</span>
<div class="ContSSBlock">
<span class="ContSS"><br /><span class="nocss"> </span><a href="chap79.html#X823965BF7DFDACC9">79.2-1 NamesOfComponents</a></span>
</div></div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap79.html#X834893D07FAA6FD2">79.3 <span class="Heading">Positional Objects</span></a>
</span>
</div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap79.html#X82309B3F81DD2237">79.4 <span class="Heading">Implementing New List Objects</span></a>
</span>
</div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap79.html#X849D8BC278649EA5">79.5 <span class="Heading">Example – Constructing Enumerators</span></a>
</span>
</div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap79.html#X7F6BF6CE7AD04EFC">79.6 <span class="Heading">Example – Constructing Iterators</span></a>
</span>
</div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap79.html#X829629E87E30090C">79.7 <span class="Heading">Arithmetic Issues in the Implementation of New Kinds of Lists</span></a>
</span>
</div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap79.html#X7EBB961E7FE1B0EB">79.8 <span class="Heading">External Representation</span></a>
</span>
<div class="ContSSBlock">
<span class="ContSS"><br /><span class="nocss"> </span><a href="chap79.html#X8542B32A8206118C">79.8-1 ExtRepOfObj</a></span>
</div></div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap79.html#X8090219A7C76AF55">79.9 <span class="Heading">Mutability and Copying</span></a>
</span>
</div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap79.html#X87E29BA57C8208A4">79.10 <span class="Heading">Global Variables in the Library</span></a>
</span>
<div class="ContSSBlock">
<span class="ContSS"><br /><span class="nocss"> </span><a href="chap79.html#X828E14ED7EE39522">79.10-1 DeclareGlobalName</a></span>
<span class="ContSS"><br /><span class="nocss"> </span><a href="chap79.html#X8324B5DE8300E0F2">79.10-2 DeclareGlobalVariable</a></span>
<span class="ContSS"><br /><span class="nocss"> </span><a href="chap79.html#X7A23F09886E936D2">79.10-3 InstallValue</a></span>
<span class="ContSS"><br /><span class="nocss"> </span><a href="chap79.html#X87A4316C818B3DE3">79.10-4 FlushCaches</a></span>
<span class="ContSS"><br /><span class="nocss"> </span><a href="chap79.html#X834A8CC587A609BE">79.10-5 DeclareGlobalFunction</a></span>
<span class="ContSS"><br /><span class="nocss"> </span><a href="chap79.html#X851654DA87616207">79.10-6 DeclareSynonym</a></span>
</div></div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap79.html#X7837CA9A83D93B38">79.11 <span class="Heading">Declaration and Implementation Part</span></a>
</span>
</div>
</div>
<h3>79 <span class="Heading">Creating New Objects</span></h3>
<p>This chapter is divided into three parts.</p>
<p>In the first part, it is explained how to create objects with given type (see <a href="chap79.html#X82E86CF37B123FD4"><span class="RefLink">79.1</span></a>).</p>
<p>In the second part, first a few small examples are given, for dealing with the usual cases of component objects (see <a href="chap79.html#X866E223484649E5A"><span class="RefLink">79.2</span></a>) and positional objects (see <a href="chap79.html#X834893D07FAA6FD2"><span class="RefLink">79.3</span></a>), and for the implementation of new kinds of lists (see <a href="chap79.html#X82309B3F81DD2237"><span class="RefLink">79.4</span></a> and <a href="chap79.html#X829629E87E30090C"><span class="RefLink">79.7</span></a>). Finally, the external representation of objects is introduced (see <a href="chap79.html#X7EBB961E7FE1B0EB"><span class="RefLink">79.8</span></a>), as a tool for representation independent access to an object.</p>
<p>The third part deals with some rules concerning the organization of the <strong class="pkg">GAP</strong> library; namely, some commands for creating global variables are explained (see <a href="chap79.html#X87E29BA57C8208A4"><span class="RefLink">79.10</span></a>) that correspond to the ones discussed in the first part of the chapter, and the idea of distinguishing declaration and implementation part of <strong class="pkg">GAP</strong> packages is outlined (see <a href="chap79.html#X7837CA9A83D93B38"><span class="RefLink">79.11</span></a>).</p>
<p>See also Chapter <a href="chap81.html#X8125CC6A87409887"><span class="RefLink">81</span></a> for examples how the functions from the first part are used, and why it is useful to have a declaration part and an implementation part.</p>
<p><a id="X82E86CF37B123FD4" name="X82E86CF37B123FD4"></a></p>
<h4>79.1 <span class="Heading">Creating Objects</span></h4>
<p><a id="X7CB5C12E813F512B" name="X7CB5C12E813F512B"></a></p>
<h5>79.1-1 Objectify</h5>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">an style='color: green'>8227; Objectify</code>( <var class="Arg">type</var>, <var class="Arg">data</var> )</td><td class="tdright">( function )</td></tr></table></div>
<p>New objects are created by <code class="func">Objectify</code>. <var class="Arg">data</var> must be a plain list or a plain record, and <var class="Arg">type</var> is the type that the desired object shall have. <code class="func">Objectify</code> turns <var class="Arg">data</var> into an object with type <var class="Arg">type</var>. That is, <var class="Arg">data</var> is changed, and afterwards it will not be a list or a record unless <var class="Arg">type</var> is of type list resp. record.</p>
<p>If <var class="Arg">data</var> is a list then <code class="func">Objectify</code> turns it into a positional object, if <var class="Arg">data</var> is a record then <code class="func">Objectify</code> turns it into a component object (for examples, see <a href="chap79.html#X866E223484649E5A"><span class="RefLink">79.2</span></a> and <a href="chap79.html#X834893D07FAA6FD2"><span class="RefLink">79.3</span></a>).</p>
<p><code class="func">Objectify</code> does also return the object that it made out of <var class="Arg">data</var>.</p>
<p>For examples where <code class="func">Objectify</code> is used, see <a href="chap79.html#X866E223484649E5A"><span class="RefLink">79.2</span></a>, <a href="chap79.html#X834893D07FAA6FD2"><span class="RefLink">79.3</span></a>, and the example in Chapter <a href="chap81.html#X8125CC6A87409887"><span class="RefLink">81</span></a>.</p>
<p><a id="X85377AC07E775066" name="X85377AC07E775066"></a></p>
<h5>79.1-2 ObjectifyWithAttributes</h5>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">an style='color: green'>8227; ObjectifyWithAttributes</code>( <var class="Arg">obj</var>, <var class="Arg">type</var>, <var class="Arg">attr1</var>, <var class="Arg">val1</var>, <var class="Arg">attr2</var>, <var class="Arg">val2</var>, <var class="Arg">...</var> )</td><td class="tdright">( function )</td></tr></table></div>
<p>Attribute assignments will change the type of an object. If you create many objects, code of the form</p>
<div class="example"><pre>
o:=Objectify(type,rec());
SetMyAttribute(o,value);
</pre></div>
<p>will take a lot of time for type changes. You can avoid this by setting the attributes immediately while the object is created, as follows. <code class="func">ObjectifyWithAttributes</code> takes a plain list or record <var class="Arg">obj</var> and turns it an object just like <code class="func">Objectify</code> (<a href="chap79.html#X7CB5C12E813F512B"><span class="RefLink">79.1-1</span></a>) and sets attribute <var class="Arg">attr1</var> to <var class="Arg">val1</var>, sets attribute <var class="Arg">attr2</var> to <var class="Arg">val2</var> and so forth.</p>
<p>If the filter list of <var class="Arg">type</var> includes that these attributes are set (and the properties also include values of the properties) and if no special setter methods are installed for any of the involved attributes then they are set simultaneously without type changes. This can produce a substantial speedup.</p>
<p>If the conditions of the last sentence are not fulfilled, an ordinary <code class="func">Objectify</code> (<a href="chap79.html#X7CB5C12E813F512B"><span class="RefLink">79.1-1</span></a>) with subsequent setter calls for the attributes is performed instead.</p>
<p><a id="X866E223484649E5A" name="X866E223484649E5A"></a></p>
<h4>79.2 <span class="Heading">Component Objects</span></h4>
<p>A <em>component object</em> is an object in the representation <code class="func">IsComponentObjectRep</code> (<a href="chap13.html#X805F1C3B7C730062"><span class="RefLink">13.4-1</span></a>) or a subrepresentation of it. Such an object <var class="Arg">cobj</var> is built from subobjects that can be accessed via <code class="code"><var class="Arg">cobj</var>!.<var class="Arg">name</var></code>, similar to components of a record. Also analogously to records, values can be assigned to components of <var class="Arg">cobj</var> via <code class="code"><var class="Arg">cobj</var>!.<var class="Arg">name</var>:= <var class="Arg">val</var></code>. For the creation of component objects, see <a href="chap79.html#X82E86CF37B123FD4"><span class="RefLink">79.1</span></a>. One must be <em>very careful</em> when using the <code class="code">!.</code> operator, in order to interpret the component in the right way, and even more careful when using the assignment to components using <code class="code">!.</code>, in order to keep the information stored in <var class="Arg">cobj</var> consistent.</p>
<p>First of all, in the access or assignment to a component as shown above, <var class="Arg">name</var> must be among the admissible component names for the representation of <var class="Arg">cobj</var>, see <code class="func">NewRepresentation</code> (<a href="chap13.html#X7CC8106F809E15CF"><span class="RefLink">13.4-4</span></a>). Second, preferably only few low level functions should use <code class="code">!.</code>, whereas this operator should not occur in <q>user interactions</q>.</p>
<p>Note that even if <var class="Arg">cobj</var> claims that it is immutable, i.e., if <var class="Arg">cobj</var> is not in the category <code class="func">IsMutable</code> (<a href="chap12.html#X7999AD1D7A4F1F46"><span class="RefLink">12.6-2</span></a>), access and assignment via <code class="code">!.</code> and <code class="code">!.:=</code> work. This is necessary for being able to store newly discovered information in immutable objects.</p>
<p>The following example shows the implementation of an iterator (see <a href="chap30.html#X85A3F00985453F95"><span class="RefLink">30.8</span></a>) for the domain of integers, which is represented as component object. See <a href="chap79.html#X834893D07FAA6FD2"><span class="RefLink">79.3</span></a> for an implementation using positional objects. (In practice, such an iterator can be implemented more elegantly using <code class="func">IteratorByFunctions</code> (<a href="chap30.html#X82677D8F817D6701"><span class="RefLink">30.8-8</span></a>), see <a href="chap79.html#X7F6BF6CE7AD04EFC"><span class="RefLink">79.6</span></a>.)</p>
<p>The used succession of integers is <span class="SimpleMath">0, 1, -1, 2, -2, 3, -3, ...</span>, that is, <span class="SimpleMath">a_n = n/2</span> if <span class="SimpleMath">n</span> is even, and <span class="SimpleMath">a_n = (1-n)/2</span> otherwise.</p>
<div class="example"><pre>
DeclareRepresentation( "IsIntegersIteratorCompRep",
IsComponentObjectRep, [ "counter" ] );
</pre></div>
<p>The above command creates a new representation (see <code class="func">NewRepresentation</code> (<a href="chap13.html#X7CC8106F809E15CF"><span class="RefLink">13.4-4</span></a>)) <code class="code">IsIntegersIteratorCompRep</code>, as a subrepresentation of <code class="func">IsComponentObjectRep</code> (<a href="chap13.html#X805F1C3B7C730062"><span class="RefLink">13.4-1</span></a>), and with one admissible component <code class="code">counter</code>. So no other components than <code class="code">counter</code> will be needed.</p>
<div class="example"><pre>
InstallMethod( Iterator,
"method for `Integers'",
[ IsIntegers ],
function( Integers )
return Objectify( NewType( IteratorsFamily,
IsIterator
and IsIntegersIteratorCompRep ),
rec( counter := 0 ) );
end );
</pre></div>
<p>After the above method installation, one can already ask for <code class="code">Iterator( Integers )</code>. Note that exactly the domain of integers is described by the filter <code class="func">IsIntegers</code> (<a href="chap14.html#X818683B17F8C97F3"><span class="RefLink">14.1-2</span></a>).</p>
<p>By the call to <code class="func">NewType</code> (<a href="chap13.html#X7CE39E9478AEC826"><span class="RefLink">13.9-3</span></a>), the returned object lies in the family containing all iterators, which is <code class="code">IteratorsFamily</code>, it lies in the category <code class="func">IsIterator</code> (<a href="chap30.html#X87168A827E5B28E4"><span class="RefLink">30.8-3</span></a>) and in the representation <code class="code">IsIntegersIteratorCompRep</code>; furthermore, it has the component <code class="code">counter</code> with value <code class="code">0</code>.</p>
<p>What is missing now are methods for the two basic operations of iterators, namely <code class="func">IsDoneIterator</code> (<a href="chap30.html#X8055FC557B5D899E"><span class="RefLink">30.8-4</span></a>) and <code class="func">NextIterator</code> (<a href="chap30.html#X879F62F77D1D1179"><span class="RefLink">30.8-5</span></a>). The former must always return <code class="keyw">false</code>, since there are infinitely many integers. The latter must return the next integer in the iteration, and update the information stored in the iterator, that is, increase the value of the component <code class="code">counter</code>.</p>
<div class="example"><pre>
InstallMethod( IsDoneIterator,
"method for iterator of `Integers'",
[ IsIterator and IsIntegersIteratorCompRep ],
ReturnFalse );
InstallMethod( NextIterator,
"method for iterator of `Integers'",
[ IsIntegersIteratorCompRep ],
function( iter )
iter!.counter:= iter!.counter + 1;
if iter!.counter mod 2 = 0 then
return iter!.counter / 2;
else
return ( 1 - iter!.counter ) / 2;
fi;
end );
</pre></div>
<p><a id="X823965BF7DFDACC9" name="X823965BF7DFDACC9"></a></p>
<h5>79.2-1 NamesOfComponents</h5>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">an style='color: green'>8227; NamesOfComponents</code>( <var class="Arg">comobj</var> )</td><td class="tdright">( function )</td></tr></table></div>
<p>For a component object <var class="Arg">comobj</var>, <code class="func">NamesOfComponents</code> returns a list of strings, which are the names of components currently bound in <var class="Arg">comobj</var>.</p>
<p>For a record <var class="Arg">comobj</var> in internal representation, <code class="func">NamesOfComponents</code> returns the result of <code class="func">RecNames</code> (<a href="chap29.html#X837F1E1F866FB1A0"><span class="RefLink">29.1-2</span></a>).</p>
<p><a id="X834893D07FAA6FD2" name="X834893D07FAA6FD2"></a></p>
<h4>79.3 <span class="Heading">Positional Objects</span></h4>
<p>A <em>positional object</em> is an object in the representation <code class="func">IsPositionalObjectRep</code> (<a href="chap13.html#X805F1C3B7C730062"><span class="RefLink">13.4-1</span></a>) or a subrepresentation of it. Such an object <var class="Arg">pobj</var> is built from subobjects that can be accessed via <code class="code"><var class="Arg">pobj</var>![<var class="Arg">pos</var>]</code>, similar to positions in a list. Also analogously to lists, values can be assigned to positions of <var class="Arg">pobj</var> via <code class="code"><var class="Arg">pobj</var>![<var class="Arg">pos</var>]:= <var class="Arg">val</var></code>. For the creation of positional objects, see <a href="chap79.html#X82E86CF37B123FD4"><span class="RefLink">79.1</span></a>.</p>
<p>One must be <em>very careful</em> when using the <code class="code">![]</code> operator, in order to interpret the position in the right way, and even more careful when using the assignment to positions using <code class="code">![]</code>, in order to keep the information stored in <var class="Arg">pobj</var> consistent.</p>
<p>First of all, in the access or assignment to a position as shown above, <var class="Arg">pos</var> must be among the admissible positions for the representation of <var class="Arg">pobj</var>, see <code class="func">NewRepresentation</code> (<a href="chap13.html#X7CC8106F809E15CF"><span class="RefLink">13.4-4</span></a>). Second, preferably only few low level functions should use <code class="code">![]</code>, whereas this operator should not occur in <q>user interactions</q>.</p>
<p>Note that even if <var class="Arg">pobj</var> claims that it is immutable, i.e., if <var class="Arg">pobj</var> is not in the category <code class="func">IsMutable</code> (<a href="chap12.html#X7999AD1D7A4F1F46"><span class="RefLink">12.6-2</span></a>), access and assignment via <code class="code">![]</code> work. This is necessary for being able to store newly discovered information in immutable objects.</p>
<p>The following example shows the implementation of an iterator (see <a href="chap30.html#X85A3F00985453F95"><span class="RefLink">30.8</span></a>) for the domain of integers, which is represented as positional object. See <a href="chap79.html#X866E223484649E5A"><span class="RefLink">79.2</span></a> for an implementation using component objects, and more details.</p>
<div class="example"><pre>
DeclareRepresentation( "IsIntegersIteratorPosRep",
IsPositionalObjectRep, [ 1 ] );
</pre></div>
<p>The above command creates a new representation (see <code class="func">NewRepresentation</code> (<a href="chap13.html#X7CC8106F809E15CF"><span class="RefLink">13.4-4</span></a>)) <code class="code">IsIntegersIteratorPosRep</code>, as a subrepresentation of <code class="func">IsPositionalObjectRep</code> (<a href="chap13.html#X805F1C3B7C730062"><span class="RefLink">13.4-1</span></a>), and with only the first position being admissible for storing data.</p>
<div class="example"><pre>
InstallMethod( Iterator,
"method for `Integers'",
[ IsIntegers ],
function( Integers )
return Objectify( NewType( IteratorsFamily,
IsIterator
and IsIntegersIteratorPosRep ),
[ 0 ] );
end );
</pre></div>
<p>After the above method installation, one can already ask for <code class="code">Iterator( Integers )</code>. Note that exactly the domain of integers is described by the filter <code class="func">IsIntegers</code> (<a href="chap14.html#X818683B17F8C97F3"><span class="RefLink">14.1-2</span></a>).</p>
<p>By the call to <code class="func">NewType</code> (<a href="chap13.html#X7CE39E9478AEC826"><span class="RefLink">13.9-3</span></a>), the returned object lies in the family containing all iterators, which is <code class="code">IteratorsFamily</code>, it lies in the category <code class="func">IsIterator</code> (<a href="chap30.html#X87168A827E5B28E4"><span class="RefLink">30.8-3</span></a>) and in the representation <code class="code">IsIntegersIteratorPosRep</code>; furthermore, the first position has value <code class="code">0</code>.</p>
<p>What is missing now are methods for the two basic operations of iterators, namely <code class="func">IsDoneIterator</code> (<a href="chap30.html#X8055FC557B5D899E"><span class="RefLink">30.8-4</span></a>) and <code class="func">NextIterator</code> (<a href="chap30.html#X879F62F77D1D1179"><span class="RefLink">30.8-5</span></a>). The former must always return <code class="keyw">false</code>, since there are infinitely many integers. The latter must return the next integer in the iteration, and update the information stored in the iterator, that is, increase the value stored in the first position.</p>
<div class="example"><pre>
InstallMethod( IsDoneIterator,
"method for iterator of `Integers'",
[ IsIterator and IsIntegersIteratorPosRep ],
ReturnFalse );
InstallMethod( NextIterator,
"method for iterator of `Integers'",
[ IsIntegersIteratorPosRep ],
function( iter )
iter![1]:= iter![1] + 1;
if iter![1] mod 2 = 0 then
return iter![1] / 2;
else
return ( 1 - iter![1] ) / 2;
fi;
end );
</pre></div>
<p>It should be noted that one can of course install both the methods shown in Section <a href="chap79.html#X866E223484649E5A"><span class="RefLink">79.2</span></a> and <a href="chap79.html#X834893D07FAA6FD2"><span class="RefLink">79.3</span></a>. The call <code class="code">Iterator( Integers )</code> will cause one of the methods to be selected, and for the returned iterator, which will have one of the representations we constructed, the right <code class="func">NextIterator</code> (<a href="chap30.html#X879F62F77D1D1179"><span class="RefLink">30.8-5</span></a>) method will be chosen.</p>
<p><a id="X82309B3F81DD2237" name="X82309B3F81DD2237"></a></p>
<h4>79.4 <span class="Heading">Implementing New List Objects</span></h4>
<p>This section gives some hints for the quite usual situation that one wants to implement new objects that are lists. More precisely, one either wants to deal with lists that have additional features, or one wants that some objects also behave as lists. An example can be found in <a href="chap79.html#X849D8BC278649EA5"><span class="RefLink">79.5</span></a>.</p>
<p>A <em>list</em> in <strong class="pkg">GAP</strong> is an object in the category <code class="func">IsList</code> (<a href="chap21.html#X7C4CC4EA8299701E"><span class="RefLink">21.1-1</span></a>). Basic operations for lists are <code class="func">Length</code> (<a href="chap21.html#X780769238600AFD1"><span class="RefLink">21.17-5</span></a>), <code class="func"><span>\</span>[<span>\</span>]</code> (<a href="chap21.html#X8297BBCD79642BE6"><span class="RefLink">21.2-1</span></a>), and <code class="func">IsBound<span>\</span>[<span>\</span>]</code> (<a href="chap21.html#X8297BBCD79642BE6"><span class="RefLink">21.2-1</span></a>) (see <a href="chap21.html#X7B202D147A5C2884"><span class="RefLink">21.2</span></a>).</p>
<p>Note that the access to the position <var class="Arg">pos</var> in the list <var class="Arg">list</var> via <code class="code"><var class="Arg">list</var>[<var class="Arg">pos</var>]</code> is handled by the call <code class="code">\[\]( <var class="Arg">list</var>, <var class="Arg">pos</var> )</code> to the operation <code class="func"><span>\</span>[<span>\</span>]</code> (<a href="chap21.html#X8297BBCD79642BE6"><span class="RefLink">21.2-1</span></a>). To explain the somewhat strange name <code class="code">\[\]</code> of this operation, note that non-alphanumeric characters like <code class="code">[</code> and <code class="code">]</code> may occur in <strong class="pkg">GAP</strong> variable names only if they are escaped by a <code class="code">\</code> character.</p>
<p>Analogously, the check <code class="code">IsBound( <var class="Arg">list</var>[<var class="Arg">pos</var>] )</code> whether the position <var class="Arg">pos</var> of the list <var class="Arg">list</var> is bound is handled by the call <code class="code">IsBound\[\]( <var class="Arg">list</var>, <var class="Arg">pos</var> )</code> to the operation <code class="func">IsBound<span>\</span>[<span>\</span>]</code> (<a href="chap21.html#X8297BBCD79642BE6"><span class="RefLink">21.2-1</span></a>).</p>
<p>For mutable lists, also assignment to positions and unbinding of positions via the operations <code class="func"><span>\</span>[<span>\</span>]\:\=</code> (<a href="chap21.html#X8297BBCD79642BE6"><span class="RefLink">21.2-1</span></a>) and <code class="func">Unbind<span>\</span>[<span>\</span>]</code> (<a href="chap21.html#X8297BBCD79642BE6"><span class="RefLink">21.2-1</span></a>) are basic operations. The assignment <code class="code"><var class="Arg">list</var>[<var class="Arg">pos</var>]:= <var class="Arg">val</var></code> is handled by the call <code class="code">\[\]\:\=( <var class="Arg">list</var>, <var class="Arg">pos</var>, <var class="Arg">val</var> )</code>, and <code class="code">Unbind( <var class="Arg">list</var>[<var class="Arg">pos</var>] )</code> is handled by the call <code class="code">Unbind\[\]( <var class="Arg">list</var>, <var class="Arg">pos</var> )</code>.</p>
<p>All other operations for lists, e.g., <code class="func">Add</code> (<a href="chap21.html#X795EC9D67E34DAB0"><span class="RefLink">21.4-2</span></a>), <code class="func">Append</code> (<a href="chap21.html#X79E31DB27C82D6E1"><span class="RefLink">21.4-5</span></a>), <code class="func">Sum</code> (<a href="chap21.html#X7A04B71C84CFCC2D"><span class="RefLink">21.20-26</span></a>), are based on these operations. This means that it is sufficient to install methods for the new list objects only for the basic operations.</p>
<p>So if one wants to implement new list objects then one creates them as objects in the category <code class="func">IsList</code> (<a href="chap21.html#X7C4CC4EA8299701E"><span class="RefLink">21.1-1</span></a>), and installs methods for <code class="func">Length</code> (<a href="chap21.html#X780769238600AFD1"><span class="RefLink">21.17-5</span></a>), <code class="func"><span>\</span>[<span>\</span>]</code> (<a href="chap21.html#X8297BBCD79642BE6"><span class="RefLink">21.2-1</span></a>), and <code class="func">IsBound<span>\</span>[<span>\</span>]</code> (<a href="chap21.html#X8297BBCD79642BE6"><span class="RefLink">21.2-1</span></a>). If the new lists shall be mutable, one needs to install also methods for <code class="func"><span>\</span>[<span>\</span>]\:\=</code> (<a href="chap21.html#X8297BBCD79642BE6"><span class="RefLink">21.2-1</span></a>) and <code class="func">Unbind<span>\</span>[<span>\</span>]</code> (<a href="chap21.html#X8297BBCD79642BE6"><span class="RefLink">21.2-1</span></a>).</p>
<p>One application for this is the implementation of <em>enumerators</em> for domains. An enumerator for the domain <span class="SimpleMath">D</span> is a dense list whose entries are in bijection with the elements of <span class="SimpleMath">D</span>. If <span class="SimpleMath">D</span> is large then it is not useful to write down all elements. Instead one can implement such a bijection implicitly. This works also for infinite domains.</p>
<p>In this situation, one implements a new representation of the lists that are already available in <strong class="pkg">GAP</strong>, in particular the family of such a list is the same as the family of the domain <span class="SimpleMath">D</span>.</p>
<p>But it is also possible to implement new kinds of lists that lie in new families, and thus are not equal to lists that were available in <strong class="pkg">GAP</strong> before. An example for this is the implementation of matrices whose multiplication via <q><code class="code">*</code></q> is the Lie product of matrices.</p>
<p>In this situation, it makes no sense to put the new matrices into the same family as the original matrices. Note that the product of two Lie matrices shall be defined but not the product of an ordinary matrix and a Lie matrix. So it is possible to have two lists that have the same entries but that are not equal w.r.t. <q><code class="code">=</code></q> because they lie in different families.</p>
<p><a id="X849D8BC278649EA5" name="X849D8BC278649EA5"></a></p>
<h4>79.5 <span class="Heading">Example – Constructing Enumerators</span></h4>
<p>When dealing with countable sets, a usual task is to define enumerations, i.e., bijections to the positive integers. In <strong class="pkg">GAP</strong>, this can be implemented via <em>enumerators</em> (see <a href="chap21.html#X7EA3ACE27E43D174"><span class="RefLink">21.23</span></a>). These are lists containing the elements in a specified ordering, and the operations <code class="func">Position</code> (<a href="chap21.html#X79975EC6783B4293"><span class="RefLink">21.16-1</span></a>) and list access via <code class="func"><span>\</span>[<span>\</span>]</code> (<a href="chap21.html#X8297BBCD79642BE6"><span class="RefLink">21.2-1</span></a>) define the desired bijection. For implementing such an enumerator, one mainly needs to install the appropriate functions for these operations.</p>
<p>A general setup for creating such lists is given by <code class="func">EnumeratorByFunctions</code> (<a href="chap30.html#X85E149177AC547C3"><span class="RefLink">30.3-4</span></a>).</p>
<p>If the set in question is a domain <var class="Arg">D</var> for which a <code class="func">Size</code> (<a href="chap30.html#X858ADA3B7A684421"><span class="RefLink">30.4-6</span></a>) method is available then all one has to do is to write down the functions for computing the <span class="SimpleMath">n</span>-th element of the list and for computing the position of a given <strong class="pkg">GAP</strong> object in the list, to put them into the components <code class="code">ElementNumber</code> and <code class="code">NumberElement</code> of a record, and to call <code class="func">EnumeratorByFunctions</code> (<a href="chap30.html#X85E149177AC547C3"><span class="RefLink">30.3-4</span></a>) with the domain <var class="Arg">D</var> and this record as arguments. For example, the following lines of code install an <code class="func">Enumerator</code> (<a href="java.lang.StringIndexOutOfBoundsException: Index 30 out of bounds for length 30
<div class="example"><pre>
InstallMethod( Enumerator,
"for integers",
[ IsIntegers ],
Integers -> EnumeratorByFunctions( Integers, rec(
ElementNumber := function( e, n ) ... end,
NumberElement := function( e, x ) ... end ) ) );
</pre></div>
<p>The bodies of the functions have been omitted above; here is the code that is actually used in <strong class="pkg">GAP</strong>. (The ordering coincides with that for the iterators for the domain of rational integers that have been discussed in <a href="chap79.html#X866E223484649E5A"><span class="RefLink">79.2</span></a> and <a href="chap79.html#X834893D07FAA6FD2"><span class="RefLink">79.3</span></a>.)</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">enum:= Enumerator( Integers );</span>
<enumerator of Integers>
<span class="GAPprompt">gap></span> <span class="GAPinput">Print( enum!.NumberElement, "\n" );</span>
function ( e, x )
local pos;
if not IsInt( x ) then
return fail;
elif 0 < x then
pos := 2 * x;
else
pos := -2 * x + 1;
fi;
return pos;
end
<span class="GAPprompt">gap></span> <span class="GAPinput">Print( enum!.ElementNumber, "\n" );</span>
function ( e, n )
if n mod 2 = 0 then
return n / 2;
else
return (1 - n) / 2;
fi;
return;
end
</pre></div>
<p>The situation becomes slightly more complicated if the set <span class="SimpleMath">S</span> in question is not a domain. This is because one must provide also at least a method for computing the length of the list, and because one has to determine the family in which it lies (see <a href="chap79.html#X82E86CF37B123FD4"><span class="RefLink">79.1</span></a>). The latter should usually not be a problem since either <span class="SimpleMath">S</span> is nonempty and all its elements lie in the same family –in this case one takes the collections family of any element in <span class="Sim | |