POP 2 Reference Manual
User Manual:
Open the PDF directly: View PDF
.
Page Count: 42
| Download | |
| Open PDF In Browser | View PDF |
POP-2
REFERENCE
MANUAL
BY
R. M. BURSTALL
AND
R. J. POPPLESTONE
DEPARTMENT OF MACHINE INTELLIGENCE AND PERCEPTION
UNIVERSITY OF EDINBURGH
CONTENTS
PAGE
1 INTRODUCTION
1.1 Aims
1.2 Main features
1.3 Examples
1.4 Notation for syntactic description
1.5 Notation for functions
209
209
210
212
213
2 ITEMS
2.1 Simple and compound items
2.2 Integers
2.3 Reals
2.4 Truth values
2.5 Undefined
2.6 Terminator
214
215
215
216
216
216
3 VARIABLES
3.1 Identifiers
3.2 Declaration and initialisation
3.3 Cancellation
216
217
219
4 FUNCTIONS
4.1 Definition of functions
4.2 Application of functions
4.3 Nonlocal variables
4.4 Partial application
4.5 Doublets
4.6 Arithmetic operations
219
220
221
221
223
224
5 EXPRESSIONS AND STATEMENTS
5.1 Expressions
5.2 Precedence
5.3 Statements and imperatives
5.4 Labels and goto statements
224
226
226
227
207
CONTENTS
PAGE
228
229
5.5 Assignment
5.6 Comments
6 CONDITIONALS
6.1 Conditional expressions
6.2 Conjunctions and disjunctions
229
230
7 DATA STRUCTURES
7.1 Functions of data structures
7.2 Records
7.3 Strips
7.4 Garbage collection
231
233
234
235
8 STANDARD STRUCTURES
8.1 References
8.2 Pairs
8.3 Lists
8.4 Full strips and character strips
8.5 Arrays
8.6 Words
8.7 Functions
236
236
236
239
239
240
241
9 INPUT AND OUTPUT
9.1 Input
9.2 Output
242
243
10 MACHINE CODE
244
11 MODES OF EVALUATION
11.1 Immediate,evaluation
11.2 Macros
11.3 Evaluation of program text
244
244
245
245
Acknowledgments
208
1.
INTRODUCTION
1.1. Aims
The following are the main design objectives for the POP-2 language:
(i) The language should allow convenient manipulation of a variety of
data structures and give powerful facilities for defining new functions over
them.
(ii) The language should be suitable for taking advantage of on-line use
at a console, i.e. it should allow immediate execution of statements and
should have a sufficiently simple syntax to avoid frequent typing errors.
(iii) A compiler and operating system should be easy to write and should
not occupy much storage.
(iv) The elementary features of the language ,should be easy to learn and
use.
(v) The language should be sufficiently self-consistent and economical in
structure to allow it to incorporate new facilities when extensions are desired.
In attaining these objectives certain other desirable features of programming languages had to be relegated to secondary importance:
(vi) Fast arithmetical facilities on integer and real numbers.
(vii) Fast subscripting of arrays.
(viii) A wide variety of elegant syntactic forms.
Naturally whether (iii) or (vi) and (vii) are attained is to a considerable
extent a matter of implementation.
1.2. Main features
The following main features are provided. Roughly analogous features of
some other programming languages are mentioned in brackets as a guide:
(i) Variables (cf. ALGOL but no types#at compile time).
(ii) Constants (cf. ALGOL numeric and string constants, LISP atoms and list
constants).
209
PROBLEM-ORIENTED LANGUAGES
(iii) Expressions and statements (cf. ALGOL).
(iv) Assignment (cf. ALGOL, also CPL left-hand functions).
(v) Conditionals, jumps and labels (cf. ALGOL but restrictions on jumps
and labels).
(vi) Functions (cf. ALGOL procedures but no call by name, cf. CPL and
'swim for full manipulation of functions).
(vii) Arrays (cf. ALGOL; cf. CPL for full manipulation of arrays).
(viii) Records (cf. COBOL, PL/l, Wirth-Hoare ALGOL records, CPL nodes).
(ix) Words (cf. LISP atoms).
(x) Lists (cf. LISP, IPL-V).
(xi) Macros.
(xii) Use of compiler during running (cf. LISP, TRAC, FORMULA ALGOL).
(Xiii) Immediate execution (cf. JOSS, TRAC).
Notes:
LISP 1.5
See Barron, D. W.,etal. 1964. The main features of CPL, Computer
J., 6, 134-43.
CPL reference manual. Edited C. Strachey (privately circulated).
Wirth-Hoare ALGOL: See Wirth, N., and Hoare, C. A. R. 1966. A contribution to the development of Algol, Communs Assn Comput. Mach.,
9, 413-32.
mc:See Mooers, C. N. 1966. TRAC, a procedure describing language
for the reactive typewriter, Communs Assn Comput. Mach., 9, 215-24.
iswm: See Landin, P. J. 1966. The next 700 programming languages,
Communs Assn Comput. Mach., 9, 157-166.
LISP:
CPL:
1.3. Examples
The following is an example of pop-2 program text. The sign (not to be
confused with that used in section 1.5 'Notation for functions') prints out
some results on a newline prefixed with two asterisks. These results are
included in the text below, as they would appear if the program were run
on-line at a console.
comment arithmetic;
12.0+ 2.5*(1.5 -I- 2.5).
**22.0
vars a b sum;
2*2—+a; 3*a—*b; a*a+b*b—osum;sum
**160
function sumsq x y;
x*x+y*y
end;
sumsq(a, b)+1
**161
210
BURSTALL AND POPPLESTONE
function fact n; vars p;
1-0p;
loop: if n=0 then p else n*p-op; n
end;
fact(fact(3))
** 720
goto loop close
comment arrays;
vars a if;
10-31; 20-tj;
newarray([%1, i, 1,j%], sumsq)-4a;
a(2,
** 13
10-■a(2, 3); a(2,
** 10
function arraysum al a2 m n;
newarray ([%1, m, 1, n%], lambda if; al(i,j)+a2(i,j) end)
end;
arrays= (a, a, 10, 20)-oa; a(2,
** 20
comment lists;
vars u;
1-41; 2--tj;
[%i, i+j, "dog", "cat" %]-'u;
** [1 3 dog cat]
cons("pig", u)** [pig 1 3 dog cat]
function appencla y;
if null(y) then[% x Y.] else cons(hd(y), append(x, tl(y))) close
end;
append(4,[% 1, 1+1, 3%])=
** [1 2 3 4]
comment records;
vars consper destperforename surname male pl p2;
recordfns("person", 500,[0 0 1])-.male-'surname-'forename
-0destper-■ consper;
comper("jane", "Jones",false)-opl; consper("sam", "smith", true)-y2;
surname(p1)
**,/ones
datalist(p1)=.
** [Jane Jones 0]
routine marry x y;
if male(x) and not(male(y)) then surname(x)--Psurname(y) close
211
PROBLEM-ORIENTED LANGUAGES
end;
marry(p2, pl); datalist(p1)
** [jane smith 0]
1.4. Notation for syntactic description
We use the
BNF (Backus-Naur
Form)notation as used in the ALGOL report:
= indicates a syntax definition;
< > are used to enclose the name of a syntax class;
I denotes disjunction (union of syntax classes).
Concatenation denotes concatenation of any elements of two syntax
classes.
We also use a convenient extension of this notation due to R. A. Brooker:
* means that a class may occur n times, n> 1;
? means that a class may occur n times, n=0 or 1;
*7 means that a class may occur n times, n 0,
e.g. the definitions
::= I
::=
::= I
may be replaced by
::=
::=
The characters <, > and * are used in the pop-2 reference language but no
confusion should arise.
When we wish to give examples of a syntax class we use the symbol
'e.g.::=',for example.:
e.g.::= I
The character set of the pop-2 reference language is as follows.
::=alblcIdlelI1011j1kIllmInlolplqirlsItluIvIwIxlyiz
::=0I11213141516171819
::= +I-14,111$18d=1l:ILIT
::=,1;
::=.
::=10
::=(1)I[1]
::= %
="
::= /I\
212
BURSTALL AND POPPLESTONE
Letters may be written in lower case, upper case or heavy type without any
change of meaning. It will be conventional however to use heavy type letters
for syntax words, i.e. those identifiers such as function, then, end and cancel
which have a special meaning for the pop-2 compiler and which characterise
certain syntactic forms.
Spaces,tabulate and new lines terminte dentifiers,integers,reals and words
but otherwise they are ignored.
A distinction is made between the reference language used in this document
and a number of possible hardware languages used by particular computer
implementations of Pop-2. Each character in the reference language should
be represented by a distinct character or sequence of characters in the hardware language. A particular letter, whether upper case, lower case, heavy
type or not is regarded as the same character in the reference language.
The symbols and used in this paper should be read as a typographical
abbreviation for the pairs of characters —> (minus greater than) and
= > (equals greater than) respectively.
1.5. Notation for functions
It is convenient to have a notation to specify the domain and range of functions. We will consider functions having several arguments (or possibly
none)and producing several results(or possibly none),the notion offunctions
with more than one result being an extension of normal mathematical usage
(see section 4.2 'Application of functions'). We introduce a special symbol
which is not to be confused with any identifier in the pop-2 language.
Suppose dl, d2, ,dm and r 1, r2, ,rn are all sets of items. Then
dl, d2, ,
r2, ,rn is the set of all functions whose domain is
dl, d2, ,dm and range r 1,r2, • ,rn,i.e. with arguments which are m-tuples
in dl x d2 x x din and with results which are n-tuples in r 1 x r2 x rn. We
express the fact that a functionf is a member of this set of functions by
fe dl, d2, ,dm rl, r2, ,rn
Some examples will make this clear.
add E integer, integer integer
divreni E integer, integer integer, integer
where divrem is 'divide with remainder', e.g. divrem (7, 3)=2, 1 and divrem
(14, 4)=3, 2
roundup e real
prime e integer
integer
truthvalue
If the function has no results we use an empty pair of parentheses, thus:
printout e integer
The arguments or results#20
may themselves be functions
differentiate e (real
real)
(real real)
213
PROBLEM-ORIENTED LANGUAGES
Where we wish to discuss a number offunctions all having the same domain
and range it is convenient to abbreviate thus:
f, g,
h all e
for
f
and
ge
and
he
Some functions do not have a fixed number of arguments and some do
not have a fixed number of results (see section 4.2'Application of functions').
In such cases we may write for example
i.e integer
real, integer, ... ,integer
for the domain or range, meaning that a real and a variable number ofintegers
are the results.
2.
ITEMS
2.1. Simple and compound items
The objects on which one can operate are called Items. They are divided into
two distinct classes: Compound items, which are represented by addresses and
Simple items which are directly represented by bitstrings which do not contain addresses (these bit strings are normally of fixed length for a given implementation, being a single machine word). The address representing a compound item points to a bit string whose length may vary from item to item.
This bit string may contain other items. The areas of store immediately
pointed to by two different compound items do not overlap.
The following standard function recognises compound items:
iscompnd e item
truthvalue
Two kinds of simple item are distinguished: integers and reals. The
following standard functions recognise them:
isinteger, isreal all e item
truth value
The standard function = (an operation of precedence 7)is used to represent
equality of items. For integers and reals it has the usual meaning. Its
meaning for compound items is given in section 7.1 'Functions of data
structures'.
= e item, item
truthvalue
214
BURSTALL AND POPPLESTONE
2.2. Integers
Integers are simple items. They may be positive, negative or zero. The size
of the largest and smallest integers allowed depends on the implementation.
The#following functions on integers are standard:
intadd, intsub, intmult, all E integer, integer integer
// e integer, integer integer, integer
intplus, intminus all e integer integer.
intsign E integer integer
intgr, mile, intgreq, intleeq all E integer, integer truthvalue
intadd, intsub, intmult and intdiv are the usual add, subtract and multiply.
// is divide with remainder and produces a quotient and a remainder (if al/b
is (q, r), then q*b+ r =a and 0.4 r::=ll
::= 8:
::= 2:binary#2digit*>
::=
::=011121314151617
::= 011
Example:
e.g.::= 8:77712:1011016559
Integers may also be treated as bit-strings (the length depending on the
implementation) and the following functions are standard:
logand, logor, logshift all e integer, integer integer
lognot e integer integer
logand and logor are the usual bit by bit 'and' and 'inclusive or'; logs/ft
causes the first integer to be shifted left by the number of places given in the
second, unless the second integer is negative when shifting to the right takes
place (all new bits to fill up the end are zero in each case).
2.3. Reals
Reals are simple items. They may be positive, negative or zero. The size of
the largest and smallest reals allowed and the precision depends on the implementation. The following functions on reals are standard:
realadd, realsub, realmult, realdiv all e real, real real
realplus, realminus all e real real
realsign e real integer
realgr, realle, realgreq, realleeq all e real, real truth value
215
PROBLEM-ORIENTED LANGUAGES
These are the usual add, subtract, multiply and divide on reals. realplus
carries a real into itself and realminus complements a real. realsign produces
—1,0 or +1 according to the sign of the real. The remaining four functions
are the relations 'greater than', 'less than', 'greater than or equal to' and
'less than or equal to'.
There are also operations to convert a real to the nearest integer and to
convert an integer to real:
intofe real integer
realofe integer real
The syntax of reals is as follows:
::=.
::=10+110— I io
Example:
e.g.::=.511.9911.510-6
2.4. Truth values
The two items True which is the integer 1 and False which is the integer 0 are
called Truthvalues.
•
•
On entry#to the pop-2 system the standard variable true is set to 1 and the
standard variable false is set to 0. The following standard functions on
truthvalues are provided:
hooland, boolor all e truthvalue, truthvalue truthvalue
not e truthvalue truthvalue
These are the usual functions'and','inclusive or' and 'not' of propositiona
calculus.
2.5. Undefined
The standard variable undef has the word "under as its value on entry to
the pop-2 system (see section 8.6 Words'). The programmer may use it as
the result of a function which fails to produce its normal result.
2.6. Terminator
The standard variable termin has the word "termini' as its value on entry to the
,pop-2 system (see section 8.6 Words'). It may be used as the first argument
of a variadic function (see section 4.2 'Application of functions') or to mark
the end of an input file (see section 9.1 'Input').
3.
VARIABLES
3.1. Identifiers
An item may be the Value of a Variable (a variable is not itself an item).
An Identifier is associated with the variable and this identifier is used to
216
BURSTALL AND POPPLESTONE
refer to it in a pop-2 program. A number of distinct variables may have
the same identifier, but only one of them is Currently associated with it at a
particular time in the evaluation process.
An identifier may be restricted to a certain range of values and it may be
given special syntactic properties by being given a precedence (see section 5.2
'Precedence').
The syntax of identifiers is:
::= I
::= I
Example:
gdentifier>e.g. ::=x I y99 I alpha I u2a I +++ 11+ I 0 I * $ $ *
Syntax words such as then, end, and : have special meanings and may
not be used as identifiers. Only the first 8 characters are significant.
3.2. Declaration and initialisation
A variable is either Global, Local or Formal. A Declaration is used to
introduce an identifier and associate it with a global or local variable. A
Local Declaration, introducing a local variable, is a declaration which occurs
in a function body. A Global Declaration, introducing a global variable, is
one which does not.
An Initialisation is used to introduce an identifier and associate it with a
formal variable and give the variable an initial value. It is achieved by
including the identifier in the formal parameter list of a function (see section
4.1 'Definition of functions').
A declaration or initialisation may also specify that the identifier is restricted to take only functions as values. This is not necessary but may make the
implementation more efficient. A declaration or initialisation may also
specify that the identifier is an Operation, i.e. it is restricted to take functions
as its values and is given a precedence. This restriction is associated with
the unique name(see below)produced by the declaration or the initialisation.
The syntax of declarations is:
::=vars
::= I
::= I ()
::= function I operation
Example:
e.g.::=vars x y I vars x y function(fg)operation 7 ==
A declaration or initialisation has a Scope, which is a piece of pop-2 text.
An identifier may not be used to represent a variable outside the scope of a
declaration or initialisation of the identifier.
217
PROBLEM-ORIENTED LANGUAGES
The scope of a global declaration starts at the declaration and continues
until the identifier is cancelled.
The scope of a local declaration starts at the declaration and continues to
the end of the innermost function body enclosing it.
The scope of an initialisation is the body of the function in which it occurs.
Each declaration or initialisation gives rise to a unique mark and this
mark is associated with all occurrences of any identifier introduced by the
declaration or initialisation within the scope of the declaration or initialisation. An identifier together with its unique mark is called a Unique name.
Thus an identifier which occurs in more than one declaration or initialisation corresponds to more than one unique name.
The generation of fresh unique names for identifiers can be suppressed by
using the standard routines:
nonunique, unique all e 0
If nonunique is applied, all declarations or initialisations of a given identifier
until unique is applied will give rise to the same unique name. This may save
storage space and can be used when no confusion is liable to occur.
To sum up:
A new identifier is introduced by introducing a fresh sequence of
characters.
A new unique name is introduced by#each declaration or initialisation
(unless nonunique has been applied).
A new variable is introduced by each dynamic activation of a declaration
or initialisation.
A variable has an Extent which is a sequence of evaluations of expressions
and statements.
The extent of a global variable starts from its declaration and continues
indefinitely.
The extent of a local or formal variable starts on entry#to the body of the
function in which it is declared or initialised and continues until exit from the
body. During this extent the extent of any other variable with the same
unique name is temporarily interrupted. This is called a Hole in the Extent
of the other variable. Its value is not altered but it cannot be accessed or
changed by assignment. Thus there is only one variable Currently Associated
with a particular unique name during any evaluation. Other variables
associated with the unique name are in abeyance.
• More than one global declaration of the same identifier is not permitted
unless a cancellation of it intervenes in the text.
Similarly a declaration of a local variable is not permitted if there is already
a declaration of a local or initialisation of a formal with the same identifier
for the same function body.
A Standard Variable is a global variable which already has a value on entry
to the pop-2 system. A Standard Function (or Routine) is one which is the
value of a standard variable. Certain standard variables are Protected, i.e.
no assignment may be made to them.
218
BURSTALL AND POPPLESTONE
3.3. Cancellation
A cancellation terminates the scope of any declaration of an identifier and
removes the effect of any restrictions placed upon the identifier. The
cancellation must occur textually between the old declaration and any new
declaration. It may not occur in a function body.
The syntax of cancellations is:
::= cancel
4.
FUNCTIONS
4.1. Definition of functions
A Function is a compound item. Definition and application of functions are
treated in this section and the next. Certain properties of a function regarded
as a data structure are treated in section 8.7 'Functions'.
A function consists of a Formal Parameter List which is a list of identifiers
offormal variables, possibly an output local list which is a list of the identifiers
of output local variables (see section 4.2 'Application of functions') and a
Body which is an imperative sequence (see section 5.3 'Statements and
imperatives').
A function which produces no results (see section 4.2 'Application of
functions') is called a Routine.
Functions may be referred to in the program by using a function constant,
called a Lambda Expression, or they may be standard functions provided by
the pop-2 system, or they may be created by partial application or by application of a standard function which produces a function as a result.
The syntactic representation of a function constant is:
(formal parameter list element>::= I
'(formal parameter list>::=