A Development of APL2 Syntax
Total Page:16
File Type:pdf, Size:1020Kb
A development by James A. Brown of APL2 syntax This paper develops the rules governing the When language extensions are proposed we find that the writing of APL2 expressions and discusses the familiar rules do not cover all cases. For example, A PL 1 principles that motivated design decisions. has the concept of an operator (for example, /) applying to a function (for example, +) and producing a new function (called summation). In APL 2 operators are generalized so 1. introduction that they can apply to all functions—including those IBM has numerous products which follow the IBM internal produced by other operators. Therefore a syntactic decision standard for 4PL {VSAPL, APLSV, PC APL, SlOO has to be made about the meaning of a statement like A PL). In this paper this level of the A PL language is referred to as A PL 1. + .X/ APL2 is based on this writer's Ph.D. thesis [1], the array theory of Trenchard More [2], and most of all on APLl. It (where " . " is the matrix product operator). This could incorporates extensions to data structures, to primitive mean either operations, and to syntax. Those wishing a complete ( + .x)/or + .(x/) description of A PL 2 may refer to the A PL 2 publications library listed in the general references. The only extensions Operators are extended so that they take arrays as covered here are those which have an effect on the syntax of operands. Therefore, if "Z)OP" is a dyadic operator taking the language and those implied by the simplifications of an array right operand, syntax. + DOP A^B A presentation of syntax would be brief This is, rather, a discussion of how the syntax was designed and what could mean motivated the choices that were made. A longer discussion {+DOPA)^B or +DOP{A^B) of this and related topics appears in "The Principles of >1PL2"[3]. and the rules of A PL 1 are of no help. In either case x gets evaluated before DOP. The only question is "does x have 2. The objectives of APL2 syntax one or two arguments?" A PL 1 has always had a simple syntax governed by only a These questions and others like them could have been few rules. These rules are phrased in terms of general resolved by stipulating new rules that cover the cases statements that are easy to apply in practice: "All functions followed by a determination that no syntactic ambiguity was have equal precedence"; "functions are executed from right introduced. Instead APL 2 uses the concept of binding to left"; "operators have higher precedence than functions"; strength, which brings together in one measure all the etc. concepts of syntax—order of execution; precedence of operators over functions; building lists of arrays; etc. "Copyright 1985 by Internationa) Business Machines Corporation. It is well known that any precedence grammar can be Copying in pMinted form for private use is permitted without described by a matrix [4, 5]. This is made easier in APL 2 payment of royalty provided that (1) each reproduction is done without alteration and (2) the Journal reference and IBM copyright (and even A PL 1) because it has a small number of object notice are included on the first page. The title and abstract, but no classes. Bunda and Gerth [6] give a development of such a other portions, of this paper may be copied or distributed royalty matrix as a model for evaluating various extensions to A PL free without further permission by computer-based and other information-service systems. Permission to republish any other syntax. However, even a small matrix can be hard to portion of this paper must be obtained from the Editor. remember and apply in practice. Benkard [7] shows that 37 IBM J. RES. DEVELOP. VOL. 29 NO. 1 JANUARY 1985 JAMES A. BROWN A PL 2 can be simplified to a linear hierarchy of syntax but is not associated with an object in most implementations classes. It is this concept that is developed here. because the number is not representable. A given numeric object may be associated with many 3. The objects of APL2 names. For example, the number "fifteen" can be written APL 2 recognizes three classes of objects: arrays, functions, 15 or 15 . 0 or 1 . 5£'l or 15</0. and operators. It is this set of objects and the operations defined on them that must be expressed by the syntax. Since Character scalars are written by enclosing the graphic the kind of data we want to represent is already known and associated with the character in single quotation marks. the style of the syntax is already given, it is not a surprise that choices are made that make the syntax and the universe of objects work together. This is a single character and is treated as an indivisible Thus we chose arrays that are finite, rectangular unit despite the fact that on input it occupies three print collections of other arrays ultimately comprised of numbers positions. The use of the quotes means it is always possible and characters. We chose operations (functions and to distinguish between a number which is represented by a operators) which may take at most two arguments and single digit and the character whose graphic is that digit. which therefore can be easily represented by an infix notation. Primitive operation names Primitive operations are named by single symbols each of 4. Names which occupies one print position. The first step in developing the rules for syntax is There is a large set of primitive functions using the establishing the rules for identifying the objects of discourse symbols in the written notation. + -X** I rL?®o:i~A A name is a string of one or more characters which is, or may be, associated with an A PL 2 object. Some names are V'rvv<< = ^>3:»p DC , <t)^ + + always associated with the same object; others may not be associated with objects at all or may be associated with different objects at ditferent times. There are only a few primitive operators using the symbols Names are considered atomic, indivisible units of writing . / \/ \" even when they take more than one character to represent. Once names are identified, they are considered the tokens Note that dot (.) is an ambiguous symbol used as a decimal of the syntax and their structure is never again of interest. point as well as an operator. Which is intended in any instance is determined from context. • Primitive names Primitive names are those that are defined as part of the • Constructed names definition of the language. They have fixed associations in Constructed names are strings of one or more characters that a given primitive name is always associated with the with the following constraints: same object. An initial or only character is from the set ABC. .JYZAD Primitive array names ABC. .XIZL A PL 2 arrays are collections of numbers and characters. The primitive arrays (the ones given names) are single and remaining characters (if any) are from the set above numbers and single characters (that is. simple scalars). (excluding quad) along with Numeric scalars are written using their decimal 0123456789"_ representations. Complete rules for writing numbers may be found in [8]. Here are examples of various styles of numbers: User names 245. 5 2J3 User names follow the above rules except that the initial "245. 5 2£)i45 character may not be D. Any name constructed according to 2. 35£'13 IRl.716 these rules is valid (no length limitation), and none has any value (i.e.. none is associated with an object) until some (The second column shows three ways to write complex action is taken to specify the association. User names may be numbers.) associated with any class o(APL2 object. While any decimal number may be written, in an Arrays and user names are associated through use of the implementation not all are associated with a scalar object. specification arrow (•<-), through parameter substitution 38 For example. 2£'9 8 7 6 5 4 is a legal name for a number caused by invoking a defined operation, and as an implicit JAMES A BROWN IBM J, Ri;S DEVELOP VOL. 29 NO I JANUARY 1985 result of the DTF function. A name which is associated with • Operators have higher binding power than functions. an array is called a variable. Functions and operators are associated with user names as The syntax oi APL 2 must be able to express an implicit result of the DFX and DTF functions. Functions may also be associated with user names through • Arrays, parameter substitution in a defined operator. Thus a user • Functions and their application to arguments. name may indicate the same function as a primitive • Operators and their application to operands. function or even a derived function. The linear collection of special symbols and names Distinguished names (primitive and constructed) used to write arrays, functions Names which begin with the character D are reserved for and their application to arguments, and operators and their fixed uses in the language and are called distinguished application to operands is called an expression. names. Any distinguished name is valid, but only a few are The names and symbols used to write an expression are associated with objects. Distinguished names associated with divided into six syntax classes: arrays are called system variables; distinguished names associated with functions are called system functions. • Array, • Function, • Monadic operator, • Writing names • Dyadic operator, When writing a linear sequence of names, a primitive operation name need never be separated from adjacent • Assignment arrow, names.