Equivalent Transformation by Safe Extension of Data Structures (Extended Abstract) Kiyoshi Akama1 and Hidekatsu Koike2 and Hiroshi Mabuchi3 Hokkaido University, Kita 11, Nishi 5, Kita-ku, Sapporo, 060-0811, Japan,
[email protected], 2 Hokkaido University, Kita 11, Nishi 5, Kita-ku, Sapporo, 060-0811, Japan,
[email protected], 3 Iwate Prefectural University, 152-52 Sugo, Takizawa, Iwate, 020-0173 Japan, 1
[email protected] Abstract. Equivalent transformation has been proposed as a method-
ology for providing programs with appropriate data structures. For instance, logic programs which use lists are transformed into equivalent programs that use dierence-lists. However lists and dierence-lists are both usual terms and in this sense no new data structures are introduced in the transformation. Since logic programming has xed data structure called terms, no one can develop theoretical foundations for introducing new data structures into programs as far as only logic programs are discussed. In this paper we develop a theoretical foundation of equivalent transformation that introduces new data structures. We introduce a parameter ; for data structures, by which many languages with dierent data structures are characterized. By changing this parameter (say from ;1 to ;2 ) we can discuss data structure change for programs. We de ne a concept of safe extension of data structures, and prove that the meaning of a program on a data structure is preserved by safe extension of the data structure.
1 Introduction Equivalent transformation is one of the most important methods for improving e ciency of programs 9]. Declarative programs such as logic and functional programs may be improved into more e cient ones by using unfolding, folding, goal replacement, tupling, and other transformations. It is well known that e ciency of computation depends on data structures. In case of procedural programming languages, it is taken for granted to adopt better data structures for e cient computation 10]. Data structures are also important in logic and functional computation model. Equivalent transformation has been proposed as a methodology for providing programs with appropriate data structures. For instance, several methods for the transformation of logic programs which use lists into equivalent and more
2
e cient programs that use dierence-lists have been proposed in the literature 5, 8, 11]. However, it should be noted that lists and dierence-lists are included in usual terms and in that sense no new data structures are introduced in the transformation. Theoretical foundation of such transformation is exactly the same as usual equivalent transformation of programs by unfolding, folding, goal replacement, and other transformation techniques. Extension of data structures in this paper is totally dierent. While the above methods transform logic programs into other logic programs with data structure unchanged in the sense that they both use terms, in our theory new data structures that are not included in the original domain are introduced in the transformation. Typical examples for such new data structures are class variables and domain variables, which are not included in usual term domain. Expressive power and e ciency is improved by using class variables based on sort hierarchy 1, 2]. Many constraint satisfaction problems cannot be solved within practical time in Prolog, while constraint logic programs with domain variables solve them far more e ciently than Prolog 6]. In this paper we develop a theoretical foundation of equivalent transformation that introduces new data structures, based on which we can often make programs more e cient. For instance, in the case of class-variables, improvement of e ciency of programs is obtained by the following equivalent transformations: 1. (ET1 ) introduction of class variables, 2. (ET2 ) transformation of programs using class variables. In the case of domain-variables, programs are similarly improved by the following two steps: 1. (ET1 ) introduction of domain variables, 2. (ET2 ) transformation of programs using domain variables. In both cases, introduction of new data structures (ET1 ) is essential to further transformation (ET2 ). The main purpose of the paper is to formalize the rst equivalent transformation (ET1 ). Since logic programming has a xed data structure called terms, no one can develop theoretical foundations for introducing new data structures into programs as far as only logic programs are discussed. It is crucial to introduce a parameter ; for many data structures, by which many languages with dierent data structures are characterized. Mathematical structure called a specialization system is introduced as a parameter for specifying data structures in problem description. Declarative descriptions are also introduced on specialization systems. A declarative description d on a specialization system ; is associated with its meaning M(; d). Equivalent transformation is a transformation of (; d) preserving M(; d). Then, we have two typical equivalent transformations for a pair (; d) of a declarative description d and a specialization system ;: 1. ET1 : (;1 d) ! (;2 d) Change from ;1 into ;2 with d unchanged,
3
2. ET2 : (; d1 ) ! (; d2 )
Change from d1 into d2 with ; unchanged.
We dene extension and safe extension of specialization systems to formulate introduction of new data structures. We also prove the correctness of equivalent transformation by safe extension of specialization systems, i.e., M(;1 d) = M(;2 d) for any ;1 and ;2 such that ;2 is a safe extension of ;1 .
2 Eciency Improvement by Class Variables 2.1 A Vehicle Problem In order to discuss improvement of e ciency by introducing new data structures, consider a sample problem : Bicycles and cars are vehicles. There are two bicycles \bike1" and \bike2." There are three cars \car1", \car2", and \mycar." Vehicles have tires. Cars have doors. I own \mycar." Find all objects that have tires and doors and that I own.
2.2 Formalization by a Logic Program The above problem is formalized easily by a logic program PL and a query qL as follows:
PL = f vehicle(X ) bicycle(X ). vehicle(X ) car(X ). bicycle(bike1) . bicycle(bike2) . car(car1) . car(car2) . car(mycar) . has(Z tires) vehicle(Z ). has(Z doors) car(Z ). own(i mycar) . answer(Y ) has(Y tires), has(Y doors), own(i Y ). g. qL = answer(Z ).
2.3 Formalization by a Sorted Logic Program Since the sample problem is related to concepts such as vehicles, cars, and bicycles, it is natural to formalize it on the basis of a concept hierarchy. One such formalization follows:
4
PS = f vehicle bicycle j car bicycle 3 bike1 j bike2 car 3 car1 j car2 j mycar has(A : vehicle tires) . has(B : car doors) . own(i mycar) . answer(Y ) has(Y tires), has(Y doors), own(i Y ). g. qS = answer(Z ). In the program PS , \vehicle", \bicycle", and \car" are classes, and \bike1",
\bike2", \car1", \car2", and \mycar" are instances. Capital letters such as Y and Z are variables. The expressions \A : vehicle" and \B : car" are sorted terms (class variables), representing a vehicle A and a car B , respectively. The sorted program PS consists of three declarations and four clauses. The knowledge of concept hierarchy is represented more compactly by declarations in PS than by clauses in PL . The four clauses in PS are also more concise than the corresponding clauses in PL due to the use of class variables.
2.4 Towards a Common Theoretical Foundation
We already know that the second program PS is more e cient than the rst one PL to answer the same queries (qL and qS ) 1, 2]. When given the rst program PL , we want to transform PL into PS for e cient computation. Two questions now arise: How is PL transformed into PS ? How is the transformation justied? In this paper, such questions will be solved in a general setting. In the previous subsections, two formalizations, (PL , qL) and (PS , qS ), have been discussed separately without common theoretical foundation. In the subsequent sections, we will introduce a general framework, where the two formalizations in the previous subsections will be re-formalized in a single framework. This is essential to establish e ciency improvement by equivalent transformation.
3 Specialization Systems 3.1 Denition of Specialization Systems Denition 1. A specialization system is a four-tuple hA G S i of three sets A, G and S , and a mapping : S ! partial map(A) that satises the following
requirements, where partial map(X ) is the set of all partial mappings on X . 1. 8s1 s2 2 S 9s 2 S : (s) = (s2 ) (s1 ). 2. 9s 2 S 8a 2 A : (s)(a) = a. 3. G A. Elements of A are called atoms. Elements of G are called ground atoms. Elements of S are called specializations.
5
When there is no danger of confusion, elements in S are regarded as partial mappings over A and the following notational convention is used. Each element in S is identied as a partial mapping on A, and the application of such a partial mapping is represented by postx notation. For example, 2 S and ()(a) are denoted respectively by 2 S and a.
3.2 Examples of Specialization Systems Example B A s-term is either a constant such as bike2 and a pure variable 1 such as X . A s-atom is an atom of the form p(t t t ), where p is a 1 2 n predicate and each ti is a s-term. Let Ab be the set of all s-atoms. Let Gb be the set of all ground (variable free) s-atoms. A binding transforms an atom in Ab by replacing variables with s-terms. Let Sb be the set of all sequences of bindings. An element s in Sb transforms an atom in Ab into another atom in Ab by successive application of bindings in s, which denes the mapping b : Sb ! map(Ab ), where partial map(X ) is the set of all mappings on X . Then ;b = hAb Gb Sb b i is a specialization system. Example D A c-term is either a constant such as bike1, a pure variable such as Y , or a class variable such as B : vehicle. A class variable is a pair of a tag such as B and a class such as vehicle. Pure variables and class variables are called simply variables. A c-atom is an atom of the form p(t1 t2 tn ), where p is a predicate and each ti is a c-term. Let Ad be the set of all c-atoms. Let Gd be the
set of all ground (variable free) c-atoms. Four kinds of basic specializations are introduced a variable substitution such as (X mycar), a tag renaming such as (B C ), a class restriction such as (C bicycle), and a tag substitution such as (B car2). A pure variable is specialized by a variable substitution. For example, Z is specialized to X : car by (Z X : car). A class variable is specialized by a tag renaming. For instance, B : car is specialized to C : car by (B C ). A class variable is specialized by reducing its class into its subclass. B : vehicle is specialized to B : car by (B car). A class variable is specialized to a constant in its class. For instance, B : car is specialized to mycar by (B mycar). A basic specialization transforms an atom in Ad into another atom in Ad . Let Sd be the set of all sequences of basic specializations. An element s in Sd transforms an atom in Ad by successive application of basic specializations in s, which denes a mapping d : Sd ! partial map(Ad ). Then ;d = hAd Gd Sd d i is a specialization system. Note that, among these four types of basic specializations, the last two basic specializations may not be applicable to some objects. For instance, basic specialization (B bicycle) cannot be applied to B : car. This is because bicycle is not a subclass of car. Similarly, basic specialization (B car1) cannot be applied to B : bicycle since car1 is not an element of bicycle. 1
A pure variable is an ordinary variable, but we use this term to distinguish it from a class variable.
6
4 Declarative Description 4.1 Declarative Description Denition 2. Let ; be a specialization system hA G S i. A denite clause on ; is a formula of the form: H B1 B2 Bn where H B1 B2 Bn are atoms in A. A declarative description on ; is a set of denite clauses on ; .
Let C be a denite clause on X . The head of a clause C is denoted by head(C ), and the set of all atoms in the body of C is denoted by body(C ). A specialization s 2 S is applicable to a 2 A i there exists b 2 A such that (s)(a) = b. When 2 S is applicable to H B1 B2 Bn , a denite clause C = (H B1 B2 Bn ) is obtained from a denite clause C = (H B1 B2 Bn ). A denite clause C is an instance of C i there is a specialization such that C = C. A denite clause C is ground i it consists of only ground atoms. A ground instance of a denite clause C is a ground denite clause that is an instance of C . Let P be a declarative description on ; . The set of all ground instances of denite clauses in P is denoted by Gclause(P ). Example 1. PL is a declarative description on ;b . PS is a declarative description on ;d . 0
0
4.2 Meaning of a Declarative Description For a declarative description P , the meaning M(P ) is dened by M(P ) = S T ]n ( ), 1
n=0 P
where TP is a mapping on the powerset 2 , which mapps a subset of G into another subset of G : TP (x) = fhead(C ) j body(C ) x C 2 Gclause(P )g. Since Gclause(P ) and TP depend on the specialization system ; , M(P ) also depends on ; . Hereafter when ; should be specied explicitly, M(P ) will be denoted by M(; P ). G
5 Increase of Meaning by Extension We consider two specialization systems ;1 and ;2 , and discuss the relationship between the two meanings of a declarative description on the two specialization systems.
5.1 Extension of Specialization Systems Consider a mapping : S ! partial map(Ad). The mapping determines a subset of S A A uniquely by 0
7
= f(s a b) j s 2 S a 2 A b 2 A, (s)(a) = bg. 0
It is obvious that the mapping that determines from is one-to-one. In this paper will be identied with , thus will be regarded as a subset of SAA, which is convenient for discussion of the relation between two specialization systems. 0
0
Denition 3. Let ;1 and ;2 be specialization systems: ;1 = hA1 G1 S1 1 i, ;2 = hA2 G2 S2 2 i. ;2 is an extension of ;1 (or ;1 is a partial specialization system of ;2 ), i A1 A2 , G1 G2 , S1 S2 , and 1 2 . Note that 1 and 2 are regarded, respectively, as subsets of S1 A1 A1 and S2 A2 A2 , and that 1 2 i any elements ( a b) in 1 are included also in 2 . Example 2. ;d is an extension of ;b .
5.2 Inclusion Relation of Meaning The following theorem states that the meaning of a declarative description on a specialization system increases by extension of the specialization system.
Theorem 1. Assume that ;2 is an extension of ;1 . If P is a declarative description on ;1 , then P is also a declarative description on ;2 , and M(;1 P ) M(;2 P ).
6 Preservation of Meaning by Safe Extension 6.1 Safe Extension Denition 4. Let ;1 and ;2 be specialization systems: ;1 = hA1 G1 S1 1 i, ;2 = hA2 G2 S2 2 i. ;2 is a safe extension of ;1 i the following conditions are satised. 1. ;2 is an extension of ;1 . 2. G1 = G2 = G . 3. For any nite subset X of A1 and for any specialization in S2 such that X G , there is a specialization in S1 such that x = x for any x 2 X . Example 3. ;d is a safe extension of ;b .
8
6.2 Preservation of Meaning
The following theorem states that the meaning of a declarative description on a specialization system is preserved by safe extension of the specialization system. Theorem 2. Safe-extension Theorem] Assume that ;2 is a safe extension of ;1 . If P is a declarative description on ;1 , then P is also a declarative description on ;2 and M(;1 P ) = M(;2 P ).
7 Application of the Theorem Based on the safe-extension theorem, we discuss the equivalence between the declarative description PL on the specialization system ;b and the declarative description PS on the specialization system ;d. Since ;d is a safe extension of ;b , we have, by theorem 2, M(;b PL) = M(;d PL ). The three predicates vehicle, bicycle, and car, which are dened in the declarative description PL on the specialization system ;d , can be represented more compactly by the following declarative description PM , when we use the structure of the specialization system ;d . PM = f vehicle(A : vehicle) . bicycle(A : bicycle) . car(A : car) . has(Z tires) vehicle(Z ). has(Z doors) car(Z ). own(i mycar) . answer(Y ) has(Y tires), has(Y doors), own(i Y ). g Then, we have M(;d PL ) = M(;d PM ). Moreover, when we remove the body of has clause in the declarative description PM on the specialization system ;d by unfolding, and delete three clauses, (vehicle, bicycle, and car clauses), we obtain a declarative description PS on the specialization system ;d and M(;d PM ) ; H = M(;d PS ), where, H is the meaning of the declarative description consisting of the three clauses vehicle, bicycle, and car. Thus we have M(;b PL) ; H = M(;d PS ). Therefore, fg j answer(g) 2 M(;b PL )g and fg j answer(g) 2 M(;d PS )g are the same, and the representation change from 2.2 to 2.3 can be justied by the theory in this paper.
9
8
Conclusion
Each theory in computer science has been usually discussed on one speci c language with xed data structure. Necessity of uni ed theoretical foundation for many languages with dierent data structures has not been widely recognized. In this paper, we rst de ned specialization systems and declarative descriptions on specialization systems. A problem was formalized as a pair of a specialization system and a declarative description. The concept of specialization systems characterizes data structures for many languages, and is essential to develop a theory for introducing new data structures into problem descriptions. If we change a specialization system ;1 into ;2 with a declarative description d left unchanged, (;1 d) is transformed into (;2 d). If ;2 is a safe extension of ;1 , the transformation from (;1 d) to (;2 d) is an equivalent transformation, i.e., (;1 d) = (;2 d). This theory can be applied to many examples of eciency improvement by introducing new data structures, including class-variable examples and domain-variable examples.
M
M
References 1. At-Kaci, H. and Nasr, R., LOGIN: A Logic Programming Language with Built-In Inheritance, The Journal of Logic Programming, 3 (1986). 2. Akama, K., PAL: An Extended Prolog with Inheritance Hierarchy, information processing society of Japan, Vol.28 No.4 pp.27-34 (1987). 3. Akama, K., Nomura, Y., and Miyamoto, E., Semantic Interpretation of Natural Languages by Program Transformation, Computer Software, Vol.12, No.5, pp.45{ 62 (1995). 4. Akama, K., Shigeta, Y., and Miyamoto, E., A Framework of Problem Solving by Equivalent Transformation of Logic Program, J. Japan Soc. Artif. Intell., Vol.12, No.2, pp.90{99 (1997). 5. Brough, D. R. and Hogger, C. J., Compiling Associativity into Logic Programs, The Journal of Logic Programming, Vol.4, pp.345{359 (1987). 6. Hentenryck, V., Constraint Satisfaction in Logic Programming, The MIT Press (1989). 7. Lloyd, J. W., Foundations of Logic Programming, Second edition, Springer-Verlag (1987). 8. Marriot, K. and Sondergaad, H., Dierence-List Transformation for Prolog, New Generation Computing, Vol.11, pp.125{177 (1993). 9. Pettorossi, A. and Proietti, M., Transformation of Logic Programs: Foundations and Techniques, The Journal of Logic Programming, Vol.19/20, 1994, pp.261{320. 10. Wirth, N., Algorithms + Data Structures = Programs, Prentice-Hall (1976). 11. Zhang, J. and Grant, P. W., An Automatic Dierence-List Transformation Algorithm for Prolog, Proc. 1988 European Conference on Arti cial Intelligence (ECAI'88), pp.320{325 (1988).