







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!








General Electric Company Corporate Research and Developrnent
Schenectady, New York 12345 I
AUTHOR Kapur, D Musser, DR Stepanov, AA Operators and Algebraic Structures (^) NO. PAGES
SUBJECT functional programming
O R 1 6 M l M CoMNENT Automation and Contro1 Laboratory*
No' 81CRD August 1981
CORPORATE RESEARCH ANO OEVELOPMENT SCHENECTAOV. N.V. SUMMARY Operators in functional languages such as APL and FFP are a useful programming concept. However, this concept cannot be fully exploited in these languages because of certain constraints. It is proposed that an operator should be associated with a structure having the algebraic properties on which the operator's behavior depends. This is illustrated by introducing a language that provides mechanisms for defining struc- tures and operators on them. Using this language, it is possible to describe algorithms abstractly, thus emphasizing the algebraic properties on which the algorithms depend. The role that forma1 representation of mathematical knowledge can play in the development of programs is illustrated through an example. An approach for associating complexity measures with a structure and operators is also suggested. This approach is useful in analyzing the complexity of algorithms in an abstract setting.
KEY W O I O S algebraic structures, operators, functional programming, functional languages, algorithm design, algorit hmic complexity, verification, specification
OPERATORS A N D ALGEBRAIC STRUCTURES I). Kapur, D.R. Musser, and A.A. Stepanov
One of the most important notions which makes functional languages different from conventional programming languages is the notion of operator or functional form. Along with a notion of abstract data types, it constitutes the most interesting development in programming languages since the early sixties. We are going to make some suggestions about further development of these two notions and their possible merger. Our main thesis will be that operators should be defined within the context of the algebraic structures to which they natur- ally belong. We shall describe a fragment of a programming language that supports the descrip- tion of algebraic structures and their operators, and illustrate the role that forma1 representation of mathematical knowledge can play in the development of programs. Operators were systematically introduced into programming by Kenneth Iverson.c7) His APL language is a widely used programming language which contains a rich set of operators. According to Iverson, "an operator is an object which applies to a function or functions to pro- duce a related function" (Ref. 7, p. 161). However, the use of operators in APL is limited by the fact that it is impossible to apply operators to user-defined functions. Let us consider reduction, a commonly used operator in APL. It is monadic, which means that it takes one function as an argument. The reduction operator applied to the function "plus", for instance, produces the "summation" function. Reduction takes as its argument any primitive binary scalar function. Since user-defined functions do not have types, the APL system cannot distinguish between scalar and vector user-defined functions. Another property of the function which the reduction operator must know is the existence of left and right identi- ty elements (Ref. 4, p. 19). Yet there is no way to specify such elements for user-defined func- tions in APL. Another problem with operators in APL is that it is impossible for the user to define new operators, since APL functions do not take functions as their arguments. John Backus in his Forma1 System for Functional Programming (FFP) attempted to resolve both of these problems.(*) The problem of defining the types of user-defined functions is elimi- nated because there is no typing in FFP. Any function^ can^ be applied to any element of the universal domain of objects. The problem of specifying properties of the user-defined functions still remains. For example, the "insert" functional form, which is the FFP analog of the APL reduction operator, utilizes the existence of a unique right identity element, but there is no fa- cility in the language for defining the right identity of a function. The problem of defining a new functional form is resolved by representing functions as ob- jects of the same universal domain, and then defining new functional forms as functions which operate on representations of other functions. But by doing so, FFP loses one of its nove
Manuscript received May 4, 198 1. l
The description of a structure includes its conjiguration, which is the list of structures from which the described structure is built and the list of primitive operations defined on these struc- tures. Examples of configurations are:
is the base structure of the configuration for module). In the list of operations we write
is G. (If * were to be used as a prefix operation, we would write _:R,G_* -. G.) A structure description may also include a set of axioms and theorems known about the structure and a set of secondary operators, defined in terms of the primitive ones. The axioms and theorems may include a description of properties of tne complexity of primitive and secon- dary operations, as will be discussed in Section 4. These parts of the description are introduced by constructs of Tecton which permit creating new structures from existing ones and modifying existing structures. The most important are: create, enrich, inform, pro vide, instantiate, implement, and represent. The .create construct adds a new element to the domain of structures. Its format is:
create < configuration> [with < properties> 1 (Brackets enclose parts of the construct that can be omitted.) For example, if we have a struc- ture "set", then we can add a structure "group":
with associativity: x+(y+z) = (x+y)+z,
By convention, the symbols x,y, and z that appear in the properties are variables that are of the type of the base structure ( S i n this case). When variables of other^ types^ are^ needed,^ they will
way of referring to the properties.
enrich
enrich group with al1 elements of order 2: x + x = 0; The instantiate construct replaces the forma1 parameters of a configuration with actual pa- rameters, giving an instance of one structure within another. Its format is:
instantiate < structure name> of < structure name> [as < name> 1 [
For example, if, in addition to the previously defined structures, we have in the domain of structures the structure of "integers" with the configuration
integers(1:set; +:I+ I- I, negate: I- I, 0:- I )
then we can
instantiate abelian group of integers (S = I, + = +, inv = negate,O = 0); We did not give any name to the structure we just created, so we can reference it only as "abelian group of integers". The inform construct allows us to add new theorems to an existing structure. Its format is: inform < structure name> that < properties> For example, we can add some useful properties to the structure of "group": inform group that x+O = x, x+inv(x) = 0; Note that this does not produce a new structure, since these properties are provable from the axioms of "group". The provide construct allows us to define additional operators on the already defined struc- tures. Its format is: provide < name> with < operator definitions> For example, we can now define a subtraction operator on the "group" structure: provide group with -: x-y - x+inv(y); From now on we can use the subtraction operation with any instance of "group". The implement construct allows us to specify some special ways in which operators can be implemented on those instances of a structure which possess some particular set of properties. Its format is:
Then we need to have something to sort:
create orderable sequences (sequences of totally ordered set);
and a way to distinguish sorted sequences:
create ordered sequences (0rdSeq:subset of orderable sequences such that for al1 u and for al1 x,y in u, x precedes y in u if and only if x < y). Now we can write our first program: provide ordered sequences with merge:x,y - if x = null or y = null then x cat y else if head(x) head(y) then < head(x) > cat merge(tail(x),y) else < h e a d ( ~ )> cat merge(x,tail (Y)); It is easy to see that the null sequence is an identity element for the merge function and that merge is both commutative and associative. Thus we may: inform ordered sequences that merge (nul1,x) = x = merge(x,null), merge (x,y) = merge (y,x), merge (merge (x,y).z) = merge (x,merge (y.2) ); This makes it reasonable to make use of some additional structures: create semigroup (S:set; + :S+S - S) with associativity: x+(y+z) = (x+y)+z ;
create monoid(S:semigroup; 0: -* S ) with O+x = x+O = x ;
enrich monoid into abelian monoid with commutativity: x+y = y + x ; and to introduce an operator reduction: provide sequences of monoid with reduction: x - if x = null then O else head(x) + reduction(tail(x)); Thus there is an instance of an abelian monoid in ordered sequences: instantiate abelian monoid of ordered sequences. as merge(S= OrdSeq, + = merge, O = null) Here we have used the function name "merge" also as the name of the instantiated structure.
defined in the monoid structure, we will denote the corresponding operator in the merge monoid as "reduction of merge". (^) Thus we can use "reduction of merge" on sequences of or- dered sequences to merge them into one. The next step is to recognize that sequences under "cat" also form a monoid: inform sequences that null cat x = x cat null = x , (xcat (ycat z)) = ((xcat y) cat z); instantiate monoid of sequences as cat ( S = sequences, + = cat, O = null) ;
Our purpose with this step is to define an abstraction function for the following representation.
represent orderable sequences as sequences of ordered sequences using seqrep and seqabs: x - reduction of cat(x);
We have only given the representation function a name, "seqrep", without defining it. The reason for this will be seen in a moment, but now we can write our program for sorting as provide orderable sequences with sort: x - reduction of merge(seqrep(x));
This is a generic algorithm for two reasons. One is that different implementations of the reduc- tion operator will give different algorithms, a point we shall study in the next section. The other reason is that we can instantiate "seqrep" in various ways. One possibility is: provide sequences with oneify: x - if x = null then null else CC head(x) >> cat oneify(tail(x)); instantiate sort as sort l (seqrep =oneify); Thus o n e i f y k a,, _a_* ,..., a,,> ) (^) = C< a i > ,C a2> ,..., < a,>>. Another possibility would be a function "runify" that keeps runs of increasing elements in the same sequence; e.g.,
This would be useful when it is known that long runs are likely to exist, i.e., when the input to "sort" is known to be almost sorted.
provide multisets with
else if singleton? (s) then u where u : s else huff( ( s - {u,v}) u { u + v } ) where u, v: s and minimalLength( u,s) and minimallength ( v,s -{ v } ) ; We now see that, since the merge monoid is a Huffman monoid, our sort program will be able to use this implementation. Thus it becomes the well-known merge sort algorithm, whose cost is order ( n log n). With an additional property of the input, we can simplify the Huffman implementation of reduction. provide sequences with
provide sequences of Huffman monoid with
else if singleton? (x) then head(x) else huff l ( tail (tail (x))
implement reduction on sequences of Huffman monoid with equallengthdx) as huff l (x);
This implementation would be used by sortl, since the instance "oneify" of "seqrep" produces sequences satisfying the "equalLengthsW relation. In closing this discussion of optimization, let us now consider briefly how the reduction operator introduced in Section 2 relates to the parallel reduction operator discussed in the Intro- duction. In Section 2, the definition of reduction is given recursively in terms of primitive operations "head" and "tail" on sequences. This permits a simple definition, but also implies a commitment to sequential computation. With a different set of primitive operations on sequences we can express the definition of reduction in a way that naturally implies parallel computation. We will thereby obtain the possibility of parallel computation in al1 applications of reduction that obey the necessary algebraic laws, such as our sort program. Let us suppose that operations on sequences of monoid include an operator "pairs" that takes a sequence < x l , ... , xn> into a sequence of two element sequences << x,,x,>, < x3,x4>, ... > , where the last pair is < xn,O> if n is odd; and that there is a primitive operator "mapall" that applies a function " f" to each element of a sequence, producing the sequence of the results. We could, of course, define these with Tecton, e.g., provide sequences of domain of function f with
else <f (head(x)) > cat mapall(tail(x));
but instead we assume "mapall" Isi wlready Implernented In ai way thar peirnh parallel computa-
provide sequences of monoid with addpair: x - h e a d x ) + head(tail(x)), parallelReduction: x - if x = null then O else if singleton? (x) then head(x) else parallelReduction (mapall of addpair(pairs(x)));
If we now
implement reduction as parallelReduction;
we obtain a parallel version of any algorithm that uses reduction, such as our "sort" program. We note that "mapall" could also have been used to define the function "oneify", which was defined recursively in the previous section.
5. CONCLUSION
By combining the notion of operators as used in languages such as APL and FFP with the ideas of algebraic structures, we have proposed mechanisms to define structures and associate operators with structures in a functional setting. This allows us to describe algorithms abstractly and without committing to any particular mode1 of computation, thus emphasizing the algebraic properties they depend on for their functional behavior. We have also suggested an abstract way to associate complexity measures with a structure and its operators. Below,^ we briefly dis- cuss some topics closely related to the ideas presented in the paper which need further investigation. We used the structures "set", "multiset", and "sequences" for illustrating various language constructs. There is a need to identify other structures useful in describing systems and develop their theory. Like the reduction operator on a monoid, other operators on a monoid and other algebraic structures like group, semi-ring, ring, etc., should be investigated within the proposed language framework. We believe that the proposed language constructs, when used with a library of judiciously chosen structures and operators, can be highly expres- sive and useful in describing complex systems. An important topic not discussed in the paper is the role of computer-assisted theorem proving. in relating various structures and operators and deriving properties about structures and operators, as well as about their complexity. An example is for the computer to assist in checking that the monoid structure can be instantiated into sequences by associating the configuration of the monoid with that of sequences and by deducing the monoid properties from the axioms and theorems of sequences. Another example is to prove, using the com- puter, that the merge monoid discussed in Section 4 is a Huffman monoid by showing that the merge operator is length additive and its cost function is linear. Deducing such information can help in developing efficient implementations of the algorithms. We also need to identify prob- lem domain-independent properties of structures such as univalency, multivalency, consistency,
h, Section 8,
A Functional
glewood Cliffs, NJ, 1978. 6. Guttag, J.V., "Abstract Data Types and the Development of Data Structures," CACM 20 (6), pp. 396-404, June 1977..
1 1. Nakajima, R., Nakahara, H., Honda, M., "Hierarchical Program Specification and Verification - A Many Sorted Logica1 Approach," preprint RIMS 256, November 1978.