Towards a Uniform Relational Semantics for Tabular Expressions Jules Desharnais Departement d'informatique Universite Laval Quebec, QC, G1K 7P4 Canada
[email protected]
Ridha Khedri Communications Research Laboratory McMaster University 1280 Main Street West Hamilton, ON, L8S 4K1 Canada
[email protected] Ali Mili Institute for Software Research 1000 Technology Drive Fairmont, WV, 26554 USA
[email protected]
1 Introduction Parnas et al. 4, 5, 6] have proposed tabular expressions as a means to represent the complex relations that are used to specify or document software systems. The idea is that a tabular expression is much easier to understand and verify than a long linear formula. Tabular expressions are intended to supplement, not replace, notations used by engineers. They were found to be useful for describing large mathematical relations in practical applications. A semantics of tabular expressions, generalizing previous work and introducing new types of tables, is given in 4]. We show here how a simple and powerful algebra of arrays of relations can be used to further generalize this semantics for some of the types of tabular expressions considered in 4]. This opens up the door to new types of tabular expressions and, more importantly, brings a set of algebraic laws for the manipulation of tables. The algebra we present is based on the known 7, 8] but little used fact that matrices of relations are themselves relations to which the usual relational operators can be applied. In addition, other array operators 2, 3] can be applied to them. For simplicity, in this short text, we restrict ourselves to zero-, one- and two-dimensional arrays.
2 An algebra of arrays of relations Our notation for relations and relational operators is that of 1]. The notation for arrays and array operators is inspired by that of the programming language APL 2, 3]. Let R be a (homogeneous or heterogeneous) relation algebra. An array over R is simply an array whose entries are elements of R. An array can have an arbitrary number of dimensions n 0. In this abstract we will consider 0-dimensional arrays: these are called scalars. In our context, a scalar is a relation. This research is supported by NSERC (Natural Sciences and Engineering Research Council of Canada) and by FCAR (Fonds pour la Formation de Chercheurs et l'Aide a la Recherche, Quebec).
1
1-dimensional arrays: these are called vectors (of relations) or tuples. For instance, the array ( P Q R ) is a vector. 2-dimensional arrays: these are called matrices. The size of an array is a vector giving the number of components along each coordinate. For instance, P Q R size(R) = ( ) size ( P Q R ) = ( 3 ) and size S T U = ( 2 3 ) : If v is a vector and M is a matrix, then v i] denotes the i-th element of v and M i j ] denotes the element in row i and column j of M . We now dene relational operations on arrays. Complementation, join, meet, composition and converse are dened as follows. Let A B L M and N be arrays over R such that size(A) = size(B ) = l, size(L) = size(M ) = ( l m ) and size(N ) = ( m n ). For all i j k such that 1 i l 1 j m 1 k n,
Ai] = Ai] (A B )i] = Ai] B i] (A \ B )i] = Ai] \ B i]
M i j ] = M i j ] (L M )i j ] = Li j ] M i j ] (L \ M )i j ] = Li j ] \ M i j ] M ` i j ] = (M i j ])` S (M N )i k] = mj=1 M i j ] N j k]
Note that if R is a heterogeneous algebra, the operations on relations must be correctly typed for the operations on arrays to be dened. In addition to these standard operations 8], we dene the following ones. Let A B M and N be arrays such that size(A) = l, size(B ) = m, size(M ) = ( l m ), size(N ) = ( m n ), and let R be a relation. Also, let and denote any of the binary relational operators ( \ ). For all i j k such that 1 i l 1 j m 1 k n, (A B )i j ] = Ai] B j ] L (M N )i k] = mj=1 M i j ] N j k] =A = Lli=1 Ai] L (=M )i] = mj=1 M i j ] L (={M )j ] = li=1 M i j ] (R M )i j ] = R M i j ] (M R)i j ] = M i j ] R (A 1] M )i j ] = Ai] M i j ] (M 1] A)i j ] = M i j ] Ai] (B 2] M )i j ] = B j ] M i j ] (M 2] B )i j ] = M i j ] B j ]
outer product of vectors inner product of matrices reduction of a vector column-reduction of a matrix row-reduction of a matrix relation-matrix binary operation matrix-relation binary operation vector-matrix binary operation matrix-vector binary operation vector-matrix binary operation matrix-vector binary operation
The notation for the inner and outer products is that of APL. One can see that M N = M N . For a generalization of these operations to higher-dimensional arrays, see 2]. 2
The ve standard operators satisfy the usual relational laws. As for the other operators, we only mention the following laws:
A 1] M = M 1] A B 2] M = M 2] B (A B ) M = (A 1] (B 2] M ))
A \1] M = M \1] A B \2] M = M \2] B (A \ B ) \ M = (A \1] (B \2] M ))
3 Tabular expressions To illustrate how the array operators introduced in the previous section can be used to give the semantics of tabular expressions, we briey present some of the examples of 4]. See Figures 1, 2 and 3. A tabular expression consists of a set of declarations (the left table in the gures) together with a set of arrays consisting of n vectors, called headers, and an n-dimensional array, called a grid (given on the right in the gures). The cells of the arrays with a single-line border are called guard cells those of the arrays with a double-line border are called value cells. The declaration table gives the types of the input and output variables, the CCG (Cell Connection Graph), which shows how to read the tabular expression, but seems to be redundant in the examples presented here, the guard predicate PT , showing how to combine the guard cells, the relation predicate rT , showing how to combine the value cells to obtain a relation, the name of the function or relation dened by the tabular expression, the expression CT of the function or relation dened by the tabular expression. This is a combination of the table elements, such as fij in Figure 1. There is one table element for each cell of the grid the value of the table element indexed by i j is PT (i j ) ^ rT (i j ). For instance, in Figure 1, f13 = H1 1] ^ H2 3] ^ G1 3], where G1 3] is understood to be f (x y ) = ;y 2 . The operator used in Figures 2 and 3 is a generalization of intersection (necessary to combine heterogeneous relations with dierent output variables) it may simply be thought of as the conjunction of predicates. The notation GH1=#], in Figure 2, means to substitute terms of the header H1 for the place holder # in the corresponding grid cells for example, from H11] and G1 2], one obtains x1 x2 20. To use our array operators, we have to explain how each entry of the headers or grids can be viewed as a relation. We discuss one way of doing it. Predicates dening guards, such as those of H1 and H2 in Figure 1 or those of G in Figure 3 are interpreted by right-ideal relations, also called vectors 8] (a right ideal r is a relation satisfying r = r V , where V is a universal element). For instance, the predicate x 0, in the header H1 of Figure 1, can be assigned the semantics f((x y ) f ) j x 0g. The semantics of a term t(x1 : : : xn ) is the relation f((x1 : : : xn) t(x1 : : : xn))g. The semantics of a predicate relating the input and output variables is the relation that it naturally denes thus, the predicate y22 + x = 1, in Figure 3, denes the relation f(x (y1 y2)) j y22 + x = 1g. A relation can also be associated to predicates with place holders for instance, the predicate # < 20, in Figure 2, denes the relation f(x y ) j x y 2 R ^ x < 20g. 3
Input variables x y 2 R Output variables f 2 R
CCG PT rT
Function name
CT
-? H1 ^ H2 G f S2 S3 i=1
H2 y = 10 y > 10 y < 10 0 y2 ;y 2 x x+y x;y G 8 0 if x 0 ^ y = 10 > > > x if x < 0 ^ y = 10 > > < y2 0 ^ y > 10 f (x y ) = > ;y 2 ifif xx 0 ^ y < 10 > > > y if x < 0 ^ y > 10 : xx + ; y if x < 0 ^ y < 10 H1 x0 x 2 x1x2 if x1 x2 = 2
Figure 2: A generalized decision table
Input variables x 2 R Output variables y1 y2 2 R
CCG PT rT
Relation name
CT
6 G H1 H2
S3 S2 i=1
j =1 ij
H1 y22 + x = 1 y22 + x = 0 y2 = x2
y1 = 0
H2
y1 = 1
x=0 0 > > ;1 ^ y1 = 0 ^ y22 + x = 0) > < __ ((xx < 2 , > _ (0 2 > _ ( ; 1 x < 0 ^ y = 1 ^ y 1 2 + x = 0) > : _ (x > 1 ^ y1 = 1 ^ y2 = x2)
Figure 3: A relation table with the value cells in the headers 4
Once the semantics of the expressions appearing in the headers and grids is xed, the relations corresponding to the tabular expressions can be given. For the three examples considered here, these expressions are = ={((H1 \ H2) \ G) for Figures 1 and 3, and =(H2 \ \ ={(H1 1]G)) for Figure 2. They can be transformed using algebraic laws, for instance to yield dierent evaluation orders.
4 Conclusion It can be seen that the tabular expressions are those relational array expressions with n vectors and one n-dimensional array (the grid). Since there are many more possible array expressions, this may hint at other types of tabular expressions, for instance tables with more than one header for a given dimension, or tables with more than one grid. Of course, whether these are useful in practice is a dierent matter. We have not explored yet all the types of tables discussed in 4, 5, 6] and this is a subject for future research.
References 1] C. Brink, W. Kahl, and G. Schmidt, editors. Relational Methods in Computer Science. Springer, 1997. 2] L. Gilman and A. J. Rose. APL: An Interactive Approach. John Wiley & Sons, New York, 2nd edition, 1976. 3] K. E. Iverson. A Programming Language. John Wiley & Sons, New York, 1962. 4] R. Janicki. On a formal semantics of tabular expressions. CRL Report 355, Communications Research Laboratory, Faculty of Engineering, McMaster University, Hamilton, ON, L8S 4K1 Canada, Oct. 1997. Available at http://www.crl.mcmaster.ca/SERG/serg.publications.html. 5] R. Janicki, D. L. Parnas, and J. Zucker. Tabular representations in relational documents. In Brink et al. 1], chapter 12, pages 184{196. 6] D. L. Parnas. Tabular representation of relations. CRL Report 260, Communications Research Laboratory, Faculty of Engineering, McMaster University, Hamilton, ON, L8S 4K1, Canada, October 1992. 7] G. Schmidt. Programs as partial graphs I: Flow equivalence and correctness. Theoret. Comput. Sci., 15:1{25, 1981. 8] G. Schmidt and T. Strohlein. Relations and Graphs, Discrete Mathematics for Computer Scientists. EATCS-Monographs on Theoretical Computer Science. Springer, 1993. 5