Electronic Workshops in Computing towards an Efficient Implementation of Distributive Programs towards an Efcient Implementation of Distributive P R Ograms

Programs and data in distributive categories must be coded in some way in order to be executed on standard archi-tectures. In the original mathematical description of an IMP(G) interpreter KW933 distributive data were coded as words on a suitable set. We present a new e f cient coding for such data. This coding allows to exploit the known techniques used in run-time environments for object-oriented systems in order to optimize the execution of IMP(G) programs on RAM machines. Moreover, the coding is at the basis of a compiler and bytecode interpreter, which have been described equationally using ASF+SDF.


Introduction
Distributive computability is a categorically based notion of computability stemming from the denition of the language IMP(G), which was introduced by R.F.C. Walters in Wal92, KW93 .For any class of ]basic functions described by a distributive graph, the functions obtained from them using the operators of a distributive category are iterated in order to compute new functions, which form the class of the functions distributively computable over the given class of basic functions i.e., over the given distributive graph.Iteration here is a very general concept which is expressible in any countably extensive category for instance, sets, topological spaces, or sheaves.The rst equivalence results with the class of recursive functions were given in SVW96 , while it was proven in Vig96 that a particular instance of the distributive model yields the functions computable in the BSS model BSS89 .These results suggest that the distributive approach encompasses and generalizes many different particular notions of computability.
The denition of the syntax of an IMP(G) program is given in an abstract form using free distributive categories.However, a n y implementation mapping a distributive program say, o v er Set onto a standard architecture must face a series of problems in the representation and manipulation of distributive data i.e., elements belonging to polynomials of sets.
In KW93 a coding for the data of IMP(G) programs has been proposed which represents an element of a set X derived from the basic sets A, B, . . .a s a w ord, using the fact that X is a ]subset^of (A + B + • • • + I) * .Correspondingly, a specic coding has been given for the terms built using a certain set of operators which come from the structure of a distributive category.The coding transforms a term in a series of rewriting rules, whose application emulates the behaviour of the function described by the term on a given word representing an element of the domain of the function.
In this paper, we propose a new coding for distributive data based on labelled forest.The coding has been used in order to implement the rst IMP(G) compiler and interpreter Vig95a , and has both space and manipulability advantages over the original coding.The paper is structured as follows: Section 2 introduces a particular instantiation of the ideas of distributive computability in the category of sets; all denitions are given here in an elementary form.Then we show h o w t o represent elements belonging to a polynomial of sets by means of certain labelled forests, and how to compile arrows into functions between forests.
This paper does not describe in full detail the compiler and the bytecode interpreter; in fact, it is essentially an extended abstract highlighting the most important points of the implementation; the interested reader can nd a complete and executable description in Vig95a .The ASF+SDF tool Kli93 has been used in order to specify a context-free grammar and a set of equational rewrite rules to this purpose; a thourough discussion of the equations, unfortunately, goes beyond the space available here.However, in the last section we glimpse through the equations in order to get a feeling of the implementation.

An Elementary Denition
In this section we shall give an elementary denition of the operators we shall use in order to build functions in the category of sets.
For any pair of sets X, Y, the sum X + Y is the disjoint union of X and Y, i.e., the set The product X × Y is the cartesian product of X and Y, i.e., the set The empty set ? and the singleton I = { * } are the units of + and × up to isomorphism, i.e., up to bijections of sets; we shall not insist in making such isomorphisms explicit.For any set X, there are unique functions !X : ?→ X X : X → I, called the initial and terminal maps.
We h a v e also corresponding constructions on functions: for any pair of functions f :

