1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2<html>
3<head>
4<title>Poly Manual</title>
5<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
6</head>
7
8<body>
9<h1 align="center">Poly Manual</h1>
10<h2 align="center"> David C.J. Matthews</h2>
11<p align="left">An original version of this document was published as University 
12  of Cambridge Computer Laboratory Technical Report 63 and appeared in SIGPLAN 
13  Notices Vol 20 No. 9 Sept. 1985.</p>
14
15
16<h2>Chapter 1. Introduction</h2> 
17<p>Poly is a general purpose high-level programming language. It has a simple
18  type system which is also very powerful. Higher order procedures, polymorphic
19  operations, parameterised abstract types and modules are all supported by a
20  single mechanism.</p>
21<p>Poly is <strong>strongly</strong> typed. All objects have a <strong>signature</strong> which
22  the compiler can use to check that operations applied to them are sensible.
23  Type errors cannot cause run time faults.</p>
24<p>The language is <strong>safe</strong>. Any faults that occur at run time will result in
25  exceptions which can be caught and handled by the user. All variables must be
26  initialised before they are used so faults due to undefined variables cannot
27  occur.</p>
28<p>Poly allows <strong>higher</strong> order procedures to be declared and used. A higher
29  order procedure is one which takes a procedure as a parameter or returns a
30  procedure as its result. Since Poly is <strong>statically</strong> scoped (the value
31  bound to an identifier is determined by the static block structure) the
32  procedure that is returned may refer to the arguments and local values
33  belonging to the procedure that returned it, even though that procedure is no
34  longer active.</p>
35<p>Poly allows <strong>polymorphic</strong> operations. For example, in many languages such
36  as Pascal or MODULA-2 programs to sort arrays of integers, arrays of strings
37  or arrays of real numbers are textually almost identical. They differ only in
38  the actual operation used to compare two elements of the array. In Poly one
39  program can be written which will sort arrays of any type provided elements
40  can be compared.</p>
41<p>Poly allows <strong>abstract</strong> types to be created and manipulated. 
42  A&quot;hash table&quot; type can be defined that allows an arbitrary object 
43  to be stored along with a string which acts as a key. There would be a look-up 
44  function that will return the object stored with a given key. These can be written 
45  so that only the functions to create a table, enter a value against a key, and 
46  return the value with the key, are available to the user of the hash table. 
47  This has the advantages that the writer of the hash table function can change 
48  the <strong>way</strong> it is implemented provided it has the same <strong>external</strong> 
49  properties. The user can make use of the hash table knowing that he will not 
50  be able to upset its internal structure by accidentally using a function which 
51  was intended to be private.</p>
52<p>Abstract types can be <strong>parameterised</strong> so that a set of types with similar
53  properties can be defined in a single definition. A specific type can then be
54  made from that. For example, a single hash table type could be declared from
55  which hash tables to hold any particular type could be derived.</p>
56<p>Types in Poly are similar to <strong>modules</strong> in other languages. For example,
57  types can be separately compiled. An abstract type which makes use of other
58  types can be written as though it were polymorphic - it will work if it is
59  given any type (module) which has the required operations. Its operation may
60  be simply to return a new type (module) as its result. This new type may be
61  used directly or as a parameter to other polymorphic abstract types. There is
62  a mechanism for constructing large programs out of their modules and
63  recompiling those which have been modified since they were last compiled.
64</p>
65<p>
66<h2>Chapter 2. The Type System</h2>
67<p>The purpose of a type system is to avoid mistakes due to using a value in a
68  way that was not intended, while making meaningful operations easy to express.
69  For example, if we have two matrices with the same dimensions, it should be
70  just as easy to write the instruction to add them together as if they were
71  integers. However it should not be possible to add an integer to a matrix. A
72  type system could be designed in which all these rules were built into the
73  type checker. This has the problem that new types with new rules cannot be
74  added in. A better way is to have a few simple rules which allow new types to
75  be added and checked but which can be used to catch most of the faults which
76  could be made. The Poly type system is based on this idea.</p>
77<p>All objects have a <strong>signature</strong> which is checked by the type-checker.
78  The signature corresponds to a <strong>type</strong> in other languages, but is more
79  general to take account of the greater power of the type system. For example,
80  in a language like Pascal, a parameter to a procedure may have type
81  <em>integer</em>. This gives enough information for the compiler to check that
82  the procedure is correctly used; it can only be applied to an integer value,
83  but it does not specify precisely which value. It can be applied to 1, 2, 3
84  etc. but not to strings such as "hello" or "goodbye". The checking done
85  by the compiler ensures that certain kinds of faults will not happen when the
86  program is run, but it cannot prevent all faults.</p>
87<p>In Poly this approach is generalised to include procedures, types and exceptions
88  as well
89  as values. The signature of an object contains the information which the
90  compiler uses to check that it is correctly used without restricting it to
91  precisely one object. Expressions and variables can be made to return any
92  kind of object and the signature of the result can be worked out by the
93  compiler, provided of course that all the signatures in the expression
94  match. Specifications have a standard textual form which allows them to be
95  written in a program or output by the compiler. The rules for matching each
96  kind of signature and their textual forms are described below.</p>
97<h3>2.1. Values</h3>
98
99<p>The simplest kind of object is the <strong>value</strong> which can be operated 
100  on but does not do anything itself. Examples are the number 42 or the string 
101  &quot;hello&quot;. A value is said to <strong>belong</strong> to a type or <strong>have</strong> 
102  a particular type, which in Poly is always a named type. The signature is the 
103  name of the type. For example, the signature of the number 42 is <em>integer</em> 
104  and that of &quot;hello&quot; is <em>string</em>. Two values only match if they 
105  belong to the same type. </p>
106<table width="600" border="1">
107  <tr>
108    <td><strong>Syntax</strong></td>
109  </tr>
110  <tr>
111    <td><p>&lt;value signature&gt; ::= &lt;identifier&gt; |&lt;value signature&gt;$&lt;identifier&gt;</p>
112      </td>
113  </tr>
114</table>
115<h3>2.2. Procedures</h3>
116<p><strong>Procedures</strong> are objects which perform a computation. They often 
117  take <strong>parameters</strong> which can be used by the procedure and always 
118  return a <strong>result</strong>. They may also have <strong>side-effects</strong> 
119  or raise <strong>exceptions</strong>. Examples of procedures are "+" which adds 
120  together two values and "<em>print</em>" which prints a value. "+" is an infix 
121  operator which takes two values as parameters, and returns a single result. 
122</p>
123<p>3 + 4</p>
124<p>"<em>print</em>" is a procedure which has the side-effect of printing the value.</p>
125<p><em>print</em>(3+4);</p>
126<p>prints out the value 7. It incidentally also produces a result, but it has 
127  type <em>void</em> which has only one value, and is ignored.</p>
128<p>Procedures can take procedures or types as parameters as well as simple values 
129  and can also return them as results. Procedures match if their parameters and 
130  results all match. The various forms of procedures will be described in the 
131  section on the procedure constructor.</p>
132<table width="600" border="1">
133  <tr> 
134    <td><strong>Syntax</strong></td>
135  </tr>
136  <tr> 
137    <td><table width="600" border="0">
138        <tr> 
139          <td valign="top">&lt;procedure signature&gt;</td>
140          <td valign="top">::=</td>
141          <td><p>proc &lt;mode list&gt; &lt;implied parameters&gt; &lt;actual 
142              parameters&gt; &lt;signature&gt;</p></td>
143        </tr>
144        <tr> 
145          <td valign="top">&lt;mode list&gt;</td>
146          <td valign="top">::=</td>
147          <td>&lt;empty&gt; | &lt;mode&gt; &lt;mode list&gt;</td>
148        </tr>
149        <tr> 
150          <td valign="top">&lt;mode&gt;</td>
151          <td valign="top">::=</td>
152          <td><strong>infix</strong> &lt;digit&gt; | <strong>infixr</strong> &lt;digit&gt; 
153            | <strong>early</strong> | <strong>inline</strong></td>
154        </tr>
155        <tr> 
156          <td valign="top">&lt;implied parameters&gt;</td>
157          <td valign="top">::=</td>
158          <td>[ &lt;parameter list&gt; ] | &lt;empty&gt;</td>
159        </tr>
160        <tr> 
161          <td valign="top">&lt;actual parameters&gt;</td>
162          <td valign="top">::=</td>
163          <td>( &lt;parameter list&gt; )</td>
164        </tr>
165        <tr> 
166          <td valign="top">&lt;parameter list&gt;</td>
167          <td valign="top">::=</td>
168          <td>&lt;empty&gt; | &lt;parameter&gt; |&lt;parameter&gt;; &lt;parameter 
169            list&gt;</td>
170        </tr>
171        <tr> 
172          <td valign="top">&lt;parameter&gt;</td>
173          <td valign="top">::=</td>
174          <td>&lt;identifier list&gt;: &lt;signature&gt; | &lt;signature&gt;</td>
175        </tr>
176        <tr>
177          <td valign="top">&lt;identifier list&gt;</td>
178          <td valign="top">::=</td>
179          <td>&lt;identifier&gt; | &lt;identifier list&gt;, &lt;identifier&gt;</td>
180        </tr>
181      </table>
182    </td>
183  </tr>
184</table>
185
186<h3>2.3. Types</h3>
187Poly has a novel view of <strong>types</strong> in that they are treated as being 
188objects as well as having a role in checking the signature of values. Each type 
189has a set of objects associated with it and a <strong>type mark</strong>. The 
190type mark is used purely by the compiler in checking the signatures of objects 
191and corresponds to the notion of a type in other languages, while the set of objects 
192makes a type in Poly very similar to a module. All types have both a set of objects 
193(which may however be empty) and a type mark, but one or the other may be more 
194important in different circumstances.
195<h4>2.3.1. Sets of Objects</h4>
196<p>As an example of the set of objects, the type <em>integer</em>
197  has various operations such as addition, subtraction, multiplication
198  etc. which can operate on values of the integer type.
199  Any program which works on integers will ultimately be written in terms
200  of these basic operations.
201  Similarly the type <em>real</em>
202  will have these operations along with others which convert between
203  integer and real.</p>
204<p>The signature of a type is the signature of the objects which
205  make it up.
206  Every object in a type has a name, and all the names in one type are
207  different, although objects with the same name frequently exist in
208  different types.
209  So for instance, many types have a <em>print</em>
210  procedure which takes as its parameter a value of the type, and prints it.</p>
211<p>The name of an object in a type is intended to suggest the semantics of the 
212  operation, but there is no guarantee that the + operations in all types are 
213  commutative; in the type <em>string</em> it is used for concatenation. ( This 
214  would require a completely new level of semantic checking which is outside the 
215  scope of a conventional compiler. The CLEAR system [Burstall and Goguen] attempts 
216  this kind of checking.).</p>
217<p>Most of the objects in types are procedures, but a type can contain simple 
218  values as well as other types. For instance there may be a <em>first</em> and 
219  a <em>last</em> value which give the maximum and minimum values. There is a 
220  distinction between objects which are <em>part</em> of the type and those which 
221  been created by operations of it and are said to <em>belong</em> to the type 
222  or <em>have</em> that type. For example, there is no 3 in the type <em>integer</em> 
223  but the number 3 <em>has</em> type <em>integer</em>.</p>
224<p>As types are regarded as sets it is reasonable to be able to take subsets or 
225  select a particular object from a type. For example, </p>
226<p><strong>type</strong> (<em>atype</em>)<em><br>
227  x</em>, <em>y</em>: <em>atype</em>;<br>
228  <em>add</em>: <strong>proc</strong>(<em>atype</em>; <em>atype</em>)<em>atype</em>;<br>
229  <em>sub</em>: <strong>proc</strong>(<em>atype</em>; <em>atype</em>)<em>atype</em><br>
230  <strong>end</strong> </p>
231<p>this is the signature of a type with four objects, called <em>x</em>, <em>y</em>, 
232  <em>add</em> and <em>sub</em>. <em>x</em> and <em>y</em> are both values of 
233  this type, and <em>add</em> and <em>sub</em> are procedures which take a pair 
234  of values, and return a value. The name <em>atype</em> in brackets after the 
235  word <strong>type</strong> is the name used to represent the type itself inside 
236  the type signature. This type will match any of the following</p>
237<p><strong>type</strong> (<em>t</em>) { Only the name has changed }<br>
238  <em>x</em>, <em>y</em>: <em>t</em>;<br>
239  <em>add</em>: <strong>proc</strong>(<em>t</em>; <em>t</em>)<em>t</em>;<br>
240  <em>sub</em>: <strong>proc</strong>(<em>t</em>; <em>t</em>)<em>t</em><br>
241  <strong>end </strong><br>
242</p>
243<p><strong>type</strong>
244  (<em>atype</em>) { The objects are in a different order }<br>
245  <em>sub</em>, <em>add</em>: <strong>proc</strong>(<em>t</em>; <em>t</em>)<em>t</em>;<br>
246  <em>y</em>: <em>t</em>;<br>
247  <em>x</em>: <em>t</em>;<br>
248  <strong>end</strong><br></p>
249<p><strong>type</strong>
250  (<em>at</em>) { A subset }<br>
251  <em>x</em>: <em>at</em>;<br>
252  <em>add</em>: <strong>proc</strong>(<em>at</em>; <em>at</em>)<em>at</em><br>
253  <strong>end</strong><br></p>
254<p><strong>type</strong>
255  (<em>atype</em>) { Another subset }<br>
256  <em>add</em>: <strong>proc</strong>(<em>atype</em>; <em>atype</em>)<em>atype</em><br>
257  <strong>end</strong><br></p>
258<p><strong>type</strong> { Another subset - No need for an internal name }<br>
259  <strong>end</strong></p>
260<p>This last example is the empty type which matches any type. The text in curly 
261  brackets is comment and ignored by the compiler.</p>
262
263<h4>2.3.2 Type Marks and the Specifications of Values</h4>
264
265<p>The function of the <strong>type mark</strong> is in the checking of the
266  signatures of values.
267  Each type has a distinct type mark which is used to identify values which
268  have that type.
269  The signature of a value is simply the type mark of the type to
270  which it belongs.
271  Checking the signatures of two values to see if they match reduces to
272  seeing if they are the same type mark, there is no question of comparing
273  the signatures of the types themselves.</p>
274<p>The reason is that there may be many types with the same signature (short and 
275  long precision integers may have the same set of operations, +, &#8211; etc. 
276  but they are different types). The compiler produces type marks in various circumstances 
277  so as to guarantee that two different types will always have different type 
278  marks. The converse of this is that there are many circumstances in which two 
279  types which are in fact identical may have different type marks, and values 
280  associated with them will be incompatible. An expression which returns a type 
281  always has a type mark which differs from any other, in particular if an existing 
282  type is bound to a new name then they will have different type marks. Values 
283  which have the old type have a different type mark from the new one and so are 
284  incompatible, despite the types being in fact identical.</p>
285
286<h4>2.3.3 Sets and Marks</h4>
287
288<p>There are circumstances when one or other of the two ways of viewing a type 
289  becomes more important. Some types are used only as collections of objects and 
290  there are no values associated with them. The type mark for those types is irrelevant. 
291  Equally there are occasions in which a type is used where the set of objects 
292  is irrelevant. Any type matches the empty type "<strong>type</strong> <strong>end</strong>" 
293  which has no objects in it. The type mark of the matching type is still there 
294  and is used by the compiler.</p>
295<p>This is important because of the matching rules for procedure parameters if 
296  one or more is a type. A procedure which takes a type as a parameter may use 
297  the formal parameter name in the signature of other parameters. For example 
298  a procedure may have signature</p>
299<p><em>aproc</em>: <strong>proc</strong>(<em>atype</em>: <strong>type</strong> 
300  <strong>end</strong>; <em>x</em>: <em>atype</em>)<em>atype</em></p>
301<p>This procedure takes two parameters, a type which may be any type, and a value 
302  which has the same type as the <strong>actual</strong> parameter. Its result 
303  also has this type. This kind of procedure is known as polymorphic. It can therefore 
304  be applied to </p>
305<p><em>aproc</em>(<em>integer</em>, 99)</p>
306<p> in which case the result will have type <em>integer</em> or </p>
307<p><em>aproc</em>(<em>string</em>, &quot;hello&quot;)</p>
308<p>returning a string. This procedure might be the identity function which simply 
309  returns its second parameter (the value) as its result. What is happening is 
310  that the actual type parameter is matched to the formal parameter using the 
311  matching rules for types (the formal parameter must be a subset of the actual 
312  parameter), and then the type mark of the formal parameter is replaced with 
313  the type mark of the actual parameter in the other parameters and the result. 
314  The other parameters therefore match if they have the type mark of the actual 
315  parameter. The signature of the result is obtained by replacing the formal parameter's 
316  type mark by the actual parameter. It is also possible to obtain the type from 
317  the type marks of values, and this is used to remove the need to explicitly 
318  pass type parameters in many cases.</p>
319<p>The reason for considering types both as sets and as marks is that it
320  becomes possible to write polymorphic operations which make use of objects
321  in types.
322  For example a sorting procedure can be written which will work on any
323  values provided they belong to a type whose values can be compared for
324  ordering.
325  How this is done will be described in detail in the section on procedures.</p>
326<table width="600" border="1">
327  <tr> 
328    <td><strong>Syntax</strong></td>
329  </tr>
330  <tr> 
331    <td><table width="600" border="0">
332        <tr> 
333          <td valign="top">&lt;type signature&gt;</td>
334          <td valign="top">::=</td>
335          <td><p><strong>type</strong> &lt;internal&nbsp;name&gt; &lt;signature&nbsp;list&gt; 
336              <strong>end</strong></p></td>
337        </tr>
338        <tr> 
339          <td valign="top">&lt;internal name&gt;</td>
340          <td valign="top">::=</td>
341          <td> &lt;empty&gt; | (&lt;identifier&gt;)</td>
342        </tr>
343        <tr> 
344          <td valign="top">&lt;signature list&gt;</td>
345          <td valign="top">::=</td>
346          <td>&lt;empty&gt; | &lt;object list&gt;</td>
347        </tr>
348        <tr> 
349          <td valign="top">&lt;object list&gt;</td>
350          <td valign="top">::=</td>
351          <td>&lt;object&gt; | &lt;object&gt;; &lt;object&nbsp;list&gt;</td>
352        </tr>
353        <tr> 
354          <td valign="top">&lt;object&gt;</td>
355          <td valign="top">::=</td>
356          <td>&lt;identifier&nbsp;list&gt; : &lt;signature&gt;</td>
357        </tr>
358        <tr> 
359          <td valign="top">&lt;identifier list&gt;</td>
360          <td valign="top">::=</td>
361          <td>&lt;identifier&gt; | &lt;identifier&gt;, &lt;identifier&nbsp;list&gt;</td>
362        </tr>
363      </table></td>
364  </tr>
365</table>
366<p>
367<h3>2.3.4. Exceptions</h3>
368  
369<p>The fourth kind of object in Poly is the exception. The mechanism of exceptions 
370  is based on that of Standard ML.</p>
371<table width="600" border="1">
372  <tr> 
373    <td><strong>Syntax</strong></td>
374  </tr>
375  <tr> 
376    <td><table width="600" border="0">
377        <tr> 
378          <td valign="top">&lt;exception signature&gt;</td>
379          <td valign="top">::=</td>
380          <td><p><strong>exception</strong> &lt;implied&nbsp;parameters&gt; &lt;actual&nbsp;parameters&gt;</p></td>
381        </tr>
382      </table></td>
383  </tr>
384</table>
385
386<h2>Chapter 3. Expressions and Values</h2>
387
388<p>The basic element of a Poly program is the <strong>expression</strong>.
389  An expression has a value and a signature which ensures that the
390  value is correctly used.
391  Expressions consist of identifiers and constructors
392  and operations on them, either procedure applications or selections from
393  types.</p>
394  
395<h3>3.1. Identifiers</h3>
396
397<p>An <strong>identifier</strong> is a name which represents an object. The binding 
398  of a name to a particular object is made by a <strong>declaration</strong>. 
399  An identifier may be any string of alphanumeric characters starting with a letter, 
400  or a string of one or more "special" characters. The underline character "_" 
401  is considered as an alphanumeric. Each of the following are identifiers, separated 
402  by spaces.</p>
403<p><em>a</em> <em>Message</em> <em>integer</em> <em>j</em> + := &gt;&gt;&gt;&gt;&gt;&gt; 
404  <em>L999a</em><br>
405  <em>An_extremely_long_identifier</em></p>
406<p>The "special" characters are often used for infix operators, but can be used 
407  for anything. They are </p>
408<p>! # %&amp; = - + * : &lt; &gt; / \ ? ~ ^ | . @</p>
409<p>Certain words are <strong>reserved</strong> and cannot be used as identifiers 
410  because they are used for special purposes. These are</p>
411
412<table border="0">
413  <tr> 
414    <td width="14%"><strong>and</strong></td>
415    <td width="14%"><strong>begin</strong> </td>
416    <td width="14%"><strong>cand</strong></td>
417    <td width="14%"><strong></strong> <strong>catch</strong> </td>
418    <td width="6"><strong>cor</strong> </td>
419    <td width="14%"><strong>do</strong> </td>
420    <td width="14%"><strong>early</strong></td>
421  </tr>
422  <tr> 
423    <td width="14%"><strong>else</strong> </td>
424    <td width="14%"><strong>end</strong></td>
425    <td width="14%"><strong>exception</strong></td>
426    <td width="14%"><strong>extends</strong></td>
427    <td><strong>if</strong></td>
428    <td width="14%"><strong>infix</strong></td>
429    <td width="14%"><strong>infixr</strong></td>
430  </tr>
431  <tr> 
432    <td width="14%"><strong>inline</strong></td>
433    <td width="14%"><strong>let</strong></td>
434    <td width="14%"><strong>letrec</strong></td>
435    <td width="14%"><strong>proc</strong></td>
436    <td><strong>raise</strong></td>
437    <td width="14%"><strong>record</strong></td>
438    <td width="14%"><strong>struct</strong></td>
439  </tr>
440  <tr>
441    <td width="14%"><strong>then</strong></td>
442    <td width="14%"><strong>type</strong></td>
443    <td width="14%"><strong>union</strong></td>
444    <td width="14%"><strong>while</strong></td>
445    <td><strong>:</strong></td>
446    <td width="14%"><strong>==</strong></td>
447    <td width="14%"><strong>.</strong></td>
448  </tr>
449</table>
450<p>Identifiers written in different cases are regarded as distinct, except that 
451  reserved words may be written in either or mixed case. In this manual reserved 
452  words are always shown in bold font while identifiers are given in italics.</p>
453
454<table width="600" border="1">
455  <tr> 
456    <td><strong>Syntax</strong></td>
457  </tr>
458  <tr> 
459    <td><table width="600" border="0">
460        <tr> 
461          <td valign="top">&lt; identifier&gt;</td>
462          <td valign="top">::=</td>
463          <td><p>&lt;letter&gt; | &lt;identifier&gt;&lt;letter&gt;|&lt;identifier&gt;&lt;digit&gt;</p></td>
464        </tr>
465      </table></td>
466  </tr>
467</table>
468<p>Comments in Poly are written between curly brackets "{" and "}". Any text in 
469  the brackets is completely ignored and the whole comment is simply regarded 
470  as a separator between words in the same way as a space or a new line. </p>
471<h3>3.2. Selectors</h3>
472
473<p>A selector selects an object from a type.</p>
474<p><em>integer</em>$+ </p>
475<p>selects the "+" operation from the type <em>integer</em>, while</p>
476<p><em>string</em>$+ </p>
477<p>selects "+" from <em>string</em>. </p>
478<table width="600" border="1">
479  <tr> 
480    <td><strong>Syntax</strong></td>
481  </tr>
482  <tr> 
483    <td><table width="600" border="0">
484        <tr> 
485          <td valign="top">&lt;selector&gt;</td>
486          <td valign="top">::=</td>
487          <td><p>&lt;identifier&gt;$&lt;identifier&gt;|&lt;selector&gt;$&lt;identifier&gt;</p></td>
488        </tr>
489      </table></td>
490  </tr>
491</table>
492
493<h3>3.3. Constructors</h3>
494<p><strong>Constructors</strong> make values from other values. There are general 
495  constructors for procedures and types as well as three constructors which make 
496  special kinds of types: <strong>records, unions, </strong> and <strong>structures</strong>. 
497  There are also constructors for values which allow literal constants to be entered 
498  in a program.
499<p>1 999 &quot;hello&quot; 'A' 0xff 
500<p>Literal constants are either numbers or strings of characters. Numbers are 
501  strings of digits or letters starting with a digit, and strings are any sequence 
502  of characters unclosed in quotation marks. By default numbers are converted 
503  to type <strong>integer</strong> and strings to either <strong>char</strong> 
504  if they are enclosed in single quotes ('), or <strong>string</strong> if they 
505  are in double quotes (&quot;). </p>
506<h3>3.4. Declarations</h3>
507
508<p>The result of any expression can be bound to an identifier by a declaration. 
509</p>
510<p><strong>let</strong> <em>result</em> == 4+3* 2;</p>
511<p>The identifier <em>result</em> can be used in place of the value that is bound 
512  to it.</p>
513<p>result+6</p>
514<p>will yield the value 16. As well as taking the value from the expression, the 
515  identifier also inherits its signature. The signature of <em>result</em> is 
516  therefore <em>integer</em>. This works for any expression including those which 
517  yield procedures or types. </p>
518<p><strong>let</strong> <em>p</em> == <em>print</em>;</p>
519<p>declares <em>p</em> to be the same as <em>print</em> so </p>
520<p><em>p</em> 10;</p>
521<p>will print the value 10.</p>
522<p>An explicit signature may be given for an identifier.</p>
523<p><strong>let</strong> <em>i</em>: <em>integer</em> == 10;</p>
524<p>The result of the expression must have this signature for the compiler to allow 
525  it. It is useful if a complex expression is being bound to an identifier to 
526  check the signature of the result when it is being bound rather than when it 
527  is used.</p>
528<p>The identifier in an ordinary declaration is declared <strong>after</strong> 
529  the expression is evaluated. </p>
530<p><strong>let</strong> <em>i</em> == <em>i</em>+1</p>
531<p> is valid provided <em>i</em> has been declared before. However when recursive 
532  procedures or types are being declared a different kind of declaration is needed. 
533</p>
534<p><strong>letrec</strong> <em>p</em> == .... </p>
535<p><strong>letrec</strong> introduces a recursive declaration, and the <em>p</em> 
536  used in the expression will be the <em>p</em> that is being declared. Recursive 
537  declarations can only be used for procedures or types and will be described 
538  in more detail below.</p>
539<p>Several declarations can be grouped together with <strong>and</strong>. </p>
540<p><strong>let</strong> <em>a</em> == 1 <strong>and</strong> <em>b</em> == 2</p>
541<p>This declares both <em>a</em> and <em>b</em>. Grouping declarations together 
542  in this way is useful for mutually recursive declarations.</p>
543<p><strong>letrec</strong> <em>p</em> == .... <strong>and</strong> <em>q</em> 
544  == ....</p>
545<table width="600" border="1">
546  <tr> 
547    <td><strong>Syntax</strong></td>
548  </tr>
549  <tr> 
550    <td><table width="600" border="0">
551        <tr> 
552          <td valign="top">&lt;declaration&gt;</td>
553          <td valign="top">::=</td>
554          <td><p><strong>let</strong> &lt;binding&gt; <strong>and</strong> .... 
555              <strong>and</strong> &lt;binding&gt; | <strong>letrec</strong> &lt;binding&gt; 
556              <strong>and</strong> .... <strong>and</strong> &lt;binding&gt;</p></td>
557        </tr>
558        <tr>
559          <td valign="top">&lt;binding&gt;</td>
560          <td valign="top">::=</td>
561          <td>&lt;identifier&gt; : &lt;signature&gt; == &lt;expression&gt; | &lt;identifier&gt; 
562            == &lt;expression&gt;</td>
563        </tr>
564      </table></td>
565  </tr>
566</table>
567
568<h3>3.5. Blocks</h3>
569<p>Commands can be grouped by enclosing them in the bracketing symbols <strong>begin</strong> 
570  and <strong>end</strong> or <strong>(</strong> and <strong>)</strong>. </p>
571<p>2* (3+4);</p>
572<p><strong>begin</strong> <em>print</em> &quot;Hello&quot;; <em>print</em> &quot; 
573  again&quot; <strong>end</strong></p>
574<p>A block can consist of several expressions separated by semicolons or just 
575  one. While <strong>begin</strong> and <strong>end</strong> or round brackets 
576  can be used in either case, it is usual to use <strong>begin</strong> and <strong>end</strong> 
577  to group several expressions together, and round brackets to group parts of 
578  an expression which are to be evaluated first. The value returned by the block 
579  is the value of the last expression. All the other expressions must return values 
580  with type <em>void</em>. Empty blocks are allowed and these return <em>void</em>.</p>
581<p>Declarations may appear in the block as well as expressions. The identifier 
582  is then available in any of the other expressions after its declaration.
583<p><strong>begin</strong> <strong>let</strong> <em>x</em> == 2; <em>x</em> + 3 
584  <strong>end</strong>
585<p>This block returns the value 5. <em>x</em> is not available outside the block, 
586  but it is available in inner blocks.
587<p><strong>begin</strong><br>
588  <strong>let</strong> <em>p</em> == <em>print</em>;<br>
589  <strong>begin</strong><br>
590  <strong>let</strong> <em>ten</em> == 10;<br>
591  <em>p</em> <em>ten</em><br>
592  <strong>end</strong><br>
593  <strong>end</strong>
594<p>An identifier declared in a block "hides" an identifier with the same name 
595  in a outer block.
596
597<table width="600" border="1">
598  <tr> 
599    <td><strong>Syntax</strong></td>
600  </tr>
601  <tr> 
602    <td><table width="600" border="0">
603        <tr> 
604          <td valign="top">&lt;block&gt;</td>
605          <td valign="top">::=</td>
606          <td><p><strong>begin</strong> &lt;expressionlist&gt; catchphrase <strong>end</strong> 
607              | <strong>(</strong> &lt;expressionlist&gt; &lt;catchphrase&gt; 
608              <strong>)<strong> |</strong> ( )</strong> | <strong>begin</strong>&nbsp;<strong>end</strong></p></td>
609        </tr>
610        <tr> 
611          <td valign="top">&lt;expressionlist&gt;</td>
612          <td valign="top">::=</td>
613          <td>&lt;expordec&gt;; ... ; &lt;expordec&gt;</td>
614        </tr>
615        <tr>
616          <td valign="top">&lt;expordec&gt;</td>
617          <td valign="top">::= </td>
618          <td>&lt;expression&gt; | &lt;declaration&gt;</td>
619        </tr>
620      </table></td>
621  </tr>
622</table>
623<h4>3.5.1. Conditionals</h4>
624<p>An expression can return different results according to the value of a test.</p>
625<p><strong>if</strong> 3 &gt; 4 <strong>then</strong> 1 <strong>else</strong> 
626  2; </p>
627<p>The result of the conditional is the expression following <strong>then</strong> 
628  if the condition is <em>true</em> and the expression after <strong>else</strong> 
629  if the expression is <em>false</em>. In this case the result will be 2, since 
630  the condition is clearly false. The expression to be tested must have type <em>boolean</em> 
631  which contains the two values <em>true</em> and <em>false</em>. The two expressions 
632  returned by the then- and else-parts must be the same. The else-part may be 
633  omitted if the then-part returns a <em>void</em> result. </p>
634<p><strong>if</strong> <em>x</em> &gt; 3 <strong>then</strong> <em>print</em> 
635  &quot;yes&quot;</p>
636<p>Conditionals may return procedures or types but the then- and else-parts must 
637  both return values with compatible signatures.</p>
638<p><strong>if</strong> ... <strong>then</strong> <em>integer</em>$<em>pred</em> 
639  <strong>else</strong> <em>integer</em>$<em>succ</em></p>
640<p>The expression returns a procedure which takes an integer parameter and returns 
641  an integer result.</p>
642<table width="600" border="1">
643  <tr> 
644    <td><strong>Syntax</strong></td>
645  </tr>
646  <tr> 
647    <td><table width="600" border="0">
648        <tr> 
649          <td valign="top">&lt;conditional&gt;</td>
650          <td valign="top">::=</td>
651          <td><p><strong>if</strong> &lt;expression&gt; <strong>then</strong> 
652              &lt;expression&gt;<strong>else</strong> &lt;expression&gt; |<br>
653              <strong>if</strong> &lt;expression&gt;<strong>then</strong> &lt;expression&gt;</p></td>
654        </tr>
655      </table></td>
656  </tr>
657</table>
658<p>Related to the if-expression are <strong>cand</strong> and <strong>cor</strong>. 
659  Syntactically they behave like infix operators of precedence -1 and -2 respectively 
660  but they are actually reserved words. </p>
661<p><em>x</em> = 1 <strong>cand</strong> <em>y</em> =2</p>
662<p>is the same as </p>
663<p><strong>if</strong> <em>x</em> = 1 <strong>then</strong> <em>y</em> = 2 <strong>else</strong> 
664  <em>false</em></p>
665<p>and </p>
666<p><em>x</em> = 1 <strong>cor</strong> <em>y</em> =2 </p>
667<p>is the same as</p>
668<p><strong>if</strong> <em>x</em> = 1 <strong>then</strong> <em>true</em> <strong>else</strong> 
669  <em>y</em> = 2</p>
670
671<h4>3.5.2. Repetition</h4>
672<p>An expression can be repeated while some condition holds. </p>
673<p><strong>while</strong> @<em>x</em> &gt; 0 <strong>do</strong> <em>x</em> := 
674  <em>pred</em>(@<em>x</em>) </p>
675<p>decrements <em>x</em> until it is zero, by repeating the second expression 
676  until the first returns <em>false</em>. The expression after the <strong>while</strong> 
677  must have type <em>boolean</em> and the expression after <strong>do</strong> 
678  must have type <em>void</em>. The result of a "while-loop" has type <em>void</em>.</p>
679<p>The "while-expression" is sometimes convenient, but it is usually both clearer 
680  and more efficient to use a recursive procedure. </p>
681<table width="600" border="1">
682  <tr> 
683    <td><strong>Syntax</strong></td>
684  </tr>
685  <tr> 
686    <td><table width="600" border="0">
687        <tr> 
688          <td valign="top">&lt;while loop&gt;</td>
689          <td valign="top">::=</td>
690          <td><p><strong>while</strong> &lt;expression&gt; <strong>do</strong> 
691              &lt;expression&gt;</p></td>
692        </tr>
693      </table></td>
694  </tr>
695</table>
696
697
698<h2>&nbsp;</h2>
699<h2>Chapter 4. Procedures</h2>
700
701<p>A procedure is an encapsulated piece of program which may take some
702  parameters and returns a result.</p>
703  
704<h3>4.1. The Procedure Constructor</h3>
705
706<p>A procedure is made by the <strong>procedure constructor</strong>, called a 
707  lambda expression in some languages, which is a expression preceded by a <strong>procedure 
708  header</strong>. The procedure header gives the names and signatures of the 
709  parameters as they will be used in the expression and the signature of the result. 
710</p>
711<p><strong>proc</strong>(<em>i</em>: <em>integer</em>)<em>integer</em> . <em>i</em> 
712  + 1</p>
713<p>This is a procedure which takes a parameter called <em>i</em> in the block, 
714  which is a value of type integer and it returns a result which is a value of 
715  type integer. The expression returns a result which is one more than the parameter. 
716  This expression is evaluated when the procedure is called and so it is equivalent 
717  to the successor function for integer.</p>
718<p>The procedure constructor is an expression which returns a procedure as its 
719  result. It can be used directly in an expression, but usually it is bound to 
720  an identifier. 
721<p><strong>let</strong> <em>imax</em> == <strong>proc</strong>(<em>i</em>, <em>j</em>: 
722  <em>integer</em>)<em>integer</em> . <strong>if</strong> <em>i</em> &gt; <em>j</em> 
723  <strong>then</strong> <em>i</em> <strong>else</strong> <em>j</em> 
724<p>The identifier is then used in an expression 
725<p><em>imax</em>(<em>1</em>, imax(2, 3))</p>
726<h3>4.2. Recursive Procedures</h3>
727<p>Recursive procedures are declared using <strong>letrec</strong>. 
728<p><strong>letrec</strong> <em>fact</em> ==<strong> proc</strong>(<em>i</em>: 
729  <em>integer</em>)<em>integer</em> . <strong>if</strong> <em>i</em> = 1 <strong>then</strong> 
730  1 <strong>else</strong> <em>fact</em>(<em>i</em>-1) * <em>i</em>
731<p>This has made the usual recursive definition of the factorial function. Recursive 
732  procedures are the preferred way of making loops and repeating expressions in 
733  Poly.</p> 
734<h3>4.3. Operators</h3>
735<p>Procedures can be declared so that they can be used as infix operators. Infix 
736  operators have a <strong>precedence</strong> which determines how tightly they 
737  bind. For example, the expression 
738<p>1* 2+3* 4
739<p>would return 20 if it were evaluated strictly from left to right, but yields 
740  14 if it is evaluated using the normal algebraic rules. In this case the multiplication 
741  operator <em>* </em> is said to have a higher precedence than the addition operator 
742  <em>+</em>. In Poly the precedence of an infix operator is given as a number 
743  between 0 and 9, the higher the number the greater the precedence. 
744<p><strong>let</strong> <em>rem</em> ==<br>
745  <strong>proc</strong> <strong>infix</strong> 7 (<em>i</em>, <em>j</em>: <em>integer</em>)<em>integer</em> 
746  . <em>i</em> - <em>i</em> <em>div</em> <em>j</em> * <em>j</em>
747<p>This declares <em>rem</em> to return the remainder after dividing <em>i</em> 
748  by <em>j</em>. 
749<p>73 <em>rem</em> 4
750<p>In this case <em>rem</em> has been given a precedence of 7, which is the same 
751  as the multiplication and division operators. The precedence of the other operators 
752  is given in the description of the standard definitions. Operators with the 
753  same precedence declared with <em>infix</em> associate to the left. Operators 
754  can be made right associative by using <em>infixr</em>. </p>
755<h3>4.4. Polymorphic Procedures</h3>
756<p>The <em>imax</em> procedure declared above takes integer values and returns 
757  the larger of the two, which is of course also an integer. A similar procedure 
758  can be written to return the greater of two strings (in alphabetical order). 
759<p><strong>let</strong> <em>smax</em> == <strong>proc</strong>(<em>i</em>, <em>j</em>: 
760  <em>string</em>)<em>string</em> . <strong>if</strong> <em>i</em> &gt; <em>j</em> 
761  <strong>then</strong> <em>i</em> <strong>else</strong> <em>j</em>
762<p><em>smax</em> is exactly the same as <em>imax</em> except for the change in 
763  the names of the types. A similar procedure could be written for any type, provided 
764  of course that values of the type can be compared with a <em>&gt;</em> operator. 
765  In Poly a single procedure can be written to handle all these cases, such a 
766  procedure is called <strong>polymorphic</strong>. 
767<p><strong>let</strong> <em>pmax</em> == <strong>proc</strong>(<em>a_type</em>: 
768  <strong>type</strong>(<em>t</em>) &gt; : <strong>proc</strong>(<em>t</em>;<em>t</em>)<em>boolean</em> 
769  <strong>end</strong>; <em>i</em>, <em>j</em>: <em>a_type</em>)<em>a_type</em> 
770  .<strong>if</strong> <em>i</em> &gt; <em>j</em> <strong>then</strong> <em>i</em> 
771  <strong>else</strong> <em>j</em> 
772<p>It works by requiring an extra parameter, <em>a_type</em>, which is the type 
773  of the values (recall that types can be passed as parameters to procedures). 
774  The important thing about this type is that it must have a <em>&gt;</em> operator 
775  which compares two values of the type and returns a boolean value. The type 
776  signature
777<p><strong>type</strong>(<em>t</em>) &gt; : <strong>proc</strong>(<em>t</em>; 
778  <em>t</em>)<em>boolean</em> <strong>end</strong>
779<p>expresses this constraint. The other two parameters and the result must be 
780  of this type. <em>pmax</em> can therefore be applied to any type which satisfies 
781  the constraint, and a pair of values of the type.
782<p><em>pmax</em>(<em>integer</em>, 1, 2)
783<p>returns an integer result, while 
784<p><em>pmax</em>(<em>string</em>, &quot;abc&quot;, &quot;abd&quot;)
785<p>will return a string. However 
786<p><em>pmax</em>(<em>integer</em>, 1, &quot;abc&quot;)<br>
787  <em>pmax</em>(<em>string</em>, 3, 4)
788<p>will be rejected by the compiler because the signatures do not match. 
789<p>max(boolean, true, false)
790<p>will also fail, because <em>boolean</em> does not possess a <em>&gt;</em> operator. </p>
791<h3>4.5. Implied Parameters</h3>
792<p>It is not very convenient to have to write an extra parameter when calling 
793  polymorphic procedures, especially since it is just the type of the other parameters. 
794  Poly allows a polymorphic procedure to be written so that the type parameters 
795  need not be given explicitly but are passed implicitly. </p>
796<p><strong>let</strong> <em>max</em> ==<br>
797  <strong>proc</strong><strong>[</strong><em>a_type</em>: <strong>type</strong> 
798  (<em>t</em>) &gt; : <strong>proc</strong>(<em>t</em>;<em>t</em>)<em>boolean</em> 
799  <strong>end</strong><strong>]</strong>(<em>i</em>, <em>j</em>: <em>a_type</em>)<em>a_type</em> 
800  . <strong>if</strong> <em>i</em> &gt; <em>j</em> <strong>then</strong> <em>i</em> 
801  <strong>else</strong> <em>j</em><br>
802</p>
803<p>The type parameter in this case is enclosed in square brackets to indicate 
804  that there will not be a corresponding actual parameter. </p>
805<p><em>max</em>(3,4)</p>
806<p>looks at the actual parameters, finds that they are integers and so passes 
807  the type <em>integer</em> implicitly.</p>
808<p><em>max</em>(&quot;abc&quot;, &quot;bcd&quot;)</p>
809<p>passes the type <em>string</em>.</p>
810<p><em>max</em>(3, &quot;abc&quot;)</p>
811<p>is incorrect because the parameters must have the same type.</p>
812<p>Implied parameters are a very powerful facility. All the operators such as 
813  <em>+</em> and <em>&gt;</em> are polymorphic procedures which take the type 
814  of their explicit parameters as an implied parameter. Their only action is to 
815  select and apply the appropriate procedure from the type (This does not mean 
816  that adding two integers together requires two procedure calls. These operations 
817  are implemented as inline procedures and the compiler optimises it down to a 
818  single "add" instruction.) For example, the definition of <em>+</em> in the 
819  standard header is </p>
820<p><strong>let</strong> + { addition } == <strong>proc</strong> <strong>early</strong> 
821  <strong>inline</strong> <strong>infix</strong> 6 <strong>[</strong><em>inttype</em> 
822  : <strong>type</strong> (<em>t</em>) + : <strong>proc</strong> (<em>t</em>; 
823  <em>t</em>)<em>t</em> <strong>end</strong><strong>]</strong> (<em>x</em>, <em>y</em> 
824  : <em>inttype</em>) <em>inttype</em>} . <em>x</em> <em>inttype</em>$+ <em>y</em></p>
825<p>The words <strong>early</strong> and <strong>inline</strong> are directives 
826  to the compiler. <strong>early</strong> tells the compiler that this procedure 
827  should be evaluated as soon as possible. This usually means that the compiler 
828  will attempt to execute it when it is compiled if its parameters are constants 
829  (Since procedures can have side-effects the compiler must not attempt to evaluate 
830  all procedures at compile-time. Consider, for example, a procedure which returns 
831  the current date and time). <strong>inline</strong> tells the compiler to insert 
832  the code for this procedure at the point of call rather than generate a procedure 
833  call. Both <strong>early</strong> and <strong>inline</strong> are hints to the 
834  compiler rather than instructions, and the compiler may choose to ignore either 
835  or both. \syntax{&lt;procedure signature&gt; . &lt;expression&gt;} { &lt;procedure 
836  constructor&gt; ::=&lt;procedure signature&gt; . &lt;expression&gt; } </p>
837<table width="600" border="1">
838  <tr> 
839    <td><strong>Syntax</strong></td>
840  </tr>
841  <tr> 
842    <td><table width="600" border="0">
843        <tr> 
844          <td valign="top">&lt;procedure constructor&gt;</td>
845          <td valign="top">::=</td>
846          <td><p>&lt;procedure signature&gt; . &lt;expression&gt;</p></td>
847        </tr>
848      </table></td>
849  </tr>
850</table>
851<h2>&nbsp;</h2>
852<h2>Chapter 5. Exceptions</h2>
853<p>Normally expressions in a block are executed one after another until the end 
854  of the block is reached. There are occasions, however, when an unusual case 
855  occurs and an escape is needed. </p>
856<p><strong>let</strong> <em>p</em> == <em>q</em> <em>div</em> <em>r</em></p>
857<p>For example, a program containing a divide operation could possibly fail if 
858  <em>r</em> were zero. Explicitly checking for zero before making the division 
859  would be tedious and unnecessary in most cases, so what happens is that an <strong>exception</strong> 
860  is generated. Exceptions are error conditions together with a string which identifies 
861  the cause of the failure. Dividing by zero, for example, results in an exception 
862  with the string <em>divideerror</em>. An exception can also be generated by 
863  using <strong>raise</strong>. </p>
864<p><strong>raise</strong> <em>an_error</em> </p>
865<p>generates an exception with the name <em>an_error</em>.</p>
866<p>Exceptions generated in a block may be caught by a <strong>handler</strong>. 
867  A handler is given control when any exception in the block happens and either 
868  returns a result or raises another exception. The handler is a procedure whose 
869  parameter is a string, the exception name, and result must be compatible with 
870  the result the block would return if the exception had not happened.</p>
871<p><strong>begin</strong><br>
872  <em>i</em> <em>div</em> <em>j</em><br>
873  <strong>catch</strong> <strong>proc</strong> (<em>name</em>: <em>string</em>)<em>integer</em><br>
874  <strong>begin</strong><br>
875  <em>print</em>(&quot;Exception-&quot;);<br>
876  <em>print</em>(<em>name</em>);<br>
877  9999<br>
878  <strong>end</strong><br>
879  <strong>end</strong></p>
880<p>This block returns the result of dividing <em>i</em> by <em>j</em> unless an 
881  exception occurs. In that case it prints out <em>Exception-</em> followed by 
882  the name of the exception, and returns the (large) value 9999.</p>
883<p>The handling procedure can be any which has the correct signature, but frequently 
884  it is written as a procedure constructor after the word <strong>catch</strong> 
885  . Any exceptions raised by the handler are, of course, not caught by it, but 
886  appear in the next block out. In addition, if the block contains declarations 
887  they are not available to the handler. This is because an exception could occur 
888  while the declarations were being made so the identifier would have no value.</p>
889<p><strong>begin</strong><br>
890  <strong>let</strong> <em>val</em> == <em>i</em> <em>div</em> <em>j</em>;<br>
891  <strong>let</strong> <em>otherval</em> == <em>i</em>+1;<br>
892  <strong>catch</strong> <strong>proc</strong> (<em>name</em>: <em>string</em>)...<br>
893  <strong>begin</strong> { Cannot use val or otherval here }<br>
894  <strong>end</strong><br>
895  <strong>end</strong></p>
896<p>If an exception is not caught in a block it automatically propagates to the 
897  containing block. This in turn can handle it, or allow it to propagate to the 
898  next block out. An exception raised in a procedure but not caught in it causes 
899  the procedure to return and the exception appears at the point where the procedure 
900  was called. The calling block will catch the exception if it has a handler or 
901  it will propagate back further.</p>
902<table width="600" border="1">
903  <tr> 
904    <td><strong>Syntax</strong></td>
905  </tr>
906  <tr> 
907    <td><table width="600" border="0">
908        <tr> 
909          <td valign="top">&lt;raise expression&gt;</td>
910          <td valign="top">::=</td>
911          <td><p><strong>raise</strong> &lt;identifier&gt;</p></td>
912        </tr>
913        <tr>
914          <td valign="top">&lt;catch phrase&gt;</td>
915          <td valign="top">::=</td>
916          <td><strong>catch</strong> &lt;expression&gt;</td>
917        </tr>
918      </table></td>
919  </tr>
920</table>
921<p>&nbsp;</p>
922<h2>Chapter 6. Specialised Type Constructors</h2>
923<p>There are three specialised constructors which make different kinds of types. 
924  They are normally used to provide the &quot;concrete&quot; type which implements 
925  an abstract type. The development of abstract types will be described in the 
926  next chapter.</p>
927<h3>6.1. Records</h3>
928<p>A <strong>record type</strong> allows objects composed of a group of values 
929  to be put together and taken apart.</p>
930<p> <strong>let</strong> <em>int_pair</em> == <strong>record</strong> (<em>first</em>, 
931  <em>second</em>: <em>integer</em>) } makes a type with the operations for making 
932  pairs of integers. The names <em>first</em> and <em>second</em> are known as 
933  <strong>field names</strong> and the signature, in this case <em>integer</em> 
934  is the <strong>field signature</strong>. The result of this declaration is a 
935  type <em>int_pair</em> has three operations in it, <em>constr</em>, <em>first</em> 
936  and <em>second</em>. </p>
937<p>Every record has a <em>constr</em> procedure which takes objects with the field 
938  signatures and makes them into a record value. The <em>constr</em> in <em>int_pair</em> 
939  takes two integer values and packages them up as a value of the <em>int_pair</em> 
940  type. </p>
941<p><strong>let</strong> <em>pair_val</em> == <em>int_pair</em>$<em>constr</em>(1, 
942  2); </p>
943<p>The field names <em>first</em> and <em>second</em> are procedures called <strong>selectors</strong> 
944  that take apart values of the <em>int_pair</em> type and return the first and 
945  second values respectively. </p>
946<p><em>int_pair</em>$<em>first</em>(<em>pair_val</em>)</p>
947<p>will return 1 and</p>
948<p><em>int_pair</em>$<em>second</em>(<em>pair_val</em>)</p>
949<p>will return 2. Records can be made with elements of any signature and any number 
950  of elements.</p>
951<p><strong>let</strong> <em>prec</em> == <strong>record</strong> (<em>val</em>: 
952  <em>integer</em>; <em>pr</em>: <strong>proc</strong> (<em>integer</em>)<em>integer</em>); 
953</p>
954<p>makes a record to hold a value and a procedure. A value of this type can be 
955  made by </p>
956<p><strong>let</strong> <em>prec_val</em> == <em>prec</em>$<em>constr</em>(1, 
957  <em>integer</em>$<em>succ</em>)</p>
958<p>and the selecting functions <em>val</em> and <em>pr</em> now return an integer 
959  value and a procedure respectively.</p>
960<p><em>prec</em>$<em>pr</em>(<em>prec_val</em>)(99) + <em>prec</em>$<em>val</em>(<em>prec_val</em>)</p>
961<p>Note, however that each invocation of the record constructor, as with the other 
962  constructors, yields a type with a new unique type mark. This means that two 
963  record types with identical field names and signatures are regarded as different 
964  types. A more convenient syntax for selection is available which allows</p>
965<p><em>pair_val</em>.<em>first</em> <em>pair_val</em>.<em>second</em></p>
966<p> to be used with exactly the same meaning as </p>
967<p><em>int_pair</em>$<em>first</em>(<em>pair_val</em>) <em>int_pair</em>$<em>second</em>(<em>pair_val</em>)</p>
968<p>This syntax is not restricted to record selection but can be used with any 
969  procedure that is an object in a type and takes one argument of that type. The 
970  meaning of <em>X.Y</em> is</p>
971<p><em>X_type</em>$<em>Y</em>(<em>X</em>)</p>
972<p>where <em>X_type</em> is the type to which <em>X</em> belongs. So for example, 
973</p>
974<p>99.<em>succ</em>.<em>print</em></p>
975<p> is equivalent to </p>
976<p><em>integer</em>$<em>print</em>(<em>integer</em>$<em>succ</em>(99))</p>
977<table width="600" border="1">
978  <tr> 
979    <td><strong>Syntax</strong></td>
980  </tr>
981  <tr> 
982    <td><table width="600" border="0">
983        <tr> 
984          <td valign="top">&lt;record constructor&gt;</td>
985          <td valign="top">::=</td>
986          <td><p><strong>record ( </strong>&lt;field list&gt; <strong>)</strong></p></td>
987        </tr>
988        <tr> 
989          <td valign="top">&lt;field list&gt;</td>
990          <td valign="top">::=</td>
991          <td>&lt;field&gt; | &lt;field&gt;<strong>;</strong> &lt;field list&gt;</td>
992        </tr>
993        <tr> 
994          <td valign="top">&lt;field&gt;</td>
995          <td valign="top">::=</td>
996          <td>&lt;identifier list&gt;<strong> :</strong> &lt;signature&gt;</td>
997        </tr>
998        <tr>
999          <td valign="top">&lt;identifier list&gt;</td>
1000          <td valign="top">::=</td>
1001          <td>&lt;identifier&gt; | &lt;identifier&gt; <strong>, </strong>&lt;identifier 
1002            list&gt; </td>
1003        </tr>
1004      </table></td>
1005  </tr>
1006</table>
1007
1008<h3> 6.2. Unions</h3>
1009<p>The record constructor makes types whose values are groups of objects. Another 
1010  constructor, the <strong>union</strong> constructor, makes types whose values 
1011  may have any of a set of signatures.</p>
1012<p><strong>let</strong> <em>int_or_str</em> == <strong>union</strong> (<em>int</em>: 
1013  <em>integer</em>; <em>str</em>: <em>string</em>)</p>
1014<p>This has created a type whose values can be either integers or strings. The 
1015  names before the colons (<em>int</em> and <em>str</em>) are called <strong>tags</strong> 
1016  and a tag and its signature (after the colon) is called a <strong>variant</strong>. 
1017  An integer or string may be converted into this type by <strong>injection</strong> 
1018  operations. </p>
1019<p><strong>let</strong> <em>int_form</em> == <em>int_or_str</em>$<em>inj_int</em>(99)<br>
1020  <strong>let</strong> <em>str_form</em> == <em>int_or_str</em>$<em>inj_str</em>("hello")</p>
1021<p>The names of the injection operations are made by prepending the word <em>inj_</em> 
1022  to the tags. The original string and integer values can be obtained by <strong>projecting</strong> 
1023  the union value.</p>
1024<p><em>int_or_str</em>$<em>proj_int</em>(<em>int_form</em>)<br>
1025  <em>int_or_str</em>$<em>proj_str</em>(<em>str_form</em>)</p>
1026<p>The names of these operations are made in a similar way to the injection operations 
1027  and return a value with the appropriate signature. Of course it is possible 
1028  to apply the wrong projection. </p>
1029<p><em>int_or_str</em>$<em>proj_str</em>(<em>int_form</em>)</p>
1030<p>Since <em>int_form</em> contains an integer it cannot be made to return a string, 
1031  and so this operation will cause an exception with the name <em>projecterror</em>. 
1032  To avoid getting exceptions, the union value can be tested to see if it is a 
1033  particular variant. </p>
1034<p><strong>if</strong> <em>int_or_str</em>$<em>is_str</em>(<em>int_form</em>) 
1035  <strong>then</strong> ... </p>
1036<p>will not execute the expression after <strong>then</strong> because the test 
1037  will return false. However</p>
1038<p><em>int_or_str</em>$<em>is_int</em>(<em>int_form</em>)</p>
1039<p>will return true. The alternative syntax for fields of records can be used 
1040  when projecting or testing unions.</p>
1041<p><em>int_form</em>.<em>proj_int</em><em><br>
1042  str_form</em>.<em>proj_str</em><em><br>
1043  int_form</em>.<em>is_int</em></p>
1044<p>As with records the variants may be procedures or types as well as values and 
1045  it is possible to have two variants with the same signature.</p>
1046<p><strong>let</strong> <em>a_union</em> == <strong>union</strong> (<em>one</em>, 
1047  <em>another</em>: <em>integer</em>; <em>p</em>: <strong>proc</strong> (<em>integer</em>)<em>integer</em>)</p>
1048<p>The two variants <em>one</em> and <em>another</em> are different, so</p>
1049<p><em>a_union</em>$<em>proj_one</em>(<em>a_union</em>$<em>inj_another</em>(99))</p>
1050<p>will result in an exception. </p>
1051<table width="600" border="1">
1052  <tr> 
1053    <td><strong>Syntax</strong></td>
1054  </tr>
1055  <tr> 
1056    <td><table width="600" border="0">
1057        <tr> 
1058          <td valign="top">&lt;union constructor&gt;</td>
1059          <td valign="top">::=</td>
1060          <td><p><strong>union ( </strong>&lt;field list&gt; <strong>)</strong></p></td>
1061        </tr>
1062      </table></td>
1063  </tr>
1064</table>
1065<h3>6.3. Structures</h3>
1066<p> The third built-in type constructor makes <strong>structure</strong> types. 
1067  Structures are very similar to records in that their values are composed of 
1068  groups of objects. The difference is that there is an additional value <em>nil</em> 
1069  in the type and there are operations to compare structure values. Structures 
1070  are mostly used in recursive declarations to create lists and trees. In fact 
1071  most structures can be represented using record and union constructors but they 
1072  are useful enough to be provided separately. </p>
1073<p><strong>letrec</strong> <em>int_list</em> == <strong>struct</strong> (<em>hd</em>: 
1074  <em>integer</em>; <em>tl</em>: <em>int_list</em>)</p>
1075<p>This has created a type which can construct lists of integers. It has five 
1076  operations together with the the <em>nil</em> value. <em>constr</em> can be 
1077  used to make <em>int_list</em> values. </p>
1078<p><strong>let</strong> <em>a_list</em> == <em>int_list</em>$<em>constr</em>(1, 
1079  <em>int_list</em>$<em>constr</em>(2, <em>int_list</em>$<em>nil</em>))</p>
1080<p>The <em>nil</em> value is used to end the list. Without it there would be no 
1081  way to construct a structure since a value of the type is needed before a node 
1082  can be made. The selector procedures, <em>hd</em> and <em>tl</em> are used to 
1083  select the parts of the structure in the same way as for a record.</p>
1084<p><em>int_list</em>$<em>hd</em>(<em>a_list</em>) <em>int_list</em>$<em>hd</em>(<em>int_list</em>$<em>tl</em>(<em>a_list</em>))</p>
1085<p>If a selector is applied to nil an exception <em>nilreference</em> is raised, 
1086  since there is no value that can be returned. There are two operations = and 
1087  <em>&lt;&gt;</em> which compare two structure values. = only returns <em>true</em> 
1088  if they the structures are <strong>identical</strong>, that is they were made 
1089  with the same call of <em>constr</em> or they are both <em>nil</em>. </p>
1090<p><strong>let</strong> <em>b_list</em> == <em>int_list</em>$<em>constr</em>(2, 
1091  <em>int_list</em>$<em>nil</em>)</p>
1092<p>has made a list with the same <em>hd</em> and <em>tl</em> values as the tail 
1093  of <em>a_list</em> but</p>
1094<p><em>b_list</em> = <em>a_list</em>.<em>tl</em></p>
1095<p>will return <em>false</em>. </p>
1096<table width="600" border="1">
1097  <tr> 
1098    <td><strong>Syntax</strong></td>
1099  </tr>
1100  <tr> 
1101    <td><table width="600" border="0">
1102        <tr> 
1103          <td valign="top">&lt;structure constructor&gt;</td>
1104          <td valign="top">::=</td>
1105          <td><p><strong>struct ( </strong>&lt;field list&gt; <strong>)</strong></p></td>
1106        </tr>
1107      </table></td>
1108  </tr>
1109</table>
1110<p>&nbsp;</p>
1111<h2>Chapter 7. Type Constructor</h2>
1112<p>As well as the using the constructors described above it is possible to make 
1113  a type by extending an existing one. This usually involves adding new procedures 
1114  or replacing existing ones. </p>
1115<p><strong>let</strong> <em>new_int</em> ==<br>
1116  <strong>type</strong> (<em>int</em>) <strong>extends</strong> <em>integer</em>;<br>
1117  <strong>let</strong> <em>sqr</em> == <strong>proc</strong> (<em>i</em>: <em>int</em>)<em>int</em> 
1118  . <em>i</em>*<em>i</em><br>
1119  <strong>end</strong></p>
1120<p> This declares <em>new_int</em> to be a type which is an <strong>extension</strong> 
1121  of the existing type <em>integer</em>. The name in brackets, <em>int</em>, is 
1122  used inside the constructor to represent the type being made. For instance the 
1123  parameter and result of <em>sqr</em> both have type <em>int</em>. The result 
1124  of this constructor is a type which has all the operations which <em>integer</em> 
1125  had, but in addition it has a <em>sqr</em> procedure which returns the square 
1126  of its parameter. This new type is different from <em>integer</em> so it cannot 
1127  be used directly on values with the integer type. The conversion operation <em>up</em> 
1128  must be used to make an <em>integer</em> value into a <em>new_int</em> one. 
1129</p>
1130<p><em>new_int</em>$<em>sqr</em>(<em>new_int</em>$<em>up</em>(99))</p>
1131<p>There is a similar operation <em>down</em> which will convert values in the 
1132  opposite direction </p>
1133<p>10 + <em>new_int</em>$<em>down</em>(<em>new_int</em>$<em>sqr</em>(<em>new_int</em>$<em>up</em>(11)))</p>
1134<p>These two operations are added to the new type when an old type is being extended 
1135  to allow conversion of values from the old to the new types. They can be redefined 
1136  if necessary or, as we shall see, "hidden" so that conversion of values is not 
1137  possible.</p>
1138<p>The above example shows how a new type can be made which is slightly
1139  different from an existing one.</p>
1140  
1141<h3>7.1. A New Type</h3>
1142
1143<p>The same approach is used to construct a completely new type. We have already 
1144  seen that a record can be used to make a pair of integers and this pair can 
1145  be extended to make a double precision integer type. Suppose that the maximum 
1146  range of numbers which could be held in a single integer was from -9999 to 9999. 
1147  Then a double-precision number could be defined by representing it as a record 
1148  with two fields, a high and low order part, and the actual number would have 
1149  value (high)*10000 + (low). This can be implemented as follows.</p>
1150<p><strong>let</strong> <em>dp</em> ==<br>
1151  <strong>type</strong> (<em>d</em>) <strong>extends</strong> <strong>record</strong> 
1152  (<em>hi</em>, <em>lo</em>: <em>integer</em>);<br>
1153  <strong>let</strong> <em>succ</em> ==<br>
1154  <strong>proc</strong> (<em>x</em>:<em>d</em>)<em>d</em><br>
1155  <strong>begin</strong><br>
1156  <strong>if</strong> <em>x</em>.<em>lo</em> = 9999<br>
1157  <strong>then</strong> <em>d</em>$<em>constr</em>(<em>succ</em>(<em>x</em>.<em>hi</em>), 
1158  0)<br>
1159  <strong>else</strong> <strong>if</strong> <em>x</em>.<em>hi</em> &lt; 0&amp; 
1160  <em>x</em>.<em>lo</em> = 0<br>
1161  <strong>then</strong> <em>d</em>$<em>constr</em>(<em>succ</em>(<em>x</em>.<em>hi</em>), 
1162  ~9999)<br>
1163  <strong>else</strong> <em>d</em>$<em>constr</em>(<em>x</em>.<em>hi</em>, <em>succ</em>(<em>x</em>.<em>lo</em>))<br>
1164  <strong>end</strong>;<br>
1165  <strong>let</strong> <em>pred</em> ==<br>
1166  <strong>proc</strong> (<em>x</em>:<em>d</em>)<em>d</em><br>
1167  <strong>begin</strong><br>
1168  <strong>if</strong> <em>x</em>.<em>lo</em> = ~9999<br>
1169  <strong>then</strong> <em>d</em>$ <em>constr</em>(<em>pred</em>(<em>x</em>.<em>hi</em>), 
1170  0)<br>
1171  <strong>else</strong> <strong>if</strong> <em>x</em>.<em>hi</em> &gt; 0 &amp; 
1172  <em>x</em>.<em>lo</em> = 0<br>
1173  <strong>then</strong> <em>d</em>$<em>constr</em>(<em>pred</em>(<em>x</em>.<em>hi</em>), 
1174  9999)<br>
1175  <strong>else</strong> <em>d</em>$<em>constr</em>(<em>x</em>.<em>hi</em>, <em>pred</em>(<em>x</em>.<em>lo</em>))<br>
1176  <strong>end</strong>;<br>
1177  <strong>let</strong> <em>zero</em> == <em>d</em>$ <em>constr</em>(0,0);<br>
1178  <strong>let</strong> <em>iszero</em> == <strong>proc</strong> (<em>x</em>:<em>d</em>) 
1179  <em>boolean</em> . <em>x</em>.<em>hi</em> = 0 &amp; <em>x</em>.<em>lo</em> = 
1180  0<br>
1181  <strong>end</strong>; </p>
1182<p>This is sufficient to provide the basis of all the arithmetic operations, since 
1183  +, -, * etc. can all be defined in terms of <em>succ</em>, <em>pred</em>, <em>zero</em> 
1184  and <em>iszero</em>. Of course they can be included in the type if required.</p>
1185  
1186<h3>7.2. Information Hiding</h3>
1187
1188<p>As it stands this type includes the operations <em>hi</em>, <em>lo</em> and 
1189  <em>constr</em> which were inherited from the record type as well as the new 
1190  operations. These old operations are a nuisance, they are not part of the double-precision 
1191  type as such and they provide a security risk since it would be possible for 
1192  someone to produce a value which appeared to be a double-precision number but, 
1193  for example, had a positive high order part and a negative low order part. Unwanted 
1194  operations can be masked out by giving an explicit signature which contains 
1195  only those operations which are actually required. 
1196<p><strong>let</strong> <em>dble</em>:<br>
1197  <strong>type</strong> (<em>d</em>)<br>
1198  <em>succ</em>, <em>pred</em>: <strong>proc</strong> (<em>d</em>)<em>d</em>;<br>
1199  <em>zero</em>: <em>d</em>;<br>
1200  <em>iszero</em>: <strong>proc</strong> (<em>d</em>)<em>boolean</em><br>
1201  <strong>end</strong><br>
1202  == <em>dp</em>;
1203<p>This has created a new type <em>dble</em> which takes objects from <em>dp</em> 
1204  but only takes those which are explicitly given in the type signature. It is 
1205  impossible to create a value of the <em>dble</em> type or take it apart except 
1206  with the given operations. An alternative would have been to have given the 
1207  explicit signature in the original declaration. 
1208<p><strong>let</strong> <em>dp</em>:<br>
1209  <strong>type</strong> (<em>d</em>)<br>
1210  <em>succ</em>, <em>pred</em>: <strong>proc</strong> (<em>d</em>)<em>d</em>;<br>
1211  <em>zero</em>: <em>d</em>;<br>
1212  <em>iszero</em>: <strong>proc</strong> (<em>d</em>)<em>boolean</em><br>
1213  <strong>end</strong><br>
1214  ==<br>
1215  <strong>type</strong> (<em>d</em>) <strong>extends</strong> ... { The body of 
1216  <em>dp</em> as before. }<br>
1217  <strong>end</strong>
1218<p>&nbsp;</p>
1219<h3>7.3. Conversions</h3>
1220<p>This double-precision type suffers from the problem that the only constant 
1221  value is <em>zero</em>. All other values have to be made by using <em>succ</em> 
1222  or <em>pred</em>. It would be convenient if other constants could be made. One 
1223  way would be to define a procedure inside the type constructor which would convert 
1224  an <em>integer</em> value into a <em>dble</em> one.</p>
1225<p><strong>let</strong> <em>make_double</em> ==<strong> proc</strong> (<em>int</em>: 
1226  <em>integer</em>)<em>d</em>. <em>d</em>$<em>constr</em>(0, <em>int</em>)</p>
1227<p>This assumes that no <em>integer</em> value is greater than 10000. If larger 
1228  <em>integer</em> values are possible then the body of the procedure would be 
1229</p>
1230<p><em>d</em>$<em>constr</em>(<em>int</em> <em>div</em> 10000, <em>int</em> <em>mod</em> 
1231  10000)</p>
1232<p><em>integer</em> values can now be made into <em>dble</em> ones.</p>
1233<p><em>dble</em>$<em>make_double</em>(42)</p>
1234<p>The maximum value is limited by the maximum possible <em>integer</em>, so very 
1235  large double-precision numbers still cannot be made. It would be nice to be 
1236  able to have large literal constants such as 12345678 in a program. A solution 
1237  to this is to convert literals directly from their string representations i.e. 
1238  from the string "12345678". This is done by defining a conversion procedure 
1239  with the name <em>convertn</em> inside the type. </p>
1240<p><strong>let</strong> <em>convertn</em> ==<br>
1241  <strong>proc</strong> (<em>rep</em>: <em>string</em>)<em>d</em><br>
1242  <strong>begin</strong><br>
1243  <strong>letrec</strong> <em>getch</em> ==<br>
1244  { Returns the result of converting the first <em>i</em> characters. }<br>
1245  <strong>proc</strong> (<em>i</em>: <em>integer</em>)<em>d</em><br>
1246  <strong>begin</strong><br>
1247  <strong>if</strong> <em>i</em> = 0<br>
1248  <strong>then</strong> <em>zero</em><br>
1249  <strong>else</strong><br>
1250  <strong>begin</strong><br>
1251  <strong>let</strong> <em>this_ch</em> == <em>rep</em> <em>sub</em> <em>i</em>; 
1252  { Obtains the ith. character }<br>
1253  <strong>if</strong> <em>this_ch</em> &lt; '0' | <em>this_ch</em> &gt; '9' { 
1254  Must be a digit }<br>
1255  <strong>then</strong> <strong>raise</strong> <em>conversionerror</em><br>
1256  <strong>else</strong><br>
1257  {Convert the first i-1 characters}<br>
1258  {then multiply by 10 and add in this digit}<br>
1259  <em>getch</em>(<em>i</em>-1)* <em>d</em>$ <em>make_value</em>(10) +<em> d</em>$ 
1260  <em>make_value</em>(<em>ord</em>(<em>this_ch</em>) - <em>ord</em>('0'))<br>
1261  <strong>end</strong><br>
1262  <strong>end</strong>;<br>
1263  <em>getch</em>(<em>string</em>$ <em>length</em>(<em>rep</em>))<br>
1264  <strong>end</strong></p>
1265<p>This procedure reads the string and converts it into the numeric value. If 
1266  any of the characters is not a digit then it raises the exception <em>conversionerror</em>. 
1267  We assume that + and <em>*</em> operations have been defined for the type.</p>
1268<p>With this operation it is possible to write expressions like</p>
1269<p><em>dble</em>$12345678 + <em>dble</em>$99999</p>
1270<p>The compiler automatically generates a call to <em>dble$convertn</em> when 
1271  it recognises these constants. It is usual to declare conversion routines as 
1272  <strong>early</strong> so that the compiler will do the conversion, rather than 
1273  leaving the conversion until the program is run. If a number is not preceeded 
1274  by a type name then the conversion used is the value of <em>convertn</em> which 
1275  is in scope. The standard header contains the binding</p>
1276<p><strong>let</strong> <em>convertn</em> == <em>integer</em>$ <em>convertn</em></p>
1277<p>so that numerical constants are converted to <em>integer</em> by default.</p>
1278<p>There are two other conversion operations, <em>convertc</em> for strings in 
1279  single quotes, and <em>converts</em> for strings in double quotes. These default 
1280  to <em>char$convertc</em> and <em>string$converts</em> respectively. </p>
1281<h3>7.4. Generic Types</h3>
1282
1283<p>Types in Poly can be treated as ordinary values. We have already seen how the
1284  ability to pass types as parameters to a procedure allows polymorphic
1285  operations, we shall now see how being able to return a type can be useful.</p>
1286<p>A type which could be used to hold lists of <em>integer</em> values was described 
1287  above. It was defined as</p>
1288<p><strong>letrec</strong> <em>int_list</em> == <strong>struct</strong> (<em>hd</em>: 
1289  <em>integer</em>; <em>tl</em>: <em>int_list</em>)</p>
1290<p>A type for lists of strings would be almost identical.</p>
1291<p><strong>letrec</strong> <em>str_list</em> == <strong>struct</strong> (<em>hd</em>: 
1292  <em>string</em>; <em>tl</em>: <em>str_list</em>)</p>
1293<p> Indeed lists of any type could be defined in the same way. The signature of 
1294  the type in each case is basically the same.</p>
1295<p><strong>type</strong> (<em>list</em>)<br>
1296  <em>hd</em>: <strong>proc</strong> (<em>list</em>)<em>integer</em>;<br>
1297  <em>tl</em>: <strong>proc</strong> (<em>list</em>)<em>list</em>;<br>
1298  <em>constr</em>: <strong>proc</strong> (<em>integer</em>; <em>list</em>)<em>list</em>;<br>
1299  <em>nil</em>: <em>list</em>;<br>
1300  &lt;&gt;, = : <strong>proc</strong> (<em>list</em>; <em>list</em>)<em>boolean</em><br>
1301  <strong>end</strong> </p>
1302<p>We can define a list type for an arbitrary element type using a procedure.</p>
1303<p><strong>let</strong> <em>list</em> ==<br>
1304  <strong>proc</strong> (<em>element_type</em>: <strong>type</strong> <strong>end</strong>)<br>
1305  <strong>type</strong> (<em>list</em>)<br>
1306  <em>hd</em>: <strong>proc</strong> (<em>list</em>)<em>element_type</em>;<br>
1307  <em>tl</em>: <strong>proc</strong> (<em>list</em>)<em>list</em>;<br>
1308  <em>constr</em>: <strong>proc</strong> (<em>element_type</em>; <em>list</em>)<em>list</em>;<br>
1309  <em>nil</em>: <em>list</em>;<br>
1310  &lt;&gt;, = : <strong>proc</strong> (<em>list</em>; <em>list</em>)<em>boolean</em><br>
1311  <strong>end</strong> .<br>
1312  <strong>begin</strong><br>
1313  <strong>letrec</strong> <em>list_type</em> == <strong>struct</strong> (<em>hd</em>: 
1314  <em>element_type</em>; <em>tl</em>: <em>list_type</em>);<br>
1315  <em>list_type</em><br>
1316  <strong>end</strong></p>
1317<p>This procedure can be applied to any type, since any type matches the empty 
1318  type "<strong>type</strong> <strong>end</strong>". </p>
1319<p><strong>let</strong> <em>int_list</em> == <em>list</em>(<em>integer</em>);<br>
1320  <strong>let</strong> <em>str_list</em> == <em>list</em>(string);<br>
1321  <strong>let</strong> <em>dble_list</em> == <em>list</em>(<em>dble</em>);</p>
1322<p>The result is always a list with the same operations, but different signatures.</p>
1323<p><strong>let</strong> <em>a_list</em> == <em>int_list</em>$ <em>constr</em>(999, 
1324  <em>int_list</em>$ <em>nil</em>);<br>
1325  <strong>let</strong> <em>b_list</em> == <em>str_list</em>$ <em>constr</em>(&quot;hello&quot;, 
1326  <em>str_list</em>$ <em>nil</em>); </p>
1327<p>The list types are different types, so only operations of the correct type 
1328  are possible.</p>
1329<p><em>int_list</em>$ <em>hd</em>(<em>a_list</em>);<br>
1330  <em>str_list</em>$ <em>hd</em>(<em>b_list</em>) </p>
1331<p>are valid, but </p>
1332<p><em>int_list</em>$ <em>hd</em>(<em>b_list</em>);<br>
1333  <em>int_list</em>$ <em>tl</em>(<em>b_list</em>);<br>
1334  <strong>let</strong> <em>c_list</em> == <em>int_list</em>$ <em>constr</em>(999, 
1335  <em>b_list</em>)</p>
1336<p>are not.</p>
1337<table width="600" border="1">
1338  <tr> 
1339    <td><strong>Syntax</strong></td>
1340  </tr>
1341  <tr> 
1342    <td><table width="600" border="0">
1343        <tr> 
1344          <td valign="top">&lt;type constructor&gt;</td>
1345          <td valign="top">::=</td>
1346          <td><p><strong>type</strong> &lt;internal name&gt; &lt;declarations&gt;<br>
1347              &lt;extension&gt; &lt;declarations&gt; <strong>end</strong> </p></td>
1348        </tr>
1349        <tr> 
1350          <td valign="top">&lt;internal name&gt;</td>
1351          <td valign="top">::=</td>
1352          <td>(&lt;identifier&gt;) | &lt;empty&gt;</td>
1353        </tr>
1354        <tr> 
1355          <td valign="top">&lt;declarations&gt;</td>
1356          <td valign="top">::=</td>
1357          <td>&lt;dec list&gt; | &lt;empty&gt;</td>
1358        </tr>
1359        <tr> 
1360          <td valign="top">&lt;dec list&gt;</td>
1361          <td valign="top">::=</td>
1362          <td>&lt;declaration&gt; | &lt;dec list&gt;; &lt;declaration&gt;</td>
1363        </tr>
1364        <tr>
1365          <td valign="top">&lt;extension&gt;</td>
1366          <td valign="top">::=</td>
1367          <td><strong>extend</strong> &lt;expression&gt; | &lt;empty&gt;</td>
1368        </tr>
1369      </table></td>
1370  </tr>
1371</table>
1372<p>&nbsp;</p>
1373<h2>Chapter 8. Standard Definitions</h2>
1374<p>Poly is extremely flexible because much of the system is built
1375  on top of the language rather than built into it.
1376  It can be changed as required by redefining or adding new definitions.
1377  The standard definitions contain types and procedures which are likely
1378  to be of use to many programmers.
1379  For efficiency reasons some are written in assembly code or are handled
1380  specially by the code generator, but they all have signatures like
1381  ordinary definitions and can be redefined if required.</p>
1382  
1383<h3>8.1. Primitive Types</h3>
1384  
1385<h4>8.1.1. void</h4>
1386
1387<p><em>void</em> is used as a form of place-holder when a type is expected. For 
1388  example, procedures which have side effects but no result are considered as 
1389  returning a value of type <em>void</em>. It has only one value <em>empty</em>, 
1390  and its signature is simply  </p>
1391<p><em>void</em> :<br>
1392  <strong>type</strong> (<em>void</em>)<br>
1393  <em>empty</em> : <em>void</em><br>
1394  <strong>end</strong> </p>
1395<h4>8.1.2. boolean</h4>
1396
1397<p><em>Boolean</em> is the type used in tests. It has two values <em>true</em> 
1398  and <em>false</em>. The complete signature is
1399<p><em>boolean</em> :<br>
1400  <strong>type</strong> (<em>boolean</em>)<br>
1401  <em>true</em>, <em>false</em> : <em>boolean</em>;<br>
1402  &amp; : <strong>proc</strong> <strong>infix</strong> 4(<em>boolean</em>; <em>boolean</em>)<em>boolean</em>;<br>
1403  | : <strong>proc</strong> <strong>infix</strong> 3(<em>boolean</em>; <em>boolean</em>)<em>boolean</em>;<br>
1404  ~ : <strong>proc</strong> (<em>boolean</em>)<em>boolean</em>;<br>
1405  &lt;&gt;, = : <strong>proc</strong> <strong>infix</strong> 5(<em>boolean</em>; 
1406  <em>boolean</em>)<em>boolean</em>;<br>
1407  <em>repr</em> : <strong>proc</strong> (<em>boolean</em>)<em>string</em>;<br>
1408  <em>print</em> : <strong>proc</strong> (<em>boolean</em>)<br>
1409  <strong>end</strong>
1410<p>The <strong>&amp;</strong>, <em><strong>|</strong></em> and <em><strong>~</strong></em> 
1411  operations correspond to the normal <em>boolean</em> operations <strong>AND</strong> 
1412  (the result is <em>true</em> only if both the arguments are <em>true</em>), 
1413  <strong>OR</strong> (the result is <em>true</em> if either arguments are <em>true</em>) 
1414  and <strong>NOT</strong> (the result is <em>true</em> if the argument is <em>false</em> 
1415  and vice-versa). <em>&lt;&gt;</em> and <em>=</em> compare the two arguments 
1416  and can be regarded as exclusive-OR and exclusive-NOR respectively. <em>repr</em> 
1417  returns the string "true" if the argument is <em>true</em> and "false" if it 
1418  is <em>false</em>. <em>print</em> prints the string representation on the terminal. </p>
1419<h4>8.1.3. integer</h4>
1420<p>The type <em>integer</em> is the basic type used for numbers. Its signature 
1421  is </p>
1422<p><em>integer</em> :<br>
1423  <strong>type</strong> (<em>integer</em>)<br>
1424  <em>first</em>, <em>last</em>, <em>zero</em> : <em>integer</em>;<br>
1425  +, - : <strong>proc</strong> <strong>infix</strong> 6(<em>integer</em>; <em>integer</em>)<em>integer</em>;<br>
1426  <em>* </em>, <em>div</em>, <em>mod</em> : <strong>proc</strong> <strong>infix</strong> 
1427  7(<em>integer</em>; <em>integer</em>)<em>integer</em>;<br>
1428  <em>pred</em>, <em>succ</em>, <em>neg</em> : <strong>proc</strong> (<em>integer</em>)<em>integer</em>;<br>
1429  ~ : <strong>proc</strong> (<em>integer</em>)<em>integer</em>;<br>
1430  &lt;, &lt;=, &lt;&gt;, =, &gt;, &gt;= : <strong>proc</strong> <strong>infix</strong> 
1431  5(<em>integer</em>; <em>integer</em>)<em>boolean</em>;<br>
1432  <em>convertn</em> : <strong>proc</strong> (<em>string</em>)<em>integer</em>;<br>
1433  <em>repr</em> : <strong>proc</strong> (<em>integer</em>)<em>string</em>;<br>
1434  <em>print</em> : <strong>proc</strong> (<em>integer</em>);<br>
1435  <strong>end</strong></p>
1436<p><em>first</em> and <em>last</em> are the minimum and maximum values that an 
1437  <em>integer</em> can have. <em>last</em> is frequently one less than the negative 
1438  of <em>first</em>.</p>
1439<p>+ and <em>-</em> are the usual infix addition and subtraction operations. They 
1440  raise the exception <em>range</em> if the result is outside the valid range.</p>
1441<p><em>* </em> is the infix multiplication operator which also raises
1442  <em>range</em> is the result is out of range.</p>
1443<p><em>div</em> is the division operator and <em>mod</em> returns the remainder.
1444  They both generate <em>divide</em> if they are asked to divide by zero, and
1445  <em>div</em> may generate <em>range</em> when
1446  <em>first</em>
1447  is divided by minus
1448  one.</p>
1449<p><em>pred</em> and <em>succ</em> respectively subtract and add one to a number,
1450  raising <em>range</em> if the result is out of range.</p>
1451<p><em>neg</em> returns the negative, raising
1452  <em>range</em> if its argument is
1453  <em>first</em>.</p>
1454<p><em>~</em> is the same as <em>neg</em>.</p>
1455<p><em>&lt;</em>, <em>&lt;=</em>, <em>&lt;&gt;</em>, <em>=</em>, <em>&gt;</em> 
1456  and <em>&gt;=</em> are the
1457  usual infix comparison operations.</p>
1458<p><em>convertn</em> is the operation which converts literal constants into integers. 
1459  It recognises strings of digits as decimal (base 10) values unless the first 
1460  character is a '0' in which case it treats it as an octal value or hexadecimal 
1461  if it starts with '0x'. <em>conversion</em> is raised if the string does not 
1462  fit one of these forms or <em>rangeerror</em> if it is too large.</p>
1463<p><em>repr</em> performs the reverse of <em>convertn</em> by generating a string
1464  from a number.
1465  It is always generated as a decimal number.</p>
1466<p><em>print</em> prints the string representation on the terminal.</p>
1467<h4>8.1.4. long_integer</h4>
1468<p><em>long_integer</em> is very similar to <em>integer</em> but it allows larger 
1469  numbers to be handled. Its signature is
1470<p><em>long_integer</em> :<br>
1471  <strong>type</strong> (<em>long_integer</em>)<br>
1472  <em>first</em>, <em>last</em>, <em>zero</em> : <em>long_integer</em>;<br>
1473  +, - : <strong>proc</strong> <strong>infix</strong> 6(<em>long_integer</em>; 
1474  <em>long_integer</em>)<em>long_integer</em>;<br>
1475  <em>* </em>, <em>div</em>, <em>mod</em>: <strong>proc</strong> <strong>infix</strong> 
1476  7(<em>long_integer</em>; <em>long_integer</em>)<em>long_integer</em>;<br>
1477  <em>pred</em>, <em>succ</em>, <em>neg</em>: <strong>proc</strong> (<em>long_integer</em>)<em>long_integer</em>;<br>
1478  ~ : <strong>proc</strong> (<em>long_integer</em>)<em>long_integer</em>;<br>
1479  &lt;, &lt;=, &lt;&gt;, =, &gt;, &gt;= : <strong>proc</strong> <strong>infix</strong> 
1480  5(<em>long_integer</em>; <em>long_integer</em>)<em>boolean</em>;<br>
1481  <em>convertn</em> : <strong>proc</strong> (<em>string</em>)<em>long_integer</em>;<br>
1482  <em>repr</em> : <strong>proc</strong> (<em>long_integer</em>)<em>string</em>;<br>
1483  <em>print</em> : <strong>proc</strong> (<em>long_integer</em>);<br>
1484  <em>from_integer</em> : <strong>proc</strong> (<em>integer</em>)<em>long_integer</em>;<br>
1485  <em>to_integer</em> : <strong>proc</strong> (<em>long_integer</em>)<em>integer</em>;<br>
1486  <strong>end</strong>
1487<p>The signature is the same as that of integer with the addition of <em>from_integer</em> 
1488  and <em>to_integer</em> which convert between <em>integer</em> and <em>long_integer</em>. 
1489  <em>to_integer</em> generates a <em>rangeerror</em> exception if the value is 
1490  too large to fit into an integer. </p>
1491<h4>8.1.5. char</h4>
1492<p>The type <em>char</em> is the type of character values. It has signature
1493<p><em>char</em> :<br>
1494  <strong>type</strong> (<em>char</em>)<br>
1495  <em>first</em>, <em>last</em> : <em>char</em>;<br>
1496  &lt;, &lt;=, &lt;&gt;, =, &gt;, &gt;= : <strong>proc</strong> <strong>infix</strong> 
1497  5(<em>char</em>; <em>char</em>)<em>boolean</em>;<br>
1498  <em>convertc</em> : <strong>proc</strong> (<em>string</em>)<em>char</em>;<br>
1499  <em>pred</em>, <em>succ</em> : <strong>proc</strong> (<em>char</em>)<em>char</em>;<br>
1500  <em>repr</em> : <strong>proc</strong> (<em>char</em>)<em>string</em>;<br>
1501  <em>print</em> : <strong>proc</strong> (<em>char</em>);<br>
1502  <strong>end</strong> 
1503<p>Characters are regarded as being ordered according to the underlying character 
1504  code. The ordering will usually follow alphabetic order. <em>first</em> and 
1505  <em>last</em> are the smallest and largest characters and <em>pred</em> and 
1506  <em>succ</em> give the previous and succeeding characters according to the ordering. 
1507  <em>pred</em> and <em>succ</em> raise the <em>range</em> exception if a value 
1508  would be produced outside the range. The comparison operations compare values 
1509  according to the ordering. </p>
1510<h4>8.1.6. string</h4>
1511<p>Character strings have type <em>string</em>. 
1512<p><em>string</em> :<br>
1513  <strong><strong></strong>type</strong> (<em>string</em>)<br>
1514  <em>mk</em> : <strong>proc</strong> (<em>char</em>)<em>string</em>;<br>
1515  &lt;, &lt;=, &lt;&gt;, =, &gt;, &gt;= : <strong>proc</strong> <strong>infix</strong> 
1516  5(<em>string</em>; <em>string</em>)<em>boolean</em>;<br>
1517  <em>converts</em> : <strong>proc</strong> (<em>string</em>)<em>string</em>;<br>
1518  <em>length</em> : <strong>proc</strong> (<em>string</em>)<em>integer</em>;<br>
1519  <em>print</em> : <strong>proc</strong> (<em>string</em>);<br>
1520  <em>repr</em> : <strong>proc</strong> (<em>string</em>)<em>string</em>;<br>
1521  + : <strong>proc</strong> <strong>infix</strong> 6(<em>string</em>; <em>string</em>)<em>string</em>;<br>
1522  <em>sub</em> : <strong>proc</strong> <strong>infix</strong> 8(<em>string</em>; 
1523  <em>integer</em>)<em>char</em>;<br>
1524  <em>substring</em> : <strong>proc</strong> (<em>string</em>; <em>integer</em>; 
1525  <em>integer</em>)<em>string</em><br>
1526  <strong>end</strong>
1527<p><em>mk</em> converts a character into a single length string, while <em>sub</em> 
1528  selects a character at a particular position. The character positions are numbered 
1529  from 1 to the length of the string, returned by <em>length</em>. Selecting a 
1530  character outside this range results in a <em>subscript</em> exception. Subcripting 
1531  a zero length string will therefore always result in an exception. <em>substring</em> 
1532  extracts a string from another. It takes as parameters a string, an integer 
1533  which gives the starting position in the string, and a second integer which 
1534  gives the number of characters to select. 
1535<p><em>string</em>$<em>substring</em>(&quot;hello&quot;, 3,2); 
1536<p>results in the string "ll". If the first parameter is outside the string or 
1537  there are not enough characters in the string then <em>subscript</em> is raised. 
1538  Two strings can be concatenated by +. </p>
1539<h3>8.2. Variables and Vectors</h3>
1540<p>So far the language described has been purely applicative, that is
1541  procedures can be applied to values, but there is no way to change the
1542  value associated with an object.
1543  Variables and vectors can be created and used in Poly but they are not built
1544  into the type system.</p>
1545  
1546<h4>8.2.1. new</h4>
1547<p>Variables are created by the procedure <em>new</em> which has the following 
1548  signature. </p>
1549<p><em>new</em> : <strong>proc</strong> <strong>[</strong><em>base</em> : <strong>type</strong> 
1550  <strong>end</strong> <strong>]</strong> (<em>base</em>)<br>
1551  <strong>type</strong><br>
1552  <em>assign</em> : <strong>proc</strong> (<em>base</em>);<br>
1553  <em>content</em> : <strong>proc</strong> ()<em>base</em><br>
1554  <strong>end</strong></p>
1555<p><em>new</em> is a procedure which takes a value of any type and returns a type 
1556  with two operations <em>assign</em> and <em>content</em> as its result. For 
1557  example, </p>
1558<p><strong>let</strong> <em>v</em> == <em>new</em>(99); </p>
1559<p>declares <em>v</em> as a type with signature </p>
1560<p><em>v</em>: <strong>type</strong><br>
1561  <em>assign</em> : <strong>proc</strong> (<em>integer</em>);<br>
1562  <em>content</em> : <strong>proc</strong> ()<em>integer</em><br>
1563  <strong>end</strong> </p>
1564<p>The type is here being used as a way of packaging together a pair of procedures, 
1565  there is no such thing as a value of this type.</p>
1566<p>The parameter type of <em>assign</em> and the result of <em>content</em> were 
1567  found from the type of the original argument (99 has type <em>integer</em>). 
1568  The current value associated with the variable is found using the <em>content</em> 
1569  procedure. </p>
1570<p><em>v</em>$<em>content</em>()</p>
1571<p>will return 99, the initial value associated with it. The value can be changed 
1572  using <em>assign</em>. </p>
1573<p><em>v</em>$<em>assign</em>(<em>v</em>$<em>content</em>()+1); </p>
1574<p>sets the value to 100.</p>
1575<p>Variables can be passed as parameters and returned as results from procedures 
1576  like any other value. However, note that</p>
1577<p><strong>let</strong> <em>vv</em> == <em>v</em>; </p>
1578<p>makes <em>vv</em> the same value as <em>v</em> which means that it refers to 
1579  the same variable, and hence changes to <em>vv</em> will affect the value returned 
1580  from <em>v</em> and vice versa.</p>
1581<p>It is not necessary to write "$<em>content</em>()" after every variable name 
1582  in order to extract its value, the compiler will attempt to call the <em>content</em> 
1583  object of a type if it is given one when it expects an ordinary value. There 
1584  is also an infix assignment operator defined in the standard header which allows 
1585  use of the normal syntax for assignment. 
1586<p><em>v</em> := <em>v</em>+1
1587<p>is therefore equivalent to
1588<p><em>v</em>$<em>assign</em>(<em>v</em>$<em>content</em>()+1)
1589
1590<h4>8.2.2. vector</h4>
1591<p><em>vector</em> is a procedure which creates an array of variables. </p>
1592<p><em>vector</em>: <strong>proc</strong> <strong>[</strong><em>base</em> : <strong>type</strong> 
1593  <strong>end</strong> <strong>]</strong> (<em>size</em>: <em>integer</em>; <em>initial_value</em>: 
1594  <em>base</em>)<br>
1595  <strong>type</strong><br>
1596  <em>sub</em>: <strong>proc</strong> (<em>integer</em>)<br>
1597  <strong>type</strong><br>
1598  <em>assign</em> : <strong>proc</strong> (<em>base</em>);<br>
1599  <em>content</em> : <strong>proc</strong> ()<em>base</em><br>
1600  <strong>end</strong>; <br>
1601  <em>first</em>, <em>last</em>: <em>integer</em><br>
1602  <strong>end</strong></p>
1603<p> It takes as parameters the size of the array (i.e. the number of variables) 
1604  and a value which is the initial value for each. </p>
1605<p><strong>let</strong> <em>v</em> == <em>vector</em>(10, &quot;init&quot;)</p>
1606<p>A particular variable is selected by applying the <em>sub</em> procedure to 
1607  a number between 1 and the size (the <strong>index</strong>). The result will 
1608  be a variable which can be assigned a value, or its value can be read. </p>
1609<p><em>v</em>$<em>sub</em>(4)<br>
1610  <em>v</em>$<em>sub</em>(5) := &quot;new string&quot;</p>
1611<p>Attempting to apply <em>sub</em> to a value outside the range causes a <em>subscript</em> 
1612  exception.</p>
1613<p><em>first</em> and <em>last</em>
1614  are two integer values that are set to the
1615  minimum and maximum index values (1 and the size respectively).
1616  If the size parameter given to <em>vector</em> is less than 1 it will raise
1617  a <em>range</em> exception.</p>
1618  
1619<h3>8.3. Iterators</h3>
1620<p>Many programs involve the processing of lists or sets of values processing 
1621  each one or searching for one which satisfies some condition. The standard header 
1622  contains definitions to make these easier. All of these work using a standard 
1623  interface, a type, called an <strong>iterator</strong> which represents an abstract 
1624  sequence of values. An iterator has the following signature. 
1625<p><strong>type</strong> (<em>iterator</em>)<br>
1626  <em>continue</em> : <strong>proc</strong> (<em>iterator</em>)<em>boolean</em>;<br>
1627  <em>init</em> : <strong>proc</strong> ()<em>iterator</em>;<br>
1628  <em>next</em> : <strong>proc</strong> (<em>iterator</em>)<em>iterator</em>;<br>
1629  <em>value</em> : <strong>proc</strong> (<em>iterator</em>)<em>base_type</em><br>
1630  <strong>end</strong>
1631<p>Values of the iterator type are elements of a sequence such that each has a 
1632  value of some base type associated with it and a way of getting to the next 
1633  element. They can be regarded as elements of a list, but equally they can be 
1634  a range of integer values. <em>init</em> generates the first element of the 
1635  sequence, and <em>continue</em> tests it to see if is a valid entry (the sequence 
1636  may be empty or exhausted). If it is valid then <em>value</em> may be used to 
1637  extract the associated value and <em>next</em> used to return the next element 
1638  in the sequence. To see how this works we will examine two procedures which 
1639  use iterators. </p>
1640<h4>8.3.1. for</h4>
1641<p>The <em>for</em> procedure is designed to apply a given procedure to every 
1642  member of a sequence. Its signature is
1643<p><em>for</em> : <strong>proc</strong> <strong>[</strong><em>base</em> : <strong>type</strong> 
1644  <strong>end</strong> <strong>]</strong><br>
1645  (<em>iterator</em> :<br>
1646  <strong>type</strong> (<em>iterator</em>)<br>
1647  <em>continue</em> : <strong>proc</strong> (<em>iterator</em>)<em>boolean</em>;<br>
1648  <em>init</em> : <strong>proc</strong> ()<em>iterator</em>;<br>
1649  <em>next</em> : <strong>proc</strong> (<em>iterator</em>)<em>iterator</em>;<br>
1650  <em>value</em> : <strong>proc</strong> (<em>iterator</em>)<em>base</em><br>
1651  <strong>end</strong>; <br>
1652  <em>body</em>: <strong>proc</strong> (<em>base</em>)) 
1653<p>It takes an iterator and applies the procedure <em>body</em> to each element 
1654  in turn. The body of <em>for</em> in Poly is 
1655<p><strong>begin</strong><br>
1656  <strong>letrec</strong> <em>successor</em> ==<br>
1657  { Loop until the condition is false }<br>
1658  <strong>proc</strong> <strong>inline</strong> (<em>counter</em>: <em>iterator</em>)<br>
1659  <strong>begin</strong><br>
1660  <strong>if</strong> <em>counter</em>.<em>continue</em><br>
1661  <strong>then</strong><br>
1662  <strong>begin</strong><br>
1663  <em>body</em>(<em>counter</em>.<em>value</em>);<br>
1664  <em>successor</em>(<em>counter</em>.<em>next</em>)<br>
1665  <strong>end</strong><br>
1666  <strong>end</strong> { successor }; <br>
1667  <em>successor</em>(<em>iterator</em>$<em>init</em>()) { The initial value of 
1668  the iterator. }<br>
1669  <strong>end</strong> { for }; 
1670<p>The <em>successor</em> loops generating elements of the sequence and applying 
1671  <em>body</em> to each value until the sequence is exhausted. </p>
1672<h4>8.3.2. first</h4>
1673<p>The other procedure which operates on iterators is <em>first</em> which searches 
1674  a sequence until a condition is found. It has signature </p>
1675<p><em>first</em> : <strong>proc</strong> <strong>[</strong><em>base</em>, <em>result</em>: 
1676  <strong>type</strong> <strong>end</strong> <strong>]</strong><br>
1677  (<em>iterator</em> :<br>
1678  <strong>type</strong> (<em>iterator</em>)<br>
1679  <em>continue</em> : <strong>proc</strong> (<em>iterator</em>)<em>boolean</em>;<br>
1680  <em>init</em> : <strong>proc</strong> ()<em>iterator</em>;<br>
1681  <em>next</em> : <strong>proc</strong> (<em>iterator</em>)<em>iterator</em>;<br>
1682  <em>value</em> : <strong>proc</strong> (<em>iterator</em>)<em>base</em><br>
1683  <strong>end</strong>;<br>
1684  <em>test</em>: <strong>proc</strong> (<em>base</em>)<em>boolean</em>;<br>
1685  <em>success</em>: <strong>proc</strong> (<em>base</em>)<em>result</em>;<br>
1686  <em>failure</em>: <strong>proc</strong> ()<em>result</em><br>
1687  )<em>result</em></p>
1688<p> As well as the iterator, <em>first</em> takes three other explicit parameters, 
1689  all procedures. The first is the test which is applied to each value. If it 
1690  succeeds (returns <em>true</em>) then the <em>success</em> procedure is called 
1691  with the value as its parameter. If the sequence is exhausted before the test 
1692  has succeeded the <em>failure</em> procedure is invoked. The result of <em>first</em> 
1693  is the result of either <em>success</em> or <em>failure</em>. </p>
1694
1695<h2>Chapter 9. Compiler and Environment</h2>
1696
1697<p>This part of the system is still under development and is not guaranteed to
1698  remain stable.
1699  Parts of it are also heavily UNIX dependent.</p>
1700<p>The current environment support provides facilities for compiling text files
1701  and remaking a system from its composite modules, compiling those which have
1702  been modified.
1703  There is a simple history mechanism for re-executing commands.</p>
1704<p>The system is used interactively with Poly expressions and declarations being
1705  typed in by the user and the reponses printed by the computer.
1706  Poly is used as a command language as well as a programming language, so all
1707  commands are simply Poly procedure calls and have their signatures checked
1708  by the compiler.
1709  Commands must either return values of type <em>void</em>, in which case they 
1710  are
1711  simply executed, or they must return values of a type which has a print
1712  operation so that the result can be printed.
1713  Variables and procedures with no parameters are allowed provided their results
1714  are void or can be printed.</p>
1715  
1716<h3>9.1. environ</h3>
1717
1718<p><em>environ</em> is the type which is the nearest equivalent to a file directory
1719  in Poly.
1720  It has signature
1721  </p>
1722<p> <em>environ</em> :<br>
1723  <strong>type</strong> (<em>environ</em>)<br>
1724  <em>enter</em> : <strong>proc</strong> (<em>environ</em>; <em>string</em>; <em>declaration</em>);<br>
1725  <em>lookup</em> : <strong>proc</strong> (<em>environ</em>; <em>string</em>)<em>declaration</em>;<br>
1726  <em>delete</em> : <strong>proc</strong> (<em>environ</em>; <em>string</em>);<br>
1727  <em>print</em> : <strong>proc</strong> (<em>environ</em>);<br>
1728  <em>in</em> : <strong>proc</strong> (<br>
1729  <strong>type</strong><br>
1730  <em>enter</em> : <strong>proc</strong> (<em>string</em>; <em>declaration</em>);<br>
1731  <em>lookup</em> : <strong>proc</strong> (<em>string</em>)<em>declaration</em>;<br>
1732  <em>delete</em> : <strong>proc</strong> (<em>string</em>);<br>
1733  <em>over</em> : <strong>type</strong> (<em>iter</em>)<br>
1734  <em>continue</em> : <strong>proc</strong> (<em>iter</em>)<em>boolean</em>;<br>
1735  <em>init</em> : <strong>proc</strong> ()<em>iter</em>;<br>
1736  <em>next</em> : <strong>proc</strong> (<em>iter</em>)<em>iter</em>;<br>
1737  <em>value</em> : <strong>proc</strong> (<em>iter</em>)<em>declaration</em><br>
1738  <strong>end</strong><br>
1739  <strong>end</strong><br>
1740  )<em>environ</em>;<br>
1741  <em>out</em> : <strong>proc</strong> (<em>environ</em>)<br>
1742  <strong>type</strong><br>
1743  <em>enter</em> : <strong>proc</strong> (<em>string</em>; <em>declaration</em>);<br>
1744  <em>lookup</em> : <strong>proc</strong> (<em>string</em>)<em>declaration</em>;<br>
1745  <em>delete</em> : <strong>proc</strong> (<em>string</em>);<br>
1746  <em>over</em> : <strong>type</strong> (<em>iter</em>)<br>
1747  <em>continue</em> : <strong>proc</strong> (<em>iter</em>)<em>boolean</em>;<br>
1748  <em>init</em> : <strong>proc</strong> ()<em>iter</em>;<br>
1749  <em>next</em> : <strong>proc</strong> (<em>iter</em>)<em>iter</em>;<br>
1750  <em>value</em> : <strong>proc</strong> (<em>iter</em>)<em>declaration</em><br>
1751  <strong>end</strong><br>
1752  <strong>end</strong><br>
1753  <strong>end</strong></p>
1754<p><em>declaration</em> is a type which the compiler uses to represent objects 
1755  that it has created.</p>
1756<p>A value of the <em>environ</em> type is a set of procedures which
1757  map strings onto <em>declaration</em> values.
1758  The compiler uses the value of <em>current_env</em> as the environment in which
1759  to compile something.
1760  It uses the <em>lookup</em> procedure to find the value and signature of
1761  identifiers and calls <em>enter</em> to store the result of making declarations.
1762  A particular value of the environ type is made by using the <em>in</em> procedure
1763  to package up a type with the appropriate operations.
1764  The inverse operation <em>out</em> can be used to extract the type.</p>
1765<p>There is a procedure <em>mkenv</em> which can be used to create environ values.
1766  It has signature
1767  </p>
1768<p> <em>mkenv</em> : <strong>proc</strong> (<em>environ</em>)<em>environ</em></p>
1769<p>It returns an environment which can be seen as an extension of the environment 
1770  which was given as the parameter. New declarations result in entries in this 
1771  new environment and they can be found by using the identifier. However, looking 
1772  up an identifier which has not been declared in this environment results in 
1773  a search in the environment originally passed as the parameter. This can be 
1774  regarded in the same way as nested declarations in Poly where an identifier 
1775  is first looked up in the current block and if it is not found there the enclosing 
1776  blocks are searched. Typically <em>mkenv</em> is called with either the current 
1777  environment or the global environment as parameter. </p>
1778<p> <strong>let</strong> <em>new_env</em> == <em>mkenv</em>(<em>current_env</em>);<br>
1779  <em>current_env</em> := <em>newenv</em>;<br>
1780  <strong>let</strong> <em>new_env</em> == <em>mkenv</em>(<em>global_env</em>); 
1781</p>
1782<p>The global environment contains declarations such as integer which it is expected 
1783  that nearly all programs will require.</p>
1784<p>The computation involved when entering or looking up an identifier may be
1785  considerably more than just operating on a table.
1786  The <em>make</em> procedure, for example, uses an environment in which looking 
1787  up
1788  an identifier may involve recursive calls to the compiler to compile the
1789  object.</p>
1790  
1791<h3>9.2. ?</h3>
1792
1793<p>? prints the signature of an object which has previously been declared. It 
1794  has signature </p>
1795<p> ? : <strong>proc</strong> (<em>string</em>)</p>
1796<p>For example, the statement </p>
1797<p> ? &quot;?&quot;;</p>
1798<p>prints </p>
1799<p> ? : <strong>proc</strong> (<em>string</em>)
1800<p> It is useful to be able to check the signature of an object before using it. </p>
1801<h3>9.3. #</h3>
1802
1803<p># runs a text file through the compiler and executes the result. It has signature 
1804</p>
1805<p> \# : <strong>proc</strong> (<em>string</em>) 
1806<p>At present the string parameter it takes is an ordinary UNIX file name, without 
1807  any processing of wild-cards. </p>
1808<h3>9.4. sh</h3>
1809<p><em>sh</em> runs a line of text through the UNIX shell.
1810  It can be used to execute any command, including starting up interactive
1811  programs.
1812  It has signature
1813  </p>
1814<p> <em>sh</em> : <strong>proc</strong> (<em>string</em>)</p>
1815<p>For example, </p>
1816<p> <em>sh</em> &quot;emacs fred&quot;;
1817<p>will start up and run the "emacs" editor on a file called "fred". The Poly 
1818  system will wait until the process is finished before continuing. </p>
1819<h3>9.5. make</h3>
1820<p>The <em>make</em> command in Poly is similar in function to the "make" command
1821  under UNIX.
1822  It constructs a Poly object by recompiling only those parts of it which have
1823  changed since it was last made.</p>
1824<p>It is generally good programming practice to break a large program down into
1825  several parts, usually called modules, and develop each independently.
1826  A module usually provides several related functions and so can be represented
1827  in Poly as a "type".
1828  Such types may or may not have values belonging to them.
1829  For example, a module to construct stacks could be the type "stack" and all
1830  stacks would be of that type, but a module for a set of trigonometrical
1831  functions would be simply a set of related procedures.</p>
1832<p>A module may be complete in itself or require other modules to make it work.
1833  The latter case is represented in Poly by a procedure which takes some types
1834  as parameters and returns a type as the result.
1835  So, for example, a module for a parser may use modules for the symbols and for
1836  the parse tree.</p>
1837<p>An important point about these modules is that each can be compiled
1838  independently and then the program can be made by applying the modules which
1839  are procedures to their parameters.
1840  The process of applying a module to other modules is known as <em>binding</em>.
1841  Like any other procedure application in Poly it is subject to the normal rules
1842  for signature matching.</p>
1843<p>When a module is changed, for example to correct a bug, it must be recompiled
1844  and rebound.
1845  The purpose of the make procedure is to ensure that everything which must be
1846  recompiled has been and to rebind all the necessary modules.
1847  Note that a change to the signature of the module may require changes to
1848  other modules that use it, otherwise a signature fault may be generated by
1849  the compiler.
1850  A change of signature may not always require all the using modules to be
1851  recompiled.
1852  For example, a module which is a type may have several operations used by
1853  different using modules.
1854  Changing the signature of one of these operations will require changes
1855  only to those modules which actually use that operation.</p>
1856<p>The make procedure assumes that the source text of the modules is held in some
1857  UNIX text files in a set of related directories.
1858  As an example suppose we have a set of modules which are combined in the
1859  following fashion to make a program.
1860  </p>
1861<p> <strong>let</strong> <em>a</em> == <em>b</em>(<em>c</em>, <em>d</em>);<br>
1862  <strong>let</strong> <em>e</em> == <em>f</em>(<em>g</em>, <em>h</em>);<br>
1863  <strong>let</strong> <em>i</em> == <em>j</em>(<em>a</em>, <em>e</em>, <em>h</em>);<br>
1864  <strong>let</strong> <em>z</em> == <em>k</em>(<em>i</em>, <em>e</em>); </p>
1865<p><em>z</em> is the result of binding the modules together and is the final program. 
1866  The source text is arranged in a series of directories with the root directory 
1867  called <strong>z</strong>. </p>
1868<p><strong>z</strong> contains <strong>k</strong>, <strong>i</strong> and <strong>e</strong> 
1869  and <strong>h</strong>.<br>
1870  <strong>z/k</strong> is the source text for <em>k</em>.<br>
1871  <strong>z/i</strong> is a directory containing <strong>j</strong> and <strong>a</strong>.<br>
1872  <strong>z/i/j</strong> is the source text for <em>j</em>.<br>
1873  <strong>z/i/a</strong> is a directory containing source files <strong>b</strong>, 
1874  <strong>c</strong> and <strong> d</strong>. }<br>
1875  <strong>z/e</strong> is a directory containing source texts <strong>f</strong> 
1876  and <strong>g</strong>.<br>
1877  <strong>z/h</strong> is the source text for <em>h</em>.</p>
1878<p>In addition each directory has a file called <strong>poly_bind</strong> which 
1879  are the instructions for binding together the modules to make the result. </p>
1880<p> <strong>z/poly_bind</strong> contains "<strong>let</strong> <em>z</em> == 
1881  <em>k</em>(<em>i</em>, <em>e</em>);"<br>
1882  <strong>i/poly_bind</strong> contains "<strong>let</strong> <em>i</em> == <em>j</em>(<em>a</em>, 
1883  <em>e</em>, <em>h</em>);"<br>
1884  <strong>e/poly_bind</strong> contains "<strong>let</strong> <em>e</em> == <em>f</em>(<em>g</em>, 
1885  <em>h</em>);"<br>
1886  <strong>a/poly_bind</strong> contains "<strong>let</strong> <em>a</em> == <em>b</em>(<em>c</em>, 
1887  <em>d</em>);"</p>
1888<p>Supposing <strong>h</strong> has been modified and we wish to remake <em>z</em>.
1889  The command
1890  </p>
1891<p> <em>make</em> &quot;z&quot;; </p>
1892<p>looks for a file "<em>z</em>" and examines its access permission. It discovers 
1893  that it is a directory and so tries to compile the file "<em>z</em>/poly_bind". 
1894  This contains the command </p>
1895<p> <strong>let</strong> <em>z</em> == <em>k</em>(<em>i</em>, <em>e</em>);</p>
1896<p> For each identifier in the command it looks up a file with that name in the 
1897  current directory and only if that fails does it treat it as an ordinary identifier. 
1898  <strong>k</strong> is a text file so it compares the time that it was last modified 
1899  (kept by the operating system) with the time on which an identifier called <em>k</em> 
1900  was last declared (kept by the Poly system). It sees that the file has not been 
1901  modified since <em>k</em> was declared so it uses that declaration. If it had 
1902  found that the file was newer it would recompile <strong>k</strong> (by a recursive 
1903  call to the compiler) before returning the newly compiled version. It does not 
1904  perform any other consistency checks relying on the type checking to ensure 
1905  that <em>k</em> really is a procedure which can correctly be applied to <em>i</em> 
1906  and <em>e</em>.</p>
1907<p>It next encounters <em>i</em> which it discovers is a directory and so it executes
1908  the file <strong>z/i/poly_bind</strong>.
1909  <strong>j</strong> is treated in the same way as <strong>k</strong>, but <strong>a</strong> is again a directory.
1910  It recurses again to process <strong>a</strong> and checks <strong>b</strong>, <strong>c</strong> and <strong>d</strong>.
1911  Finding that all these are text files and are up to date and that <em>a</em> 
1912  is
1913  newer than each of them, it concludes that <em>a</em> is up to date and uses 
1914  its
1915  current value without rebinding.</p>
1916<p><strong>e</strong>, being a directory, is processed in the same way as <strong>a</strong>.
1917  <em>f</em> and <em>g</em> are both found to be up to date, but <strong>h</strong> is not 
1918  found in the
1919  directory.
1920  The directories are regarded as nested blocks so that if a file is not found
1921  in the current directory the make program looks in the immediately enclosing
1922  one (i.e. the parent directory).
1923  It fails to find <strong>h</strong> in <strong>i</strong> and so tries <strong>z</strong>.
1924  There it finds the source text for <em>h</em> and discovers that it has been
1925  modified and must be recompiled.
1926  It recompiles it, returning the newly compiled <em>h</em> as its result.
1927  <em>e</em> must now be rebound so the declaration is executed and the new value
1928  returned as the result.</p>
1929<p>The next identifier in the declaration of <em>i</em> is <em>h</em> itself.
1930  The program remembers that <em>h</em> has been checked and uses the new
1931  value, rather than repeating the check on when the files were modified.
1932  It does this whether it has recompiled the file or just checked that it does
1933  not need recompiling.
1934  It executes the declaration of <em>i</em> because <em>e</em> and <em>h</em> have 
1935  been remade
1936  and returns this as its result.</p>
1937<p>In the declaration of <em>z</em> the next identifier is <em>e</em>.
1938  Again it uses the fact that <em>e</em> has been checked to save processing the
1939  declaration of <em>e</em> again.
1940  Finally it can rebind <em>z</em> and the construction is complete.
1941  If make is rerun immediately after this it will simply check everything and
1942  not rebind any of the files.</p>
1943<p>Note that each file must be "in scope" when it is required.
1944  Because <strong>h</strong> is used by both <strong>i</strong> and <strong>e</strong> it must be in the path to 
1945  both
1946  of them i.e. in the <strong>z</strong> directory.</p>
1947  
1948<h3>9.6. Persistent Storage</h3>
1949<p>The Poly system runs under a persistent storage system, that is any declarations 
1950  of identifiers or values in variables can be retained from one session to the 
1951  next on permanent storage. The database is held on a file and objects are read 
1952  in from it as required. Once read in they are retained in store until the end 
1953  of the session when those which are to be retained are written out again. The 
1954  criterion for writing something out to the database is whether it is reachable 
1955  from the root procedure which is the one used when Poly is started up. In the 
1956  normal Poly system this essentially means that any declarations made in the 
1957  global environment will be retained. When the user exits normally from Poly 
1958  all the reachable objects are written back and the database is updated. The 
1959  database can also be written back by executing the procedure <em>commit</em>();<em> 
1960  </em>which writes back the database and exits from Poly. It is currently not 
1961  possible to write the database and continue. </p>
1962<h3>9.7. history</h3>
1963
1964<p>The normal Poly system reads commands from the input stream, usually the
1965terminal, and compiles and executes them.
1966It also remembers the last few commands typed so that they can be re-executed
1967if necessary.
1968The commands in the table can be printed by the <em>history</em> procedure.
1969</p>
1970<p> <em>history</em>();</p>
1971<p>There are three procedures which execute commands from the history table. Each 
1972  command prints the command before executing it, and also enters the command 
1973  it will execute in the history table. The previous command can be executed by 
1974  the !!procedure. </p>
1975<p> !!(); </p>
1976<p>Another command can be executed using the <strong>!-</strong> procedure. It 
1977  has signature </p>
1978<p> !- : <strong>proc</strong> (<em>integer</em>) </p>
1979<p>The integer parameter is the number of the command counting back from the current 
1980  one, so </p>
1981<p> !- 1;</p>
1982<p> is equivalent to </p>
1983<p> !!(); </p>
1984<p> The third command <strong>!</strong> has signature </p>
1985<p> ! : <strong>proc</strong> (<em>string</em>)</p>
1986<p>The string is the first few characters of the command to be executed, so to 
1987  re-execute the last declaration, </p>
1988<p> ! "let";
1989<p>can be used. The command found is the first one whose characters match, working 
1990  from the last command back. 
1991</body>
1992</html>
1993