Upload
doandien
View
216
Download
3
Embed Size (px)
Citation preview
4
August 8, 196"3c.R.0.- 3
An Extension to ALGOL for Manipulating Formulae
by
A. J. Perlis
and
Renato Iturriaga
Computation Center, Carnegie Institute of Technology QI)'"
I. ALGOL is an elegant programming language. The concepts of procedures
and block structure permit straightforward composition of both large and
small programs of scientific computation in a natural and concise way.
The key to ALGOL's utility rests, however, with the sequential evalua-
tion of assignment statements; i.e., programming numerical evaluation
of formulae. Each executed evaluation produces a (new) value for some
declared variable. The data used is numeric, not only in representation,
but in intent.
There are some applications where the value of the "computation" is
shared between the manipulation and the evaluation of formulae. Although
such computations are customarily programmed in "list" languages [I_ ,
the numerical evaluation phases therein may be cumbersome to express and
inefficient to execute. Another solution has been to add list manipula-
tion procedures to a (numerically oriented) statement language _2J and
(1)The research reported here has been supported by the Advanced Resem:ch
Projects Agency of the Office of the Secretary of Defense (SD-Ih6).
and program by sequences of explicit procedure calls.
A third method will now be described. It is a proper extension
to ALGOL accomplished by three additions to the language: (i) the
fo____declaration, (ii) the operator eval, and (ill) certain logical
machinery attached to a relation inst. The explicit syntax is attached
as appendix A.
2. form declaration. The syntax of fo______is precisely that of the
t_ee present types (rea____l,integer, Boolean). The list of identifiers
declared as 'for____m'sare understood to take on expression forms as
values. Hence the class of possible values includes, in addition to
those specified in the ALGOL report [3] , an ordered set of expressions
(special case: a single expression). Thus, if f and g are two expres-
sions and x is of type form, the statement
x -(f+ g)* (f- g) (I)
causes the value of x to become the value of the right hand side
expression. Similarly, the statement
(b+V) (2)
where b is of type real (dynamically) assigns to f the value of the
expression on the right hand side with, however, b + 7 assigned its
(dynamic) value which is, of course, numerical.
-3-
After a simple variable has been declared of type form, it has
itself as value. By means of subsequently executed assigr_aent state-
ments it is possible to change its value. Subscripted variables do
not have any value upon being declared of type for___m;their value has
to be dynamically assigned by means of subsequent assigr_nent statements.
Variables declared of type form may have, during execution, (dynamic)
values of any type assigned to them.
Expressions are represented at runtime as (prefix)lists L4] and
the (internal) value of a form variable is the current address of its
assigned expression. Storage required for the expressions is taken,
as needed_ from an available space list. When uncommitted space becomes
insufficient to continue a computation, a "garbage collection" process
[A_ is invoked to return disconnected storage space.to the available
These processes are not part of the normal ALGOL block storage alloca-
tion. Normally an assig_nent statement_such as (2) above, does not
create new copies of the expressions involved. Thus if the state of
storage immediately preceeding execution of (2) is:
f: 0[_
b : 283
oi0
where 01 and /3 are addresses in the free storage area and ' I ' is
an internal word divider, after the execution of (2) memory will be:
-4-
In the ALGOL block and In the free storage area:storage area:
Here _ is the list terminal symbol, and c and f are locations fixed at
translate time.
The operation of a translator on encountering form variables is
straightforward. A first (syntactic) phase of the translation process
reduces the ALGOL text to a prefix or postfix forth and builds the sym-
bol table of declared identifiers. A second phase produces the working
code. Thus, in the case of (2), there is produced the postfix string
xf g*b 7+** (3)
A second phase, on scanning the operator '+', finds that f and g are
of type fo___rmand generates code to produce a list at _2(.,v_f_ , v M )
where v _t_ means 'value of t'. Next, code to compute b + 7 and its
storage in location c is created, since b is of type re____. Code is next
generated to produce a list at _ (*' _2' c) and, finally, code which
stores the location g in f. In case at least one of the operands of
-5-
a given operator is of type fo____,so is the result. If f is of type
fo____and b rea____l,f _-b is legitimate, creating a constant; but b _-f
is meaningless.
3. The formula operator eva____l.This operator is used to evaluate an
expression subject to a pattern of substitutions. For example, the
phrase
eva__l
causes the substitution of 5 for each occurrence of X in the expression
which is the current value of B [I,J] and the evaluation of the resul-
tant expression.
Consider a statement of the form:
D _-eval (XI, X2,...Xm) F (YI' Y2'"'Yn ) ($)
where n __ I, m>_ I. Then, it is the case that:
a) F must be a variable declared of type form; nevertheless at
execution time it may become any type.
b) If the current value of F is not an expression, then the effect
of (4) is precisely that of D _-F
c) If the current value of F is an expression, then D will have
the value obtained by the execution of (the piece of code produced by
the interpretation of) F as an arithmetic or Boolean expression, in
which were substituted Yi for all of the corresponding occurrences of Xi
-6-
in F, for all i __ m. The substitution is made in accordance with the
following rules : If
X. is not of type form, Xi is ignored; or if
Xi is of type __f°rm,but it does not occtw in F, X.l is ignored; or if
Xi is of type form, and it occurs in F, but i>n, Xi is ignored; or if
Xi is of type __f°rm,and it occurs in F, i __n, X.l is replaced by Yi"
d) Y. may be any expression of any type.l
e) The expression F is not changed as a consequence of the exe-
cution of eval.
f) D must be of type form, unless the evaluation of F produces a
numerical or a logical value.
The evaluation process creates a new expression which may ultimately
be of any type, depending on c_wrent values. Thus, for b of type real,
b _-eval (X) f (O) will yield an error if the right hand side is still
of type fo_____after the evaluation process is completed. For example,
suppose f is r + s + t where s, t are of type fo_____and r is of type
real. Then eval (t) f (6) produces the expression r + s + 6 rather
than v {(r+6)} + s. However eval (5,6)f (s,t)produces v {v {(r+5)]}+6}
which is of type real. More specifically, the commutative laws of addi-
tion and multiplication are not used in the present system during the
evaluation process. However the arithmetic of the system does automat-
ically employ the elementary simplifications given below. Here x is of
type fo______and a is rea____lor integer.
-7-
O+ X-*X X+ O-_X I * X-*X
0 - X-_-X X - O->X 1 T X-> I
O* X-_O X* 0-_0 X* I-_X
0 / X-_O X / 0 -_ Alarm X T I -_X
0 _X-,O X _0-,I X / I _ X
x + (-a) _ x - a X * (-I) -,-X
X- (-_)--* X+ _ (-I) * x--_-x
X V true -_ true true V X -_ true
X A true -_X true A X-@X
X A false -, false false _ X -, false
X V false -_X false v X -_ X
4. Patterns. The syntax of ALGOL's Boolean expression is extended
to permit formula expressions to be decomposed and their parts to be
labeled for the purpose of manipulation in subsequent computations.
This approach is much like that used in COMIT [5] •The value of
patterns is clearly seen in the ALGOL program for differentiation
which is attached in Appendix B. The line marked _ in the listing
should be read as stating:
If F is an instance of an element (which we label A) of a type
which is a member of D followed by + followed by an element
-8-
(which we label B) of a type which is a member of Dj then
DERV becomes DERV (element labeled A, X) + DERV (eleraent
labeled B, X) else
A pattern permits the asking of questions about the configuration
of an expression; and at the same time, if the answer to the questions
is affirmative, the defining of labels which have as values the differ-
ent components of the expressions. Beyond the pattern (but in the same
block) the defined labels may be used as operands, thus permitting
operations on the subexpressions. In the present system, the questions
which may be put are:
a) What are the operators of the expressions?
b) What are the types of the operands?
c) Are the operands members of a defined set of symbols?
In order to accomplish the above, two "type predicates" and a
definition scheme for classes of types and/or symbols are introduced.
The full syntax is in the appendix.
Examples:
a) The predicate "F is real" will have the value true if the
current value of F is real, false otherwise. Tlls predicate is only
useful if F is declared of type fo_ because its type is then possibly
dynamic. The assignment statement
De-_real, integer, BooleanJ
-9-
will define D as a class which contains the types real, _,
Boolean. D must itself be declared of type fo____.
b) The predicate "F of (D)" will have the value tru___eif the
current type of F "i__ss"one of the elements of D, namely, (in this
case) real, integer, Boolean. The predicate will be false otherwise.
The predicate "of ( )" is thus a disjunction of 'i__s''s.
It is possible to use any of these predicates without any pattern
as a Boolean primary.
A label used must_ of course_ be declared and of the same type as
the quantity it defines. Its maximal scope is the block in which it
is declared.
A pattern is a Boolean primary with the following two properties:
a) By definition it has always a logical value.
b) If the value is tru____e,all the labels used in the pattern are
defined. If the value is false , the labels are undefined. (Caution:
if a label is defined because some pattern was true, and the same
label occurs in another pattern in the same block and this pattern is
executed and turns out to be false , the label will lose its previous
value and become actually undefined.
The precise circumstances under which, in a pattern, a label is assigned
a value are somewhat involved. Suffice it to say that, in a conjunction,
only those terms which are true (ordered from the left up to the first
false term) have their labels assigned; while, in a disjunction, only the
-10-
first term (ordered from the left) which is true has its labels
assigned. Hierarchy of Boolean operations naturally also determines
the order in which terms are tested, and hence labels assignable.
Furthermore a label once assigned can be used in any subsequent pattern
as determined by sequencing; e.g., in the next term to the right connected
by the operator /%.
The evaluation of patterns can best be imderstood by exmnple. In
the following the evaluation is specified in terms of ALGOL segments.
.thFj , j = I, 2, 3 names the j-- element of an expression, F, organized
in prefix form. Thus FI is the operator and F2 and F3 the operands.
F inst -_ B: form means
if FI = '-,' then
begin B +-F2; G _-F2 is form end
else G _- false ;
F inst A:OF(D) + B: real means
A 4- F2;
if F2 of (D) then
begin if FI = '+' the____n
begin B _-F3; Ge-F3 is real end
else G _-false end G _- false
where the value of the primaries is taken to be that of G.
5. The Evans Model. These extensions to ALGOL were programmed into
an interpreter version of the Carnegie ALC_DL system in about I.5 man
months. This rapidity is due to an effective programming tool devel-
oped by Arth_m Evans_ Jr. of the Carnegie Tech Computation Center.
This tool consists of: (i) A progr_n for analyzing (input strings
converted to postfix output) ALGOL like languages, the analysis being
parametrized in terms of an input set of productions [6] ; and (ii) a
program which accepts as inputs a set of productions expressed in
symbolic form and translates them into an assembled set of programs
which execute the effect of the productions.
Thus extensions to the lang_mge may be rapidly organized and
tested. The postfix output is printed along with the ALGOL text in
the examples included in this paper.
Bibliography
I. Slagle, James R.: "A Heuristic Program That Solves Symbolic
Integration Problems in Fresl_an Calculus", (SAINT),
Lincoln Laboratories, M.I.T., Lexington, Mass., 5G-0001(May 1961), 110 pages.
2. Celernter, H., Hansen, J.R., and Gerberich, C.L.: "A Fortran-
compiled List-processing Language" Journal of the ACM, 7,87-101 (April 1960).
3. Naur, Peter (Editor): "Revised Report on the Algorithmic
Language ALGOL 60" Commm_cations of the ACM, 6, I 17(January 1963).
_. McCarthy, J. : "Recursive Functions of Symbolic F_ressions and
Their Computation by Machine, Comm_nmications of the ACM, _,18_-195 (April 1960).
5. C_T Programmer's Reference Manual. The M.I.T. Press, Cambridge,Massachusetts, 1962, 61 pages.
6. Floyd, Robert W.: "A Descriptive Language for Syn_ol Manipulation",
Journal of the ACM, _, 579-58& (October 1961)
7. Milne-Thompson, L.M.: Th__seCalculus of Finit______eDifferences, Macmillan,(1933), Page 119.
8. Whittaker, E.T. and Watson, G. N.: Modern Analysis, Cambridge Press(1952) Page 133.
Appendix A: Syntax Addendum to ALGOL
Note: The section numbers refer to those in reference [3]. The
notation <the same as in the report> refers to syntax types defined
in reference [3].
In 2.3. - Delimiters :
<operator>: := <the same as in the report> I<formulae operator>
<formulae operator>: := eva___lIi_ssI
<arithmetic operator>: := <the same as in the report> ,I(_')abs Isin cos Isqrt I
arc t Iin lexpllsig_
<declaration>: := <the same as in the report> Iform
<separator>::= <the same as in the report>linst
In 3.3.1. - Arithmetic Expressions:
<primary>: := <the same as in the report> I <evaluated formula>
<formula identifier>: := <variable>
<evaluated formula>::= eva__l(<formal parameter list>)<formula identifier>
(<actual parameter llst>)
In 3._.2. - Boolean Expressions:
primary>: := <the same as in the report>l <type predicate>l <pattern><Boolean
<class name>: :- <variable>
<type predicate>: := <variable>is__<type> I <variable>off(<class name>)
<pattern primary>: := <unlabeled pattern primary> I<variable>:<unlabeledpattern primary>
(1.) is used as a single character representation for 'entier'.
<unlabeled pattern primary>: :=<type> I of(<class name>)
<'.unaryoperator>: := -I &labsl sinl c°sl sqrtl arctl lnl explsignl _
<binary operator>::= +I-I*I/I_I>I -_<I =I-_> I< I_I^I v
<simple pattern term>: := <pattern primaryD_binary operatorXpattern primary> I<unary operatorXpattern primary>
<pattern term>: := <simple pattern term> I<if clauseXsimple pattern term><pattern term>
<simple pattern>: := <variable>inst<pattern term>
<pattern>: :=<simple pattern>l <if clauseXsimple pattern>else<pattern>
In A.2.1. - Assignment Statements:
<assignment statement>::= <the same as in the report> I<left part list><class>
<class element>::= <basic symbol>l<identifier> I<number>
<class list>::= <class element>l<class list>,<class element>
<class>:-= [<class list>]
In 5.1. - Type Declaration:
<type>::= <the same as in the report> Ifo_
IIPOv
v-1
s-,
r_3
d.
X [:3
t ,_ LU
4, +
4- x
o
4- I 4_ ""
_ LLJ
_.....jo <:_, .t...i ti.,_ <::_ r_ r,_.,_.,, _ ,,.,, It _ _all Ill 4- , _ _._ , _ _-.-+ el 1'-_) LL)
Oc_ c._ rl kO, c:_ _'0 _. ,_ x -I- X * ,-4 4- 4, (I g
#-.- • • • • . • • • •
X n It n n n ii it It II It o II _ It _ It H II Ll_ It ¢ It I_ n It _D _[ C
£1D(IDl_ru or, l_D OO I_D IBro _r,c£, ._[ .,_ ._i .el ._ _I I]L_ _ IIl I OD _ (3[ e Q, El.! _
W
_ U
,,, L/3 J L),_ .._ _r4 ..J LU I1"
UJ -o _ _
Z _" O: _ ILl
Z L_ I IlJ L_
U.! _--_ _ LU Z_-I 0 UI _.1 -I: Z7 IJJ "1"I 1- I.- d.J ]"
.
I.J- l._ CJ _- LL GO
........ C) "" ;J r_
C_ LI.J I---" ',_ ,H .
.. = _ _, _ _ , ._,-1" 2" _. ClC -_ _l ,_ C'_ ,_(/3 r._ <1( _ Ll: _ CD *-- I
p-,-- _ o-, _ ._ J .., v
, in tr, tn uh ,,_ _) ..o ,,o ._m.4)
).-
,g_z_ o .J
o
c_
.. _ r_ _
oo _ _ _ _ _ o_
c uJ .,4 .. ifl _oz &
-- cJ c) z c N '.U _ -_
c: ./) L) _" -+) "" _j "" _'k_
c) c> ._J .u +_ c_ L,+._
I.++t.,. +<:t" _ -- JJ ]- '_ LC.LL+ c_
I
or -_" • t_ t_ I- (n t_ c_
%
LW
0 _00 000 O(_OZ O0 0 0 O0 0000 0
ooE)
CJL ULI
0 0 _ X "' 0', _L
L) Z Z + Z l:3 c_ Z,,. {]L (21 Z3 _'3 -. (:3
_, 0 '-" _ Z OL _ Z I-- L_ I----" F- I--- _:) LIJ _-, _ C)_ I--- -,_ _ LL_ LLI
_.: L_ _ 5.5 _ ' rj_ "_ L.; I--- _ _ U'J LL' LI.J ,.._..l _ \ _ I Z _. (___'l_l"
CL _i- L3 ,I, ,r-I _ _ _ OL i--- 03 l--Iv3 -., OL 0.. LU 0 • i n C_ _ I-- Q• LIU LU I--- Z -- -- 0 _ I.ILJ r,.J tui.J {J) 0-->( Or• r..) (i) -- t/) -- , _ z , c_:(x_ -- .- o .-;. c_c_
uJ'_ "-," o* (1 C) _ 0 _ Z _ -'3 L.} "-) o,_ 2" & 0 _ 41 -,_ I:_ 2: I----,_il ,"*.Z _ _ _ _ _ (#I X Z -'._ 0 ;(7(3L :X Cl ,,, .& (.) a; [3 -._ & _ ,It, l bO, (P) _ >- & ,L,) L._ UU n: _:" LL_
Z Z_E Z Z _ -,_Z ,'--' Z'--,4, _L_.. _ _, _ .--- ,,I, ('E" ,_ 4, _ & _ L-'3.--. _ L"_L (_ C.l[" l, & _l:_
CLO Ill,-- IJ. _ 1.1. I1 bOLl. _D )- IJ_ )- br} LIJ n (_. LL _ i.l- )- OC OL Ill