and for any pair of functions
Of course, the composition of two functions is still a function.If f : X → Y, g : X → Y we write also Sums and products of sets are related by the distributivity isomorphism which maps x, w , k to x, w, k , with k = 0 o r 1 , a n d w an element of Y or Z , respectively.Moreover, a n y set X has an associated identity map 1 X : X → X. W e shall write X for the map (1 X , 1 X ) : X → X × X.
Theory and Practice of Algebraic Specications ASF+SDF'97 Now, i f w e h a v e a f amily of sets the basic sets, consider all the sets that can be obtained from them and ?, I by repeated application of sums and products these are the derived sets.Given a family of basic functions between derived sets, any function obtained by repeated use of (− | −), (−, −) and composition on the basic functions, injections, projections, identities, initial maps, terminal maps and δ −1 is again a function between derived sets: all functions dened in this way are called derived functions.
Finally, for each triple of sets X, U, Y, and any derived function f : X + U → U + Y such that for each x ∈ X there is an n x such that 1 f n x (x) ∈ Y i.e., the function terminates for each input we dene the function which f computes by iteration, denoted by call X, U, Y, f or call f , if X, U and Y are clear from the context, as Note that if such an n x exists, it is unique, because we cannot iterate f over an element of Y.The class of distributively computable functions is exactly the class of functions of the form call f , with f a derived function.
A fundamental property, called the normal form theorem SVW96 , is that the functions computed by iteration, when added to the family of basic functions, do not change its computational power.In other words, we can equivalently dene the class of distributively computable functions as the class of functions obtained by repeated use of (− | −), (−, −), composition and call − on the basic functions, injections, projections, identities, initial maps, terminal maps and δ −1 .
Note that without loss of generality we can restrict our attention to derived sets built without using ?, and to derived functions whose denition does not use initial maps.This happens because of the strictness of ?, i.e., because if a function X → ?exists then X = ?.It is easy to show that if a derived function f contains an initial map then, once its domain and codomain have been normalized using the rules X + ?= X and X × ?= ?, f can be rewritten without using initial maps unless, of course, f itself is such a map.Since initial maps themselves do not compute anything, we can forget about the empty set and all maps which have it as domain or codomain by strictness these maps are all initial.

A New Efcient Coding for IMP(G) Data
In this section we shall introduce a new coding for IMP(G) data and programs which is particularly suited to implementation.We shall show h o w the elements of a derived set can be described as a suitable forest, and that it is possible to associate inductively to a derived arrow a function on forests.The basic idea behind the coding is that, in analogy with standard programming, products represent structures and sums represent union types.Thus, each time we shall need to represent an element of a sum, we shall tag it with the index of its summand.
When discussing such strictly syntactical issues, two possibilities arise: a w ordy description, which highlights the main ideas and skips the syntactical details, or a very formal development, which usually turns the reader away.
Since the coding has been used in the formal specication of a series of IMP(G) tools, we shall discuss the main mathematical and algorithmical ideas informally; a fully formal even executable!description of the same ideas is given in Vig95a .

The Old Coding
Let D be the sum of all the basic sets plus { * , e}.The length |X| of a derived set X is inductively given by |?| = |I| = 1 and |X +Y | = |X ×Y| = |X|+|Y | if X is seen as a tree, it is just its number of leaves.An element of X is represented b y a w ord over D: i. the only element of I is represented by * ?has no elements, but we assign it the word e; ii. the elements of a basic set are represented by themselves as one-letter words; iii. the elements of X × Y are represented by words uv, and the elements of X + Y by words ue | Y | or e |X| v, where u and v represent elements of X and Y, respectively.The distinguished element e is used in order to tell which components of a derived set ]do hold^a value.For instance, if X = A + B then the elements of X can be elements of A or elements of B; correspondingly, a representation of either of the form ae , or of the form eb. It is immediate to see that the length of the word representing an element of X is |X|.

