Docsity

Programming with Structures in Functional Languages: A Fragment of Tecton, Study notes of Functional Programming

Toggle sidebar
bg1
GENERAL
@
ELECTRIC
TECHNICAL INFORMATION
General Electric Company
Corporate Research and Developrnent
SERIES
Schenectady,
New
York
12345
I
MLE
I
GE CLASS
-p
AUTHOR
Kapur, D
Musser, DR
Stepanov, AA
Operators and Algebraic Structures
NO. PAGES
t+
SUBJECT
functional programming
OR16MlM
CoM*NENT
Automation and Contro1 Laboratory
No'
81CRD114
August
1981
CORPORAT E
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 WOIOS
algebraic structures, operators, functional
programming, functional languages, algorithm design,
algorit hmic complexity, verification, specification
INFORMATION PREPARED FOR
Additional Hard or Microfiche Copies
Available from Technical Information Exchange
Bldg. 81 Room A133, Schenectady,
N.Y.
12345
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Programming with Structures in Functional Languages: A Fragment of Tecton and more Study notes Functional Programming in PDF only on Docsity!

G E N E R A L @ E L E C T R I C TECHNICAL INFORMATION

General Electric Company Corporate Research and Developrnent

SERIES

Schenectady, New York 12345 I

M L E I GE CLASS -p

AUTHOR Kapur, D Musser, DR Stepanov, AA Operators and Algebraic Structures (^) NO. PAGES

  • t

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

INFORMATION PREPARED FOR

Additional Hard or Microfiche Copies

Available from

Technical Information Exchange

Bldg. 81 Room A133, Schenectady, N.Y. 12345

OPERATORS A N D ALGEBRAIC STRUCTURES I). Kapur, D.R. Musser, and A.A. Stepanov

1. INTRODUCTION

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

2. STRUCTURES IN TECTON

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:

module( G:abelian group, R :ring; *: R *G - G)

The first member of the list of structures is called the "base structure" of the configuration (G

is the base structure of the configuration for module). In the list of operations we write

*: R *G - G to mean that * is a binary infix operation whose domain is R x G and whose range

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":

create group(S:set; +:S+S - S, inv:S -. S, O: S )

with associativity: x+(y+z) = (x+y)+z,

leftidentity: O + x - x,

leftinverses: inv (x) + x = 0;

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

be explicitly typed. The names of the properties can be omitted; they merely provide an extra

way of referring to the properties.

The enrich construct allows us to add a new structure to the domain of structures by means

of adding new axioms to an existing structure. Its format is:

enrich [into < structure name> 1 with < properties> For example, having created "group" we can introduce a structure "abelian group": enrich group into abelian group with x+y = y+x; As another example, a "group with al1 elements of order 2" can be introduced by:

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 [ 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.

We shall refer to it as the "merge monoid". When we use an operator such as "reduction"

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

huff: s - if s = empty then O

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

relation equallengths: x - for al1 u, v inx, lengthb) = length ( v ) ;

provide sequences of Huffman monoid with

huff l : x - if x = null then O

else if singleton? (x) then head(x) else huff l ( tail (tail (x))

cat < head(x) + head(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

mapall: x - if x = null then null

else <f (head(x)) > cat mapall(tail(x));

but instead we assume "mapall" Isi wlready Implernented In ai way thar peirnh parallel computa-

tion, so that it may be used to implement other parallel operaters, Por example, we may now

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,

REFERENCES

  1. Bourbaki, N., Theory of Sets, Chapter IV, "Structures" and Summary of Resu "Scales of Sets. Structures," Addison-Wesley, 1968. 2. Backus, J., "Can Programming Be Liberated from the von Neumann Style? Style and Its Algebra of Programs," CACM 8 (211, August 1978.

h, Section 8,

A Functional

  1. Burstall, R.M., Goguen, J.A., "Putting Theories Together to Make Specifications," Fifth International Joint Conference on Artificial Intelligence, Cambridge, MA, August 1977.
  2. Falkoff, A.D. and Orth, D.L., "Development of an APL Standard," RC 7542, IBM Tho- mas J. Watson Research Center, Yorktown Heights, NY, February 1979.
  3. Goguen, J.A., Thatcher, J.W., Wagner, E.W., "Initial Algebra Approach to the Specification, Correctness, and Implementation of Abstract Data Types" in Current Trends

in Programming Methodology, Vol. IV, Data Structuring (R.T. Yeh, ed.), Prentice Hall, En-

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. Iverson, K.E., "Operators," TOPLAS 1(2), October 1979.
  2. Jenks, R.D., Trager, B.M., "A Language for Computational Algebra," Proceedings of the 1981 ACM Symposium on Symbolic and Algebraic Computation, Snowbird, August 198 1.
  3. Knuth, D.E. The Art of Computer Programming, Vol. 1, Vol. 3 , Addison-Wesley, 1968,
  4. Musser, D.R., "Abstract Data Types in the AFFIRM System," IEEE TSE 1(6), January

1 1. Nakajima, R., Nakahara, H., Honda, M., "Hierarchical Program Specification and Verification - A Many Sorted Logica1 Approach," preprint RIMS 256, November 1978.

  1. Winkler, F., "A Language for Specifying Algebraic Structures," unpublished manuscript, Fa11 1979.
  2. Zilles, S.N., "An Introduction to Data Algebra," Draft Working Paper, IBM San Jose Research Laboratory, September 1975.
  3. Zippel, R., private communication, March l98 1.