The New Coding
The new coding we are going to propose is based on ordered forests whose leaves are labelled by elements of the basic sets and whose nodes are labelled by natural numbers.We shall identify the empty forest with the only element of I, the one-node forests with the elements of the basic sets, denote by juxtaposition the ordered concatenation of forests and by n, f the creation of a tree with a root labelled by n ∈ N and having as rst-level children the trees of the forest f .The precise denition and properties of such a structure, the free N-motor on the sum of the basic sets, h a v e been studied in PQ68, V ig91, KV91 .We shall describe the manipulation of forests informally; the discussion can be recasted in term of algebraic operations and initiality in the category of N-motors.
We shall dene our coding on the basis of a normal form for the derived sets, and consider + and × as operators of variable arity something which is allowed by associativity.A at term or object is dened inductively as: i. a basic set; ii. a list of at terms surrounded by ]+(^and ])^, or ]×(^and ]) a + -list and a ×-list, respectively.We confuse again a at term and and its obvious interpretation as a set, calling both of them at objects.Now we dene a normal form obtained by associative merge on the at objects which optimizes the storage requirements.A at object in normal form is inductively dened as: i. a basic set ii. a +-list of length different from 1, whose items are basic sets and ×-lists in normal form; iii. a ×-list of length different from 1, whose items are basic sets and +-lists in normal form.
In other words, at objects in normal form do not contain lists with just one element, and it never happens that a sublist can be merged in the containing one by associativity.
For each derived set or at object X there is exactly one corresponding at object in normal form, obtained by list merging after having applied the mapping ν inductively dened by A simple example of at normal form is given in the following picture: Theory and Practice of Algebraic Specications ASF+SDF'97 We shall denote the normal form of a at object X by X. B y a b use of notation, we shall write X for ν(X) when X is a derived set.Of course, many derived sets or at objects correspond to a normal form.Note also that there is a canonical isomorphism between a derived set X, ν(X) and ν(X).
Now the representation of an element x ∈ X, where X is a at object, is dened as follows: i. if X is a basic set, x represents itself; ii. if X = ×(X 1 X 2 • • • X p ), then x is a tuple x 1 , x 2 , . . ., x p , and if u k represents x k as an element of X k the representation of x as an element of X is u 1 u 2 x is a pair u, k , where 0 ≤ k < p and u represents x as an element of X k , and the representation of x as an element of X is k, u .
Given an element x of X, we shall write x for its representation as a forest following the notation of KW93 ; when X is a derived set, we shall write x for the representation of x as an element of ν(X).It is immediate that Proposition 1 For all x, x ∈ X, x = x implies x = x .
Note that for the sake of notational simplicity we shall write +(X) or ×(X) for the simple object or ×-list in normal form X for the simple object or +-list in normal form X, respectively.The notation is not ambiguous, since there are no +/×-lists in normal form with just one element, and it allows us to describe in one shot as +(X 1 • • • X p ) a +-list, when p > 1, a ×-list or simple object, when p = 1, or the empty set, when p = 0 analogously for Once we reduce to at objects in normal form, it is immediate, for instance, to note that the space necessary for storing n • X n is O(n 2 ) with the old coding and O(n) with the new one.In general, the storage required for the sum of two at objects is the sum of the storage for X and Y in the old case, and the maximum plus one of the storage for X and Y in the new case.

Normalization and Associativity
The new coding we just described has a disadvantage: while the associativity isomorphisms related to the product, i.e., the maps derived from the projections, satisfy x = a(x) for every element x of X, and moreover x i s e xactly the representation of x as an element of ×(X Y Z ) , w e h a v e that if is the analogous isomorphism for the sum and z is an element of Z , z = 1, 1, z but b(z) = 1, z .Moreover, the representation of z as an element of +(X Y Z ) is 2, z .Thus, the associativity isomorphisms of product induce the identity at the representation level, but this does not happen for the sum.
However, there is a simple way of overcoming this problem.For a forest u, let ū denote its unique normal form with respect to the conuent, strongly normalizing rewrite rule j, k, u = j + k, u .
It is straightforward to prove the following Proposition 2 Let X, Y and Z be at objects such that and suppose q, r > 0. Then if x 1 is an element of Y, with representation u 1 in Ȳ, the representation of inj 1 (x 1 ) in X is 0, u 1 ; analogously, i f x 2 is an element of Z , with representation u 2 in Z , the representation of inj 2 (x 2 ) in X is q, u 2 .I f q = 0 or r = 0 the representation of inj 2 (x 2 ) or inj 1 (x 1 ) i s u 2 u 1 , respectively.

Theory and Practice of Algebraic Specications ASF+SDF'97
This proposition has several consequences: rst of all, it explains how to obtain the representation of an element of X as an element of X; second, it denes the effect of injections on representations; third, it allows to reverse the effect of the injections: Proposition 3 With the same notation of Proposition 2, suppose q, r > 1; then an element of Y or Z injected in X is coded as n, u , with 0 ≤ n < q q ≤ n < q + r, respectively.If q = 1 r = 1, respectively its coding as an element of Ȳ is u, otherwise it is n, u n − q, u , respectively.
The categorical result underlying the previous discussion is the coherence theorem for monoidal categories see Mac71 .The corollary of interest here is that if we choose a normal form with respect to associativity of + and ×, there is a canonical way of making the interpretation of the original term and of the normal form isomorphic.
Thus, we shall suppose that all objects are given in at normal form some preprocessing can take care of this fact and that all arrows are precomposed and postcomposed with the canonical isomorphisms from and to the normal forms in practice, we shall work in an equivalent category where + and × happen to be strict.This has the major advantage that all the monoidal isomorphisms associativity and unit axioms will induce identities at the representation level.

Coding IMP(G) Programs
Recall that an IMP(G) program is an arrow in a free distributive category Vig95a .For our purposes, it can be thought of as a term built out of the operations described in Section 2. Our goal is to transform it into an operator on forests, which can be actually computed by a Turing machine.

Coding Simple Functions
We n o w introduce the coding of the simple functions, which is given by stating the effect of the coding on normal form representations.For each basic function a we denote with a the function that a induces on the representations and dened in the obvious way: if u = x then a (u) = a(x) .Moreover, Note that all operations on the right side produce automatically forests in normal form, except for the injections, for which the required normalization is shallow: one just need to check that the rewrite rule j, k, u = j + k, u is applicable at the outermost level.We supposed p, q > 0, for otherwise the injections trivialize to identities or initial maps.
The distributivity isomorphism has a more complex description; Proposition 3 tell us how to ]unpack^the representation of an element of a sum, and, depending on q or r being equal to one, we have the following four cases: Theory and Practice of Algebraic Specications ASF+SDF'97 where we assumed p, q, r > 0 and note that n > 0, and that n > 1 implies p, q = 1, Y 1 = * () and X 1 = +(S 1 • • • S n ).I f p , q or r is equal to zero, δ −1 is the identity or an injection.

Coding Derived Functions
To each operator used in the construction of derived arrows we associate inductively an operator on forests: In the case of the (− | −) operator, as for the distributivity isomorphism, we must take some more care.Suppose and p, q > 0.Then, following Proposition 3 we have The relevant result here is the following Theorem 1 Let f be a derived function.Then f ( x ) = f (x) .
Proof.A straightforward but tedious exercise in structural induction and case-by-case analysis.
The reader should notice that − sends arrows to simple operations of forests.This allows to precompile an arrow into a bytecode which can be easily executed.

Some Remarks on the Implementation
We w ould like to remark that the new coding proposed here can be easily and efciently implemented on a standard computing machine as follows: each tree is represented by a pointer either to some basic data representation, or to a forest tagged with a natural number, and a forest is represented by an array of such pointers.It is not difcult to see that this is the standard way of storing variant records, for instance in object-oriented languages with dynamic typing.Thus, the coding presented here is an essential step towards an efcient implementation of distributive programs on standard machines.
It could be argued that the old coding could be ]compressed^by representing sequences of the special e symbol as tagged natural numbers.Thus, for instance, the representation of an element of n • X n would be of the form e j n x 1 x 2 • • • x n e kn , which requires O(n) space.However, this would impose the application of a very complicated ]packing unpacking mechanism^every time a derived arrow has to be computed.Moreover, w e w ould always need, for instance, O(n) storage for I n against O(1) in the new coding.
In other words, in real implementations the new coding has always a good advantage although in some cases it is asymptotically equivalent.

A Glimpse at the Implementation
The description given in Vig95a of the tools for the manipulation of programs written in IMP(G) has been written using ASF+SDF, a system for developing specications and manipulating rewriting systems created at the University Theory and Practice of Algebraic Specications ASF+SDF'97 of Amsterdam Kli93 .The description, which alternates formal and natural language, is an example of literate programming Knu84 , and has been realized using the tool ToL A T E X VK94 .The type-checker has been presented previously in Vig95b .
The choice of ASF+SDF has the great advantage of making extentions or changes to the syntax of the language very easy, so that one can try, for instance, many different mathematical formalisms e.g., f g, f g or f ; g for functional composition.The main disadvantage is the need of large resources, even for running relatively small programs.However, this is not a big issue, since the main point is the implementation of the algorithmical ideas described in the previous sections.The description given in the ASF+SDF syntax, while being very near to the standard mathematical notation, has the advantage of being executable.A direct implementation in a more efcient language would be straightforward.
The reader interested in understanding every detail of the implementation should have some knowledge of the ASF+SDF system, but for a general understanding of the following examples no particular background is necessary.We just remark that in the following the formal syntax is context-free the rules are written ]from right to left^, i.e., they are seen as functions rather than productions, while the equational part species rewrite rules with equational conditions.]Execution^in the context of ASF+SDF means that a term is reduced using the given equations, thought of as reduction rules directed from left to right.A rule is applied only if its conditions are all satised.
We n o w highlight two pieces of the system, which show h o w the operations of forests are dened, and how their composition is optimized locally.The code has been slightly simplied with respect to Vig95a .
The compilation function − we are going to specify will transform an arrow into a form which can be used by the interpreter.We shall construct inductively a CODE which mimicks the structure of an arrow f , b ut uses as building blocks simple operations on forests called instructions.
The basic codes are those instructions which can be executed independently of the basic arrows chosen.Distinguishing them greatly simplies the rules for compilation and execution.In order to reduce ambiguity, we surround all instructions with braces.
The basic codes described here correspond to the operations on forests used in the inductive denitions of Section 4. The TREE instruction corresponds to the n, − operator, while RIGHTDEL and LEFTDEL delete the rightmost or leftmost n trees of a forest.Note that the code for the distributivity isomorphism requires four integer arguments, which are the p, q, r and n of Section 4.1.

context-free syntax BASIC-ARROW → BASIC-CODE ]NOP^→ BASIC-CODE ]INIT^→ BASIC-CODE ]TERM^→ BASIC-CODE ]DIST] ^INT ],^INT ],^INT ],^INT ] ^→ BASIC-CODE ]TREE] ^INT ] ^→ BASIC-CODE ]RIGHTDEL^] ^INT ] ^→ BASIC-CODE ]LEFTDEL^] ^INT ] ^→ BASIC-CODE ] ^BASIC-CODE ] ^→ CODE ] ^]ITER] , ^CODE ],^INT ],^INT ],^INT ] ^→ CODE
The general strategy of the compiler is to create redundant information in the code, which however avoids special cases during compilation.Then, a simple sequence of peephole optimizations see McK65 handles special cases and reduces redundancy.Such optimizations are obtained by examining small fragments of code, in order to make them more efcient.The rules we apply, which are based on equations true in every distributive category, can in turn activate other rules, until no other rule is applicable.This technique particularly useful in our case because it allows to eliminate a series of trivial maps which would be very cumbersome to manage during compilation on a case-by-case basis.For instance, composition with NOP i.e., with the identity can be eliminated: The most trivialized map is the distributivity isomorphism.If one of its parameters is zero, it reduces to an identity or to an injection.For instance, the rst rule correspond to the case I ×(X + Y) δ −1 − − → I × X + I × Y, where the at normal form for both the domain and the codomain is +(X Y ) , and δ −1 is the identity, while the second rule correspond to the case X × (? + Y) δ −1 − − → X × ?+ X × Y with X = I, where the normal form of the domain is ×(X Y , the normal form of the codomain is +(×(X + ()) × (X Y )) and δ −1 is the second injection.
CODIST1 DIST 0, q, q , n = NOP CODIST2 p = 0 ⇒ DIST p, 0, q , n = TREE 1 CODIST3 p = 0 ⇒ DIST p, q, 0, n = TREE 0 7 Conclusions We h a v e presented a new e f cient coding for distributive data.Moreover, this coding allows to exploit the known techniques used in run-time environments for object-oriented systems in order to optimize the execution of IMP(G) programs on RAM machines, due to the strict similarities with the machine representation of variant records or polymorphic objects.Finally, the coding is at the basis of a compiler and bytecode interpreter, which have been specied equationally.W e feel that these results are important steps towards an efcient implementation mapping of distributive programs on standard architectures.
Theory and Practice of Algebraic Specications ASF+SDF'97 CONOP1 NOP ; γ = γ CONOP2 γ ; NOP = γ Iteration starting from the empty set can only produce the initial map, and if the local state space is empty we can apply the normal form theorem SVW96 .COITER1 ITER, γ , 0, n, p = INIT COITER2 m = 0 ⇒ ITER, γ , m, 0, p = γInitial and terminal maps enjoy the following properties:COINIT INIT ; γ = INIT COTERM γ ; TERM = TERMNow w e h a v e a series of rules which eliminate trivial cases of the (− | −) operator in which the arrow really acting is just one.The two parameters correspond to the integers p and q of Section 4.2.COb1 γ | 0, q γ = γ COb2 γ | p, 0 γ = γ